|
| 1 | +package coffee.waffle.emcutils.features; |
| 2 | + |
| 3 | +import coffee.waffle.emcutils.utils.EmpireServer; |
| 4 | +import coffee.waffle.emcutils.utils.Log; |
| 5 | +import coffee.waffle.emcutils.utils.Util; |
| 6 | +import com.google.common.collect.Lists; |
| 7 | +import com.google.gson.JsonObject; |
| 8 | +import com.google.gson.JsonParser; |
| 9 | +import lombok.AllArgsConstructor; |
| 10 | +import lombok.Data; |
| 11 | +import net.minecraft.util.ActionResult; |
| 12 | +import org.apache.commons.lang3.math.NumberUtils; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.net.URL; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Scanner; |
| 18 | +import java.util.stream.IntStream; |
| 19 | + |
| 20 | +public class VisitResidenceHandler { |
| 21 | + public static List<EmpireResidence> residences = Lists.newArrayList(); |
| 22 | + |
| 23 | + public static ActionResult handleVisitCommand(String[] args) { |
| 24 | + if (args == null) { |
| 25 | + return ActionResult.PASS; |
| 26 | + } |
| 27 | + |
| 28 | + String res = args[0]; |
| 29 | + String loc = args.length > 1 ? args[1] : ""; |
| 30 | + |
| 31 | + if (res.contains("@")) { |
| 32 | + String[] split = res.split("@"); |
| 33 | + |
| 34 | + res = split[0]; |
| 35 | + loc = (split.length > 1 ? split[1] : ""); |
| 36 | + } |
| 37 | + |
| 38 | + EmpireServer server = getResidenceServer(res); |
| 39 | + |
| 40 | + if (server != EmpireServer.NULL_SERVER && server != Util.getCurrentServer()) { |
| 41 | + Util.getOnJoinCommandQueue().add("v " + res + " " + loc); |
| 42 | + |
| 43 | + server.sendToServer(); |
| 44 | + |
| 45 | + return ActionResult.FAIL; |
| 46 | + } |
| 47 | + |
| 48 | + return ActionResult.PASS; |
| 49 | + } |
| 50 | + |
| 51 | + public static ActionResult handleHomeCommand(String[] args) { |
| 52 | + int num = 1; |
| 53 | + String loc = ""; |
| 54 | + |
| 55 | + if (args != null) { |
| 56 | + if (args.length == 1) { |
| 57 | + if (NumberUtils.isParsable(args[0])) { |
| 58 | + num = Integer.parseInt(args[0]); |
| 59 | + } else { |
| 60 | + loc = args[0]; |
| 61 | + } |
| 62 | + } else if (args.length == 2) { |
| 63 | + if (NumberUtils.isParsable(args[0])) { |
| 64 | + num = Integer.parseInt(args[0]); |
| 65 | + loc = args[1]; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + String resName = Util.getPlayer().getEntityName() + (num > 1 ? "-" + num : ""); |
| 71 | + |
| 72 | + EmpireServer server = getResidenceServer(resName); |
| 73 | + |
| 74 | + if (server != EmpireServer.NULL_SERVER && server != Util.getCurrentServer()) { |
| 75 | + Util.getOnJoinCommandQueue().add("v " + resName + " " + loc); |
| 76 | + |
| 77 | + server.sendToServer(); |
| 78 | + |
| 79 | + return ActionResult.FAIL; |
| 80 | + } |
| 81 | + |
| 82 | + return ActionResult.PASS; |
| 83 | + } |
| 84 | + |
| 85 | + public static void loadResidences() { |
| 86 | + residences.clear(); |
| 87 | + |
| 88 | + Thread loader = new Thread(() -> { |
| 89 | + IntStream.rangeClosed(1, 10).forEach(serverId -> { |
| 90 | + try { |
| 91 | + URL url = new URL("https://empireminecraft.com/api/server-residence-" + serverId + ".json"); |
| 92 | + Scanner scanner = new Scanner(url.openStream()); |
| 93 | + String json = scanner.nextLine(); |
| 94 | + scanner.close(); |
| 95 | + |
| 96 | + JsonObject obj = new JsonParser().parse(json).getAsJsonObject(); |
| 97 | + |
| 98 | + obj.entrySet().stream().map(e -> obj.get(e.getKey()).getAsJsonObject()).forEach(resObj -> { |
| 99 | + int address = resObj.get("address").getAsInt(); |
| 100 | + String name = resObj.has("name") ? resObj.get("name").getAsString() : null; |
| 101 | + |
| 102 | + residences.add(new EmpireResidence(address, serverId, name)); |
| 103 | + }); |
| 104 | + } catch (IOException e) { |
| 105 | + e.printStackTrace(); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + Log.info("Loaded " + residences.size() + " residences"); |
| 110 | + }); |
| 111 | + |
| 112 | + loader.setName("loader"); |
| 113 | + loader.start(); |
| 114 | + } |
| 115 | + |
| 116 | + public static EmpireServer getResidenceServer(String name) { |
| 117 | + for (EmpireResidence residence : residences) { |
| 118 | + if (name.contains(".")) { |
| 119 | + name = name.split("\\.")[0]; |
| 120 | + } |
| 121 | + |
| 122 | + if (NumberUtils.isParsable(name)) { |
| 123 | + return getResidenceServer(Integer.parseInt(name)); |
| 124 | + } |
| 125 | + |
| 126 | + if (residence.getName() != null && residence.getName().equalsIgnoreCase(name)) { |
| 127 | + return EmpireServer.getById(residence.serverId); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return EmpireServer.NULL_SERVER; |
| 132 | + } |
| 133 | + |
| 134 | + public static EmpireServer getResidenceServer(int address) { |
| 135 | + for (EmpireResidence residence : residences) { |
| 136 | + if (residence.getAddress() == address) { |
| 137 | + return EmpireServer.getById(residence.serverId); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return EmpireServer.NULL_SERVER; |
| 142 | + } |
| 143 | + |
| 144 | + @Data |
| 145 | + @AllArgsConstructor |
| 146 | + private static class EmpireResidence { |
| 147 | + private int address; |
| 148 | + private int serverId; |
| 149 | + private String name; |
| 150 | + } |
| 151 | +} |
0 commit comments