NMS enchant table

Discussion in 'Plugin Development' started by guitargun, Oct 23, 2015.

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

    guitargun

    so I am trying to send packets to allow the player to click the enchant options:
    however all of my tries don't seem to work
    Code:
    ContainerEnchantTable enchant = (ContainerEnchantTable) player.activeContainer;
                        enchant.costs[0] = 5;
                        enchant.costs[1] = 8;
                        enchant.costs[2] = 10;
                        player.setContainerData(enchant, 0, enchant.costs[0]);
                        player.setContainerData(enchant, 1, enchant.costs[1]);
                        player.setContainerData(enchant, 2, enchant.costs[2]);
    and I tried with simple packets
    Code:
    PacketPlayOutWindowData data = new PacketPlayOutWindowData(enchant.windowId, 0, enchant.costs[0]);
    yet nothing is happening
     
  2. Offline

    CoolDude53

    Does anything happen, or is nothing being changed?
     
  3. Offline

    guitargun

    @CoolDude53 as said nothing is happening.. the buttons stay gray and unchanged and there is no error. I know the code reaches there
     
  4. Offline

    CoolDude53

    @guitargun

    Took a while, but I managed to get it working. You are going to need to do it in an InventoryClickEvent.

    Code:
    @EventHandler
        public void onEnchantClick(InventoryClickEvent event)
        {
            new BukkitRunnable()
            {
                @Override
                public void run()
                {
                    if (event.getWhoClicked() instanceof Player && event.getInventory().getType().equals(InventoryType.ENCHANTING))
                    {
                        ContainerEnchantTableInventory originalInv = (ContainerEnchantTableInventory) ((CraftInventory) event.getInventory()).getInventory();
    
                        try
                        {
                            Field container = ContainerEnchantTableInventory.class.getDeclaredField("enchantTable");
                            container.setAccessible(true);
    
                            ContainerEnchantTable original = (ContainerEnchantTable) container.get(originalInv);
    
                            if (original.enchantSlots.getItem(0) != null)
                            {
                                original.costs[0] = 2;
                                original.costs[1] = 4;
                                original.costs[2] = 6;
                                original.b();
                            }
                        } catch (NoSuchFieldException | IllegalAccessException e)
                        {
                            e.printStackTrace();
                        }
                    }
                }
            }.runTask(plugin);
        }
     
  5. Offline

    guitargun

    @CoolDude53 thx man
    also to simplify:
    Code:
        new BukkitRunnable() {
                           
                            @Override
                            public void run() {
                                EntityPlayer player = ((CraftPlayer) ((Player) event
                                        .getWhoClicked())).getHandle();
                                ContainerEnchantTable enchant = (ContainerEnchantTable) player.activeContainer;
                                enchant.costs[0] = 5;
                                enchant.costs[1] = 8;
                                enchant.costs[2] = 10;
                                enchant.b();
                            }
                        }.runTask(plugin);
    no need for reflection. you just need to entityplayer since the containerenchanttableis unique for each player
     
  6. Offline

    CoolDude53

    That container didn't return an ContainerEnchantmentTable when I tested it, but I only tested it in an InventoryOpenEvent.
     
Thread Status:
Not open for further replies.

Share This Page