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

66 lines
1.9 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.block.Block;
import org.bukkit.entity.*;
import java.text.DecimalFormat;
import java.util.Set;
import java.util.UUID;
public class BreederMinion extends Minion {
public BreederMinion(final UUID owner, final String ownerName) {
super(
MinionsPlus.getInstance().getConfig().getConfigurationSection("Breeder"),
owner,
ownerName
);
}
public BreederMinion(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 useEnergy = false;
for (Entity e : getStand().getNearbyEntities(radius, radius, radius)) {
if (!(e instanceof Animals)) continue;
final Animals a = (Animals) e;
if (!a.canBreed()) return;
if (!a.isAdult()) return;
a.setBreed(false);
final Animals a2 = (Animals) getLocation().getWorld().spawnEntity(a.getLocation(), a.getType());
a2.setBaby();
useEnergy = true;
}
if (useEnergy) 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())));
}
}