diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java new file mode 100644 index 0000000..ff777d3 --- /dev/null +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java @@ -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> 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 ConfigData getData(String path, T def) { + return IHaveConfig.getData(datamap, section, path, def); + } + + /** + * @see IHaveConfig#getData(Map, ConfigurationSection, String, Object, Function, Function) + */ + protected ConfigData getData(String path, T def, Function getter, Function setter) { + return IHaveConfig.getData(datamap, section, path, def, getter, setter); + } +} diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java index 4b9219c..57a98d5 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java @@ -11,7 +11,6 @@ import lombok.NonNull; import lombok.experimental.var; import lombok.val; import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.event.Listener; import org.bukkit.plugin.java.JavaPlugin; @@ -41,35 +40,17 @@ public abstract class Component { private HashMap> datamap = new HashMap<>(); /** - * 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 The type of this variable (only use primitives or String) - * @return The data object that can be used to get or set the value + * @see IHaveConfig#getData(Map, ConfigurationSection, String, Object) */ - @SuppressWarnings("unchecked") protected ConfigData getData(String path, T def) { - ConfigData data = datamap.get(path); - if (data == null) datamap.put(path, data = new ConfigData<>(config, path, def)); - return (ConfigData) data; + return IHaveConfig.getData(datamap, config, path, def); } /** - * 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 The type of this variable (can be any class) - * @return The data object that can be used to get or set the value + * @see IHaveConfig#getData(Map, ConfigurationSection, String, Object, Function, Function) */ - @SuppressWarnings("unchecked") protected ConfigData getData(String path, T def, Function getter, Function setter) { - ConfigData data = datamap.get(path); - if (data == null) datamap.put(path, data = new ConfigData<>(config, path, def, getter, setter)); - return (ConfigData) data; + return IHaveConfig.getData(datamap, config, path, def, getter, setter); } /** @@ -210,14 +191,6 @@ public abstract class Component { 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() { Class enclosingClass = getClass().getEnclosingClass(); String className; diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ConfigData.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ConfigData.java index 829ea2b..1d833f4 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ConfigData.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ConfigData.java @@ -1,5 +1,6 @@ package buttondevteam.lib.architecture; +import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.RequiredArgsConstructor; import org.bukkit.configuration.ConfigurationSection; @@ -8,10 +9,11 @@ import org.bukkit.configuration.file.YamlConfiguration; 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.
+ * Use {@link Component#getData(String, Object)} or {@link ButtonPlugin#getData(String, Object)} to get an instance. */ -@RequiredArgsConstructor -@AllArgsConstructor +@RequiredArgsConstructor(access = AccessLevel.PACKAGE) +@AllArgsConstructor(access = AccessLevel.PACKAGE) public class ConfigData { //TODO: Save after a while private final ConfigurationSection config; private final String path; diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java new file mode 100644 index 0000000..77ad752 --- /dev/null +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java @@ -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 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 ConfigData getData(Map> 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) 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 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 ConfigData getData(Map> datamap, ConfigurationSection config, String path, T def, Function getter, Function setter) { + ConfigData data = datamap.get(path); + if (data == null) datamap.put(path, data = new ConfigData<>(config, path, def, getter, setter)); + return (ConfigData) data; + } +} diff --git a/ButtonCore/src/main/java/buttondevteam/lib/chat/Channel.java b/ButtonCore/src/main/java/buttondevteam/lib/chat/Channel.java index c18c557..bddc0f3 100755 --- a/ButtonCore/src/main/java/buttondevteam/lib/chat/Channel.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/chat/Channel.java @@ -34,7 +34,7 @@ public class Channel { /** * Filters both the sender and the targets */ - public final Function filteranderrormsg; + private final Function filteranderrormsg; private static final List channels = new ArrayList<>(); @@ -87,10 +87,7 @@ public class Channel { * Note: Errors are sent to the sender automatically */ public int getMCScore(CommandSender sender) { - if (filteranderrormsg == null) - return SCORE_SEND_OK; - RecipientTestResult result = filteranderrormsg.apply(sender); - return result.errormessage == null ? result.score : SCORE_SEND_NOPE; + return getRTR(sender).score; //No need to check if there was an error } /** @@ -100,10 +97,13 @@ public class Channel { */ @Nullable 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) - return GROUP_EVERYONE; - RecipientTestResult result = filteranderrormsg.apply(sender); - return result.errormessage == null ? result.groupID : null; + return new RecipientTestResult(SCORE_SEND_OK, GROUP_EVERYONE); + return filteranderrormsg.apply(sender); } public static List getChannels() { @@ -143,9 +143,10 @@ public class Channel { } public static class RecipientTestResult { - public String errormessage = null; - public int score = SCORE_SEND_NOPE; // Anything below 0 is "never send" - public String groupID = null; + public final String errormessage; + public final int score; // Anything below 0 is "never send" + public final String groupID; + public static final RecipientTestResult ALL = new RecipientTestResult(SCORE_SEND_OK, GROUP_EVERYONE); /** * Creates a result that indicates an error @@ -154,6 +155,8 @@ public class Channel { */ public RecipientTestResult(String 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. */ public RecipientTestResult(int score, String groupID) { + if (score < 0) throw new IllegalArgumentException("Score must be non-negative!"); this.score = score; this.groupID = groupID; + this.errormessage = null; } } } diff --git a/ButtonCore/src/main/java/buttondevteam/lib/chat/ChatRoom.java b/ButtonCore/src/main/java/buttondevteam/lib/chat/ChatRoom.java index 8a1e75d..9819d7c 100755 --- a/ButtonCore/src/main/java/buttondevteam/lib/chat/ChatRoom.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/chat/ChatRoom.java @@ -15,11 +15,11 @@ public class ChatRoom extends Channel { public void joinRoom(CommandSender 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) { usersInRoom.remove(sender); - TBMCChatAPI.SendSystemMessage(this, 0, "everyone", sender.getName() + " left the room"); + TBMCChatAPI.SendSystemMessage(this, RecipientTestResult.ALL, sender.getName() + " left the room"); } } diff --git a/ButtonCore/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java b/ButtonCore/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java index 4cb7894..d5cddef 100755 --- a/ButtonCore/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java @@ -231,7 +231,7 @@ public class TBMCChatAPI { val permcheck = cm.getPermCheck() == null ? cm.getSender() : cm.getPermCheck(); RecipientTestResult rtr = getScoreOrSendError(channel, permcheck); int score = rtr.score; - if (score == -1 || rtr.groupID == null) + if (score == Channel.SCORE_SEND_NOPE || rtr.groupID == null) return true; TBMCChatPreprocessEvent eventPre = new TBMCChatPreprocessEvent(cm.getSender(), channel, cm.getMessage()); Bukkit.getPluginManager().callEvent(eventPre); @@ -248,29 +248,25 @@ public class TBMCChatAPI { * * @param channel * The channel to send to - * @param score - * The score to use to find the group - use 0 if the channel doesn't have scores + * @param rtr + * The score&group to use to find the group - use {@link RecipientTestResult#ALL} if the channel doesn't have scores * @param message * The message to send * @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)) 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); return event.isCancelled(); } private static RecipientTestResult getScoreOrSendError(Channel channel, CommandSender sender) { - if (channel.filteranderrormsg == null) - return new RecipientTestResult(0, "everyone"); - else { - RecipientTestResult result = channel.filteranderrormsg.apply(sender); - if (result.errormessage != null) - sender.sendMessage("§c" + result.errormessage); - return result; - } + RecipientTestResult result = channel.getRTR(sender); + if (result.errormessage != null) + sender.sendMessage("§c" + result.errormessage); + return result; } /**