From 65c49190cb664f65fd2e3bd17a1bdc1917d9f4c8 Mon Sep 17 00:00:00 2001 From: sarthakpatel Date: Wed, 5 Apr 2023 14:45:31 -0300 Subject: [PATCH 1/5] Move Method Refactoring Implemented. --- .../LoggingMinecraftInterface.java | 15 ++++++++++ .../MinecraftInterface.java | 5 ++++ .../legacy/BetaMinecraftInterface.java | 20 ++++++++++--- .../legacy/LegacyMinecraftInterface.java | 30 +++++++++++++++---- .../legacy/_1_13MinecraftInterface.java | 21 +++++++++---- .../legacy/_1_15MinecraftInterface.java | 21 +++++++++---- .../local/LocalMinecraftInterface.java | 21 +++++++++---- .../amidst/mojangapi/world/WorldBuilder.java | 15 ++-------- .../BenchmarkingMinecraftInterface.java | 19 ++++++++++-- .../mocking/FakeMinecraftInterface.java | 19 ++++++++++-- .../RequestStoringMinecraftInterface.java | 19 ++++++++++-- 11 files changed, 159 insertions(+), 46 deletions(-) diff --git a/src/main/java/amidst/mojangapi/minecraftinterface/LoggingMinecraftInterface.java b/src/main/java/amidst/mojangapi/minecraftinterface/LoggingMinecraftInterface.java index cd4087e46..b72b96d42 100644 --- a/src/main/java/amidst/mojangapi/minecraftinterface/LoggingMinecraftInterface.java +++ b/src/main/java/amidst/mojangapi/minecraftinterface/LoggingMinecraftInterface.java @@ -5,7 +5,10 @@ import amidst.documentation.ThreadSafe; import amidst.logging.AmidstLogger; import amidst.mojangapi.world.Dimension; +import amidst.mojangapi.world.SeedHistoryLogger; import amidst.mojangapi.world.WorldOptions; +import amidst.mojangapi.world.versionfeatures.DefaultVersionFeatures; +import amidst.mojangapi.world.versionfeatures.VersionFeatures; @ThreadSafe public class LoggingMinecraftInterface implements MinecraftInterface { @@ -55,4 +58,16 @@ public WorldAccessor createWorldAccessor(WorldOptions worldOptions) throws Minec public RecognisedVersion getRecognisedVersion() { return inner.getRecognisedVersion(); } + + @Override + public VersionFeatures initInterfaceAndGetFeatures(WorldOptions worldOptions, MinecraftInterface minecraftInterface, SeedHistoryLogger seedHistoryLogger) + throws MinecraftInterfaceException { + RecognisedVersion recognisedVersion = minecraftInterface.getRecognisedVersion(); + if(minecraftInterface instanceof LoggingMinecraftInterface) { + ((LoggingMinecraftInterface) minecraftInterface).logNextAccessor(); + } + MinecraftInterface.WorldAccessor worldAccessor = new ThreadedWorldAccessor(v -> minecraftInterface.createWorldAccessor(worldOptions)); + seedHistoryLogger.log(recognisedVersion, worldOptions.getWorldSeed()); + return DefaultVersionFeatures.builder(worldOptions, worldAccessor).create(recognisedVersion); + } } diff --git a/src/main/java/amidst/mojangapi/minecraftinterface/MinecraftInterface.java b/src/main/java/amidst/mojangapi/minecraftinterface/MinecraftInterface.java index d52036836..d539ba2e0 100644 --- a/src/main/java/amidst/mojangapi/minecraftinterface/MinecraftInterface.java +++ b/src/main/java/amidst/mojangapi/minecraftinterface/MinecraftInterface.java @@ -5,7 +5,9 @@ import amidst.documentation.ThreadSafe; import amidst.mojangapi.world.Dimension; +import amidst.mojangapi.world.SeedHistoryLogger; import amidst.mojangapi.world.WorldOptions; +import amidst.mojangapi.world.versionfeatures.VersionFeatures; /** * Acts as an additional layer of abstraction for interfacing with Minecraft. @@ -21,6 +23,9 @@ public interface MinecraftInterface { public RecognisedVersion getRecognisedVersion(); + VersionFeatures initInterfaceAndGetFeatures(WorldOptions worldOptions, MinecraftInterface minecraftInterface, SeedHistoryLogger seedHistoryLogger) + throws MinecraftInterfaceException; + /** * Represents a Minecraft world, allowing for querying of biome data. * diff --git a/src/main/java/amidst/mojangapi/minecraftinterface/legacy/BetaMinecraftInterface.java b/src/main/java/amidst/mojangapi/minecraftinterface/legacy/BetaMinecraftInterface.java index c943f5586..5c87100a6 100644 --- a/src/main/java/amidst/mojangapi/minecraftinterface/legacy/BetaMinecraftInterface.java +++ b/src/main/java/amidst/mojangapi/minecraftinterface/legacy/BetaMinecraftInterface.java @@ -2,13 +2,13 @@ import amidst.clazz.symbolic.SymbolicClass; import amidst.clazz.symbolic.SymbolicObject; -import amidst.mojangapi.minecraftinterface.MinecraftInterface; -import amidst.mojangapi.minecraftinterface.MinecraftInterfaceException; -import amidst.mojangapi.minecraftinterface.RecognisedVersion; -import amidst.mojangapi.minecraftinterface.UnsupportedDimensionException; +import amidst.mojangapi.minecraftinterface.*; import amidst.mojangapi.world.Dimension; +import amidst.mojangapi.world.SeedHistoryLogger; import amidst.mojangapi.world.WorldOptions; import amidst.mojangapi.world.versionfeatures.DefaultBiomes; +import amidst.mojangapi.world.versionfeatures.DefaultVersionFeatures; +import amidst.mojangapi.world.versionfeatures.VersionFeatures; import amidst.util.ChunkBasedGen; import java.lang.reflect.InvocationTargetException; @@ -89,6 +89,18 @@ public RecognisedVersion getRecognisedVersion() { return recognisedVersion; } + @Override + public VersionFeatures initInterfaceAndGetFeatures(WorldOptions worldOptions, MinecraftInterface minecraftInterface, SeedHistoryLogger seedHistoryLogger) + throws MinecraftInterfaceException { + RecognisedVersion recognisedVersion = minecraftInterface.getRecognisedVersion(); + if(minecraftInterface instanceof LoggingMinecraftInterface) { + ((LoggingMinecraftInterface) minecraftInterface).logNextAccessor(); + } + MinecraftInterface.WorldAccessor worldAccessor = new ThreadedWorldAccessor(v -> minecraftInterface.createWorldAccessor(worldOptions)); + seedHistoryLogger.log(recognisedVersion, worldOptions.getWorldSeed()); + return DefaultVersionFeatures.builder(worldOptions, worldAccessor).create(recognisedVersion); + } + private SymbolicObject constructDimension() throws IllegalAccessException, InstantiationException { return new SymbolicObject(dimensionBaseClass, dimensionOverworldClass.getClazz().newInstance()); } diff --git a/src/main/java/amidst/mojangapi/minecraftinterface/legacy/LegacyMinecraftInterface.java b/src/main/java/amidst/mojangapi/minecraftinterface/legacy/LegacyMinecraftInterface.java index 699961a31..24b990d06 100644 --- a/src/main/java/amidst/mojangapi/minecraftinterface/legacy/LegacyMinecraftInterface.java +++ b/src/main/java/amidst/mojangapi/minecraftinterface/legacy/LegacyMinecraftInterface.java @@ -10,14 +10,13 @@ import amidst.clazz.symbolic.SymbolicClass; import amidst.clazz.symbolic.SymbolicObject; import amidst.documentation.ThreadSafe; -import amidst.mojangapi.minecraftinterface.MinecraftInterface; -import amidst.mojangapi.minecraftinterface.MinecraftInterfaceException; -import amidst.mojangapi.minecraftinterface.RecognisedVersion; -import amidst.mojangapi.minecraftinterface.ReflectionUtils; -import amidst.mojangapi.minecraftinterface.UnsupportedDimensionException; +import amidst.mojangapi.minecraftinterface.*; import amidst.mojangapi.world.Dimension; +import amidst.mojangapi.world.SeedHistoryLogger; import amidst.mojangapi.world.WorldOptions; import amidst.mojangapi.world.WorldType; +import amidst.mojangapi.world.versionfeatures.DefaultVersionFeatures; +import amidst.mojangapi.world.versionfeatures.VersionFeatures; @ThreadSafe /** @@ -154,6 +153,16 @@ private SymbolicObject getWorldType(WorldType worldType) throws IllegalArgumentE public RecognisedVersion getRecognisedVersion() { return recognisedVersion; } + public VersionFeatures initInterfaceAndGetFeatures(WorldOptions worldOptions, MinecraftInterface minecraftInterface, SeedHistoryLogger seedHistoryLogger) + throws MinecraftInterfaceException { + RecognisedVersion recognisedVersion = minecraftInterface.getRecognisedVersion(); + if(minecraftInterface instanceof LoggingMinecraftInterface) { + ((LoggingMinecraftInterface) minecraftInterface).logNextAccessor(); + } + MinecraftInterface.WorldAccessor worldAccessor = new ThreadedWorldAccessor(v -> minecraftInterface.createWorldAccessor(worldOptions)); + seedHistoryLogger.log(recognisedVersion, worldOptions.getWorldSeed()); + return DefaultVersionFeatures.builder(worldOptions, worldAccessor).create(recognisedVersion); + } private class WorldAccessor implements MinecraftInterface.WorldAccessor { private final Object quarterResolutionBiomeGenerator; @@ -180,5 +189,16 @@ public T getBiomeData(Dimension dimension, public Set supportedDimensions() { return Collections.singleton(Dimension.OVERWORLD); } + + public VersionFeatures initInterfaceAndGetFeatures(WorldOptions worldOptions, MinecraftInterface minecraftInterface, SeedHistoryLogger seedHistoryLogger) + throws MinecraftInterfaceException { + RecognisedVersion recognisedVersion = minecraftInterface.getRecognisedVersion(); + if(minecraftInterface instanceof LoggingMinecraftInterface) { + ((LoggingMinecraftInterface) minecraftInterface).logNextAccessor(); + } + MinecraftInterface.WorldAccessor worldAccessor = new ThreadedWorldAccessor(v -> minecraftInterface.createWorldAccessor(worldOptions)); + seedHistoryLogger.log(recognisedVersion, worldOptions.getWorldSeed()); + return DefaultVersionFeatures.builder(worldOptions, worldAccessor).create(recognisedVersion); + } } } diff --git a/src/main/java/amidst/mojangapi/minecraftinterface/legacy/_1_13MinecraftInterface.java b/src/main/java/amidst/mojangapi/minecraftinterface/legacy/_1_13MinecraftInterface.java index 9a93beedc..2d1dd1301 100644 --- a/src/main/java/amidst/mojangapi/minecraftinterface/legacy/_1_13MinecraftInterface.java +++ b/src/main/java/amidst/mojangapi/minecraftinterface/legacy/_1_13MinecraftInterface.java @@ -12,14 +12,13 @@ import amidst.clazz.symbolic.SymbolicClass; import amidst.clazz.symbolic.SymbolicObject; import amidst.logging.AmidstLogger; -import amidst.mojangapi.minecraftinterface.MinecraftInterface; -import amidst.mojangapi.minecraftinterface.MinecraftInterfaceException; -import amidst.mojangapi.minecraftinterface.RecognisedVersion; -import amidst.mojangapi.minecraftinterface.ReflectionUtils; -import amidst.mojangapi.minecraftinterface.UnsupportedDimensionException; +import amidst.mojangapi.minecraftinterface.*; import amidst.mojangapi.world.Dimension; +import amidst.mojangapi.world.SeedHistoryLogger; import amidst.mojangapi.world.WorldOptions; import amidst.mojangapi.world.WorldType; +import amidst.mojangapi.world.versionfeatures.DefaultVersionFeatures; +import amidst.mojangapi.world.versionfeatures.VersionFeatures; import amidst.util.ArrayCache; public class _1_13MinecraftInterface implements MinecraftInterface { @@ -245,6 +244,18 @@ public RecognisedVersion getRecognisedVersion() { return recognisedVersion; } + @Override + public VersionFeatures initInterfaceAndGetFeatures(WorldOptions worldOptions, MinecraftInterface minecraftInterface, SeedHistoryLogger seedHistoryLogger) + throws MinecraftInterfaceException { + RecognisedVersion recognisedVersion = minecraftInterface.getRecognisedVersion(); + if(minecraftInterface instanceof LoggingMinecraftInterface) { + ((LoggingMinecraftInterface) minecraftInterface).logNextAccessor(); + } + MinecraftInterface.WorldAccessor worldAccessor = new ThreadedWorldAccessor(v -> minecraftInterface.createWorldAccessor(worldOptions)); + seedHistoryLogger.log(recognisedVersion, worldOptions.getWorldSeed()); + return DefaultVersionFeatures.builder(worldOptions, worldAccessor).create(recognisedVersion); + } + private class WorldAccessor implements MinecraftInterface.WorldAccessor { /** * A GenLayer instance, at quarter scale to the final biome layer (i.e. both diff --git a/src/main/java/amidst/mojangapi/minecraftinterface/legacy/_1_15MinecraftInterface.java b/src/main/java/amidst/mojangapi/minecraftinterface/legacy/_1_15MinecraftInterface.java index 31fdefa47..9669c8085 100644 --- a/src/main/java/amidst/mojangapi/minecraftinterface/legacy/_1_15MinecraftInterface.java +++ b/src/main/java/amidst/mojangapi/minecraftinterface/legacy/_1_15MinecraftInterface.java @@ -25,14 +25,13 @@ import amidst.clazz.symbolic.SymbolicClass; import amidst.clazz.symbolic.SymbolicObject; import amidst.logging.AmidstLogger; -import amidst.mojangapi.minecraftinterface.MinecraftInterface; -import amidst.mojangapi.minecraftinterface.MinecraftInterfaceException; -import amidst.mojangapi.minecraftinterface.RecognisedVersion; -import amidst.mojangapi.minecraftinterface.ReflectionUtils; -import amidst.mojangapi.minecraftinterface.UnsupportedDimensionException; +import amidst.mojangapi.minecraftinterface.*; import amidst.mojangapi.world.Dimension; +import amidst.mojangapi.world.SeedHistoryLogger; import amidst.mojangapi.world.WorldOptions; import amidst.mojangapi.world.WorldType; +import amidst.mojangapi.world.versionfeatures.DefaultVersionFeatures; +import amidst.mojangapi.world.versionfeatures.VersionFeatures; import amidst.util.ArrayCache; public class _1_15MinecraftInterface implements MinecraftInterface { @@ -266,6 +265,18 @@ public RecognisedVersion getRecognisedVersion() { return recognisedVersion; } + @Override + public VersionFeatures initInterfaceAndGetFeatures(WorldOptions worldOptions, MinecraftInterface minecraftInterface, SeedHistoryLogger seedHistoryLogger) + throws MinecraftInterfaceException { + RecognisedVersion recognisedVersion = minecraftInterface.getRecognisedVersion(); + if(minecraftInterface instanceof LoggingMinecraftInterface) { + ((LoggingMinecraftInterface) minecraftInterface).logNextAccessor(); + } + MinecraftInterface.WorldAccessor worldAccessor = new ThreadedWorldAccessor(v -> minecraftInterface.createWorldAccessor(worldOptions)); + seedHistoryLogger.log(recognisedVersion, worldOptions.getWorldSeed()); + return DefaultVersionFeatures.builder(worldOptions, worldAccessor).create(recognisedVersion); + } + private synchronized void initializeIfNeeded() throws MinecraftInterfaceException { if (isInitialized) { return; diff --git a/src/main/java/amidst/mojangapi/minecraftinterface/local/LocalMinecraftInterface.java b/src/main/java/amidst/mojangapi/minecraftinterface/local/LocalMinecraftInterface.java index 21a5739b1..342410b6c 100644 --- a/src/main/java/amidst/mojangapi/minecraftinterface/local/LocalMinecraftInterface.java +++ b/src/main/java/amidst/mojangapi/minecraftinterface/local/LocalMinecraftInterface.java @@ -20,14 +20,13 @@ import amidst.clazz.symbolic.SymbolicClass; import amidst.clazz.symbolic.SymbolicObject; import amidst.logging.AmidstLogger; -import amidst.mojangapi.minecraftinterface.MinecraftInterface; -import amidst.mojangapi.minecraftinterface.MinecraftInterfaceException; -import amidst.mojangapi.minecraftinterface.RecognisedVersion; -import amidst.mojangapi.minecraftinterface.ReflectionUtils; -import amidst.mojangapi.minecraftinterface.UnsupportedDimensionException; +import amidst.mojangapi.minecraftinterface.*; import amidst.mojangapi.world.Dimension; +import amidst.mojangapi.world.SeedHistoryLogger; import amidst.mojangapi.world.WorldOptions; import amidst.mojangapi.world.WorldType; +import amidst.mojangapi.world.versionfeatures.DefaultVersionFeatures; +import amidst.mojangapi.world.versionfeatures.VersionFeatures; import amidst.util.ArrayCache; public class LocalMinecraftInterface implements MinecraftInterface { @@ -187,6 +186,18 @@ public RecognisedVersion getRecognisedVersion() { return recognisedVersion; } + @Override + public VersionFeatures initInterfaceAndGetFeatures(WorldOptions worldOptions, MinecraftInterface minecraftInterface, SeedHistoryLogger seedHistoryLogger) + throws MinecraftInterfaceException { + RecognisedVersion recognisedVersion = minecraftInterface.getRecognisedVersion(); + if(minecraftInterface instanceof LoggingMinecraftInterface) { + ((LoggingMinecraftInterface) minecraftInterface).logNextAccessor(); + } + MinecraftInterface.WorldAccessor worldAccessor = new ThreadedWorldAccessor(v -> minecraftInterface.createWorldAccessor(worldOptions)); + seedHistoryLogger.log(recognisedVersion, worldOptions.getWorldSeed()); + return DefaultVersionFeatures.builder(worldOptions, worldAccessor).create(recognisedVersion); + } + private synchronized void initializeIfNeeded() throws MinecraftInterfaceException { if (isInitialized) { return; diff --git a/src/main/java/amidst/mojangapi/world/WorldBuilder.java b/src/main/java/amidst/mojangapi/world/WorldBuilder.java index 54677eb43..b29d19fb1 100644 --- a/src/main/java/amidst/mojangapi/world/WorldBuilder.java +++ b/src/main/java/amidst/mojangapi/world/WorldBuilder.java @@ -46,7 +46,7 @@ public WorldBuilder(PlayerInformationProvider playerInformationProvider, SeedHis public World from( MinecraftInterface minecraftInterface, WorldOptions worldOptions) throws MinecraftInterfaceException { - VersionFeatures versionFeatures = initInterfaceAndGetFeatures(worldOptions, minecraftInterface); + VersionFeatures versionFeatures = minecraftInterface.initInterfaceAndGetFeatures(worldOptions, minecraftInterface, seedHistoryLogger); return create( minecraftInterface.getRecognisedVersion(), MovablePlayerList.dummy(), @@ -57,7 +57,7 @@ public World from( public World fromSaveGame(MinecraftInterface minecraftInterface, SaveGame saveGame) throws IOException, MinecraftInterfaceException { - VersionFeatures versionFeatures = initInterfaceAndGetFeatures(WorldOptions.fromSaveGame(saveGame), minecraftInterface); + VersionFeatures versionFeatures = minecraftInterface.initInterfaceAndGetFeatures(WorldOptions.fromSaveGame(saveGame), minecraftInterface, seedHistoryLogger); return create( minecraftInterface.getRecognisedVersion(), new MovablePlayerList( @@ -69,17 +69,6 @@ public World fromSaveGame(MinecraftInterface minecraftInterface, SaveGame saveGa new ImmutableWorldSpawnOracle(saveGame.getWorldSpawn())); } - private VersionFeatures initInterfaceAndGetFeatures(WorldOptions worldOptions, MinecraftInterface minecraftInterface) - throws MinecraftInterfaceException { - RecognisedVersion recognisedVersion = minecraftInterface.getRecognisedVersion(); - if(minecraftInterface instanceof LoggingMinecraftInterface) { - ((LoggingMinecraftInterface) minecraftInterface).logNextAccessor(); - } - MinecraftInterface.WorldAccessor worldAccessor = new ThreadedWorldAccessor(v -> minecraftInterface.createWorldAccessor(worldOptions)); - seedHistoryLogger.log(recognisedVersion, worldOptions.getWorldSeed()); - return DefaultVersionFeatures.builder(worldOptions, worldAccessor).create(recognisedVersion); - } - private World create( RecognisedVersion recognisedVersion, MovablePlayerList movablePlayerList, diff --git a/src/test/java/amidst/mojangapi/mocking/BenchmarkingMinecraftInterface.java b/src/test/java/amidst/mojangapi/mocking/BenchmarkingMinecraftInterface.java index ec2e3020d..b04fd76bf 100644 --- a/src/test/java/amidst/mojangapi/mocking/BenchmarkingMinecraftInterface.java +++ b/src/test/java/amidst/mojangapi/mocking/BenchmarkingMinecraftInterface.java @@ -6,12 +6,13 @@ import java.util.function.Function; import amidst.documentation.ThreadSafe; -import amidst.mojangapi.minecraftinterface.MinecraftInterface; -import amidst.mojangapi.minecraftinterface.MinecraftInterfaceException; -import amidst.mojangapi.minecraftinterface.RecognisedVersion; +import amidst.mojangapi.minecraftinterface.*; import amidst.mojangapi.mocking.json.BiomeRequestRecordJson; import amidst.mojangapi.world.Dimension; +import amidst.mojangapi.world.SeedHistoryLogger; import amidst.mojangapi.world.WorldOptions; +import amidst.mojangapi.world.versionfeatures.DefaultVersionFeatures; +import amidst.mojangapi.world.versionfeatures.VersionFeatures; @ThreadSafe public class BenchmarkingMinecraftInterface implements MinecraftInterface { @@ -33,6 +34,18 @@ public RecognisedVersion getRecognisedVersion() { return inner.getRecognisedVersion(); } + @Override + public VersionFeatures initInterfaceAndGetFeatures(WorldOptions worldOptions, MinecraftInterface minecraftInterface, SeedHistoryLogger seedHistoryLogger) + throws MinecraftInterfaceException { + RecognisedVersion recognisedVersion = minecraftInterface.getRecognisedVersion(); + if(minecraftInterface instanceof LoggingMinecraftInterface) { + ((LoggingMinecraftInterface) minecraftInterface).logNextAccessor(); + } + MinecraftInterface.WorldAccessor worldAccessor = new ThreadedWorldAccessor(v -> minecraftInterface.createWorldAccessor(worldOptions)); + seedHistoryLogger.log(recognisedVersion, worldOptions.getWorldSeed()); + return DefaultVersionFeatures.builder(worldOptions, worldAccessor).create(recognisedVersion); + } + private class WorldAccessor implements MinecraftInterface.WorldAccessor { private final MinecraftInterface.WorldAccessor innerWorld; diff --git a/src/test/java/amidst/mojangapi/mocking/FakeMinecraftInterface.java b/src/test/java/amidst/mojangapi/mocking/FakeMinecraftInterface.java index 41e234db6..811b18961 100644 --- a/src/test/java/amidst/mojangapi/mocking/FakeMinecraftInterface.java +++ b/src/test/java/amidst/mojangapi/mocking/FakeMinecraftInterface.java @@ -5,13 +5,14 @@ import java.util.function.Function; import amidst.documentation.ThreadSafe; -import amidst.mojangapi.minecraftinterface.MinecraftInterface; -import amidst.mojangapi.minecraftinterface.MinecraftInterfaceException; -import amidst.mojangapi.minecraftinterface.RecognisedVersion; +import amidst.mojangapi.minecraftinterface.*; import amidst.mojangapi.world.Dimension; +import amidst.mojangapi.world.SeedHistoryLogger; import amidst.mojangapi.world.WorldOptions; import amidst.mojangapi.world.testworld.storage.json.BiomeDataJson; import amidst.mojangapi.world.testworld.storage.json.WorldMetadataJson; +import amidst.mojangapi.world.versionfeatures.DefaultVersionFeatures; +import amidst.mojangapi.world.versionfeatures.VersionFeatures; @ThreadSafe public class FakeMinecraftInterface implements MinecraftInterface { @@ -43,6 +44,18 @@ public RecognisedVersion getRecognisedVersion() { return worldMetadataJson.getRecognisedVersion(); } + @Override + public VersionFeatures initInterfaceAndGetFeatures(WorldOptions worldOptions, MinecraftInterface minecraftInterface, SeedHistoryLogger seedHistoryLogger) + throws MinecraftInterfaceException { + RecognisedVersion recognisedVersion = minecraftInterface.getRecognisedVersion(); + if(minecraftInterface instanceof LoggingMinecraftInterface) { + ((LoggingMinecraftInterface) minecraftInterface).logNextAccessor(); + } + MinecraftInterface.WorldAccessor worldAccessor = new ThreadedWorldAccessor(v -> minecraftInterface.createWorldAccessor(worldOptions)); + seedHistoryLogger.log(recognisedVersion, worldOptions.getWorldSeed()); + return DefaultVersionFeatures.builder(worldOptions, worldAccessor).create(recognisedVersion); + } + private class WorldAccessor implements MinecraftInterface.WorldAccessor { private WorldAccessor() { } diff --git a/src/test/java/amidst/mojangapi/mocking/RequestStoringMinecraftInterface.java b/src/test/java/amidst/mojangapi/mocking/RequestStoringMinecraftInterface.java index 8f10da5fe..625e29877 100644 --- a/src/test/java/amidst/mojangapi/mocking/RequestStoringMinecraftInterface.java +++ b/src/test/java/amidst/mojangapi/mocking/RequestStoringMinecraftInterface.java @@ -4,11 +4,12 @@ import java.util.function.Function; import amidst.documentation.ThreadSafe; -import amidst.mojangapi.minecraftinterface.MinecraftInterface; -import amidst.mojangapi.minecraftinterface.MinecraftInterfaceException; -import amidst.mojangapi.minecraftinterface.RecognisedVersion; +import amidst.mojangapi.minecraftinterface.*; import amidst.mojangapi.world.Dimension; +import amidst.mojangapi.world.SeedHistoryLogger; import amidst.mojangapi.world.WorldOptions; +import amidst.mojangapi.world.versionfeatures.DefaultVersionFeatures; +import amidst.mojangapi.world.versionfeatures.VersionFeatures; @ThreadSafe public class RequestStoringMinecraftInterface implements MinecraftInterface { @@ -34,6 +35,18 @@ public synchronized RecognisedVersion getRecognisedVersion() { return realMinecraftInterface.getRecognisedVersion(); } + @Override + public VersionFeatures initInterfaceAndGetFeatures(WorldOptions worldOptions, MinecraftInterface minecraftInterface, SeedHistoryLogger seedHistoryLogger) + throws MinecraftInterfaceException { + RecognisedVersion recognisedVersion = minecraftInterface.getRecognisedVersion(); + if(minecraftInterface instanceof LoggingMinecraftInterface) { + ((LoggingMinecraftInterface) minecraftInterface).logNextAccessor(); + } + MinecraftInterface.WorldAccessor worldAccessor = new ThreadedWorldAccessor(v -> minecraftInterface.createWorldAccessor(worldOptions)); + seedHistoryLogger.log(recognisedVersion, worldOptions.getWorldSeed()); + return DefaultVersionFeatures.builder(worldOptions, worldAccessor).create(recognisedVersion); + } + private class WorldAccessor implements MinecraftInterface.WorldAccessor { private final MinecraftInterface.WorldAccessor realMinecraftWorld; From 0d9b71d6d8d90ca6029d797be81f4da9fe18d78d Mon Sep 17 00:00:00 2001 From: sarthakpatel Date: Wed, 5 Apr 2023 19:10:29 -0300 Subject: [PATCH 2/5] Extract Class Implemented. --- .../amidst/fragment/drawer/GridDrawer.java | 39 ++---------- .../amidst/fragment/drawer/TextDrawer.java | 59 +++++++++++++++++++ 2 files changed, 63 insertions(+), 35 deletions(-) create mode 100644 src/main/java/amidst/fragment/drawer/TextDrawer.java diff --git a/src/main/java/amidst/fragment/drawer/GridDrawer.java b/src/main/java/amidst/fragment/drawer/GridDrawer.java index 3b1ef020b..16c0bc45f 100644 --- a/src/main/java/amidst/fragment/drawer/GridDrawer.java +++ b/src/main/java/amidst/fragment/drawer/GridDrawer.java @@ -19,6 +19,7 @@ public class GridDrawer extends FragmentDrawer { private final StringBuffer textBuffer = new StringBuffer(128); private final char[] textCache = new char[128]; private final Zoom zoom; + TextDrawer textDrawer = new TextDrawer(); public GridDrawer(LayerDeclaration declaration, Zoom zoom) { super(declaration); @@ -36,10 +37,10 @@ public void draw(Fragment fragment, Graphics2D g2d, float time) { if (isGrid00(gridX, gridY)) { double invZoom = 1.0 / zoom.getCurrentValue(); g2d.scale(invZoom, invZoom); - updateText(fragment); - drawText(g2d); + textDrawer.updateText(fragment); + textDrawer.drawText(g2d); // drawThickTextOutline(g2d); - drawTextOutline(g2d); + textDrawer.drawTextOutline(g2d); } } @@ -84,36 +85,4 @@ private void drawGridLines(Graphics2D g2d, int stride, int gridX, int gridY) { private boolean isGrid00(int gridX, int gridY) { return gridX == 0 && gridY == 0; } - - @CalledOnlyBy(AmidstThread.EDT) - private void updateText(Fragment fragment) { - textBuffer.setLength(0); - textBuffer.append(fragment.getCorner().getX()); - textBuffer.append(", "); - textBuffer.append(fragment.getCorner().getY()); - textBuffer.getChars(0, textBuffer.length(), textCache, 0); - } - - @CalledOnlyBy(AmidstThread.EDT) - private void drawText(Graphics2D g2d) { - g2d.drawChars(textCache, 0, textBuffer.length(), 12, 17); - g2d.drawChars(textCache, 0, textBuffer.length(), 8, 17); - g2d.drawChars(textCache, 0, textBuffer.length(), 10, 19); - g2d.drawChars(textCache, 0, textBuffer.length(), 10, 15); - } - - // This makes the text outline a bit thicker, but seems unneeded. - @SuppressWarnings("unused") - private void drawThickTextOutline(Graphics2D g2d) { - g2d.drawChars(textCache, 0, textBuffer.length(), 12, 15); - g2d.drawChars(textCache, 0, textBuffer.length(), 12, 19); - g2d.drawChars(textCache, 0, textBuffer.length(), 8, 15); - g2d.drawChars(textCache, 0, textBuffer.length(), 8, 19); - } - - @CalledOnlyBy(AmidstThread.EDT) - private void drawTextOutline(Graphics2D g2d) { - g2d.setColor(Color.white); - g2d.drawChars(textCache, 0, textBuffer.length(), 10, 17); - } } diff --git a/src/main/java/amidst/fragment/drawer/TextDrawer.java b/src/main/java/amidst/fragment/drawer/TextDrawer.java new file mode 100644 index 000000000..197451fee --- /dev/null +++ b/src/main/java/amidst/fragment/drawer/TextDrawer.java @@ -0,0 +1,59 @@ +package amidst.fragment.drawer; + +import java.awt.Color; +import java.awt.Font; +import java.awt.Graphics2D; + +import amidst.documentation.AmidstThread; +import amidst.documentation.CalledOnlyBy; +import amidst.documentation.NotThreadSafe; +import amidst.fragment.Fragment; +import amidst.mojangapi.world.coordinates.Resolution; + +@NotThreadSafe +public class TextDrawer { + + private static final Font DRAW_FONT = new Font("arial", Font.BOLD, 16); + + private final StringBuffer textBuffer = new StringBuffer(128); + private final char[] textCache = new char[128]; + + @CalledOnlyBy(AmidstThread.EDT) + public void updateText(Fragment fragment) { + textBuffer.setLength(0); + textBuffer.append(fragment.getCorner().getX()); + textBuffer.append(", "); + textBuffer.append(fragment.getCorner().getY()); + textBuffer.getChars(0, textBuffer.length(), textCache, 0); + } + + @CalledOnlyBy(AmidstThread.EDT) + public void drawText(Graphics2D g2d) { + g2d.drawChars(textCache, 0, textBuffer.length(), 12, 17); + g2d.drawChars(textCache, 0, textBuffer.length(), 8, 17); + g2d.drawChars(textCache, 0, textBuffer.length(), 10, 19); + g2d.drawChars(textCache, 0, textBuffer.length(), 10, 15); + } + + // This makes the text outline a bit thicker, but seems unneeded. + @SuppressWarnings("unused") + private void drawThickTextOutline(Graphics2D g2d) { + g2d.drawChars(textCache, 0, textBuffer.length(), 12, 15); + g2d.drawChars(textCache, 0, textBuffer.length(), 12, 19); + g2d.drawChars(textCache, 0, textBuffer.length(), 8, 15); + g2d.drawChars(textCache, 0, textBuffer.length(), 8, 19); + } + + @CalledOnlyBy(AmidstThread.EDT) + public void drawTextOutline(Graphics2D g2d) { + g2d.setColor(Color.white); + g2d.drawChars(textCache, 0, textBuffer.length(), 10, 17); + } + + @CalledOnlyBy(AmidstThread.EDT) + public void initGraphics(Graphics2D g2d) { + g2d.setFont(DRAW_FONT); + g2d.setColor(Color.black); + } + +} From c5aa4c2d7bdff9c0ddbd754e261998d5f4823c50 Mon Sep 17 00:00:00 2001 From: sarthakpatel Date: Thu, 6 Apr 2023 03:35:33 -0300 Subject: [PATCH 3/5] Replace Conditional with Polymorphism implemented. --- .../java/amidst/mojangapi/world/WorldBuilder.java | 2 +- .../mojangapi/world/player/BothWorldPlayerType.java | 9 +++++++++ .../world/player/MultiplayerWorldPlayerType.java | 9 +++++++++ .../mojangapi/world/player/NoneWorldPlayerType.java | 9 +++++++++ .../world/player/SingleplayerWorldPlayerType.java | 9 +++++++++ .../mojangapi/world/player/WorldPlayerType.java | 11 ++++++----- 6 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 src/main/java/amidst/mojangapi/world/player/BothWorldPlayerType.java create mode 100644 src/main/java/amidst/mojangapi/world/player/MultiplayerWorldPlayerType.java create mode 100644 src/main/java/amidst/mojangapi/world/player/NoneWorldPlayerType.java create mode 100644 src/main/java/amidst/mojangapi/world/player/SingleplayerWorldPlayerType.java diff --git a/src/main/java/amidst/mojangapi/world/WorldBuilder.java b/src/main/java/amidst/mojangapi/world/WorldBuilder.java index b29d19fb1..14a52ae59 100644 --- a/src/main/java/amidst/mojangapi/world/WorldBuilder.java +++ b/src/main/java/amidst/mojangapi/world/WorldBuilder.java @@ -64,7 +64,7 @@ public World fromSaveGame(MinecraftInterface minecraftInterface, SaveGame saveGa playerInformationProvider, saveGame, true, - WorldPlayerType.from(saveGame)), + (WorldPlayerType) WorldPlayerType.from(saveGame)), versionFeatures, new ImmutableWorldSpawnOracle(saveGame.getWorldSpawn())); } diff --git a/src/main/java/amidst/mojangapi/world/player/BothWorldPlayerType.java b/src/main/java/amidst/mojangapi/world/player/BothWorldPlayerType.java new file mode 100644 index 000000000..38d18ceb5 --- /dev/null +++ b/src/main/java/amidst/mojangapi/world/player/BothWorldPlayerType.java @@ -0,0 +1,9 @@ +package amidst.mojangapi.world.player; + +import amidst.mojangapi.file.SaveGame; + +public class BothWorldPlayerType { + public BothWorldPlayerType from(SaveGame saveGame) { + return new BothWorldPlayerType(); + } +} diff --git a/src/main/java/amidst/mojangapi/world/player/MultiplayerWorldPlayerType.java b/src/main/java/amidst/mojangapi/world/player/MultiplayerWorldPlayerType.java new file mode 100644 index 000000000..74bf7cc0f --- /dev/null +++ b/src/main/java/amidst/mojangapi/world/player/MultiplayerWorldPlayerType.java @@ -0,0 +1,9 @@ +package amidst.mojangapi.world.player; + +import amidst.mojangapi.file.SaveGame; + +public class MultiplayerWorldPlayerType { + public MultiplayerWorldPlayerType from(SaveGame saveGame) { + return new MultiplayerWorldPlayerType(); + } +} diff --git a/src/main/java/amidst/mojangapi/world/player/NoneWorldPlayerType.java b/src/main/java/amidst/mojangapi/world/player/NoneWorldPlayerType.java new file mode 100644 index 000000000..ec2454f79 --- /dev/null +++ b/src/main/java/amidst/mojangapi/world/player/NoneWorldPlayerType.java @@ -0,0 +1,9 @@ +package amidst.mojangapi.world.player; + +import amidst.mojangapi.file.SaveGame; + +public class NoneWorldPlayerType { + public NoneWorldPlayerType from(SaveGame saveGame) { + return new NoneWorldPlayerType(); + } +} diff --git a/src/main/java/amidst/mojangapi/world/player/SingleplayerWorldPlayerType.java b/src/main/java/amidst/mojangapi/world/player/SingleplayerWorldPlayerType.java new file mode 100644 index 000000000..8690274d3 --- /dev/null +++ b/src/main/java/amidst/mojangapi/world/player/SingleplayerWorldPlayerType.java @@ -0,0 +1,9 @@ +package amidst.mojangapi.world.player; + +import amidst.mojangapi.file.SaveGame; + +public class SingleplayerWorldPlayerType { + public SingleplayerWorldPlayerType from(SaveGame saveGame) { + return new SingleplayerWorldPlayerType(); + } +} diff --git a/src/main/java/amidst/mojangapi/world/player/WorldPlayerType.java b/src/main/java/amidst/mojangapi/world/player/WorldPlayerType.java index 21b50dad4..d7e3fa125 100644 --- a/src/main/java/amidst/mojangapi/world/player/WorldPlayerType.java +++ b/src/main/java/amidst/mojangapi/world/player/WorldPlayerType.java @@ -24,17 +24,18 @@ public static List getSelectable() { return SELECTABLE; } - public static WorldPlayerType from(SaveGame saveGame) { + public static Object from(SaveGame saveGame) { boolean hasSingleplayerPlayer = saveGame.hasSingleplayerPlayer(); boolean hasMultiplayerPlayers = saveGame.hasMultiplayerPlayers(); + if (hasSingleplayerPlayer && hasMultiplayerPlayers) { - return BOTH; + return new BothWorldPlayerType().from(saveGame); } else if (hasSingleplayerPlayer) { - return SINGLEPLAYER; + return new SingleplayerWorldPlayerType().from(saveGame); } else if (hasMultiplayerPlayers) { - return MULTIPLAYER; + return new MultiplayerWorldPlayerType().from(saveGame); } else { - return NONE; + return new NoneWorldPlayerType().from(saveGame); } } From 0e9d5e9d927858b1107e1931819a0225dfdf64c1 Mon Sep 17 00:00:00 2001 From: sarthakpatel Date: Thu, 6 Apr 2023 17:28:11 -0300 Subject: [PATCH 4/5] Decompose Conditional Implemented. --- .../mocking/FakeMinecraftInterface.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/test/java/amidst/mojangapi/mocking/FakeMinecraftInterface.java b/src/test/java/amidst/mojangapi/mocking/FakeMinecraftInterface.java index 811b18961..075996cad 100644 --- a/src/test/java/amidst/mojangapi/mocking/FakeMinecraftInterface.java +++ b/src/test/java/amidst/mojangapi/mocking/FakeMinecraftInterface.java @@ -30,13 +30,27 @@ public FakeMinecraftInterface( } @Override - public MinecraftInterface.WorldAccessor createWorldAccessor(WorldOptions worldOptions) throws MinecraftInterfaceException { + /*public MinecraftInterface.WorldAccessor createWorldAccessor(WorldOptions worldOptions) throws MinecraftInterfaceException { if (worldMetadataJson.getSeed() == worldOptions.getWorldSeed().getLong() && worldMetadataJson.getWorldType().equals(worldOptions.getWorldType()) && worldOptions.getGeneratorOptions().isEmpty()) { return new WorldAccessor(); } else { throw new MinecraftInterfaceException("the world has to match"); } + }*/ + + public MinecraftInterface.WorldAccessor createWorldAccessor(WorldOptions worldOptions) throws MinecraftInterfaceException { + if (worldMatches(worldOptions)) { + return new WorldAccessor(); + } else { + throw new MinecraftInterfaceException("the world has to match"); + } + } + + private boolean worldMatches(WorldOptions worldOptions) { + return worldMetadataJson.getSeed() == worldOptions.getWorldSeed().getLong() && + worldMetadataJson.getWorldType().equals(worldOptions.getWorldType()) && + worldOptions.getGeneratorOptions().isEmpty(); } @Override From ca55af393d2253bc6f6c1f0e649989e251bdfb43 Mon Sep 17 00:00:00 2001 From: sarthakpatel Date: Thu, 6 Apr 2023 17:33:21 -0300 Subject: [PATCH 5/5] Renaming method implemented. --- src/main/java/amidst/fragment/ClosestWorldIconFinder.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/amidst/fragment/ClosestWorldIconFinder.java b/src/main/java/amidst/fragment/ClosestWorldIconFinder.java index 25a93d547..cb5674d29 100644 --- a/src/main/java/amidst/fragment/ClosestWorldIconFinder.java +++ b/src/main/java/amidst/fragment/ClosestWorldIconFinder.java @@ -26,11 +26,11 @@ public ClosestWorldIconFinder( this.positionInWorld = positionInWorld; this.closestIcon = null; this.closestDistanceSq = maxDistanceInWorld * maxDistanceInWorld; - find(); + findWorldIcon(); } @CalledOnlyBy(AmidstThread.EDT) - private void find() { + private void findWorldIcon() { for (FragmentGraphItem fragmentGraphItem : graph) { Fragment fragment = fragmentGraphItem.getFragment(); for (LayerDeclaration declaration : declarations) {