139 lines
4.5 KiB
Java
139 lines
4.5 KiB
Java
package pw.hamzantal.minionsplus.model.minions;
|
|
|
|
import pw.hamzantal.minionsplus.MinionsPlus;
|
|
import pw.hamzantal.minionsplus.model.Minion;
|
|
import pw.hamzantal.minionsplus.util.Replacer;
|
|
import org.bukkit.Location;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.block.Block;
|
|
import org.bukkit.inventory.InventoryHolder;
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import java.text.DecimalFormat;
|
|
import java.util.*;
|
|
|
|
public class MinerMinion extends Minion {
|
|
|
|
private static Set<Material> VALID_BLOCKS = new HashSet<>();
|
|
|
|
static {
|
|
VALID_BLOCKS.add(Material.STONE);
|
|
VALID_BLOCKS.add(Material.COBBLESTONE);
|
|
for (Material mat : Material.values()) {
|
|
if (!mat.name().toLowerCase().contains("ore")) continue;
|
|
VALID_BLOCKS.add(mat);
|
|
}
|
|
}
|
|
|
|
public MinerMinion(final UUID owner, final String ownerName) {
|
|
super(
|
|
MinionsPlus.getInstance().getConfig().getConfigurationSection("Miner"),
|
|
owner,
|
|
ownerName
|
|
);
|
|
}
|
|
|
|
public MinerMinion(final UUID owner, final String ownerName, final Location location, final double energy, final int level, final Set<Block> linkedBlocks) {
|
|
this(owner, ownerName);
|
|
setLocation(location);
|
|
setEnergy(energy);
|
|
setLevel(level);
|
|
setLinkedBlocks(linkedBlocks);
|
|
findStand();
|
|
}
|
|
|
|
public int getBlockLimit(final int level) {
|
|
return getSection().getInt("Levels." + level + ".BlockLimit", 1);
|
|
}
|
|
|
|
@Override
|
|
public void tick() {
|
|
if (getEnergy() < getEnergyPerUse(getLevel())) return;
|
|
// Block block = getLocation().getBlock();
|
|
// for (BlockFace direction : new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) {
|
|
// if (!VALID_BLOCKS.contains(block.getRelative(direction).getType())) continue;
|
|
// block = block.getRelative(direction);
|
|
// final Collection<ItemStack> drops = block.getDrops();
|
|
// float pitch = 0;
|
|
// switch (direction) {
|
|
// case NORTH:
|
|
// pitch = 180;
|
|
// break;
|
|
// case SOUTH:
|
|
// pitch = 360;
|
|
// break;
|
|
// case EAST:
|
|
// pitch = 270;
|
|
// break;
|
|
// case WEST:
|
|
// pitch = 90;
|
|
// break;
|
|
// }
|
|
// val l = getStand().getLocation();
|
|
// l.setYaw(pitch);
|
|
// getStand().teleport(l);
|
|
// block.setType(Material.AIR);
|
|
// if (!drops.isEmpty()) consumeEnergy(getEnergyPerUse(getLevel()));
|
|
//
|
|
// getLinkedBlocks().stream().filter(b -> b.getState() instanceof InventoryHolder).forEachOrdered(b -> {
|
|
// InventoryHolder inv = (InventoryHolder) b.getState();
|
|
// final Collection<ItemStack> remaining = inv.getInventory().addItem(drops.toArray(new ItemStack[]{})).values();
|
|
// drops.clear();
|
|
// drops.addAll(remaining);
|
|
// });
|
|
// if (!drops.isEmpty()) {
|
|
// drops.forEach(item -> getLocation().getWorld().dropItemNaturally(getLocation(), item));
|
|
// }
|
|
// break;
|
|
// }
|
|
|
|
int radius = 1;
|
|
int y = getLocation().getBlockY();
|
|
for (int x = getLocation().getBlockX() - radius; x <= getLocation().getBlockX() + radius; x++) {
|
|
for (int z = getLocation().getBlockZ() - radius; z <= getLocation().getBlockZ() + radius; z++) {
|
|
Block blk = getLocation().getWorld().getBlockAt(x, y, z);
|
|
if (!VALID_BLOCKS.contains(blk.getType())) continue;
|
|
Location l = getStand().getLocation();
|
|
l.setYaw(getNewDirection(x - getLocation().getBlockX(), z - getLocation().getBlockZ()));
|
|
getStand().teleport(l);
|
|
final Collection<ItemStack> drops = blk.getDrops();
|
|
blk.setType(Material.AIR);
|
|
if (!drops.isEmpty()) consumeEnergy(getEnergyPerUse(getLevel()));
|
|
getLinkedBlocks().stream().filter(b -> b.getState() instanceof InventoryHolder).forEachOrdered(b -> {
|
|
InventoryHolder inv = (InventoryHolder) b.getState();
|
|
final Collection<ItemStack> remaining = inv.getInventory().addItem(drops.toArray(new ItemStack[]{})).values();
|
|
drops.clear();
|
|
drops.addAll(remaining);
|
|
});
|
|
if (!drops.isEmpty()) {
|
|
drops.forEach(item -> getLocation().getWorld().dropItemNaturally(getLocation(), item));
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
private float getNewDirection(int x, int y) {
|
|
if (x == 1 && y == -1) return 225;
|
|
if (x == 1 && y == 0) return 270;
|
|
if (x == 1 && y == 1) return 315;
|
|
if (x == 0 && y == -1) return 180;
|
|
if (x == 0 && y == 1) return 0;
|
|
if (x == -1 && y == -1) return 135;
|
|
if (x == -1 && y == 0) return 90;
|
|
if (x == -1 && y == 1) return 45;
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public Replacer getReplacer() {
|
|
final DecimalFormat format = new DecimalFormat("#.#");
|
|
return Replacer.create()
|
|
.add("%level%", String.valueOf(getLevel()))
|
|
.add("%energy%", format.format(getEnergy()))
|
|
.add("%owner%", getOwnerName())
|
|
.add("%blocklimit%", String.valueOf(getBlockLimit(getLevel())));
|
|
}
|
|
|
|
}
|