From 41a9f0cca4f599ffe57152f700326bb06b920554 Mon Sep 17 00:00:00 2001 From: Pedro Mendes Date: Wed, 21 Feb 2024 23:53:21 -0300 Subject: [PATCH 1/2] feat: Add a time event --- backend/src/models.py | 1 + .../mitsuaky/stanleyparable/EventSubscriber.java | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/backend/src/models.py b/backend/src/models.py index a89b19a..b2514cc 100644 --- a/backend/src/models.py +++ b/backend/src/models.py @@ -18,6 +18,7 @@ class Event(StrEnum): ITEM_SMELTED = "item_smelted" MOB_KILLED = "mob_killed" DIMENSION_CHANGED = "dimension_changed" + TIME_CHANGED = "time_changed" PLAYER_CHAT = "player_chat" PLAYER_ATE = "player_ate" RIDING = "riding" diff --git a/forge/src/main/java/com/mitsuaky/stanleyparable/EventSubscriber.java b/forge/src/main/java/com/mitsuaky/stanleyparable/EventSubscriber.java index 1fa6198..79463dd 100644 --- a/forge/src/main/java/com/mitsuaky/stanleyparable/EventSubscriber.java +++ b/forge/src/main/java/com/mitsuaky/stanleyparable/EventSubscriber.java @@ -4,7 +4,6 @@ import net.minecraft.advancements.AdvancementHolder; import net.minecraft.advancements.DisplayInfo; import net.minecraft.client.Minecraft; -import net.minecraft.client.player.LocalPlayer; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; import net.minecraft.network.chat.Component; @@ -56,6 +55,7 @@ public class EventSubscriber { private static Set lastInventory = null; private static boolean isRiding = false; private static boolean isFishing = false; + private static String timeState = ""; public enum Event { ITEM_CRAFTED("item_crafted"), @@ -68,6 +68,7 @@ public enum Event { ITEM_SMELTED("item_smelted"), MOB_KILLED("mob_killed"), DIMENSION_CHANGED("dimension_changed"), + TIME_CHANGED("time_changed"), PLAYER_CHAT("player_chat"), PLAYER_ATE("player_ate"), RIDING("riding"), @@ -173,6 +174,19 @@ public static void onPlayerTick(TickEvent.PlayerTickEvent event) { } else { isRiding = false; } + + // Custom time event + long time = event.player.level().dayTime(); + if (0 == time && !timeState.equals("day")) { + wsClient.sendEvent(Event.TIME_CHANGED.getValue(), "O sol nasceu no minecraft"); + timeState = "day"; + } else if (12000 == time && !timeState.equals("sunset")) { + wsClient.sendEvent(Event.TIME_CHANGED.getValue(), "O sol está se pondo no minecraft"); + timeState = "sunset"; + } else if (13000 == time && !timeState.equals("night")) { + wsClient.sendEvent(Event.TIME_CHANGED.getValue(), "O sol se pôs no minecraft e está escuro"); + timeState = "night"; + } } @SubscribeEvent From 8cc879e94d16a243c9746f3276fa02ef302b9728 Mon Sep 17 00:00:00 2001 From: Pedro Mendes Date: Thu, 22 Feb 2024 00:11:25 -0300 Subject: [PATCH 2/2] refactor: Use an enum to check state --- .../stanleyparable/EventSubscriber.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/forge/src/main/java/com/mitsuaky/stanleyparable/EventSubscriber.java b/forge/src/main/java/com/mitsuaky/stanleyparable/EventSubscriber.java index 79463dd..a831253 100644 --- a/forge/src/main/java/com/mitsuaky/stanleyparable/EventSubscriber.java +++ b/forge/src/main/java/com/mitsuaky/stanleyparable/EventSubscriber.java @@ -55,7 +55,7 @@ public class EventSubscriber { private static Set lastInventory = null; private static boolean isRiding = false; private static boolean isFishing = false; - private static String timeState = ""; + private static TimeState timeState = null; public enum Event { ITEM_CRAFTED("item_crafted"), @@ -175,17 +175,17 @@ public static void onPlayerTick(TickEvent.PlayerTickEvent event) { isRiding = false; } - // Custom time event + long time = event.player.level().dayTime(); - if (0 == time && !timeState.equals("day")) { + if (0 == time && timeState != TimeState.DAY) { wsClient.sendEvent(Event.TIME_CHANGED.getValue(), "O sol nasceu no minecraft"); - timeState = "day"; - } else if (12000 == time && !timeState.equals("sunset")) { + timeState = TimeState.DAY; + } else if (12000 == time && timeState != TimeState.SUNSET) { wsClient.sendEvent(Event.TIME_CHANGED.getValue(), "O sol está se pondo no minecraft"); - timeState = "sunset"; - } else if (13000 == time && !timeState.equals("night")) { + timeState = TimeState.SUNSET; + } else if (13000 == time && timeState != TimeState.NIGHT) { wsClient.sendEvent(Event.TIME_CHANGED.getValue(), "O sol se pôs no minecraft e está escuro"); - timeState = "night"; + timeState = TimeState.NIGHT; } } @@ -461,4 +461,7 @@ public static void onPlayerJoin(PlayerEvent.PlayerLoggedInEvent event) { String playerName = getPlayerName(player); wsClient.sendEvent(Event.JOIN_WORLD.getValue(), String.format("Jogador \"%s\" entrou no mundo \"%s\"", playerName, worldName)); } + private enum TimeState { + DAY, SUNSET, NIGHT + } }