Skip to content

Commit

Permalink
More work on Biome config
Browse files Browse the repository at this point in the history
  • Loading branch information
GirafiStudios committed Apr 12, 2018
1 parent 4d1e8b2 commit f506cb8
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 14 deletions.
47 changes: 33 additions & 14 deletions src/main/java/com/girafi/waddles/init/PenguinRegistry.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.girafi.waddles.init;

import com.girafi.waddles.entity.EntityAdeliePenguin;
import com.girafi.waddles.utils.BiomeDictionaryHelper;
import com.girafi.waddles.utils.ConfigurationHandler;
import com.girafi.waddles.utils.Reference;
import com.google.common.base.CaseFormat;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityList;
Expand All @@ -19,6 +21,7 @@
import net.minecraftforge.fml.common.registry.EntityEntryBuilder;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;

Expand All @@ -37,15 +40,31 @@ public class PenguinRegistry {
private static EntityEntry createEntity(Class<? extends Entity> entityClass, int eggPrimary, int eggSecondary, int weight, int min, int max, BiomeDictionary.Type[] typesInclude, BiomeDictionary.Type[] typesExclude) {
List<Biome> spawnable_biomes = Lists.newArrayList();

String subCategoryNames = ConfigurationHandler.CATEGORY_PENGUIN_SPAWNS + Configuration.CATEGORY_SPLITTER + "adelie_penguin" + Configuration.CATEGORY_SPLITTER + "Spawnable Biomes";
String[] include = ConfigurationHandler.config.getStringList("Include", subCategoryNames, toStringArray(typesInclude), "BiomeDictionary types to include");
String[] exclude = ConfigurationHandler.config.getStringList("Exclude", subCategoryNames, toStringArray(typesExclude), "BiomeDictionary types to exclude");

String subCategoryNames = ConfigurationHandler.CATEGORY_PENGUIN_SPAWNS + Configuration.CATEGORY_SPLITTER + classToRegistryName(entityClass).getResourcePath() + Configuration.CATEGORY_SPLITTER + "Spawnable Biomes";
String[] include = ConfigurationHandler.config.getStringList("Include", subCategoryNames, BiomeDictionaryHelper.toStringArray(typesInclude), "BiomeDictionary types to include");
String[] exclude = ConfigurationHandler.config.getStringList("Exclude", subCategoryNames, BiomeDictionaryHelper.toStringArray(typesExclude), "BiomeDictionary types to exclude");
ConfigurationHandler.config.save();

if (include.length == 0) {
throw new IllegalArgumentException("Do not leave the list of biomes to include empty. If you wish to disable spawning of an entity, set the weight to 0 instead.");
} else {
Collection<BiomeDictionary.Type> biomeTypes = BiomeDictionary.Type.getAll();

for (String in : include) {
if (!biomeTypes.contains(BiomeDictionaryHelper.getType(in))) {
throw new IllegalArgumentException("Waddles could not find BiomeDictionary Type '" + in + "' to include, please consult the config file");
}
}
for (String ex : exclude) {
if (!biomeTypes.contains(BiomeDictionaryHelper.getType(ex))) {
throw new IllegalArgumentException("Waddles could not find BiomeDictionary Type '" + ex + "' to exclude, please consult the config file");
}
}
}

for (Biome biome : Biome.REGISTRY) {
Set<BiomeDictionary.Type> types = BiomeDictionary.getTypes(biome);
if (types.containsAll(Arrays.asList(include)) && !types.containsAll(Arrays.asList(exclude)) && !types.contains(NETHER) && !biome.getSpawnableList(EnumCreatureType.CREATURE).isEmpty()) {
if (types.containsAll(Arrays.asList(BiomeDictionaryHelper.toBiomeTypeArray(include))) && !types.containsAll(Arrays.asList(BiomeDictionaryHelper.toBiomeTypeArray(exclude))) && !types.contains(NETHER) && !biome.getSpawnableList(EnumCreatureType.CREATURE).isEmpty()) {
spawnable_biomes.add(biome);
}
}
Expand All @@ -57,14 +76,18 @@ private static EntityEntry createEntity(Class<? extends Entity> entityClass, int
}

private static EntityEntry createEntity(Class<? extends Entity> entityClass, int eggPrimary, int eggSecondary, int weight, int min, int max, Iterable<Biome> biomes) {
ResourceLocation location = new ResourceLocation(Reference.MOD_ID, CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, entityClass.getSimpleName()).replace("entity_", ""));
ResourceLocation location = classToRegistryName(entityClass);
EntityEntry entry = new EntityEntry(entityClass, location.toString());
entry.setRegistryName(location);
entry.setEgg(new EntityList.EntityEggInfo(location, eggPrimary, eggSecondary));
PenguinRegistry.biomes = biomes;
for (Biome biome : biomes) {
System.out.println("Biome: " + biome.getRegistryName());
}
System.out.println("Biomes: " + biomes);
entities.add(entry);

String subCategoryNames = ConfigurationHandler.CATEGORY_PENGUIN_SPAWNS + Configuration.CATEGORY_SPLITTER + entry.getRegistryName().getResourcePath();
String subCategoryNames = ConfigurationHandler.CATEGORY_PENGUIN_SPAWNS + Configuration.CATEGORY_SPLITTER + location.getResourcePath();
PenguinRegistry.weight = ConfigurationHandler.config.get(subCategoryNames, "Weight", weight).getInt();
PenguinRegistry.minCount = ConfigurationHandler.config.get(subCategoryNames, "Min", min).getInt();
PenguinRegistry.maxCount = ConfigurationHandler.config.get(subCategoryNames, "Max", max).getInt();
Expand All @@ -74,19 +97,15 @@ private static EntityEntry createEntity(Class<? extends Entity> entityClass, int
return entry;
}

private static String[] toStringArray(BiomeDictionary.Type[] types) {
String[] def = new String[types.length];
for (int i = 0; i < types.length; i++) {
BiomeDictionary.Type type = types[i];
def[i] = type.getName();
}
return def;
private static ResourceLocation classToRegistryName(Class<? extends Entity> entityClass) {
return new ResourceLocation(Reference.MOD_ID, CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, entityClass.getSimpleName()).replace("entity_", ""));
}

@SubscribeEvent
public static void registerPenguins(RegistryEvent.Register<EntityEntry> event) {
int networkId = 0;
for (EntityEntry entry : entities) {
Preconditions.checkNotNull(entry.getRegistryName(), "registryName");
networkId++;
event.getRegistry().register(EntityEntryBuilder.create()
.entity(entry.getEntityClass())
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/com/girafi/waddles/utils/BiomeDictionaryHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.girafi.waddles.utils;

import net.minecraftforge.common.BiomeDictionary;

import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class BiomeDictionaryHelper {

/**
* Retrieves a #BiomeDictionary.Type
* Based on #BiomeDictionary.Type.getType(), but doesn't create a new #BiomeDictionary.Type if the input is not already a #BiomeDictionary.Type
*
* @param name The name of this #BiomeDictionary.Type
* @return An instance of this #BiomeDictionary.Type
*/
public static BiomeDictionary.Type getType(String name) {
Map<String, BiomeDictionary.Type> byName = BiomeDictionary.Type.getAll().stream().collect(Collectors.toMap(BiomeDictionary.Type::getName, Function.identity()));
name = name.toUpperCase();
return byName.get(name);
}

/**
* Converts a #BiomeDictionary.Type array to a String array
* Useful for config options
*
* @param types array of #BiomeDictionary.Type
* @return String array based on the input
*/
public static String[] toStringArray(BiomeDictionary.Type[] types) {
String[] strings = new String[types.length];
for (int i = 0; i < types.length; i++) {
BiomeDictionary.Type type = types[i];
strings[i] = type.getName();
}
return strings;
}

/**
* Converts a String array to a #BiomeDictionary.Type array
*
* @param strings string array containing valid #BiomeDictionary.Types
* @return #BiomeDictionary.Types based on the string input
*/
public static BiomeDictionary.Type[] toBiomeTypeArray(String[] strings) {
BiomeDictionary.Type[] types = new BiomeDictionary.Type[strings.length];
for (int i = 0; i < strings.length; i++) {
String string = strings[i];
types[i] = getType(string);
}
return types;
}
}

0 comments on commit f506cb8

Please sign in to comment.