Exemplo n.º 1
0
  @SubscribeEvent
  public void onInteract(PlayerInteractEvent.RightClickBlock event) {
    // does the player clicks on an echanting table with moss with 5 levels?
    if (ItemStack.areItemsEqual(event.getItemStack(), TinkerCommons.matMoss)) {
      if (event.getWorld().getBlockState(event.getPos()).getBlock() == Blocks.BOOKSHELF) {
        if (event.getEntityPlayer().experienceLevel >= ModMendingMoss.MENDING_MOSS_LEVELS) {
          // convert moss to mending moss
          EntityPlayer player = event.getEntityPlayer();
          player.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1f, 1f);

          if (!event.getWorld().isRemote) {
            event.getItemStack().stackSize--;
            player.removeExperienceLevel(ModMendingMoss.MENDING_MOSS_LEVELS);
            ItemHandlerHelper.giveItemToPlayer(player, TinkerCommons.matMendingMoss.copy());

            event.setUseBlock(Event.Result.DENY);
            event.setUseItem(Event.Result.DENY);
            event.setCanceled(true);
          }
        }
      }
    }
  }
Exemplo n.º 2
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);
        }
      }
    }
  }