Util ItemStack Data Encoder - Store invisible data in an item name.

Discussion in 'Resources' started by DeadlyScone, Nov 8, 2015.

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

    DeadlyScone

    Code:
         /*
            Returns an encoded string that appears invisible to the
            client.
        */
        public static String encodeItemData(String str){
            try {
                String hiddenData = "";
                for(char c : str.toCharArray()){
                    hiddenData += "§" + c;
                }
                return hiddenData;
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }
        }
    
        /*
            Decodes an encoded string
        */
        public static String decodeItemData(String str){
            try {
                String[] hiddenData = str.split("(?:\\w{2,}|\\d[0-9A-Fa-f])+");
                String returnData = "";
                if(hiddenData == null){
                    hiddenData = str.split("§");
                    for(int i = 0; i < hiddenData.length; i++){
                        returnData += hiddenData[i];
                    }
                    return returnData;
                }else{
                    String[] d = hiddenData[hiddenData.length-1].split("§");
                    for(int i = 1; i < d.length; i++){
                        returnData += d[i];
                    }
                    return returnData;
                }
    
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }
        }
    
    Summary
    When you call encodeItemData, you will encode the argument into an "invisible" string, basically its handling each character in the string as a color character. This is useful if you wish to use it in the displayname or the lore list of an itemstack. By adding the returned string to the itemstack, you can store data inside without the use of packets(search hiding lores).
    Once you need to pull back out the data, simply call the decodeItemData and whala, you get back the same data you put inside originally.

    Usage
    [/I]
    Lets say I WANT to display the name of an itemstack okay cool, now I need some data in there, to do this call the encode method passing ONLY the message to be encoded.
    The setup may look like this:
    Encoding data (open)

    Code:
    String myName = "§bGUN: §4AMMO")
    String secretMsg = "secret message"
    ItemMeta im = ItemStack.getItemMeta();
    im.setDisplayName(myName + encodeItemData(secretMsg));
    



    Now come time to get our data back out of this item, we will do the following:
    Decoding data (open)

    Code:
    ItemMeta im = ItemStack.getItemMeta();
    String theMsg = decodeItemData(im.getDisplayName);
     
    Last edited: Nov 11, 2015
  2. Offline

    mcdorli

    This is actually a nice idea. I always used lores for making the items uncraftable.
     
  3. Offline

    DeadlyScone

    Thanks for your input! I think the regex needs some fine tuning to be able to use chat colors that contain letters (a-f), but as long as you use chat colors that contain a digit, this will work nicely.

    UPDATE: I fixed it.
     
    Last edited: Nov 9, 2015
  4. Offline

    ChipDev

    Cool! Better than I expected, almost passed it off thinking it was another "HOW TO DO ITEM MATA THINGZ" (Notice the typo ;D) I love how creative it is.
     
  5. Offline

    Skyost

    Nicely found.
     
  6. Offline

    pie_flavor

    @DeadlyScone I actually came up with this exact thing a while back, with the idea that I could store UUIDs in them. I then discovered that a full UUID encoded into colors crashed the client, and promptly forgot that this was possible.
     
Thread Status:
Not open for further replies.

Share This Page