package os.arcadiadevs.playerservers.servercore.commands; import com.cryptomorin.xseries.XMaterial; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.plugin.InvalidDescriptionException; import org.bukkit.plugin.InvalidPluginException; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.UnknownDependencyException; import os.arcadiadevs.playerservers.servercore.utils.ColorUtils; import os.arcadiadevs.playerservers.servercore.utils.FileUtils; import os.arcadiadevs.playerservers.servercore.utils.FormattingUtils; import os.arcadiadevs.playerservers.servercore.utils.Hastebin; import os.arcadiadevs.playerservers.servercore.utils.TPSUtils; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Locale; public class Commands implements CommandExecutor { private final Plugin SC; public Commands(Plugin sc) { this.SC = sc; } @Override public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) { if (commandSender instanceof Player) { final Player sender = (Player) commandSender; if (!hasAccess(sender)) { sender.sendMessage(ChatColor.RED + "I'm sorry, but you do not have permission to perform " + "this command. Please contact server administrators if you believe that this is in error."); return true; } switch (command.getName().toLowerCase()) { case "addplugin": if (strings.length != 1) { sender.sendMessage(ChatColor.RED + "Not enough, or too many arguments!"); return true; } new Thread(() -> { if (Bukkit.getPluginManager().getPlugins().length > SC.getConfig().getInt("MaxPlugins")) { sender.sendMessage(ColorUtils.translate("&9Error> &7You can have a max of &e" + (Bukkit.getPluginManager().getPlugins().length-1) + "&7 plugins.")); return; } final String displayName = strings[0].toLowerCase().endsWith(".jar") ? strings[0] : strings[0] + ".jar"; final String PATH1 = SC.getDataFolder().getAbsoluteFile().getParentFile().getParentFile().getParentFile().getParentFile().getAbsolutePath() + File.separatorChar + "plugins-to-be-added-ingame" + File.separatorChar + displayName; if (!(new File(PATH1).exists())) { sender.sendMessage(ColorUtils.translate("&9Error> &7The plugin you're trying to add does not exist!")); return; } final String PLUGINSPATH = SC.getDataFolder().getAbsoluteFile().getParentFile().getAbsolutePath(); final File f = new File(PLUGINSPATH + File.separatorChar + displayName); try { Runtime.getRuntime().exec("cp -r " + PATH1 + " " + PLUGINSPATH); Bukkit.getScheduler().runTaskLater(SC, () -> { Plugin pl = null; try { pl = Bukkit.getPluginManager().loadPlugin(f); } catch (InvalidPluginException | InvalidDescriptionException | UnknownDependencyException ex) { if (ex instanceof UnknownDependencyException) { UnknownDependencyException udp = (UnknownDependencyException) ex; sender.sendMessage(ColorUtils.translate(String.format("&9Error> &7Oops, it looks like that plugin could not be loaded! The plugin you tried to load requires an additional dependency : %s (Code #PL3)", udp.getLocalizedMessage()))); f.delete(); } else { sender.sendMessage(ColorUtils.translate("&9Error> &7Oops, it looks like that plugin could not be loaded! The error exception has been printed to the console, please ask an administrator to take a look. (Code #PL2)")); ex.printStackTrace(); } } Bukkit.getPluginManager().enablePlugin(pl); sender.sendMessage(ColorUtils.translate("&9PluginManager> &7We've tried to enable &a" + pl.getName() + "&7. &cPLEASE NOTE THAT NOW YOU SHOULD RESTART YOUR SERVER OR SOME ISSUES MAY OCCUR!!")); final List current = SC.getConfig().getStringList("PluginsEnabled"); current.add(displayName + ";" + pl.getName() + ";" + pl.getDescription().getDescription()); SC.getConfig().set("PluginsEnabled", current); SC.saveConfig(); }, 10); } catch (IOException ex) { sender.sendMessage(ColorUtils.translate("&9Error> &7Oops, it looks like that plugin could not be loaded! The error exception has been printed to the console, please ask an administrator to take a look. (Code #PL1)")); ex.printStackTrace(); } }).start(); return true; case "removeplugin": if (strings.length != 1) { sender.sendMessage(ChatColor.RED + "Not enough, or too many arguments!"); return true; } final FormattingUtils removeFu = new FormattingUtils(SC); final String displayName = strings[0].toLowerCase().endsWith(".jar") ? strings[0] : strings[0] + ".jar"; final String PLUGINSPATH = SC.getDataFolder().getAbsoluteFile().getParentFile().getAbsolutePath(); final List current = SC.getConfig().getStringList("PluginsEnabled"); try { final Plugin pl; try { pl = Bukkit.getPluginManager().getPlugin(removeFu.findRealName(current, displayName)); } catch (Exception ex) { sender.sendMessage(ColorUtils.translate("&9Error> &7The plugin you're trying to remove does not exist!")); return true; } if (pl == null) { sender.sendMessage(ColorUtils.translate("&9Error> &7The plugin you're trying to remove does not exist!")); return true; } Bukkit.getPluginManager().disablePlugin(pl); Runtime.getRuntime().exec("rm -r " + PLUGINSPATH + File.separatorChar + displayName); sender.sendMessage(ColorUtils.translate("&9PluginManager> &7Successfully disabled &a" + pl.getName() + "&7. &cTHE PLUGIN WILL BE REMOVED ONCE THE SERVER RESTARTS. YOU SHOULD RESTART THE SERVER NOW TO AVOID SIDE-EFFECTS")); current.removeIf(el -> el.toLowerCase().contains(displayName.toLowerCase())); SC.getConfig().set("PluginsEnabled", current); SC.saveConfig(); } catch (IOException ex) { sender.sendMessage(ColorUtils.translate("&9Error> &7Oops, it looks like that plugin could not be loaded! The error exception has been printed to the console, please ask an administrator to take a look.")); ex.printStackTrace(); } break; case "updateconfig": if (strings.length != 2) { sender.sendMessage(ChatColor.RED + "Not enough, or too many arguments!"); return true; } final Hastebin hb = new Hastebin(); final String path = SC.getDataFolder().getAbsoluteFile().getParentFile().getAbsolutePath() + File.separatorChar + strings[0]; final String link = strings[1] .replaceAll("https://", "") .replaceAll("http://", "") .replaceAll("hastebin\\.com", "") .replaceAll("paste\\.md-5\\.net", "") .replaceAll("/", ""); final File editFile = new File(path); if (!editFile.exists()) { sender.sendMessage(ColorUtils.translate("&9Error> &7The plugin you're trying to edit does not exist!")); return true; } final String output = (link.toLowerCase().contains("hastebin")) ? hb.getPaste(false, link) : hb.getPaste(true, link); try { final FileUtils fu = new FileUtils(); fu.replaceConfig(output, new File(path)); } catch (NullPointerException ex) { ex.printStackTrace(); sender.sendMessage(ColorUtils.translate(ColorUtils.translate("&9Error> &7Error replacing config content."))); return true; } sender.sendMessage(ColorUtils.translate("&9Config> &7You've successfully edited the config! You should restart the server now in order to apply the changes.")); break; case "menu": new Thread(() -> { final Inventory gui = Bukkit.createInventory(sender, 3*9, ChatColor.BLACK + "Server Statistics"); final String tps = String.format(Locale.US, "%.2f", TPSUtils.getRecentTps()[0]); final String tpsString = (Double.parseDouble(tps) > 20) ? "20.00" : tps; ItemStack itemStack = XMaterial.CRAFTING_TABLE.parseItem(); ItemMeta im = itemStack.getItemMeta(); im.setDisplayName(ColorUtils.translate("&cStatistics & Info")); ArrayList lore = new ArrayList() {{ add(ColorUtils.translate("&aUUID: &7" + SC.getConfig().getString("ServerUUID").split("-")[0])); add(ColorUtils.translate("&aTPS: &7" + tpsString)); add(ColorUtils.translate("&aOnline: &7" + Bukkit.getOnlinePlayers().size() + " players")); add(ColorUtils.translate("&aWhitelist: &7" + Bukkit.getServer().hasWhitelist())); add(ColorUtils.translate("&aBanned: &7" + Bukkit.getServer().getBannedPlayers().size() + " players")); }}; im.setLore(lore); itemStack.setItemMeta(im); gui.setItem(10, itemStack); lore.clear(); itemStack = XMaterial.CLOCK.parseItem(); im = itemStack.getItemMeta(); im.setDisplayName(ColorUtils.translate("&cPlugin Manager")); lore.add(ColorUtils.translate("&7Click on this menu in order")); lore.add(ColorUtils.translate("&7to install new plugins or")); lore.add(ColorUtils.translate("&7to remove existing ones.")); im.setLore(lore); itemStack.setItemMeta(im); gui.setItem(16, itemStack); lore.clear(); itemStack = XMaterial.WRITABLE_BOOK.parseItem(); im = itemStack.getItemMeta(); im.setDisplayName(ColorUtils.translate("&cSet MOTD")); lore.add(ColorUtils.translate("&7Click here to set Server MOTD")); im.setLore(lore); itemStack.setItemMeta(im); gui.setItem(12, itemStack); lore.clear(); itemStack = XMaterial.PLAYER_HEAD.parseItem(); SkullMeta playerheadmeta = (SkullMeta) itemStack.getItemMeta(); playerheadmeta.setOwner(sender.getName()); playerheadmeta.setDisplayName(ColorUtils.translate("&cSet Max Players")); lore.add(ColorUtils.translate("&7Click here to set max players count")); playerheadmeta.setLore(lore); itemStack.setItemMeta(playerheadmeta); // TODO: Make permissions for this gui.setItem(14, itemStack); Bukkit.getScheduler().runTask(SC, () -> sender.openInventory(gui)); }).start(); return true; case "config": final Inventory i = Bukkit.createInventory(sender, 6 * 9, ColorUtils.translate("&0Config Manager")); sender.sendMessage(ColorUtils.translate("&9Config> &7Loading the GUI...")); new Thread(() -> { final File f = new File(SC.getDataFolder().getParentFile().getPath()); final ArrayList al = new ArrayList<>(); final FormattingUtils fu = new FormattingUtils(SC); for (File file : f.listFiles()) { if (SC.getConfig().getStringList("DisabledAccess").contains(file.getName())) continue; if (file.isDirectory()) { if (file.getName().equalsIgnoreCase("PSServerCore")) continue; ItemStack is = XMaterial.CHEST.parseItem(); ItemMeta im = is.getItemMeta(); im.setDisplayName(file.getName()); im.setLore(fu.location(file.getPath())); is.setItemMeta(im); i.addItem(is); } else { final ItemStack is = (file.getName().contains(".yml") || file.getName().contains(".json") || file.getName().contains(".toml") || file.getName().contains(".sk")) ? XMaterial.WRITABLE_BOOK.parseItem() : XMaterial.BOOK.parseItem(); ItemMeta im = is.getItemMeta(); im.setDisplayName(file.getName()); is.setItemMeta(im); al.add(is); } } al.forEach(i::addItem); Bukkit.getScheduler().runTask(SC, () -> sender.openInventory(i)); }).start(); return true; } } return true; } private boolean hasAccess(Player player) { return SC.getConfig().getStringList("Access").contains(player.getName().toLowerCase()) || SC.getConfig().getString("Owner").equalsIgnoreCase(player.getName()) || SC.getConfig().getString("Owner").equalsIgnoreCase(player.getUniqueId().toString()) || SC.getConfig().getStringList("Access").contains(player.getUniqueId().toString()) || SC.getConfig().getString("Owner").equalsIgnoreCase(player.getUniqueId().toString()) || SC.getConfig().getString("Owner").equalsIgnoreCase(player.getUniqueId().toString()); } }