Added MaterialConverter util class for type id conversions.

This commit is contained in:
Jeremy Wood 2019-01-29 23:46:40 -05:00
parent a7cace0fdc
commit 606a3fa3ce
5 changed files with 50 additions and 31 deletions

View File

@ -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

View File

@ -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"))

View File

@ -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;

View File

@ -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<Material> 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<Material> aClass) {
return MaterialConverter.convertTypeString(o.toString());
}
}
}

View File

@ -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);
}
}
}