Search in sources :

Example 1 with RightClickBlock

use of net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickBlock in project SecurityCraft by Geforce132.

the class ForgeEventHandler method onRightClickBlock.

@SubscribeEvent
public void onRightClickBlock(RightClickBlock event) {
    if (!event.getEntityPlayer().worldObj.isRemote) {
        World world = event.getEntityPlayer().worldObj;
        TileEntity tileEntity = event.getEntityPlayer().worldObj.getTileEntity(event.getPos());
        Block block = event.getEntityPlayer().worldObj.getBlockState(event.getPos()).getBlock();
        if (PlayerUtils.isHoldingItem(event.getEntityPlayer(), mod_SecurityCraft.codebreaker) && handleCodebreaking(event)) {
            event.setCanceled(true);
            return;
        }
        if (tileEntity != null && tileEntity instanceof CustomizableSCTE && PlayerUtils.isHoldingItem(event.getEntityPlayer(), mod_SecurityCraft.universalBlockModifier)) {
            event.setCanceled(true);
            if (!((IOwnable) tileEntity).getOwner().isOwner(event.getEntityPlayer())) {
                PlayerUtils.sendMessageToPlayer(event.getEntityPlayer(), I18n.translateToLocal("item.universalBlockModifier.name"), I18n.translateToLocal("messages.notOwned").replace("#", ((TileEntityOwnable) tileEntity).getOwner().getName()), TextFormatting.RED);
                return;
            }
            event.getEntityPlayer().openGui(mod_SecurityCraft.instance, GuiHandler.CUSTOMIZE_BLOCK, world, event.getPos().getX(), event.getPos().getY(), event.getPos().getZ());
            return;
        }
        if (tileEntity instanceof INameable && ((INameable) tileEntity).canBeNamed() && PlayerUtils.isHoldingItem(event.getEntityPlayer(), Items.NAME_TAG) && event.getEntityPlayer().inventory.getCurrentItem().hasDisplayName()) {
            event.setCanceled(true);
            for (String character : new String[] { "(", ")" }) {
                if (event.getEntityPlayer().inventory.getCurrentItem().getDisplayName().contains(character)) {
                    PlayerUtils.sendMessageToPlayer(event.getEntityPlayer(), "Naming", I18n.translateToLocal("messages.naming.error").replace("#n", event.getEntityPlayer().inventory.getCurrentItem().getDisplayName()).replace("#c", character), TextFormatting.RED);
                    return;
                }
            }
            if (((INameable) tileEntity).getCustomName().matches(event.getEntityPlayer().inventory.getCurrentItem().getDisplayName())) {
                PlayerUtils.sendMessageToPlayer(event.getEntityPlayer(), "Naming", I18n.translateToLocal("messages.naming.alreadyMatches").replace("#n", ((INameable) tileEntity).getCustomName()), TextFormatting.RED);
                return;
            }
            event.getEntityPlayer().inventory.getCurrentItem().stackSize--;
            ((INameable) tileEntity).setCustomName(event.getEntityPlayer().inventory.getCurrentItem().getDisplayName());
            return;
        }
        if (tileEntity != null && isOwnableBlock(block, tileEntity) && PlayerUtils.isHoldingItem(event.getEntityPlayer(), mod_SecurityCraft.universalBlockRemover)) {
            event.setCanceled(true);
            if (!((IOwnable) tileEntity).getOwner().isOwner(event.getEntityPlayer())) {
                PlayerUtils.sendMessageToPlayer(event.getEntityPlayer(), I18n.translateToLocal("item.universalBlockRemover.name"), I18n.translateToLocal("messages.notOwned").replace("#", ((TileEntityOwnable) tileEntity).getOwner().getName()), TextFormatting.RED);
                return;
            }
            if (block == mod_SecurityCraft.laserBlock) {
                world.destroyBlock(event.getPos(), true);
                BlockLaserBlock.destroyAdjacentLasers(event.getWorld(), event.getPos().getX(), event.getPos().getY(), event.getPos().getZ());
                event.getEntityPlayer().inventory.getCurrentItem().damageItem(1, event.getEntityPlayer());
            } else {
                world.destroyBlock(event.getPos(), true);
                world.removeTileEntity(event.getPos());
                event.getEntityPlayer().inventory.getCurrentItem().damageItem(1, event.getEntityPlayer());
            }
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) CustomizableSCTE(net.geforcemods.securitycraft.api.CustomizableSCTE) INameable(net.geforcemods.securitycraft.api.INameable) RightClickBlock(net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickBlock) Block(net.minecraft.block.Block) BlockLaserBlock(net.geforcemods.securitycraft.blocks.BlockLaserBlock) IOwnable(net.geforcemods.securitycraft.api.IOwnable) TextComponentString(net.minecraft.util.text.TextComponentString) World(net.minecraft.world.World) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Aggregations

CustomizableSCTE (net.geforcemods.securitycraft.api.CustomizableSCTE)1 INameable (net.geforcemods.securitycraft.api.INameable)1 IOwnable (net.geforcemods.securitycraft.api.IOwnable)1 BlockLaserBlock (net.geforcemods.securitycraft.blocks.BlockLaserBlock)1 Block (net.minecraft.block.Block)1 TileEntity (net.minecraft.tileentity.TileEntity)1 TextComponentString (net.minecraft.util.text.TextComponentString)1 World (net.minecraft.world.World)1 RightClickBlock (net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickBlock)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1