minions-plus/src/main/java/pw/hamzantal/minionsplus/model/minions/SellerMinion.java

81 lines
2.8 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.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.inventory.ItemStack;
import java.text.DecimalFormat;
import java.util.Set;
import java.util.UUID;
public class SellerMinion extends Minion {
public SellerMinion(final UUID owner, final String ownerName) {
super(
MinionsPlus.getInstance().getConfig().getConfigurationSection("Seller"),
owner,
ownerName
);
}
public SellerMinion(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 getRadius(final int level) {
return getSection().getInt("Levels." + level + ".Radius", 1);
}
@Override
public void tick() {
if (getEnergy() < getEnergyPerUse(getLevel())) return;
final int radius = getRadius(getLevel());
boolean sold = false;
for (int y = getLocation().getBlockY() - radius; y < getLocation().getBlockY() + radius; y++) {
for (int x = getLocation().getBlockX() - radius; x < getLocation().getBlockX() + radius; x++) {
for (int z = getLocation().getBlockZ() - radius; z < getLocation().getBlockZ() + radius; z++) {
final Block b = getLocation().getWorld().getBlockAt(x, y, z);
if (b.getType() != Material.CHEST && b.getType() != Material.TRAPPED_CHEST) continue;
final Chest chest = (Chest) b.getState();
for (int i = 0; i < chest.getInventory().getSize(); i++) {
final ItemStack item = chest.getInventory().getItem(i);
if (item == null || item.getType() == Material.AIR) continue;
try {
double sellPrice = MinionsPlus.getEssentials().getWorth().getPrice(MinionsPlus.getEssentials(), item).doubleValue();
if (sellPrice <= 0) continue;
MinionsPlus.getEcon().depositPlayer(Bukkit.getOfflinePlayer(getOwner()), sellPrice * item.getAmount());
chest.getInventory().setItem(i, new ItemStack(Material.AIR));
sold = true;
} catch(final Exception e) {
Bukkit.getLogger().info("SELLER MINION ERROR: Price not found for " + item.toString());
}
}
}
}
}
if (sold) consumeEnergy(getEnergyPerUse(getLevel()));
}
@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("%radius%", String.valueOf(getRadius(getLevel())));
}
}