@SubscribeEvent public void onBreakBlock(BreakEvent event) { if (event.getWorld().isRemote) return; EntityPlayerMP player = (EntityPlayerMP) event.getPlayer(); TileEntity tileEntity = event.getWorld().getTileEntity(event.getPos()); if (tileEntity instanceof TileEntityLockable) { TileEntityLockable tileEntityLockable = (TileEntityLockable) tileEntity; if (tileEntityLockable.isLocked()) { if (!hasRequiredKey(event.getPlayer(), tileEntityLockable)) { player.addChatComponentMessage( new TextComponentTranslation("text.key.errorOnDestroyBlock", new Object[0]), true); event.setCanceled(true); } } } }
@SubscribeEvent(priority = EventPriority.HIGHEST) public void onPlayerInteract(PlayerInteractEvent.RightClickBlock event) { World world = event.getWorld(); BlockPos pos = event.getPos(); if (world.isRemote) return; TileEntity tileEntity = world.getTileEntity(pos); if (tileEntity instanceof TileEntityLockable) { TileEntityLockable tileEntityLockable = (TileEntityLockable) tileEntity; EntityPlayerMP player = (EntityPlayerMP) event.getEntityPlayer(); ItemStack current = player.getHeldItemMainhand(); if (tileEntityLockable.isLocked()) { if (current != null && current.getItem() == ZeiyoItems.key) { String unlockPass = new StringBuilder(current.getDisplayName()).reverse().toString(); if (tileEntityLockable.getLockCode().getLock().equals(unlockPass)) { tileEntityLockable.setLockCode(new LockCode("")); world.playSound( (EntityPlayer) null, player.posX, player.posY, player.posZ, SoundEvents.UI_BUTTON_CLICK, SoundCategory.BLOCKS, 0.5F, world.rand.nextFloat() * 0.1F + 0.9F); player.addChatComponentMessage( new TextComponentTranslation("text.key.successOnUnlock", new Object[0]), true); } else if (!tileEntityLockable.getLockCode().getLock().equals(current.getDisplayName())) { world.playSound( (EntityPlayer) null, player.posX, player.posY, player.posZ, SoundEvents.UI_BUTTON_CLICK, SoundCategory.BLOCKS, 0.5F, world.rand.nextFloat() * 0.1F + 0.9F); player.addChatComponentMessage( new TextComponentTranslation("text.key.errorOnKey", new Object[0]), true); event.setCanceled(true); } } else { world.playSound( (EntityPlayer) null, player.posX, player.posY, player.posZ, SoundEvents.UI_BUTTON_CLICK, SoundCategory.BLOCKS, 0.5F, world.rand.nextFloat() * 0.1F + 0.9F); player.addChatComponentMessage( new TextComponentTranslation("text.key.errorOnBlock", new Object[0]), true); event.setCanceled(true); } } else { if (current != null && current.getItem() == ZeiyoItems.key) { if (!current .getDisplayName() .equals(I18n.translateToLocal(current.getItem().getUnlocalizedName() + ".name"))) { tileEntityLockable.setLockCode(new LockCode(current.getDisplayName())); world.playSound( (EntityPlayer) null, player.posX, player.posY, player.posZ, SoundEvents.UI_BUTTON_CLICK, SoundCategory.BLOCKS, 0.5F, world.rand.nextFloat() * 0.1F + 0.9F); player.addChatComponentMessage( new TextComponentTranslation("text.key.successOnLock", new Object[0]), true); } else { player.addChatComponentMessage( new TextComponentTranslation("text.key.errorOnKeyName", new Object[0]), true); } event.setCanceled(true); } } } }