From 59fad9b13699ef8e4d48aed7a71d90ee55bc3193 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Fri, 24 Mar 2017 15:53:20 +0100 Subject: [PATCH] Add Migrator for EnumerationUtil addition. --- src/com/massivecraft/factions/Factions.java | 3 + .../massivecraft/factions/entity/MConf.java | 6 ++ .../MigratorMConf001EnumerationUtil.java | 67 +++++++++++++++++++ .../factions/util/EnumerationUtil.java | 27 ++++---- 4 files changed, 89 insertions(+), 14 deletions(-) create mode 100644 src/com/massivecraft/factions/entity/migrator/MigratorMConf001EnumerationUtil.java diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index c7cb31fa..efb84127 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -49,6 +49,7 @@ import com.massivecraft.factions.entity.MConfColl; import com.massivecraft.factions.entity.MFlagColl; import com.massivecraft.factions.entity.MPermColl; import com.massivecraft.factions.entity.MPlayerColl; +import com.massivecraft.factions.entity.migrator.MigratorMConf001EnumerationUtil; import com.massivecraft.factions.event.EventFactionsChunkChangeType; import com.massivecraft.factions.integration.V19.IntegrationV19; import com.massivecraft.factions.integration.herochat.IntegrationHerochat; @@ -120,6 +121,8 @@ public class Factions extends MassivePlugin // Initialize Database this.databaseInitialized = false; + MigratorMConf001EnumerationUtil.get().setActive(true); + MFlagColl.get().setActive(true); MPermColl.get().setActive(true); MConfColl.get().setActive(true); diff --git a/src/com/massivecraft/factions/entity/MConf.java b/src/com/massivecraft/factions/entity/MConf.java index ba08e39f..36466f64 100644 --- a/src/com/massivecraft/factions/entity/MConf.java +++ b/src/com/massivecraft/factions/entity/MConf.java @@ -52,6 +52,12 @@ public class MConf extends Entity return this; } + // -------------------------------------------- // + // VERSION + // -------------------------------------------- // + + public int version = 1; + // -------------------------------------------- // // COMMAND ALIASES // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/entity/migrator/MigratorMConf001EnumerationUtil.java b/src/com/massivecraft/factions/entity/migrator/MigratorMConf001EnumerationUtil.java new file mode 100644 index 00000000..56ff7fd6 --- /dev/null +++ b/src/com/massivecraft/factions/entity/migrator/MigratorMConf001EnumerationUtil.java @@ -0,0 +1,67 @@ +package com.massivecraft.factions.entity.migrator; + +import com.massivecraft.factions.entity.MConf; +import com.massivecraft.factions.util.EnumerationUtil; +import com.massivecraft.massivecore.store.migration.VersionMigratorRoot; +import com.massivecraft.massivecore.xlib.gson.JsonArray; +import com.massivecraft.massivecore.xlib.gson.JsonElement; +import com.massivecraft.massivecore.xlib.gson.JsonObject; +import com.massivecraft.massivecore.xlib.gson.JsonPrimitive; + +import java.util.Collection; +import java.util.Iterator; + +public class MigratorMConf001EnumerationUtil extends VersionMigratorRoot +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static MigratorMConf001EnumerationUtil i = new MigratorMConf001EnumerationUtil(); + public static MigratorMConf001EnumerationUtil get() { return i; } + private MigratorMConf001EnumerationUtil() + { + super(MConf.class); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void migrateInner(JsonObject entity) + { + removeFromStringsField(entity, "materialsEditOnInteract", EnumerationUtil.MATERIALS_EDIT_ON_INTERACT.getStringSet()); + removeFromStringsField(entity, "materialsEditTools", EnumerationUtil.MATERIALS_EDIT_TOOL.getStringSet()); + removeFromStringsField(entity, "materialsDoor", EnumerationUtil.MATERIALS_DOOR.getStringSet()); + removeFromStringsField(entity, "materialsContainer", EnumerationUtil.MATERIALS_CONTAINER.getStringSet()); + removeFromStringsField(entity, "entityTypesEditOnInteract", EnumerationUtil.ENTITY_TYPES_EDIT_ON_INTERACT.getStringSet()); + removeFromStringsField(entity, "entityTypesEditOnDamage", EnumerationUtil.ENTITY_TYPES_EDIT_ON_DAMAGE.getStringSet()); + removeFromStringsField(entity, "entityTypesContainer", EnumerationUtil.ENTITY_TYPES_CONTAINER.getStringSet()); + removeFromStringsField(entity, "entityTypesMonsters", EnumerationUtil.ENTITY_TYPES_MONSTER.getStringSet()); + removeFromStringsField(entity, "entityTypesAnimals", EnumerationUtil.ENTITY_TYPES_ANIMAL.getStringSet()); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + private void removeFromStringsField(JsonObject entity, String fieldName, Collection removals) + { + JsonElement stringsElement = entity.get(fieldName); + if (!(stringsElement instanceof JsonArray)) return; + JsonArray strings = (JsonArray)stringsElement; + + for (Iterator iterator = strings.iterator(); iterator.hasNext();) + { + JsonElement stringElement = iterator.next(); + if (!(stringElement instanceof JsonPrimitive)) continue; + JsonPrimitive string = (JsonPrimitive)stringElement; + + if (!removals.contains(string.getAsString())) continue; + + iterator.remove(); + } + } + +} diff --git a/src/com/massivecraft/factions/util/EnumerationUtil.java b/src/com/massivecraft/factions/util/EnumerationUtil.java index 8c70bcec..b8c4eafb 100644 --- a/src/com/massivecraft/factions/util/EnumerationUtil.java +++ b/src/com/massivecraft/factions/util/EnumerationUtil.java @@ -11,7 +11,7 @@ public class EnumerationUtil // MATERIAL EDIT ON INTERACT // -------------------------------------------- // - private static final BackstringSet MATERIALS_EDIT_ON_INTERACT = new BackstringSet<>(Material.class, + public static final BackstringSet MATERIALS_EDIT_ON_INTERACT = new BackstringSet<>(Material.class, "DIODE_BLOCK_OFF", // Minecraft 1.? "DIODE_BLOCK_ON", // Minecraft 1.? "NOTE_BLOCK", // Minecraft 1.? @@ -32,7 +32,7 @@ public class EnumerationUtil // MATERIAL EDIT TOOLS // -------------------------------------------- // - private static final BackstringSet MATERIALS_EDIT_TOOLS = new BackstringSet<>(Material.class, + public static final BackstringSet MATERIALS_EDIT_TOOL = new BackstringSet<>(Material.class, "FIREBALL", // Minecraft 1.? "FLINT_AND_STEEL", // Minecraft 1.? "BUCKET", // Minecraft 1.? @@ -53,7 +53,7 @@ public class EnumerationUtil public static boolean isMaterialEditTool(Material material) { - return MATERIALS_EDIT_TOOLS.contains(material) || MConf.get().materialsEditTools.contains(material); + return MATERIALS_EDIT_TOOL.contains(material) || MConf.get().materialsEditTools.contains(material); } // -------------------------------------------- // @@ -61,7 +61,7 @@ public class EnumerationUtil // -------------------------------------------- // // Interacting with these materials placed in the terrain results in door toggling. - private static final BackstringSet MATERIALS_DOOR = new BackstringSet<>(Material.class, + public static final BackstringSet MATERIALS_DOOR = new BackstringSet<>(Material.class, "WOODEN_DOOR", // Minecraft 1.? "ACACIA_DOOR", // Minecraft 1.8 "BIRCH_DOOR", // Minecraft 1.8 @@ -86,7 +86,7 @@ public class EnumerationUtil // MATERIAL CONTAINER // -------------------------------------------- // - private static final BackstringSet MATERIALS_CONTAINER = new BackstringSet<>(Material.class, + public static final BackstringSet MATERIALS_CONTAINER = new BackstringSet<>(Material.class, "DISPENSER", // Minecraft 1.? "CHEST", // Minecraft 1.? "FURNACE", // Minecraft 1.? @@ -129,7 +129,7 @@ public class EnumerationUtil // -------------------------------------------- // // Interacting with these entities results in an edit. - private static final BackstringSet ENTITY_TYPES_EDIT_ON_INTERACT = new BackstringSet<>(EntityType.class, + public static final BackstringSet ENTITY_TYPES_EDIT_ON_INTERACT = new BackstringSet<>(EntityType.class, "ITEM_FRAME", // Minecraft 1.? "ARMOR_STAND" // Minecraft 1.8 ); @@ -144,7 +144,7 @@ public class EnumerationUtil // -------------------------------------------- // // Damaging these entities results in an edit. - private static final BackstringSet ENTITY_TYPES_EDIT_ON_DAMAGE = new BackstringSet<>(EntityType.class, + public static final BackstringSet ENTITY_TYPES_EDIT_ON_DAMAGE = new BackstringSet<>(EntityType.class, "ITEM_FRAME", // Minecraft 1.? "ARMOR_STAND", // Minecraft 1.8 "ENDER_CRYSTAL" // Minecraft 1.10 @@ -159,21 +159,21 @@ public class EnumerationUtil // ENTITY TYPE CONTAINER // -------------------------------------------- // - private static final BackstringSet ENTITY_TYPE_CONTAINERS = new BackstringSet<>(EntityType.class, + public static final BackstringSet ENTITY_TYPES_CONTAINER = new BackstringSet<>(EntityType.class, "MINECART_CHEST", // Minecraft 1.? "MINECART_HOPPER" // Minecraft 1.? ); public static boolean isEntityTypeContainer(EntityType entityType) { - return ENTITY_TYPE_CONTAINERS.contains(entityType) || MConf.get().entityTypesContainer.contains(entityType); + return ENTITY_TYPES_CONTAINER.contains(entityType) || MConf.get().entityTypesContainer.contains(entityType); } // -------------------------------------------- // // ENTITY TYPE MONSTER // -------------------------------------------- // - private static final BackstringSet ENTITY_TYPE_MONSTERS = new BackstringSet<>(EntityType.class, + public static final BackstringSet ENTITY_TYPES_MONSTER = new BackstringSet<>(EntityType.class, "BLAZE", // Minecraft 1.? "CAVE_SPIDER", // Minecraft 1.? "CREEPER", // Minecraft 1.? @@ -206,14 +206,14 @@ public class EnumerationUtil public static boolean isEntityTypeMonster(EntityType entityType) { - return ENTITY_TYPE_MONSTERS.contains(entityType) || MConf.get().entityTypesMonsters.contains(entityType); + return ENTITY_TYPES_MONSTER.contains(entityType) || MConf.get().entityTypesMonsters.contains(entityType); } // -------------------------------------------- // // ENTITY TYPE ANIMAL // -------------------------------------------- // - private static final BackstringSet ENTITY_TYPE_ANIMALS = new BackstringSet<>(EntityType.class, + public static final BackstringSet ENTITY_TYPES_ANIMAL = new BackstringSet<>(EntityType.class, "BAT", // Minecraft 1.? "CHICKEN", // Minecraft 1.? "COW", // Minecraft 1.? @@ -234,8 +234,7 @@ public class EnumerationUtil public static boolean isEntityTypeAnimal(EntityType entityType) { - return ENTITY_TYPE_ANIMALS.contains(entityType) || MConf.get().entityTypesAnimals.contains(entityType); + return ENTITY_TYPES_ANIMAL.contains(entityType) || MConf.get().entityTypesAnimals.contains(entityType); } - }