Base64 (Encode/Decode ItemStack)

Discussion in 'Plugin Development' started by PressDev, Jun 23, 2015.

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

    PressDev

    Hello, this is my first thread on bukkit forum, i need help for make Base64 to Encode and Decode ItemStack to String, i am using ItemHandler to this, and ItemHandler Necessite Base64 to work, however cause error on eclipse, more, on other plugin, work 100%
    Code Base64
    Code:
    package ms.kaway.essentials;
    
    
    
    
    
    public class Base64
    {
      public static final String encode(byte[] d)
      {
        if (d == null) return null;
        byte[] data = new byte[d.length + 2];
        System.arraycopy(d, 0, data, 0, d.length);
        byte[] dest = new byte[data.length / 3 * 4];
    
    
        int sidx = 0; for (int didx = 0; sidx < d.length; didx += 4)
        {
          dest[didx] = ((byte)(data[sidx] >>> 2 & 0x3F));
          dest[(didx + 1)] =
            ((byte)(data[(sidx + 1)] >>> 4 & 0xF | data[sidx] << 4 & 0x3F));
          dest[(didx + 2)] =
            ((byte)(data[(sidx + 2)] >>> 6 & 0x3 | data[(sidx + 1)] << 2 & 0x3F));
          dest[(didx + 3)] = ((byte)(data[(sidx + 2)] & 0x3F));sidx += 3;
        }
    
    
    
    
    
    
    
        for (int idx = 0; idx < dest.length; idx++)
        {
          if (dest[idx] < 26) { dest[idx] = ((byte)(dest[idx] + 65));
          } else if (dest[idx] < 52) { dest[idx] = ((byte)(dest[idx] + 97 - 26));
          } else if (dest[idx] < 62) { dest[idx] = ((byte)(dest[idx] + 48 - 52));
          } else if (dest[idx] < 63) dest[idx] = 43; else {
            dest[idx] = 47;
          }
        }
    
        for (int idx = dest.length - 1; idx > d.length * 4 / 3; idx--)
        {
          dest[idx] = 61;
        }
        return new String(dest);
      }
    
    
    
      public static final String encode(String s)
      {
        return encode(s.getBytes());
      }
    
    
    
    
      public static final String decode(String str)
      {
        if (str == null) return null;
        byte[] data = str.getBytes();
        return new String(decode(data));
      }
    
    
    
    
    
      public static final byte[] decode(byte[] data)
      {
        int tail = data.length;
        while (data[(tail - 1)] == 61) tail--;
        byte[] dest = new byte[tail - data.length / 4];
    
    
        for (int idx = 0; idx < data.length; idx++)
        {
          if (data[idx] == 61) { data[idx] = 0;
          } else if (data[idx] == 47) { data[idx] = 63;
          } else if (data[idx] == 43) { data[idx] = 62;
          } else if ((data[idx] >= 48) && (data[idx] <= 57)) {
            data[idx] = ((byte)(data[idx] - -4));
          } else if ((data[idx] >= 97) && (data[idx] <= 122)) {
            data[idx] = ((byte)(data[idx] - 71));
          } else if ((data[idx] >= 65) && (data[idx] <= 90)) {
            data[idx] = ((byte)(data[idx] - 65));
          }
        }
    
    
        int sidx = 0; for (int didx = 0; didx < dest.length - 2; didx += 3)
        {
          dest[didx] =
            ((byte)(data[sidx] << 2 & 0xFF | data[(sidx + 1)] >>> 4 & 0x3));
          dest[(didx + 1)] =
            ((byte)(data[(sidx + 1)] << 4 & 0xFF | data[(sidx + 2)] >>> 2 & 0xF));
          dest[(didx + 2)] =
            ((byte)(data[(sidx + 2)] << 6 & 0xFF | data[(sidx + 3)] & 0x3F));sidx += 4;
        }
    
    
    
    
    
        if (didx < dest.length)
        {
          dest[didx] =
            ((byte)(data[sidx] << 2 & 0xFF | data[(sidx + 1)] >>> 4 & 0x3));
        }
        didx++; if (didx < dest.length)
        {
          dest[didx] =
            ((byte)(data[(sidx + 1)] << 4 & 0xFF | data[(sidx + 2)] >>> 2 & 0xF));
        }
        return dest;
      }
    }
    
    @Edit
    Error on Eclipse: [​IMG]
     
    Last edited: Jun 24, 2015
  2. Offline

    caderape

    @PressDev And what the problem ? You don't get why 'didx' do not work ?
     
  3. Offline

    PressDev


    Exactly, i not found 'didx', i don't get why 'didx' do not work!
     
  4. Offline

    xTigerRebornx

    @PressDev If you use Java 8, there is a class bundled with it that can do Base64 Encoding/Decoding (called Base64)
     
  5. Offline

    PressDev

    Java7
     
  6. @PressDev It's clear from your code that you're fairly new to Java. Please learn the basics of Java before trying to make Bukkit plugins. As Bukkit is written in Java, learning it is compulsory. If you continue to try to make plugins without sufficient knowledge, you will continue to run into simple problems like this and have no idea how to solve them. I recommend the Oracle tutorials or a Java book.
     
  7. Offline

    PressDev

    I know to program in Java and also develop plugins, though, I'm just wondering HOW DO for this part of Running Base64, since recreated the ItemHandler, this Base64 I got on the internet, did not really understand it, but I understand a lot of other areas .

    [​IMG][​IMG]

    This plugin is to MineCraft 1.5.2
    If an error is so simple and easy, help me, so I can better enteder the code and study more, I'm asking just that.
     
  8. @PressDev The error message says the didxisn't defined. Which means it isn't defined in the scope of where you're trying to access it. If you don't know how to solve that, then I'm afraid you don't know enough Java. :(
     
  9. Offline

    PressDev

    that part I know, of course, I'm afraid to set the value didx wrong.You understand now?
     
  10. Offline

    xTrollxDudex

    You can't set didx to a wrong value if it doesn't exist... Or if it does exist, can you show the code?
     
    AdamQpzm likes this.
  11. Offline

    PressDev

    SPOILER Base64 has the code
     
  12. Offline

    Zombie_Striker

    @PressDev
    • Format your code (you're writing in JavaScript, not Java)
    • Locate didx, and right underneath where that field is created, use system.out.println("didx :"+didx);
    From the way you code, I think you learned Python,C# or some other program and you are just using what you know from there and applying it here. Formatting is a big indicator of this.
     
  13. What do you mean by this?
     
  14. Offline

    Zombie_Striker

    Code:
    public void method()
    {
    
    }
    The propor way to format for Java is like so
    Code:
    public void method{
    
    }
    It's a preference, I know, but it does show that you learned to format differently.
     
  15. @Zombie_Striker While I don't like the Allman style either, it's not exactly "wrong". As you said, it's personal preference
     
  16. Offline

    Zombie_Striker

    @AdamQpzm
    If he learned to format by using Python, why may have it in his head that he can reference didx from another class/method. If he learned from C# or C++, he may come into different problems such as referencing before the instance is created.
     
  17. @Zombie_Striker So? If he doesn't know how to language works (which I suspect may be the case) then that's obviously a problem. But the difference in formatting is not a problem, changing from Allman to K&R will get him no closer to understanding.
     
  18. Offline

    Zombie_Striker

    @AdamQpzm
    The formatting is not a problem, it just shows that there is a possibility that he has learned a different language and possibly hasn't learn Java yet. My original post was just to see if he learned another language and used the things he knows to code for Java.


    @PressDev
    Did you learn Java?
     
  19. Offline

    xTrollxDudex

Thread Status:
Not open for further replies.

Share This Page