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);
 }
  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);
  }
Beispiel #3
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;
 }
Beispiel #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;
 }