예제 #1
0
 @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
 public void onInventoryOpen(final InventoryOpenEvent e) {
   if (e.getView() instanceof LocaleInventory) return;
   if (e.getInventory().getType() != InventoryType.CHEST || !useLocaleInv) {
     Inventory in = e.getInventory();
     Iterator<ItemStack> it = in.iterator();
     String locale = Locale.getPlayerLocale((Player) e.getPlayer());
     try {
       while (it.hasNext()) {
         ItemStack item = it.next();
         if (ItemManager.toRPGItem(item) != null) RPGItem.updateItem(item, locale);
       }
     } catch (ArrayIndexOutOfBoundsException ex) {
       // Fix for the bug with anvils in craftbukkit
     }
   } else if (useLocaleInv) {
     LocaleInventory localeInv = new LocaleInventory((Player) e.getPlayer(), e.getView());
     e.setCancelled(true);
     e.getPlayer().openInventory(localeInv);
     localeInventories.add(localeInv);
   }
 }
  @Override
  public Inventory importInventory(
      ChestOwner chestOwner, WorldGroup worldGroup, BetterEnderChest plugin) throws IOException {
    // Cannot import vanilla chests
    if (chestOwner.isSpecialChest()) {
      return null;
    }

    Player player = chestOwner.getPlayer();
    Inventory betterEnderInventory;
    if (player == null) {

      // Offline, load from file
      File playerDirectory =
          new File(Bukkit.getWorlds().get(0).getWorldFolder().getAbsolutePath() + "/playerdata");

      File playerFile =
          new File(playerDirectory.getAbsolutePath() + "/" + chestOwner.getSaveFileName() + ".dat");
      if (!playerFile.exists()) {
        return null;
      }

      // Load it from the file (mainworld/playerdata/playername.dat)
      betterEnderInventory =
          plugin
              .getNMSHandlers()
              .getSelectedRegistration()
              .loadNBTInventoryFromFile(playerFile, chestOwner, worldGroup, "EnderItems");
      if (betterEnderInventory == null) {
        // Cannot load the inventory from that file, most likely because
        // it is empty
        return null;
      }
    } else {
      // Online, load now
      Inventory vanillaInventory = player.getEnderChest();
      int inventoryRows =
          plugin.getEmptyInventoryProvider().getInventoryRows(chestOwner, vanillaInventory);
      betterEnderInventory =
          plugin
              .getEmptyInventoryProvider()
              .loadEmptyInventory(
                  chestOwner,
                  worldGroup,
                  inventoryRows,
                  plugin.getChestSizes().getDisabledSlots(player));

      // Copy all items
      ListIterator<ItemStack> copyIterator = vanillaInventory.iterator();
      while (copyIterator.hasNext()) {
        int slot = copyIterator.nextIndex();
        ItemStack stack = copyIterator.next();
        if (slot < betterEnderInventory.getSize()) {
          betterEnderInventory.setItem(slot, stack);
        }
      }
    }

    // Check if the inventory is empty
    if (BetterEnderUtils.isInventoryEmpty(betterEnderInventory)) {
      // Empty inventory, return null
      return null;
    } else {
      // Return the inventory
      return betterEnderInventory;
    }
  }