use of net.minecraft.entity.item.EntityArmorStand in project CC-Tweaked by cc-tweaked.
the class TurtleTool method attack.
private TurtleCommandResult attack(final ITurtleAccess turtle, EnumFacing direction, TurtleSide side) {
// Create a fake player, and orient it appropriately
final World world = turtle.getWorld();
final BlockPos position = turtle.getPosition();
final TurtlePlayer turtlePlayer = TurtlePlaceCommand.createPlayer(turtle, position, direction);
// See if there is an entity present
Vec3d turtlePos = new Vec3d(turtlePlayer.posX, turtlePlayer.posY, turtlePlayer.posZ);
Vec3d rayDir = turtlePlayer.getLook(1.0f);
Pair<Entity, Vec3d> hit = WorldUtil.rayTraceEntities(world, turtlePos, rayDir, 1.5);
if (hit != null) {
// Load up the turtle's inventory
ItemStack stackCopy = item.copy();
turtlePlayer.loadInventory(stackCopy);
Entity hitEntity = hit.getKey();
// Fire several events to ensure we have permissions.
if (MinecraftForge.EVENT_BUS.post(new AttackEntityEvent(turtlePlayer, hitEntity)) || !hitEntity.canBeAttackedWithItem()) {
return TurtleCommandResult.failure("Nothing to attack here");
}
TurtleAttackEvent attackEvent = new TurtleAttackEvent(turtle, turtlePlayer, hitEntity, this, side);
if (MinecraftForge.EVENT_BUS.post(attackEvent)) {
return TurtleCommandResult.failure(attackEvent.getFailureMessage());
}
// Start claiming entity drops
DropConsumer.set(hitEntity, turtleDropConsumer(turtle));
// Attack the entity
boolean attacked = false;
if (!hitEntity.hitByEntity(turtlePlayer)) {
float damage = (float) turtlePlayer.getEntityAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).getAttributeValue();
damage *= getDamageMultiplier();
if (damage > 0.0f) {
DamageSource source = DamageSource.causePlayerDamage(turtlePlayer);
if (hitEntity instanceof EntityArmorStand) {
// Special case for armor stands: attack twice to guarantee destroy
hitEntity.attackEntityFrom(source, damage);
if (!hitEntity.isDead) {
hitEntity.attackEntityFrom(source, damage);
}
attacked = true;
} else {
if (hitEntity.attackEntityFrom(source, damage)) {
attacked = true;
}
}
}
}
// Stop claiming drops
stopConsuming(turtle);
// Put everything we collected into the turtles inventory, then return
if (attacked) {
turtlePlayer.unloadInventory(turtle);
return TurtleCommandResult.success();
}
}
return TurtleCommandResult.failure("Nothing to attack here");
}
Aggregations