NMS Items?

Discussion in 'Plugin Development' started by TwoPointDuck, Mar 24, 2014.

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

    TwoPointDuck

    Anyone know everything that goes into adding a CustomItem into players inventory.

    It's not too hard creating it, but after that, I fail to create the ItemStack and then place it into the inventory.

    Here's my custom item class:

    Code:
    package com.twopointduck.nms;
     
    import net.minecraft.server.v1_7_R1.CreativeModeTab;
    import net.minecraft.server.v1_7_R1.EnumToolMaterial;
    import net.minecraft.server.v1_7_R1.ItemSword;
     
    public class CustomItemSword extends ItemSword {
     
        private int i;
        private float damage;
        private final EnumToolMaterial b;
     
        public CustomItemSword(EnumToolMaterial enumtoolmaterial, int i) {
            super(enumtoolmaterial);
       
            this.b = enumtoolmaterial;
            this.maxStackSize = 1;
            this.setMaxDurability(enumtoolmaterial.a());
            this.a(CreativeModeTab.j);
            this.damage = 50.0F;
       
            this.i = i;
        }
     
     
     
     
    }
    EDIT: Still getting errors when trying to make it ItemStack

    This should be the NMS code for it, anyone know whats what?

    Code:
     public void a(Container container, int i, ItemStack itemstack) {
            if (!(container.getSlot(i) instanceof SlotResult)) {
                if (!this.g) {
                    this.playerConnection.sendPacket(new PacketPlayOutSetSlot(container.windowId, i, itemstack));
                }
            }
        }
     
  2. Offline

    Minnymin3

    Why are you creating a new item class?
     
    ZeusAllMighty11 likes this.
  3. Offline

    TwoPointDuck


    Because I need to store some more info in them, i.e. custom damage, stun % chances, all kinda stuff.
     
  4. Offline

    Codisimus

    Bukkit won't retain that info. It may be lost when handled by another plugin or even simply put inside a chest
     
  5. Offline

    TwoPointDuck


    It won't be put anywhere but the players inventory, nor will any other plugin touch it. I will also handle saving them. This is for a large RPG type plugin.
     
  6. Offline

    Codisimus

    TwoPointDuck I'm just warning you that it won't be reliable. Bukkit may handle the item when the player simply opens their inventory. My suggestion is to use a modified Bukkit build that supports your custom NMS values.
     
  7. Offline

    Barinade

    Just use lore
     
  8. Offline

    Codisimus

    Then you are storing all data as Strings and it is visible to the Player.
     
  9. Offline

    TwoPointDuck

    Codisimus

    Ye I guess, any idea where I could get my hand on one of those? Any place where people post those?
     
  10. Offline

    Codisimus

    TwoPointDuck
    I've seen code on how to add it to Bukkit, problem is you have to manually build each new version yourself.
     
  11. Offline

    Barinade

    How is that bad?
    You could try extending ItemStack and adding your own fields to the custom class, I'll tinker with this if you can't get it to work
     
  12. Offline

    Codisimus

    It would be a lot slower and an eyesore.
     
  13. Offline

    bobacadodl

    You can store invisible text in the lore. Just put a Section symbol before every character in the string

    Code:
    public static void encryptItem(ItemStack i, String message) {
            ItemMeta im = i.getItemMeta();
            List<String> lore = new ArrayList<String>(im.getLore());
            String crypt = encryptString(message);
            lore.add(0, crypt);
            im.setLore(lore);
            i.setItemMeta(im);
        }
     
        public static String encryptString(String message) {
            String crypt = "";
            for (char c : message.toCharArray()) {
                crypt += ChatColor.COLOR_CHAR + Character.toString(c);
            }
            return crypt;
        }
     
        public static String decryptItem(ItemStack i) {
            if (i != null) {
                if (i.hasItemMeta()) {
                    List<String> lore = i.getItemMeta().getLore();
                    if (lore != null) {
                        if (lore.size() > 0) {
                            StringBuilder sb = new StringBuilder();
                            for (char c : lore.get(0).toCharArray()) {
                                if (c != ChatColor.COLOR_CHAR) {
                                    sb.append(c);
                                }
                            }
                            return sb.toString();
                        }
                    }
                }
            }
            return null;
        }
     
    Minnymin3 likes this.
  14. Offline

    Minnymin3

    You could also just wait till 1.8 is released. Itemstacks allow for custom NBT data which will be a huge step when storing custom data.

    For now you can only use lore.
     
  15. Offline

    tylersyme

    Yea, I'm waiting with great anticipation for that! In the mean time I've always used lore for all the custom information.

    Another way I accomplish this is to give each item a unique ID number in the lore, and then all the custom information about that item I put inside a config file. Like every time a player hits another player with a sword, it checks that sword's ID number and looks at the config to see if there's any special information it needs to know, like giving extra damage or knockback or something. But this can lead to massive config files, so I also have to delete the items in the config when they get destroyed, so it's a whole lot more work in general to do it that way, but it does give you more control
     
  16. Offline

    Codisimus


    Genius! and that works even though most characters aren't a valid color in Minecraft?
     
Thread Status:
Not open for further replies.

Share This Page