Ejemplo n.º 1
0
  @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);
        }
      }
    }
  }
 @Override
 protected boolean set(TileEntityLockable container, String value) {
   container.setLockCode(value.length() == 0 ? LockCode.EMPTY_CODE : new LockCode(value));
   return true;
 }