jamig
jamig

Reputation: 23

Getting multiple UUIDs in Json from URL

I am working on a cosmetics base for a Minecraft client I'm making. I am using a json file on GitHub (here) with UUIDs of players which I am trying to give cosmetics to, I am trying to put UUIDs into a List which gives those players the item (the names don't do anything, I just have them so I know which id is who). So far I have been able to give the right cosmetic to UUIDs but only hardcoded into the client, I don't want to have to export my workspace every time I need to add someone to the list so I want to try get it to talk to a file online. Here is my current code:

private static List<UUID> players = new ArrayList<UUID>();

public static boolean hasCape(AbstractClientPlayer player) {
    
    players.add(UUID.fromString("288f696b-44d2-4915-a8cc-aa3fd5ea889c")); //jamig
    players.add(UUID.fromString("ecad61c5-6a42-4018-b1c7-6c5c19ec1cd1")); //cxplxsok
    players.add(UUID.fromString("093f3473-587b-4a2a-b50f-f4a136e9033b")); //DarylHotstuff69
    players.add(UUID.fromString("a22ab0ef-18b2-4cc6-ac17-2d9ae40a42ee")); //chfff
    
    for(UUID uuid : players) {
        if(player.getUniqueID().equals(uuid)) {
            return true;
        }
    }
    
    return false;
    
}

Upvotes: 2

Views: 572

Answers (1)

Pieter12345
Pieter12345

Reputation: 1789

You'll be best off using a library to parse json for you. In your case, com.google.code.gson.gson is included in Minecraft already. Using a json string as input (read from the file), you can parse it using something like:

String json = ...; // Your json.
JsonArray jsonArr = new JsonParser().parse(json).getAsJsonArray();
for(int i = 0; i < jsonArr.size(); i++) {
    JsonObject jsonObj = jsonArr.get(i).getAsJsonObject();
    String name = jsonObj.get("name").getAsString();
    String uuid = jsonObj.get("uuid").getAsString();
    // Do something with the name and uuid.
}

Note that you might have to put the data in your file between []'s to indicate that it is an array.

Upvotes: 1

Related Questions