From 7f0bd579b46c608e1f7af5e8ae7c1ce5e85bf8b3 Mon Sep 17 00:00:00 2001 From: Mysticpasta1 Date: Wed, 15 Sep 2021 20:06:21 -0500 Subject: [PATCH] fixed more stuff - refactored duplicate code in some checks - replace empty if (JEID) check with if (!JEID) - change BiomeConfig[] to ArrayList() in a lot of places --- .../src/main/java/com/pg85/otg/OTGEngine.java | 18 ++++++++--------- .../otg/network/ServerConfigProvider.java | 20 +++++++++---------- .../biomes/ForgeBiomeRegistryManager.java | 7 ++++--- .../forge/events/client/ClientFogHandler.java | 2 +- .../gui/dimensions/OTGGuiDimensionList.java | 20 ++++++------------- .../packets/CreateDeleteDimensionPacket.java | 3 +-- 6 files changed, 31 insertions(+), 39 deletions(-) diff --git a/common/src/main/java/com/pg85/otg/OTGEngine.java b/common/src/main/java/com/pg85/otg/OTGEngine.java index f1baa82b0..f045845de 100644 --- a/common/src/main/java/com/pg85/otg/OTGEngine.java +++ b/common/src/main/java/com/pg85/otg/OTGEngine.java @@ -34,7 +34,7 @@ public abstract class OTGEngine { - private HashMap otgBiomeIdsByWorld = new HashMap(); + private HashMap> otgBiomeIdsByWorld = new HashMap>(); private BiomeModeManager biomeManagers; private List cancelableEventHandlers = new ArrayList(5); private BiomeResourcesManager biomeResourcesManager; @@ -304,29 +304,29 @@ public void setOTGBiomeId(String worldName, int i, BiomeConfig biomeConfig, bool { if(!otgBiomeIdsByWorld.containsKey(worldName)) { - otgBiomeIdsByWorld.put(worldName, new BiomeConfig[999999]); + otgBiomeIdsByWorld.put(worldName, new ArrayList<>()); } - if(replaceExisting || otgBiomeIdsByWorld.get(worldName)[i] == null) + if(replaceExisting || otgBiomeIdsByWorld.get(worldName).get(i) == null) { - otgBiomeIdsByWorld.get(worldName)[i] = biomeConfig; + otgBiomeIdsByWorld.get(worldName).set(i, biomeConfig); } else { - throw new RuntimeException("Tried to register OTG biome " + biomeConfig.getName() + " with id " + i + " but the id is in use by biome " + otgBiomeIdsByWorld.get(worldName)[i].getName() + ". OTG 1.12.2 v7 and above use dynamic biome id's for new worlds, this avoids the problem completely."); + throw new RuntimeException("Tried to register OTG biome " + biomeConfig.getName() + " with id " + i + " but the id is in use by biome " + otgBiomeIdsByWorld.get(worldName).get(i).getName() + ". OTG 1.12.2 v7 and above use dynamic biome id's for new worlds, this avoids the problem completely."); } } - public BiomeConfig[] getOTGBiomeIds(String worldName) + public ArrayList getOTGBiomeIds(String worldName) { - return otgBiomeIdsByWorld.containsKey(worldName) ? otgBiomeIdsByWorld.get(worldName) : new BiomeConfig[999999]; + return otgBiomeIdsByWorld.containsKey(worldName) ? otgBiomeIdsByWorld.get(worldName) : new ArrayList(); } public boolean isOTGBiomeIdAvailable(String worldName, int i) { - return !otgBiomeIdsByWorld.containsKey(worldName) || otgBiomeIdsByWorld.get(worldName)[i] == null; + return !otgBiomeIdsByWorld.containsKey(worldName) || otgBiomeIdsByWorld.get(worldName).get(i) == null; } public void unregisterOTGBiomeId(String worldName, int i) { - otgBiomeIdsByWorld.get(worldName)[i] = null; + otgBiomeIdsByWorld.get(worldName).set(i, null); } // Materials diff --git a/common/src/main/java/com/pg85/otg/network/ServerConfigProvider.java b/common/src/main/java/com/pg85/otg/network/ServerConfigProvider.java index 6c26a404a..350fa4b11 100644 --- a/common/src/main/java/com/pg85/otg/network/ServerConfigProvider.java +++ b/common/src/main/java/com/pg85/otg/network/ServerConfigProvider.java @@ -533,7 +533,7 @@ public int compare(BiomeConfig a, BiomeConfig b) { { OTG.getEngine().setOTGBiomeId(world.getName(), biomeIdData.otgBiomeId, biomeConfig, false); } - else if((world.getName() + "_" + OTG.getEngine().getOTGBiomeIds(world.getName())[biomeIdData.otgBiomeId].getName()).equals(biomeIdData.biomeName)) + else if((world.getName() + "_" + OTG.getEngine().getOTGBiomeIds(world.getName()).get(biomeIdData.otgBiomeId).getName()).equals(biomeIdData.biomeName)) { OTG.getEngine().setOTGBiomeId(world.getName(), biomeIdData.otgBiomeId, biomeConfig, true); } else { @@ -569,15 +569,15 @@ else if(biomeConfig.replaceToBiomeName != null && biomeConfig.replaceToBiomeName loadedBiomeNames.append(biomeConfig.getName()); loadedBiomeNames.append(", "); - BiomeConfig[] otgIds2 = OTG.getEngine().getOTGBiomeIds(world.getName()); + ArrayList otgIds2 = OTG.getEngine().getOTGBiomeIds(world.getName()); int otgBiomeId = -1; // Exclude already registered biomes from loadedBiomeIdData / default biomes boolean bFound = false; - for(int i = 0; i < otgIds2.length; i++) + for(int i = 0; i < otgIds2.size(); i++) { - BiomeConfig biomeConfig2 = otgIds2[i]; + BiomeConfig biomeConfig2 = otgIds2.get(i); if(biomeConfig == biomeConfig2) { bFound = true; @@ -603,9 +603,9 @@ else if( if(otgBiomeId == -1) { // Find the next available id - for (int i = 0; i < otgIds2.length; i++) // Virtual (replacetobiomename) biomes can only have id's above 255 + for (int i = 0; i < otgIds2.size(); i++) // Virtual (replacetobiomename) biomes can only have id's above 255 { - if ((biomeConfig.replaceToBiomeName.isEmpty() && (i > (otgIds2.length - 1 ))) || (biomeConfig.replaceToBiomeName.isEmpty() && i >= OTG.getEngine().getOTGBiomeIds(world.getName()).length)) { + if ((biomeConfig.replaceToBiomeName.isEmpty() && (i > (otgIds2.size() - 1 ))) || (biomeConfig.replaceToBiomeName.isEmpty() && i >= OTG.getEngine().getOTGBiomeIds(world.getName()).size())) { OTG.log(LogMarker.FATAL, "Biome could not be registered, no free biome id's!"); throw new RuntimeException("Biome could not be registered, no free biome id's!"); } @@ -685,10 +685,10 @@ private void createAndRegisterBiome(ArrayList loadedBiomeIdData, Bi // Get the assigned OTG biome id int otgBiomeId = -1; - BiomeConfig[] otgIds2 = OTG.getEngine().getOTGBiomeIds(world.getName()); - for(int i = 0; i < otgIds2.length; i++) + ArrayList otgIds2 = OTG.getEngine().getOTGBiomeIds(world.getName()); + for(int i = 0; i < otgIds2.size(); i++) { - if(otgIds2[i] == biomeConfig) + if(otgIds2.get(i) == biomeConfig) { otgBiomeId = i; break; @@ -752,7 +752,7 @@ private void createAndRegisterBiome(ArrayList loadedBiomeIdData, Bi * @param counter Simple recursion counter to make sure it doesn't endlessly recurse * @return The saved ID of a registered biome */ - private int findSavedBiomeId(BiomeConfig biomeConfig, BiomeConfig[] configArray, int counter) { + private int findSavedBiomeId(BiomeConfig biomeConfig, ArrayList configArray, int counter) { if (counter > 100) { OTG.log(LogMarker.FATAL, "Failed to replace, recursion went deeper than 100 layers. Did you create a replace loop at "+biomeConfig.replaceToBiomeName+"?"); throw new RuntimeException("Failed to replace, recursion went deeper than 100 layers. Did you create a replace loop at "+biomeConfig.replaceToBiomeName+"?"); diff --git a/platforms/forge/src/main/java/com/pg85/otg/forge/biomes/ForgeBiomeRegistryManager.java b/platforms/forge/src/main/java/com/pg85/otg/forge/biomes/ForgeBiomeRegistryManager.java index f2d9b70f6..b4680c7a1 100644 --- a/platforms/forge/src/main/java/com/pg85/otg/forge/biomes/ForgeBiomeRegistryManager.java +++ b/platforms/forge/src/main/java/com/pg85/otg/forge/biomes/ForgeBiomeRegistryManager.java @@ -145,15 +145,16 @@ else if(biomeIds.getSavedId() > -1) // Always try to register biomes and create Biome Configs. Biomes with id's > 255 are registered // only for biome -> id queries, any (saved)id -> biome query will return the ReplaceToBiomeName biome. - Biome existingBiome = Biome.getBiome(biomeIds.getSavedId()); + Biome existingBiome = Biome.getBiome(biomeIds.getSavedId()); + String errorText = "Could not allocate the requested id " + biomeIds.getSavedId() + " for biome "; if(JEID) { if (biomeIds.getSavedId() < 0) { - throw new RuntimeException("Could not allocate the requested id " + biomeIds.getSavedId() + " for biome " + biomeConfig.getName() + ". a biome id under 0 have been allocated\n" + ". Please report this to JEID issue tracker."); + throw new RuntimeException(errorText + biomeConfig.getName() + ". a biome id under 0 have been allocated\n" + ". Please report this to JEID issue tracker."); } } else { if (biomeIds.getSavedId() >= 256 || biomeIds.getSavedId() < 0) { - throw new RuntimeException("Could not allocate the requested id " + biomeIds.getSavedId() + " for biome " + biomeConfig.getName() + ". All available id's under 256 have been allocated\n" + ". To proceed, adjust your WorldConfig or use the ReplaceToBiomeName feature to make the biome virtual."); + throw new RuntimeException(errorText + ". All available id's under 256 have been allocated\n" + ". To proceed, adjust your WorldConfig or use the ReplaceToBiomeName feature to make the biome virtual."); } } diff --git a/platforms/forge/src/main/java/com/pg85/otg/forge/events/client/ClientFogHandler.java b/platforms/forge/src/main/java/com/pg85/otg/forge/events/client/ClientFogHandler.java index f3abc8f5e..19d984560 100644 --- a/platforms/forge/src/main/java/com/pg85/otg/forge/events/client/ClientFogHandler.java +++ b/platforms/forge/src/main/java/com/pg85/otg/forge/events/client/ClientFogHandler.java @@ -404,7 +404,7 @@ private BiomeConfig getBiomeConfig(ForgeWorld world, int x, int z, MutableBlockP short cachedId = biomeCache[x][z]; if (cachedId != -1 && !hasMoved) { - return OTG.getEngine().getOTGBiomeIds(world.getName())[cachedId]; + return OTG.getEngine().getOTGBiomeIds(world.getName()).get(cachedId); } else { Biome biome = world.getBiomeFromChunk(blockPos.getX(), blockPos.getZ()); LocalBiome localBiome = biome != null ? world.getBiomeByNameOrNull(biome.getBiomeName()) : null; diff --git a/platforms/forge/src/main/java/com/pg85/otg/forge/gui/dimensions/OTGGuiDimensionList.java b/platforms/forge/src/main/java/com/pg85/otg/forge/gui/dimensions/OTGGuiDimensionList.java index 5a630cdb9..d32e84df3 100644 --- a/platforms/forge/src/main/java/com/pg85/otg/forge/gui/dimensions/OTGGuiDimensionList.java +++ b/platforms/forge/src/main/java/com/pg85/otg/forge/gui/dimensions/OTGGuiDimensionList.java @@ -483,17 +483,18 @@ protected void actionPerformed(GuiButton button) throws IOException } } } + String preset = this.dimensions.get(0).PresetName; if(!bPortalColorsUnique) { this.mc.displayGuiScreen(new GuiErrorScreen("Error", "Multiple dimensions are using the same portal color, each dimension's portal color must be unique.")); } else if (JEID) { - this.mc.displayGuiScreen(new OTGGuiEnterWorldName(this, this.dimensions.get(0).PresetName)); + this.mc.displayGuiScreen(new OTGGuiEnterWorldName(this, preset)); } else { if (!OTG.getEngine().areEnoughBiomeIdsAvailableForPresets(presetNames)) { this.mc.displayGuiScreen(new GuiErrorScreen("Error", "Not enough biome id's available to add all dimensions.")); } else { - this.mc.displayGuiScreen(new OTGGuiEnterWorldName(this, this.dimensions.get(0).PresetName)); + this.mc.displayGuiScreen(new OTGGuiEnterWorldName(this, preset)); } } } else { @@ -526,7 +527,7 @@ else if (JEID) { { ArrayList presetNames = new ArrayList(); presetNames.add(dimConfig.PresetName); - if (JEID) { + if (OTG.getEngine().areEnoughBiomeIdsAvailableForPresets(presetNames) || JEID) { dimConfig.isNewConfig = false; OTG.IsNewWorldBeingCreated = true; if (!OTGDimensionManager.createNewDimensionSP(dimConfig, this.mc.getIntegratedServer())) { @@ -534,17 +535,8 @@ else if (JEID) { } OTG.IsNewWorldBeingCreated = false; } else { - if (OTG.getEngine().areEnoughBiomeIdsAvailableForPresets(presetNames)) { - dimConfig.isNewConfig = false; - OTG.IsNewWorldBeingCreated = true; - if (!OTGDimensionManager.createNewDimensionSP(dimConfig, this.mc.getIntegratedServer())) { - this.mc.displayGuiScreen(new GuiErrorScreen("Error", "Dimension id " + dimConfig.DimensionId + " was taken.")); - } - OTG.IsNewWorldBeingCreated = false; - } else { - this.mc.displayGuiScreen(new GuiErrorScreen("Error", "Not enough biome id's available to add all dimensions.")); - break; - } + this.mc.displayGuiScreen(new GuiErrorScreen("Error", "Not enough biome id's available to add all dimensions.")); + break; } } } diff --git a/platforms/forge/src/main/java/com/pg85/otg/forge/network/client/packets/CreateDeleteDimensionPacket.java b/platforms/forge/src/main/java/com/pg85/otg/forge/network/client/packets/CreateDeleteDimensionPacket.java index 12974d6e0..47c021893 100644 --- a/platforms/forge/src/main/java/com/pg85/otg/forge/network/client/packets/CreateDeleteDimensionPacket.java +++ b/platforms/forge/src/main/java/com/pg85/otg/forge/network/client/packets/CreateDeleteDimensionPacket.java @@ -109,8 +109,7 @@ public void run() // Ensure the portal color is unique (not already in use), otherwise correct it. PortalColors.correctPortalColor(dimConfig, OTG.getDimensionsConfig().getAllDimensions()); - if(JEID) { - } else { + if (!JEID) { ArrayList presetNames = new ArrayList(); presetNames.add(dimConfig.PresetName); if (!OTG.getEngine().areEnoughBiomeIdsAvailableForPresets(presetNames)) {