Util Serialization Functions.

Discussion in 'Resources' started by PhantomUnicorns, Aug 22, 2016.

?

Is this useful to you

  1. Yes

    50.0%
  2. No

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

    PhantomUnicorns

    Code:
    public static String compress (String[][] strings) {
            String ser = new String();
            ser = ser + strings.length + blockSep;
            ser = ser + strings[0].length + blockSep;
            for (int k = 0;k<strings.length/2;k+=2) {
                String[] part = strings[k];
                String[] part2 = strings[k+1];
                for (int i = 0;i<part.length;i++) {
                    ser = ser + part[i] + sep + part2[i] + blockSep;
                }
            }
            return ser;
        }
    
        public static String[][] uncompress (String string) {
            String[] blocks = string.split(blockSep);
            String[][] uncompress = new String[Integer.valueOf(blocks[0])][Integer.valueOf(blocks[1])];
            for (int i = 2;i<blocks.length;i++) {
                String[] attribute = blocks[i].split(sep);
                uncompress[0][i-2] = attribute[0];
                uncompress[1][i-2] = attribute[1];
            }
            return uncompress;
        }
    
         public static String[] getValues (String string, int level, int index) {
            if (uncompress(string).length>=level) {
                if (uncompress(string)[level].length>=index) {
                    String[] value = {
                            uncompress(string)[level-1][index-1],
                            uncompress(string)[level][index-1]
                    };
                    return value;
                }
            }
            return null;
        }
     
        public static String getValue (String string, int level, int index) {
            if (uncompress(string).length>=level)
                if (uncompress(string)[level].length>=index)
                return uncompress(string)[level-1][index-1] + "==" + uncompress(string)[level][index-1];
            return null;
        }
     
        public static String getValue (String string, int level, String var) {
            for (int i = 0;i<uncompress(string)[level-1].length;i++) {
                if (uncompress(string)[level-1][i].equals(var)) {
                    return uncompress(string)[level][i];
                }
            }
            return null;
        }
    
    That can be used as a universal serializer, example below
    Code:
    String[][] compress = {
                            {
                                "w",
                                "x",
                                "y",
                                "z"
                            },
                            {
                                "1",
                                "2",
                                "3",
                                "4"
                            }
                    };
    Bukkit.broadcastMessage(Save.compress(compress));
    Bukkit.broadcastMessage(Save.getValue(Save.compress(compress), 1, 2)); // Returns 2
    Bukkit.broadcastMessage(Save.getValue(Save.compress(compress), 1, "y")); // Returns 2
    
    Example two, which is pretty much what you would use it for
    Code:
    String[] item = new String[10];
                    for (int i = 0;i<10;i++)
                        if (player.getInventory().getItem(i)!=null)
                            item[i] = player.getInventory().getItem(i).getAmount() + "";
                        else
                            item[i] = "0";
                    String[][] compress = {
                            {
                                "1",
                                "2",
                                "3",
                                "4",
                                "5",
                                "6",
                                "7",
                                "8",
                                "9",
                                "10"
                            },
                            item
                    };
                    Bukkit.broadcastMessage(Save.compress(compress));
                    Bukkit.broadcastMessage(Save.getValue(Save.compress(compress), 1, 2));
                    Bukkit.broadcastMessage(Save.getValue(Save.compress(compress), 1, "1"));
    
     
    Last edited: Aug 22, 2016
Thread Status:
Not open for further replies.

Share This Page