Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
remove duplicate functions and put upload/download functions in addon…
Browse files Browse the repository at this point in the history
… configuration
  • Loading branch information
RealMuffinTime committed Nov 24, 2023
1 parent ead7997 commit 769c43f
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,121 +17,196 @@
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")
public class UUIDCollectorConfiguration extends AddonConfig {

@SwitchSetting
private final ConfigProperty<Boolean> enabled = new ConfigProperty<>(true);
@MethodOrder(after = "getOnServer")
@TextFieldSetting
private final ConfigProperty<String> collectionServer = new ConfigProperty<>(
"https://users.northernsi.de/");
@MethodOrder(after = "collectionServer")
@TextFieldSetting
private final ConfigProperty<String> 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<Boolean> 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<Boolean> 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<WebInputStream> 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<String> collectionServer = new ConfigProperty<>(
"https://users.northernsi.de/");

public ConfigProperty<String> collectionServer() {
return this.collectionServer;
}
@MethodOrder(after = "collectionServer")
@TextFieldSetting
private final ConfigProperty<String> authenticationKey = new ConfigProperty<>("Your AuthKey");

public ConfigProperty<String> authenticationKey() {
return this.authenticationKey;
Expand Down
Loading

0 comments on commit 769c43f

Please sign in to comment.