1. Xizz3l

    Xizz3l New Member

    Xizz3l
    VIP
    Messages:
    10
    Hi, I am completely new to this. I am playing with the new Skyblock api.
    I am trying to decode the item_bytes from the auctions. I know it's a base 64 gzip nbt data.
    Is there any lib or code that I can use to decode it.

    I found a way to decode the base 64 but don't know what to do next.
     
    #1
  2. You can use the built in java classes (assuming you use at least java 8 i believe) java.util.Base64 (to decode from base64) and java.util.zip.GZIPInputStream

    Here's a code snippet that you can use to return the data unzipped, as a string:

    Code:
    import java.io.*;
    import java.util.Base64;
    import java.util.zip.GZIPInputStream;
    
    public void jsonCode() throws IOException {
        String jsonFormatOutput = decompressGzip(decodeBase64String(inputString));
        System.out.println(jsonFormatOutput);
    }
    
    public byte[] decodeBase64String(String string){
        return Base64.getDecoder().decode(string);
    }
    
    public String decompressGzip(byte[] compressed) throws IOException {
        final int BUFFER_SIZE = 32;
        ByteArrayInputStream byteArrayStream = new ByteArrayInputStream(compressed);
        GZIPInputStream gzipStream = new GZIPInputStream(byteArrayStream, BUFFER_SIZE);
        StringBuilder string = new StringBuilder();
        byte[] data = new byte[BUFFER_SIZE];
        int bytesRead;
        while ((bytesRead = gzipStream.read(data)) != -1) {
            string.append(new String(data, 0, bytesRead));
        }
        gzipStream.close();
        byteArrayStream.close();
        return string.toString();
    }
    
    from there, on there's a lot of libraries you can use to parse the NBT data and manipulate however you see fit
    https://wiki.vg/NBT#Libraries
     
    #2
  3. Xizz3l

    Xizz3l New Member

    Xizz3l
    VIP
    Messages:
    10

    Ty for the extremely quick response. I used the java example code from GitHub in combination with my api key to get some example string of an item_bytes:
    "H4sIAAAAAAAAAB2OMQ6CQBREB0SFLbQysfQo4rKGApZCrc1HfnATBANroifiHhzMCE43k8nLE0AAxwgAjgvXFPAczGXzqu1eYGapDOBxfbtjygxBbAo+VlR2Y/0KLAvTPSv6jK+kadmfOFgNPQ19ddEyS9NM+/A0PRjbcVYjimrLxU42eV5xZ5uaIbBWb9tSaG1r8pflzv+7bJSWcajPKrrK7HBI1OmcaQW4WET0oJInqR/1qTtowwAAAA=="

    When I set this as inputString I get the following result:

    " i
    [email protected]
    tag ench HideFlags�
    display Lor§a§lUNCOMMOName§aEnchanted Cobblestone
    ExtraAttributeidENCHANTED_COBBLESTONEDamage"

    Did I something wrong? I copied the code and changed nothing. For example the count of the item would be interesting, but it doesn't show that information. After that I tested 2 more and got the same result.
     
    #3
  4. You did not do anything wrong, but the way i gave it to you was in string format. The actual file would be in NBT format, where stuff doesnt always translate to a string correctly. To properly read it, you would need some NBT library.

    I've looked into this for you, and using the link i previously sent, found this java library: https://github.com/MrYurihi/TBNBT

    It actually does all the gzip decoding for you already, so my previous code renders largely unnecessary when using this library.

    I've provided a small piece of example code with this library, as to how you would read the NBT data. Most of this would be for you figuring out what data is included, and finding the correct types/names etc. You can do this in the debugger with decent ease

    Code:
    public void createNBTfromString() throws IOException {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(decodeBase64String(inputString));
        NBTInputStream stream = new NBTInputStream(inputStream, true);
        NBTTagList tagList = stream.readTag().getAsTagCompound().get("i").getAsTagList();
        for(NBTTag t: tagList){
            NBTTagCompound compound = t.getAsTagCompound();
            short itemID = compound.get("id").getAsTagShort().getValue();
            byte count = compound.get("Count").getAsTagByte().getValue();
            NBTTagCompound details = compound.get("tag").getAsTagCompound();
            short damage = compound.get("Damage").getAsTagShort().getValue();
            //do whatever you want with these values
        }
    }
    
    The library i used does the gzip decoding for you, so the only thing you have to do is decode from base64 first.
     
    #4
  5. #5
  6. Xizz3l

    Xizz3l New Member

    Xizz3l
    VIP
    Messages:
    10
    Where can I find it or extract it from?
     
    #6
  7. Its part of the MC source code...
    You can import it when developing with forge/MCP.
     
    #7

Share This Page