Explanation in thread!

Discussion in 'Plugin Development' started by GijsCo, Jan 16, 2018.

Thread Status:
Not open for further replies.
  1. Okay so I had the idea of assigning a class to an other class.
    I had the idea of creating a HashMap<ItemStack, MyOtherClass>. So you would be able to get the OtherClass from the specific ItemStack, but the problem is is that when i try to add more than one it's going to overwrite the old itemstack if you have the same Material.

    So I did some debugging and this an output when you print out an itemstack class: ItemStack{PAPER x 1}. This is not an normal out put for an class normally it would have an @(with some numbers). I self think that this is the issu that the HashMap sees the ItemStack as the same ItemStack while it has an other displayname for example!

    Also i was wondering if HashMap uses the method "equals" if so is there an way to overwrite that? So it uses "==" instead of equals!

    I was trying to make this concept with the regualar Bukkit ItemStacks, but maybe that's isn't possible and I have to make NMS ItemStacks? Pls let me know ^^

    I was wondering if you guys maybe an solution! ^^
    Have an wonderfull day and thanks for reading my Thread!
     
    Last edited: Jan 16, 2018
  2. Offline

    timtower Administrator Administrator Moderator

  3. @timtower Well the end goal is is that i can assign an class to an itemstack! So for example i have an itemstack for a wand. I want to be able to toss around that ItemStack and than get the class Wand for that ItemStack Whenever i want by using hashmap.get(key<ItemStack>) returns the class wand that is connceted it with. Normally you would use extend but that isn't possible cause you cannot get into the ItemStack class. Also to mention is that i tried by creating a new ItemStack and got the old ItemStack and tried to do newItemStack == oldItemStack. returns false so I don't know why it's overriding the other key. At the end I could always just an ArrayList and loop trew it but I don't know how more efficent that would be if I had for example 2000 Objects
     
    Last edited: Jan 16, 2018
  4. Offline

    timtower Administrator Administrator Moderator

    @GijsCo == returns false for most things that have fields of their own as it compares memory addresses, or something like that.
    Use .equals
     
  5. @timtower I know that .equals and == is diffrent. But if i would use equals it will return true when the data is the same as in the other class. The problem with that is. Is that for example 2 same ItemStacks will both have an other class assignd.
     
  6. Offline

    timtower Administrator Administrator Moderator

    @GijsCo Why do you have 1 itemstack with 2 classes assigned?
     
  7. @timtower Well I could have created 2 ItemStacks, but they have the same data inside so for ItemStack that would mean the lore, name etc. But those 2 "same" ItemStacks will have 2 diffrent classes assigned
     
  8. Offline

    timtower Administrator Administrator Moderator

    @GijsCo But if they are exactly the same then the hashmap is just doing its job.
    You should have a check if there is one already.
     
  9. @timtower No cause for example I have 2 Sticks Same data etc. One stick will let me jump, and the other one will let run faster. If i first have an stick that has a jump class assigned. And than add another stick with the same data but has an speed class assigned. Than the class jump will be overriden. Let me give an example

    Map<ItemStack, AbilityClass> hashmap = new HashMap<ItemStack, AbilityClass>();

    ItemStack stack1 = new ItemStack(Material.STICK);
    ItemStack stack2 = new ItemStack(Material.STICK);

    AbilityClass ac1 = new AbilityClass(1);
    AbilityClass ac2 = new AbilityClass(2);

    hashmap.put(stack1, ac1);
    hashmap.put(stack2, ac2);

    And now if i would do

    System.out.printLn(hashmap.get(stack1).getValue());

    Will print out 1 and

    System.out.printLn(hashmap.get(stack2).getValue());

    Will print out 2
     
    Last edited: Jan 16, 2018
  10. Offline

    timtower Administrator Administrator Moderator

    @GijsCo Which is normal.
    Why would you have 2 sticks that do different things? What is the difference between them? Is there a difference between them?

    @GijsCo Don't back edit, nobody will see it.
    That is just how hashmaps work.
     
    Last edited: Jan 16, 2018
  11. @timtower Well the sticks could have an cooldown that is handeld in the other class. But I could always have an ArrayList and loop trew it but it's more i don't know how efficent that will be if i have like 2000 object in there
     
  12. Offline

    timtower Administrator Administrator Moderator

    @GijsCo 2000 is a big number.
    Then you need a HashMap<ItemStack,List<Ability>>
    Where the ability would handle the cooldown etc, if an ability is not allowed to run then it goes to the next one.

    But you can prevent that by using different items. Change the name, change the lore.
     
  13. @timtower Well I suppose I will have to find an other way, thanks anyway!
     
  14. Offline

    timtower Administrator Administrator Moderator

    @GijsCo Not having the exact item twice is your best shot, how would the user know what it would do?
     
  15. @timtower Well it's more that i have an ItemStack That has an class spellscroll assigned to it and the spell scroll has an cooldown. This means that in the server there will be multipule SpellScroll(ItemStack) with the same data, but the assigned class is diffrent cause of the cooldown.
     
  16. Offline

    timtower Administrator Administrator Moderator

  17. @timtower Yh was thinking about that, I only don't know it will be inefficent if have to loop trew 2000 objects it's not on a timer
     
  18. Offline

    timtower Administrator Administrator Moderator

    @GijsCo Then you probably want to think about your actions, 2000 is a lot.
    Users won't know what they get.
     
  19. @timtower What do you mean by "Users won't know what they get?"
     
  20. Offline

    timtower Administrator Administrator Moderator

    If you have 5 spells on a stick, how will the user know which one will run?
     
  21. @timtower Spell code mechanism by pressing shift and entering hot key nums. will launch the spell that been entered. But every stick contains diffrent spells, and can be trown away and picked up by someone else. That's why i need to know the excact ItemStack
     
  22. @GijsCo I think I could be of a lot of help regarding your confusion by this. Looping through 2000 items to find which one it is is certainly a bad idea, and that will cause a performance impact on the server (most plugins don't cause a noticeable performance impact).

    A hashmap is actually built to handle looking through a large number of keys, because it uses hashcodes. The general contract is that a unique object almost always has a different hashcode (there is a limited number of possible hashcodes, so sometimes different objects have the same one). When you do hashmap.containsKey(key), it finds the hashcode of key, calculates where the index would be, and sees if it is there already. The lookup cost doesn't change much at all so long as each key has a unique hashcode- it doesn't loop through everything in the map, it just sees if it is where it would be.

    All classes have the method toString()- if they don't override it, it prints the memory address (@1244something). ItemStack overrides this.

    Now for what you can do... Internally, Bukkit itemstacks are actually CraftItemStack (do not import this unless you want to either download every version you want your plugin to work on, or have it only work on one version). And, this class is final (meaning you can't extend it). So you can't override .equals(Object) in ItemStack... but you can either use Strings as the key instead, and have those strings be the display names of the items, or you can use a wrapper class.
     
  23. Offline

    timtower Administrator Administrator Moderator

    @GijsCo Use the lores. Then you can just parse though and fire a spell based off it.
    Or give them ID's.
    But having 2 itemstacks exactly the same won't be in your benefit.
     
  24. Offline

    timtower Administrator Administrator Moderator

    @GijsCo UUID'S for every stick. Something unique.
    Would still stick with the abilities in the lore though.
     
  25. @timtower I get that lore idea, but some lore's are the exact same but the abilities are diffrent like the current cooldown etc. Also how would you set UUID's for a stick never heared of assigning UUIDs to items
     
  26. Offline

    timtower Administrator Administrator Moderator

    @GijsCo You put an uuid in the lore.
     
  27. Yh i sugess i have no other chooce than that atm :/. Anyway thanks for all your help and time! :)
     
Thread Status:
Not open for further replies.

Share This Page