Item tracking

Discussion in 'Plugin Development' started by V10lator, Sep 7, 2011.

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

    Shamebot

    You'd need to replace the ItemStack object with an instance of yours.
     
  2. I thought that after writing my last post, too, so I added this before a cast:
    Code:
    if(!(item instanceof CustomItemStack))
    	  {
    		  item = new CustomItemStack(item.getType());
    	  }
    And now the error is gone but i can set what I want, I always get 0. :(

    //EDIT: If it helps, here is the class for the testplugin:
    Code:
    public class TestPlugin extends JavaPlugin implements CommandExecutor
    {
      public void onEnable()
      {
    	  getCommand("tp").setExecutor(this);
      }
      public void onDisable()
      {
    
      }
      public boolean onCommand(CommandSender sender, Command command, String cmd, String[] args)
      {
    	  ItemStack item = ((Player)sender).getItemInHand();
    	  if(!(item instanceof CustomItemStack))
    	  {
    		  item = new CustomItemStack(item.getType());
    	  }
    	  if(cmd.equalsIgnoreCase("tp"))
    	  {
    	  sender.sendMessage("Data: "+((CustomItemStack)item).getCustomData());
    	  sender.sendMessage("Durability: "+item.getDurability());
    	  }
    	  else
    	  {
    		  ((CustomItemStack)item).setCustomData(Integer.parseInt(args[0]));
    	  }
    	  return true;
      }
    }
    
     
  3. Offline

    nisovin

    It's not going to be as easy as that. An ItemStack doesn't really represent a single item in the game the way you want it to. You'd have to track all of the movement of that item manually. You'd need to track when it changes inventory slots, when it gets dropped, when it gets picked up, when it gets placed in a chest, when a chest gets destroyed and thus that item gets dropped, and probably lots of other situations. You can't just create some class that extends ItemStack, add some methods, and hope it will work.

    I actually started writing some code for this once. It ended up being annoyingly complex, so I just gave it up.
     
  4. Do you still have the code? I would like to try to finish it. :)

    //EDIT: But for a non stacked item like a weapon it should work like that, it saves the durability in the ItemStack, too...?
     
  5. *bump* Is really nobody out there who knows an easy solution? :(
     
  6. I've been looking for some time. For weapons I have never found a nice method. And items that don't care about durability/data are limited by only being able to store shorts (which gives you ~64000 indexes to play with actually, ~32000 if you don't want to deal with negatives... so that's not too bad).

    But I don't know any way to properly track items.
     
Thread Status:
Not open for further replies.

Share This Page