Skip to content

Commit

Permalink
Make tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Warriorrrr committed Sep 8, 2024
1 parent ccb53d5 commit ddb8366
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 85 deletions.
35 changes: 25 additions & 10 deletions Towny/src/main/java/com/palmergames/bukkit/towny/TownySettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -63,6 +64,7 @@
import java.util.TreeMap;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class TownySettings {
Expand Down Expand Up @@ -507,7 +509,7 @@ private static void loadSwitchAndItemUseMaterialsLists() {
}

public static Set<EntityType> toEntityTypeSet(final List<String> entityList) {
final Set<EntityType> entities = new HashSet<>();
final Set<EntityType> entities = new LinkedHashSet<>();

for (final String entityName : entityList) {
if (EntityLists.hasGroup(entityName)) {
Expand Down Expand Up @@ -548,7 +550,7 @@ public static Set<EntityType> toEntityTypeSet(final List<String> entityList) {
}

public static Collection<Material> toMaterialSet(List<String> materialList) {
Set<Material> materials = new HashSet<>();
Set<Material> materials = new LinkedHashSet<>();

for (String materialName : materialList) {
if (materialName.isEmpty())
Expand Down Expand Up @@ -1101,7 +1103,7 @@ private static void loadCustomRegistryLists() {
for (Map.Entry<String, String> entry : getMap(ConfigNodes.CUSTOM_LISTS_ITEM_LISTS).entrySet())
ItemLists.addGroup(entry.getKey(), constructRegistryList(ItemLists.newBuilder(), Tag.REGISTRY_BLOCKS, Arrays.asList(entry.getValue().split(",")), mat -> mat.data));

EntityLists.clearGroups();
EntityLists.clearCustomGroups();
for (Map.Entry<String, String> entry : getMap(ConfigNodes.CUSTOM_LISTS_ENTITY_LISTS).entrySet())
EntityLists.addGroup(entry.getKey(), constructRegistryList(EntityLists.newBuilder(), Tag.REGISTRY_ENTITY_TYPES, Arrays.asList(entry.getValue().split(",")), EntityType::getEntityClass));
}
Expand Down Expand Up @@ -1131,16 +1133,24 @@ else if (element.startsWith("~"))
builder.contains(element.substring(1));
else if (element.startsWith("!~"))
builder.notContains(element.substring(2));
else if (element.startsWith("c:")) {
final String className = element.substring(2);
else if (element.startsWith("c:") || element.startsWith("!c:")) {
int substr = 2;
boolean add = true;

if (element.startsWith("!")) {
substr++;
add = false;
}

final String className = element.substring(substr);
boolean classFound = false;

// Check certain packages so that fully qualified names aren't required, i.e. 'Animals' will work.
for (String packageName : REGISTRY_LIST_PACKAGES)
classFound |= checkClass(builder, classExtractor, packageName + "." + className);
classFound |= checkClass(builder, classExtractor, packageName + "." + className, add);

// Check exact class, since this could be a fully qualified name already.
classFound |= checkClass(builder, classExtractor, className);
classFound |= checkClass(builder, classExtractor, className, add);

// The user's class name might be wrong/wrongly cased, let them know about it.
if (!classFound)
Expand All @@ -1152,18 +1162,23 @@ else if (element.startsWith("c:")) {
return builder.build();
}

private static <T extends Keyed, F extends AbstractRegistryList<T>> boolean checkClass(AbstractRegistryList.Builder<T, F> builder, Function<T, Class<?>> classExtractor, String className) {
private static <T extends Keyed, F extends AbstractRegistryList<T>> boolean checkClass(AbstractRegistryList.Builder<T, F> builder, Function<T, Class<?>> classExtractor, String className, boolean add) {
final Class<?> desired;
try {
desired = Class.forName(className);
} catch (ClassNotFoundException e) {
return false;
}

builder.filter(t -> {
Predicate<T> predicate = t -> {
final Class<?> clazz = classExtractor.apply(t);
return clazz != null && desired.isAssignableFrom(clazz);
});
};

if (add)
builder.addIf(predicate);
else
builder.removeIf(predicate);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2360,7 +2360,7 @@ public boolean saveWorld(TownyWorld world) {

// Unclaimed Zone Ignore Ids
if (world.getUnclaimedZoneIgnoreMaterials() != null)
list.add("unclaimedZoneIgnoreIds=" + StringMgmt.join(world.getUnclaimedZoneIgnoreMaterials(), ","));
list.add("unclaimedZoneIgnoreIds=" + StringMgmt.join(BukkitTools.toMinimalMaterialCollection(world.getUnclaimedZoneIgnoreMaterials()), ","));

// PlotManagement Delete
list.add("");
Expand All @@ -2369,15 +2369,15 @@ public boolean saveWorld(TownyWorld world) {
list.add("usingPlotManagementDelete=" + world.isUsingPlotManagementDelete());
// Plot Management Delete Ids
if (world.getPlotManagementDeleteIds() != null)
list.add("plotManagementDeleteIds=" + StringMgmt.join(world.getPlotManagementDeleteIds(), ","));
list.add("plotManagementDeleteIds=" + StringMgmt.join(BukkitTools.toMinimalMaterialCollection(world.getPlotManagementDeleteIds()), ","));

// EntityType removal on unclaim.
list.add("");
list.add("# The following settings control what EntityTypes are deleted upon a townblock being unclaimed");
list.add("# Valid EntityTypes are listed here: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/entity/EntityType.html");
list.add("isDeletingEntitiesOnUnclaim=" + world.isDeletingEntitiesOnUnclaim());
if (world.getUnclaimDeleteEntityTypes() != null)
list.add("unclaimDeleteEntityTypes=" + StringMgmt.join(BukkitTools.convertKeyedToString(world.getUnclaimDeleteEntityTypes()), ","));
list.add("unclaimDeleteEntityTypes=" + StringMgmt.join(BukkitTools.toMinimalEntityCollection(world.getUnclaimDeleteEntityTypes()), ","));

// PlotManagement
list.add("");
Expand All @@ -2398,10 +2398,10 @@ public boolean saveWorld(TownyWorld world) {
list.add("# Any block Id's listed here will not be respawned. Instead it will revert to air. This list also world on the WildRegen settings below.");
// Plot Management Ignore Ids
if (world.getPlotManagementIgnoreIds() != null)
list.add("plotManagementIgnoreIds=" + StringMgmt.join(world.getPlotManagementIgnoreIds(), ","));
list.add("plotManagementIgnoreIds=" + StringMgmt.join(BukkitTools.toMinimalMaterialCollection(world.getPlotManagementIgnoreIds()), ","));
// Revert on Unclaim whitelisted Materials.
if (world.getRevertOnUnclaimWhitelistMaterials() != null)
list.add("revertOnUnclaimWhitelistMaterials=" + StringMgmt.join(world.getRevertOnUnclaimWhitelistMaterials(), "#"));
list.add("revertOnUnclaimWhitelistMaterials=" + StringMgmt.join(BukkitTools.toMinimalMaterialCollection(world.getRevertOnUnclaimWhitelistMaterials()), "#"));

// PlotManagement Wild Regen
list.add("");
Expand All @@ -2413,7 +2413,7 @@ public boolean saveWorld(TownyWorld world) {
list.add("# The list of entities whose explosions would be reverted.");
// Wilderness Explosion Protection entities
if (world.getPlotManagementWildRevertEntities() != null)
list.add("PlotManagementWildRegenEntities=" + StringMgmt.join(BukkitTools.convertKeyedToString(world.getPlotManagementWildRevertEntities()), ","));
list.add("PlotManagementWildRegenEntities=" + StringMgmt.join(BukkitTools.toMinimalEntityCollection(world.getPlotManagementWildRevertEntities()), ","));

list.add("# If enabled any damage caused by block explosions will repair itself.");
// Using PlotManagement Wild Block Regen
Expand All @@ -2422,17 +2422,17 @@ public boolean saveWorld(TownyWorld world) {
list.add("# The list of blocks whose explosions would be reverted.");
// Wilderness Explosion Protection blocks
if (world.getPlotManagementWildRevertBlocks() != null)
list.add("PlotManagementWildRegenBlocks=" + StringMgmt.join(world.getPlotManagementWildRevertBlocks(), ","));
list.add("PlotManagementWildRegenBlocks=" + StringMgmt.join(BukkitTools.toMinimalMaterialCollection(world.getPlotManagementWildRevertBlocks()), ","));

list.add("# The list of blocks to regenerate. (if empty all blocks will regenerate)");
// Wilderness Explosion Protection entities
if (world.getPlotManagementWildRevertBlockWhitelist() != null)
list.add("PlotManagementWildRegenBlockWhitelist=" + StringMgmt.join(world.getPlotManagementWildRevertBlockWhitelist(), ","));
list.add("PlotManagementWildRegenBlockWhitelist=" + StringMgmt.join(BukkitTools.toMinimalMaterialCollection(world.getPlotManagementWildRevertBlockWhitelist()), ","));

list.add("# The list of blocks to that should not get replaced when an explosion is reverted in the wilderness, ie: a chest placed in a creeper hole that is reverting.");
// Wilderness Explosion materials to not overwrite.
if (world.getWildRevertMaterialsToNotOverwrite() != null)
list.add("wildRegenBlocksToNotOverwrite=" + StringMgmt.join(world.getWildRevertMaterialsToNotOverwrite(), ","));
list.add("wildRegenBlocksToNotOverwrite=" + StringMgmt.join(BukkitTools.toMinimalMaterialCollection(world.getWildRevertMaterialsToNotOverwrite()), ","));

list.add("# The delay after which the explosion reverts will begin.");
// Using PlotManagement Wild Regen Delay
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2587,47 +2587,47 @@ public synchronized boolean saveWorld(TownyWorld world) {
// Deleting EntityTypes from Townblocks on Unclaim.
nat_hm.put("isDeletingEntitiesOnUnclaim", world.isDeletingEntitiesOnUnclaim());
if (world.getUnclaimDeleteEntityTypes() != null)
nat_hm.put("unclaimDeleteEntityTypes", StringMgmt.join(BukkitTools.convertKeyedToString(world.getUnclaimDeleteEntityTypes()), "#"));
nat_hm.put("unclaimDeleteEntityTypes", StringMgmt.join(BukkitTools.toMinimalEntityCollection(world.getUnclaimDeleteEntityTypes()), "#"));

// Using PlotManagement Delete
nat_hm.put("usingPlotManagementDelete", world.isUsingPlotManagementDelete());
// Plot Management Delete Ids
if (world.getPlotManagementDeleteIds() != null)
nat_hm.put("plotManagementDeleteIds", StringMgmt.join(world.getPlotManagementDeleteIds(), "#"));
nat_hm.put("plotManagementDeleteIds", StringMgmt.join(BukkitTools.toMinimalMaterialCollection(world.getPlotManagementDeleteIds()), "#"));

// Using PlotManagement Mayor Delete
nat_hm.put("usingPlotManagementMayorDelete", world.isUsingPlotManagementMayorDelete());
// Plot Management Mayor Delete
if (world.getPlotManagementMayorDelete() != null)
nat_hm.put("plotManagementMayorDelete", StringMgmt.join(world.getPlotManagementMayorDelete(), "#"));
nat_hm.put("plotManagementMayorDelete", StringMgmt.join(BukkitTools.toMinimalMaterialCollection(world.getPlotManagementMayorDelete()), "#"));

// Using PlotManagement Revert
nat_hm.put("usingPlotManagementRevert", world.isUsingPlotManagementRevert());

// Plot Management Ignore Ids
if (world.getPlotManagementIgnoreIds() != null)
nat_hm.put("plotManagementIgnoreIds", StringMgmt.join(world.getPlotManagementIgnoreIds(), "#"));
nat_hm.put("plotManagementIgnoreIds", StringMgmt.join(BukkitTools.toMinimalMaterialCollection(world.getPlotManagementIgnoreIds()), "#"));

// Revert on Unclaim whitelisted materials
if (world.getRevertOnUnclaimWhitelistMaterials() != null)
nat_hm.put("revertOnUnclaimWhitelistMaterials", StringMgmt.join(world.getRevertOnUnclaimWhitelistMaterials(), "#"));
nat_hm.put("revertOnUnclaimWhitelistMaterials", StringMgmt.join(BukkitTools.toMinimalMaterialCollection(world.getRevertOnUnclaimWhitelistMaterials()), "#"));

// Using PlotManagement Wild Regen
nat_hm.put("usingPlotManagementWildRegen", world.isUsingPlotManagementWildEntityRevert());

// Wilderness Explosion Protection entities
if (world.getPlotManagementWildRevertEntities() != null)
nat_hm.put("PlotManagementWildRegenEntities", StringMgmt.join(BukkitTools.convertKeyedToString(world.getPlotManagementWildRevertEntities()), "#"));
nat_hm.put("PlotManagementWildRegenEntities", StringMgmt.join(BukkitTools.toMinimalEntityCollection(world.getPlotManagementWildRevertEntities()), "#"));

// Wilderness Explosion Protection Block Whitelist
if (world.getPlotManagementWildRevertBlockWhitelist() != null)
nat_hm.put("PlotManagementWildRegenBlockWhitelist",
StringMgmt.join(world.getPlotManagementWildRevertBlockWhitelist(), "#"));
StringMgmt.join(BukkitTools.toMinimalMaterialCollection(world.getPlotManagementWildRevertBlockWhitelist()), "#"));

// Wilderness Explosion Protection Materials to not overwrite.
if (world.getWildRevertMaterialsToNotOverwrite() != null)
nat_hm.put("wildRegenBlocksToNotOverwrite",
StringMgmt.join(world.getWildRevertMaterialsToNotOverwrite(), "#"));
StringMgmt.join(BukkitTools.toMinimalMaterialCollection(world.getWildRevertMaterialsToNotOverwrite()), "#"));

// Using PlotManagement Wild Regen Delay
nat_hm.put("plotManagementWildRegenSpeed", world.getPlotManagementWildRevertDelay());
Expand Down
Loading

0 comments on commit ddb8366

Please sign in to comment.