public void openInventory(InventoryView inventory) {
    if (!(getHandle() instanceof EntityPlayer)) return; // TODO: NPC support?
    if (((EntityPlayer) getHandle()).playerConnection == null) return;
    if (getHandle().activeContainer != getHandle().defaultContainer) {
      // fire INVENTORY_CLOSE if one already open
      ((EntityPlayer) getHandle())
          .playerConnection.handleContainerClose(
              new Packet101CloseWindow(getHandle().activeContainer.windowId));
    }
    EntityPlayer player = (EntityPlayer) getHandle();
    Container container;
    if (inventory instanceof CraftInventoryView) {
      container = ((CraftInventoryView) inventory).getHandle();
    } else {
      container = new CraftContainer(inventory, player.nextContainerCounter());
    }

    // Trigger an INVENTORY_OPEN event
    container = CraftEventFactory.callInventoryOpenEvent(player, container);
    if (container == null) {
      return;
    }

    // Now open the window
    InventoryType type = inventory.getType();
    int windowType = CraftContainer.getNotchInventoryType(type);
    String title = inventory.getTitle();
    int size = inventory.getTopInventory().getSize();
    player.playerConnection.sendPacket(
        new Packet100OpenWindow(container.windowId, windowType, title, size, false));
    player.activeContainer = container;
    player.activeContainer.addSlotListener(player);
  }
Example #2
0
 /**
  * Open the menu to the specified player.
  *
  * @param player The {@link Player} the inventory is going to be shown to.
  * @return the {@link Menu} instance.
  */
 public final Menu open(Player player) {
   if (this.hasOpen(player)) {
     // Nope
     return this;
   }
   InventoryView view = player.openInventory(getInventory(player));
   if (!view.getTopInventory().equals(getInventory(player))) {
     throw new IllegalStateException(
         "Failed to open inventory (was the InventoryOpenEvent cancelled?)");
   }
   setMenu(player, this);
   this.onOpen(player);
   this.components
       .keySet()
       .forEach(
           component -> {
             component.onOpen(player);
             component.draw(player);
           });
   return this;
 }
Example #3
0
 public void transferTo(Container other, org.bukkit.craftbukkit.entity.CraftHumanEntity player) {
   InventoryView source = this.getBukkitView(), destination = other.getBukkitView();
   ((CraftInventory) source.getTopInventory()).getInventory().onClose(player);
   ((CraftInventory) source.getBottomInventory()).getInventory().onClose(player);
   ((CraftInventory) destination.getTopInventory()).getInventory().onOpen(player);
   ((CraftInventory) destination.getBottomInventory()).getInventory().onOpen(player);
 }
Example #4
0
 public void refresh(Player player) {
   if (!this.hasOpen(player)) {
     return;
   }
   if (!this.nameProvider.apply(player).equals(getInventory(player).getTitle())) {
     this.byPlayer.remove(player);
     this.nameSwitch = true;
     InventoryView view = player.openInventory(getInventory(player));
     if (!view.getTopInventory().equals(getInventory(player))) {
       throw new IllegalStateException(
           "Failed to open inventory (was the InventoryOpenEvent cancelled?)");
     }
     this.nameSwitch = false;
   }
   this.components
       .keySet()
       .forEach(
           component -> {
             component.onOpen(player);
             component.draw(player);
           });
 }
 public static SlotType getSlotType(InventoryView inventory, int slot) {
   SlotType type = SlotType.CONTAINER;
   if (inventory == null) return type; // Cauldron - modded inventories with no Bukkit wrapper
   if (slot >= 0 && slot < inventory.getTopInventory().getSize()) {
     switch (inventory.getType()) {
       case FURNACE:
         if (slot == 2) {
           type = SlotType.RESULT;
         } else if (slot == 1) {
           type = SlotType.FUEL;
         } else {
           type = SlotType.CRAFTING;
         }
         break;
       case BREWING:
         if (slot == 3) {
           type = SlotType.FUEL;
         } else {
           type = SlotType.CRAFTING;
         }
         break;
       case ENCHANTING:
         type = SlotType.CRAFTING;
         break;
       case WORKBENCH:
       case CRAFTING:
         if (slot == 0) {
           type = SlotType.RESULT;
         } else {
           type = SlotType.CRAFTING;
         }
         break;
       case MERCHANT:
         if (slot == 2) {
           type = SlotType.RESULT;
         } else {
           type = SlotType.CRAFTING;
         }
         break;
       case BEACON:
         type = SlotType.CRAFTING;
         break;
       case ANVIL:
         if (slot == 2) {
           type = SlotType.RESULT;
         } else {
           type = SlotType.CRAFTING;
         }
         break;
       default:
         // Nothing to do, it's a CONTAINER slot
     }
   } else {
     if (slot == -999) {
       type = SlotType.OUTSIDE;
     } else if (inventory.getType() == InventoryType.CRAFTING) {
       if (slot < 9) {
         type = SlotType.ARMOR;
       } else if (slot > 35) {
         type = SlotType.QUICKBAR;
       }
     } else if (slot >= (inventory.countSlots() - 9)) {
       type = SlotType.QUICKBAR;
     }
   }
   return type;
 }
 /**
  * Change the items in an inventory view, will overwrite if any item already exists in the slot
  * indicated by the map key. DOES NOT UPDATE THE VIEW FOR THOSE LOOKING.
  *
  * @param inventoryView view to modify the items of
  * @param inventoryItems map of index -> item entries to set the view to.
  */
 public static void setViewItems(
     InventoryView inventoryView, Map<Integer, ItemStack> inventoryItems) {
   for (Map.Entry<Integer, ItemStack> itemEntry : inventoryItems.entrySet()) {
     inventoryView.setItem(itemEntry.getKey(), itemEntry.getValue());
   }
 }
 /**
  * Set the item at a specific slot in the inventory view. DOES NOT PERFORM AN UPDATE FOR EVERYONE
  * CURRENTLY LOOKING.
  *
  * @param inventoryView inventory view to modify
  * @param itemSlot slot to set the item at
  * @param itemStack item to set in the chosen slot
  */
 public static void setViewItemAtSlot(
     InventoryView inventoryView, int itemSlot, ItemStack itemStack) {
   inventoryView.setItem(itemSlot, itemStack);
 }
  // Handles player interaction with anvil while in or out of creative.
  @SuppressWarnings("deprecation")
  @EventHandler(priority = EventPriority.NORMAL)
  public void anvilFix(final InventoryClickEvent e) {
    if (!e.isCancelled()) {
      HumanEntity ent = e.getWhoClicked();

      // Anvil used with survival
      if (ent instanceof Player) {
        final Player player = (Player) ent;
        Inventory inv = e.getInventory();

        // Anvil used with creative:
        if (player.getGameMode() == GameMode.CREATIVE) {
          if (inv instanceof AnvilInventory) {
            AnvilInventory anvil = (AnvilInventory) inv;
            InventoryView view = e.getView();
            int rawSlot = e.getRawSlot();
            if (rawSlot == view.convertSlot(rawSlot)) {
              if (rawSlot == 2) {
                ItemStack[] items = anvil.getContents();
                ItemStack item1 = items[0];
                ItemStack item2 = items[1];
                if (item1 != null && item2 != null) {
                  int id1 = item1.getTypeId();
                  int id2 = item2.getTypeId();
                  if (id1 != 0 && id2 != 0) {
                    ItemStack item3 = e.getCurrentItem();
                    if (item3 != null) {
                      ItemMeta meta = item3.getItemMeta();
                      if (meta != null) {
                        // Player has to have 60 or more levels to use the creative combine.
                        if (player.getLevel() >= 60) {
                          player.setLevel(player.getLevel() - 60);
                          player.sendMessage(ChatColor.GREEN + "Repair/Combine Successful!");
                          Bukkit.getServer()
                              .getScheduler()
                              .scheduleSyncDelayedTask(
                                  mobfighter,
                                  new Runnable() {
                                    public void run() {
                                      // Bypass being set to survival after using anvil if the
                                      // player has creative immunity.
                                      if (mobfighter
                                          .getConfig()
                                          .getList("Creative Immunity")
                                          .contains(player.getDisplayName())) return;
                                      // Sets the player back to survival once the anvil is used.
                                      e.getWhoClicked().getOpenInventory().close();
                                      player.setGameMode(GameMode.SURVIVAL);
                                    }
                                  },
                                  20 * 2);
                        } // End of creative combine.
                      } // Didn't have enough levels to combine/finishing.
                    } // Item 3 was/wasn't null.
                  } // Item 1 & 2 id's were/weren't null.
                } // Item 1 & 2 was/wasn't null.
              } // End of (rawSlot == 2)
            } // End of convert view
          } // End of anvil inventory
          else {
            // Bypass having the player's inventory closed if they have creative immunity.
            if (mobfighter
                .getConfig()
                .getList("Creative Immunity")
                .contains(player.getDisplayName())) e.setCancelled(false);
            // Closes the player's inventory after the anvil is used.
            else {
              e.setCancelled(true);
              player.closeInventory();
            }
          }
        } // End of anvil with creative
      } // End of anvil with survival
    } // End of !isCancelled
  } // End of anvilFix