Exclude plugins from syschat event
This commit is contained in:
parent
7f385df3b1
commit
bb31f4e378
4 changed files with 364 additions and 343 deletions
15
.editorconfig
Normal file
15
.editorconfig
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
[*]
|
||||||
|
charset=utf-8
|
||||||
|
end_of_line=lf
|
||||||
|
insert_final_newline=false
|
||||||
|
indent_style=space
|
||||||
|
indent_size=4
|
||||||
|
|
||||||
|
[*.java]
|
||||||
|
indent_style=tab
|
||||||
|
tab_width=4
|
||||||
|
|
||||||
|
[{*.yml,*.yaml}]
|
||||||
|
indent_style=space
|
||||||
|
indent_size=2
|
||||||
|
|
|
@ -1,61 +1,64 @@
|
||||||
package buttondevteam.core;
|
package buttondevteam.core;
|
||||||
|
|
||||||
import buttondevteam.lib.TBMCSystemChatEvent;
|
import buttondevteam.lib.TBMCSystemChatEvent;
|
||||||
import buttondevteam.lib.chat.IFakePlayer;
|
import buttondevteam.lib.chat.IFakePlayer;
|
||||||
import buttondevteam.lib.player.TBMCPlayerBase;
|
import buttondevteam.lib.player.TBMCPlayerBase;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.Statistic;
|
import org.bukkit.Statistic;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.player.PlayerJoinEvent;
|
import org.bukkit.event.player.PlayerJoinEvent;
|
||||||
import org.bukkit.event.player.PlayerQuitEvent;
|
import org.bukkit.event.player.PlayerQuitEvent;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.Date;
|
import java.util.Arrays;
|
||||||
|
import java.util.Date;
|
||||||
import static buttondevteam.core.MainPlugin.permission;
|
|
||||||
|
import static buttondevteam.core.MainPlugin.permission;
|
||||||
public class PlayerListener implements Listener {
|
|
||||||
|
public class PlayerListener implements Listener {
|
||||||
@EventHandler(priority = EventPriority.NORMAL)
|
|
||||||
public void OnPlayerJoin(PlayerJoinEvent event) {
|
@EventHandler(priority = EventPriority.NORMAL)
|
||||||
TBMCPlayerBase.joinPlayer(event.getPlayer());
|
public void OnPlayerJoin(PlayerJoinEvent event) {
|
||||||
if (permission != null && !permission.playerInGroup(event.getPlayer(), "member")
|
TBMCPlayerBase.joinPlayer(event.getPlayer());
|
||||||
&& (new Date(event.getPlayer().getFirstPlayed()).toInstant().plus(7, ChronoUnit.DAYS).isBefore(Instant.now())
|
if (permission != null && !permission.playerInGroup(event.getPlayer(), "member")
|
||||||
|| event.getPlayer().getStatistic(Statistic.PLAY_ONE_TICK) > 20 * 3600 * 12)) {
|
&& (new Date(event.getPlayer().getFirstPlayed()).toInstant().plus(7, ChronoUnit.DAYS).isBefore(Instant.now())
|
||||||
permission.playerAddGroup(null, event.getPlayer(), "member");
|
|| event.getPlayer().getStatistic(Statistic.PLAY_ONE_TICK) > 20 * 3600 * 12)) {
|
||||||
event.getPlayer().sendMessage("§bYou are a member now. YEEHAW");
|
permission.playerAddGroup(null, event.getPlayer(), "member");
|
||||||
MainPlugin.Instance.getLogger().info("Added " + event.getPlayer().getName() + " as a member.");
|
event.getPlayer().sendMessage("§bYou are a member now. YEEHAW");
|
||||||
}
|
MainPlugin.Instance.getLogger().info("Added " + event.getPlayer().getName() + " as a member.");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private long lasttime = 0;
|
|
||||||
@EventHandler(priority = EventPriority.NORMAL)
|
private long lasttime = 0;
|
||||||
public void OnPlayerLeave(PlayerQuitEvent event) {
|
@EventHandler(priority = EventPriority.NORMAL)
|
||||||
TBMCPlayerBase.quitPlayer(event.getPlayer());
|
public void OnPlayerLeave(PlayerQuitEvent event) {
|
||||||
if (PrimeRestartCommand.isPlsrestart()
|
TBMCPlayerBase.quitPlayer(event.getPlayer());
|
||||||
&& !event.getQuitMessage().equalsIgnoreCase("Server closed")
|
if (PrimeRestartCommand.isPlsrestart()
|
||||||
&& !event.getQuitMessage().equalsIgnoreCase("Server is restarting")) {
|
&& !event.getQuitMessage().equalsIgnoreCase("Server closed")
|
||||||
if (Bukkit.getOnlinePlayers().size() <= 1) {
|
&& !event.getQuitMessage().equalsIgnoreCase("Server is restarting")) {
|
||||||
if (PrimeRestartCommand.isLoud())
|
if (Bukkit.getOnlinePlayers().size() <= 1) {
|
||||||
Bukkit.broadcastMessage("§cNobody is online anymore. Restarting.");
|
if (PrimeRestartCommand.isLoud())
|
||||||
Bukkit.spigot().restart();
|
Bukkit.broadcastMessage("§cNobody is online anymore. Restarting.");
|
||||||
} else if (!(event.getPlayer() instanceof IFakePlayer) && System.nanoTime() - 10 * 1000000000L - lasttime > 0) { //Ten seconds passed since last reminder
|
Bukkit.spigot().restart();
|
||||||
lasttime = System.nanoTime();
|
} else if (!(event.getPlayer() instanceof IFakePlayer) && System.nanoTime() - 10 * 1000000000L - lasttime > 0) { //Ten seconds passed since last reminder
|
||||||
if (PrimeRestartCommand.isLoud())
|
lasttime = System.nanoTime();
|
||||||
Bukkit.broadcastMessage(ChatColor.DARK_RED + "The server will restart as soon as nobody is online.");
|
if (PrimeRestartCommand.isLoud())
|
||||||
}
|
Bukkit.broadcastMessage(ChatColor.DARK_RED + "The server will restart as soon as nobody is online.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@EventHandler(priority = EventPriority.HIGHEST)
|
|
||||||
public void onSystemChat(TBMCSystemChatEvent event) {
|
@EventHandler(priority = EventPriority.HIGHEST)
|
||||||
if (event.isHandled())
|
public void onSystemChat(TBMCSystemChatEvent event) {
|
||||||
return; // Only handle here if ButtonChat couldn't
|
if (event.isHandled())
|
||||||
Bukkit.getOnlinePlayers().stream().filter(event::shouldSendTo)
|
return; // Only handle here if ButtonChat couldn't
|
||||||
.forEach(p -> p.sendMessage(event.getChannel().DisplayName.substring(0, 2) + event.getMessage()));
|
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()));
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -13,14 +13,16 @@ import org.bukkit.event.HandlerList;
|
||||||
*/
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
public class TBMCSystemChatEvent extends TBMCChatEventBase {
|
public class TBMCSystemChatEvent extends TBMCChatEventBase {
|
||||||
|
private final String[] exceptions;
|
||||||
private boolean handled;
|
private boolean handled;
|
||||||
|
|
||||||
public void setHandled() {
|
public void setHandled() {
|
||||||
handled = true;
|
handled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TBMCSystemChatEvent(Channel channel, String message, int score, String groupid) { // TODO: Rich message
|
public TBMCSystemChatEvent(Channel channel, String message, int score, String groupid, String[] exceptions) { // TODO: Rich message
|
||||||
super(channel, message, score, groupid);
|
super(channel, message, score, groupid);
|
||||||
|
this.exceptions = exceptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final HandlerList handlers = new HandlerList();
|
private static final HandlerList handlers = new HandlerList();
|
||||||
|
|
|
@ -1,282 +1,283 @@
|
||||||
package buttondevteam.lib.chat;
|
package buttondevteam.lib.chat;
|
||||||
|
|
||||||
import buttondevteam.core.CommandCaller;
|
import buttondevteam.core.CommandCaller;
|
||||||
import buttondevteam.core.MainPlugin;
|
import buttondevteam.core.MainPlugin;
|
||||||
import buttondevteam.lib.TBMCChatEvent;
|
import buttondevteam.lib.TBMCChatEvent;
|
||||||
import buttondevteam.lib.TBMCChatPreprocessEvent;
|
import buttondevteam.lib.TBMCChatPreprocessEvent;
|
||||||
import buttondevteam.lib.TBMCCoreAPI;
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
import buttondevteam.lib.TBMCSystemChatEvent;
|
import buttondevteam.lib.TBMCSystemChatEvent;
|
||||||
import buttondevteam.lib.chat.Channel.RecipientTestResult;
|
import buttondevteam.lib.chat.Channel.RecipientTestResult;
|
||||||
import lombok.val;
|
import lombok.val;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
import org.reflections.Reflections;
|
import org.reflections.Reflections;
|
||||||
import org.reflections.scanners.SubTypesScanner;
|
import org.reflections.scanners.SubTypesScanner;
|
||||||
import org.reflections.util.ClasspathHelper;
|
import org.reflections.util.ClasspathHelper;
|
||||||
import org.reflections.util.ConfigurationBuilder;
|
import org.reflections.util.ConfigurationBuilder;
|
||||||
|
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class TBMCChatAPI {
|
public class TBMCChatAPI {
|
||||||
|
|
||||||
private static final HashMap<String, TBMCCommandBase> commands = new HashMap<>();
|
private static final HashMap<String, TBMCCommandBase> commands = new HashMap<>();
|
||||||
|
|
||||||
public static HashMap<String, TBMCCommandBase> GetCommands() {
|
public static HashMap<String, TBMCCommandBase> GetCommands() {
|
||||||
return commands;
|
return commands;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns messages formatted for Minecraft chat listing the subcommands of the command.
|
* Returns messages formatted for Minecraft chat listing the subcommands of the command.
|
||||||
*
|
*
|
||||||
* @param command
|
* @param command
|
||||||
* The command which we want the subcommands of
|
* The command which we want the subcommands of
|
||||||
* @param sender
|
* @param sender
|
||||||
* The sender for permissions
|
* The sender for permissions
|
||||||
* @return The subcommands
|
* @return The subcommands
|
||||||
*/
|
*/
|
||||||
public static String[] GetSubCommands(TBMCCommandBase command, CommandSender sender) {
|
public static String[] GetSubCommands(TBMCCommandBase command, CommandSender sender) {
|
||||||
return GetSubCommands(command.GetCommandPath(), sender);
|
return GetSubCommands(command.GetCommandPath(), sender);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns messages formatted for Minecraft chat listing the subcommands of the command.<br>
|
* Returns messages formatted for Minecraft chat listing the subcommands of the command.<br>
|
||||||
* Returns a header if subcommands were found, otherwise returns an empty array.
|
* Returns a header if subcommands were found, otherwise returns an empty array.
|
||||||
*
|
*
|
||||||
* @param command
|
* @param command
|
||||||
* The command which we want the subcommands of
|
* The command which we want the subcommands of
|
||||||
* @param sender
|
* @param sender
|
||||||
* The sender for permissions
|
* The sender for permissions
|
||||||
* @return The subcommands
|
* @return The subcommands
|
||||||
*/
|
*/
|
||||||
public static String[] GetSubCommands(String command, CommandSender sender) {
|
public static String[] GetSubCommands(String command, CommandSender sender) {
|
||||||
ArrayList<String> cmds = new ArrayList<String>();
|
ArrayList<String> cmds = new ArrayList<String>();
|
||||||
Consumer<String> addToCmds = cmd -> {
|
Consumer<String> addToCmds = cmd -> {
|
||||||
if (cmds.size() == 0)
|
if (cmds.size() == 0)
|
||||||
cmds.add("§6---- Subcommands ----");
|
cmds.add("§6---- Subcommands ----");
|
||||||
cmds.add(cmd);
|
cmds.add(cmd);
|
||||||
};
|
};
|
||||||
for (Entry<String, TBMCCommandBase> cmd : TBMCChatAPI.GetCommands().entrySet()) {
|
for (Entry<String, TBMCCommandBase> cmd : TBMCChatAPI.GetCommands().entrySet()) {
|
||||||
if (cmd.getKey().startsWith(command + " ")) {
|
if (cmd.getKey().startsWith(command + " ")) {
|
||||||
if (cmd.getValue().isPlayerOnly() && !(sender instanceof Player))
|
if (cmd.getValue().isPlayerOnly() && !(sender instanceof Player))
|
||||||
continue;
|
continue;
|
||||||
if (cmd.getValue().isModOnly() && (MainPlugin.permission != null ? !MainPlugin.permission.has(sender, "tbmc.admin") : !sender.isOp()))
|
if (cmd.getValue().isModOnly() && (MainPlugin.permission != null ? !MainPlugin.permission.has(sender, "tbmc.admin") : !sender.isOp()))
|
||||||
continue;
|
continue;
|
||||||
int ind = cmd.getKey().indexOf(' ', command.length() + 2);
|
int ind = cmd.getKey().indexOf(' ', command.length() + 2);
|
||||||
if (ind >= 0) {
|
if (ind >= 0) {
|
||||||
String newcmd = cmd.getKey().substring(0, ind);
|
String newcmd = cmd.getKey().substring(0, ind);
|
||||||
if (!cmds.contains("/" + newcmd))
|
if (!cmds.contains("/" + newcmd))
|
||||||
addToCmds.accept("/" + newcmd);
|
addToCmds.accept("/" + newcmd);
|
||||||
} else
|
} else
|
||||||
addToCmds.accept("/" + cmd.getKey());
|
addToCmds.accept("/" + cmd.getKey());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cmds.toArray(new String[0]); //Apparently it's faster to use an empty array in modern Java
|
return cmds.toArray(new String[0]); //Apparently it's faster to use an empty array in modern Java
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* This method adds a plugin's commands to help and sets their executor.
|
* This method adds a plugin's commands to help and sets their executor.
|
||||||
* </p>
|
* </p>
|
||||||
* <p>
|
* <p>
|
||||||
* </p>
|
* </p>
|
||||||
* <b>The command classes have to have a constructor each with no parameters</b>
|
* <b>The command classes have to have a constructor each with no parameters</b>
|
||||||
* <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.
|
* The <u>command must be registered</u> in the caller plugin's plugin.yml. Otherwise the plugin will output a messsage to console.
|
||||||
* </p>
|
* </p>
|
||||||
* <p>
|
* <p>
|
||||||
* <i>Using this method after the server is done loading will have no effect.</i>
|
* <i>Using this method after the server is done loading will have no effect.</i>
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param plugin
|
* @param plugin
|
||||||
* The caller plugin
|
* The caller plugin
|
||||||
* @param acmdclass
|
* @param acmdclass
|
||||||
* A command's class to get the package name for commands. The provided class's package and subpackages are scanned for commands.
|
* A command's class to get the package name for commands. The provided class's package and subpackages are scanned for commands.
|
||||||
*/
|
*/
|
||||||
public static synchronized void AddCommands(JavaPlugin plugin, Class<? extends TBMCCommandBase> acmdclass) {
|
public static synchronized void AddCommands(JavaPlugin plugin, Class<? extends TBMCCommandBase> acmdclass) {
|
||||||
plugin.getLogger().info("Registering commands from " + acmdclass.getPackage().getName());
|
plugin.getLogger().info("Registering commands from " + acmdclass.getPackage().getName());
|
||||||
Reflections rf = new Reflections(new ConfigurationBuilder()
|
Reflections rf = new Reflections(new ConfigurationBuilder()
|
||||||
.setUrls(ClasspathHelper.forPackage(acmdclass.getPackage().getName(),
|
.setUrls(ClasspathHelper.forPackage(acmdclass.getPackage().getName(),
|
||||||
plugin.getClass().getClassLoader()))
|
plugin.getClass().getClassLoader()))
|
||||||
.addUrls(
|
.addUrls(
|
||||||
ClasspathHelper.forClass(OptionallyPlayerCommandBase.class,
|
ClasspathHelper.forClass(OptionallyPlayerCommandBase.class,
|
||||||
OptionallyPlayerCommandBase.class.getClassLoader()),
|
OptionallyPlayerCommandBase.class.getClassLoader()),
|
||||||
ClasspathHelper.forClass(PlayerCommandBase.class, PlayerCommandBase.class.getClassLoader())) // http://stackoverflow.com/questions/12917417/using-reflections-for-finding-the-transitive-subtypes-of-a-class-when-not-all
|
ClasspathHelper.forClass(PlayerCommandBase.class, PlayerCommandBase.class.getClassLoader())) // http://stackoverflow.com/questions/12917417/using-reflections-for-finding-the-transitive-subtypes-of-a-class-when-not-all
|
||||||
.addClassLoader(plugin.getClass().getClassLoader()).addScanners(new SubTypesScanner()));
|
.addClassLoader(plugin.getClass().getClassLoader()).addScanners(new SubTypesScanner()));
|
||||||
Set<Class<? extends TBMCCommandBase>> cmds = rf.getSubTypesOf(TBMCCommandBase.class);
|
Set<Class<? extends TBMCCommandBase>> cmds = rf.getSubTypesOf(TBMCCommandBase.class);
|
||||||
for (Class<? extends TBMCCommandBase> cmd : cmds) {
|
for (Class<? extends TBMCCommandBase> cmd : cmds) {
|
||||||
try {
|
try {
|
||||||
if (!cmd.getPackage().getName().startsWith(acmdclass.getPackage().getName()))
|
if (!cmd.getPackage().getName().startsWith(acmdclass.getPackage().getName()))
|
||||||
continue; // It keeps including the commands from here
|
continue; // It keeps including the commands from here
|
||||||
if (Modifier.isAbstract(cmd.getModifiers()))
|
if (Modifier.isAbstract(cmd.getModifiers()))
|
||||||
continue;
|
continue;
|
||||||
TBMCCommandBase c = cmd.newInstance();
|
TBMCCommandBase c = cmd.newInstance();
|
||||||
c.plugin = plugin;
|
c.plugin = plugin;
|
||||||
if (HasNulls(plugin, c))
|
if (HasNulls(plugin, c))
|
||||||
continue;
|
continue;
|
||||||
commands.put(c.GetCommandPath(), c);
|
commands.put(c.GetCommandPath(), c);
|
||||||
CommandCaller.RegisterCommand(c);
|
CommandCaller.RegisterCommand(c);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
TBMCCoreAPI.SendException("An error occured while registering command " + cmd.getName(), e);
|
TBMCCoreAPI.SendException("An error occured while registering command " + cmd.getName(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <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.
|
||||||
* </p>
|
* </p>
|
||||||
* <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.
|
* The <u>command must be registered</u> in the caller plugin's plugin.yml. Otherwise the plugin will output a messsage to console.
|
||||||
* </p>
|
* </p>
|
||||||
* <p>
|
* <p>
|
||||||
* <i>Using this method after the server is done loading will have no effect.</i>
|
* <i>Using this method after the server is done loading will have no effect.</i>
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param plugin
|
* @param plugin
|
||||||
* The caller plugin
|
* The caller plugin
|
||||||
* @param thecmdclass
|
* @param thecmdclass
|
||||||
* The command's class to create it (because why let you create the command class)
|
* The command's class to create it (because why let you create the command class)
|
||||||
*/
|
*/
|
||||||
public static void AddCommand(JavaPlugin plugin, Class<? extends TBMCCommandBase> thecmdclass, Object... params) {
|
public static void AddCommand(JavaPlugin plugin, Class<? extends TBMCCommandBase> thecmdclass, Object... params) {
|
||||||
// plugin.getLogger().info("Registering command " + thecmdclass.getSimpleName() + " for " + plugin.getName());
|
// plugin.getLogger().info("Registering command " + thecmdclass.getSimpleName() + " for " + plugin.getName());
|
||||||
try {
|
try {
|
||||||
TBMCCommandBase c;
|
TBMCCommandBase c;
|
||||||
if (params.length > 0)
|
if (params.length > 0)
|
||||||
c = thecmdclass.getConstructor(Arrays.stream(params).map(Object::getClass).toArray(Class[]::new))
|
c = thecmdclass.getConstructor(Arrays.stream(params).map(Object::getClass).toArray(Class[]::new))
|
||||||
.newInstance(params);
|
.newInstance(params);
|
||||||
else
|
else
|
||||||
c = thecmdclass.newInstance();
|
c = thecmdclass.newInstance();
|
||||||
c.plugin = plugin;
|
c.plugin = plugin;
|
||||||
if (HasNulls(plugin, c))
|
if (HasNulls(plugin, c))
|
||||||
return;
|
return;
|
||||||
commands.put(c.GetCommandPath(), c);
|
commands.put(c.GetCommandPath(), c);
|
||||||
CommandCaller.RegisterCommand(c);
|
CommandCaller.RegisterCommand(c);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
TBMCCoreAPI.SendException("An error occured while registering command " + thecmdclass.getSimpleName(), e);
|
TBMCCoreAPI.SendException("An error occured while registering command " + thecmdclass.getSimpleName(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <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.
|
||||||
* </p>
|
* </p>
|
||||||
* <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.
|
* The <u>command must be registered</u> in the caller plugin's plugin.yml. Otherwise the plugin will output a messsage to console.
|
||||||
* </p>
|
* </p>
|
||||||
* <p>
|
* <p>
|
||||||
* <i>Using this method after the server is done loading will have no effect.</i>
|
* <i>Using this method after the server is done loading will have no effect.</i>
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param plugin
|
* @param plugin
|
||||||
* The caller plugin
|
* The caller plugin
|
||||||
* @param cmd
|
* @param cmd
|
||||||
* The command to add
|
* The command to add
|
||||||
*/
|
*/
|
||||||
public static void AddCommand(JavaPlugin plugin, TBMCCommandBase cmd) {
|
public static void AddCommand(JavaPlugin plugin, TBMCCommandBase cmd) {
|
||||||
if (HasNulls(plugin, cmd))
|
if (HasNulls(plugin, cmd))
|
||||||
return;
|
return;
|
||||||
// plugin.getLogger().info("Registering command /" + cmd.GetCommandPath() + " for " + plugin.getName());
|
// plugin.getLogger().info("Registering command /" + cmd.GetCommandPath() + " for " + plugin.getName());
|
||||||
try {
|
try {
|
||||||
cmd.plugin = plugin;
|
cmd.plugin = plugin;
|
||||||
commands.put(cmd.GetCommandPath(), cmd);
|
commands.put(cmd.GetCommandPath(), cmd);
|
||||||
CommandCaller.RegisterCommand(cmd);
|
CommandCaller.RegisterCommand(cmd);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
TBMCCoreAPI.SendException("An error occured while registering command " + cmd.GetCommandPath(), e);
|
TBMCCoreAPI.SendException("An error occured while registering command " + cmd.GetCommandPath(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean HasNulls(JavaPlugin plugin, TBMCCommandBase cmd) {
|
private static boolean HasNulls(JavaPlugin plugin, TBMCCommandBase cmd) {
|
||||||
if (cmd == null) {
|
if (cmd == null) {
|
||||||
TBMCCoreAPI.SendException("An error occured while registering a command for plugin " + plugin.getName(),
|
TBMCCoreAPI.SendException("An error occured while registering a command for plugin " + plugin.getName(),
|
||||||
new Exception("The command is null!"));
|
new Exception("The command is null!"));
|
||||||
return true;
|
return true;
|
||||||
} else if (cmd.GetCommandPath() == null) {
|
} else if (cmd.GetCommandPath() == null) {
|
||||||
TBMCCoreAPI.SendException("An error occured while registering command " + cmd.getClass().getSimpleName()
|
TBMCCoreAPI.SendException("An error occured while registering command " + cmd.getClass().getSimpleName()
|
||||||
+ " for plugin " + plugin.getName(), new Exception("The command path is null!"));
|
+ " for plugin " + plugin.getName(), new Exception("The command path is null!"));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a chat message to Minecraft. Make sure that the channel is registered with {@link #RegisterChatChannel(Channel)}.<br>
|
* Sends a chat message to Minecraft. Make sure that the channel is registered with {@link #RegisterChatChannel(Channel)}.<br>
|
||||||
* This will also send the error message to the sender, if they can't send the message.
|
* This will also send the error message to the sender, if they can't send the message.
|
||||||
*
|
*
|
||||||
* @param cm The message to send
|
* @param cm The message to send
|
||||||
* @return The event cancelled state
|
* @return The event cancelled state
|
||||||
*/
|
*/
|
||||||
public static boolean SendChatMessage(ChatMessage cm) {
|
public static boolean SendChatMessage(ChatMessage cm) {
|
||||||
return SendChatMessage(cm, cm.getUser().channel().get());
|
return SendChatMessage(cm, cm.getUser().channel().get());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a chat message to Minecraft. Make sure that the channel is registered with {@link #RegisterChatChannel(Channel)}.<br>
|
* Sends a chat message to Minecraft. Make sure that the channel is registered with {@link #RegisterChatChannel(Channel)}.<br>
|
||||||
* This will also send the error message to the sender, if they can't send the message.
|
* This will also send the error message to the sender, if they can't send the message.
|
||||||
*
|
*
|
||||||
* @param cm The message to send
|
* @param cm The message to send
|
||||||
* @param channel The MC channel to send in
|
* @param channel The MC channel to send in
|
||||||
* @return The event cancelled state
|
* @return The event cancelled state
|
||||||
*/
|
*/
|
||||||
public static boolean SendChatMessage(ChatMessage cm, Channel channel) {
|
public static boolean SendChatMessage(ChatMessage cm, Channel channel) {
|
||||||
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!");
|
||||||
val permcheck = cm.getPermCheck();
|
val permcheck = cm.getPermCheck();
|
||||||
RecipientTestResult rtr = getScoreOrSendError(channel, permcheck);
|
RecipientTestResult rtr = getScoreOrSendError(channel, permcheck);
|
||||||
int score = rtr.score;
|
int score = rtr.score;
|
||||||
if (score == Channel.SCORE_SEND_NOPE || 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);
|
||||||
if (eventPre.isCancelled())
|
if (eventPre.isCancelled())
|
||||||
return true;
|
return true;
|
||||||
cm.setMessage(eventPre.getMessage());
|
cm.setMessage(eventPre.getMessage());
|
||||||
TBMCChatEvent event;
|
TBMCChatEvent event;
|
||||||
event = new TBMCChatEvent(channel, cm, rtr);
|
event = new TBMCChatEvent(channel, cm, rtr);
|
||||||
Bukkit.getPluginManager().callEvent(event);
|
Bukkit.getPluginManager().callEvent(event);
|
||||||
return event.isCancelled();
|
return event.isCancelled();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a regular message to Minecraft. Make sure that the channel is registered with {@link #RegisterChatChannel(Channel)}.
|
* Sends a regular message to Minecraft. Make sure that the channel is registered with {@link #RegisterChatChannel(Channel)}.
|
||||||
*
|
*
|
||||||
* @param channel
|
* @param channel
|
||||||
* The channel to send to
|
* The channel to send to
|
||||||
* @param rtr
|
* @param rtr
|
||||||
* The score&group to use to find the group - use {@link RecipientTestResult#ALL} 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
|
* @param exceptions Platforms where this message shouldn't be sent (same as {@link ChatMessage#getOrigin()}
|
||||||
*/
|
* @return The event cancelled state
|
||||||
public static boolean SendSystemMessage(Channel channel, RecipientTestResult rtr, String message) {
|
*/
|
||||||
if (!Channel.getChannels().contains(channel))
|
public static boolean SendSystemMessage(Channel channel, RecipientTestResult rtr, String message, String... exceptions) {
|
||||||
throw new RuntimeException("Channel " + channel.DisplayName + " not registered!");
|
if (!Channel.getChannels().contains(channel))
|
||||||
TBMCSystemChatEvent event = new TBMCSystemChatEvent(channel, message, rtr.score, rtr.groupID);
|
throw new RuntimeException("Channel " + channel.DisplayName + " not registered!");
|
||||||
Bukkit.getPluginManager().callEvent(event);
|
TBMCSystemChatEvent event = new TBMCSystemChatEvent(channel, message, rtr.score, rtr.groupID, exceptions);
|
||||||
return event.isCancelled();
|
Bukkit.getPluginManager().callEvent(event);
|
||||||
}
|
return event.isCancelled();
|
||||||
|
}
|
||||||
private static RecipientTestResult getScoreOrSendError(Channel channel, CommandSender sender) {
|
|
||||||
RecipientTestResult result = channel.getRTR(sender);
|
private static RecipientTestResult getScoreOrSendError(Channel channel, CommandSender sender) {
|
||||||
if (result.errormessage != null)
|
RecipientTestResult result = channel.getRTR(sender);
|
||||||
sender.sendMessage("§c" + result.errormessage);
|
if (result.errormessage != null)
|
||||||
return result;
|
sender.sendMessage("§c" + result.errormessage);
|
||||||
}
|
return result;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Register a chat channel. See {@link Channel#Channel(String, Color, String, java.util.function.Function)} for details.
|
/**
|
||||||
*
|
* Register a chat channel. See {@link Channel#Channel(String, Color, String, java.util.function.Function)} for details.
|
||||||
* @param channel
|
*
|
||||||
* A new {@link Channel} to register
|
* @param channel
|
||||||
*/
|
* A new {@link Channel} to register
|
||||||
public static void RegisterChatChannel(Channel channel) {
|
*/
|
||||||
Channel.RegisterChannel(channel);
|
public static void RegisterChatChannel(Channel channel) {
|
||||||
}
|
Channel.RegisterChannel(channel);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue