Command fix, using the brtoggles

DiscordPlugin#89
Started #60
Should have committed this earlier
This commit is contained in:
Norbi Peti 2019-02-17 02:22:06 +01:00
parent a91786cf0d
commit 95796a1b3e
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
9 changed files with 32 additions and 15 deletions

View file

@ -1,5 +1,6 @@
package buttondevteam.core.component.channel; package buttondevteam.core.component.channel;
import buttondevteam.lib.TBMCSystemChatEvent;
import buttondevteam.lib.chat.Color; import buttondevteam.lib.chat.Color;
import buttondevteam.lib.chat.TBMCChatAPI; import buttondevteam.lib.chat.TBMCChatAPI;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -17,7 +18,7 @@ public class ChatRoom extends Channel {
public void joinRoom(CommandSender sender) { public void joinRoom(CommandSender sender) {
usersInRoom.add(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) { public void leaveRoom(CommandSender sender) {

View file

@ -1,26 +1,31 @@
package buttondevteam.core.component.restart; package buttondevteam.core.component.restart;
import buttondevteam.core.component.channel.Channel;
import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.chat.TBMCCommandBase; import buttondevteam.lib.chat.TBMCCommandBase;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@CommandClass(path = "primerestart", modOnly = true) @CommandClass(path = "primerestart", modOnly = true)
@RequiredArgsConstructor
public class PrimeRestartCommand extends TBMCCommandBase { public class PrimeRestartCommand extends TBMCCommandBase {
private final RestartComponent component;
@Override @Override
public boolean OnCommand(CommandSender sender, String alias, String[] args) { public boolean OnCommand(CommandSender sender, String alias, String[] args) {
loud = args.length > 0; loud = args.length > 0;
if (Bukkit.getOnlinePlayers().size() > 0) { if (Bukkit.getOnlinePlayers().size() > 0) {
sender.sendMessage("§bPlayers online, restart delayed."); sender.sendMessage("§bPlayers online, restart delayed.");
if (loud) 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; plsrestart = true;
} else { } else {
sender.sendMessage("§bNobody is online. Restarting now."); sender.sendMessage("§bNobody is online. Restarting now.");
if (loud) 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(); Bukkit.spigot().restart();
} }
return true; return true;

View file

@ -1,5 +1,7 @@
package buttondevteam.core.component.restart; package buttondevteam.core.component.restart;
import buttondevteam.core.component.channel.Channel;
import buttondevteam.lib.TBMCSystemChatEvent;
import buttondevteam.lib.architecture.Component; import buttondevteam.lib.architecture.Component;
import buttondevteam.lib.chat.IFakePlayer; import buttondevteam.lib.chat.IFakePlayer;
import buttondevteam.lib.chat.TBMCChatAPI; import buttondevteam.lib.chat.TBMCChatAPI;
@ -13,17 +15,19 @@ public class RestartComponent extends Component implements Listener {
@Override @Override
public void enable() { public void enable() {
//TODO: Permissions for the commands //TODO: Permissions for the commands
TBMCChatAPI.AddCommand(this, new ScheduledRestartCommand()); TBMCChatAPI.AddCommand(this, new ScheduledRestartCommand(this));
TBMCChatAPI.AddCommand(this, new PrimeRestartCommand()); TBMCChatAPI.AddCommand(this, new PrimeRestartCommand(this));
registerListener(this); registerListener(this);
restartBroadcast = TBMCSystemChatEvent.BroadcastTarget.add("restartCountdown");
} }
@Override @Override
public void disable() { public void disable() {
TBMCSystemChatEvent.BroadcastTarget.remove(restartBroadcast);
} }
private long lasttime = 0; private long lasttime = 0;
TBMCSystemChatEvent.BroadcastTarget restartBroadcast;
@EventHandler @EventHandler
public void onPlayerLeave(PlayerQuitEvent event) { public void onPlayerLeave(PlayerQuitEvent event) {
@ -32,12 +36,12 @@ public class RestartComponent extends Component implements Listener {
&& !event.getQuitMessage().equalsIgnoreCase("Server is restarting")) { && !event.getQuitMessage().equalsIgnoreCase("Server is restarting")) {
if (Bukkit.getOnlinePlayers().size() <= 1) { if (Bukkit.getOnlinePlayers().size() <= 1) {
if (PrimeRestartCommand.isLoud()) 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(); Bukkit.spigot().restart();
} else if (!(event.getPlayer() instanceof IFakePlayer) && System.nanoTime() - 10 * 1000000000L - lasttime > 0) { //Ten seconds passed since last reminder } else if (!(event.getPlayer() instanceof IFakePlayer) && System.nanoTime() - 10 * 1000000000L - lasttime > 0) { //Ten seconds passed since last reminder
lasttime = System.nanoTime(); lasttime = System.nanoTime();
if (PrimeRestartCommand.isLoud()) 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; package buttondevteam.core.component.restart;
import buttondevteam.core.MainPlugin; import buttondevteam.core.MainPlugin;
import buttondevteam.core.component.channel.Channel;
import buttondevteam.lib.ScheduledServerRestartEvent; import buttondevteam.lib.ScheduledServerRestartEvent;
import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.chat.TBMCCommandBase; import buttondevteam.lib.chat.TBMCCommandBase;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter; import lombok.Setter;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.boss.BarColor; import org.bukkit.boss.BarColor;
@ -15,12 +18,14 @@ import org.bukkit.command.CommandSender;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
@CommandClass(modOnly = true, path = "schrestart") @CommandClass(modOnly = true, path = "schrestart")
@RequiredArgsConstructor
public class ScheduledRestartCommand extends TBMCCommandBase { public class ScheduledRestartCommand extends TBMCCommandBase {
@Getter @Getter
@Setter @Setter
private int restartCounter; private int restartCounter;
private BukkitTask restarttask; private BukkitTask restarttask;
private volatile BossBar restartbar; private volatile BossBar restartbar;
private final RestartComponent component;
@Override @Override
public boolean OnCommand(CommandSender sender, String alias, String[] args) { public boolean OnCommand(CommandSender sender, String alias, String[] args) {
@ -51,7 +56,7 @@ public class ScheduledRestartCommand extends TBMCCommandBase {
Bukkit.spigot().restart(); Bukkit.spigot().restart();
} }
if (restartCounter % 200 == 0) 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.setProgress(restartCounter / (double) restarttime);
restartbar.setTitle(String.format("Server restart in %.2f", restartCounter / 20f)); restartbar.setTitle(String.format("Server restart in %.2f", restartCounter / 20f));
restartCounter--; restartCounter--;

View file

@ -64,7 +64,7 @@ public class TBMCSystemChatEvent extends TBMCChatEventBase {
@Nullable @Nullable
public static BroadcastTarget get(String name) { 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() { public static Stream<BroadcastTarget> stream() {

View file

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

View file

@ -31,6 +31,7 @@ public abstract class Component {
@NonNull @NonNull
private @Getter private @Getter
IHaveConfig config; IHaveConfig config;
private @Getter IHaveConfig data; //TODO
public final ConfigData<Boolean> shouldBeEnabled() { public final ConfigData<Boolean> shouldBeEnabled() {
return config.getData("enabled", true); 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); TBMCCoreAPI.SendException("Could not register default handler for command /" + path, e);
} }
for (val method : command.getClass().getMethods()) { 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) { if (ht != null) {
val subcommand = commandChar + path + //Add command path (class name by default) 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' (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 * @param method The method of the subcommand
* @return The help text, empty array or null * @return The help text, empty array or null
*/ */
public String[] getHelpText(Method method) { public String[] getHelpText(Method method, Command2.Subcommand ann) {
val ann = method.getAnnotation(Command2.Subcommand.class);
if (ann == null)
return null;
val cc = getClass().getAnnotation(CommandClass.class); 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 return ann.helpText().length != 0 || cc == null ? ann.helpText() : cc.helpText(); //If cc is null then it's empty array
} }