Search in sources :

Example 36 with EntityArmorStand

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");
}
Also used : Entity(net.minecraft.entity.Entity) TileEntity(net.minecraft.tileentity.TileEntity) TurtlePlayer(dan200.computercraft.shared.turtle.core.TurtlePlayer) DamageSource(net.minecraft.util.DamageSource) EntityArmorStand(net.minecraft.entity.item.EntityArmorStand) World(net.minecraft.world.World) Vec3d(net.minecraft.util.math.Vec3d) AttackEntityEvent(net.minecraftforge.event.entity.player.AttackEntityEvent) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) TurtleAttackEvent(dan200.computercraft.api.turtle.event.TurtleAttackEvent)

Aggregations

EntityArmorStand (net.minecraft.entity.item.EntityArmorStand)36 Entity (net.minecraft.entity.Entity)13 BlockPos (net.minecraft.util.math.BlockPos)8 ArrayList (java.util.ArrayList)7 EntityItemFrame (net.minecraft.entity.item.EntityItemFrame)7 ItemStack (net.minecraft.item.ItemStack)7 EntityFallingBlock (net.minecraft.entity.item.EntityFallingBlock)6 EntityLivingBase (net.minecraft.entity.EntityLivingBase)5 EntityBoat (net.minecraft.entity.item.EntityBoat)5 EntityPig (net.minecraft.entity.passive.EntityPig)5 TileEntity (net.minecraft.tileentity.TileEntity)5 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)5 EntityPlayer (net.minecraft.entity.player.EntityPlayer)4 Location (com.wynntils.core.utils.objects.Location)3 Matcher (java.util.regex.Matcher)3 EntityLightningBolt (net.minecraft.entity.effect.EntityLightningBolt)3 EntityEnderPearl (net.minecraft.entity.item.EntityEnderPearl)3 EntityItem (net.minecraft.entity.item.EntityItem)3 RayTraceResult (net.minecraft.util.math.RayTraceResult)3 Iterator (java.util.Iterator)2