diff --git a/core/src/main/java/de/northernside/uuidcollector/UUIDCollector.java b/core/src/main/java/de/northernside/uuidcollector/UUIDCollector.java index 7b2596d..f227c88 100644 --- a/core/src/main/java/de/northernside/uuidcollector/UUIDCollector.java +++ b/core/src/main/java/de/northernside/uuidcollector/UUIDCollector.java @@ -2,26 +2,13 @@ // Here it is. :) package de.northernside.uuidcollector; -import com.google.gson.Gson; import de.northernside.uuidcollector.hud.InCollectionHUD; import de.northernside.uuidcollector.hud.OnServerHUD; import de.northernside.uuidcollector.listener.PlayerInfoAddListener; import de.northernside.uuidcollector.listener.ServerDisconnectListener; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; -import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.UUID; -import de.northernside.uuidcollector.misc.UUIDJsonModel; -import net.labymod.api.Laby; import net.labymod.api.addon.LabyAddon; -import net.labymod.api.client.component.Component; -import net.labymod.api.client.gui.icon.Icon; -import net.labymod.api.client.resources.ResourceLocation; import net.labymod.api.models.addon.annotation.AddonMain; import net.labymod.api.notification.Notification; @@ -41,66 +28,7 @@ protected void enable() { this.labyAPI().hudWidgetRegistry().register(new OnServerHUD()); this.logger().info("Enabled UUIDCollector Addon."); - getOnServerCollection(configuration().collectionServer().get(), configuration().authenticationKey().get()); - } - - public static Integer getOnServerCollection(String address, String key) { - veryClassy classy = new veryClassy(address, key); - Thread thread = new Thread(classy); - thread.start(); - try { - thread.join(); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - return classy.getAmount(); - } - - - - static class veryClassy implements Runnable { - private Integer amount = -1; - private String address; - private String key; - - public veryClassy(String address, String key) { - this.address = address; - this.key = key; - } - - @Override - public void run(){ - try { - URL url = new URL( address + "api/key/" + key); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - InputStream inputStream = connection.getInputStream(); - UUIDJsonModel json = new Gson().fromJson(new InputStreamReader(inputStream, StandardCharsets.UTF_8), UUIDJsonModel.class); - - if (connection.getResponseCode() != 200) { - Notification errorNotification = Notification.builder() - .icon(Component.icon(Icon.texture(ResourceLocation.create("uuidcollector", "textures/icon.png")).aspectRatio(10, 10)) - .getIcon()) - .title(Component.text("Error " + connection.getResponseCode())) - .text(Component.text("The collection server responded with an error.")) - .duration(4500) - .build(); - - Laby.labyAPI().minecraft().executeOnRenderThread( - () -> Laby.labyAPI().notificationController().push(errorNotification)); - } else { - - amount = json.getLength(); - OnServerHUD.updateOnServer(amount); - } - } catch (IOException exception) { -// UUIDCollector.this.logger().error("IOException", exception); - exception.printStackTrace(); - } - } - - public Integer getAmount() { - return amount; - } + this.configuration().getOnServer(); } @Override diff --git a/core/src/main/java/de/northernside/uuidcollector/UUIDCollectorConfiguration.java b/core/src/main/java/de/northernside/uuidcollector/UUIDCollectorConfiguration.java index aa90db5..f8d2d55 100644 --- a/core/src/main/java/de/northernside/uuidcollector/UUIDCollectorConfiguration.java +++ b/core/src/main/java/de/northernside/uuidcollector/UUIDCollectorConfiguration.java @@ -17,7 +17,15 @@ import net.labymod.api.configuration.loader.property.ConfigProperty; import net.labymod.api.notification.Notification; import net.labymod.api.util.MethodOrder; +import net.labymod.api.util.io.web.WebInputStream; import net.labymod.api.util.io.web.request.Request; +import net.labymod.api.util.io.web.request.Response; +import java.net.HttpURLConnection; +import java.net.URL; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.IOException; +import java.nio.charset.StandardCharsets; @SuppressWarnings("FieldMayBeFinal") @ConfigName("settings") @@ -25,113 +33,180 @@ public class UUIDCollectorConfiguration extends AddonConfig { @SwitchSetting private final ConfigProperty enabled = new ConfigProperty<>(true); - @MethodOrder(after = "getOnServer") - @TextFieldSetting - private final ConfigProperty collectionServer = new ConfigProperty<>( - "https://users.northernsi.de/"); - @MethodOrder(after = "collectionServer") - @TextFieldSetting - private final ConfigProperty authenticationKey = new ConfigProperty<>("Your AuthKey"); - @MethodOrder(after = "enabled") - @ButtonSetting(translation = "uuidcollector.settings.uploadUUIDs.text") - public void uploadUUIDs() { - int uuidAmount = UUIDCollector.users.size(); - if (uuidAmount == 0) { - Notification noUUIDsNotification = Notification.builder() - .icon(Component.icon(Icon.texture(ResourceLocation.create("uuidcollector", "textures/icon.png")).aspectRatio(10, 10)) - .getIcon()) - .title(Component.text("Nothing to upload!")) - .text(Component.text("Uploaded 0 UUIDs to the collection server")) - .duration(4000) - .build(); - - Laby.labyAPI().minecraft().executeOnRenderThread( - () -> Laby.labyAPI().notificationController().push(noUUIDsNotification)); - return; - } - - Thread uploadThread = new Thread(() -> { - // Allows collection size to be put back accurately, by using a temporary system while it looks up the UUIDs. - UUIDCollector.tempCollection.putAll(UUIDCollector.users); - UUIDCollector.users.clear(); - - try { - UUIDPostRequestModel usersRequestModel = new UUIDPostRequestModel( - UUIDCollector.tempCollection); - String usersJson = new Gson().toJson(usersRequestModel); - Request.ofString() - .url(this.collectionServer.get() + "api/donate/" + authenticationKey.get()) - .json(usersJson) - .async().execute(result -> { - UUIDJsonModel json = new Gson().fromJson(result.get(), UUIDJsonModel.class); - Notification uploadedNotification = Notification.builder() - .icon(Component.icon( - Icon.texture(ResourceLocation.create("uuidcollector", "textures/icon.png")).aspectRatio(10, 10)) - .getIcon()) - .title(Component.text("Uploaded UUIDs!")) - .text(Component.text("Uploaded " + json.getValid() + " valid UUIDs. Total UUIDs: " + json.getLength())) - .duration(8000) - .build(); - - Laby.labyAPI().notificationController().push(uploadedNotification); - UUIDCollector.tempCollection.clear(); - InCollectionHUD.updateInCollection(0); - OnServerHUD.updateOnServer(json.getLength()); - }); - } catch (Exception exception) { - exception.printStackTrace(); - } - }); - - uploadThread.start(); - try { - uploadThread.join(); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } + @Override + public ConfigProperty enabled() { + return this.enabled; } - @MethodOrder(after = "uploadUUIDs") + @MethodOrder(after = "enabled") @ButtonSetting(translation = "uuidcollector.settings.getInCollection.text") public void getInCollection() { int uuidAmount = UUIDCollector.users.size(); - Notification noUUIDsNotification = Notification.builder() + Notification notification = Notification.builder() .icon(Component.icon(Icon.texture(ResourceLocation.create("uuidcollector", "textures/icon.png")).aspectRatio(10, 10)) .getIcon()) .title(Component.text("UUIDCollector")) - .text(Component.text("You've collected " + uuidAmount + " UUIDs.")) - .duration(3500) + .text(Component.text("You've collected locally " + uuidAmount + " UUIDs.")) + .duration(4000) .build(); Laby.labyAPI().minecraft().executeOnRenderThread( - () -> Laby.labyAPI().notificationController().push(noUUIDsNotification)); + () -> Laby.labyAPI().notificationController().push(notification)); } @MethodOrder(after = "getInCollection") @ButtonSetting(translation = "uuidcollector.settings.getOnServer.text") public void getOnServer() { - String uuidAmount = UUIDCollector.getOnServerCollection(collectionServer.get(), authenticationKey.get()).toString(); - Notification noUUIDsNotification = Notification.builder() + class veryClassy implements Runnable { + private String error; + private UUIDJsonModel json; + + public veryClassy() {} + + @Override + public void run() { + try { + URL url = new URL(collectionServer.get() + "api/key/" + authenticationKey.get()); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + InputStream inputStream = connection.getInputStream(); + UUIDJsonModel json = new Gson().fromJson( + new InputStreamReader(inputStream, StandardCharsets.UTF_8), UUIDJsonModel.class); + + if (connection.getResponseCode() != 200) { + error = "The collection server responded with an error " + connection.getResponseCode() + "."; + } else { + this.json = json; + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public UUIDJsonModel getJson() { + return json; + } + + public String getError() { + return error; + } + } + + veryClassy classy = new veryClassy(); + Thread thread = new Thread(classy); + thread.start(); + try { + thread.join(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + String text; + if (classy.getError() == null) { + text = "There are " + classy.getJson().getLength() + " UUIDs on the server."; + OnServerHUD.updateOnServer(classy.getJson().getLength()); + } else { + text = classy.getError(); + OnServerHUD.updateOnServer(-1); + } + + Notification notification = Notification.builder() .icon(Component.icon(Icon.texture(ResourceLocation.create("uuidcollector", "textures/icon.png")).aspectRatio(10, 10)) .getIcon()) .title(Component.text("UUIDCollector")) - .text(Component.text("There are " + uuidAmount + " UUIDs on the server.")) - .duration(3500) + .text(Component.text(text)) + .duration(4000) .build(); Laby.labyAPI().minecraft().executeOnRenderThread( - () -> Laby.labyAPI().notificationController().push(noUUIDsNotification)); + () -> Laby.labyAPI().notificationController().push(notification)); } - @Override - public ConfigProperty enabled() { - return this.enabled; + @MethodOrder(after = "getOnServer") + @ButtonSetting(translation = "uuidcollector.settings.uploadToServer.text") + public void uploadToServer() { + class veryClassy implements Runnable { + private String error; + private UUIDJsonModel json; + + public veryClassy() {} + + @Override + public void run() { + // Allows collection size to be put back accurately, by using a temporary system while it looks up the UUIDs. + UUIDCollector.tempCollection.putAll(UUIDCollector.users); + UUIDCollector.users.clear(); + + UUIDPostRequestModel usersRequestModel = new UUIDPostRequestModel( + UUIDCollector.tempCollection); + String usersJson = new Gson().toJson(usersRequestModel); + Response response = Request.ofInputStream() + .url(collectionServer.get() + "api/donate/" + authenticationKey.get()) + .json(usersJson).executeSync(); + + if (response.getStatusCode() != 200) { + error = "The collection server responded with an error " + response.getStatusCode() + "."; + UUIDCollector.users.putAll(UUIDCollector.tempCollection); + InCollectionHUD.updateInCollection(UUIDCollector.users.size()); + } else { + json = new Gson().fromJson(new InputStreamReader(response.get(), StandardCharsets.UTF_8), UUIDJsonModel.class); + UUIDCollector.tempCollection.clear(); + InCollectionHUD.updateInCollection(0); + } + } + + public UUIDJsonModel getJson() { + return json; + } + + public String getError() { + return error; + } + } + + veryClassy classy = new veryClassy(); + Thread thread = new Thread(classy); + thread.start(); + try { + thread.join(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + String text; + int duration = 4000; + if (classy.getError() == null) { + text = "Uploaded " + classy.getJson().getValid() + " valid UUIDs. Total UUIDs: " + classy.getJson().getLength(); + OnServerHUD.updateOnServer(classy.getJson().getLength()); + duration = 8000; + } else { + text = classy.getError(); + OnServerHUD.updateOnServer(-1); + } + + Notification notification = Notification.builder() + .icon(Component.icon( + Icon.texture(ResourceLocation.create("uuidcollector", "textures/icon.png")).aspectRatio(10, 10)) + .getIcon()) + .title(Component.text("UUIDCollector")) + .text(Component.text(text)) + .duration(duration) + .build(); + + Laby.labyAPI().minecraft().executeOnRenderThread( + () -> Laby.labyAPI().notificationController().push(notification)); } + @MethodOrder(after = "uploadToServer") + @TextFieldSetting + private final ConfigProperty collectionServer = new ConfigProperty<>( + "https://users.northernsi.de/"); public ConfigProperty collectionServer() { return this.collectionServer; } + @MethodOrder(after = "collectionServer") + @TextFieldSetting + private final ConfigProperty authenticationKey = new ConfigProperty<>("Your AuthKey"); public ConfigProperty authenticationKey() { return this.authenticationKey; diff --git a/core/src/main/java/de/northernside/uuidcollector/listener/ServerDisconnectListener.java b/core/src/main/java/de/northernside/uuidcollector/listener/ServerDisconnectListener.java index 998f010..c385c17 100644 --- a/core/src/main/java/de/northernside/uuidcollector/listener/ServerDisconnectListener.java +++ b/core/src/main/java/de/northernside/uuidcollector/listener/ServerDisconnectListener.java @@ -1,17 +1,8 @@ package de.northernside.uuidcollector.listener; -import com.google.gson.Gson; import de.northernside.uuidcollector.UUIDCollector; -import de.northernside.uuidcollector.hud.InCollectionHUD; -import de.northernside.uuidcollector.misc.UUIDPostRequestModel; -import net.labymod.api.Laby; -import net.labymod.api.client.component.Component; -import net.labymod.api.client.gui.icon.Icon; -import net.labymod.api.client.resources.ResourceLocation; import net.labymod.api.event.Subscribe; import net.labymod.api.event.client.network.server.ServerDisconnectEvent; -import net.labymod.api.notification.Notification; -import net.labymod.api.util.io.web.request.Request; public class ServerDisconnectListener { @@ -23,51 +14,7 @@ public ServerDisconnectListener(UUIDCollector addon) { @Subscribe public void onServerDisconnect(ServerDisconnectEvent event) { - int uuidAmount = UUIDCollector.users.size(); - if (uuidAmount == 0) { - return; - } - - Thread uploadThread = new Thread(() -> { - // Allows collection size to be put back accurately, by using a temporary system while it looks up the UUIDs. - UUIDCollector.tempCollection.putAll(UUIDCollector.users); - UUIDCollector.users.clear(); - - try { - UUIDPostRequestModel usersRequestModel = new UUIDPostRequestModel( - UUIDCollector.tempCollection); - String usersJson = new Gson().toJson(usersRequestModel); - Request.ofString() - .url(addon.configuration().collectionServer().get() + "api/donate/" + addon.configuration().authenticationKey().get()) - .json(usersJson) - .async().execute(result -> { - Notification uploadedNotification = Notification.builder() - .icon(Component.icon( - Icon.texture( - ResourceLocation.create("uuidcollector", "textures/icon.png")).aspectRatio(10, 10)) - .getIcon()) - .title(Component.text("Uploaded UUIDs!")) - .text(Component.text("Uploaded " + uuidAmount + " UUIDs to the collection server.")) - .duration(8000) - .build(); - - Laby.labyAPI().notificationController().push(uploadedNotification); - UUIDCollector.tempCollection.clear(); - InCollectionHUD.updateInCollection(0); - }); - } catch (Exception exception) { - exception.printStackTrace(); - } - }); - - uploadThread.start(); - try { - uploadThread.join(); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - - UUIDCollector.getOnServerCollection(addon.configuration().collectionServer().get(), addon.configuration().authenticationKey().get()); + addon.configuration().uploadToServer(); } } diff --git a/core/src/main/resources/assets/uuidcollector/i18n/en_us.json b/core/src/main/resources/assets/uuidcollector/i18n/en_us.json index de47732..a6ea93b 100644 --- a/core/src/main/resources/assets/uuidcollector/i18n/en_us.json +++ b/core/src/main/resources/assets/uuidcollector/i18n/en_us.json @@ -5,7 +5,7 @@ "enabled": { "name": "Enabled" }, - "uploadUUIDs": { + "uploadToServer": { "name": "Upload UUIDs", "text": "Click" },