Search in sources :

Example 26 with Item

use of net.minecraft.server.v1_15_R1.DataWatcher.Item in project CokesAddon by Cokes86.

the class v1_16_R2 method injectPlayer.

public void injectPlayer(Player player) {
    CraftPlayer player1 = (CraftPlayer) player;
    if (!player.isValid())
        return;
    if (channelHandlers.containsKey(player.getUniqueId())) {
        final Pair<CraftPlayer, ChannelOutboundHandlerAdapter> pair = channelHandlers.get(player.getUniqueId());
        if (!pair.getLeft().isValid()) {
            try {
                pair.getLeft().getHandle().playerConnection.networkManager.channel.pipeline().remove(pair.getRight());
            } catch (NoSuchElementException ignored) {
            }
        } else
            return;
    }
    final ChannelOutboundHandlerAdapter handler = new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(ChannelHandlerContext ctx, Object packet, ChannelPromise promise) throws Exception {
            if (packet instanceof PacketPlayOutEntityEquipment) {
                if ((int) ReflectionUtil.FieldUtil.getValue(packet, "a") == getPlayer().getEntityId()) {
                    ReflectionUtil.FieldUtil.setValue(packet, "c", ItemStack.a);
                }
            } else if (packet instanceof PacketPlayOutEntityMetadata) {
                if ((int) ReflectionUtil.FieldUtil.getValue(packet, "a") == getPlayer().getEntityId()) {
                    List<Item<?>> items = ReflectionUtil.FieldUtil.getValue(packet, "b");
                    if (items.size() != 0) {
                        Item<?> item = items.get(0);
                        if (BYTE_DATA_WATCHER_OBJECT.equals(item.a())) {
                            Item<Byte> byteItem = (Item<Byte>) item;
                            byteItem.a((byte) (byteItem.b() | 1 << 5));
                            ((CraftPlayer) getPlayer()).getHandle().setInvisible(true);
                        }
                    }
                }
            }
            super.write(ctx, packet, promise);
        }
    };
    channelHandlers.put(player.getUniqueId(), Pair.of(player1, handler));
    player1.getHandle().playerConnection.networkManager.channel.pipeline().addBefore("packet_handler", hashCode() + ":" + player.getName(), handler);
}
Also used : Item(net.minecraft.server.v1_16_R2.DataWatcher.Item) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) CraftPlayer(org.bukkit.craftbukkit.v1_16_R2.entity.CraftPlayer) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise)

Example 27 with Item

use of net.minecraft.server.v1_15_R1.DataWatcher.Item in project CokesAddon by Cokes86.

the class v1_16_R3 method injectPlayer.

public void injectPlayer(Player player) {
    CraftPlayer player1 = (CraftPlayer) player;
    if (!player.isValid())
        return;
    if (channelHandlers.containsKey(player.getUniqueId())) {
        final Pair<CraftPlayer, ChannelOutboundHandlerAdapter> pair = channelHandlers.get(player.getUniqueId());
        if (!pair.getLeft().isValid()) {
            try {
                pair.getLeft().getHandle().playerConnection.networkManager.channel.pipeline().remove(pair.getRight());
            } catch (NoSuchElementException ignored) {
            }
        } else
            return;
    }
    final ChannelOutboundHandlerAdapter handler = new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(ChannelHandlerContext ctx, Object packet, ChannelPromise promise) throws Exception {
            if (packet instanceof PacketPlayOutEntityEquipment) {
                if ((int) ReflectionUtil.FieldUtil.getValue(packet, "a") == getPlayer().getEntityId()) {
                    ReflectionUtil.FieldUtil.setValue(packet, "c", ItemStack.a);
                }
            } else if (packet instanceof PacketPlayOutEntityMetadata) {
                if ((int) ReflectionUtil.FieldUtil.getValue(packet, "a") == getPlayer().getEntityId()) {
                    List<Item<?>> items = ReflectionUtil.FieldUtil.getValue(packet, "b");
                    if (items.size() != 0) {
                        Item<?> item = items.get(0);
                        if (BYTE_DATA_WATCHER_OBJECT.equals(item.a())) {
                            Item<Byte> byteItem = (Item<Byte>) item;
                            byteItem.a((byte) (byteItem.b() | 1 << 5));
                            ((CraftPlayer) getPlayer()).getHandle().setInvisible(true);
                        }
                    }
                }
            }
            super.write(ctx, packet, promise);
        }
    };
    channelHandlers.put(player.getUniqueId(), Pair.of(player1, handler));
    player1.getHandle().playerConnection.networkManager.channel.pipeline().addBefore("packet_handler", hashCode() + ":" + player.getName(), handler);
}
Also used : Item(net.minecraft.server.v1_16_R3.DataWatcher.Item) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) CraftPlayer(org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise)

Example 28 with Item

use of net.minecraft.server.v1_15_R1.DataWatcher.Item in project MechanicsMain by WeaponMechanics.

the class NonNullList_1_15_R1 method set.

@Override
public ItemStack set(int index, ItemStack newItem) {
    ItemStack oldItem = get(index);
    Item item = (Item) ReflectionUtil.invokeField(itemField, newItem);
    if (newItem.getCount() == 0 && item != null) {
        newItem.setCount(1);
        consumer.accept(CraftItemStack.asBukkitCopy(oldItem), CraftItemStack.asBukkitCopy(newItem), index);
        newItem.setCount(0);
    } else if (!ItemStack.matches(oldItem, newItem)) {
        consumer.accept(CraftItemStack.asBukkitCopy(oldItem), CraftItemStack.asBukkitCopy(newItem), index);
    }
    return super.set(index, newItem);
}
Also used : Item(net.minecraft.server.v1_15_R1.Item) ItemStack(net.minecraft.server.v1_15_R1.ItemStack) CraftItemStack(org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack)

Example 29 with Item

use of net.minecraft.server.v1_15_R1.DataWatcher.Item in project TheAPI by TheDevTec.

the class v1_15_R1 method processInvClickPacket.

@Override
public boolean processInvClickPacket(Player player, HolderGUI gui, Object provPacket) {
    PacketPlayInWindowClick packet = (PacketPlayInWindowClick) provPacket;
    int slot = packet.c();
    if (slot == -999)
        return false;
    int id = packet.b();
    int mouseClick = packet.d();
    net.minecraft.server.v1_15_R1.InventoryClickType nmsType = packet.g();
    InventoryClickType type = InventoryClickType.valueOf(nmsType.name());
    Object container = gui.getContainer(player);
    ItemStack item = asBukkitItem(packet.f());
    if ((type == InventoryClickType.QUICK_MOVE || type == InventoryClickType.CLONE || type == InventoryClickType.THROW || item.getType().isAir()) && item.getType().isAir())
        item = asBukkitItem(getSlotItem(container, slot));
    boolean cancel = false;
    if (InventoryClickType.SWAP == type) {
        item = player.getInventory().getItem(mouseClick);
        mouseClick = 0;
        cancel = true;
    }
    if (item == null)
        item = new ItemStack(Material.AIR);
    ItemStack before = player.getItemOnCursor();
    ClickType clickType = BukkitLoader.buildClick(item, type, slot, mouseClick);
    if (!cancel)
        cancel = BukkitLoader.useItem(player, item, gui, slot, clickType);
    if (!gui.isInsertable())
        cancel = true;
    int gameSlot = slot > gui.size() - 1 ? InventoryUtils.convertToPlayerInvSlot(slot - gui.size()) : slot;
    if (!cancel)
        cancel = gui.onIteractItem(player, item, clickType, gameSlot, slot < gui.size());
    else
        gui.onIteractItem(player, item, clickType, gameSlot, slot < gui.size());
    int position = 0;
    if (!cancel && type == InventoryClickType.QUICK_MOVE) {
        ItemStack[] contents = slot < gui.size() ? player.getInventory().getStorageContents() : gui.getInventory().getStorageContents();
        List<Integer> modified = slot < gui.size() ? InventoryUtils.shift(slot, player, gui, clickType, gui instanceof AnvilGUI ? DestinationType.PLAYER_INV_ANVIL : DestinationType.PLAYER_INV_CUSTOM_INV, null, contents, item) : InventoryUtils.shift(slot, player, gui, clickType, DestinationType.CUSTOM_INV, gui.getNotInterableSlots(player), contents, item);
        if (!modified.isEmpty()) {
            if (slot < gui.size()) {
                boolean canRemove = !modified.contains(-1);
                player.getInventory().setStorageContents(contents);
                if (canRemove) {
                    gui.remove(gameSlot);
                } else {
                    gui.getInventory().setItem(gameSlot, item);
                }
            } else {
                boolean canRemove = !modified.contains(-1);
                gui.getInventory().setStorageContents(contents);
                if (canRemove) {
                    player.getInventory().setItem(gameSlot, null);
                } else {
                    player.getInventory().setItem(gameSlot, item);
                }
            }
        }
        return true;
    }
    if (cancel) {
        // MOUSE
        BukkitLoader.getPacketHandler().send(player, packetSetSlot(-1, -1, asNMSItem(before)));
        switch(type) {
            case CLONE:
                return true;
            case SWAP:
            case QUICK_MOVE:
            case PICKUP_ALL:
                // TOP
                for (ItemStack cItem : gui.getInventory().getContents()) {
                    BukkitLoader.getPacketHandler().send(player, packetSetSlot(id, position++, asNMSItem(cItem)));
                }
                // BUTTON
                player.updateInventory();
                return true;
            default:
                BukkitLoader.getPacketHandler().send(player, packetSetSlot(id, slot, getSlotItem(container, slot)));
                if (gui instanceof AnvilGUI) {
                    // TOP
                    for (ItemStack cItem : gui.getInventory().getContents()) {
                        if (position != slot)
                            BukkitLoader.getPacketHandler().send(player, packetSetSlot(id, position++, asNMSItem(cItem)));
                    }
                    // BUTTON
                    player.updateInventory();
                }
                return true;
        }
    } else {
        if (gui instanceof AnvilGUI && slot == 2)
            postToMainThread(() -> ((ContainerAnvil) container).shiftClick((EntityPlayer) getPlayer(player), slot));
    }
    return false;
}
Also used : AnvilGUI(me.devtec.theapi.bukkit.gui.AnvilGUI) InventoryClickType(me.devtec.theapi.bukkit.BukkitLoader.InventoryClickType) InventoryClickType(me.devtec.theapi.bukkit.BukkitLoader.InventoryClickType) ClickType(me.devtec.theapi.bukkit.gui.GUI.ClickType) ContainerAnvil(net.minecraft.server.v1_15_R1.ContainerAnvil) PacketPlayInWindowClick(net.minecraft.server.v1_15_R1.PacketPlayInWindowClick) ItemStack(org.bukkit.inventory.ItemStack) CraftItemStack(org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack)

Example 30 with Item

use of net.minecraft.server.v1_15_R1.DataWatcher.Item in project PublicCrafters by BananaPuncher714.

the class CustomInventoryCrafting method setItems.

protected void setItems(List<org.bukkit.inventory.ItemStack> items) {
    int index = 0;
    for (org.bukkit.inventory.ItemStack item : items) {
        this.items.set(index++, CraftItemStack.asNMSCopy(item));
    }
    // Want to update the result without having to use a real player
    if (this.resultInventory instanceof InventoryCraftResult) {
        CustomContainerWorkbench container = new CustomContainerWorkbench(0, manager.mockPlayer.getBukkitEntity(), bloc, this, (InventoryCraftResult) resultInventory);
        container.a(this);
        CraftingInventory crafting = (CraftingInventory) container.getBukkitView().getTopInventory();
        Bukkit.getPluginManager().callEvent(new PrepareItemCraftEvent(crafting, container.getBukkitView(), false));
    }
    display.forceUpdate();
}
Also used : CraftingInventory(org.bukkit.inventory.CraftingInventory) PublicCraftingInventory(io.github.bananapuncher714.crafters.implementation.api.PublicCraftingInventory) PrepareItemCraftEvent(org.bukkit.event.inventory.PrepareItemCraftEvent) InventoryCraftResult(net.minecraft.server.v1_15_R1.InventoryCraftResult)

Aggregations

CraftItemStack (org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack)12 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)8 ChannelOutboundHandlerAdapter (io.netty.channel.ChannelOutboundHandlerAdapter)8 ChannelPromise (io.netty.channel.ChannelPromise)8 NBTTagCompound (net.minecraft.server.v1_15_R1.NBTTagCompound)8 ItemStack (org.bukkit.inventory.ItemStack)7 ArrayList (java.util.ArrayList)5 CoreStateInitException (com.solinia.solinia.Exceptions.CoreStateInitException)4 ISoliniaItem (com.solinia.solinia.Interfaces.ISoliniaItem)3 Container (net.minecraft.server.v1_15_R1.Container)3 CraftPlayer (org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer)3 ISoliniaSpell (com.solinia.solinia.Interfaces.ISoliniaSpell)2 Field (java.lang.reflect.Field)2 Timestamp (java.sql.Timestamp)2 UUID (java.util.UUID)2 Nullable (javax.annotation.Nullable)2 ContainerAnvil (net.minecraft.server.v1_15_R1.ContainerAnvil)2 EntityPlayer (net.minecraft.server.v1_15_R1.EntityPlayer)2 Item (net.minecraft.server.v1_15_R1.Item)2 ItemStack (net.minecraft.server.v1_15_R1.ItemStack)2