aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/de/torui/coflsky/CoflSky.java2
-rw-r--r--src/main/java/de/torui/coflsky/CoflSkyCommand.java30
-rw-r--r--src/main/java/de/torui/coflsky/Config.java1
-rw-r--r--src/main/java/de/torui/coflsky/handlers/DescriptionHandler.java91
-rw-r--r--src/main/java/de/torui/coflsky/handlers/EventRegistry.java20
-rw-r--r--src/main/java/de/torui/coflsky/network/WSClient.java15
-rw-r--r--src/main/java/de/torui/coflsky/network/WSClientWrapper.java7
7 files changed, 117 insertions, 49 deletions
diff --git a/src/main/java/de/torui/coflsky/CoflSky.java b/src/main/java/de/torui/coflsky/CoflSky.java
index 57af091..53c3fd3 100644
--- a/src/main/java/de/torui/coflsky/CoflSky.java
+++ b/src/main/java/de/torui/coflsky/CoflSky.java
@@ -45,7 +45,7 @@ public class CoflSky
"ws://sky-mod.coflnet.com/modsocket",
};
- public static String CommandUri = "https://sky.coflnet.com/api/mod/commands";
+ public static String CommandUri = Config.BaseUrl + "/api/mod/commands";
@EventHandler
public void preInit(FMLPreInitializationEvent event) {
String configString = null;
diff --git a/src/main/java/de/torui/coflsky/CoflSkyCommand.java b/src/main/java/de/torui/coflsky/CoflSkyCommand.java
index 5b21274..de82f12 100644
--- a/src/main/java/de/torui/coflsky/CoflSkyCommand.java
+++ b/src/main/java/de/torui/coflsky/CoflSkyCommand.java
@@ -95,8 +95,18 @@ public class CoflSkyCommand extends CommandBase {
// + "g º87,000 -> 13,999 ºg[BUY]\\\",\\\"onClick\\\":\\\"/viewauction f7d7295ca72f43e9876bf6da7424000c\\\",\\\"hover\\\":\\\"\\\"}\"}"), sender.getCommandSenderEntity());
//WSCommandHandler.HandleCommand(new Command(CommandType.PlaySound, "{\"name\":\"random.orb\",\"pitch\":0.5}"), sender.getCommandSenderEntity());
break;
- case "callback":
- CallbackCommand(args);
+ case "callback":
+ CallbackCommand(args);
+ break;
+ case "dev":
+ if(Config.BaseUrl.contains("localhost")) {
+ CoflSky.Wrapper.startConnection();
+ Config.BaseUrl = "https://sky.coflnet.com";
+ } else {
+ CoflSky.Wrapper.initializeNewSocket("ws://localhost:8009/modsocket");
+ Config.BaseUrl = "http://localhost:5005";
+ }
+ sender.addChatMessage(new ChatComponentText("toggled dev mode, now using " + Config.BaseUrl));
break;
case "status":
sender.addChatMessage(new ChatComponentText(StatusMessage()));
@@ -177,15 +187,17 @@ public class CoflSkyCommand extends CommandBase {
if(CoflSky.Wrapper.isRunning) {
CoflSky.Wrapper.SendMessage(rc);
} else {
- sender.addChatMessage(new ChatComponentText("CoflSky wasn't active.").setChatStyle(new ChatStyle().setColor(EnumChatFormatting.RED)));
- CoflSky.Wrapper.stop();
- CoflSky.Wrapper.startConnection();
- CoflSky.Wrapper.SendMessage(rc);
+ SendAfterStart(sender, rc);
}
-
-
}
-
+
+ private static synchronized void SendAfterStart(ICommandSender sender, RawCommand rc) {
+ sender.addChatMessage(new ChatComponentText("CoflSky wasn't active.").setChatStyle(new ChatStyle().setColor(EnumChatFormatting.RED)));
+ //CoflSky.Wrapper.stop();
+ CoflSky.Wrapper.startConnection();
+ CoflSky.Wrapper.SendMessage(rc);
+ }
+
public void ListHelp(ICommandSender sender) {
sender.addChatMessage(new ChatComponentText(HelpText));
sender.addChatMessage(new ChatComponentText(QueryServerCommands.QueryCommands()));
diff --git a/src/main/java/de/torui/coflsky/Config.java b/src/main/java/de/torui/coflsky/Config.java
index 314d83b..25040b9 100644
--- a/src/main/java/de/torui/coflsky/Config.java
+++ b/src/main/java/de/torui/coflsky/Config.java
@@ -2,4 +2,5 @@ package de.torui.coflsky;
public class Config {
public static final int KeepFlipsForSeconds = 42;
+ public static String BaseUrl = "https://sky.coflnet.com";
} \ No newline at end of file
diff --git a/src/main/java/de/torui/coflsky/handlers/DescriptionHandler.java b/src/main/java/de/torui/coflsky/handlers/DescriptionHandler.java
index 48643bf..34d89ad 100644
--- a/src/main/java/de/torui/coflsky/handlers/DescriptionHandler.java
+++ b/src/main/java/de/torui/coflsky/handlers/DescriptionHandler.java
@@ -1,5 +1,6 @@
package de.torui.coflsky.handlers;
+import de.torui.coflsky.Config;
import de.torui.coflsky.network.QueryServerCommands;
import de.torui.coflsky.network.WSClient;
import net.minecraft.client.gui.inventory.GuiChest;
@@ -16,8 +17,10 @@ import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
+import java.util.List;
public class DescriptionHandler {
@@ -38,6 +41,14 @@ public class DescriptionHandler {
public static final DescModification[] EMPTY_ARRAY = new DescModification[0];
public static final NBTTagCompound EMPTY_COMPOUND = new NBTTagCompound();
+ private boolean IsOpen = true;
+ private boolean shouldUpdate = false;
+
+ public void Close()
+ {
+ IsOpen = false;
+ }
+
public static String ExtractStackableIdFromItemStack(ItemStack stack) {
if (stack != null) {
try {
@@ -66,7 +77,7 @@ public class DescriptionHandler {
}
return "";
}
- private static DescModification[] getTooltipData(ItemStack itemStack) {
+ private DescModification[] getTooltipData(ItemStack itemStack) {
if (tooltipItemMap.containsKey(itemStack)) {
return tooltipItemMap.getOrDefault(itemStack, EMPTY_ARRAY);
}
@@ -75,34 +86,55 @@ public class DescriptionHandler {
if (tooltipItemUuidMap.containsKey(id)) {
return tooltipItemUuidMap.getOrDefault(id, EMPTY_ARRAY);
}
+ shouldUpdate = true;
} else {
String itemId = ExtractStackableIdFromItemStack(itemStack);
if(tooltipItemIdMap.containsKey(itemId)){
return tooltipItemIdMap.getOrDefault(itemId, EMPTY_ARRAY);
}
+ shouldUpdate = true;
}
return EMPTY_ARRAY;
}
- public static void getTooltipDataFromBackend(GuiOpenEvent event){
-
- InventoryWrapper wrapper = new InventoryWrapper();
+ public void loadDescriptionAndListenForChanges(GuiOpenEvent event){
GuiContainer gc = (GuiContainer) event.gui;
- if (event.gui instanceof GuiChest) {
- ContainerChest chest = (ContainerChest) ((GuiChest) event.gui).inventorySlots;
- for(int i = 1; i < 10; i++) {
- if(gc.inventorySlots.inventorySlots.get(gc.inventorySlots.inventorySlots.size()-37).getStack() != null)
- break;
- try {
- // incremental backoff to wait for all inventory packages to arrive (each slot is sent individually)
- Thread.sleep(20 * i);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
+ loadDescriptionForInventory(event, gc, false);
+ int iteration = 0;
+ while(IsOpen)
+ {
+ iteration++;
+ try {
+ Thread.sleep(300 + iteration);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ if(shouldUpdate || iteration % 10 == 0 && hasAnyStackChanged(gc))
+ {
+ shouldUpdate = false;
+ loadDescriptionForInventory(event, gc, true);
+ }
+ }
+ }
+
+ private static boolean hasAnyStackChanged(GuiContainer gc) {
+ for (Slot obj : gc.inventorySlots.inventorySlots) {
+ ItemStack stack = obj.getStack();
+ if (stack != null && !tooltipItemMap.containsKey(stack)) {
+ return true;
}
+ }
+ return false;
+ }
+
+ private static void loadDescriptionForInventory(GuiOpenEvent event, GuiContainer gc, boolean skipLoadCheck) {
+ InventoryWrapper wrapper = new InventoryWrapper();
+ if (event.gui instanceof GuiChest && !skipLoadCheck){
+ waitForChestContentLoad(event, gc);
+ ContainerChest chest = (ContainerChest) ((GuiChest) event.gui).inventorySlots;
IInventory inv = chest.getLowerChestInventory();
if (inv.hasCustomName()) {
String chestName = inv.getName();
@@ -129,13 +161,17 @@ public class DescriptionHandler {
wrapper.fullInventoryNbt = Base64.getEncoder().encodeToString(baos.toByteArray());
+ List<ItemStack> stacks = new ArrayList<>();
+ for (Slot obj : gc.inventorySlots.inventorySlots) {
+ stacks.add(obj.getStack());
+ }
+
String data = WSClient.gson.toJson(wrapper);
- String info = QueryServerCommands.PostRequest("https://sky.coflnet.com/api/mod/description/modifications", data);
+ String info = QueryServerCommands.PostRequest(Config.BaseUrl + "/api/mod/description/modifications", data);
DescModification[][] arr = WSClient.gson.fromJson(info, DescModification[][].class);
int i = 0;
- for (Slot obj : gc.inventorySlots.inventorySlots) {
- ItemStack stack = obj.getStack();
+ for (ItemStack stack : stacks) {
tooltipItemMap.put(stack, arr[i]);
String uuid = ExtractUuidFromItemStack(stack);
if(uuid.length()>0) tooltipItemUuidMap.put(uuid, arr[i]);
@@ -149,7 +185,21 @@ public class DescriptionHandler {
e.printStackTrace();
}
}
- public static void setTooltips(ItemTooltipEvent event) {
+
+ private static void waitForChestContentLoad(GuiOpenEvent event, GuiContainer gc) {
+ for(int i = 1; i < 10; i++) {
+ if(gc.inventorySlots.inventorySlots.get(gc.inventorySlots.inventorySlots.size()-37).getStack() != null)
+ break;
+ try {
+ // incremental backoff to wait for all inventory packages to arrive (each slot is sent individually)
+ Thread.sleep(20 * i);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ public void setTooltips(ItemTooltipEvent event) {
DescModification[] data = getTooltipData(event.itemStack);
if (data == null || data.length == 0)
@@ -177,6 +227,9 @@ public class DescriptionHandler {
}
}
+ /**
+ * Called when the inventory is closed
+ */
public static void emptyTooltipData(){
tooltipItemMap.clear();
tooltipItemIdMap.clear();
diff --git a/src/main/java/de/torui/coflsky/handlers/EventRegistry.java b/src/main/java/de/torui/coflsky/handlers/EventRegistry.java
index aaee721..bd5cd25 100644
--- a/src/main/java/de/torui/coflsky/handlers/EventRegistry.java
+++ b/src/main/java/de/torui/coflsky/handlers/EventRegistry.java
@@ -57,6 +57,7 @@ public class EventRegistry {
}
public long LastClick = System.currentTimeMillis();
+ private DescriptionHandler descriptionHandler;
@SideOnly(Side.CLIENT)
@SubscribeEvent(priority = EventPriority.NORMAL, receiveCanceled = true)
@@ -162,8 +163,7 @@ public class EventRegistry {
IInventory inv = chest.getLowerChestInventory();
if (inv.hasCustomName()) { // verify that the chest actually has a custom name
String chestName = inv.getName();
-
- if (chestName.equalsIgnoreCase("BIN Auction View") || chestName.equalsIgnoreCase("Ekwav")) {
+ if (chestName.equalsIgnoreCase("BIN Auction View")) {
ItemStack heldItem = Minecraft.getMinecraft().thePlayer.inventory.getItemStack();
@@ -217,15 +217,25 @@ public class EventRegistry {
@SubscribeEvent(priority = EventPriority.LOWEST)
public void onGuiOpen(GuiOpenEvent event) {
if (!config.extendedtooltips) return;
- emptyTooltipData();
+ if(descriptionHandler != null)
+ descriptionHandler.Close();
+ if(event.gui == null)
+ emptyTooltipData();
+
if (!(event.gui instanceof GuiContainer)) return;
new Thread(() -> {
- getTooltipDataFromBackend(event);
+ try {
+ descriptionHandler = new DescriptionHandler();
+ descriptionHandler.loadDescriptionAndListenForChanges(event);
+ } catch (Exception e)
+ {
+ System.out.println("failed to update description " + e);
+ }
}).start();
}
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void onItemTooltipEvent(ItemTooltipEvent event) {
if (!config.extendedtooltips) return;
- setTooltips(event);
+ descriptionHandler.setTooltips(event);
}
}
diff --git a/src/main/java/de/torui/coflsky/network/WSClient.java b/src/main/java/de/torui/coflsky/network/WSClient.java
index 46fdbb4..f306751 100644
--- a/src/main/java/de/torui/coflsky/network/WSClient.java
+++ b/src/main/java/de/torui/coflsky/network/WSClient.java
@@ -38,7 +38,7 @@ public class WSClient extends WebSocketAdapter {
})*/.create();
}
public URI uri;
- public WebSocket socket;
+ private WebSocket socket;
public boolean shouldRun = false;
public WebSocketState currentState = WebSocketState.CLOSED;
@@ -79,19 +79,10 @@ public class WSClient extends WebSocketAdapter {
public void stop() {
System.out.println("Closing Socket");
- // socket.sendClose();
+ if(socket == null)
+ return;
socket.clearListeners();
-
socket.disconnect();
- /*try {
- socket.getConnectedSocket().close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (WebSocketException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }*/
System.out.println("Socket closed");
}
diff --git a/src/main/java/de/torui/coflsky/network/WSClientWrapper.java b/src/main/java/de/torui/coflsky/network/WSClientWrapper.java
index 044a3e2..eccc0e7 100644
--- a/src/main/java/de/torui/coflsky/network/WSClientWrapper.java
+++ b/src/main/java/de/torui/coflsky/network/WSClientWrapper.java
@@ -35,7 +35,6 @@ public class WSClientWrapper {
}
public void restartWebsocketConnection() {
- socket.socket.clearListeners();
socket.stop();
System.out.println("Sleeping...");
@@ -103,9 +102,11 @@ public class WSClientWrapper {
String coflSessionID = CoflSessionManager.GetCoflSession(username).SessionUUID;
uri += "&SId=" + coflSessionID;
-
+
+ if(socket != null)
+ socket.stop();
socket = new WSClient(URI.create(uri));
-
+ isRunning = false;
boolean successfull = start();
if(successfull) {
socket.shouldRun = true;