Solved Open working Workbench / Crafting table

Discussion in 'Plugin Development' started by TGamingStudio, Apr 27, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    TGamingStudio

    Hello i want to make player open a crafting table .
    i used :
    Code:
    Inventory inv = Bukkit.createInventory(p, InventoryType.CRAFTING);
                     p.openInventory(inv);     
    but it didnt show anything .
    than :
    Code:
    Inventory inv = Bukkit.createInventory(p, InventoryType.WORKBENCH);
                     p.openInventory(inv);     
    that one works . but i cant craft in it and it just pop up for a half second and its gone.
    When i try use custom crafting it doesnt pop up .

    When i close it . Items dont drop . And please how to add items to it ? Like what is id of every Frame (the square) . Thanks

    EDIT :
    oh i have fixed the crafting with
    Code:
    p.openWorkbench(null, true);
    but how i can put items in it ?
     
    Last edited: Apr 29, 2018
  2. Offline

    MightyOne

    I just saw that this method returns an InventoryView. Perhaps p.openWorkbench (...).getTopInventory returns you the workbench field. But inventories are always tricky xD
     
  3. Offline

    KarimAKL

    @TGamingStudio What do you mean "put items in it"? I just tried this:
    Code:Java
    1.  
    2. p.openWorkbench(null, true);
    3.  

    and it works without any errors.
     
  4. Offline

    MightyOne

    @KarimAKL putting items in it means setting items in the different slots of the workbench
     
    Last edited: May 9, 2018
  5. Offline

    KarimAKL

  6. Offline

    MightyOne

    @KarimAKL we are talking here about setting items into the grid with the plugin and not by hand
     
  7. Offline

    KarimAKL

  8. Offline

    TGamingStudio

    @KarimAKL Yes , you are right .
    But i think it can work only with
    Code:
    Inventory inv = Bukkit.createInventory(p, InventoryType.CRAFTING);
    and this one is bugged so i cant craft in it.
    but there is easy to put items in .
     
  9. Offline

    KarimAKL

  10. Offline

    DanielTheDev

    @TGamingStudio
    @KarimAKL
    Here, I wrote this code to create an easy util for the Nms inventory.
    The only thing you need to do is getting inventory events with the defaut events and synchronizing the id.

    Here is the CustomWorkBench code:
    Code:
    package com.plugin.npc;
    
    
    
    import java.io.IOException;
    
    import org.bukkit.Material;
    import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
    import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack;
    import net.minecraft.server.v1_12_R1.BlockPosition;
    import net.minecraft.server.v1_12_R1.ChatMessage;
    import net.minecraft.server.v1_12_R1.Container;
    import net.minecraft.server.v1_12_R1.ContainerWorkbench;
    import net.minecraft.server.v1_12_R1.EntityHuman;
    import net.minecraft.server.v1_12_R1.EntityPlayer;
    import net.minecraft.server.v1_12_R1.ICrafting;
    import net.minecraft.server.v1_12_R1.IInventory;
    import net.minecraft.server.v1_12_R1.NonNullList;
    import net.minecraft.server.v1_12_R1.PacketPlayOutOpenWindow;
    
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    
    
    
    public class WorkBench implements ICrafting {
    
    
        private final EntityPlayer ep;
        private final int id;
        private final IInventory craftInventory;
        private final IInventory resultInventory;
        private final ContainerWorkbench inventory;
        private final int[] slots ={1,2,3,
                                       4,5,6,
                                       7,8,9};
    
        public WorkBench(Player player) {
            this.ep = ((CraftPlayer)player).getHandle();
            this.id = ep.nextContainerCounter();
            this.inventory = new WorkBenchGui(ep);
            this.craftInventory = this.inventory.craftInventory;
            this.resultInventory = this.inventory.resultInventory;
        }
    
        public void open_gui() {
        
            ep.playerConnection.sendPacket(new PacketPlayOutOpenWindow(id, "minecraft:crafting_table", new ChatMessage("WorkBench", new Object[]{}), 0));
            ep.activeContainer = inventory;
            ep.activeContainer.windowId = id;
            ep.activeContainer.addSlotListener(ep);
            ep.activeContainer = inventory;
            ep.activeContainer.windowId = id;
        
            this.setItems();
        }
    
        public void closeGui() {
            this.ep.closeInventory();
        }
    
        public void setItems() {
            ItemStack wood = new ItemStack(Material.WOOD);
            try {
                this.addItem(wood, 2);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public EntityPlayer getEntityPlayer() {
            return this.ep;
        }
    
        public IInventory getCraftInventory() {
            return craftInventory;
        }
    
        public IInventory getResultInventory() {
            return resultInventory;
        }
    
        public ContainerWorkbench getInventory() {
            return inventory;
        }
    
        public int getId() {
            return this.id;
        }
    
        public void addItem(ItemStack item, int slot) throws IOException {
            if(!isvalidSlot(slot)) throw new IOException("invalid slot");
            else if(item == null) throw new IOException("item cannot be null");
            else this.ep.activeContainer.setItem(slot, CraftItemStack.asNMSCopy(item));
        }
    
        private boolean isvalidSlot(int slot) {
            for(int slot_number : slots) {
                if(slot_number == slot) return true;
            }
            return false;
        }
    
    
        private static class WorkBenchGui extends ContainerWorkbench {
    
            private WorkBenchGui(EntityHuman entityHuman) {
                super(entityHuman.inventory, entityHuman.world, new BlockPosition(0,0,0));
            }
            @Override
            public boolean a(EntityHuman entityHuman) {return true;}
        }
    
    
        @Override
        public void a(Container arg0, NonNullList<net.minecraft.server.v1_12_R1.ItemStack> arg1) {    
        }
    
        @Override
        public void a(Container arg0, int arg1, net.minecraft.server.v1_12_R1.ItemStack arg2) {    
        }
    
        @Override
        public void setContainerData(Container arg0, IInventory arg1) {    
        }
    
        @Override
        public void setContainerData(Container arg0, int arg1, int arg2) {
        }
    }
    


    To check if player has WorkBench open into an inventory event:
    Code:
        @EventHandler
        public void onClick(InventoryClickEvent e) {
            if(e.getView().getTopInventory() instanceof CraftInventoryCrafting)  {
                CraftInventoryCrafting inventory = ((CraftInventoryCrafting) e.getView().getTopInventory());
                if(inventory.getInventory() instanceof InventoryCrafting) {
                    InventoryCrafting craft_inv = (InventoryCrafting) inventory.getInventory();
                    if(craft_inv.container.windowId == workbench.getId()) {                
                        //CODE HERE
                    }
                }
            
            }
        }

    I hope this helps you out , if you got any more questions, I am willing to help :)
     
    Last edited: May 11, 2018
  11. Offline

    TGamingStudio

    @DanielTheDev
    thank you for your help. But i dont use version 1.12
    Code:
    import net.minecraft.server.v1_12_R1.NonNullList;
    so i fixed it witch inventory.put(item); :D now i dont need the workbench . but it can be helpfull for other people . Nice Code!
     
  12. Offline

    DanielTheDev

    @TGamingStudio
    Could you tell me what Version you are using. Then I will convert my code to your version
     
Thread Status:
Not open for further replies.

Share This Page