Skip to content

Commit

Permalink
fixed more stuff
Browse files Browse the repository at this point in the history
- refactored duplicate code in some checks
- replace empty if (JEID) check with if (!JEID)
- change BiomeConfig[] to ArrayList<BiomeConfig>() in a lot of places
  • Loading branch information
Mysticpasta1 committed Sep 16, 2021
1 parent f269734 commit 7f0bd57
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 39 deletions.
18 changes: 9 additions & 9 deletions common/src/main/java/com/pg85/otg/OTGEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

public abstract class OTGEngine
{
private HashMap<String, BiomeConfig[]> otgBiomeIdsByWorld = new HashMap<String, BiomeConfig[]>();
private HashMap<String, ArrayList<BiomeConfig>> otgBiomeIdsByWorld = new HashMap<String, ArrayList<BiomeConfig>>();
private BiomeModeManager biomeManagers;
private List<EventHandler> cancelableEventHandlers = new ArrayList<EventHandler>(5);
private BiomeResourcesManager biomeResourcesManager;
Expand Down Expand Up @@ -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<BiomeConfig> getOTGBiomeIds(String worldName)
{
return otgBiomeIdsByWorld.containsKey(worldName) ? otgBiomeIdsByWorld.get(worldName) : new BiomeConfig[999999];
return otgBiomeIdsByWorld.containsKey(worldName) ? otgBiomeIdsByWorld.get(worldName) : new ArrayList<BiomeConfig>();
}

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
Expand Down
20 changes: 10 additions & 10 deletions common/src/main/java/com/pg85/otg/network/ServerConfigProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<BiomeConfig> 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;
Expand All @@ -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!");
}
Expand Down Expand Up @@ -685,10 +685,10 @@ private void createAndRegisterBiome(ArrayList<BiomeIdData> 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<BiomeConfig> 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;
Expand Down Expand Up @@ -752,7 +752,7 @@ private void createAndRegisterBiome(ArrayList<BiomeIdData> 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<BiomeConfig> 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+"?");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -526,25 +527,16 @@ else if (JEID) {
{
ArrayList<String> presetNames = new ArrayList<String>();
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())) {
this.mc.displayGuiScreen(new GuiErrorScreen("Error", "Dimension id " + dimConfig.DimensionId + " was taken."));
}
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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> presetNames = new ArrayList<String>();
presetNames.add(dimConfig.PresetName);
if (!OTG.getEngine().areEnoughBiomeIdsAvailableForPresets(presetNames)) {
Expand Down

0 comments on commit 7f0bd57

Please sign in to comment.