diff options
author | Torui <44932079+ToruiDev@users.noreply.github.com> | 2022-05-21 16:11:01 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-21 16:11:01 +0200 |
commit | b9b1294882b68e2d85aec0c1d28570f5d68359b9 (patch) | |
tree | 599b996aa7844a4bc9d989d75f879c5a065e6c55 | |
parent | 4a85bf6754c101b43d2d0d3a6c9ed5744dd0308d (diff) | |
download | COFL-b9b1294882b68e2d85aec0c1d28570f5d68359b9.tar.gz COFL-b9b1294882b68e2d85aec0c1d28570f5d68359b9.tar.bz2 COFL-b9b1294882b68e2d85aec0c1d28570f5d68359b9.zip |
initial implementation (#38)
* rewrite everything
* Add replace and insert methods for tooltips
* Add sleep bcz hypixel doesnt want a prefilled chest
* Commit requested changes
* Fix messed up item stacks for stackables
* Move handlers to handlers package
* Empty hashmap on gui close
* merge conflixt fix
* Fix index out of bound exception
* Fix bcz forge
* fix messed slot assigns when there is empoty inventory slot
* Make tooltips respect mod settings
* Fix typo
* save tab contents
* optimise description loading & fix tab
Co-authored-by: HackedOS <63157139+HackedOS@users.noreply.github.com>
Co-authored-by: Äkwav <xekwavx@gmail.com>
-rwxr-xr-x[-rw-r--r--] | gradlew | 0 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/CoflSky.java | 3 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/WSCommandHandler.java | 2 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/commands/CommandType.java | 2 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/configuration/Configuration.java | 5 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/configuration/LocalConfig.java | 6 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/handlers/DescriptionHandler.java | 182 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/handlers/EventHandler.java (renamed from src/main/java/de/torui/coflsky/EventHandler.java) | 23 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/handlers/EventRegistry.java (renamed from src/main/java/de/torui/coflsky/EventRegistry.java) | 29 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/network/QueryServerCommands.java | 49 |
10 files changed, 278 insertions, 23 deletions
diff --git a/src/main/java/de/torui/coflsky/CoflSky.java b/src/main/java/de/torui/coflsky/CoflSky.java index 7ecccf0..9811ec7 100644 --- a/src/main/java/de/torui/coflsky/CoflSky.java +++ b/src/main/java/de/torui/coflsky/CoflSky.java @@ -10,6 +10,7 @@ import java.nio.file.Paths; import com.google.gson.Gson; import de.torui.coflsky.configuration.LocalConfig; +import de.torui.coflsky.handlers.EventRegistry; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import org.lwjgl.input.Keyboard; @@ -44,7 +45,7 @@ public class CoflSky "ws://sky-mod.coflnet.com/modsocket", }; - public static String CommandUri = "https://sky-commands.coflnet.com/api/mod/commands"; + public static String CommandUri = "https://sky.coflnet.com/api/mod/commands"; @EventHandler public void preInit(FMLPreInitializationEvent event) { String configString = null; diff --git a/src/main/java/de/torui/coflsky/WSCommandHandler.java b/src/main/java/de/torui/coflsky/WSCommandHandler.java index 39c4495..ca583a2 100644 --- a/src/main/java/de/torui/coflsky/WSCommandHandler.java +++ b/src/main/java/de/torui/coflsky/WSCommandHandler.java @@ -10,7 +10,7 @@ import de.torui.coflsky.commands.models.FlipData; import de.torui.coflsky.commands.models.SoundData; import de.torui.coflsky.configuration.ConfigurationManager; import de.torui.coflsky.commands.models.TimerData; -import de.torui.coflsky.network.WSClient; +import de.torui.coflsky.handlers.EventRegistry; import net.minecraft.client.Minecraft; import net.minecraft.client.audio.PositionedSoundRecord; import net.minecraft.client.audio.SoundHandler; diff --git a/src/main/java/de/torui/coflsky/commands/CommandType.java b/src/main/java/de/torui/coflsky/commands/CommandType.java index 533af1e..7f196d4 100644 --- a/src/main/java/de/torui/coflsky/commands/CommandType.java +++ b/src/main/java/de/torui/coflsky/commands/CommandType.java @@ -51,6 +51,8 @@ public enum CommandType { updateLocation, @SerializedName("chatBatch") chatBatch, + @SerializedName("uploadTab") + uploadTab, ; public static Map<CommandType,String> data; static { diff --git a/src/main/java/de/torui/coflsky/configuration/Configuration.java b/src/main/java/de/torui/coflsky/configuration/Configuration.java index 1ef040f..58ca4fd 100644 --- a/src/main/java/de/torui/coflsky/configuration/Configuration.java +++ b/src/main/java/de/torui/coflsky/configuration/Configuration.java @@ -1,7 +1,7 @@ package de.torui.coflsky.configuration; import de.torui.coflsky.CoflSky; -import de.torui.coflsky.EventRegistry; +import de.torui.coflsky.handlers.EventRegistry; import java.util.regex.Pattern; @@ -21,6 +21,7 @@ public class Configuration { public static void setInstance(Configuration config) { instance = config; CoflSky.config.autoStart = instance.autoStart; + CoflSky.config.extendedtooltips = instance.extendDescriptions; EventRegistry.chatpattern = Pattern.compile(instance.chatRegex, Pattern.CASE_INSENSITIVE);; } @@ -36,7 +37,7 @@ public class Configuration { public boolean collectEntities; public boolean autoStart; @Description("Wherever or not to send item descriptions for extending to the server") - public boolean ExtendDescriptions; + public boolean extendDescriptions; @Description("Chat input starting with one of these prefixes is sent to the server") public String[] CommandPrefixes; diff --git a/src/main/java/de/torui/coflsky/configuration/LocalConfig.java b/src/main/java/de/torui/coflsky/configuration/LocalConfig.java index 28909c4..5c16fca 100644 --- a/src/main/java/de/torui/coflsky/configuration/LocalConfig.java +++ b/src/main/java/de/torui/coflsky/configuration/LocalConfig.java @@ -11,8 +11,10 @@ import java.nio.file.Paths; public class LocalConfig { public boolean autoStart; - public LocalConfig(boolean autoStart) { + public boolean extendedtooltips; + public LocalConfig(boolean autoStart,boolean extendedtooltips) { this.autoStart = autoStart; + this.extendedtooltips = extendedtooltips; } public static void saveConfig(File file, LocalConfig Config) { Gson gson = new Gson(); @@ -28,6 +30,6 @@ public class LocalConfig { } public static LocalConfig createDefaultConfig() { - return new LocalConfig(false); + return new LocalConfig(false,true); } } diff --git a/src/main/java/de/torui/coflsky/handlers/DescriptionHandler.java b/src/main/java/de/torui/coflsky/handlers/DescriptionHandler.java new file mode 100644 index 0000000..08eb819 --- /dev/null +++ b/src/main/java/de/torui/coflsky/handlers/DescriptionHandler.java @@ -0,0 +1,182 @@ +package de.torui.coflsky.handlers; + +import de.torui.coflsky.network.QueryServerCommands; +import de.torui.coflsky.network.WSClient; +import net.minecraft.client.gui.inventory.GuiChest; +import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.inventory.ContainerChest; +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.CompressedStreamTools; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraftforge.client.event.GuiOpenEvent; +import net.minecraftforge.event.entity.player.ItemTooltipEvent; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Base64; +import java.util.HashMap; + +public class DescriptionHandler { + + private static class InventoryWrapper { + public String chestName; + public String fullInventoryNbt; + } + private static class DescModification { + public String type; + public String value; + public int line; + } + + public static HashMap<ItemStack, DescModification[]> tooltipItemMap = new HashMap<>(); + public static HashMap<String, DescModification[]> tooltipItemUuidMap = new HashMap<>(); + public static HashMap<String, DescModification[]> tooltipItemIdMap = new HashMap<>(); + + public static final DescModification[] EMPTY_ARRAY = new DescModification[0]; + public static final NBTTagCompound EMPTY_COMPOUND = new NBTTagCompound(); + + public static String ExtractStackableIdFromItemStack(ItemStack stack) { + if (stack != null) { + try { + String uuid = stack.serializeNBT().getCompoundTag("tag").getCompoundTag("ExtraAttributes") + .getString("id") + ":" + stack.stackSize; + if (uuid.length() == 0) { + throw new Exception(); + } + return uuid; + } catch (Exception e) { + } + } + return ""; + } + public static String ExtractUuidFromItemStack(ItemStack stack) { + if (stack != null) { + try { + String uuid = stack.serializeNBT().getCompoundTag("tag").getCompoundTag("ExtraAttributes") + .getString("uuid"); + if (uuid.length() == 0) { + throw new Exception(); + } + return uuid; + } catch (Exception e) { + } + } + return ""; + } + private static DescModification[] getTooltipData(ItemStack itemStack) { + if (tooltipItemMap.containsKey(itemStack)) { + return tooltipItemMap.getOrDefault(itemStack, EMPTY_ARRAY); + } + if(!itemStack.isStackable()){ + String id = ExtractUuidFromItemStack(itemStack); + if (tooltipItemUuidMap.containsKey(id)) { + return tooltipItemUuidMap.getOrDefault(id, EMPTY_ARRAY); + } + } else { + String itemId = ExtractStackableIdFromItemStack(itemStack); + if(tooltipItemIdMap.containsKey(itemId)){ + return tooltipItemIdMap.getOrDefault(itemId, EMPTY_ARRAY); + } + } + + return EMPTY_ARRAY; + } + public static void getTooltipDataFromBackend(GuiOpenEvent event){ + + InventoryWrapper wrapper = new InventoryWrapper(); + + 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(); + } + } + + IInventory inv = chest.getLowerChestInventory(); + if (inv.hasCustomName()) { + String chestName = inv.getName(); + wrapper.chestName = chestName; + } + } + + NBTTagCompound compound = new NBTTagCompound(); + NBTTagList tl = new NBTTagList(); + + for (Slot obj : gc.inventorySlots.inventorySlots) { + ItemStack stack = obj.getStack(); + if (stack != null) { + tl.appendTag(stack.serializeNBT()); + } else { + tl.appendTag(EMPTY_COMPOUND); + } + } + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try { + compound.setTag("i", tl); + CompressedStreamTools.writeCompressed(compound, baos); + + wrapper.fullInventoryNbt = Base64.getEncoder().encodeToString(baos.toByteArray()); + + String data = WSClient.gson.toJson(wrapper); + String info = QueryServerCommands.PostRequest("https://sky.coflnet.com/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(); + tooltipItemMap.put(stack, arr[i]); + String uuid = ExtractUuidFromItemStack(stack); + if(uuid.length()>0) tooltipItemUuidMap.put(uuid, arr[i]); + + String id = ExtractStackableIdFromItemStack(stack); + if(id.length()>0) tooltipItemIdMap.put(id, arr[i]); + i++; + } + + } catch (IOException e) { + e.printStackTrace(); + } + } + public static void setTooltips(ItemTooltipEvent event) { + DescModification[] data = getTooltipData(event.itemStack); + + if (data == null || data.length == 0) + return; + + for (DescModification datum : data) { + if (!(event.toolTip.size() >= datum.line)) return; + switch (datum.type) { + case "APPEND": + event.toolTip.add(datum.value); + break; + case "REPLACE": + event.toolTip.set(datum.line, datum.value); + break; + case "INSERT": + event.toolTip.add(datum.line, datum.value); + break; + case "DELETE": + event.toolTip.remove(datum.line); + break; + } + } + } + + public static void emptyTooltipData(){ + tooltipItemMap.clear(); + tooltipItemIdMap.clear(); + tooltipItemUuidMap.clear(); + } +} diff --git a/src/main/java/de/torui/coflsky/EventHandler.java b/src/main/java/de/torui/coflsky/handlers/EventHandler.java index 55bd4ba..59db467 100644 --- a/src/main/java/de/torui/coflsky/EventHandler.java +++ b/src/main/java/de/torui/coflsky/handlers/EventHandler.java @@ -1,5 +1,6 @@ -package de.torui.coflsky; +package de.torui.coflsky.handlers; +import de.torui.coflsky.CoflSky; import de.torui.coflsky.commands.Command; import de.torui.coflsky.commands.CommandType; import de.torui.coflsky.configuration.Configuration; @@ -13,12 +14,10 @@ import net.minecraft.scoreboard.Scoreboard; import net.minecraft.util.ChatComponentText; import net.minecraft.util.ChatStyle; import net.minecraft.util.EnumChatFormatting; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; +import java.util.*; import static de.torui.coflsky.CoflSky.config; +import static java.lang.Integer.parseInt; public class EventHandler { @@ -39,6 +38,14 @@ public class EventHandler { } } } + + public static void UploadTabData() { + if (!CoflSky.Wrapper.isRunning) + return; + Command<List<String>> data = new Command<>(CommandType.uploadTab, getTabList()); + CoflSky.Wrapper.SendMessage(data); + } + public static void ScoreboardData() { String s; try { @@ -126,6 +133,7 @@ public class EventHandler { server = server_; Command<String> data = new Command<>(CommandType.updateServer, server); CoflSky.Wrapper.SendMessage(data); + UploadTabData(); } } else if (line.contains("area:")) { String location_ = line.split("area: ")[1]; @@ -164,7 +172,7 @@ public class EventHandler { if (line.contains("purse") || line.contains("piggy")) { int purse_ = 0; try { - purse_ = Integer.parseInt(line.split(": ")[1].replace(",", "")); + purse_ = parseInt(line.split(" ")[1].replace(",", "")); } catch (NumberFormatException e) { e.printStackTrace(); } @@ -176,7 +184,7 @@ public class EventHandler { } else if (line.contains("bits")) { int bits_ = 0; try { - bits_ = Integer.parseInt(line.split(": ")[1].replace(",", "")); + bits_ = parseInt(line.split(" ")[1].replace(",", "")); } catch (NumberFormatException e) { e.printStackTrace(); } @@ -187,5 +195,4 @@ public class EventHandler { } } } - } diff --git a/src/main/java/de/torui/coflsky/EventRegistry.java b/src/main/java/de/torui/coflsky/handlers/EventRegistry.java index 9d82f14..d2b8113 100644 --- a/src/main/java/de/torui/coflsky/EventRegistry.java +++ b/src/main/java/de/torui/coflsky/handlers/EventRegistry.java @@ -1,4 +1,4 @@ -package de.torui.coflsky; +package de.torui.coflsky.handlers; import java.time.LocalDateTime; import java.util.concurrent.ExecutorService; @@ -8,7 +8,9 @@ import java.util.regex.Pattern; import com.mojang.realmsclient.util.Pair; +import de.torui.coflsky.CoflSky; import de.torui.coflsky.FlipHandler.Flip; +import de.torui.coflsky.WSCommandHandler; import de.torui.coflsky.commands.Command; import de.torui.coflsky.commands.CommandType; import de.torui.coflsky.commands.JsonStringCommand; @@ -17,15 +19,18 @@ import de.torui.coflsky.configuration.Configuration; import de.torui.coflsky.network.WSClient; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.inventory.GuiChest; +import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.inventory.ContainerChest; import net.minecraft.inventory.IInventory; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.util.ResourceLocation; import net.minecraftforge.client.event.ClientChatReceivedEvent; +import net.minecraftforge.client.event.GuiOpenEvent; import net.minecraftforge.client.event.GuiScreenEvent; import net.minecraftforge.client.event.RenderGameOverlayEvent; import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType; +import net.minecraftforge.event.entity.player.ItemTooltipEvent; import net.minecraftforge.fml.common.eventhandler.EventPriority; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.InputEvent.KeyInputEvent; @@ -35,8 +40,9 @@ import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.fml.common.gameevent.TickEvent; -import static de.torui.coflsky.EventHandler.ScoreboardData; -import static de.torui.coflsky.EventHandler.TabMenuData; +import static de.torui.coflsky.CoflSky.config; +import static de.torui.coflsky.handlers.DescriptionHandler.*; +import static de.torui.coflsky.handlers.EventHandler.*; public class EventRegistry { public static Pattern chatpattern = Pattern.compile("a^", Pattern.CASE_INSENSITIVE); @@ -120,7 +126,7 @@ public class EventRegistry { return uuid; } catch (Exception e) { System.out.println("Clicked item " + stack.getDisplayName() + " has the following meta: " - + stack.serializeNBT().toString()); + + stack.serializeNBT()); } } return ""; @@ -214,4 +220,19 @@ public class EventRegistry { }); } } + + @SubscribeEvent(priority = EventPriority.LOWEST) + public void onGuiOpen(GuiOpenEvent event) { + if (!config.extendedtooltips) return; + emptyTooltipData(); + if (!(event.gui instanceof GuiContainer)) return; + new Thread(() -> { + getTooltipDataFromBackend(event); + }).start(); + } + @SubscribeEvent(priority = EventPriority.HIGHEST) + public void onItemTooltipEvent(ItemTooltipEvent event) { + if (!config.extendedtooltips) return; + setTooltips(event); + } } diff --git a/src/main/java/de/torui/coflsky/network/QueryServerCommands.java b/src/main/java/de/torui/coflsky/network/QueryServerCommands.java index 0bf54a3..67f8f2a 100644 --- a/src/main/java/de/torui/coflsky/network/QueryServerCommands.java +++ b/src/main/java/de/torui/coflsky/network/QueryServerCommands.java @@ -1,9 +1,6 @@ package de.torui.coflsky.network; -import java.io.BufferedInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.util.Arrays; @@ -12,6 +9,8 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import de.torui.coflsky.CoflSky; +import de.torui.coflsky.minecraft_integration.CoflSessionManager; +import de.torui.coflsky.minecraft_integration.PlayerDataProvider; public class QueryServerCommands { @@ -37,7 +36,7 @@ public class QueryServerCommands { } - return "§4ERROR: Could not connect to command server!"; + return "§4ERROR: Could not connect to command server!"; } private static class CommandInfo { @@ -102,4 +101,44 @@ public class QueryServerCommands { return null; } + public static String PostRequest(String uri, String data) { + try { + String username = PlayerDataProvider.getUsername(); + URL url = new URL(uri); + HttpURLConnection con; + con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod("POST"); + + con.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); + con.setRequestProperty("Accept", "application/json"); + con.setRequestProperty("User-Agent", "CoflMod"); + con.setRequestProperty("conId", CoflSessionManager.GetCoflSession(username).SessionUUID); + con.setRequestProperty("uuid",username); + con.setDoInput(true); + con.setDoOutput(true); + // ... + + OutputStream os = con.getOutputStream(); + byte[] bytes = data.getBytes("UTF-8"); + os.write(bytes); + os.close(); + + InputStream in = new BufferedInputStream(con.getInputStream()); + ByteArrayOutputStream result = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + for (int length; (length = in.read(buffer)) != -1; ) { + result.write(buffer, 0, length); + } + // StandardCharsets.UTF_8.name() > JDK 7 + String resString = result.toString("UTF-8"); + + System.out.println("Result= " + resString); + return resString; + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return null; + } } |