store only biome IDs not instances
reorganized utility methods
This commit is contained in:
@@ -8,7 +8,7 @@ import mods.betterfoliage.client.BlockMatcher;
|
||||
import mods.betterfoliage.client.gui.AlternateTextBooleanEntry;
|
||||
import mods.betterfoliage.client.gui.BiomeListConfigEntry;
|
||||
import mods.betterfoliage.client.gui.NonVerboseArrayEntry;
|
||||
import mods.betterfoliage.common.util.Utils;
|
||||
import mods.betterfoliage.common.util.BiomeUtils;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.world.biome.BiomeGenBase;
|
||||
@@ -17,9 +17,6 @@ import net.minecraftforge.common.config.ConfigElement;
|
||||
import net.minecraftforge.common.config.Configuration;
|
||||
import net.minecraftforge.common.config.Property;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Collections2;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import cpw.mods.fml.client.config.IConfigElement;
|
||||
@@ -190,32 +187,10 @@ public class Config {
|
||||
}
|
||||
|
||||
public static void getDefaultBiomes() {
|
||||
for (BiomeGenBase biome : BiomeGenBase.getBiomeGenArray()) {
|
||||
if (biome == null) continue;
|
||||
if (Utils.biomeTempRainFilter(0.4f, null, 0.4f, null).apply(biome)) {
|
||||
reedBiomeList.add(biome.biomeID);
|
||||
BiomeListConfigEntry.reedBiomeList.add(biome);
|
||||
}
|
||||
if (Utils.biomeClassNameFilter("river", "ocean").apply(biome)) {
|
||||
algaeBiomeList.add(biome.biomeID);
|
||||
BiomeListConfigEntry.algaeBiomeList.add(biome);
|
||||
}
|
||||
if (Utils.biomeClassNameFilter("river", "ocean", "beach").apply(biome)) {
|
||||
coralBiomeList.add(biome.biomeID);
|
||||
BiomeListConfigEntry.coralBiomeList.add(biome);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static List<Integer> getFilteredBiomeIds(List<BiomeGenBase> biomes, Predicate<BiomeGenBase> filter) {
|
||||
return Lists.newArrayList(Collections2.transform(
|
||||
Collections2.filter(biomes, filter),
|
||||
new Function<BiomeGenBase, Integer>() {
|
||||
public Integer apply(BiomeGenBase input) {
|
||||
return input.biomeID;
|
||||
}
|
||||
}
|
||||
));
|
||||
List<BiomeGenBase> biomes = BiomeUtils.getAllBiomes();
|
||||
reedBiomeList = BiomeUtils.getFilteredBiomeIds(biomes, BiomeUtils.biomeTempRainFilter(0.4f, null, 0.4f, null));
|
||||
algaeBiomeList = BiomeUtils.getFilteredBiomeIds(biomes, BiomeUtils.biomeClassNameFilter("river", "ocean"));
|
||||
algaeBiomeList = BiomeUtils.getFilteredBiomeIds(biomes, BiomeUtils.biomeClassNameFilter("river", "ocean", "beach"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
|
||||
87
src/main/java/mods/betterfoliage/common/util/BiomeUtils.java
Normal file
87
src/main/java/mods/betterfoliage/common/util/BiomeUtils.java
Normal file
@@ -0,0 +1,87 @@
|
||||
package mods.betterfoliage.common.util;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.world.biome.BiomeGenBase;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Predicates;
|
||||
import com.google.common.collect.Collections2;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
|
||||
public class BiomeUtils {
|
||||
|
||||
/** Hide constructor */
|
||||
private BiomeUtils() {}
|
||||
|
||||
public static List<BiomeGenBase> getAllBiomes() {
|
||||
List<BiomeGenBase> biomes = Lists.newArrayList(Collections2.filter(Arrays.asList(BiomeGenBase.getBiomeGenArray()), Predicates.notNull()));
|
||||
Collections.sort(biomes, new Comparator<BiomeGenBase>() {
|
||||
@Override
|
||||
public int compare(BiomeGenBase o1, BiomeGenBase o2) {
|
||||
return o1.biomeName.compareTo(o2.biomeName);
|
||||
}
|
||||
});
|
||||
return biomes;
|
||||
}
|
||||
|
||||
public static Predicate<BiomeGenBase> biomeIdFilter(final List<Integer> biomeIdList) {
|
||||
return new Predicate<BiomeGenBase>() {
|
||||
public boolean apply(BiomeGenBase biome) {
|
||||
return biomeIdList.contains(biome.biomeID);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static Function<BiomeGenBase, Integer> biomeIdTransform() {
|
||||
return new Function<BiomeGenBase, Integer>() {
|
||||
public Integer apply(BiomeGenBase input) {
|
||||
return input.biomeID;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static Predicate<BiomeGenBase> biomeTempRainFilter(final Float minTemp, final Float maxTemp, final Float minRain, final Float maxRain) {
|
||||
return new Predicate<BiomeGenBase>() {
|
||||
public boolean apply(BiomeGenBase biome) {
|
||||
if (minTemp != null && biome.temperature < minTemp) return false;
|
||||
if (maxTemp != null && biome.temperature > maxTemp) return false;
|
||||
if (minRain != null && biome.rainfall < minRain) return false;
|
||||
if (maxRain != null && biome.rainfall > maxRain) return false;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static Predicate<BiomeGenBase> biomeClassFilter(final Class<?>... classList) {
|
||||
return new Predicate<BiomeGenBase>() {
|
||||
public boolean apply(BiomeGenBase biome) {
|
||||
for (Class<?> clazz : classList)
|
||||
if (clazz.isAssignableFrom(biome.getClass()) || clazz.equals(biome.getClass()))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static Predicate<BiomeGenBase> biomeClassNameFilter(final String... names) {
|
||||
return new Predicate<BiomeGenBase>() {
|
||||
public boolean apply(BiomeGenBase biome) {
|
||||
for (String name : names) if (biome.getClass().getName().toLowerCase().contains(name.toLowerCase())) return true;
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static List<Integer> getFilteredBiomeIds(Collection<BiomeGenBase> biomes, Predicate<BiomeGenBase> filter) {
|
||||
return Lists.newArrayList(Collections2.transform(Collections2.filter(biomes, filter), biomeIdTransform()));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package mods.betterfoliage.common.util;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.minecraft.util.EnumChatFormatting;
|
||||
import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
|
||||
import cpw.mods.fml.client.registry.RenderingRegistry;
|
||||
|
||||
|
||||
public class RenderUtils {
|
||||
|
||||
/** Hide constructor */
|
||||
private RenderUtils() {}
|
||||
|
||||
/** Retrieve a specific rendering handler from the registry
|
||||
* @param renderType render type of block
|
||||
* @return {@link ISimpleBlockRenderingHandler} if defined, null otherwise
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static ISimpleBlockRenderingHandler getRenderingHandler(int renderType) {
|
||||
try {
|
||||
Field field = RenderingRegistry.class.getDeclaredField("INSTANCE");
|
||||
field.setAccessible(true);
|
||||
RenderingRegistry inst = (RenderingRegistry) field.get(null);
|
||||
field = RenderingRegistry.class.getDeclaredField("blockRenderers");
|
||||
field.setAccessible(true);
|
||||
return ((Map<Integer, ISimpleBlockRenderingHandler>) field.get(inst)).get(renderType);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void stripTooltipDefaultText(List<String> tooltip) {
|
||||
boolean defaultRows = false;
|
||||
Iterator<String> iter = tooltip.iterator();
|
||||
while(iter.hasNext()) {
|
||||
if (iter.next().startsWith(EnumChatFormatting.AQUA.toString())) defaultRows = true;
|
||||
if (defaultRows) iter.remove();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package mods.betterfoliage.common.util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Map;
|
||||
|
||||
import mods.betterfoliage.loader.DeobfHelper;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.resources.IResource;
|
||||
import net.minecraft.client.resources.IResourceManager;
|
||||
import net.minecraft.client.resources.SimpleReloadableResourceManager;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
|
||||
import cpw.mods.fml.relauncher.ReflectionHelper;
|
||||
import cpw.mods.fml.relauncher.ReflectionHelper.UnableToAccessFieldException;
|
||||
|
||||
public class ResourceUtils {
|
||||
|
||||
/** Hide constructor */
|
||||
private ResourceUtils() {}
|
||||
|
||||
/**
|
||||
* @return (({@link SimpleReloadableResourceManager}) Minecraft.getMinecraft().getResourceManager()).domainResourceManagers
|
||||
*/
|
||||
public static Map<String, IResourceManager> getDomainResourceManagers() {
|
||||
try {
|
||||
return ReflectionHelper.<Map<String, IResourceManager>, SimpleReloadableResourceManager> getPrivateValue(
|
||||
SimpleReloadableResourceManager.class, (SimpleReloadableResourceManager) Minecraft.getMinecraft().getResourceManager(), DeobfHelper.transformElementSearge("domainResourceManagers"), "domainResourceManagers"
|
||||
);
|
||||
} catch (UnableToAccessFieldException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Check for the existence of a {@link IResource}
|
||||
* @param resourceLocation
|
||||
* @return true if the resource exists
|
||||
*/
|
||||
public static boolean resourceExists(ResourceLocation resourceLocation) {
|
||||
try {
|
||||
IResource resource = Minecraft.getMinecraft().getResourceManager().getResource(resourceLocation);
|
||||
if (resource != null) return true;
|
||||
} catch (IOException e) {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Copy a text file from a resource to the filesystem
|
||||
* @param resourceLocation resource location of text file
|
||||
* @param target target file
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void copyFromTextResource(ResourceLocation resourceLocation, File target) throws IOException {
|
||||
IResource defaults = Minecraft.getMinecraft().getResourceManager().getResource(resourceLocation);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(defaults.getInputStream(), Charsets.UTF_8));
|
||||
FileWriter writer = new FileWriter(target);
|
||||
|
||||
String line = reader.readLine();
|
||||
while(line != null) {
|
||||
writer.write(line + System.lineSeparator());
|
||||
line = reader.readLine();
|
||||
}
|
||||
|
||||
reader.close();
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
@@ -1,157 +0,0 @@
|
||||
package mods.betterfoliage.common.util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import mods.betterfoliage.loader.DeobfHelper;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.resources.IResource;
|
||||
import net.minecraft.client.resources.IResourceManager;
|
||||
import net.minecraft.client.resources.SimpleReloadableResourceManager;
|
||||
import net.minecraft.util.EnumChatFormatting;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.world.biome.BiomeGenBase;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.base.Predicate;
|
||||
|
||||
import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
|
||||
import cpw.mods.fml.client.registry.RenderingRegistry;
|
||||
|
||||
public class Utils {
|
||||
|
||||
/** Hide constructor */
|
||||
private Utils() {}
|
||||
|
||||
/**
|
||||
* @return (({@link SimpleReloadableResourceManager}) Minecraft.getMinecraft().getResourceManager()).domainResourceManagers
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Map<String, IResourceManager> getDomainResourceManagers() {
|
||||
IResourceManager manager = Minecraft.getMinecraft().getResourceManager();
|
||||
Map<String, IResourceManager> result = getField(manager, DeobfHelper.transformElementSearge("domainResourceManagers"), Map.class);
|
||||
if (result == null) result = getField(manager, "domainResourceManagers", Map.class);
|
||||
return result;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T getField(Object target, String fieldName, Class<T> resultClass) {
|
||||
try {
|
||||
Field field = target.getClass().getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
return (T) field.get(target);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T getStaticField(Class<?> clazz, String fieldName, Class<T> resultClass) {
|
||||
try {
|
||||
Field field = clazz.getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
return (T) field.get(null);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Retrieve a specific rendering handler from the registry
|
||||
* @param renderType render type of block
|
||||
* @return {@link ISimpleBlockRenderingHandler} if defined, null otherwise
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static ISimpleBlockRenderingHandler getRenderingHandler(int renderType) {
|
||||
try {
|
||||
Field field = RenderingRegistry.class.getDeclaredField("INSTANCE");
|
||||
field.setAccessible(true);
|
||||
RenderingRegistry inst = (RenderingRegistry) field.get(null);
|
||||
field = RenderingRegistry.class.getDeclaredField("blockRenderers");
|
||||
field.setAccessible(true);
|
||||
return ((Map<Integer, ISimpleBlockRenderingHandler>) field.get(inst)).get(renderType);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Check for the existence of a {@link IResource}
|
||||
* @param resourceLocation
|
||||
* @return true if the resource exists
|
||||
*/
|
||||
public static boolean resourceExists(ResourceLocation resourceLocation) {
|
||||
try {
|
||||
IResource resource = Minecraft.getMinecraft().getResourceManager().getResource(resourceLocation);
|
||||
if (resource != null) return true;
|
||||
} catch (IOException e) {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Copy a text file from a resource to the filesystem
|
||||
* @param resourceLocation resource location of text file
|
||||
* @param target target file
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void copyFromTextResource(ResourceLocation resourceLocation, File target) throws IOException {
|
||||
IResource defaults = Minecraft.getMinecraft().getResourceManager().getResource(resourceLocation);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(defaults.getInputStream(), Charsets.UTF_8));
|
||||
FileWriter writer = new FileWriter(target);
|
||||
|
||||
String line = reader.readLine();
|
||||
while(line != null) {
|
||||
writer.write(line + System.lineSeparator());
|
||||
line = reader.readLine();
|
||||
}
|
||||
|
||||
reader.close();
|
||||
writer.close();
|
||||
}
|
||||
|
||||
public static void stripTooltipDefaultText(List<String> tooltip) {
|
||||
boolean defaultRows = false;
|
||||
Iterator<String> iter = tooltip.iterator();
|
||||
while(iter.hasNext()) {
|
||||
if (iter.next().startsWith(EnumChatFormatting.AQUA.toString())) defaultRows = true;
|
||||
if (defaultRows) iter.remove();
|
||||
}
|
||||
}
|
||||
|
||||
public static Predicate<BiomeGenBase> biomeTempRainFilter(final Float minTemp, final Float maxTemp, final Float minRain, final Float maxRain) {
|
||||
return new Predicate<BiomeGenBase>() {
|
||||
public boolean apply(BiomeGenBase biome) {
|
||||
if (minTemp != null && biome.temperature < minTemp) return false;
|
||||
if (maxTemp != null && biome.temperature > maxTemp) return false;
|
||||
if (minRain != null && biome.rainfall < minRain) return false;
|
||||
if (maxRain != null && biome.rainfall > maxRain) return false;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static Predicate<BiomeGenBase> biomeClassFilter(final Class<?>... classList) {
|
||||
return new Predicate<BiomeGenBase>() {
|
||||
public boolean apply(BiomeGenBase biome) {
|
||||
for (Class<?> clazz : classList)
|
||||
if (clazz.isAssignableFrom(biome.getClass()) || clazz.equals(biome.getClass()))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static Predicate<BiomeGenBase> biomeClassNameFilter(final String... names) {
|
||||
return new Predicate<BiomeGenBase>() {
|
||||
public boolean apply(BiomeGenBase biome) {
|
||||
for (String name : names) if (biome.getClass().getName().toLowerCase().contains(name.toLowerCase())) return true;
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user