From 6f2440ab98c40da43a3a0e7571c541a30af91305 Mon Sep 17 00:00:00 2001 From: vipinpanwar Date: Sun, 29 Jun 2025 00:09:22 +0530 Subject: [PATCH 1/2] refactor/snitchmod-nullables-to-optional --- .../civ/snitchmod/common/Renderer.java | 10 +-- .../civ/snitchmod/common/SnitchMod.java | 78 +++++++++---------- .../civ/snitchmod/common/SnitchSqliteDb.java | 4 +- .../civ/snitchmod/common/model/Snitch.java | 6 +- 4 files changed, 46 insertions(+), 52 deletions(-) diff --git a/common/src/main/java/gjum/minecraft/civ/snitchmod/common/Renderer.java b/common/src/main/java/gjum/minecraft/civ/snitchmod/common/Renderer.java index c51235c..95fcd3c 100644 --- a/common/src/main/java/gjum/minecraft/civ/snitchmod/common/Renderer.java +++ b/common/src/main/java/gjum/minecraft/civ/snitchmod/common/Renderer.java @@ -70,9 +70,7 @@ public static void renderOverlays(Matrix4f matrixArg) { .forEach(Renderer::renderPlacementHelper); } - if (getMod().snitchFieldToPreview != null) { - renderSnitchFieldPreview(getMod().snitchFieldToPreview); - } + getMod().snitchFieldToPreview.ifPresent(Renderer::renderSnitchFieldPreview); RenderSystem.enableDepthTest(); RenderSystem.depthMask(true); @@ -145,7 +143,7 @@ enum SnitchLiveliness { } long now = System.currentTimeMillis(); - long snitchTimer = snitch.getType() != null ? snitch.getType().timer : Snitch.Type.NOTEBLOCK.timer; + long snitchTimer = snitch.getType().isPresent() ? snitch.getType().get().timer : Snitch.Type.NOTEBLOCK.timer; SnitchLiveliness snitchLiveliness = SnitchLiveliness.DORMANT_EVENTUALLY; if (snitch.wasBroken()) { snitchLiveliness = SnitchLiveliness.BROKEN; @@ -216,8 +214,8 @@ enum SnitchLiveliness { final AABB blockBox = new AABB(snitch.pos).inflate(.01); Color boxOutlineColor = snitchLiveliness.color; if ( - getMod().snitchFieldToPreview != null - && getMod().snitchFieldToPreview.source().equals(snitch) + getMod().snitchFieldToPreview.isPresent() + && getMod().snitchFieldToPreview.get().source().equals(snitch) ) { boxOutlineColor = PINK; } diff --git a/common/src/main/java/gjum/minecraft/civ/snitchmod/common/SnitchMod.java b/common/src/main/java/gjum/minecraft/civ/snitchmod/common/SnitchMod.java index ede9c5b..757464d 100644 --- a/common/src/main/java/gjum/minecraft/civ/snitchmod/common/SnitchMod.java +++ b/common/src/main/java/gjum/minecraft/civ/snitchmod/common/SnitchMod.java @@ -74,13 +74,9 @@ public abstract class SnitchMod { public boolean rangeOverlayVisible = false; public boolean placementHelperVisible = false; - @Nullable - public SnitchFieldPreview snitchFieldToPreview = null; - @Nullable - public Snitch lastBrokenSnitch = null; - - @Nullable - private SnitchesStore store; + public Optional snitchFieldToPreview = Optional.empty(); + public Optional lastBrokenSnitch = Optional.empty(); + private Optional store = Optional.empty(); public static SnitchMod getMod() { return INSTANCE; @@ -114,14 +110,14 @@ public UUID getClientUuid() { return mc.player.getUUID(); } - public @Nullable SnitchesStore getStore() { + public Optional getStore() { String server = getCurrentServer(); - if (store != null && !store.server.equals(server)) { - store.close(); - store = null; + if (store.isPresent() && !store.get().server.equals(server)) { + store.get().close(); + store = Optional.empty(); } - if (store == null && server != null) { - store = new SnitchesStore(server); + if (store.isPresent() && server != null) { + store = Optional.of(new SnitchesStore(server)); } return store; } @@ -131,15 +127,15 @@ public void handleConnectedToServer() { } public void handleDisconnectedFromServer() { - if (store != null) store.close(); - store = null; + store.ifPresent(SnitchesStore::close); + store = Optional.empty(); } public void handleTick() { while (openGuiKey.consumeClick()) { // TODO open gui, and rename keybind - store.close(); - store = null; + store.ifPresent(SnitchesStore::close); + store = Optional.empty(); getStore(); logToChat(Component.literal("Reloaded the database")); } @@ -169,10 +165,10 @@ public void handleTick() { snitch.getPos().getZ() ); if (snitch.isGone()) { - store.updateSnitchNoLongerGone(snitch.pos); + store.get().updateSnitchNoLongerGone(snitch.pos); logToChat(Component.literal(String.format("Marked snitch %s as alive", formattedSnitch))); } else { - store.updateSnitchGone(snitch.pos); + store.get().updateSnitchGone(snitch.pos); logToChat(Component.literal(String.format("Marked snitch %s as gone", formattedSnitch))); } } @@ -188,7 +184,7 @@ public void handleTick() { placementHelperVisible = !placementHelperVisible; if (placementHelperVisible) { - snitchFieldToPreview = null; + snitchFieldToPreview = Optional.empty(); } logToChat( @@ -201,7 +197,7 @@ public void handleTick() { .filter(Snitch::isAlive) .findFirst(); if (optNearestSnitch.isEmpty()) { - snitchFieldToPreview = null; + snitchFieldToPreview = Optional.empty(); logToChat(Component.literal("No nearby alive snitches to base a field preview on")); break; } @@ -214,15 +210,15 @@ public void handleTick() { SnitchFieldPreview newSnitchFieldToPreview = new SnitchFieldPreview( nearestSnitch, Direction.ofPlayer(mc.player)); if ( - snitchFieldToPreview != null - && newSnitchFieldToPreview.equals(snitchFieldToPreview) + snitchFieldToPreview.isPresent() + && newSnitchFieldToPreview.equals(snitchFieldToPreview.get()) ) { logToChat(Component.literal("Turning off the snitch field preview")); snitchFieldToPreview = null; break; } - snitchFieldToPreview = newSnitchFieldToPreview; + snitchFieldToPreview = Optional.of(newSnitchFieldToPreview); logToChat(Component.literal("Showing a snitch field preview in the direction you're looking")); } // TODO if block pos changed -> if pos inside snitch range not in before -> send jainfo -> mark refreshed @@ -233,24 +229,24 @@ public void handleTick() { */ public boolean handleChat(Component message) { getStore(); - if (store == null) return false; + if (store.isEmpty()) return false; - SnitchAlert snitchAlert = SnitchAlert.fromChat(message, store.server, getCurrentWorld()); + SnitchAlert snitchAlert = SnitchAlert.fromChat(message, store.get().server, getCurrentWorld()); if (snitchAlert != null) { - store.updateSnitchFromAlert(snitchAlert); + store.get().updateSnitchFromAlert(snitchAlert); return false; } - SnitchRename snitchRename = SnitchRename.fromChat(message, store.server, getCurrentWorld(), getClientUuid()); + SnitchRename snitchRename = SnitchRename.fromChat(message, store.get().server, getCurrentWorld(), getClientUuid()); if (snitchRename != null) { - store.updateSnitchFromRename(snitchRename); + store.get().updateSnitchFromRename(snitchRename); return false; } - Snitch snitchCreated = SnitchCreatedChatParser.fromChat(message, store.server, getCurrentWorld(), getClientUuid()); + Snitch snitchCreated = SnitchCreatedChatParser.fromChat(message, store.get().server, getCurrentWorld(), getClientUuid()); if (snitchCreated != null) { - Snitch alreadyExistingSnitch = store.getSnitch(snitchCreated.pos); - store.updateSnitchFromCreation(snitchCreated); + Snitch alreadyExistingSnitch = store.get().getSnitch(snitchCreated.pos); + store.get().updateSnitchFromCreation(snitchCreated); if ( alreadyExistingSnitch != null && alreadyExistingSnitch.getName() != null @@ -269,15 +265,15 @@ public boolean handleChat(Component message) { } if ( - snitchFieldToPreview != null - && snitchFieldToPreview.field().pos.equals(snitchCreated.pos) + snitchFieldToPreview.isPresent() + && snitchFieldToPreview.get().field().pos.equals(snitchCreated.pos) ) { if (placementHelperVisible) { placementHelperVisible = false; } - snitchFieldToPreview = new SnitchFieldPreview( - snitchCreated, snitchFieldToPreview.direction()); + snitchFieldToPreview = Optional.of(new SnitchFieldPreview( + snitchCreated, snitchFieldToPreview.get().direction())); logToChat(Component.literal("Showing an inferred snitch field preview")); } @@ -292,12 +288,12 @@ public boolean handleChat(Component message) { public void handleWindowItems(List stacks) { getStore(); - if (store == null) return; + if (store.isEmpty()) return; List jalistEntries = new ArrayList(stacks.size()); for (int i = 0; i < stacks.size(); i++) { ItemStack stack = stacks.get(i); try { - JalistEntry jalistEntry = JalistEntry.fromStack(stack, store.server); + JalistEntry jalistEntry = JalistEntry.fromStack(stack, store.get().server); if (jalistEntry != null) { jalistEntries.add(jalistEntry); } @@ -307,7 +303,7 @@ public void handleWindowItems(List stacks) { logToChat(Component.literal("Failed reading snitch " + i + " on JAList page")); } } - store.updateSnitchesFromJalist(jalistEntries); + store.get().updateSnitchesFromJalist(jalistEntries); if (jalistEntries.size() > 0) { logToChat(Component.literal("Found " + jalistEntries.size() + " snitches on JAList page")); } @@ -319,8 +315,8 @@ public void handleRenderBlockOverlay(Matrix4f matrix) { public Stream streamNearbySnitches(Vec3 playerPos, int distance) { getStore(); - if (store == null) return Stream.empty(); - return store.getAllSnitches().stream() + if (store.isEmpty()) return Stream.empty(); + return store.get().getAllSnitches().stream() .filter(s -> s.getPos().getCenter().distanceTo(playerPos) < distance) .sorted(Comparator.comparing(s -> s.getPos().getCenter().distanceTo(playerPos))); } diff --git a/common/src/main/java/gjum/minecraft/civ/snitchmod/common/SnitchSqliteDb.java b/common/src/main/java/gjum/minecraft/civ/snitchmod/common/SnitchSqliteDb.java index b62ce27..0512c50 100644 --- a/common/src/main/java/gjum/minecraft/civ/snitchmod/common/SnitchSqliteDb.java +++ b/common/src/main/java/gjum/minecraft/civ/snitchmod/common/SnitchSqliteDb.java @@ -147,8 +147,8 @@ public void upsertSnitches(List snitches) { for (Snitch snitch : snitches) { String type = null; - if (snitch.getType() != null) { - type = snitch.getType().dbRepresentation; + if (snitch.getType().isPresent()) { + type = snitch.getType().get().dbRepresentation; } try { int i = 0; diff --git a/common/src/main/java/gjum/minecraft/civ/snitchmod/common/model/Snitch.java b/common/src/main/java/gjum/minecraft/civ/snitchmod/common/model/Snitch.java index 591f300..9c1bb13 100644 --- a/common/src/main/java/gjum/minecraft/civ/snitchmod/common/model/Snitch.java +++ b/common/src/main/java/gjum/minecraft/civ/snitchmod/common/model/Snitch.java @@ -178,9 +178,9 @@ public String getGroup() { return group; } - @Nullable - public Type getType() { - return type; + @NotNull + public Optional getType() { + return Optional.ofNullable(type); } @Nullable From 82bad2ad49c9295e5638b578d675e199b263ed2f Mon Sep 17 00:00:00 2001 From: vipinpanwar Date: Mon, 7 Jul 2025 20:59:03 +0530 Subject: [PATCH 2/2] refactor/snitchmod-nullables-to-optional_review_comments_fixed --- .../java/gjum/minecraft/civ/snitchmod/common/SnitchMod.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/main/java/gjum/minecraft/civ/snitchmod/common/SnitchMod.java b/common/src/main/java/gjum/minecraft/civ/snitchmod/common/SnitchMod.java index 757464d..2db1191 100644 --- a/common/src/main/java/gjum/minecraft/civ/snitchmod/common/SnitchMod.java +++ b/common/src/main/java/gjum/minecraft/civ/snitchmod/common/SnitchMod.java @@ -116,7 +116,7 @@ public Optional getStore() { store.get().close(); store = Optional.empty(); } - if (store.isPresent() && server != null) { + if (store.isEmpty() && server != null) { store = Optional.of(new SnitchesStore(server)); } return store;