Search in sources :

Example 1 with IRemovable

use of com.builtbroken.mc.api.tile.IRemovable in project Engine by VoltzEngine-Project.

the class InteractionHandler method onPlayerInteract.

@SubscribeEvent
public void onPlayerInteract(PlayerInteractEvent event) {
    if (!event.entityPlayer.worldObj.isRemote && event.entityPlayer != null) {
        Location vec = new Location(event.entityPlayer.worldObj, event.x, event.y, event.z);
        TileEntity tile = vec.getTileEntity();
        if (event.action == Action.RIGHT_CLICK_BLOCK) {
            //Handle IRemovable allow more uniform removal of blocks
            if (tile instanceof IRemovable) {
                boolean do_drop = false;
                List<ItemStack> drops;
                if (tile instanceof ICustomRemoval) {
                    do_drop = ((ICustomRemoval) tile).canBeRemoved(event.entityPlayer);
                } else if (tile instanceof ISneakWrenchable) {
                    do_drop = event.entityPlayer.isSneaking() && WrenchUtility.isHoldingWrench(event.entityPlayer);
                } else if (tile instanceof IWrenchable) {
                    do_drop = WrenchUtility.isHoldingWrench(event.entityPlayer);
                } else if (tile instanceof ISneakPickup) {
                    do_drop = event.entityPlayer.isSneaking() && event.entityPlayer.getHeldItem() == null;
                } else {
                    do_drop = tile instanceof IPickup && event.entityPlayer.getHeldItem() == null;
                }
                if (do_drop) {
                    drops = ((IRemovable) tile).getRemovedItems(event.entityPlayer);
                    //Not sure if we need to cancel but there is nothing to right click after this
                    if (event.isCancelable()) {
                        event.setCanceled(true);
                    }
                    //Drop all items
                    try {
                        vec.world().removeTileEntity(vec.xi(), vec.yi(), vec.zi());
                        vec.setBlock(Blocks.air);
                        if (drops != null && !drops.isEmpty()) {
                            for (ItemStack item : drops) {
                                if (!event.entityPlayer.inventory.addItemStackToInventory(item)) {
                                    InventoryUtility.dropItemStack(vec, item);
                                } else {
                                    event.entityPlayer.inventory.markDirty();
                                }
                            }
                            event.entityPlayer.inventoryContainer.detectAndSendChanges();
                        }
                    } catch (Exception e) {
                        Engine.instance.logger().error("Failed to pick up block using event system");
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IRemovable(com.builtbroken.mc.api.tile.IRemovable) ItemStack(net.minecraft.item.ItemStack) Location(com.builtbroken.mc.imp.transform.vector.Location) SubscribeEvent(cpw.mods.fml.common.eventhandler.SubscribeEvent)

Aggregations

IRemovable (com.builtbroken.mc.api.tile.IRemovable)1 Location (com.builtbroken.mc.imp.transform.vector.Location)1 SubscribeEvent (cpw.mods.fml.common.eventhandler.SubscribeEvent)1 ItemStack (net.minecraft.item.ItemStack)1 TileEntity (net.minecraft.tileentity.TileEntity)1