-
-
Save TheGamingGrunts/3949e9e6c4bd585aa992 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.projectx.Settlements.Utils; | |
import com.google.common.collect.ImmutableList; | |
import org.json.simple.JSONObject; | |
import org.json.simple.parser.JSONParser; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.UUID; | |
import java.util.concurrent.Callable; | |
public class NameFetcher implements Callable<Map<UUID, String>> { | |
private static final String PROFILE_URL = "https://sessionserver.mojang.com/session/minecraft/profile/"; | |
private final JSONParser jsonParser = new JSONParser(); | |
private final List<UUID> uuids; | |
public NameFetcher(List<UUID> uuids) { | |
this.uuids = ImmutableList.copyOf(uuids); | |
} | |
@Override | |
public Map<UUID, String> call() throws Exception { | |
Map<UUID, String> uuidStringMap = new HashMap<UUID, String>(); | |
for (UUID uuid: uuids) { | |
HttpURLConnection connection = (HttpURLConnection) new URL(PROFILE_URL+uuid.toString().replace("-", "")).openConnection(); | |
JSONObject response = (JSONObject) jsonParser.parse(new InputStreamReader(connection.getInputStream())); | |
String name = (String) response.get("name"); | |
if (name == null) { | |
continue; | |
} | |
String cause = (String) response.get("cause"); | |
String errorMessage = (String) response.get("errorMessage"); | |
if (cause != null && cause.length() > 0) { | |
throw new IllegalStateException(errorMessage); | |
} | |
uuidStringMap.put(uuid, name); | |
} | |
return uuidStringMap; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.projectx.Settlements.Commands; | |
import java.sql.SQLException; | |
import me.projectx.Settlements.Managers.ChunkManager; | |
import me.projectx.Settlements.Managers.PlayerManager; | |
import me.projectx.Settlements.Managers.SettlementManager; | |
import me.projectx.Settlements.Models.CommandModel; | |
import me.projectx.Settlements.Models.Settlement; | |
import me.projectx.Settlements.Utils.ClaimType; | |
import me.projectx.Settlements.Utils.CommandType; | |
import me.projectx.Settlements.Utils.MessageType; | |
import me.projectx.Settlements.Utils.PlayerUtils; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.entity.Player; | |
public class CommandSettlementPlayer extends CommandModel { | |
public CommandSettlementPlayer() { | |
super("settlement.player", "/s"); | |
} | |
@Override | |
public boolean onCmd(CommandSender sender, String cml, String[] args) throws SQLException { | |
if (cml.equalsIgnoreCase("s")){ | |
if (args.length > 0){ | |
switch(args[0]){ //removed all the unnecessary stuff | |
case "id": | |
Player p = (Player)sender; | |
p.sendMessage(PlayerUtils.getNameFromUUID(p.getUniqueId())); //returns null | |
break; | |
default: | |
sender.sendMessage(MessageType.COMMAND_INVALID_ARGUMENT.getMsg() + " Type /s for help"); | |
break; | |
} | |
} | |
} | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.projectx.Settlements.Utils; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.UUID; | |
import me.projectx.Settlements.Main; | |
import org.bukkit.entity.Player; | |
import org.bukkit.scheduler.BukkitRunnable; | |
public class PlayerUtils { | |
private static Map<UUID, String> response = new HashMap<UUID, String>(); | |
public static String getNameFromUUID(final UUID id){ | |
new BukkitRunnable(){ | |
public void run(){ | |
NameFetcher nf = new NameFetcher(Arrays.asList(id)); | |
try { | |
response = nf.call(); | |
} catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
}.runTaskAsynchronously(Main.getInstance()); | |
return response.get(id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment