Search in sources :

Example 6 with IEntityOwnable

use of net.minecraft.entity.IEntityOwnable in project EnderIO by SleepyTrousers.

the class ItemSoulVial method itemInteractionForEntity.

@Override
public boolean itemInteractionForEntity(@Nonnull ItemStack item, @Nonnull EntityPlayer player, @Nonnull EntityLivingBase entity, @Nonnull EnumHand hand) {
    if (entity.world.isRemote) {
        return false;
    }
    boolean isCreative = player.capabilities.isCreativeMode;
    if (CapturedMob.containsSoul(item) && !isCreative) {
        return false;
    }
    // first check if that entity can be picked up at all
    CapturedMob capturedMob = CapturedMob.create(entity);
    if (capturedMob == null) {
        if (entity instanceof EntityPlayer) {
            player.sendMessage(Lang.SOUL_VIAL_DENIED_PLAYER.toChatServer());
        } else if (CapturedMob.isBlacklisted(entity)) {
            player.sendMessage(Lang.SOUL_VIAL_DENIED_AALISTED.toChatServer());
        }
        return false;
    }
    // then check for reasons this specific one cannot
    if (entity instanceof IEntityOwnable && ((IEntityOwnable) entity).getOwnerId() != null && !player.equals(((IEntityOwnable) entity).getOwner()) && !PermissionAPI.hasPermission(player.getGameProfile(), permissionPickupOwned, new TargetContext(player, entity))) {
        player.sendMessage(Lang.SOUL_VIAL_DENIED_OWNED_PET.toChatServer());
        return false;
    }
    if (!PermissionAPI.hasPermission(player.getGameProfile(), permissionPickup, new TargetContext(player, entity))) {
        player.sendMessage(Lang.SOUL_VIAL_DENIED.toChatServer());
        return false;
    }
    ItemStack capturedMobVessel = capturedMob.toStack(this, 1, 1);
    player.swingArm(hand);
    if (!isCreative) {
        entity.setDead();
        if (entity.isDead) {
            // So we do not shrink the stack when it is 1.
            if (item.getCount() > 1) {
                item.shrink(1);
                // Since this stack still exists, add the new vial to the first location, or drop
                if (!player.inventory.addItemStackToInventory(capturedMobVessel)) {
                    player.dropItem(capturedMobVessel, false);
                }
            } else {
                // Otherwise, just replace the stack
                player.setHeldItem(hand, capturedMobVessel);
            }
            player.inventoryContainer.detectAndSendChanges();
            return true;
        }
    } else {
        if (!player.inventory.addItemStackToInventory(capturedMobVessel)) {
            player.dropItem(capturedMobVessel, false);
        }
        player.inventoryContainer.detectAndSendChanges();
        return true;
    }
    return false;
}
Also used : CapturedMob(crazypants.enderio.util.CapturedMob) TargetContext(net.minecraftforge.server.permission.context.TargetContext) EntityPlayer(net.minecraft.entity.player.EntityPlayer) IEntityOwnable(net.minecraft.entity.IEntityOwnable) ItemStack(net.minecraft.item.ItemStack)

Aggregations

IEntityOwnable (net.minecraft.entity.IEntityOwnable)6 EntityPlayer (net.minecraft.entity.player.EntityPlayer)4 UUID (java.util.UUID)2 EntityLivingBase (net.minecraft.entity.EntityLivingBase)2 IMixinEntity (org.spongepowered.common.interfaces.entity.IMixinEntity)2 CapturedMob (crazypants.enderio.util.CapturedMob)1 Nullable (javax.annotation.Nullable)1 IMob (net.minecraft.entity.monster.IMob)1 EntityThrowable (net.minecraft.entity.projectile.EntityThrowable)1 ItemStack (net.minecraft.item.ItemStack)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 ReportedException (net.minecraft.util.ReportedException)1 Vec3d (net.minecraft.util.math.Vec3d)1 World (net.minecraft.world.World)1 WorldServer (net.minecraft.world.WorldServer)1 TargetContext (net.minecraftforge.server.permission.context.TargetContext)1 Entity (org.spongepowered.api.entity.Entity)1 User (org.spongepowered.api.entity.living.player.User)1 IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)1