Plugin configs, ButtonPlugin

Using the same thing for config as components
This commit is contained in:
Norbi Peti 2018-12-18 00:47:00 +01:00
parent 9b097b7858
commit a417d0ea3f
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
7 changed files with 133 additions and 60 deletions

View file

@ -0,0 +1,52 @@
package buttondevteam.lib.architecture;
import buttondevteam.lib.TBMCCoreAPI;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
public abstract class ButtonPlugin extends JavaPlugin {
private HashMap<String, ConfigData<?>> datamap = new HashMap<>();
private ConfigurationSection section;
protected abstract void pluginEnable();
protected abstract void pluginDisable();
@Override
public void onEnable() {
section = getConfig().getConfigurationSection("global");
if (section == null) getConfig().createSection("global");
try {
pluginEnable();
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while enabling plugin " + getName() + "!", e);
}
}
@Override
public void onDisable() {
try {
pluginDisable();
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while disabling plugin " + getName() + "!", e);
}
}
/**
* @see IHaveConfig#getData(Map, ConfigurationSection, String, Object)
*/
protected <T> ConfigData<T> getData(String path, T def) {
return IHaveConfig.getData(datamap, section, path, def);
}
/**
* @see IHaveConfig#getData(Map, ConfigurationSection, String, Object, Function, Function)
*/
protected <T> ConfigData<T> getData(String path, T def, Function<Object, T> getter, Function<T, Object> setter) {
return IHaveConfig.getData(datamap, section, path, def, getter, setter);
}
}

View file

@ -11,7 +11,6 @@ import lombok.NonNull;
import lombok.experimental.var; import lombok.experimental.var;
import lombok.val; import lombok.val;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -41,35 +40,17 @@ public abstract class Component {
private HashMap<String, ConfigData<?>> datamap = new HashMap<>(); private HashMap<String, ConfigData<?>> datamap = new HashMap<>();
/** /**
* This method overload should only be used with primitves or String. * @see IHaveConfig#getData(Map, ConfigurationSection, String, Object)
*
* @param path The path in config to use
* @param def The value to use by default
* @param <T> The type of this variable (only use primitives or String)
* @return The data object that can be used to get or set the value
*/ */
@SuppressWarnings("unchecked")
protected <T> ConfigData<T> getData(String path, T def) { protected <T> ConfigData<T> getData(String path, T def) {
ConfigData<?> data = datamap.get(path); return IHaveConfig.getData(datamap, config, path, def);
if (data == null) datamap.put(path, data = new ConfigData<>(config, path, def));
return (ConfigData<T>) data;
} }
/** /**
* This method overload may be used with any class. * @see IHaveConfig#getData(Map, ConfigurationSection, String, Object, Function, Function)
*
* @param path The path in config to use
* @param def The value to use by default
* @param getter A function that converts a primitive representation to the correct value
* @param setter A function that converts a value to a primitive representation
* @param <T> The type of this variable (can be any class)
* @return The data object that can be used to get or set the value
*/ */
@SuppressWarnings("unchecked")
protected <T> ConfigData<T> getData(String path, T def, Function<Object, T> getter, Function<T, Object> setter) { protected <T> ConfigData<T> getData(String path, T def, Function<Object, T> getter, Function<T, Object> setter) {
ConfigData<?> data = datamap.get(path); return IHaveConfig.getData(datamap, config, path, def, getter, setter);
if (data == null) datamap.put(path, data = new ConfigData<>(config, path, def, getter, setter));
return (ConfigData<T>) data;
} }
/** /**
@ -210,14 +191,6 @@ public abstract class Component {
return listener; return listener;
} }
public void saveData(FileConfiguration config, String pathToData, Object data) {
config.set("moduledata." + this.getClassName() + "." + pathToData, data);
}
public Object getData(FileConfiguration config, String pathToData, Object data) {
return config.get("moduledata." + this.getClassName() + "." + pathToData, data);
}
private String getClassName() { private String getClassName() {
Class<?> enclosingClass = getClass().getEnclosingClass(); Class<?> enclosingClass = getClass().getEnclosingClass();
String className; String className;

View file

@ -1,5 +1,6 @@
package buttondevteam.lib.architecture; package buttondevteam.lib.architecture;
import lombok.AccessLevel;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
@ -8,10 +9,11 @@ import org.bukkit.configuration.file.YamlConfiguration;
import java.util.function.Function; import java.util.function.Function;
/** /**
* Use the getter/setter constructor if {@link T} isn't a primitive type or String. * Use the getter/setter constructor if {@link T} isn't a primitive type or String.<br>
* Use {@link Component#getData(String, Object)} or {@link ButtonPlugin#getData(String, Object)} to get an instance.
*/ */
@RequiredArgsConstructor @RequiredArgsConstructor(access = AccessLevel.PACKAGE)
@AllArgsConstructor @AllArgsConstructor(access = AccessLevel.PACKAGE)
public class ConfigData<T> { //TODO: Save after a while public class ConfigData<T> { //TODO: Save after a while
private final ConfigurationSection config; private final ConfigurationSection config;
private final String path; private final String path;

View file

@ -0,0 +1,45 @@
package buttondevteam.lib.architecture;
import org.bukkit.configuration.ConfigurationSection;
import java.util.Map;
import java.util.function.Function;
/**
* Members of this interface should be protected (access level)
*/
final class IHaveConfig {
private IHaveConfig() {}
/**
* This method overload should only be used with primitves or String.
*
* @param path The path in config to use
* @param def The value to use by default
* @param <T> The type of this variable (only use primitives or String)
* @return The data object that can be used to get or set the value
*/
@SuppressWarnings("unchecked")
protected static <T> ConfigData<T> getData(Map<String, ConfigData<?>> datamap, ConfigurationSection config, String path, T def) {
ConfigData<?> data = datamap.get(path);
if (data == null) datamap.put(path, data = new ConfigData<>(config, path, def));
return (ConfigData<T>) data;
}
/**
* This method overload may be used with any class.
*
* @param path The path in config to use
* @param def The value to use by default
* @param getter A function that converts a primitive representation to the correct value
* @param setter A function that converts a value to a primitive representation
* @param <T> The type of this variable (can be any class)
* @return The data object that can be used to get or set the value
*/
@SuppressWarnings("unchecked")
protected static <T> ConfigData<T> getData(Map<String, ConfigData<?>> datamap, ConfigurationSection config, String path, T def, Function<Object, T> getter, Function<T, Object> setter) {
ConfigData<?> data = datamap.get(path);
if (data == null) datamap.put(path, data = new ConfigData<>(config, path, def, getter, setter));
return (ConfigData<T>) data;
}
}

View file

@ -34,7 +34,7 @@ public class Channel {
/** /**
* Filters both the sender and the targets * Filters both the sender and the targets
*/ */
public final Function<CommandSender, RecipientTestResult> filteranderrormsg; private final Function<CommandSender, RecipientTestResult> filteranderrormsg;
private static final List<Channel> channels = new ArrayList<>(); private static final List<Channel> channels = new ArrayList<>();
@ -87,10 +87,7 @@ public class Channel {
* Note: Errors are sent to the sender automatically * Note: Errors are sent to the sender automatically
*/ */
public int getMCScore(CommandSender sender) { public int getMCScore(CommandSender sender) {
if (filteranderrormsg == null) return getRTR(sender).score; //No need to check if there was an error
return SCORE_SEND_OK;
RecipientTestResult result = filteranderrormsg.apply(sender);
return result.errormessage == null ? result.score : SCORE_SEND_NOPE;
} }
/** /**
@ -100,10 +97,13 @@ public class Channel {
*/ */
@Nullable @Nullable
public String getGroupID(CommandSender sender) { public String getGroupID(CommandSender sender) {
return getRTR(sender).groupID; //No need to check if there was an error
}
public RecipientTestResult getRTR(CommandSender sender) {
if (filteranderrormsg == null) if (filteranderrormsg == null)
return GROUP_EVERYONE; return new RecipientTestResult(SCORE_SEND_OK, GROUP_EVERYONE);
RecipientTestResult result = filteranderrormsg.apply(sender); return filteranderrormsg.apply(sender);
return result.errormessage == null ? result.groupID : null;
} }
public static List<Channel> getChannels() { public static List<Channel> getChannels() {
@ -143,9 +143,10 @@ public class Channel {
} }
public static class RecipientTestResult { public static class RecipientTestResult {
public String errormessage = null; public final String errormessage;
public int score = SCORE_SEND_NOPE; // Anything below 0 is "never send" public final int score; // Anything below 0 is "never send"
public String groupID = null; public final String groupID;
public static final RecipientTestResult ALL = new RecipientTestResult(SCORE_SEND_OK, GROUP_EVERYONE);
/** /**
* Creates a result that indicates an <b>error</b> * Creates a result that indicates an <b>error</b>
@ -154,6 +155,8 @@ public class Channel {
*/ */
public RecipientTestResult(String errormessage) { public RecipientTestResult(String errormessage) {
this.errormessage = errormessage; this.errormessage = errormessage;
this.score = SCORE_SEND_NOPE;
this.groupID = null;
} }
/** /**
@ -163,8 +166,10 @@ public class Channel {
* @param groupID The ID of the target group. * @param groupID The ID of the target group.
*/ */
public RecipientTestResult(int score, String groupID) { public RecipientTestResult(int score, String groupID) {
if (score < 0) throw new IllegalArgumentException("Score must be non-negative!");
this.score = score; this.score = score;
this.groupID = groupID; this.groupID = groupID;
this.errormessage = null;
} }
} }
} }

View file

@ -15,11 +15,11 @@ public class ChatRoom extends Channel {
public void joinRoom(CommandSender sender) { public void joinRoom(CommandSender sender) {
usersInRoom.add(sender); usersInRoom.add(sender);
TBMCChatAPI.SendSystemMessage(this, 0, "everyone", sender.getName() + " joined the room"); TBMCChatAPI.SendSystemMessage(this, RecipientTestResult.ALL, sender.getName() + " joined the room");
} }
public void leaveRoom(CommandSender sender) { public void leaveRoom(CommandSender sender) {
usersInRoom.remove(sender); usersInRoom.remove(sender);
TBMCChatAPI.SendSystemMessage(this, 0, "everyone", sender.getName() + " left the room"); TBMCChatAPI.SendSystemMessage(this, RecipientTestResult.ALL, sender.getName() + " left the room");
} }
} }

View file

@ -231,7 +231,7 @@ public class TBMCChatAPI {
val permcheck = cm.getPermCheck() == null ? cm.getSender() : cm.getPermCheck(); val permcheck = cm.getPermCheck() == null ? cm.getSender() : cm.getPermCheck();
RecipientTestResult rtr = getScoreOrSendError(channel, permcheck); RecipientTestResult rtr = getScoreOrSendError(channel, permcheck);
int score = rtr.score; int score = rtr.score;
if (score == -1 || rtr.groupID == null) if (score == Channel.SCORE_SEND_NOPE || rtr.groupID == null)
return true; return true;
TBMCChatPreprocessEvent eventPre = new TBMCChatPreprocessEvent(cm.getSender(), channel, cm.getMessage()); TBMCChatPreprocessEvent eventPre = new TBMCChatPreprocessEvent(cm.getSender(), channel, cm.getMessage());
Bukkit.getPluginManager().callEvent(eventPre); Bukkit.getPluginManager().callEvent(eventPre);
@ -248,29 +248,25 @@ public class TBMCChatAPI {
* *
* @param channel * @param channel
* The channel to send to * The channel to send to
* @param score * @param rtr
* The score to use to find the group - use 0 if the channel doesn't have scores * The score&group to use to find the group - use {@link RecipientTestResult#ALL} if the channel doesn't have scores
* @param message * @param message
* The message to send * The message to send
* @return The event cancelled state * @return The event cancelled state
*/ */
public static boolean SendSystemMessage(Channel channel, int score, String groupid, String message) { public static boolean SendSystemMessage(Channel channel, RecipientTestResult rtr, String message) {
if (!Channel.getChannels().contains(channel)) if (!Channel.getChannels().contains(channel))
throw new RuntimeException("Channel " + channel.DisplayName + " not registered!"); throw new RuntimeException("Channel " + channel.DisplayName + " not registered!");
TBMCSystemChatEvent event = new TBMCSystemChatEvent(channel, message, score, groupid); TBMCSystemChatEvent event = new TBMCSystemChatEvent(channel, message, rtr.score, rtr.groupID);
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
return event.isCancelled(); return event.isCancelled();
} }
private static RecipientTestResult getScoreOrSendError(Channel channel, CommandSender sender) { private static RecipientTestResult getScoreOrSendError(Channel channel, CommandSender sender) {
if (channel.filteranderrormsg == null) RecipientTestResult result = channel.getRTR(sender);
return new RecipientTestResult(0, "everyone"); if (result.errormessage != null)
else { sender.sendMessage("§c" + result.errormessage);
RecipientTestResult result = channel.filteranderrormsg.apply(sender); return result;
if (result.errormessage != null)
sender.sendMessage("§c" + result.errormessage);
return result;
}
} }
/** /**