Exemple #1
0
 @EventHandler
 public void onInventoryClick(InventoryClickEvent event) {
   Inventory inventory = event.getInventory();
   if (inventory.getHolder() instanceof dNPC) {
     dNPC npc = (dNPC) inventory.getHolder();
     npc.getInventory().setContents(inventory.getContents());
     Equipment equipment = npc.getEquipmentTrait();
     for (int i = 0; i < 5; i++) {
       equipment.set(i, inventory.getItem(i));
     }
   }
 }
  private int findSpace(
      ItemStack item, Map<Integer, StackWithAmount> lookaside, boolean findEmpty) {
    // 1.9.4 and later extend living entity slots beyond 36, but some of these are read-only.
    // We could use .getStorageContents(), but this breaks 1.8.8 compatibility
    int length = (inventory.getHolder() instanceof HumanEntity) ? 36 : inventory.getSize();
    ItemStack[] stacks = Arrays.copyOfRange(inventory.getContents(), 0, length);

    if (item == null) {
      return -1;
    }

    for (int i = 0; i < stacks.length; i++) {
      boolean contains = lookaside.containsKey(i);
      if (findEmpty && !contains) {
        return i;
      } else if (!findEmpty && contains) {
        StackWithAmount compareWith = lookaside.get(i);
        if (compareWith != null) {
          ItemStack compareWithStack = compareWith.getStack();
          if (compareWith.getAmount() < compareWithStack.getMaxStackSize()
              && compareWithStack.isSimilar(item)) {
            return i;
          }
        }
      }
    }

    return -1;
  }
Exemple #3
0
  @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
  public void onMenuClosed(InventoryCloseEvent event) {
    if (event.getPlayer() instanceof Player) {
      Inventory inventory = event.getInventory();

      if (inventory.getHolder() instanceof InventoryMenu) {
        InventoryMenu menu = (InventoryMenu) inventory.getHolder();

        MenuCloseBehaviour menuCloseBehaviour = menu.getMenuCloseBehaviour();

        if (menuCloseBehaviour != null) {
          menuCloseBehaviour.onClose((Player) event.getPlayer());
        }
      }
    }
  }
Exemple #4
0
  @EventHandler(priority = EventPriority.LOWEST)
  public void onMenuItemClicked(InventoryClickEvent event) {
    Inventory inventory = event.getInventory();

    if (inventory.getHolder() instanceof InventoryMenu) {
      InventoryMenu menu = (InventoryMenu) inventory.getHolder();

      if (event.getWhoClicked() instanceof Player) {
        Player player = (Player) event.getWhoClicked();
        int index = event.getRawSlot();

        if (index >= 0 && index < inventory.getSize()) {
          menu.selectItem(player, index, event.isRightClick(), event.isShiftClick());
        }
      }

      event.setCancelled(true);
    }
  }
 @EventHandler
 public void closeOfflineInventory(InventoryCloseEvent e) {
   Inventory i = e.getInventory();
   final InventoryHolder ih = i.getHolder();
   if (!(ih instanceof WorldHolder)) return;
   final WorldHolder wh = (WorldHolder) ih;
   if (wh instanceof OfflineInventoryHolder) {
     this.saveInventory(
         this.plugin.getServer().getOfflinePlayer(wh.getUUID()), wh.getWorld().getName(), i);
   } else if (wh instanceof EnderInventoryHolder) {
     this.saveEnderInventory(
         this.plugin.getServer().getOfflinePlayer(wh.getUUID()), wh.getWorld().getName(), i);
   }
 }
Exemple #6
0
 public void run() {
   if ((new BC7011(
           player.getLocation().getBlock(),
           ((org.bukkit.entity.Vehicle) inventory.getHolder())))
       .setAddress(address, null, this.istrain)) {
     LogUtil.sendSuccess(
         player,
         ByteCart.myPlugin.getConfig().getString("Info.SetAddress") + " " + address);
     LogUtil.sendSuccess(
         player,
         ByteCart.myPlugin.getConfig().getString("Info.GetTTL")
             + AddressFactory.<AddressRouted>getAddress(inventory).getTTL());
   } else
     LogUtil.sendError(
         player, ByteCart.myPlugin.getConfig().getString("Error.SetAddress"));
 }
Exemple #7
0
 public void run() {
   int id = ((StorageMinecart) inventory.getHolder()).getEntityId();
   try {
     UpdaterContentFactory.createRoutingTableExchange(
         inventory, region, level, player, isfullreset, isnew);
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   if (!ByteCartUpdaterMoveListener.isExist()) {
     Listener updatermove = new ByteCartUpdaterMoveListener();
     ByteCart.myPlugin
         .getServer()
         .getPluginManager()
         .registerEvents(updatermove, ByteCart.myPlugin);
     ByteCartUpdaterMoveListener.setExist(true);
   }
   ByteCartUpdaterMoveListener.addUpdater(id);
   LogUtil.sendError(player, ByteCart.myPlugin.getConfig().getString("Info.SetUpdater"));
 }
  public static Inventory clone(Inventory inventory, boolean playerSupport) {
    // Evade
    if (inventory == null) return null;

    // Create
    Inventory ret = null;
    if (inventory instanceof PlayerInventory && playerSupport) {
      ret = MixinInventory.get().createPlayerInventory();
    } else {
      InventoryHolder holder = inventory.getHolder();
      int size = inventory.getSize();
      if (inventory instanceof PlayerInventory) size = SIZE_PLAYER_STORAGE;
      String title = inventory.getTitle();
      ret = MixinInventory.get().createInventory(holder, size, title);
    }

    // Fill
    ItemStack[] all = getContentsAll(inventory);
    all = clone(all);
    setContentsAll(ret, all);

    // Return
    return ret;
  }
 /**
  * Check if the type of an inventory is a chest
  *
  * @param inventory inventory to check
  * @return true if the inventories type is a chest
  */
 public static boolean isChest(Inventory inventory) {
   return isChest(inventory.getHolder());
 }
 /**
  * Gets the <code>BetterEnderInventoryHolder</code> of the given inventory. This method is
  * equivalent to calling <code>(BetterEnderInventoryHolder) inventory.getHolder()</code>.
  *
  * @param inventory The inventory to get the holder of.
  * @return The holder.
  * @throws IllegalArgumentException If the inventory doesn't have a <code>
  *     BetterEnderInventoryHolder</code> as holder.
  */
 public static BetterEnderInventoryHolder of(Inventory inventory) throws IllegalArgumentException {
   Preconditions.checkArgument(
       inventory.getHolder() instanceof BetterEnderInventoryHolder, "not an Ender inventory");
   return (BetterEnderInventoryHolder) inventory.getHolder();
 }
  @Override
  public boolean execute(CommandSender sender, String[] args, Location location) {
    ArrayList<Inventory> inventoryList = new ArrayList<Inventory>();
    if ((args[0].startsWith("@l[") && args[0].endsWith("]"))
        || inventorySubs.containsKey(args[0])) {
      Location invLocation = LogiBlocksMain.parseLocation(args[0], location);
      for (int x = -1; x <= 1; x++) {
        for (int y = -1; y <= 1; y++) {
          for (int z = -1; z <= 1; z++) {
            Block testBlock = invLocation.clone().add(x, y, z).getBlock();
            if (testBlock.getState() instanceof InventoryHolder) {
              inventoryList.add(((InventoryHolder) testBlock.getState()).getInventory());
            }
          }
        }
      }
    } else {
      Player player = Bukkit.getPlayer(args[0]);
      if (player == null) {
        return false;
      }
      inventoryList.add(player.getInventory());
    }
    int subIndex = inventorySubs.containsKey(args[0]) ? 0 : 1;
    if (!inventorySubs.containsKey(args[subIndex])) {
      return false;
    }
    if (inventorySubs.get(args[subIndex]) + subIndex + 1 > args.length) {
      return false;
    }
    for (Inventory inventory : inventoryList) {
      switch (args[subIndex]) {
        case "add":
          for (int i = subIndex + 1; i < args.length; i++) {
            ItemStack item1 = LogiBlocksMain.parseItemStack(args[i]);
            if (item1 == null) {
              continue;
            }
            inventory.addItem(item1);
          }
          break;
        case "remove":
          for (int i = subIndex + 1; i < args.length; i++) {
            ItemStack removeItem = LogiBlocksMain.parseItemStack(args[i]);
            if (removeItem == null) {
              continue;
            }
            for (ItemStack targetItem : inventory.getContents()) {
              if (targetItem == null) {
                continue;
              }
              if (targetItem.isSimilar(removeItem)) {
                if (removeItem.getAmount() > targetItem.getAmount()) {
                  removeItem.setAmount(removeItem.getAmount() - targetItem.getAmount());
                  inventory.remove(removeItem);
                } else {
                  targetItem.setAmount(targetItem.getAmount() - removeItem.getAmount());
                  break;
                }
              }
            }
          }
          break;
        case "removeall":
          for (int i = subIndex + 1; i < args.length; i++) {
            ItemStack removeItem = LogiBlocksMain.parseItemStack(args[i]);
            for (int j = 1; j <= 64; j++) {
              removeItem.setAmount(j);
              inventory.remove(removeItem);
            }
          }
          break;
        case "clear":
          inventory.clear();
          break;
        case "refill":
          for (ItemStack itemStack : inventory.getContents()) {
            if (itemStack == null) {
              continue;
            }
            itemStack.setAmount(
                (args.length > subIndex + 1
                    ? Boolean.parseBoolean(args[subIndex + 1]) ? 64 : itemStack.getMaxStackSize()
                    : itemStack.getMaxStackSize()));
          }
          break;
        case "set":
          int slot = Integer.parseInt(args[subIndex + 1]);
          inventory.setItem(
              slot >= inventory.getSize() ? inventory.getSize() - 1 : slot,
              LogiBlocksMain.parseItemStack(args[subIndex + 2]));
          break;
        case "copy":
          Inventory targetInventory;
          if (args[subIndex + 1].startsWith("@l[") && args[subIndex + 1].endsWith("]")) {
            Block targetBlock =
                LogiBlocksMain.parseLocation(args[subIndex + 1], location).getBlock();
            if (targetBlock.getState() instanceof InventoryHolder) {
              targetInventory = ((InventoryHolder) targetBlock.getState()).getInventory();
            } else {
              return false;
            }
          } else {
            Player player = Bukkit.getPlayer(args[subIndex + 1]);
            if (player == null) {
              return false;
            }
            targetInventory = player.getInventory();
          }
          for (int i = 0; i < inventory.getSize() && i < targetInventory.getSize(); i++) {
            targetInventory.setItem(i, inventory.getItem(i));
          }
          break;
        case "show":
          Player player = Bukkit.getPlayer(args[subIndex + 1]);
          if (player == null) {
            return false;
          }
          if (args.length > subIndex + 2) {
            if (Boolean.parseBoolean(args[subIndex + 2])) {
              Inventory fakeInventory =
                  Bukkit.createInventory(inventory.getHolder(), inventory.getType());
              fakeInventory.setContents(inventory.getContents());
              player.openInventory(fakeInventory);
              return true;
            }
          } else {
            player.openInventory(inventory);
          }
          break;
      }
    }

    return true;
  }