From e3aedc3ffea344fbdda63aeb8a3fae8af9b1e681 Mon Sep 17 00:00:00 2001 From: Äkwav <16632490+Ekwav@users.noreply.github.com> Date: Tue, 12 Apr 2022 00:02:21 +0200 Subject: Feature/countdown (#54) * v1 * fixes #20 * code review comments * update version * fix aliases --- src/main/java/de/torui/coflsky/CoflSky.java | 2 +- src/main/java/de/torui/coflsky/CoflSkyCommand.java | 10 ++ src/main/java/de/torui/coflsky/EventRegistry.java | 6 ++ .../java/de/torui/coflsky/FlipperChatCommand.java | 9 ++ .../java/de/torui/coflsky/WSCommandHandler.java | 13 ++- .../de/torui/coflsky/commands/CommandType.java | 2 + .../de/torui/coflsky/commands/models/FlipData.java | 2 - .../torui/coflsky/commands/models/TimerData.java | 22 +++++ .../minecraft_integration/CountdownTimer.java | 102 +++++++++++++++++++++ .../java/de/torui/coflsky/network/WSClient.java | 8 ++ 10 files changed, 172 insertions(+), 4 deletions(-) create mode 100644 src/main/java/de/torui/coflsky/commands/models/TimerData.java create mode 100644 src/main/java/de/torui/coflsky/minecraft_integration/CountdownTimer.java (limited to 'src') diff --git a/src/main/java/de/torui/coflsky/CoflSky.java b/src/main/java/de/torui/coflsky/CoflSky.java index 18b4008..4a48958 100644 --- a/src/main/java/de/torui/coflsky/CoflSky.java +++ b/src/main/java/de/torui/coflsky/CoflSky.java @@ -20,7 +20,7 @@ import net.minecraftforge.fml.relauncher.Side; public class CoflSky { public static final String MODID = "CoflSky"; - public static final String VERSION = "1.3-Alpha"; + public static final String VERSION = "1.3.3-Alpha"; public static WSClientWrapper Wrapper; public static KeyBinding[] keyBindings; diff --git a/src/main/java/de/torui/coflsky/CoflSkyCommand.java b/src/main/java/de/torui/coflsky/CoflSkyCommand.java index d5ee857..4ff5d5b 100644 --- a/src/main/java/de/torui/coflsky/CoflSkyCommand.java +++ b/src/main/java/de/torui/coflsky/CoflSkyCommand.java @@ -26,6 +26,7 @@ import net.minecraft.util.ChatComponentText; import net.minecraft.util.ChatStyle; import net.minecraft.util.EnumChatFormatting; import net.minecraft.util.IChatComponent; +import java.util.ArrayList; public class CoflSkyCommand extends CommandBase { @@ -40,6 +41,15 @@ public class CoflSkyCommand extends CommandBase { public String getCommandName() { return "cofl"; } + @Override + public List getCommandAliases() + { + ArrayList al = new ArrayList(); + al.add("Cofl"); + al.add("coflnet"); + al.add("cl"); + return al; + } @Override public String getCommandUsage(ICommandSender sender) { diff --git a/src/main/java/de/torui/coflsky/EventRegistry.java b/src/main/java/de/torui/coflsky/EventRegistry.java index f8047e7..238d7e7 100644 --- a/src/main/java/de/torui/coflsky/EventRegistry.java +++ b/src/main/java/de/torui/coflsky/EventRegistry.java @@ -27,6 +27,7 @@ import net.minecraftforge.fml.common.gameevent.InputEvent.MouseInputEvent; import net.minecraftforge.fml.common.network.FMLNetworkEvent.ClientDisconnectionFromServerEvent; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; +import net.minecraftforge.fml.common.gameevent.TickEvent; public class EventRegistry { @@ -201,4 +202,9 @@ public class EventRegistry { } } + + @SubscribeEvent + public void OnRenderTick(TickEvent.RenderTickEvent event) { + CountdownTimer.onRenderTick(event); + } } diff --git a/src/main/java/de/torui/coflsky/FlipperChatCommand.java b/src/main/java/de/torui/coflsky/FlipperChatCommand.java index 61e7c52..618348e 100644 --- a/src/main/java/de/torui/coflsky/FlipperChatCommand.java +++ b/src/main/java/de/torui/coflsky/FlipperChatCommand.java @@ -26,6 +26,7 @@ import net.minecraft.util.ChatComponentText; import net.minecraft.util.ChatStyle; import net.minecraft.util.EnumChatFormatting; import net.minecraft.util.IChatComponent; +import java.util.ArrayList; public class FlipperChatCommand extends CoflSkyCommand { @@ -35,6 +36,14 @@ public class FlipperChatCommand extends CoflSkyCommand { return "fc"; } + @Override + public List getCommandAliases() + { + ArrayList al = new ArrayList(); + al.add("coflchat"); + return al; + } + @Override public String getCommandUsage(ICommandSender sender) { return "Shorthand for /cofl chat"; diff --git a/src/main/java/de/torui/coflsky/WSCommandHandler.java b/src/main/java/de/torui/coflsky/WSCommandHandler.java index 0fc834f..616f860 100644 --- a/src/main/java/de/torui/coflsky/WSCommandHandler.java +++ b/src/main/java/de/torui/coflsky/WSCommandHandler.java @@ -8,6 +8,7 @@ import de.torui.coflsky.commands.JsonStringCommand; import de.torui.coflsky.commands.models.ChatMessageData; import de.torui.coflsky.commands.models.FlipData; import de.torui.coflsky.commands.models.SoundData; +import de.torui.coflsky.commands.models.TimerData; import de.torui.coflsky.network.WSClient; import net.minecraft.client.Minecraft; import net.minecraft.client.audio.PositionedSoundRecord; @@ -46,6 +47,10 @@ public class WSCommandHandler { break; case Flip: Flip(cmd.GetAs(new TypeToken() {})); + break; + case Countdown: + StartTimer(cmd.GetAs(new TypeToken() {})); + break; default: break; } @@ -83,9 +88,15 @@ public class WSCommandHandler { Execute(cmd.getData(),sender); } + /** + * Starts a countdown + */ + private static void StartTimer(Command cmd) { + CountdownTimer.startCountdown(cmd.getData()); + } + public static void Execute(String cmd, Entity sender) { - if(cmd.startsWith("/viewauction")){ String[] args = cmd.split(" "); diff --git a/src/main/java/de/torui/coflsky/commands/CommandType.java b/src/main/java/de/torui/coflsky/commands/CommandType.java index ead7d3e..3d4ec05 100644 --- a/src/main/java/de/torui/coflsky/commands/CommandType.java +++ b/src/main/java/de/torui/coflsky/commands/CommandType.java @@ -35,6 +35,8 @@ public enum CommandType { Reset, @SerializedName("flip") Flip, + @SerializedName("countdown") + Countdown, ; public static Map data; static { diff --git a/src/main/java/de/torui/coflsky/commands/models/FlipData.java b/src/main/java/de/torui/coflsky/commands/models/FlipData.java index bdd955b..88836d2 100644 --- a/src/main/java/de/torui/coflsky/commands/models/FlipData.java +++ b/src/main/java/de/torui/coflsky/commands/models/FlipData.java @@ -21,6 +21,4 @@ public class FlipData { Id = id; Worth = worth; } - - } diff --git a/src/main/java/de/torui/coflsky/commands/models/TimerData.java b/src/main/java/de/torui/coflsky/commands/models/TimerData.java new file mode 100644 index 0000000..50b3d68 --- /dev/null +++ b/src/main/java/de/torui/coflsky/commands/models/TimerData.java @@ -0,0 +1,22 @@ +package de.torui.coflsky.commands.models; + +import com.google.gson.annotations.SerializedName; + +public class TimerData { + + @SerializedName("seconds") + public double seconds; + + @SerializedName("heightPercent") + public int height; + @SerializedName("widthPercent") + public int width; + @SerializedName("scale") + public double scale; + + @SerializedName("prefix") + public String prefix; + + @SerializedName("maxPrecision") + public int maxPrecision; +} diff --git a/src/main/java/de/torui/coflsky/minecraft_integration/CountdownTimer.java b/src/main/java/de/torui/coflsky/minecraft_integration/CountdownTimer.java new file mode 100644 index 0000000..9c63627 --- /dev/null +++ b/src/main/java/de/torui/coflsky/minecraft_integration/CountdownTimer.java @@ -0,0 +1,102 @@ +package de.torui.coflsky; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.gui.GuiChat; +import net.minecraft.client.gui.ScaledResolution; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.client.settings.KeyBinding; +import net.minecraftforge.fml.client.registry.ClientRegistry; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.gameevent.InputEvent; +import net.minecraftforge.fml.common.gameevent.TickEvent; +import de.torui.coflsky.commands.models.TimerData; +import org.lwjgl.input.Keyboard; + +import java.util.Locale; + +public class CountdownTimer { + private static Minecraft mc = Minecraft.getMinecraft(); + private static FontRenderer fr = mc.fontRendererObj; + + private static long currentEndTime; + private static int currentWidth; + private static int currentHeight; + private static double currentScale; + private static String currentPrefix; + private static int currentPrecision; + + public CountdownTimer() { + } + + public static void onRenderTick(TickEvent.RenderTickEvent event) { + if (mc.currentScreen == null || mc.currentScreen instanceof GuiChat) // will only draw the Timer while no GUI or the Chat is open + if (currentEndTime - System.currentTimeMillis() > 0) + drawTimer(); + } + + /** + * @param seconds will start a timer starting at seconds + * @param widthPercentage width in correlation to the window size + * @param heightPercentage height in correlation to the window size + * @param fontScale scales the text size by factor (1 = no change) + */ + public static void startCountdown(double seconds, int widthPercentage, int heightPercentage, double fontScale) { + startCountdown(seconds, widthPercentage, heightPercentage, fontScale, "", 4); + } + + /** + * @param seconds will start a timer starting at seconds + * @param widthPercentage width in correlation to the window size + * @param heightPercentage height in correlation to the window size + * @param fontScale scales the text size by factor (1 = no change) + * @param prefix will put that text infront of the seconds (supports color codes using §) + * @param maxPrecision length of the seconds in the timer + */ + public static void startCountdown(double seconds, int widthPercentage, int heightPercentage, double fontScale, String prefix, int maxPrecision) { + System.out.println("###Starting countdown " + seconds); + currentEndTime = (long) (System.currentTimeMillis() + (seconds * 1000)); + currentWidth = widthPercentage; + currentHeight = heightPercentage; + currentScale = fontScale; + currentPrefix = prefix; + currentPrecision = maxPrecision; + } + + public static void startCountdown(TimerData data) { + startCountdown(data.seconds, data.width, data.height, data.scale, data.prefix, data.maxPrecision ); + } + + private static void drawTimer() { + long curMillis = currentEndTime - System.currentTimeMillis(); + String render = getStringFromDouble(curMillis / 1000D); + + ScaledResolution scaled = new ScaledResolution(mc); + + GlStateManager.pushMatrix(); + GlStateManager.scale(currentScale, currentScale, currentScale); + int scaledX = (int) (scaled.getScaledWidth() * (currentWidth / 100D) / currentScale); + int scaledY = (int) (scaled.getScaledHeight() * (currentHeight / 100D) / currentScale); + drawHVCenteredString(currentPrefix + render, scaledX, scaledY); + GlStateManager.popMatrix(); + } + + private static String getStringFromDouble(double seconds) { + String render; + + if (seconds > 100) { + render = String.valueOf((int) seconds); + } else { + render = String.format(Locale.US, "%.3f", seconds).substring(0, currentPrecision); + if(render.charAt(render.length() - 1) == '.') + render = render.substring(0, currentPrecision -1); + } + + return render + "s"; + } + + public static void drawHVCenteredString(String text, int x, int y) { + text = text.replaceAll("§", "" + ((char) 167)); + fr.drawString(text, x - (fr.getStringWidth(text) >> 1), y - (fr.FONT_HEIGHT >> 1), 0xFFFFFFFF, true); + } +} \ No newline at end of file diff --git a/src/main/java/de/torui/coflsky/network/WSClient.java b/src/main/java/de/torui/coflsky/network/WSClient.java index ad9c436..46fdbb4 100644 --- a/src/main/java/de/torui/coflsky/network/WSClient.java +++ b/src/main/java/de/torui/coflsky/network/WSClient.java @@ -131,6 +131,14 @@ public class WSClient extends WebSocketAdapter { public void Send(Object obj) { String json = gson.toJson(obj); System.out.println("###Sending message of json value " + json); + if(this.socket == null) + try + { + start(); + } catch(Exception e) + { + System.out.println("Ran into an error on implicit start for send: "+ e); + } this.socket.sendText(json); } -- cgit