BlockBreaker is now a Goal/Behavior

This commit is contained in:
fullwall 2013-03-17 22:40:37 +08:00
parent 5a74a02f33
commit 7a030885e8

View File

@ -1,5 +1,7 @@
package net.citizensnpcs.npc.ai; package net.citizensnpcs.npc.ai;
import net.citizensnpcs.api.ai.tree.BehaviorGoalAdapter;
import net.citizensnpcs.api.ai.tree.BehaviorStatus;
import net.citizensnpcs.util.PlayerAnimation; import net.citizensnpcs.util.PlayerAnimation;
import net.minecraft.server.v1_5_R1.Block; import net.minecraft.server.v1_5_R1.Block;
import net.minecraft.server.v1_5_R1.Enchantment; import net.minecraft.server.v1_5_R1.Enchantment;
@ -15,7 +17,8 @@ import org.bukkit.craftbukkit.v1_5_R1.inventory.CraftItemStack;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class BlockBreaker implements Runnable { public class BlockBreaker extends BehaviorGoalAdapter {
private final LivingEntity bukkitEntity;
private final Configuration configuration; private final Configuration configuration;
private int currentDamage; private int currentDamage;
private int currentTick; private int currentTick;
@ -26,6 +29,7 @@ public class BlockBreaker implements Runnable {
private BlockBreaker(LivingEntity entity, org.bukkit.block.Block target, Configuration config) { private BlockBreaker(LivingEntity entity, org.bukkit.block.Block target, Configuration config) {
this.entity = ((CraftLivingEntity) entity).getHandle(); this.entity = ((CraftLivingEntity) entity).getHandle();
this.bukkitEntity = entity;
this.x = target.getX(); this.x = target.getX();
this.y = target.getY(); this.y = target.getY();
this.z = target.getZ(); this.z = target.getZ();
@ -33,15 +37,6 @@ public class BlockBreaker implements Runnable {
this.configuration = config; this.configuration = config;
} }
public void cancel() {
if (configuration.callback() != null) {
configuration.callback().run();
}
isDigging = false;
currentDamage = -1;
entity.world.f(entity.id, x, y, z, -1);
}
private double distanceSquared() { private double distanceSquared() {
return Math.pow(entity.locX - x, 2) + Math.pow(entity.locY - y, 2) + Math.pow(entity.locZ - z, 2); return Math.pow(entity.locX - x, 2) + Math.pow(entity.locY - y, 2) + Math.pow(entity.locZ - z, 2);
} }
@ -69,28 +64,37 @@ public class BlockBreaker implements Runnable {
} }
@Override @Override
public void run() { public void reset() {
if (configuration.callback() != null) {
configuration.callback().run();
}
isDigging = false;
setBlockDamage(currentDamage = -1);
}
@Override
public BehaviorStatus run() {
if (!isDigging) { if (!isDigging) {
cancel(); reset();
return; return BehaviorStatus.SUCCESS;
} }
currentTick = (int) (System.currentTimeMillis() / 50); // CraftBukkit currentTick = (int) (System.currentTimeMillis() / 50); // CraftBukkit
if (configuration.radiusSquared() > 0 && distanceSquared() >= configuration.radiusSquared()) { if (configuration.radiusSquared() > 0 && distanceSquared() >= configuration.radiusSquared()) {
startDigTick = currentTick; startDigTick = currentTick;
return; return BehaviorStatus.RUNNING;
} }
if (entity instanceof EntityPlayer) if (entity instanceof EntityPlayer)
PlayerAnimation.ARM_SWING.play((Player) entity.getBukkitEntity()); PlayerAnimation.ARM_SWING.play((Player) entity.getBukkitEntity());
Block block = Block.byId[entity.world.getTypeId(x, y, z)]; Block block = Block.byId[entity.world.getTypeId(x, y, z)];
if (block == null) { if (block == null) {
cancel(); return BehaviorStatus.SUCCESS;
} else { } else {
int tickDifference = currentTick - startDigTick; int tickDifference = currentTick - startDigTick;
float damage = getStrength(block) * (tickDifference + 1); float damage = getStrength(block) * (tickDifference + 1);
if (damage >= 1F) { if (damage >= 1F) {
entity.world.getWorld().getBlockAt(x, y, z) entity.world.getWorld().getBlockAt(x, y, z)
.breakNaturally(CraftItemStack.asCraftMirror(getCurrentItem())); .breakNaturally(CraftItemStack.asCraftMirror(getCurrentItem()));
cancel(); return BehaviorStatus.SUCCESS;
} }
int modifiedDamage = (int) (damage * 10.0F); int modifiedDamage = (int) (damage * 10.0F);
if (modifiedDamage != currentDamage) { if (modifiedDamage != currentDamage) {
@ -98,12 +102,18 @@ public class BlockBreaker implements Runnable {
currentDamage = modifiedDamage; currentDamage = modifiedDamage;
} }
} }
return BehaviorStatus.RUNNING;
} }
private void setBlockDamage(int modifiedDamage) { private void setBlockDamage(int modifiedDamage) {
entity.world.f(entity.id, x, y, z, modifiedDamage); entity.world.f(entity.id, x, y, z, modifiedDamage);
} }
@Override
public boolean shouldExecute() {
return org.bukkit.Material.getMaterial(entity.world.getTypeId(x, y, z)) != null;
}
private float strengthMod(Block block) { private float strengthMod(Block block) {
ItemStack itemstack = getCurrentItem(); ItemStack itemstack = getCurrentItem();
float strength = itemstack != null ? itemstack.a(block) : 1; float strength = itemstack != null ? itemstack.a(block) : 1;