LibsDisguises/src/main/java/me/libraryaddict/disguise/LibsDisguises.java

261 lines
10 KiB
Java
Raw Normal View History

package me.libraryaddict.disguise;
import lombok.Getter;
2020-03-08 21:00:58 +01:00
import me.libraryaddict.disguise.commands.LibsDisguisesCommand;
import me.libraryaddict.disguise.commands.disguise.DisguiseCommand;
import me.libraryaddict.disguise.commands.disguise.DisguiseEntityCommand;
import me.libraryaddict.disguise.commands.disguise.DisguisePlayerCommand;
import me.libraryaddict.disguise.commands.disguise.DisguiseRadiusCommand;
import me.libraryaddict.disguise.commands.modify.DisguiseModifyCommand;
import me.libraryaddict.disguise.commands.modify.DisguiseModifyEntityCommand;
import me.libraryaddict.disguise.commands.modify.DisguiseModifyPlayerCommand;
import me.libraryaddict.disguise.commands.modify.DisguiseModifyRadiusCommand;
import me.libraryaddict.disguise.commands.undisguise.UndisguiseCommand;
import me.libraryaddict.disguise.commands.undisguise.UndisguiseEntityCommand;
import me.libraryaddict.disguise.commands.undisguise.UndisguisePlayerCommand;
import me.libraryaddict.disguise.commands.undisguise.UndisguiseRadiusCommand;
import me.libraryaddict.disguise.commands.utils.*;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.LibsPremium;
2020-04-20 14:52:58 +02:00
import me.libraryaddict.disguise.utilities.listeners.DisguiseListener;
import me.libraryaddict.disguise.utilities.listeners.PaperDisguiseListener;
import me.libraryaddict.disguise.utilities.metrics.MetricsInitalizer;
import me.libraryaddict.disguise.utilities.packets.PacketsManager;
import me.libraryaddict.disguise.utilities.parser.DisguiseParser;
2020-02-06 00:15:20 +01:00
import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import me.libraryaddict.disguise.utilities.reflection.asm.WatcherSanitizer;
2020-05-01 16:09:24 +02:00
import me.libraryaddict.disguise.utilities.updates.UpdateChecker;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.*;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
2017-06-22 18:14:19 +02:00
import java.io.File;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Collectors;
public class LibsDisguises extends JavaPlugin {
private static LibsDisguises instance;
private DisguiseListener listener;
private String buildNumber;
@Getter
2020-01-18 03:12:32 +01:00
private boolean reloaded;
@Getter
2020-05-13 23:04:48 +02:00
private final UpdateChecker updateChecker = new UpdateChecker();
2020-01-18 03:12:32 +01:00
@Override
public void onLoad() {
2020-02-21 02:24:07 +01:00
instance = this;
2020-02-21 02:45:11 +01:00
if (!Bukkit.getServer().getWorlds().isEmpty()) {
reloaded = true;
getLogger().severe("Server was reloaded! Please do not report any bugs! This plugin can't handle " +
"reloads gracefully!");
2020-01-18 03:12:32 +01:00
return;
}
2020-02-21 01:53:08 +01:00
WatcherSanitizer.init();
2020-01-18 03:12:32 +01:00
}
@Override
public void onEnable() {
2020-01-18 03:12:32 +01:00
if (reloaded) {
getLogger().severe("Server was reloaded! Please do not report any bugs! This plugin can't handle " +
"reloads gracefully!");
2020-01-18 03:12:32 +01:00
}
if (Bukkit.getBukkitVersion().startsWith("git-Bukkit-")) {
DisguiseUtilities.getLogger()
.severe("Oh dear, you seem to be using CraftBukkit. Please use Spigot or Paper instead! This " +
"plugin will continue to load, but it will look like a mugging victim");
}
if (!new File(getDataFolder(), "disguises.yml").exists()) {
saveResource("disguises.yml", false);
}
YamlConfiguration pluginYml = ReflectionManager.getPluginYAML(getFile());
buildNumber = StringUtils.stripToNull(pluginYml.getString("build-number"));
2020-02-06 01:05:59 +01:00
getLogger().info("Discovered nms version: " + ReflectionManager.getBukkitVersion());
getLogger().info("Jenkins Build: " + (isNumberedBuild() ? "#" : "") + getBuildNo());
getLogger().info("Build Date: " + pluginYml.getString("build-date"));
DisguiseConfig.loadInternalConfig();
LibsPremium.check(getDescription().getVersion(), getFile());
if (!LibsPremium.isPremium()) {
2019-12-21 22:15:52 +01:00
getLogger()
.info("You are running the free version, commands limited to non-players and operators. (Console," +
" Command " + "Blocks, Admins)");
}
2020-02-06 00:15:20 +01:00
if (ReflectionManager.getVersion() == null) {
getLogger().severe("You're using the wrong version of Lib's Disguises for your server! This is " +
"intended for " + StringUtils
.join(Arrays.stream(NmsVersion.values()).map(v -> v.name().replace("_", "."))
.collect(Collectors.toList()), " & ") + "!");
getPluginLoader().disablePlugin(this);
return;
}
// If this is a release build, even if jenkins build..
if (isReleaseBuild()) {
// If downloaded from spigot, forcibly set release build to true
if (LibsPremium.getUserID().matches("[0-9]+")) {
DisguiseConfig.setUsingReleaseBuilds(true);
}
// Otherwise leave it untouched as they might've just happened to hit a dev build, which is a release build
} else {
DisguiseConfig.setUsingReleaseBuilds(false);
}
2019-12-24 05:40:56 +01:00
ReflectionManager.init();
2020-01-19 03:48:26 +01:00
PacketsManager.init();
DisguiseUtilities.init();
2020-01-19 03:48:26 +01:00
ReflectionManager.registerValues();
DisguiseConfig.loadConfig();
DisguiseParser.createDefaultMethods();
PacketsManager.addPacketListeners();
listener = new DisguiseListener(this);
if (DisguiseUtilities.isRunningPaper()) {
Bukkit.getPluginManager().registerEvents(new PaperDisguiseListener(), this);
}
2020-02-06 00:15:20 +01:00
registerCommand("libsdisguises", new LibsDisguisesCommand());
if (!DisguiseConfig.isDisableCommands()) {
registerCommand("disguise", new DisguiseCommand());
registerCommand("undisguise", new UndisguiseCommand());
registerCommand("disguiseplayer", new DisguisePlayerCommand());
registerCommand("undisguiseplayer", new UndisguisePlayerCommand());
registerCommand("undisguiseentity", new UndisguiseEntityCommand());
registerCommand("disguiseentity", new DisguiseEntityCommand());
registerCommand("disguiseradius", new DisguiseRadiusCommand(getConfig().getInt("DisguiseRadiusMax")));
registerCommand("undisguiseradius", new UndisguiseRadiusCommand(getConfig().getInt("UndisguiseRadiusMax")));
registerCommand("disguisehelp", new DisguiseHelpCommand());
registerCommand("disguiseclone", new DisguiseCloneCommand());
registerCommand("disguiseviewself", new DisguiseViewSelfCommand());
registerCommand("disguisemodify", new DisguiseModifyCommand());
registerCommand("disguisemodifyentity", new DisguiseModifyEntityCommand());
registerCommand("disguisemodifyplayer", new DisguiseModifyPlayerCommand());
registerCommand("disguisemodifyradius",
new DisguiseModifyRadiusCommand(getConfig().getInt("DisguiseRadiusMax")));
registerCommand("copydisguise", new CopyDisguiseCommand());
registerCommand("grabskin", new GrabSkinCommand());
registerCommand("savedisguise", new SaveDisguiseCommand());
} else {
getLogger().info("Commands has been disabled, as per config");
}
2020-05-21 12:30:53 +02:00
unregisterCommands(false);
new MetricsInitalizer();
2017-06-22 18:14:19 +02:00
}
2020-05-21 12:30:53 +02:00
public void unregisterCommands(boolean force) {
CommandMap map = ReflectionManager.getCommandMap();
Map<String, Command> commands = ReflectionManager.getCommands(map);
for (String command : getDescription().getCommands().keySet()) {
PluginCommand cmd = getCommand("libsdisguises:" + command);
2020-05-21 12:30:53 +02:00
if (cmd.getExecutor() != this && !force) {
continue;
}
if (cmd.getPermission() != null && cmd.getPermission().startsWith("libsdisguises.seecmd")) {
Bukkit.getPluginManager().removePermission(cmd.getPermission());
}
Iterator<Map.Entry<String, Command>> itel = commands.entrySet().iterator();
while (itel.hasNext()) {
Map.Entry<String, Command> entry = itel.next();
if (entry.getValue() != cmd) {
continue;
}
itel.remove();
}
}
}
2020-04-17 04:14:49 +02:00
@Override
public File getFile() {
return super.getFile();
}
@Override
public void onDisable() {
DisguiseUtilities.saveDisguises();
for (Player player : Bukkit.getOnlinePlayers()) {
DisguiseUtilities.removeSelfDisguiseScoreboard(player);
}
}
public boolean isReleaseBuild() {
return !getDescription().getVersion().contains("-SNAPSHOT");
}
public String getBuildNo() {
return buildNumber;
2017-06-02 15:51:03 +02:00
}
public int getBuildNumber() {
return isNumberedBuild() ? Integer.parseInt(getBuildNo()) : 0;
}
public boolean isNumberedBuild() {
return getBuildNo() != null && getBuildNo().matches("[0-9]+");
}
2016-11-28 15:01:06 +01:00
private void registerCommand(String commandName, CommandExecutor executioner) {
PluginCommand command = getCommand("libsdisguises:" + commandName);
2016-11-28 15:01:06 +01:00
command.setExecutor(executioner);
if (executioner instanceof TabCompleter) {
command.setTabCompleter((TabCompleter) executioner);
}
}
/**
* Reloads the config with new config options.
*/
@Deprecated
public void reload() {
DisguiseConfig.loadConfig();
}
public DisguiseListener getListener() {
return listener;
}
/**
* External APIs shouldn't actually need this instance. DisguiseAPI should be enough to handle most cases.
*
* @return The instance of this plugin
*/
public static LibsDisguises getInstance() {
return instance;
}
}