Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Incomplete] Friendly Biome IDs #161

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 46 additions & 8 deletions src/main/java/hunternif/mc/atlas/client/BiomeTextureConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import hunternif.mc.atlas.util.AbstractJSONConfig;
import hunternif.mc.atlas.util.CustomFormatter;
import hunternif.mc.atlas.util.Log;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.biome.Biome;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

Expand Down Expand Up @@ -41,8 +44,39 @@ protected void loadData(JsonObject json, BiomeTextureMap data, int version) {
+ " disregarding this config entirely");
return;
}

for (Entry<String, JsonElement> entry : json.entrySet()) {
int biomeID = Integer.parseInt(entry.getKey());
String key = entry.getKey();

if (!(key.length() > 0)) {
Log.warn("No biome ID specified, skipping entry");
return;
}

int biomeID;
Biome biome;

try {
// See if the biome id is an integer
biomeID = Integer.parseInt(key);
biome = Biome.getBiomeForId(biomeID);

if (biome == null) {
Log.warn("Cannot find biome for ID %d", biomeID);
return;
}
} catch(NumberFormatException e) {
// If it is not an integer, attempt to find the biome ID assuming it is a resource location
biome = ForgeRegistries.BIOMES.getValue(new ResourceLocation(key));

if (biome == null) {
Log.warn("Biome ID is invalid, skipping entry with key: %s", key);
return;
}

biomeID = Biome.getIdForBiome(biome);
}

if (entry.getValue().isJsonArray()) {
// List of textures: (this should be gone as of VERSION > 1)
JsonArray array = entry.getValue().getAsJsonArray();
Expand All @@ -52,16 +86,18 @@ protected void loadData(JsonObject json, BiomeTextureMap data, int version) {
textures[i] = new ResourceLocation(path);
}
data.setTexture(biomeID, new TextureSet(null, textures));
Log.info("Registered %d custom texture(s) for biome %d",
textures.length, biomeID);
Log.info("Registered %d custom texture(s) for biome %s",
textures.length, CustomFormatter.getRegistryString(biome));
} else {
// Texture set:
String name = entry.getValue().getAsString();
if (textureSetMap.isRegistered(name)) {
data.setTexture(biomeID, textureSetMap.getByName(name));
Log.info("Registered texture set %s for biome %d", name, biomeID);
Log.info("Registered texture set %s for biome %s",
name, CustomFormatter.getRegistryString(biome));
} else {
Log.warn("Unknown texture set %s for biome %d", name, biomeID);
Log.warn("Unknown texture set %s for biome %s",
name, CustomFormatter.getRegistryString(biome));
}
}
}
Expand All @@ -70,13 +106,15 @@ protected void loadData(JsonObject json, BiomeTextureMap data, int version) {
@Override
protected void saveData(JsonObject json, BiomeTextureMap data) {
// Sort keys (biome IDs) numerically:
Queue<Integer> queue = new PriorityQueue<>(data.textureMap.keySet());
Queue<Biome> queue = new PriorityQueue<>(data.textureMap.keySet());
while (!queue.isEmpty()) {
int biomeID = queue.poll();
Biome biome = queue.poll();
int biomeID = Biome.getIdForBiome(biome);
// Only save biomes 0-256 in this config.
// The rest goes into ExtTileTextureConfig
if (biomeID >= 0 && biomeID < 256) {
json.addProperty(String.valueOf(biomeID), data.textureMap.get(biomeID).name);
String key = CustomFormatter.getRegistryString(biome);
json.addProperty(key, data.textureMap.get(biome).name);
}
}
}
Expand Down
31 changes: 20 additions & 11 deletions src/main/java/hunternif/mc/atlas/client/BiomeTextureMap.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hunternif.mc.atlas.client;

import hunternif.mc.atlas.core.Tile;
import hunternif.mc.atlas.util.CustomFormatter;
import hunternif.mc.atlas.util.Log;
import hunternif.mc.atlas.util.SaveData;
import net.minecraft.util.ResourceLocation;
Expand Down Expand Up @@ -30,30 +31,36 @@ public static BiomeTextureMap instance() {
}

/** This map allows keys other than the 256 biome IDs to use for special tiles. */
final Map<Integer, TextureSet> textureMap = new HashMap<>();
final Map<Biome, TextureSet> textureMap = new HashMap<>();

public static final TextureSet defaultTexture = PLAINS;

/** Assign texture set to biome. */
public void setTexture(int biomeID, TextureSet textureSet) {
Biome biome = Biome.getBiomeForId(biomeID);

if (biome == null) {
return;
}

if (textureSet == null) {
if (textureMap.remove(biomeID) != null) {
Log.warn("Removing old texture for biome %s", biomeID);
if (textureMap.remove(biome) != null) {
Log.warn("Removing old texture for biome %s", CustomFormatter.getRegistryString(biome));
if (biomeID >= 0 && biomeID < 256) {
markDirty();
}
}
return;
}
TextureSet previous = textureMap.put(biomeID, textureSet);
TextureSet previous = textureMap.put(biome, textureSet);
if (biomeID >= 0 && biomeID < 256) {
// The config only concerns itself with biomes 0-256.
// If the old texture set is equal to the new one (i.e. has equal name
// and equal texture files), then there's no need to update the config.
if (previous == null) {
markDirty();
} else if (!previous.equals(textureSet)) {
Log.warn("Overwriting texture set for biome %d", biomeID);
Log.warn("Overwriting texture set for biome %s", CustomFormatter.getRegistryString(biome));
markDirty();
}
}
Expand All @@ -62,12 +69,13 @@ public void setTexture(int biomeID, TextureSet textureSet) {
/** Find the most appropriate standard texture set depending on
* BiomeDictionary types. */
private void autoRegister(int biomeID) {
Biome biome = Biome.getBiomeForId(biomeID);
if (biomeID < 0 || biomeID >= 256) {
Log.warn("Biome ID %d is out of range. Auto-registering default texture set", biomeID);
Log.warn("Biome ID %s is out of range. Auto-registering default texture set",
biome == null ? biomeID : CustomFormatter.getRegistryString(biome));
setTexture(biomeID, defaultTexture);
return;
}
Biome biome = Biome.getBiomeForId(biomeID);
if (biome == null) {
Log.warn("Biome ID %d is null. Auto-registering default texture set", biomeID);
setTexture(biomeID, defaultTexture);
Expand Down Expand Up @@ -205,7 +213,8 @@ else if (types.contains(Type.HILLS)) {
} else {
setTexture(biomeID, defaultTexture);
}
Log.info("Auto-registered standard texture set for biome %d", biomeID);
Log.info("Auto-registered standard texture set for biome %s",
CustomFormatter.getRegistryString(biome));
}

/** Auto-registers the biome ID if it is not registered. */
Expand All @@ -217,14 +226,14 @@ private void checkRegistration(int biomeID) {
}

public boolean isRegistered(int biomeID) {
return textureMap.containsKey(biomeID);
return textureMap.containsKey(Biome.getBiomeForId(biomeID));
}

/** If unknown biome, auto-registers a texture set. If null, returns default set. */
public TextureSet getTextureSet(Tile tile) {
if (tile == null) return defaultTexture;
checkRegistration(tile.biomeID);
return textureMap.get(tile.biomeID);
return textureMap.get(Biome.getBiomeForId(tile.biomeID));
}

public ResourceLocation getTexture(Tile tile) {
Expand All @@ -237,7 +246,7 @@ public ResourceLocation getTexture(Tile tile) {

public List<ResourceLocation> getAllTextures() {
List<ResourceLocation> list = new ArrayList<>(textureMap.size());
for (Entry<Integer, TextureSet> entry : textureMap.entrySet()) {
for (Entry<Biome, TextureSet> entry : textureMap.entrySet()) {
list.addAll(Arrays.asList(entry.getValue().textures));
}
return list;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/hunternif/mc/atlas/util/CustomFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package hunternif.mc.atlas.util;

import java.util.Objects;

public class CustomFormatter {
public static String getRegistryString(net.minecraftforge.registries.IForgeRegistryEntry.Impl<?> entry) {
return Objects.requireNonNull(entry.getRegistryName()).toString();
}
}