Put channels in a component
Using the per-component AddCommand method CommandComponent got removed, no need for that Made channel properties configurable (DisplayName, Color) Made configs testable, it will only use the cached value in that case This was needed so channel registering continue to work during testing Made channels possible to disable (WIP) #48
This commit is contained in:
parent
a6d1122f7f
commit
1b152d8b4e
21 changed files with 298 additions and 233 deletions
|
@ -1,7 +1,7 @@
|
|||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="WeakerAccess" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<inspection_tool class="WeakerAccess" enabled="false" level="WARNING" enabled_by_default="false">
|
||||
<option name="SUGGEST_PACKAGE_LOCAL_FOR_MEMBERS" value="false" />
|
||||
<option name="SUGGEST_PACKAGE_LOCAL_FOR_TOP_CLASSES" value="false" />
|
||||
<option name="SUGGEST_PRIVATE_FOR_INNERS" value="false" />
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
package buttondevteam.lib.chat;
|
||||
package buttondevteam.component.channel;
|
||||
|
||||
import buttondevteam.core.ComponentManager;
|
||||
import buttondevteam.core.MainPlugin;
|
||||
import buttondevteam.lib.architecture.Component;
|
||||
import buttondevteam.lib.architecture.ConfigData;
|
||||
import buttondevteam.lib.chat.Color;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
@ -26,11 +31,37 @@ public class Channel {
|
|||
* Send the message to everyone <i>who has access to the channel</i> - this does not necessarily mean all players
|
||||
*/
|
||||
public static final String GROUP_EVERYONE = "everyone";
|
||||
public final String DisplayName;
|
||||
public final Color color;
|
||||
|
||||
private static ChannelComponent component;
|
||||
|
||||
private String defDisplayName;
|
||||
private Color defColor;
|
||||
|
||||
private void throwGame() {
|
||||
if (component == null) throw new RuntimeException("Cannot access channel properties until registered!");
|
||||
}
|
||||
|
||||
public final ConfigData<Boolean> Enabled() { //TODO: Implement in all plugins
|
||||
throwGame();
|
||||
return component.getConfig().getData(ID + ".enabled", true);
|
||||
}
|
||||
|
||||
public final ConfigData<String> DisplayName() {
|
||||
throwGame();
|
||||
return component.getConfig().getData(ID + ".displayName", defDisplayName);
|
||||
}
|
||||
|
||||
public final ConfigData<Color> Color() {
|
||||
throwGame();
|
||||
return component.getConfig().getData(ID + ".color", defColor, c -> Color.valueOf((String) c), Enum::toString);
|
||||
}
|
||||
public final String ID;
|
||||
@Nullable
|
||||
public String[] IDs;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public ConfigData<String[]> IDs() {
|
||||
throwGame();
|
||||
return component.getConfig().getData(ID + ".IDs", new String[0], l -> ((List<String>) l).toArray(new String[0]), Lists::newArrayList);
|
||||
}
|
||||
/**
|
||||
* Filters both the sender and the targets
|
||||
*/
|
||||
|
@ -49,8 +80,8 @@ public class Channel {
|
|||
*/
|
||||
public Channel(String displayname, Color color, String command,
|
||||
Function<CommandSender, RecipientTestResult> filteranderrormsg) {
|
||||
DisplayName = displayname;
|
||||
this.color = color;
|
||||
defDisplayName = displayname;
|
||||
defColor = color;
|
||||
ID = command;
|
||||
this.filteranderrormsg = filteranderrormsg;
|
||||
}
|
||||
|
@ -63,8 +94,8 @@ public class Channel {
|
|||
@SuppressWarnings("unchecked")
|
||||
protected <T extends Channel> Channel(String displayname, Color color, String command,
|
||||
BiFunction<T, CommandSender, RecipientTestResult> filteranderrormsg) {
|
||||
DisplayName = displayname;
|
||||
this.color = color;
|
||||
defDisplayName = displayname;
|
||||
defColor = color;
|
||||
ID = command;
|
||||
this.filteranderrormsg = s -> filteranderrormsg.apply((T) this, s);
|
||||
}
|
||||
|
@ -137,7 +168,13 @@ public class Channel {
|
|||
public static Channel AdminChat;
|
||||
public static Channel ModChat;
|
||||
|
||||
static void RegisterChannel(Channel channel) {
|
||||
public static void RegisterChannel(Channel channel) {
|
||||
if (!channel.isGlobal() && !ComponentManager.isEnabled(ChannelComponent.class))
|
||||
return; //Allow registering the global chat (and I guess other chats like the RP chat)
|
||||
if (component == null)
|
||||
component = (ChannelComponent) Component.getComponents().get(ChannelComponent.class);
|
||||
if (component == null)
|
||||
throw new RuntimeException("Attempting to register a channel before the component is registered!");
|
||||
channels.add(channel);
|
||||
Bukkit.getScheduler().runTask(MainPlugin.Instance, () -> Bukkit.getPluginManager().callEvent(new ChatChannelRegisterEvent(channel))); // Wait for server start
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package buttondevteam.component.channel;
|
||||
|
||||
import buttondevteam.lib.architecture.Component;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class ChannelComponent extends Component {
|
||||
@Override
|
||||
protected void register(JavaPlugin plugin) {
|
||||
super.register(plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void unregister(JavaPlugin plugin) {
|
||||
super.unregister(plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void enable() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void disable() {
|
||||
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package buttondevteam.lib.chat;
|
||||
package buttondevteam.component.channel;
|
||||
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
|
@ -1,5 +1,7 @@
|
|||
package buttondevteam.lib.chat;
|
||||
package buttondevteam.component.channel;
|
||||
|
||||
import buttondevteam.lib.chat.Color;
|
||||
import buttondevteam.lib.chat.TBMCChatAPI;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.ArrayList;
|
|
@ -1,15 +0,0 @@
|
|||
package buttondevteam.component.commands;
|
||||
|
||||
import buttondevteam.lib.architecture.Component;
|
||||
|
||||
public class CommandComponent extends Component { //TODO: Do we just move everything here?
|
||||
@Override
|
||||
public void enable() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disable() {
|
||||
|
||||
}
|
||||
}
|
|
@ -9,8 +9,8 @@ public class RestartComponent extends Component {
|
|||
@Override
|
||||
public void enable() {
|
||||
//TODO: Permissions for the commands
|
||||
TBMCChatAPI.AddCommand(getPlugin(), ScheduledRestartCommand.class);
|
||||
TBMCChatAPI.AddCommand(getPlugin(), PrimeRestartCommand.class);
|
||||
TBMCChatAPI.AddCommand(this, new ScheduledRestartCommand());
|
||||
TBMCChatAPI.AddCommand(this, new PrimeRestartCommand());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -6,11 +6,11 @@ import buttondevteam.lib.chat.TBMCChatAPI;
|
|||
public class PluginUpdaterComponent extends Component {
|
||||
@Override
|
||||
public void enable() {
|
||||
TBMCChatAPI.AddCommand(getPlugin(), UpdatePluginCommand.class);
|
||||
TBMCChatAPI.AddCommand(this, new UpdatePluginCommand());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disable() { //TODO: Unregister commands and such
|
||||
public void disable() { //Commands are automatically unregistered
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
package buttondevteam.core;
|
||||
|
||||
import buttondevteam.component.channel.Channel;
|
||||
import buttondevteam.component.channel.ChannelComponent;
|
||||
import buttondevteam.component.channel.ChatRoom;
|
||||
import buttondevteam.component.restart.RestartComponent;
|
||||
import buttondevteam.component.updater.PluginUpdater;
|
||||
import buttondevteam.component.updater.PluginUpdaterComponent;
|
||||
import buttondevteam.lib.TBMCCoreAPI;
|
||||
import buttondevteam.lib.architecture.Component;
|
||||
import buttondevteam.lib.chat.Channel;
|
||||
import buttondevteam.lib.chat.ChatRoom;
|
||||
import buttondevteam.lib.chat.Color;
|
||||
import buttondevteam.lib.chat.TBMCChatAPI;
|
||||
import buttondevteam.lib.player.ChromaGamerBase;
|
||||
|
@ -54,6 +55,7 @@ public class MainPlugin extends JavaPlugin {
|
|||
saveConfig();
|
||||
Component.registerComponent(this, new PluginUpdaterComponent());
|
||||
Component.registerComponent(this, new RestartComponent());
|
||||
Component.registerComponent(this, new ChannelComponent());
|
||||
ComponentManager.enableComponents();
|
||||
TBMCChatAPI.AddCommand(this, MemberCommand.class);
|
||||
TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this);
|
||||
|
@ -62,8 +64,7 @@ public class MainPlugin extends JavaPlugin {
|
|||
ChromaGamerBase.addConverter(sender -> Optional.ofNullable(sender instanceof Player
|
||||
? TBMCPlayer.getPlayer(((Player) sender).getUniqueId(), TBMCPlayer.class) : null)); //Players, has higher priority
|
||||
TBMCCoreAPI.RegisterUserClass(TBMCPlayerBase.class);
|
||||
TBMCChatAPI.RegisterChatChannel(Channel.GlobalChat = new Channel("§fOOC§f", Color.White, "ooc", null));
|
||||
Channel.GlobalChat.IDs = new String[]{"g"}; //Support /g as well
|
||||
TBMCChatAPI.RegisterChatChannel(Channel.GlobalChat = new Channel("§fOOC§f", Color.White, "g", null)); //The /ooc ID has moved to the config
|
||||
TBMCChatAPI.RegisterChatChannel(
|
||||
Channel.AdminChat = new Channel("§cADMIN§f", Color.Red, "a", Channel.inGroupFilter(null)));
|
||||
TBMCChatAPI.RegisterChatChannel(
|
||||
|
|
|
@ -59,6 +59,6 @@ public class PlayerListener implements Listener {
|
|||
if (Arrays.stream(event.getExceptions()).anyMatch("Minecraft"::equalsIgnoreCase))
|
||||
return;
|
||||
Bukkit.getOnlinePlayers().stream().filter(event::shouldSendTo)
|
||||
.forEach(p -> p.sendMessage(event.getChannel().DisplayName.substring(0, 2) + event.getMessage()));
|
||||
.forEach(p -> p.sendMessage(event.getChannel().DisplayName().get().substring(0, 2) + event.getMessage()));
|
||||
}
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
package buttondevteam.core;
|
||||
|
||||
import buttondevteam.lib.chat.Channel;
|
||||
import buttondevteam.component.channel.Channel;
|
||||
import buttondevteam.component.channel.ChannelComponent;
|
||||
import buttondevteam.lib.architecture.Component;
|
||||
import buttondevteam.lib.chat.Color;
|
||||
import buttondevteam.lib.chat.TBMCChatAPI;
|
||||
import org.bukkit.Bukkit;
|
||||
|
@ -38,6 +40,7 @@ public class TestPrepare {
|
|||
return cl.isAssignableFrom(invocation.getMethod().getReturnType());
|
||||
}
|
||||
}));
|
||||
Component.registerComponent(Mockito.mock(MainPlugin.class), new ChannelComponent());
|
||||
TBMCChatAPI.RegisterChatChannel(Channel.GlobalChat = new Channel("§fg§f", Color.White, "g", null));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package buttondevteam.lib;
|
||||
|
||||
import buttondevteam.lib.chat.Channel;
|
||||
import buttondevteam.component.channel.Channel;
|
||||
import buttondevteam.lib.chat.ChatMessage;
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.Delegate;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package buttondevteam.lib;
|
||||
|
||||
import buttondevteam.lib.chat.Channel;
|
||||
import buttondevteam.component.channel.Channel;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package buttondevteam.lib;
|
||||
|
||||
import buttondevteam.lib.chat.Channel;
|
||||
import buttondevteam.component.channel.Channel;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package buttondevteam.lib;
|
||||
|
||||
import buttondevteam.lib.chat.Channel;
|
||||
import buttondevteam.component.channel.Channel;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
|
|
@ -34,7 +34,7 @@ public abstract class Component {
|
|||
private @Getter
|
||||
IHaveConfig config;
|
||||
|
||||
public ConfigData<Boolean> shouldBeEnabled() {
|
||||
public final ConfigData<Boolean> shouldBeEnabled() {
|
||||
return config.getData("enabled", true);
|
||||
}
|
||||
|
||||
|
@ -78,12 +78,15 @@ public abstract class Component {
|
|||
}
|
||||
if (register) {
|
||||
component.plugin = plugin;
|
||||
if (plugin.getConfig() != null) { //Production
|
||||
var compconf = plugin.getConfig().getConfigurationSection("components");
|
||||
if (compconf == null) compconf = plugin.getConfig().createSection("components");
|
||||
component.configSect = compconf.getConfigurationSection(component.getClassName());
|
||||
if (component.configSect == null)
|
||||
component.configSect = compconf.createSection(component.getClassName());
|
||||
component.config = new IHaveConfig(component.configSect);
|
||||
} else //Testing
|
||||
component.config = new IHaveConfig(null);
|
||||
component.register(plugin);
|
||||
components.put(component.getClass(), component);
|
||||
if (ComponentManager.areComponentsEnabled() && component.shouldBeEnabled().get()) {
|
||||
|
@ -179,23 +182,21 @@ public abstract class Component {
|
|||
protected abstract void disable();
|
||||
|
||||
/**
|
||||
* Registers a TBMCCommand to the plugin. Make sure to add it to plugin.yml and use {@link buttondevteam.lib.chat.CommandClass}.
|
||||
* Registers a TBMCCommand to the component. Make sure to add it to plugin.yml and use {@link buttondevteam.lib.chat.CommandClass}.
|
||||
*
|
||||
* @param plugin Main plugin responsible for stuff
|
||||
* @param commandBase Custom coded command class
|
||||
*/
|
||||
protected void registerCommand(JavaPlugin plugin, TBMCCommandBase commandBase) {
|
||||
TBMCChatAPI.AddCommand(plugin, commandBase);
|
||||
protected final void registerCommand(TBMCCommandBase commandBase) {
|
||||
TBMCChatAPI.AddCommand(this, commandBase);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a Listener to this plugin
|
||||
* Registers a Listener to this component
|
||||
*
|
||||
* @param plugin Main plugin responsible for stuff
|
||||
* @param listener The event listener to register
|
||||
* @return The provided listener
|
||||
*/
|
||||
protected Listener registerListener(JavaPlugin plugin, Listener listener) {
|
||||
protected final Listener registerListener(Listener listener) {
|
||||
TBMCCoreAPI.RegisterEventsForExceptions(listener, plugin);
|
||||
return listener;
|
||||
}
|
||||
|
|
|
@ -15,6 +15,9 @@ import java.util.function.Function;
|
|||
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
|
||||
//@AllArgsConstructor(access = AccessLevel.PACKAGE)
|
||||
public class ConfigData<T> { //TODO: Save after a while
|
||||
/**
|
||||
* May be null for testing
|
||||
*/
|
||||
private final ConfigurationSection config;
|
||||
private final String path;
|
||||
private @Nullable final T def;
|
||||
|
@ -46,14 +49,14 @@ public class ConfigData<T> { //TODO: Save after a while
|
|||
@SuppressWarnings("unchecked")
|
||||
public T get() {
|
||||
if (value != null) return value; //Speed things up
|
||||
Object val = config.get(path);
|
||||
Object val = config == null ? null : config.get(path); //config==null: testing
|
||||
if (val == null) {
|
||||
val = primitiveDef;
|
||||
}
|
||||
if (val == primitiveDef && !saved) {
|
||||
if (def != null)
|
||||
set(def); //Save default value
|
||||
else
|
||||
else if (config != null) //config==null: testing
|
||||
config.set(path, primitiveDef);
|
||||
saved = true;
|
||||
}
|
||||
|
@ -70,7 +73,8 @@ public class ConfigData<T> { //TODO: Save after a while
|
|||
if (setter != null)
|
||||
val = setter.apply(value);
|
||||
else val = value;
|
||||
if (config != null)
|
||||
config.set(path, val);
|
||||
this.value =value;
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,6 +12,11 @@ public final class IHaveConfig {
|
|||
private final HashMap<String, ConfigData<?>> datamap = new HashMap<>();
|
||||
private ConfigurationSection config;
|
||||
|
||||
/**
|
||||
* May be used in testing
|
||||
*
|
||||
* @param section May be null for testing
|
||||
*/
|
||||
IHaveConfig(ConfigurationSection section) {
|
||||
config = section;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package buttondevteam.lib.chat;
|
||||
|
||||
import buttondevteam.component.channel.Channel;
|
||||
import buttondevteam.component.channel.Channel.RecipientTestResult;
|
||||
import buttondevteam.core.CommandCaller;
|
||||
import buttondevteam.core.MainPlugin;
|
||||
import buttondevteam.lib.TBMCChatEvent;
|
||||
|
@ -7,7 +9,6 @@ import buttondevteam.lib.TBMCChatPreprocessEvent;
|
|||
import buttondevteam.lib.TBMCCoreAPI;
|
||||
import buttondevteam.lib.TBMCSystemChatEvent;
|
||||
import buttondevteam.lib.architecture.Component;
|
||||
import buttondevteam.lib.chat.Channel.RecipientTestResult;
|
||||
import lombok.val;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
@ -130,7 +131,7 @@ public class TBMCChatAPI {
|
|||
|
||||
/**
|
||||
* <p>
|
||||
* This method adds a plugin's command to help and sets it's executor.
|
||||
* This method adds a plugin's command to help and sets it's executor. They will be automatically unregistered on plugin disable.
|
||||
* </p>
|
||||
* <p>
|
||||
* The <u>command must be registered</u> in the caller plugin's plugin.yml. Otherwise the plugin will output a messsage to console.
|
||||
|
@ -163,7 +164,7 @@ public class TBMCChatAPI {
|
|||
|
||||
/**
|
||||
* <p>
|
||||
* This method adds a plugin's command to help and sets its executor.
|
||||
* This method adds a plugin's command to help and sets its executor. They will be automatically unregistered on plugin disable.
|
||||
* </p>
|
||||
* <p>
|
||||
* The <u>command must be registered</u> in the caller plugin's plugin.yml. Otherwise the plugin will output a message to console.
|
||||
|
@ -191,7 +192,7 @@ public class TBMCChatAPI {
|
|||
|
||||
/**
|
||||
* <p>
|
||||
* This method adds a plugin's command to help and sets its executor.
|
||||
* This method adds a plugin's command to help and sets its executor. They will be automatically unregistered on component disable.
|
||||
* </p>
|
||||
* <p>
|
||||
* The <u>command must be registered</u> in the caller plugin's plugin.yml. Otherwise the plugin will output a message to console.
|
||||
|
@ -277,7 +278,7 @@ public class TBMCChatAPI {
|
|||
*/
|
||||
public static boolean SendChatMessage(ChatMessage cm, Channel channel) {
|
||||
if (!Channel.getChannels().contains(channel))
|
||||
throw new RuntimeException("Channel " + channel.DisplayName + " not registered!");
|
||||
throw new RuntimeException("Channel " + channel.DisplayName().get() + " not registered!");
|
||||
val permcheck = cm.getPermCheck();
|
||||
RecipientTestResult rtr = getScoreOrSendError(channel, permcheck);
|
||||
int score = rtr.score;
|
||||
|
@ -308,7 +309,7 @@ public class TBMCChatAPI {
|
|||
*/
|
||||
public static boolean SendSystemMessage(Channel channel, RecipientTestResult rtr, String message, String... exceptions) {
|
||||
if (!Channel.getChannels().contains(channel))
|
||||
throw new RuntimeException("Channel " + channel.DisplayName + " not registered!");
|
||||
throw new RuntimeException("Channel " + channel.DisplayName().get() + " not registered!");
|
||||
TBMCSystemChatEvent event = new TBMCSystemChatEvent(channel, message, rtr.score, rtr.groupID, exceptions);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
return event.isCancelled();
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package buttondevteam.lib.player;
|
||||
|
||||
import buttondevteam.lib.chat.Channel;
|
||||
import buttondevteam.component.channel.Channel;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
public class ChannelPlayerData { //I just want this to work
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package buttondevteam.lib.player;
|
||||
|
||||
import buttondevteam.component.channel.Channel;
|
||||
import buttondevteam.lib.TBMCCoreAPI;
|
||||
import buttondevteam.lib.chat.Channel;
|
||||
import com.google.common.collect.HashBiMap;
|
||||
import lombok.val;
|
||||
import org.bukkit.Bukkit;
|
||||
|
|
Loading…
Reference in a new issue