getHandle() at CraftItemStack ?

Discussion in 'Plugin Development' started by lucasdidur, Dec 20, 2012.

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

    lucasdidur

    With new ItemMeta API, I cant update my plugins, because bukkit has removed getHandle() from CraftItemStack (I save Inventory at NBT).


    How I can get working again?

    Code:
    private void saveToFile(Player player, String number, Inventario inventario) throws IOException
        {
            File playerDir = new File(STORAGE_DIR, player.getName());
            File playerDat = new File(playerDir, number + ".inv.nbt");
            
            if(!playerDir.exists())
                playerDir.mkdir();
            
            if(inventoryEmpty(inventario))
            {
                log.debug("Apagando salve em branco");
                playerDat.delete();
                
                if(playerDir.isDirectory())
                {
                    String[] files = playerDir.list();
                    
                    if(files.length <= 0)
                    {
                        log.debug("Apagando pasta em branco");
                        playerDir.delete();
                    }
                }
                return;
            }
            
            final DataOutputStream out = new DataOutputStream(new GZIPOutputStream(new FileOutputStream(playerDat)));
            
            NBTTagList items = new NBTTagList();
            
            int invSize = inventario.getSize();
            
            for (int slot = 0; slot < invSize; slot++)
            {
                ItemStack stack = inventario.getItem(slot);
                
                if(stack != null)
                {
                    NBTTagCompound item = new NBTTagCompound();
                    item.setByte("Slot", (byte) slot);
                    ((CraftItemStack) stack).getHandle().save(item);
                    items.add(item);
                }
            }
            
            final NBTTagCompound nbt = new NBTTagCompound();
            nbt.set("Items", items);
            
            NBTBase.a(nbt, out);
            out.close();
        }
    
    I've been looking for a solution to change that:

    Code:
    ((CraftItemStack) stack).getHandle().save(item);
    
    And found this. Will it work?

    Code:
    ((CraftItemStack) stack).save(item);
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  2. Offline

    fireblast709

    If you really really really want it, use reflection. Cast an ItemStack to CraftItemStack, and get "handle" (cis is the CraftItemStack
    Code:java
    1. Field handle = CraftItemStack.class.getDeclaredField("handle");
    2. handle.setAccessible(true);
    3. net.minecraft.server.v1_4_5.ItemStack nis = (net.minecraft.server.v1_4_5.ItemStack)handle.get(cis);
    (From the top of my head, besides the needed try-catch, it might contain some other errors). Also you could try
    Code:java
    1. net.minecraft.server.v1_4_5.ItemStack nis = CraftItemStack.asNMSCopy(bukkitStack);


    Where did you find thát?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  3. Offline

    lucasdidur

  4. Offline

    fireblast709

    lucasdidur I ment more how you came up with the second codeblock (as CraftItemStack has no save() method :3)
     
  5. Offline

    lucasdidur

    Line 41 of first code.
     
  6. Offline

    fireblast709

    I was talking about the "save() method in CraftItemStack" you were talking about, which is not in that thread (as it does not exist)
     
Thread Status:
Not open for further replies.

Share This Page