Adds some changes to begin the switch to UUID based homes.
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
package com.majinnaibu.bukkitplugins.metropolis;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
@@ -40,7 +39,6 @@ import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisHomeMoveComman
|
||||
import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisPlotGoCommand;
|
||||
import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisPlotReserveCommand;
|
||||
import com.majinnaibu.bukkitplugins.metropolis.eventlisteners.PlayerJoinListener;
|
||||
import com.sk89q.util.yaml.YAMLProcessor;
|
||||
import com.sk89q.worldedit.BlockVector;
|
||||
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
|
||||
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
|
||||
@@ -68,9 +66,9 @@ public class MetropolisPlugin extends JavaPlugin {
|
||||
public RegionManager regionManager = null;
|
||||
|
||||
private List<Plot> _occupiedPlots;
|
||||
private HashMap<String, List<Plot>> _ownedPlots;
|
||||
private HashMap<UUID, List<Plot>> _ownedPlots;
|
||||
private HashMap<String, UserOverride> _userOverrides;
|
||||
private HashMap<String, Integer> _currentHomes;
|
||||
private HashMap<UUID, Integer> _currentHomes;
|
||||
|
||||
private PlayerJoinListener _playerJoinListener = null;
|
||||
|
||||
@@ -119,16 +117,17 @@ public class MetropolisPlugin extends JavaPlugin {
|
||||
public void onEnable() {
|
||||
pdf = getDescription();
|
||||
|
||||
_ownedPlots = new HashMap<String, List<Plot>>();
|
||||
_ownedPlots = new HashMap<UUID, List<Plot>>();
|
||||
_userOverrides = new HashMap<String, UserOverride>();
|
||||
_currentHomes = new HashMap<String, Integer>();
|
||||
_currentHomes = new HashMap<UUID, Integer>();
|
||||
loadCurrentHomes();
|
||||
|
||||
if(DEBUG){log.info("Checking config");}
|
||||
Configuration config = getConfig();
|
||||
if(!config.contains("version")){
|
||||
//new
|
||||
if(DEBUG){log.info("No config exists. Assuming new installation.");}
|
||||
config.set("version", version);
|
||||
|
||||
}else{
|
||||
int configVersion = safeGetIntFromConfig(config, "version");
|
||||
if(configVersion < version){
|
||||
@@ -137,14 +136,10 @@ public class MetropolisPlugin extends JavaPlugin {
|
||||
//upgrade config
|
||||
config.set("version", version);
|
||||
}
|
||||
saveConfig();
|
||||
if(DEBUG){log.info("Config updated");}
|
||||
}
|
||||
}
|
||||
|
||||
config.set("version", version);
|
||||
saveConfig();
|
||||
|
||||
config.options().copyDefaults(true);
|
||||
|
||||
if(DEBUG){log.info("Reading configuration from file.");}
|
||||
@@ -167,7 +162,7 @@ public class MetropolisPlugin extends JavaPlugin {
|
||||
spawnFloorMaterial = safeGetMaterialFromConfig(config, "spawn.material");
|
||||
generateWall = safeGetBooleanFromConfig(config, "wall.generate");
|
||||
wallMaterial = safeGetMaterialFromConfig(config, "wall.material");
|
||||
wallHeight = safeGetIntFromConfig(config, "wall.material");
|
||||
wallHeight = safeGetIntFromConfig(config, "wall.height");
|
||||
worldName = safeGetStringFromConfig(config, "worldname");
|
||||
_maxPlots = safeGetIntFromConfig(config, "plot.multiplier");
|
||||
_plotMultiplier = safeGetIntFromConfig(config, "plot.maxPerPlayer");
|
||||
@@ -302,20 +297,20 @@ public class MetropolisPlugin extends JavaPlugin {
|
||||
}
|
||||
|
||||
private void loadCurrentHomes() {
|
||||
YAMLProcessor processor = new YAMLProcessor(new File(getDataFolder(), "currentHomes.yml"), true);
|
||||
try {
|
||||
processor.load();
|
||||
} catch (IOException e) {
|
||||
log.info(e.toString());
|
||||
return;
|
||||
}
|
||||
|
||||
Set<String> keys = processor.getMap().keySet();
|
||||
|
||||
_currentHomes.clear();
|
||||
for(String username : keys){
|
||||
_currentHomes.put(username, processor.getInt(username, 0));
|
||||
}
|
||||
// YAMLProcessor processor = new YAMLProcessor(new File(getDataFolder(), "currentHomes.yml"), true);
|
||||
// try {
|
||||
// processor.load();
|
||||
// } catch (IOException e) {
|
||||
// log.info(e.toString());
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// Set<String> keys = processor.getMap().keySet();
|
||||
//
|
||||
// _currentHomes.clear();
|
||||
// for(String username : keys){
|
||||
// _currentHomes.put(username, processor.getInt(username, 0));
|
||||
// }
|
||||
}
|
||||
|
||||
private void buildUserOverrides() {
|
||||
@@ -413,7 +408,7 @@ public class MetropolisPlugin extends JavaPlugin {
|
||||
}
|
||||
|
||||
private void throwInvalidConfigException() {
|
||||
log.info("Metropolis: ERROR config file is invalid. Please correct Metropolis/config.yml and restart the server.");
|
||||
log.info("Metropolis: ERROR config file is invalid. Please correct or delete plugins/Metropolis/config.yml and restart the server.");
|
||||
throw new RuntimeException("Config file is invalid.");
|
||||
}
|
||||
|
||||
@@ -457,12 +452,12 @@ public class MetropolisPlugin extends JavaPlugin {
|
||||
ProtectedCuboidRegion cuboidRegion = (ProtectedCuboidRegion) region;
|
||||
if(cuboidRegion.getId().startsWith("h_")){
|
||||
PlayerHome home = PlayerHome.get(region);
|
||||
if(!_currentHomes.containsKey(home.getPlayerName()))
|
||||
if(!_currentHomes.containsKey(home.getPlayerUUID()))
|
||||
{
|
||||
_currentHomes.put(home.getPlayerName(), home.getNumber());
|
||||
_currentHomes.put(home.getPlayerUUID(), home.getNumber());
|
||||
}
|
||||
_occupiedPlots.add(home);
|
||||
addOwnedPlot(home.getPlayerName(), home);
|
||||
addOwnedPlot(home.getPlayerUUID(), home);
|
||||
}else if(cuboidRegion.getId().startsWith("r_")){
|
||||
_occupiedPlots.add(Plot.get(cuboidRegion));
|
||||
}
|
||||
@@ -472,14 +467,14 @@ public class MetropolisPlugin extends JavaPlugin {
|
||||
size=calculateCitySize();
|
||||
}
|
||||
|
||||
private void addOwnedPlot(String substring, Plot plot) {
|
||||
if(_ownedPlots.containsKey(substring)){
|
||||
List<Plot> plots = _ownedPlots.get(substring);
|
||||
private void addOwnedPlot(UUID playerUUID, Plot plot) {
|
||||
if(_ownedPlots.containsKey(playerUUID)){
|
||||
List<Plot> plots = _ownedPlots.get(playerUUID);
|
||||
plots.add(plot);
|
||||
}else{
|
||||
List<Plot> plots = new ArrayList<Plot>();
|
||||
plots.add(plot);
|
||||
_ownedPlots.put(substring, plots);
|
||||
_ownedPlots.put(playerUUID, plots);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -491,14 +486,14 @@ public class MetropolisPlugin extends JavaPlugin {
|
||||
public PlayerHome getPlayerHome(Player player) {
|
||||
PlayerHome home = null;
|
||||
|
||||
String regionName = "h_" + player.getName();
|
||||
String regionName = "h_1_" + player.getUniqueId().toString();
|
||||
ProtectedRegion homeRegion = regionManager.getRegion(regionName);
|
||||
|
||||
if(homeRegion == null){
|
||||
if(DEBUG){
|
||||
log.info(String.format("Creating home for player %s", player.getName()));
|
||||
}
|
||||
home = generateHome(player.getName());
|
||||
home = generateHome(player);
|
||||
}else{
|
||||
home = new PlayerHome(homeRegion);
|
||||
}
|
||||
@@ -833,7 +828,7 @@ public class MetropolisPlugin extends JavaPlugin {
|
||||
return (cuboid.minZ - roadWidth/2)/gridSizeZ;
|
||||
}
|
||||
|
||||
private void setHomeOccupied(String owner, BlockVector minimumPoint, BlockVector maximumPoint) {
|
||||
private void setHomeOccupied(OfflinePlayer owner, BlockVector minimumPoint, BlockVector maximumPoint) {
|
||||
|
||||
PlayerHome home = new PlayerHome(owner, minimumPoint, maximumPoint);
|
||||
if(!_occupiedPlots.contains(home)){
|
||||
@@ -841,13 +836,13 @@ public class MetropolisPlugin extends JavaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
public PlayerHome generateHome(String playerName) {
|
||||
int multiplier = getPlotMultiplier(playerName);
|
||||
public PlayerHome generateHome(OfflinePlayer playerName) {
|
||||
int multiplier = getPlotMultiplier(playerName.getName());
|
||||
|
||||
if(DEBUG){log.info(String.format("Generating home for %s", playerName));}
|
||||
if(DEBUG){log.info(String.format("Generating home for %s", playerName.getName()));}
|
||||
Cuboid homeCuboid = null;
|
||||
ProtectedRegion phomeRegion = null;
|
||||
String regionName = "h_1_" + playerName;
|
||||
String regionName = "h_1_" + playerName.getUniqueId();
|
||||
phomeRegion = regionManager.getRegion(regionName);
|
||||
if(phomeRegion != null){
|
||||
return PlayerHome.get(phomeRegion);
|
||||
@@ -867,7 +862,7 @@ public class MetropolisPlugin extends JavaPlugin {
|
||||
newHomeRegion.setFlag(DefaultFlag.TNT, StateFlag.State.DENY);
|
||||
|
||||
DefaultDomain d = newHomeRegion.getOwners();
|
||||
d.addPlayer(playerName);
|
||||
d.addPlayer(playerName.getName());
|
||||
newHomeRegion.setPriority(1);
|
||||
|
||||
regionManager.addRegion(newHomeRegion);
|
||||
@@ -904,10 +899,10 @@ public class MetropolisPlugin extends JavaPlugin {
|
||||
|
||||
if(DEBUG){log.info(String.format("generateSign: %s", String.valueOf(generateSign)));}
|
||||
if(generateSign){
|
||||
generateSign(homeCuboid, playerName);
|
||||
generateSign(homeCuboid, playerName.getName());
|
||||
}
|
||||
|
||||
if(DEBUG){log.info(String.format("Done generating home for %s", playerName));}
|
||||
if(DEBUG){log.info(String.format("Done generating home for %s", playerName.getName()));}
|
||||
|
||||
return new PlayerHome(newHomeRegion);
|
||||
}
|
||||
@@ -1021,7 +1016,7 @@ public class MetropolisPlugin extends JavaPlugin {
|
||||
|
||||
public void assignPlot(OfflinePlayer player) {
|
||||
//PlayerHome home = generateHome(player.getName());
|
||||
generateHome(player.getName());
|
||||
generateHome(player);
|
||||
}
|
||||
|
||||
private int getPlotMultiplier(String name) {
|
||||
@@ -1047,12 +1042,33 @@ public class MetropolisPlugin extends JavaPlugin {
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public Player getPlayer(String name) {
|
||||
return getServer().getPlayer(name);
|
||||
}
|
||||
Player player = null;
|
||||
UUID playerUUID = null;
|
||||
|
||||
try {
|
||||
playerUUID = UUID.fromString(name);
|
||||
player = Bukkit.getPlayer(playerUUID);
|
||||
} catch (IllegalArgumentException ex) {
|
||||
player = Bukkit.getPlayer(name);
|
||||
}
|
||||
|
||||
return player; }
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public OfflinePlayer getOfflinePlayer(String name){
|
||||
return getServer().getOfflinePlayer(name);
|
||||
OfflinePlayer player = null;
|
||||
UUID playerUUID = null;
|
||||
|
||||
try {
|
||||
playerUUID = UUID.fromString(name);
|
||||
player = Bukkit.getOfflinePlayer(playerUUID);
|
||||
} catch (IllegalArgumentException ex) {
|
||||
player = Bukkit.getOfflinePlayer(name);
|
||||
}
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
public String teleportPlayerToPlot(Player player, Plot plot) {
|
||||
@@ -1065,9 +1081,9 @@ public class MetropolisPlugin extends JavaPlugin {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean homeExists(String playerName, int homeNumber) {
|
||||
public boolean homeExists(UUID playerUUID, int homeNumber) {
|
||||
for(Plot plot: _occupiedPlots){
|
||||
if(plot.getRegionName().equalsIgnoreCase(String.format("h_%d_%s", homeNumber, playerName))){
|
||||
if(plot.getRegionName().equalsIgnoreCase(String.format("h_%d_%s", homeNumber, playerUUID.toString()))){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1075,8 +1091,8 @@ public class MetropolisPlugin extends JavaPlugin {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setHome(String name, int newHomeNumber) {
|
||||
_currentHomes.put(name, newHomeNumber);
|
||||
public void setHome(UUID playerUUID, int newHomeNumber) {
|
||||
_currentHomes.put(playerUUID, newHomeNumber);
|
||||
saveCurrentHomes();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package com.majinnaibu.bukkitplugins.metropolis;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
import com.avaje.ebean.validation.NotNull;
|
||||
import com.sk89q.worldedit.BlockVector;
|
||||
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
|
||||
@@ -12,22 +16,30 @@ import com.sk89q.worldguard.protection.regions.ProtectedRegion;
|
||||
@Table(name="Metropolis_PlayerHome")
|
||||
public class PlayerHome extends Plot{
|
||||
@NotNull
|
||||
private String playerName;
|
||||
public String getPlayerName(){return this.playerName;}
|
||||
public void setPlayerName(String playerName){this.playerName = playerName;}
|
||||
private UUID playerUUID = null;
|
||||
public UUID getPlayerUUID(){return this.playerUUID;}
|
||||
public void setPlayerUUID(UUID playerUUID){this.playerUUID = playerUUID;}
|
||||
|
||||
private int number;
|
||||
|
||||
public PlayerHome(String owner, BlockVector min, BlockVector max) {
|
||||
super("h_" + owner, min, max);
|
||||
this.playerName = owner;
|
||||
public PlayerHome(UUID owner, BlockVector min, BlockVector max) {
|
||||
super("h_1_" + owner, min, max);
|
||||
this.playerUUID = owner;
|
||||
}
|
||||
|
||||
public PlayerHome(OfflinePlayer owner, BlockVector min, BlockVector max) {
|
||||
super("h_1_" + owner.getUniqueId(), min, max);
|
||||
this.playerUUID = owner.getUniqueId();
|
||||
}
|
||||
|
||||
public PlayerHome() {
|
||||
this.playerName = "";
|
||||
this.playerUUID = null;
|
||||
}
|
||||
|
||||
public PlayerHome(ProtectedRegion homeRegion){
|
||||
this.playerUUID = null;
|
||||
this.number = 0;
|
||||
|
||||
try{
|
||||
String rname = homeRegion.getId();
|
||||
|
||||
@@ -36,13 +48,12 @@ public class PlayerHome extends Plot{
|
||||
if(secondUnderscore > 2){
|
||||
try{
|
||||
this.number = Integer.parseInt(rname.substring(2, secondUnderscore));
|
||||
this.playerName = rname.substring(secondUnderscore+1);
|
||||
this.playerUUID = UUID.fromString(rname.substring(secondUnderscore+1));
|
||||
}catch(Exception ex){
|
||||
this.number = 0;
|
||||
}
|
||||
}else{
|
||||
this.number = 0;
|
||||
this.playerName = rname.substring(2);
|
||||
}
|
||||
|
||||
setCuboid(new Cuboid(homeRegion.getMinimumPoint(), homeRegion.getMaximumPoint()));
|
||||
@@ -63,7 +74,7 @@ public class PlayerHome extends Plot{
|
||||
|
||||
PlayerHome otherPlayerHome = (PlayerHome)other;
|
||||
|
||||
if(!this.playerName.equals(otherPlayerHome.playerName)){
|
||||
if(!this.playerUUID.equals(otherPlayerHome.playerUUID)){
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -77,7 +88,7 @@ public class PlayerHome extends Plot{
|
||||
public String toFriendlyString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append(String.format("Metropolis Home {Owner: %s min: (%d, %d, %d) max: (%d, %d, %d)}", getPlayerName(), getCuboid().getMinX(), getCuboid().getMinY(), getCuboid().getMinZ(), getCuboid().getMaxX(), getCuboid().getMaxY(), getCuboid().getMaxZ()));
|
||||
sb.append(String.format("Metropolis Home {Owner: %s min: (%d, %d, %d) max: (%d, %d, %d)}", getPlayerUUID(), getCuboid().getMinX(), getCuboid().getMinY(), getCuboid().getMinZ(), getCuboid().getMaxX(), getCuboid().getMaxY(), getCuboid().getMaxZ()));
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.majinnaibu.bukkitplugins.metropolis.commands;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -20,7 +21,7 @@ public class MetropolisDebugGenerateTestHomesCommand implements CommandExecutor
|
||||
try{
|
||||
int numHomes = Integer.parseInt(args[0]);
|
||||
for(int i=1; i<= numHomes; i++){
|
||||
_plugin.generateHome(String.format("test%d", i));
|
||||
_plugin.generateHome(Bukkit.getOfflinePlayer(String.format("test%d", i)));
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -18,31 +18,33 @@ public class MetropolisHomeEvictCommand implements CommandExecutor {
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
//get the player and region
|
||||
String playerName = "";
|
||||
String targetUUIDString = null;
|
||||
|
||||
OfflinePlayer targetPlayer = null;
|
||||
|
||||
if(args.length == 0){
|
||||
return false;
|
||||
}
|
||||
|
||||
if(args.length >= 1){
|
||||
playerName = args[0];
|
||||
targetUUIDString = args[0];
|
||||
targetPlayer = _plugin.getOfflinePlayer(targetUUIDString);
|
||||
}
|
||||
|
||||
OfflinePlayer player = _plugin.getServer().getOfflinePlayer(playerName);
|
||||
if(player == null){
|
||||
sender.sendMessage(String.format("The requested player {%s}does not appear to exist.", playerName));
|
||||
if(targetPlayer == null){
|
||||
sender.sendMessage(String.format("The requested player {%s}does not appear to exist.", targetUUIDString));
|
||||
return false;
|
||||
}
|
||||
|
||||
ProtectedRegion region = _plugin.getRegion(String.format("h_%s", player.getName()));
|
||||
ProtectedRegion region = _plugin.getRegion(String.format("h_%s", targetPlayer.getUniqueId().toString()));
|
||||
if(region == null){
|
||||
sender.sendMessage(String.format("The player {%s} has no home to be evicted from."));
|
||||
return false;
|
||||
}
|
||||
|
||||
//remove the player as owner and/or member of the region
|
||||
region.getMembers().removePlayer(playerName);
|
||||
region.getOwners().removePlayer(playerName);
|
||||
region.getMembers().removePlayer(targetPlayer.getName());//playerName);
|
||||
region.getOwners().removePlayer(targetPlayer.getName());
|
||||
|
||||
//if the region has no owners delete the region
|
||||
if(region.getMembers().size() == 0 && region.getOwners().size() == 0){
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.majinnaibu.bukkitplugins.metropolis.commands;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -14,13 +15,14 @@ public class MetropolisHomeGenerateCommand implements CommandExecutor {
|
||||
_plugin = plugin;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if(args.length < 1){
|
||||
return false;
|
||||
}
|
||||
|
||||
_plugin.generateHome(args[0]);
|
||||
_plugin.generateHome(Bukkit.getOfflinePlayer(args[0]));
|
||||
|
||||
sender.sendMessage("[Metropolis] Home generated for " + args[0]);
|
||||
|
||||
|
||||
@@ -21,32 +21,9 @@ public class MetropolisHomeGoCommand implements CommandExecutor {
|
||||
Player player = null;
|
||||
if(sender instanceof Player){
|
||||
player = (Player)sender;
|
||||
|
||||
if(args.length >= 1){
|
||||
if(player.hasPermission("")){
|
||||
player = _plugin.getServer().getPlayer(args[0]);
|
||||
|
||||
if(player == null){
|
||||
sender.sendMessage(String.format("Unable to find player %s", args[0]));
|
||||
return false;
|
||||
}
|
||||
}else{
|
||||
sender.sendMessage("Permission denied");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
if(args.length >= 1){
|
||||
player = _plugin.getServer().getPlayer(args[0]);
|
||||
|
||||
if(player == null){
|
||||
sender.sendMessage(String.format("Unable to find player %s", args[0]));
|
||||
return false;
|
||||
}
|
||||
}else{
|
||||
sender.sendMessage("You must be a player");
|
||||
return false;
|
||||
}
|
||||
sender.sendMessage("You must be a player");
|
||||
return false;
|
||||
}
|
||||
|
||||
PlayerHome home = _plugin.getPlayerHome(player);
|
||||
|
||||
@@ -41,11 +41,11 @@ public class MetropolisHomeMoveCommand implements CommandExecutor {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(player == null || !_plugin.homeExists(player.getName(), newHomeNumber)){
|
||||
if(player == null || !_plugin.homeExists(player.getUniqueId(), newHomeNumber)){
|
||||
return false;
|
||||
}
|
||||
|
||||
_plugin.setHome(player.getName(), newHomeNumber);
|
||||
_plugin.setHome(player.getUniqueId(), newHomeNumber);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
//From evilmidget38 at https://gist.github.com/evilmidget38/a5c971d2f2b2c3b3fb37
|
||||
package com.majinnaibu.bukkitplugins.metropolis.util;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
//From evilmidget38 at https://gist.github.com/evilmidget38/26d70114b834f71fb3b4
|
||||
package com.majinnaibu.bukkitplugins.metropolis.util;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class UUIDFetcher implements Callable<Map<String, UUID>> {
|
||||
private static final double PROFILES_PER_REQUEST = 100;
|
||||
private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft";
|
||||
private final JSONParser jsonParser = new JSONParser();
|
||||
private final List<String> names;
|
||||
private final boolean rateLimiting;
|
||||
|
||||
public UUIDFetcher(List<String> names, boolean rateLimiting) {
|
||||
this.names = ImmutableList.copyOf(names);
|
||||
this.rateLimiting = rateLimiting;
|
||||
}
|
||||
|
||||
public UUIDFetcher(List<String> names) {
|
||||
this(names, true);
|
||||
}
|
||||
|
||||
public Map<String, UUID> call() throws Exception {
|
||||
Map<String, UUID> uuidMap = new HashMap<String, UUID>();
|
||||
int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);
|
||||
for (int i = 0; i < requests; i++) {
|
||||
HttpURLConnection connection = createConnection();
|
||||
String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size())));
|
||||
writeBody(connection, body);
|
||||
JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
|
||||
for (Object profile : array) {
|
||||
JSONObject jsonProfile = (JSONObject) profile;
|
||||
String id = (String) jsonProfile.get("id");
|
||||
String name = (String) jsonProfile.get("name");
|
||||
UUID uuid = UUIDFetcher.getUUID(id);
|
||||
uuidMap.put(name, uuid);
|
||||
}
|
||||
if (rateLimiting && i != requests - 1) {
|
||||
Thread.sleep(100L);
|
||||
}
|
||||
}
|
||||
return uuidMap;
|
||||
}
|
||||
|
||||
private static void writeBody(HttpURLConnection connection, String body) throws Exception {
|
||||
OutputStream stream = connection.getOutputStream();
|
||||
stream.write(body.getBytes());
|
||||
stream.flush();
|
||||
stream.close();
|
||||
}
|
||||
|
||||
private static HttpURLConnection createConnection() throws Exception {
|
||||
URL url = new URL(PROFILE_URL);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type", "application/json");
|
||||
connection.setUseCaches(false);
|
||||
connection.setDoInput(true);
|
||||
connection.setDoOutput(true);
|
||||
return connection;
|
||||
}
|
||||
|
||||
private static UUID getUUID(String id) {
|
||||
return UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" +id.substring(20, 32));
|
||||
}
|
||||
|
||||
public static byte[] toBytes(UUID uuid) {
|
||||
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
|
||||
byteBuffer.putLong(uuid.getMostSignificantBits());
|
||||
byteBuffer.putLong(uuid.getLeastSignificantBits());
|
||||
return byteBuffer.array();
|
||||
}
|
||||
|
||||
public static UUID fromBytes(byte[] array) {
|
||||
if (array.length != 16) {
|
||||
throw new IllegalArgumentException("Illegal byte array length: " + array.length);
|
||||
}
|
||||
ByteBuffer byteBuffer = ByteBuffer.wrap(array);
|
||||
long mostSignificant = byteBuffer.getLong();
|
||||
long leastSignificant = byteBuffer.getLong();
|
||||
return new UUID(mostSignificant, leastSignificant);
|
||||
}
|
||||
|
||||
public static UUID getUUIDOf(String name) throws Exception {
|
||||
return new UUIDFetcher(Arrays.asList(name)).call().get(name);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
name: Metropolis
|
||||
main: com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin
|
||||
version: 1.0
|
||||
depend: [WorldGuard]
|
||||
softdepend: [WorldEdit,CommandBook]
|
||||
version: 1.0
|
||||
database: false
|
||||
commands:
|
||||
metropolis:
|
||||
|
||||
Reference in New Issue
Block a user