New command system improvements, broadcast toggles, config fixes #62

Merged
NorbiPeti merged 23 commits from dev into master 2019-03-17 01:27:43 +00:00
9 changed files with 32 additions and 15 deletions
Showing only changes of commit 95796a1b3e - Show all commits

View file

@ -1,5 +1,6 @@
package buttondevteam.core.component.channel;
import buttondevteam.lib.TBMCSystemChatEvent;
import buttondevteam.lib.chat.Color;
import buttondevteam.lib.chat.TBMCChatAPI;
import org.bukkit.command.CommandSender;
@ -17,7 +18,7 @@ public class ChatRoom extends Channel {
public void joinRoom(CommandSender sender) {
usersInRoom.add(sender);
TBMCChatAPI.SendSystemMessage(this, RecipientTestResult.ALL, sender.getName() + " joined the room", ChannelComponent.roomJoinLeave);
TBMCChatAPI.SendSystemMessage(this, RecipientTestResult.ALL, sender.getName() + " joined the room", TBMCSystemChatEvent.BroadcastTarget.ALL); //Always show message in the same kind of channel
}
public void leaveRoom(CommandSender sender) {

View file

@ -1,26 +1,31 @@
package buttondevteam.core.component.restart;
import buttondevteam.core.component.channel.Channel;
import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.chat.TBMCCommandBase;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
@CommandClass(path = "primerestart", modOnly = true)
@RequiredArgsConstructor
public class PrimeRestartCommand extends TBMCCommandBase {
private final RestartComponent component;
@Override
public boolean OnCommand(CommandSender sender, String alias, String[] args) {
loud = args.length > 0;
if (Bukkit.getOnlinePlayers().size() > 0) {
sender.sendMessage("§bPlayers online, restart delayed.");
if (loud)
Bukkit.broadcastMessage(ChatColor.DARK_RED + "The server will restart as soon as nobody is online.");
TBMCChatAPI.SendSystemMessage(Channel.GlobalChat, Channel.RecipientTestResult.ALL, ChatColor.DARK_RED + "The server will restart as soon as nobody is online.", component.restartBroadcast);
plsrestart = true;
} else {
sender.sendMessage("§bNobody is online. Restarting now.");
if (loud)
Bukkit.broadcastMessage("§cNobody is online. Restarting server.");
TBMCChatAPI.SendSystemMessage(Channel.GlobalChat, Channel.RecipientTestResult.ALL, "§cNobody is online. Restarting server.", component.restartBroadcast);
Bukkit.spigot().restart();
}
return true;

View file

@ -1,5 +1,7 @@
package buttondevteam.core.component.restart;
import buttondevteam.core.component.channel.Channel;
import buttondevteam.lib.TBMCSystemChatEvent;
import buttondevteam.lib.architecture.Component;
import buttondevteam.lib.chat.IFakePlayer;
import buttondevteam.lib.chat.TBMCChatAPI;
@ -13,17 +15,19 @@ public class RestartComponent extends Component implements Listener {
@Override
public void enable() {
//TODO: Permissions for the commands
TBMCChatAPI.AddCommand(this, new ScheduledRestartCommand());
TBMCChatAPI.AddCommand(this, new PrimeRestartCommand());
TBMCChatAPI.AddCommand(this, new ScheduledRestartCommand(this));
TBMCChatAPI.AddCommand(this, new PrimeRestartCommand(this));
registerListener(this);
restartBroadcast = TBMCSystemChatEvent.BroadcastTarget.add("restartCountdown");
}
@Override
public void disable() {
TBMCSystemChatEvent.BroadcastTarget.remove(restartBroadcast);
}
private long lasttime = 0;
TBMCSystemChatEvent.BroadcastTarget restartBroadcast;
@EventHandler
public void onPlayerLeave(PlayerQuitEvent event) {
@ -32,12 +36,12 @@ public class RestartComponent extends Component implements Listener {
&& !event.getQuitMessage().equalsIgnoreCase("Server is restarting")) {
if (Bukkit.getOnlinePlayers().size() <= 1) {
if (PrimeRestartCommand.isLoud())
Bukkit.broadcastMessage("§cNobody is online anymore. Restarting.");
TBMCChatAPI.SendSystemMessage(Channel.GlobalChat, Channel.RecipientTestResult.ALL, "§cNobody is online anymore. Restarting.", restartBroadcast);
Bukkit.spigot().restart();
} else if (!(event.getPlayer() instanceof IFakePlayer) && System.nanoTime() - 10 * 1000000000L - lasttime > 0) { //Ten seconds passed since last reminder
lasttime = System.nanoTime();
if (PrimeRestartCommand.isLoud())
Bukkit.broadcastMessage(ChatColor.DARK_RED + "The server will restart as soon as nobody is online.");
TBMCChatAPI.SendSystemMessage(Channel.GlobalChat, Channel.RecipientTestResult.ALL, ChatColor.DARK_RED + "The server will restart as soon as nobody is online.", restartBroadcast);
}
}
}

View file

@ -1,10 +1,13 @@
package buttondevteam.core.component.restart;
import buttondevteam.core.MainPlugin;
import buttondevteam.core.component.channel.Channel;
import buttondevteam.lib.ScheduledServerRestartEvent;
import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.chat.TBMCCommandBase;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.boss.BarColor;
@ -15,12 +18,14 @@ import org.bukkit.command.CommandSender;
import org.bukkit.scheduler.BukkitTask;
@CommandClass(modOnly = true, path = "schrestart")
@RequiredArgsConstructor
public class ScheduledRestartCommand extends TBMCCommandBase {
@Getter
@Setter
private int restartCounter;
private BukkitTask restarttask;
private volatile BossBar restartbar;
private final RestartComponent component;
@Override
public boolean OnCommand(CommandSender sender, String alias, String[] args) {
@ -51,7 +56,7 @@ public class ScheduledRestartCommand extends TBMCCommandBase {
Bukkit.spigot().restart();
}
if (restartCounter % 200 == 0)
Bukkit.broadcastMessage("§c-- The server is restarting in " + restartCounter / 20 + " seconds! (/press)");
TBMCChatAPI.SendSystemMessage(Channel.GlobalChat, Channel.RecipientTestResult.ALL, "§c-- The server is restarting in " + restartCounter / 20 + " seconds! (/press)", component.restartBroadcast);
restartbar.setProgress(restartCounter / (double) restarttime);
restartbar.setTitle(String.format("Server restart in %.2f", restartCounter / 20f));
restartCounter--;

View file

@ -64,7 +64,7 @@ public class TBMCSystemChatEvent extends TBMCChatEventBase {
@Nullable
public static BroadcastTarget get(String name) {
return targets.stream().filter(bt -> bt.name.equals(name)).findAny().orElse(null);
return targets.stream().filter(bt -> bt.name.equalsIgnoreCase(name)).findAny().orElse(null);
}
public static Stream<BroadcastTarget> stream() {

View file

@ -16,6 +16,8 @@ public abstract class ButtonPlugin extends JavaPlugin {
private static Command2MC command2MC = new Command2MC();
@Getter(AccessLevel.PROTECTED)
private IHaveConfig iConfig;
@Getter(AccessLevel.PROTECTED)
private IHaveConfig data; //TODO
/**
* Used to unregister components in the right order
*/

View file

@ -31,6 +31,7 @@ public abstract class Component {
@NonNull
private @Getter
IHaveConfig config;
private @Getter IHaveConfig data; //TODO
public final ConfigData<Boolean> shouldBeEnabled() {
return config.getData("enabled", true);

View file

@ -181,7 +181,9 @@ public abstract class Command2<TC extends ICommand2, TP extends Command2Sender>
TBMCCoreAPI.SendException("Could not register default handler for command /" + path, e);
}
for (val method : command.getClass().getMethods()) {
var ht = command.getHelpText(method);
val ann = method.getAnnotation(Subcommand.class);
if (ann == null) continue; //Don't call the method on non-subcommands because they're not in the yaml
var ht = command.getHelpText(method, ann);
if (ht != null) {
val subcommand = commandChar + path + //Add command path (class name by default)
(method.getName().equals("def") ? "" : " " + method.getName().replace('_', ' ').toLowerCase()); //Add method name, unless it's 'def'

View file

@ -37,10 +37,7 @@ public abstract class ICommand2<TP extends Command2Sender> {
* @param method The method of the subcommand
* @return The help text, empty array or null
*/
public String[] getHelpText(Method method) {
val ann = method.getAnnotation(Command2.Subcommand.class);
if (ann == null)
return null;
public String[] getHelpText(Method method, Command2.Subcommand ann) {
val cc = getClass().getAnnotation(CommandClass.class);
return ann.helpText().length != 0 || cc == null ? ann.helpText() : cc.helpText(); //If cc is null then it's empty array
}