QoL/src/main/java/xyz/etztech/qol/QoL.java

282 lines
9.6 KiB
Java

package xyz.etztech.qol;
import net.ess3.api.IEssentials;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.dynmap.DynmapAPI;
import xyz.etztech.qol.commands.*;
import xyz.etztech.qol.listeners.*;
import xyz.etztech.qol.other.LinkCommand;
import xyz.etztech.qol.other.TPSRunnable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.logging.Logger;
public class QoL extends JavaPlugin {
private static QoL instance;
private IEssentials essentials = null;
private DynmapAPI dynmap = null;
public static FileConfiguration config;
private Logger log = Logger.getLogger( "Minecraft" );
private static List<UUID> mutes = new ArrayList<>();
private static List<UUID> deathMutes = new ArrayList<>();
private static boolean whitelist = false;
private static boolean timeout = false;
private static List<String> audits = new ArrayList<>();
private static List<LinkCommand> links = new ArrayList<>();
public void onEnable() {
instance = this;
saveDefaultConfig();
reloadConfig();
saveResource("qol.png", true);
//Essentials hook
if (Bukkit.getPluginManager().isPluginEnabled("Essentials")) {
log("Hooked into Essentials for TPS alert.");
essentials = (IEssentials) Bukkit.getPluginManager().getPlugin("Essentials");
}
//Dynmap hook
if (Bukkit.getPluginManager().isPluginEnabled("dynmap")) {
log("Hooked into Dynmap.");
dynmap = (DynmapAPI) Bukkit.getPluginManager().getPlugin("dynmap");
}
if( isEnabled() ) {
// Add listeners
ServerListPingListener serverListPingListener = new ServerListPingListener(this);
getServer().getPluginManager().registerEvents(serverListPingListener, this);
AsyncPlayerChatListener asyncPlayerChatListener = new AsyncPlayerChatListener(this);
getServer().getPluginManager().registerEvents(asyncPlayerChatListener, this);
LoginListener loginListener = new LoginListener(this);
getServer().getPluginManager().registerEvents(loginListener, this);
JoinListener joinListener = new JoinListener(this);
getServer().getPluginManager().registerEvents(joinListener, this);
BlockIgniteListener blockIgniteListener = new BlockIgniteListener(this);
getServer().getPluginManager().registerEvents(blockIgniteListener, this);
CommandPreprocessListener commandPreprocessListener = new CommandPreprocessListener(this);
getServer().getPluginManager().registerEvents(commandPreprocessListener, this);
DeathListener deathListener = new DeathListener(this);
getServer().getPluginManager().registerEvents(deathListener, this);
// Add commands
MainCommand mainCommand = new MainCommand(this);
this.getCommand("qol").setExecutor(mainCommand);
UUIDCommand uuidCommand = new UUIDCommand(this);
this.getCommand("uuid").setExecutor(uuidCommand);
NameHistoryCommand nameHistoryCommand = new NameHistoryCommand(this);
this.getCommand("history").setExecutor(nameHistoryCommand);
PortalCommand portalCommand = new PortalCommand(this);
this.getCommand("portal").setExecutor(portalCommand);
SudoCommand sudoCommand = new SudoCommand(this);
this.getCommand("sudo").setExecutor(sudoCommand);
MakeMeCommand makeMeCommand = new MakeMeCommand(this);
this.getCommand("makeme").setExecutor(makeMeCommand);
ShadowMuteCommand shadowMuteCommand = new ShadowMuteCommand(this);
this.getCommand("shadowmute").setExecutor(shadowMuteCommand);
WhitelistCommand whitelistCommand = new WhitelistCommand(this);
this.getCommand("whitelist").setExecutor(whitelistCommand);
TimeoutCommand timeoutCommand = new TimeoutCommand(this);
this.getCommand("timeout").setExecutor(timeoutCommand);
ColorsCommand colorsCommand = new ColorsCommand(this);
this.getCommand("colors").setExecutor(colorsCommand);
WorldInfoCommand worldInfoCommand = new WorldInfoCommand(this);
this.getCommand("worldinfo").setExecutor(worldInfoCommand);
DeathMuteCommand deathMuteCommand = new DeathMuteCommand(this);
this.getCommand("deathmute").setExecutor(deathMuteCommand);
KaratTrophyCommand karatTrophyCommand = new KaratTrophyCommand(this);
this.getCommand("karattrophy").setExecutor(karatTrophyCommand);
if (getConfig().getStringList("list").size() > 0) {
ListCommand listCommand = new ListCommand(this);
this.getCommand("list").setExecutor(listCommand);
}
if (getConfig().getStringList("plugins").size() > 0) {
PluginsCommand pluginsCommand = new PluginsCommand(this);
this.getCommand("plugins").setExecutor(pluginsCommand);
}
// Scheduler
int schedule = config.getInt("schedule.frequency");
if (schedule > 0) {
Bukkit.getScheduler().scheduleSyncRepeatingTask(QoL.getInstance(), () -> {
List<String> scheduled = config.getStringList("schedule.commands");
for (String command : scheduled) {
runTask(command);
}
}, 0, EtzTechUtil.minutesToTicks(schedule));
}
// Reminders
int frequency = config.getInt("reminders.frequency");
if (frequency > 0) {
Bukkit.getScheduler().scheduleSyncRepeatingTask(QoL.getInstance(), new Runnable() {
int idx = 0;
@Override
public void run() {
List<String> reminders = config.getStringList("reminders.messages");
ChatColor color = ChatColor.getByChar(config.getString("reminders.color").replace("&", ""));
if (idx >= reminders.size()) {
idx = 0;
}
for (Player player : Bukkit.getOnlinePlayers()) {
EtzTechUtil.sms(player, color + reminders.get(idx));
}
idx++;
}
}, 0, EtzTechUtil.minutesToTicks(frequency));
}
// TPS Check
Bukkit.getScheduler().scheduleSyncRepeatingTask(QoL.getInstance(), new TPSRunnable(this), 0, EtzTechUtil.minutesToTicks(1));
}
}
public void loadConfig() {
config = Bukkit.getPluginManager().getPlugin("QoL").getConfig();
}
@Override
public void reloadConfig() {
super.reloadConfig();
loadConfig();
audits = new ArrayList<>();
for (String command : config.getStringList("audit.commands")) {
audits.add(command.toLowerCase());
}
links = new ArrayList<>();
for (String raw : config.getStringList("links")) {
links.add(LinkCommand.fromString(raw));
}
}
/**
*
* @param message
*/
public void log(String message) {
log.info( "[QoL]: " + message );
}
/**
*
* @param messages
*/
public void logSection(String[] messages) {
if( messages.length > 0 ) {
log( "--------------------- ## Important ## ---------------------" );
for ( final String msg : messages ) {
log( msg );
}
log( "--------------------- ## ========= ## ---------------------" );
}
}
public void onDisable() {
}
public void disablePlugin() {
this.setEnabled( false );
}
public static QoL getInstance() {
return instance;
}
public Logger getLog()
{
return this.log;
}
public void setLog(Logger paramLogger)
{
this.log = paramLogger;
}
public static void addSM(Player player) {
if (!mutes.contains(player.getUniqueId())) {
mutes.add(player.getUniqueId());
}
}
public static boolean toggleDeathMute(Player player) {
if (!deathMutes.contains(player.getUniqueId())) {
deathMutes.add(player.getUniqueId());
return true;
} else {
deathMutes.remove(player.getUniqueId());
return false;
}
}
public static boolean hasSM(Player player) {
return mutes.contains(player.getUniqueId());
}
public static boolean hasDeathMute(Player player) { return deathMutes.contains(player.getUniqueId()); }
public static void removeSM(Player player) {
mutes.remove(player.getUniqueId());
}
public static boolean getWhitelist() {
return whitelist;
}
public static void setWhitelist(boolean enabled) {
whitelist = enabled;
}
public static boolean getTimeout() {
return timeout;
}
public static void setTimeout(boolean enabled) {
timeout = enabled;
}
public static List<String> getAudits() { return audits; }
public static List<LinkCommand> getLinks() {
return links;
}
public IEssentials getEssentials() { return essentials; }
public DynmapAPI getDynmap() { return dynmap; }
private void runTask(final String command) {
Bukkit.getScheduler().runTask(QoL.instance, () -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command));
}
}