Search in sources :

Example 1 with InventoryCreativeEvent

use of org.bukkit.event.inventory.InventoryCreativeEvent in project Glowstone by GlowstoneMC.

the class CreativeItemHandler method handle.

@Override
public void handle(GlowSession session, CreativeItemMessage message) {
    GlowPlayer player = session.getPlayer();
    // CraftBukkit does use a inventory view with both inventories set to the player's inventory
    // for the creative inventory as there is no second inventory (no crafting) visible for the
    // client
    InventoryView view = player.getOpenInventory();
    // only if creative mode; only if default (player) inventory
    if (player.getGameMode() != GameMode.CREATIVE || !GlowInventoryView.isDefault(player.getOpenInventory())) {
        player.kickPlayer(GlowstoneMessages.Kick.CREATIVE_ITEM.get());
        return;
    }
    ItemStack stack = ItemIds.sanitize(message.getItem());
    // clicking outside drops the item
    EventFactory eventFactory = EventFactory.getInstance();
    if (message.getSlot() < 0) {
        InventoryCreativeEvent event = eventFactory.callEvent(new InventoryCreativeEvent(view, SlotType.OUTSIDE, -999, stack));
        if (event.isCancelled()) {
            session.send(new SetWindowSlotMessage(-1, -1, stack));
        } else {
            player.drop(event.getCursor());
        }
        return;
    }
    int viewSlot = message.getSlot();
    // this happens quiet often as the client tends to update the whole inventory at once
    if (Objects.equals(stack, view.getItem(viewSlot))) {
        return;
    }
    GlowInventory inv = player.getInventory();
    int slot = view.convertSlot(viewSlot);
    SlotType type = inv.getSlotType(slot);
    InventoryCreativeEvent event = eventFactory.callEvent(new InventoryCreativeEvent(view, type, viewSlot, stack));
    if (event.isCancelled()) {
        // send original slot to player to prevent async inventories
        player.sendItemChange(viewSlot, view.getItem(viewSlot));
        // don't keep track of player's current item, just give them back what they tried to
        // place
        session.send(new SetWindowSlotMessage(-1, -1, stack));
        return;
    }
    view.setItem(viewSlot, stack);
}
Also used : GlowInventoryView(net.glowstone.inventory.GlowInventoryView) InventoryView(org.bukkit.inventory.InventoryView) InventoryCreativeEvent(org.bukkit.event.inventory.InventoryCreativeEvent) GlowPlayer(net.glowstone.entity.GlowPlayer) SetWindowSlotMessage(net.glowstone.net.message.play.inv.SetWindowSlotMessage) GlowInventory(net.glowstone.inventory.GlowInventory) EventFactory(net.glowstone.EventFactory) ItemStack(org.bukkit.inventory.ItemStack) SlotType(org.bukkit.event.inventory.InventoryType.SlotType)

Aggregations

EventFactory (net.glowstone.EventFactory)1 GlowPlayer (net.glowstone.entity.GlowPlayer)1 GlowInventory (net.glowstone.inventory.GlowInventory)1 GlowInventoryView (net.glowstone.inventory.GlowInventoryView)1 SetWindowSlotMessage (net.glowstone.net.message.play.inv.SetWindowSlotMessage)1 InventoryCreativeEvent (org.bukkit.event.inventory.InventoryCreativeEvent)1 SlotType (org.bukkit.event.inventory.InventoryType.SlotType)1 InventoryView (org.bukkit.inventory.InventoryView)1 ItemStack (org.bukkit.inventory.ItemStack)1