From 606a3fa3ce8f3c4266aa4eabe6571a483ebe4acd Mon Sep 17 00:00:00 2001 From: Jeremy Wood Date: Tue, 29 Jan 2019 23:46:40 -0500 Subject: [PATCH] Added MaterialConverter util class for type id conversions. --- .travis.yml | 14 +++---- .../MultiverseCore/MultiverseCore.java | 17 +-------- .../MultiverseCore/WorldProperties.java | 1 - .../configuration/EntryFee.java | 11 ++---- .../utils/MaterialConverter.java | 38 +++++++++++++++++++ 5 files changed, 50 insertions(+), 31 deletions(-) create mode 100644 src/main/java/com/onarandombox/MultiverseCore/utils/MaterialConverter.java diff --git a/.travis.yml b/.travis.yml index a64f9f60..1ecfa2d1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ -language: java -jdk: - - oraclejdk8 -notifications: - email: false -before_install: - - sed -i.bak -e 's|https://nexus.codehaus.org/snapshots/|https://oss.sonatype.org/content/repositories/codehaus-snapshots/|g' ~/.m2/settings.xml +language: java +jdk: + - oraclejdk8 +notifications: + email: false +before_install: + - sed -i.bak -e 's|https://nexus.codehaus.org/snapshots/|https://oss.sonatype.org/content/repositories/codehaus-snapshots/|g' ~/.m2/settings.xml diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index cef29997..6d37f01e 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -77,6 +77,7 @@ import com.onarandombox.MultiverseCore.utils.MVEconomist; import com.onarandombox.MultiverseCore.utils.MVMessaging; import com.onarandombox.MultiverseCore.utils.MVPermissions; import com.onarandombox.MultiverseCore.utils.MVPlayerSession; +import com.onarandombox.MultiverseCore.utils.MaterialConverter; import com.onarandombox.MultiverseCore.utils.SimpleBlockSafety; import com.onarandombox.MultiverseCore.utils.SimpleLocationManipulation; import com.onarandombox.MultiverseCore.utils.SimpleSafeTTeleporter; @@ -84,15 +85,12 @@ import com.onarandombox.MultiverseCore.utils.UnsafeCallWrapper; import com.onarandombox.MultiverseCore.utils.VaultHandler; import com.onarandombox.MultiverseCore.utils.WorldManager; import com.pneumaticraft.commandhandler.CommandHandler; -import de.themoep.idconverter.IdMappings; import me.main__.util.SerializationConfig.NoSuchPropertyException; import me.main__.util.SerializationConfig.SerializationConfig; import org.bukkit.ChatColor; import org.bukkit.Difficulty; import org.bukkit.GameMode; import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.Server; import org.bukkit.World.Environment; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -103,7 +101,6 @@ import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.plugin.PluginDescriptionFile; -import org.bukkit.plugin.PluginLoader; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPluginLoader; @@ -641,17 +638,7 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { if (section.isConfigurationSection("entryfee")) { ConfigurationSection feeSection = section.getConfigurationSection("entryfee"); if (feeSection.isInt("currency")) { - int oldCurrencyItemId = feeSection.getInt("currency", -1); - if (oldCurrencyItemId >= 0) { - IdMappings.Mapping mapping = IdMappings.getById(Integer.toString(oldCurrencyItemId)); - if (mapping != null) { - world.setCurrency(Material.matchMaterial(mapping.getFlatteningType())); - } else { - world.setCurrency(null); - } - } else { - world.setCurrency(null); - } + world.setCurrency(MaterialConverter.convertConfigType(feeSection, "currency")); } if (feeSection.isDouble("amount")) diff --git a/src/main/java/com/onarandombox/MultiverseCore/WorldProperties.java b/src/main/java/com/onarandombox/MultiverseCore/WorldProperties.java index 1e882c71..3dd208b8 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/WorldProperties.java +++ b/src/main/java/com/onarandombox/MultiverseCore/WorldProperties.java @@ -8,7 +8,6 @@ import com.onarandombox.MultiverseCore.configuration.WorldPropertyValidator; import com.onarandombox.MultiverseCore.enums.AllowedPortalType; import com.onarandombox.MultiverseCore.enums.EnglishChatColor; import com.onarandombox.MultiverseCore.enums.EnglishChatStyle; -import de.themoep.idconverter.IdMappings; import me.main__.util.SerializationConfig.IllegalPropertyValueException; import me.main__.util.SerializationConfig.Property; import me.main__.util.SerializationConfig.SerializationConfig; diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/EntryFee.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/EntryFee.java index 42a1d4c3..be66fb3a 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/EntryFee.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/EntryFee.java @@ -2,8 +2,7 @@ package com.onarandombox.MultiverseCore.configuration; import java.util.Map; -import de.themoep.idconverter.IdMappings; -import me.main__.util.SerializationConfig.IllegalPropertyValueException; +import com.onarandombox.MultiverseCore.utils.MaterialConverter; import me.main__.util.SerializationConfig.Property; import me.main__.util.SerializationConfig.SerializationConfig; @@ -78,12 +77,8 @@ public class EntryFee extends SerializationConfig { } @Override - public Material deserialize(Object o, Class aClass) throws IllegalPropertyValueException { - IdMappings.Mapping mapping = IdMappings.getById(o.toString()); - if (mapping != null) { - return Material.matchMaterial(mapping.getFlatteningType()); - } - return Material.matchMaterial(o.toString()); + public Material deserialize(Object o, Class aClass) { + return MaterialConverter.convertTypeString(o.toString()); } } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/MaterialConverter.java b/src/main/java/com/onarandombox/MultiverseCore/utils/MaterialConverter.java new file mode 100644 index 00000000..cf4e1286 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/MaterialConverter.java @@ -0,0 +1,38 @@ +package com.onarandombox.MultiverseCore.utils; + +import de.themoep.idconverter.IdMappings; +import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; + +/** + * A tool for converting values which may be an old type ID to a Material. + */ +public class MaterialConverter { + + /** + * Converts the value in the given config at the given path from a numeric id or flattened material name to a + * Material. + * + * @param config The config with the value to convert. + * @param path The path of the value in the config. + * @return The converted Material type or null if no matching type. + */ + public static Material convertConfigType(ConfigurationSection config, String path) { + return convertTypeString(config.getString(path)); + } + + /** + * Converts a string representing a numeric id or flattened material name to a Material. + * + * @param value The value to convert. + * @return The converted Material type or null if no matching type. + */ + public static Material convertTypeString(String value) { + IdMappings.Mapping mapping = IdMappings.getById(value); + if (mapping != null) { + return Material.matchMaterial(mapping.getFlatteningType()); + } else { + return Material.matchMaterial(value); + } + } +}