Search in sources :

Example 71 with SubscribeEvent

use of cpw.mods.fml.common.eventhandler.SubscribeEvent in project BetterStorage by copygirl.

the class CommonProxy method onPlayerInteract.

@SubscribeEvent
public void onPlayerInteract(PlayerInteractEvent event) {
    World world = event.entity.worldObj;
    int x = event.x;
    int y = event.y;
    int z = event.z;
    EntityPlayer player = event.entityPlayer;
    ItemStack holding = player.getCurrentEquippedItem();
    Block block = world.getBlock(x, y, z);
    boolean leftClick = (event.action == Action.LEFT_CLICK_BLOCK);
    boolean rightClick = (event.action == Action.RIGHT_CLICK_BLOCK);
    // Interact with attachments.
    if (leftClick || rightClick) {
        IHasAttachments hasAttachments = WorldUtils.get(world, x, y, z, IHasAttachments.class);
        if (hasAttachments != null) {
            EnumAttachmentInteraction interactionType = ((event.action == Action.LEFT_CLICK_BLOCK) ? EnumAttachmentInteraction.attack : EnumAttachmentInteraction.use);
            if (hasAttachments.getAttachments().interact(WorldUtils.rayTrace(player, 1.0F), player, interactionType)) {
                event.useBlock = Result.DENY;
                event.useItem = Result.DENY;
            }
        }
    }
    // Use cauldron to remove color from dyable items
    if (rightClick && (block == Blocks.cauldron)) {
        int metadata = world.getBlockMetadata(x, y, z);
        if (metadata > 0) {
            IDyeableItem dyeable = (((holding != null) && (holding.getItem() instanceof IDyeableItem)) ? (IDyeableItem) holding.getItem() : null);
            if ((dyeable != null) && (dyeable.canDye(holding))) {
                StackUtils.remove(holding, "display", "color");
                world.setBlockMetadataWithNotify(x, y, z, metadata - 1, 2);
                world.func_147453_f(x, y, z, block);
                event.useBlock = Result.DENY;
                event.useItem = Result.DENY;
            }
        }
    }
    // Prevent players from breaking blocks with broken cardboard items.
    if (leftClick && (holding != null) && (holding.getItem() instanceof ICardboardItem) && !ItemCardboardSheet.isEffective(holding))
        event.useItem = Result.DENY;
    // Attach locks to iron doors.
    if (!world.isRemote && BetterStorageTiles.lockableDoor != null && rightClick && block == Blocks.iron_door) {
        MovingObjectPosition target = WorldUtils.rayTrace(player, 1F);
        if (target != null && getIronDoorHightlightBox(player, world, x, y, z, target.hitVec, block) != null) {
            int meta = world.getBlockMetadata(x, y, z);
            boolean isMirrored;
            if (meta >= 8) {
                isMirrored = meta == 9;
                y -= 1;
                meta = world.getBlockMetadata(x, y, z);
            } else
                isMirrored = world.getBlockMetadata(x, y + 1, z) == 9;
            int rotation = meta & 3;
            ForgeDirection orientation = rotation == 0 ? ForgeDirection.WEST : rotation == 1 ? ForgeDirection.NORTH : rotation == 2 ? ForgeDirection.EAST : ForgeDirection.SOUTH;
            orientation = isMirrored ? (orientation == ForgeDirection.WEST ? ForgeDirection.SOUTH : orientation == ForgeDirection.NORTH ? ForgeDirection.WEST : orientation == ForgeDirection.EAST ? ForgeDirection.NORTH : ForgeDirection.EAST) : orientation;
            world.setBlock(x, y, z, BetterStorageTiles.lockableDoor, 0, SetBlockFlag.SEND_TO_CLIENT);
            world.setBlock(x, y + 1, z, BetterStorageTiles.lockableDoor, 8, SetBlockFlag.SEND_TO_CLIENT);
            TileEntityLockableDoor te = WorldUtils.get(world, x, y, z, TileEntityLockableDoor.class);
            te.orientation = orientation;
            te.isOpen = isMirrored;
            te.isMirrored = isMirrored;
            te.setLock(holding);
            player.inventory.setInventorySlotContents(player.inventory.currentItem, null);
        }
    }
    // Prevent eating of slime buckets after capturing them.
    if (preventSlimeBucketUse) {
        event.setCanceled(true);
        preventSlimeBucketUse = false;
    }
}
Also used : IDyeableItem(net.mcft.copy.betterstorage.item.IDyeableItem) World(net.minecraft.world.World) TileEntityLockableDoor(net.mcft.copy.betterstorage.tile.entity.TileEntityLockableDoor) MovingObjectPosition(net.minecraft.util.MovingObjectPosition) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Block(net.minecraft.block.Block) ItemStack(net.minecraft.item.ItemStack) EnumAttachmentInteraction(net.mcft.copy.betterstorage.attachment.EnumAttachmentInteraction) IHasAttachments(net.mcft.copy.betterstorage.attachment.IHasAttachments) ICardboardItem(net.mcft.copy.betterstorage.item.cardboard.ICardboardItem) SubscribeEvent(cpw.mods.fml.common.eventhandler.SubscribeEvent)

Example 72 with SubscribeEvent

use of cpw.mods.fml.common.eventhandler.SubscribeEvent in project BetterStorage by copygirl.

the class CommonProxy method onBreakSpeed.

@SubscribeEvent
public void onBreakSpeed(BreakSpeed event) {
    // Stupid Forge not firing PlayerInteractEvent for left-clicks!
    // This is a workaround to instead make blocks appear unbreakable.
    EntityPlayer player = event.entityPlayer;
    ItemStack holding = player.getCurrentEquippedItem();
    if ((holding != null) && (holding.getItem() instanceof ICardboardItem) && !ItemCardboardSheet.isEffective(holding))
        event.newSpeed = -1;
}
Also used : EntityPlayer(net.minecraft.entity.player.EntityPlayer) ItemStack(net.minecraft.item.ItemStack) ICardboardItem(net.mcft.copy.betterstorage.item.cardboard.ICardboardItem) SubscribeEvent(cpw.mods.fml.common.eventhandler.SubscribeEvent)

Example 73 with SubscribeEvent

use of cpw.mods.fml.common.eventhandler.SubscribeEvent in project BetterStorage by copygirl.

the class BackpackHandler method onEntityJoinWorldEvent.

@SubscribeEvent
public void onEntityJoinWorldEvent(EntityJoinWorldEvent event) {
    if (event.world.isRemote)
        return;
    if (!(event.entity instanceof EntityItem))
        return;
    EntityItem entity = (EntityItem) event.entity;
    ItemStack stack = entity.getDataWatcher().getWatchableObjectItemStack(10);
    if ((stack == null) || !(stack.getItem() instanceof ItemEnderBackpack))
        return;
    event.setCanceled(true);
    for (int i = 0; i < 64; i++) if (TileEnderBackpack.teleportRandomly(entity.worldObj, entity.posX, entity.posY, entity.posZ, (i > 48), stack))
        break;
}
Also used : ItemStack(net.minecraft.item.ItemStack) ItemEnderBackpack(net.mcft.copy.betterstorage.item.ItemEnderBackpack) EntityItem(net.minecraft.entity.item.EntityItem) SubscribeEvent(cpw.mods.fml.common.eventhandler.SubscribeEvent)

Example 74 with SubscribeEvent

use of cpw.mods.fml.common.eventhandler.SubscribeEvent in project BetterStorage by copygirl.

the class BackpackHandler method onLivingDeath.

@SubscribeEvent
public void onLivingDeath(LivingDeathEvent event) {
    // If an entity wearing a backpack dies,
    // try to place it, or drop the items.
    EntityLivingBase entity = event.entityLiving;
    if (entity.worldObj.isRemote)
        return;
    EntityPlayer player = ((entity instanceof EntityPlayer) ? (EntityPlayer) entity : null);
    ItemStack backpack = ItemBackpack.getBackpack(entity);
    if (backpack == null)
        return;
    PropertiesBackpack backpackData = ItemBackpack.getBackpackData(entity);
    if (backpackData.contents == null)
        return;
    boolean keepInventory = entity.worldObj.getGameRules().getGameRuleBooleanValue("keepInventory");
    if ((player != null) && keepInventory) {
        // If keep inventory is on, instead temporarily save the contents
        // to the persistent NBT tag and get them back when the player respawns.
        NBTTagCompound compound = player.getEntityData();
        NBTTagCompound persistent;
        if (!compound.hasKey(EntityPlayer.PERSISTED_NBT_TAG)) {
            persistent = new NBTTagCompound();
            compound.setTag(EntityPlayer.PERSISTED_NBT_TAG, persistent);
        } else
            persistent = compound.getCompoundTag(EntityPlayer.PERSISTED_NBT_TAG);
        ;
        NBTTagCompound backpackCompound = new NBTTagCompound();
        backpackCompound.setInteger("count", backpackData.contents.length);
        backpackCompound.setTag("Items", NbtUtils.writeItems(backpackData.contents));
        if (!ItemBackpack.hasChestplateBackpackEquipped(entity))
            backpackCompound.setTag("Stack", backpack.writeToNBT(new NBTTagCompound()));
        persistent.setTag("Backpack", backpackCompound);
    } else {
        // Attempt to place the backpack as a block instead of dropping the items.
        if (BetterStorage.globalConfig.getBoolean(GlobalConfig.dropBackpackOnDeath)) {
            ForgeDirection orientation = DirectionUtils.getOrientation(entity);
            int recentlyHit = ReflectionUtils.get(EntityLivingBase.class, entity, "field_70718_bc", "recentlyHit");
            boolean despawn = ((player == null) && (recentlyHit <= 0));
            List<BlockCoordinate> coords = new ArrayList<BlockCoordinate>();
            for (int x = -2; x <= 2; x++) for (int z = -2; z <= 2; z++) coords.add(new BlockCoordinate(entity.posX, entity.posY, entity.posZ, x, 0, z));
            // Try to place the backpack on the ground nearby,
            // or look for a ground above or below to place it.
            Collections.sort(coords, blockDistanceComparator);
            while (!coords.isEmpty()) {
                Iterator<BlockCoordinate> iter = coords.iterator();
                while (iter.hasNext()) {
                    BlockCoordinate coord = iter.next();
                    if (ItemBackpack.placeBackpack(entity, player, backpack, coord.x, coord.y, coord.z, 1, orientation, despawn, true)) {
                        ItemBackpack.setBackpack(entity, null, null);
                        return;
                    }
                    boolean replacable = entity.worldObj.getBlock(coord.x, coord.y, coord.z).isReplaceable(entity.worldObj, coord.x, coord.y, coord.z);
                    coord.y += (replacable ? -1 : 1);
                    coord.moved += (replacable ? 1 : 5);
                    if ((coord.y <= 0) || (coord.y > entity.worldObj.getHeight()) || (coord.moved > 24 - coord.distance * 4))
                        iter.remove();
                }
            }
            // If backpack couldn't be placed and isn't equipped as armor, drop it.
            if (backpackData.backpack != null)
                WorldUtils.dropStackFromEntity(entity, backpack, 4.0F);
        }
        for (ItemStack stack : backpackData.contents) WorldUtils.dropStackFromEntity(entity, stack, 4.0F);
        backpackData.contents = null;
    }
}
Also used : EntityLivingBase(net.minecraft.entity.EntityLivingBase) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) ArrayList(java.util.ArrayList) EntityPlayer(net.minecraft.entity.player.EntityPlayer) PropertiesBackpack(net.mcft.copy.betterstorage.misc.PropertiesBackpack) ItemStack(net.minecraft.item.ItemStack) SubscribeEvent(cpw.mods.fml.common.eventhandler.SubscribeEvent)

Example 75 with SubscribeEvent

use of cpw.mods.fml.common.eventhandler.SubscribeEvent in project BetterStorage by copygirl.

the class KeyBindingHandler method onKey.

@SubscribeEvent
public void onKey(KeyInputEvent event) {
    Minecraft mc = Minecraft.getMinecraft();
    EntityPlayer player = mc.thePlayer;
    if (!mc.inGameHasFocus || (player == null))
        return;
    if (backpackOpen.isPressed() && (ItemBackpack.getBackpack(player) != null) && BetterStorage.globalConfig.getBoolean(GlobalConfig.enableBackpackOpen))
        BetterStorage.networkChannel.sendToServer(new PacketBackpackOpen());
    else if (drinkingHelmet.isPressed() && (player.getEquipmentInSlot(EquipmentSlot.HEAD) != null))
        BetterStorage.networkChannel.sendToServer(new PacketDrinkingHelmetUse());
}
Also used : PacketBackpackOpen(net.mcft.copy.betterstorage.network.packet.PacketBackpackOpen) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Minecraft(net.minecraft.client.Minecraft) PacketDrinkingHelmetUse(net.mcft.copy.betterstorage.network.packet.PacketDrinkingHelmetUse) SubscribeEvent(cpw.mods.fml.common.eventhandler.SubscribeEvent)

Aggregations

SubscribeEvent (cpw.mods.fml.common.eventhandler.SubscribeEvent)138 EntityPlayer (net.minecraft.entity.player.EntityPlayer)57 ItemStack (net.minecraft.item.ItemStack)48 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)18 Minecraft (net.minecraft.client.Minecraft)14 EntityLivingBase (net.minecraft.entity.EntityLivingBase)14 World (net.minecraft.world.World)14 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)12 ArrayList (java.util.ArrayList)10 EntityItem (net.minecraft.entity.item.EntityItem)10 SideOnly (cpw.mods.fml.relauncher.SideOnly)9 ChunkPosition (net.minecraft.world.ChunkPosition)9 ChatComponentText (net.minecraft.util.ChatComponentText)8 PlayerPointer (riskyken.armourersWorkshop.common.data.PlayerPointer)8 Block (net.minecraft.block.Block)7 MovingObjectPosition (net.minecraft.util.MovingObjectPosition)6 AffinityData (am2.playerextensions.AffinityData)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 ChunkCoordIntPair (net.minecraft.world.ChunkCoordIntPair)5