Extended the config api annotations.

Added the concept of an adapter class that would convert the YAML input
to the setting type and value.
I included an example adapter class for Enum.

I fixed a number of the settings to match the config/yml or the other
way around.

After compiling and running, do /bsb reload to see the result.

This is a WIP and obviously not finished.
This commit is contained in:
Tastybento 2018-01-04 19:58:04 -08:00
parent 1430eda5ce
commit 478968dac7
8 changed files with 177 additions and 31 deletions

View File

@ -70,7 +70,7 @@ general:
mute-death-messages: false
# Allow FTB Autonomous Activator to work (will allow a pseudo player [CoFH] to place and break blocks and hang items)
FTB-auto-activator: false
allow-FTB-auto-activator: false
# Allow obsidian to be scooped up with an empty bucket back into lava
# This only works if there is a single block of obsidian (no obsidian within 10 blocks)
@ -102,7 +102,7 @@ world:
# Will be rounded up to the nearest 16 blocks.
# It is the same for every dimension : Overworld, Nether and End.
# This value cannot be changed mid-game and the plugin will not start if it is different.
distance: 64
distance-between-islands: 64
# Default protection range radius in blocks. Cannot be larger than distance.
# Admins can change protection sizes for players individually using /bsadmin setrange
@ -157,7 +157,11 @@ world:
# Minimum is 0 (not recommended), maximum is 100. Default is 25.
# Only applies to vanilla nether
spawn-radius: 25
end:
generate: true
islands: true
### Entities-related Settings ###
entities:
# Sets the limit for number of entities that can spawn in a chunk in this world.
@ -404,7 +408,7 @@ protection:
# Restrict Wither and other flying mobs.
# Any flying mobs that exit the island space where they were spawned will be removed.
# Includes blaze and ghast.
restrict-wither: true
restrict-flying-mobs: true
# Invincible visitors - Prevent players from killing them (intentionally or by accident)
# If they fall to the void, they get TP'd to their island.

View File

@ -0,0 +1,25 @@
/**
*
*/
package us.tastybento.bskyblock;
import org.bukkit.Bukkit;
/**
* @author ben
* Takes in inputs and turns them into an enum
*/
public class EnumAdapter {
/**
* Takes in inputs and turns them into an enum of type
* @param input
* @param type
* @return
*/
public Object adapt(Object input, Object type) {
Bukkit.getLogger().info("Ran the EnumAdapter: input is " + input + " and type = " + type);
return Enum.valueOf((Class)type, (String)input);
}
}

View File

@ -6,7 +6,6 @@ import java.util.HashMap;
import java.util.List;
import org.bukkit.entity.EntityType;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffectType;
@ -24,7 +23,7 @@ public class Settings implements ISettings {
// Game Type BSKYBLOCK or ACIDISLAND
public enum GameType {
BSKYBLOCK, ACIDISLAND
BSKYBLOCK, ACIDISLAND, BOTH
}
/*
public final static GameType GAMETYPE = GameType.ACIDISLAND;
@ -65,29 +64,29 @@ public class Settings implements ISettings {
public static boolean useEconomy = true;
// Purge
@ConfigEntry(path = "general.purge.max-islands")
public static int purgeMaxIslands = 50;
@ConfigEntry(path = "general.purge.max-island-level")
public static int purgeMaxIslandLevel = 50;
@ConfigEntry(path = "general.purge.remove-user-data")
public static boolean purgeRemoveUserData = false;
// Database
@ConfigEntry(path = "general.database.type")
@ConfigEntry(path = "general.database.type", adapter = EnumAdapter.class)
public static DatabaseType databaseType = DatabaseType.FLATFILE;
@ConfigEntry(path = "general.database.settings.host")
@ConfigEntry(path = "general.database.host")
public static String dbHost = "localhost";
@ConfigEntry(path = "general.database.settings.port")
@ConfigEntry(path = "general.database.port")
public static int dbPort = 3306;
@ConfigEntry(path = "general.database.settings.name")
@ConfigEntry(path = "general.database.name")
public static String dbName = "BSkyBlock";
@ConfigEntry(path = "general.database.settings.username")
@ConfigEntry(path = "general.database.username")
public static String dbUsername = "username";
@ConfigEntry(path = "general.database.settings.password")
@ConfigEntry(path = "general.database.password")
public static String dbPassword = "password";
@ConfigEntry(path = "general.database.backup-period")
@ -121,7 +120,7 @@ public class Settings implements ISettings {
public static int islandXOffset;
public static int islandZOffset;
@ConfigEntry(path = "world.sea-height", specificTo = ConfigEntry.GameType.ACIDISLAND)
@ConfigEntry(path = "world.sea-height", specificTo = GameType.ACIDISLAND)
public static int seaHeight = 100;
@ConfigEntry(path = "world.island-height")
@ -220,23 +219,23 @@ public class Settings implements ISettings {
* This settings category only exists if the GameType is ACIDISLAND.
*/
@ConfigEntry(path = "acid.options.damage-op", specificTo = ConfigEntry.GameType.ACIDISLAND)
@ConfigEntry(path = "acid.damage-op", specificTo = GameType.ACIDISLAND)
public static boolean acidDamageOp = false;
@ConfigEntry(path = "acid.options.damage-chickens", specificTo = ConfigEntry.GameType.ACIDISLAND)
@ConfigEntry(path = "acid.damage-chickens", specificTo = GameType.ACIDISLAND)
public static boolean acidDamageChickens = false;
@ConfigEntry(path = "acid.options.item-destroy-time", specificTo = ConfigEntry.GameType.ACIDISLAND)
@ConfigEntry(path = "acid.options.item-destroy-time", specificTo = GameType.ACIDISLAND)
public static int acidDestroyItemTime = 0;
// Damage
@ConfigEntry(path = "acid.damage", specificTo = ConfigEntry.GameType.ACIDISLAND)
@ConfigEntry(path = "acid.damage.acid.player", specificTo = GameType.ACIDISLAND)
public static int acidDamage = 10;
@ConfigEntry(path = "acid.rain-damage", specificTo = ConfigEntry.GameType.ACIDISLAND)
@ConfigEntry(path = "acid.damage.rain", specificTo = GameType.ACIDISLAND)
public static int acidRainDamage = 1;
@ConfigEntry(path = "acid.effects", specificTo = ConfigEntry.GameType.ACIDISLAND)
@ConfigEntry(path = "acid.damage.effects", specificTo = GameType.ACIDISLAND)
public static List<PotionEffectType> acidEffects = new ArrayList<>(Arrays.asList(PotionEffectType.CONFUSION, PotionEffectType.SLOW));
/* SCHEMATICS */

View File

@ -1,16 +1,16 @@
package us.tastybento.bskyblock.api.configuration;
import us.tastybento.bskyblock.Settings;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import us.tastybento.bskyblock.Settings.GameType;
/**
*
*
* @author Poslovitch
* @author Poslovitch, tastybento
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@ -22,10 +22,7 @@ public @interface ConfigEntry {
boolean experimental() default false;
boolean needsReset() default false;
GameType specificTo() default GameType.BOTH;
enum GameType {
BSKYBLOCK,
ACIDISLAND,
BOTH
}
Class<?> adapter() default NoAdapter.class;
public class NoAdapter {}
}

View File

@ -0,0 +1,5 @@
package us.tastybento.bskyblock.api.configuration;
public abstract class Conversion {
public abstract void convert();
}

View File

@ -2,10 +2,11 @@ package us.tastybento.bskyblock.commands;
import java.util.List;
import us.tastybento.bskyblock.Settings;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.commands.admin.AdminReloadCommand;
import us.tastybento.bskyblock.commands.admin.AdminVersionCommand;
import us.tastybento.bskyblock.Settings;
public class AdminCommand extends CompositeCommand {
@ -19,6 +20,7 @@ public class AdminCommand extends CompositeCommand {
this.setOnlyPlayer(false);
this.setDescription("admin.help.description");
new AdminVersionCommand(this);
new AdminReloadCommand(this);
}
@Override

View File

@ -0,0 +1,45 @@
/**
*
*/
package us.tastybento.bskyblock.commands.admin;
import java.util.List;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.config.ConfigLoader;
/**
* @author ben
*
*/
public class AdminReloadCommand extends CompositeCommand {
/**
* @param parent
* @param label
* @param aliases
*/
public AdminReloadCommand(CompositeCommand parent) {
super(parent, "reload");
}
/* (non-Javadoc)
* @see us.tastybento.bskyblock.api.commands.BSBCommand#setup()
*/
@Override
public void setup() {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see us.tastybento.bskyblock.api.commands.BSBCommand#execute(us.tastybento.bskyblock.api.commands.User, java.util.List)
*/
@Override
public boolean execute(User user, List<String> args) {
new ConfigLoader();
return true;
}
}

View File

@ -0,0 +1,69 @@
package us.tastybento.bskyblock.config;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.bukkit.Bukkit;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.Settings;
import us.tastybento.bskyblock.Settings.GameType;
import us.tastybento.bskyblock.api.configuration.ConfigEntry;
import us.tastybento.bskyblock.api.configuration.ConfigEntry.NoAdapter;
public class ConfigLoader {
public ConfigLoader() {
try {
getAnnotations(Settings.class);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void getAnnotations(Class<Settings> clazz) throws IllegalArgumentException, IllegalAccessException, NoSuchMethodException, SecurityException, InvocationTargetException, InstantiationException{
for(Field field : clazz.getDeclaredFields()){
Class<?> type = field.getType();
String name = field.getName();
ConfigEntry configEntry = field.getAnnotation(ConfigEntry.class);
// If there is a config annotation then do something
if (configEntry != null) {
// Get the current value
Object currentvalue = field.get(null);
// Get the setting as defined in the config file
Object configValue = BSkyBlock.getInstance().getConfig().get(configEntry.path());
// If this setting is for use in this game
if (configEntry.specificTo().equals(GameType.BOTH) || configEntry.specificTo().equals(Settings.GAMETYPE)) {
// If there's a difference in the value. Note for non-primitives, this will be true
if (!currentvalue.equals(configValue)) {
Bukkit.getLogger().info(name + " changed value from " + currentvalue + " to " + configValue);
// If there's an adapter use it to convert from the YML object to the setting field
if (!configEntry.adapter().equals(NoAdapter.class)) {
// Create an instance of the adapter class
Object instance = configEntry.adapter().newInstance();
// Get the adapt method - it should be there.
Method method = configEntry.adapter().getMethod("adapt", Object.class, Object.class);
if (method != null) {
// It exists, so invoke it
configValue = method.invoke(instance, configValue, type);
if (configValue != null) {
// Set the field.
field.set(null, configValue);
}
}
} else if (configValue != null){
// No adapter - I hope this works!
field.set(null, configValue);
}
}
} else {
Bukkit.getLogger().info(name + " not applicable to this game type");
}
}
}
}
}