Solved Changing item tooltips on the fly

Discussion in 'Plugin Development' started by DrEinsteinium, Jul 23, 2014.

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

    DrEinsteinium

    Hello Bukkit devs!

    I am currently trying to create an easy system that will help me change what appears on an item tooltip dynamically. It's easier to explain what I am trying to do with an example:

    I have a diamond sword with a tooltip that looks like this:
    Code:
      Diamond Sword
        - key.helloworld
        - Attack Damage +7
    
    I want to be able to dynamically replace the key with something else whenever the player opens their inventory so it will appear like this:
    Code:
      Diamond Sword
        - Hello World!
        - Attack Damage +7
    

    I know that I could probably do this by examining every item during InventoryOpenEvent, add the replaced strings to a HashMap and store it somewhere, and during InventoryCloseEvent, re-replace the strings that were originally replaced during the other event.

    In my opinion, the system described above is a little bit messy to me and I am not sure what kind of pitfalls it will include, so if there are some more experienced developers that want to give their two cents, let me know how you feel!

    Thanks,
     
  2. If I understand what you mean why dont you make a list of stuff you want like this
    List<String> lores = new ArrayList<String>();
    Then in onEnable just add a bunch of names to it
    lores.add("PIE");
    lores.add("PI");

    Then you can just get the items lore and do
    lore.add(lores.get(new Random().nextInt(lores.size())));
    and set the lore to the itemmeta and set the itemmeta to the itemstack
     
  3. Offline

    DrEinsteinium

    WD_MUFFINFLUFFER
    This is not what I am trying to do. I am trying to create a system that is able to recognize different lore strings, but I want to be able to change those strings later.

    We can just take an enchantment for example. When I add a new line of lore to the item's meta that represents a custom enchantment(theoretical), the line "enchantment.lifesteal" will be added.

    Now, whenever the player opens their inventory, the plugin will look for all items with the lore "enchantment.lifesteal" and replace it with "Lifesteal". After the player is done viewing their inventory, it will replace it back to "enchantment.lifesteal"

    The whole idea is to be able to change the "Lifesteal" string to something else later but the lore line will still be recognized because behind the scenes it is "enchantment.lifesteal"
     
  4. I see, but then it seems like you will just have to examine everything and replace it.
    However you don't need to save it? If you replace enchantment.lifesteal with LifeSteal on open just replace LifeSteal with enchantment.lifesteal on close. No need to save?
     
  5. Offline

    DrEinsteinium

    WD_MUFFINFLUFFER I would have saved it in a hashmap based on inventory hashcode, but I just figured out it's impossible to tell when the player's inventory is open. Too bad.
     
  6. Oh, alright. May I ask what the reason was for renaming it constantly? It seems as though there would be no point to me.
     
  7. Offline

    DrEinsteinium

    WD_MUFFINFLUFFER Well I wanted the ability to change the display strings to something else later. Let's say I wanted to change "Lifesteal" to "Health per hit". Since EntityDamage events aren't while the inventory is open, my listener could listen for items that have enchantment.lifesteal on them instead of "Lifesteal".
     
    WD_MUFFINFLUFFER likes this.
  8. Offline

    xize

    you could better clone the ItemStack change the lore and replace the current ItemStack in the inventory, somehow lores wouldn't update in a open inventory (maybe it does with nms) even with p.updateInventory(), however the only problem remains how do you know what you are replacing?, ive made the same smilliar method for backpacks but I could easily get the item from hand but if its non handed that could be a problem.
     
  9. Offline

    DrEinsteinium

    xize Cloning the itemstacks would work, and that's actually a really good idea, but still remains the problem with knowing when to replace the lore and when not to. Opening a player inventory is all client side until they interact with it, so there is no way for Bukkit to know when a player opens their inventory.
     
  10. Offline

    xize

    DrEinsteinium

    Well from what I know it is possible to detect if the player opens it with InventoryOpenEvent, but to update it I probably would remove the item and use Inventory#addItem() for the cloned item.

    And for comparing things I may would do a lazy way of checking by creating a dummy ItemStack with the possible ItemMeta and from there just do meta.equals(item.getItemMeta())

    To get a idea of getting a current slot you may want to use the boolean from Inventory#containsAtleast(yourdummystack); or just contains.

    and then for loop like:

    Code:
    for(int id = 0; id < inv.size(); id++) { //maybe id should start with 1
       //use getItem(id)
       //do null check
       //compare your pre maded ItemStack's meta data with the meta data from the one you get in this loop
       //then do something
    }
    
     
  11. Offline

    Garris0n

    As far as I know, the client doesn't tell the server when it opens its own inventory.

    Ignoring that, what are you even attempting to accomplish with this?
     
Thread Status:
Not open for further replies.

Share This Page