diff options
author | matthias-luger <58751503+matthias-luger@users.noreply.github.com> | 2023-02-17 17:32:26 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-17 17:32:26 +0100 |
commit | 8f3bce7d6fe41e85f2654f7557bbb564305e58b5 (patch) | |
tree | 4bae9237e3c0b87bf27e5f590f4c0ca91b145cab /src/main/java/de/torui/coflsky/WSCommandHandler.java | |
parent | e43d5685fdf0d436d5836d06c5d3f15da674e7df (diff) | |
download | COFL-8f3bce7d6fe41e85f2654f7557bbb564305e58b5.tar.gz COFL-8f3bce7d6fe41e85f2654f7557bbb564305e58b5.tar.bz2 COFL-8f3bce7d6fe41e85f2654f7557bbb564305e58b5.zip |
Add TFM Purchase UI to Mod (#78)
* Add TFM Purchase UI to Mod
* fix up opening flips by hotkey
fix up flip message in tfm ui
* readded check if the auction should be drawn
mod doesnt crash anymore if a non flip auction is opened
* added /cofl set gui command
changed gui title
* added /fc toggle command
dont store toggle locally
* rename purchaseOverlay config
removed unused code
renamed gui command to /cofl set ahbuygui
* fix message not available if flip opened by mouse
fix old message shown for non flip auctions
* added /cofl setGui command
added cofl purchase gui
* Add sound on flip
* Fixed new flip sound
Optimized cofl gui
Removed flickering of default mc gui
Move mouse to middle on screen open
* remove unused code
* remove debug code
* don't open guis if not in skyblock
* fix sound being null
* fix bed flips
fix opening /cofl blocked
* remove logging
* Rewrote cofl gui to render before the chest gui opens
* - fix bed flips
- invalidate best flips after open
* - fix scrolling
* - fix description not updating
- case insensitive commands
- fix up , at the end of the message
* - add gui if item was already bought
- fix bug that invalidated too many flips
- added missing "break"
* fix merge error
* add .vscode to gitignore
formatting
* fix formatting issue
* - remove comment
- remove emty line
Diffstat (limited to 'src/main/java/de/torui/coflsky/WSCommandHandler.java')
-rw-r--r-- | src/main/java/de/torui/coflsky/WSCommandHandler.java | 383 |
1 files changed, 200 insertions, 183 deletions
diff --git a/src/main/java/de/torui/coflsky/WSCommandHandler.java b/src/main/java/de/torui/coflsky/WSCommandHandler.java index 0d37851..83e9222 100644 --- a/src/main/java/de/torui/coflsky/WSCommandHandler.java +++ b/src/main/java/de/torui/coflsky/WSCommandHandler.java @@ -12,6 +12,7 @@ import de.torui.coflsky.configuration.ConfigurationManager; import de.torui.coflsky.handlers.EventRegistry; import de.torui.coflsky.proxy.ProxyManager; import de.torui.coflsky.utils.FileUtils; +import de.torui.coflsky.commands.models.TimerData; import net.minecraft.client.Minecraft; import net.minecraft.client.audio.PositionedSoundRecord; import net.minecraft.client.audio.SoundHandler; @@ -30,190 +31,206 @@ import net.minecraftforge.fml.common.ModContainer; import java.io.File; +import java.util.Arrays; +import java.util.stream.Stream; + public class WSCommandHandler { - public static transient String lastOnClickEvent; - public static FlipHandler flipHandler = new FlipHandler(); - private static final ModListData modListData = new ModListData(); - private static final Gson gson = new Gson(); - private static final ProxyManager proxyManager = new ProxyManager(); - - public static boolean HandleCommand(JsonStringCommand cmd, Entity sender) { - // Entity sender = Minecraft.getMinecraft().thePlayer; - System.out.println("Handling Command=" + cmd.toString()); - - switch (cmd.getType()) { - case WriteToChat: - WriteToChat(cmd.GetAs(new TypeToken<ChatMessageData>() {})); - break; - case Execute: - Execute(cmd.GetAs(new TypeToken<String>() {}), sender); - break; - case PlaySound: - PlaySound(cmd.GetAs(new TypeToken<SoundData>() {}), sender); - break; - case ChatMessage: - ChatMessage(cmd.GetAs(new TypeToken<ChatMessageData[]>() {})); - break; - case Flip: - Flip(cmd.GetAs(new TypeToken<FlipData>() {})); - break; - case PrivacySettings: - new ConfigurationManager().UpdateConfiguration(cmd.getData()); - case Countdown: - StartTimer(cmd.GetAs(new TypeToken<TimerData>() {})); - break; - case GetMods: - getMods(); - break; - case ProxyRequest: - handleProxyRequest(cmd.GetAs(new TypeToken<ProxyRequest[]>() {}).getData()); - break; - default: - break; - } - - return true; - } - - private static void handleProxyRequest(ProxyRequest[] request){ - for(ProxyRequest req : request){ - proxyManager.handleRequestAsync(req); - } - } - - - public static void cacheMods(){ - File modFolder = new File(Minecraft.getMinecraft().mcDataDir, "mods"); - for(File mods : modFolder.listFiles()){ - modListData.addFilename(mods.getName()); - try { - modListData.addFileHashes(FileUtils.getMD5Checksum(mods)); - } catch (Exception exception){ - // Highly less likely to happen unless something goes wrong - exception.printStackTrace(); - } - } - - for(ModContainer mod : Loader.instance().getModList()){ - modListData.addModname(mod.getName()); - modListData.addModname(mod.getModId()); - } - } - - private static void getMods(){ - // the Cofl server has asked for an mod list now let's respond with all the info - CoflSky.Wrapper.SendMessage(new RawCommand("foundMods",gson.toJson(modListData))); - } - - - private static void Flip(Command<FlipData> cmd) { - //handle chat message - ChatMessageData[] messages = cmd.getData().Messages; - Command<ChatMessageData[]> showCmd = new Command<ChatMessageData[]>(CommandType.ChatMessage, messages); - ChatMessage(showCmd); - flipHandler.fds.Insert(new de.torui.coflsky.FlipHandler.Flip(cmd.getData().Id, cmd.getData().Worth)); - - // trigger the keyevent to execute the event handler - CoflSky.Events.onKeyEvent(null); - } - - private static void PlaySound(Command<SoundData> cmd, Entity sender) { - - SoundData sc = cmd.getData(); - - SoundHandler handler = Minecraft.getMinecraft().getSoundHandler(); - - // random.explode - PositionedSoundRecord psr = PositionedSoundRecord - .create(new ResourceLocation(sc.Name), sc.Pitch); - - handler.playSound(psr); - } - - private static void Execute(Command<String> cmd, Entity sender) { - System.out.println("Execute: " + cmd.getData() + " sender:" + sender); - //String dummy = WSClient.gson.fromJson(cmd.getData(), String.class); - Execute(cmd.getData(),sender); - } - - /** - * Starts a countdown - */ - private static void StartTimer(Command<TimerData> cmd) { - de.torui.coflsky.CountdownTimer.startCountdown(cmd.getData()); - } - - public static void Execute(String cmd, Entity sender) - { - if(cmd.startsWith("/viewauction")){ - String[] args = cmd.split(" "); - - String uuid = args[args.length-1]; - EventRegistry.LastViewAuctionUUID = uuid; - EventRegistry.LastViewAuctionInvocation = System.currentTimeMillis(); - } - - if(cmd.startsWith("/cofl") || cmd.startsWith("http")) { - ClientCommandHandler.instance.executeCommand(sender, cmd); - } else { - Minecraft.getMinecraft().thePlayer.sendChatMessage(cmd); - } - } - - - private static IChatComponent CommandToChatComponent(ChatMessageData wcmd) { - if(wcmd.OnClick != null) - lastOnClickEvent = "/cofl callback " + wcmd.OnClick; - if (wcmd.Text != null) { - IChatComponent comp = new ChatComponentText(wcmd.Text); - - ChatStyle style; - if (wcmd.OnClick != null) { - if (wcmd.OnClick.startsWith("http")) { - style = new ChatStyle().setChatClickEvent(new ClickEvent(Action.OPEN_URL, wcmd.OnClick)); - } else { - style = new ChatStyle() - .setChatClickEvent(new ClickEvent(Action.RUN_COMMAND, "/cofl callback " + wcmd.OnClick)); - } - comp.setChatStyle(style); - } - - if (wcmd.Hover != null && !wcmd.Hover.isEmpty()) { - if (comp.getChatStyle() == null) - comp.setChatStyle(new ChatStyle()); - comp.getChatStyle().setChatHoverEvent( - new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ChatComponentText(wcmd.Hover))); - } - return comp; - } - return null; - } - - private static void ChatMessage(Command<ChatMessageData[]> cmd) { - ChatMessageData[] list = cmd.getData() ;//WSClient.gson.fromJson(cmd.getData(), WriteToChatCommand[].class); - - IChatComponent master = new ChatComponentText(""); - - for (ChatMessageData wcmd : list) { - IChatComponent comp = CommandToChatComponent(wcmd); - if (comp != null) - master.appendSibling(comp); - } - Minecraft.getMinecraft().thePlayer.addChatMessage(master); - } - - - - private static void WriteToChat(Command<ChatMessageData> cmd) { - ChatMessageData wcmd = cmd.getData(); - - IChatComponent comp = CommandToChatComponent(wcmd); - if (comp != null) - { - Minecraft.getMinecraft().thePlayer.addChatMessage(comp); - } - - } + public static transient String lastOnClickEvent; + public static FlipHandler flipHandler = new FlipHandler(); + private static final ModListData modListData = new ModListData(); + private static final Gson gson = new Gson(); + private static final ProxyManager proxyManager = new ProxyManager(); + + public static boolean HandleCommand(JsonStringCommand cmd, Entity sender) { + System.out.println("Handling Command=" + cmd.toString()); + + switch (cmd.getType()) { + case WriteToChat: + WriteToChat(cmd.GetAs(new TypeToken<ChatMessageData>() { + })); + break; + case Execute: + Execute(cmd.GetAs(new TypeToken<String>() { + }), sender); + break; + case PlaySound: + SoundData sc = cmd.GetAs(new TypeToken<SoundData>() { + }).getData(); + PlaySound(sc.Name, sc.Pitch); + break; + case ChatMessage: + ChatMessage(cmd.GetAs(new TypeToken<ChatMessageData[]>() { + })); + break; + case Flip: + Flip(cmd.GetAs(new TypeToken<FlipData>() { + })); + break; + case PrivacySettings: + new ConfigurationManager().UpdateConfiguration(cmd.getData()); + case Countdown: + StartTimer(cmd.GetAs(new TypeToken<TimerData>() { + })); + break; + case GetMods: + getMods(); + break; + case ProxyRequest: + handleProxyRequest(cmd.GetAs(new TypeToken<ProxyRequest[]>() { + }).getData()); + break; + default: + break; + } + + return true; + } + + private static void Flip(Command<FlipData> cmd) { + //handle chat message + ChatMessageData[] messages = cmd.getData().Messages; + SoundData sound = cmd.getData().Sound; + if (sound != null && sound.Name != null) { + PlaySound(sound.Name, sound.Pitch); + } + Command<ChatMessageData[]> showCmd = new Command<ChatMessageData[]>(CommandType.ChatMessage, messages); + ChatMessage(showCmd); + flipHandler.fds.Insert(cmd.getData()); + // trigger the keyevent to execute the event handler + CoflSky.Events.onKeyEvent(null); + } + + private static void handleProxyRequest(ProxyRequest[] request) { + for (ProxyRequest req : request) { + proxyManager.handleRequestAsync(req); + } + } + + + public static void cacheMods() { + File modFolder = new File(Minecraft.getMinecraft().mcDataDir, "mods"); + for (File mods : modFolder.listFiles()) { + modListData.addFilename(mods.getName()); + try { + modListData.addFileHashes(FileUtils.getMD5Checksum(mods)); + } catch (Exception exception) { + // Highly less likely to happen unless something goes wrong + exception.printStackTrace(); + } + } + + for (ModContainer mod : Loader.instance().getModList()) { + modListData.addModname(mod.getName()); + modListData.addModname(mod.getModId()); + } + } + + private static void getMods() { + // the Cofl server has asked for an mod list now let's respond with all the info + CoflSky.Wrapper.SendMessage(new RawCommand("foundMods", gson.toJson(modListData))); + } + + private static void PlaySound(String soundName, float pitch) { + SoundHandler handler = Minecraft.getMinecraft().getSoundHandler(); + + // random.explode + PositionedSoundRecord psr = PositionedSoundRecord + .create(new ResourceLocation(soundName), pitch); + + handler.playSound(psr); + } + + private static void Execute(Command<String> cmd, Entity sender) { + System.out.println("Execute: " + cmd.getData() + " sender:" + sender); + //String dummy = WSClient.gson.fromJson(cmd.getData(), String.class); + Execute(cmd.getData(), sender); + } + + /** + * Starts a countdown + */ + private static void StartTimer(Command<TimerData> cmd) { + de.torui.coflsky.CountdownTimer.startCountdown(cmd.getData()); + } + + public static void Execute(String cmd, Entity sender) { + if (cmd.startsWith("/cofl") || cmd.startsWith("http")) { + ClientCommandHandler.instance.executeCommand(sender, cmd); + } else { + Minecraft.getMinecraft().thePlayer.sendChatMessage(cmd); + } + } + + + private static IChatComponent CommandToChatComponent(ChatMessageData wcmd, String fullMessage) { + if (wcmd.OnClick != null) { + if (wcmd.Text != null && wcmd.OnClick.startsWith("/viewauction")) { + lastOnClickEvent = "/cofl openAuctionGUI " + wcmd.OnClick.split(" ")[1] + " false"; + } else { + lastOnClickEvent = "/cofl callback " + wcmd.OnClick; + } + } + if (wcmd.Text != null) { + IChatComponent comp = new ChatComponentText(wcmd.Text); + + ChatStyle style; + if (wcmd.OnClick != null) { + if (wcmd.OnClick.startsWith("http")) { + style = new ChatStyle().setChatClickEvent(new ClickEvent(Action.OPEN_URL, wcmd.OnClick)); + } else { + style = new ChatStyle() + .setChatClickEvent(new ClickEvent(Action.RUN_COMMAND, lastOnClickEvent)); + } + comp.setChatStyle(style); + } + + if (wcmd.Hover != null && !wcmd.Hover.isEmpty()) { + if (comp.getChatStyle() == null) + comp.setChatStyle(new ChatStyle()); + comp.getChatStyle().setChatHoverEvent( + new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ChatComponentText(wcmd.Hover))); + } + return comp; + } + return null; + } + + public static void sendChatMessage(IChatComponent message) { + Minecraft.getMinecraft().thePlayer.addChatMessage(message); + } + + public static IChatComponent ChatMessage(Command<ChatMessageData[]> cmd) { + ChatMessageData[] list = cmd.getData(); + + IChatComponent master = new ChatComponentText(""); + String fullMessage = ChatMessageDataToString(list); + + for (ChatMessageData wcmd : list) { + IChatComponent comp = CommandToChatComponent(wcmd, fullMessage); + if (comp != null) + master.appendSibling(comp); + } + Minecraft.getMinecraft().thePlayer.addChatMessage(master); + return master; + } + + + private static void WriteToChat(Command<ChatMessageData> cmd) { + ChatMessageData wcmd = cmd.getData(); + + IChatComponent comp = CommandToChatComponent(wcmd, wcmd.Text); + if (comp != null) { + Minecraft.getMinecraft().thePlayer.addChatMessage(comp); + } + + } + + public static String ChatMessageDataToString(ChatMessageData[] messages) { + Stream<String> stream = Arrays.stream(messages).map(message -> message.Text); + String s = String.join(",", stream.toArray(String[]::new)); + stream.close(); + return s; + } } |