RPG Item Name Display

Discussion in 'Spigot Plugin Development' started by Daggard, Jun 29, 2016.

  1. I'm creating an RPG plugin which displays the name of rare items, only. Lets say the rarities are Common Common1 Common2 Common3 and Common5. I need to check if a dropped item(Which doesn't have to be dropped by a player) has any of those words in the lore. I'm sort of new to coding and I'm stuck.
    Code (Text):
        public void onItemSpawn(EntitySpawnEvent e){
            if (e.getEntityType() == EntityType.DROPPED_ITEM) {
    I want to use EntitySpawnEvent instead of a Dropped Item event. How would I check for those words in the lore then proceed to use
    Code (Text):
    e.getEntity().setCustomNameVisible(true);                
    e.getEntity().setCustomName()
    to display the name
     
  2. Cast dropped item to the entity and then use the .getItemStack() method.
     
  3. Could you explain it to me in more detail please.. sorry I get confused easily :p
     
  4. You are checking if a dropped item has that specific lore, get the entity check if the entity is instance of Item, then define that Item entity, using
    Code (Text):
     Item item = (Item) getEntity();
    then use the #getItemStack() method
    Code (Text):
    ItemStack itemstack = item.getItemStack();
    And treat it like a normal itemstack and check if it #hasItemMeta() then check if it has lore and if your lore contains what you are looking for.
     
  5. Thats the part I'm stuck on. How would I check for any of the 4 lores in one line?
     
  6. Lores are saved as a list of Strings, You can use .contains() or just loop through and check each individual String.
     
  7. How could I cast getLore?
    Here is my current code:
    Code (Text):

        @EventHandler
        public void onItemSpawn(EntitySpawnEvent e){
            if (e.getEntityType() == EntityType.DROPPED_ITEM) {
                Item item = (Item) e.getEntity();
                ItemStack itemstack = item.getItemStack();
                if (itemstack.hasItemMeta()){
                    if (((Item) itemstack).getLore().toString().contains("Uncommon", "Rare", "Fabled", "Mythical")){
                        String itemname  = e.getEntity().getCustomName();;
                        e.getEntity().setCustomNameVisible(true);
                        e.getEntity().setCustomName(itemname);
                    }
                }
            }
        }
    }
     
     
  8. Right after you check if it hasItemMeta()
    check if it .getItemMeta().hasLore()
    then you can use itemstack.getItemMeta().getLore() which returns a List<String>
     
  9. Thanks! It works now, but how do I check if its "Uncommon" or "Rare" or etc. (An item either has Uncommon, or Rare, etc)

    Code for reference:

    Code (Text):
        @EventHandler
        public void onItemSpawn(EntitySpawnEvent e){
            if (e.getEntityType() == EntityType.DROPPED_ITEM) {
                Item item = (Item) e.getEntity();
                ItemStack itemstack = item.getItemStack();
                if (itemstack.hasItemMeta()){
                    if (itemstack.getItemMeta().hasLore()){
                        if (((ItemMeta) itemstack).getLore().toString().contains("Uncommon")){
                            String itemname  = e.getEntity().getCustomName();
                            e.getEntity().setCustomNameVisible(true);
                            e.getEntity().setCustomName(itemname);
                        }
                    }
                }
            }
        }
    }
     
  10. if(itemstack.getItemMeta().getLore().contains("Uncommon")){
    //do stuff
    }
    if(itemstack.getItemMeta().getLore().contains("Rare")){
    //do stuff
    }
    etc.. etc.
     
  11. Ah so theres no way to do it one line :/ Thanks anyways!
     
  12. What should it do if the item is a rare item and so on?
    Then you can maybe do it easier.
     
  13. Upon testing the plugin, it doesn't do what it's supposed to. I give myself a golden sword with the lore with this command:
    Code (Text):
    /give DaggardBR minecraft:golden_sword 1 0 {display:{Name:"CustomItem",Lore:[Uncommon]}
    But dropping it doesn't give it a name displayed above it.
     
  14. Why do you store the data on NBT Keys, let's say you serialize the CustomItem class to a String using JSON, and then you put it on the Item using Reflections, and after you can get it and deserialize it to a CustomItem, you can also store something like an ID and get from a map the CustomItem with that id, but that wouldn't let you have different stats for the same item (it can but the Map would be big, very big).
     
  15. What I want is for the player to see the item name even if they picked it up and threw it back on the ground again. The name won't be customitem that's just a test. I didn't use names, and used lore instead because the items will always have Uncommon or rare etc in the lore at all times as long as it was spawned as a rare. For some reason it can't detect the lore. :S
     
  16. I don't mean that, if you're creating a RPG Item, you should have a class to store all it's Data, store the Rarity, damage, stats, etc... So you could serialize that class using JSON and put it with reflections using NBT into the ItemStack, then you can easily deserialize it into your class, and that way you can get the stats of the Item and rarity, i wouldn't use the Lore to store any kind of data.
     
  17. Well I want the player to see the Rarity of the item in the lore anyways so might as well :p
     
  18. Of course, but you don't store the data on the ItemStack, on the class where you store item's data you should also add a method to create a ItemStack from your CustomClass, but the Lore would be only something visual.
     
  19. Could you provide me with an example of how to do this. I currently haven't made a class that spawns the item in yet, and I do it manually as of now, but I plan on having a mob drop an item with specific data and then a different class takes that data such as "Tier1Sword", make it a wood sword, and randomly give it a rarity which then determines other things like damage etc.
     
  20. Code (Java):

    public enum Quality {

     RARE; //Add an enum for the quality

    }

    public class CustomItem {

     private final String name;
     private final Quality quality;

     public CustomItem(String name, Quality quality) { //Basic constructor, improve it adding Materials, the lore and stuff
      this.name = name;
      this.quality = quality;
     }

     //Getters

     public ItemStack toItemStack() { //Add to convert the data you have defined here into a ItemStack

     }

    }

    String serialized = serialize(customItem); //Serialize the item using jsom
    addNBTKey(itemStack, "Item", serialized);

    //Now you have the data in the ItemStack

    String serialized = getNBTKey(itemStack, "Item");
    CustomItem deserialized = deserialize(serialized); //Deserialize the Item using json

    //Now you have the item deserialized from a Stirng