From Ungracious Rhinoceros, 1 Month ago, written in Java.
This paste will croak in 1 Second.
Embed
  1. package os.arcadiadevs.playerservers.servercore.commands;
  2.  
  3. import com.cryptomorin.xseries.XMaterial;
  4. import org.bukkit.Bukkit;
  5. import org.bukkit.ChatColor;
  6. import org.bukkit.command.Command;
  7. import org.bukkit.command.CommandExecutor;
  8. import org.bukkit.command.CommandSender;
  9. import org.bukkit.entity.Player;
  10. import org.bukkit.inventory.Inventory;
  11. import org.bukkit.inventory.ItemStack;
  12. import org.bukkit.inventory.meta.ItemMeta;
  13. import org.bukkit.inventory.meta.SkullMeta;
  14. import org.bukkit.plugin.InvalidDescriptionException;
  15. import org.bukkit.plugin.InvalidPluginException;
  16. import org.bukkit.plugin.Plugin;
  17. import org.bukkit.plugin.UnknownDependencyException;
  18. import os.arcadiadevs.playerservers.servercore.utils.ColorUtils;
  19. import os.arcadiadevs.playerservers.servercore.utils.FileUtils;
  20. import os.arcadiadevs.playerservers.servercore.utils.FormattingUtils;
  21. import os.arcadiadevs.playerservers.servercore.utils.Hastebin;
  22. import os.arcadiadevs.playerservers.servercore.utils.TPSUtils;
  23.  
  24. import java.io.File;
  25. import java.io.IOException;
  26.  
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. import java.util.Locale;
  30.  
  31. public class Commands implements CommandExecutor {
  32.  
  33.     private final Plugin SC;
  34.  
  35.     public Commands(Plugin sc) {
  36.         this.SC = sc;
  37.     }
  38.  
  39.     @Override
  40.     public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
  41.         if (commandSender instanceof Player) {
  42.  
  43.             final Player sender = (Player) commandSender;
  44.  
  45.             if (!hasAccess(sender)) {
  46.                 sender.sendMessage(ChatColor.RED + "I'm sorry, but you do not have permission to perform " +
  47.                         "this command. Please contact server administrators if you believe that this is in error.");
  48.                 return true;
  49.             }
  50.  
  51.             switch (command.getName().toLowerCase()) {
  52.                 case "addplugin":
  53.                     if (strings.length != 1) {
  54.                         sender.sendMessage(ChatColor.RED + "Not enough, or too many arguments!");
  55.                         return true;
  56.                     }
  57.  
  58.                     new Thread(() -> {
  59.                         if (Bukkit.getPluginManager().getPlugins().length > SC.getConfig().getInt("MaxPlugins")) {
  60.                             sender.sendMessage(ColorUtils.translate("&9Error> &7You can have a max of &e" + (Bukkit.getPluginManager().getPlugins().length-1) + "&7 plugins."));
  61.                             return;
  62.                         }
  63.  
  64.                         final String displayName = strings[0].toLowerCase().endsWith(".jar") ? strings[0] : strings[0] + ".jar";
  65.                         final String PATH1 = SC.getDataFolder().getAbsoluteFile().getParentFile().getParentFile().getParentFile().getParentFile().getAbsolutePath() + File.separatorChar + "plugins-to-be-added-ingame" + File.separatorChar + displayName;
  66.  
  67.                         if (!(new File(PATH1).exists())) {
  68.                             sender.sendMessage(ColorUtils.translate("&9Error> &7The plugin you're trying to add does not exist!"));
  69.                             return;
  70.                         }
  71.  
  72.                         final String PLUGINSPATH = SC.getDataFolder().getAbsoluteFile().getParentFile().getAbsolutePath();
  73.                         final File f = new File(PLUGINSPATH + File.separatorChar + displayName);
  74.                         try {
  75.                             Runtime.getRuntime().exec("cp -r " + PATH1 + " " + PLUGINSPATH);
  76.                             Bukkit.getScheduler().runTaskLater(SC, () -> {
  77.                                 Plugin pl = null;
  78.                                 try {
  79.                                     pl = Bukkit.getPluginManager().loadPlugin(f);
  80.                                 } catch (InvalidPluginException | InvalidDescriptionException | UnknownDependencyException ex) {
  81.                                     if (ex instanceof UnknownDependencyException) {
  82.                                         UnknownDependencyException udp = (UnknownDependencyException) ex;
  83.                                         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())));
  84.                                         f.delete();
  85.                                     } else {
  86.                                         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)"));
  87.                                         ex.printStackTrace();
  88.                                     }
  89.                                 }
  90.                                 Bukkit.getPluginManager().enablePlugin(pl);
  91.                                 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!!"));
  92.                                 final List<String> current = SC.getConfig().getStringList("PluginsEnabled");
  93.                                 current.add(displayName + ";" + pl.getName() + ";" + pl.getDescription().getDescription());
  94.                                 SC.getConfig().set("PluginsEnabled", current);
  95.                                 SC.saveConfig();
  96.                             }, 10);
  97.                         } catch (IOException ex) {
  98.                             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)"));
  99.                             ex.printStackTrace();
  100.                         }
  101.                     }).start();
  102.                     return true;
  103.                 case "removeplugin":
  104.                     if (strings.length != 1) {
  105.                         sender.sendMessage(ChatColor.RED + "Not enough, or too many arguments!");
  106.                         return true;
  107.                     }
  108.  
  109.                     final FormattingUtils removeFu = new FormattingUtils(SC);
  110.                     final String displayName = strings[0].toLowerCase().endsWith(".jar") ? strings[0] : strings[0] + ".jar";
  111.                     final String PLUGINSPATH = SC.getDataFolder().getAbsoluteFile().getParentFile().getAbsolutePath();
  112.                     final List<String> current = SC.getConfig().getStringList("PluginsEnabled");
  113.  
  114.                     try {
  115.                         final Plugin pl;
  116.                         try {
  117.                             pl = Bukkit.getPluginManager().getPlugin(removeFu.findRealName(current, displayName));
  118.                         } catch (Exception ex) {
  119.                             sender.sendMessage(ColorUtils.translate("&9Error> &7The plugin you're trying to remove does not exist!"));
  120.                             return true;
  121.                         }
  122.  
  123.                         if (pl == null) {
  124.                             sender.sendMessage(ColorUtils.translate("&9Error> &7The plugin you're trying to remove does not exist!"));
  125.                             return true;
  126.                         }
  127.  
  128.                         Bukkit.getPluginManager().disablePlugin(pl);
  129.                         Runtime.getRuntime().exec("rm -r " + PLUGINSPATH + File.separatorChar + displayName);
  130.                         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"));
  131.                         current.removeIf(el -> el.toLowerCase().contains(displayName.toLowerCase()));
  132.                         SC.getConfig().set("PluginsEnabled", current);
  133.                         SC.saveConfig();
  134.                     } catch (IOException ex) {
  135.                         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."));
  136.                         ex.printStackTrace();
  137.                     }
  138.                     break;
  139.                 case "updateconfig":
  140.                     if (strings.length != 2) {
  141.                         sender.sendMessage(ChatColor.RED + "Not enough, or too many arguments!");
  142.                         return true;
  143.                     }
  144.  
  145.                     final Hastebin hb = new Hastebin();
  146.                     final String path = SC.getDataFolder().getAbsoluteFile().getParentFile().getAbsolutePath() + File.separatorChar + strings[0];
  147.                     final String link = strings[1]
  148.                         .replaceAll("https://", "")
  149.                         .replaceAll("http://", "")
  150.                         .replaceAll("hastebin\\.com", "")
  151.                         .replaceAll("paste\\.md-5\\.net", "")
  152.                         .replaceAll("/", "");
  153.                     final File editFile = new File(path);
  154.  
  155.                     if (!editFile.exists()) {
  156.                         sender.sendMessage(ColorUtils.translate("&9Error> &7The plugin you're trying to edit does not exist!"));
  157.                         return true;
  158.                     }
  159.  
  160.                     final String output = (link.toLowerCase().contains("hastebin")) ? hb.getPaste(false, link) : hb.getPaste(true, link);
  161.  
  162.                     try {
  163.                         final FileUtils fu = new FileUtils();
  164.                         fu.replaceConfig(output, new File(path));
  165.                     } catch (NullPointerException ex) {
  166.                         ex.printStackTrace();
  167.                         sender.sendMessage(ColorUtils.translate(ColorUtils.translate("&9Error> &7Error replacing config content.")));
  168.                         return true;
  169.                     }
  170.  
  171.                     sender.sendMessage(ColorUtils.translate("&9Config> &7You've successfully edited the config! You should restart the server now in order to apply the changes."));
  172.                     break;
  173.                 case "menu":
  174.                     new Thread(() -> {
  175.                         final Inventory gui = Bukkit.createInventory(sender, 3*9, ChatColor.BLACK + "Server Statistics");
  176.                         final String tps = String.format(Locale.US, "%.2f", TPSUtils.getRecentTps()[0]);
  177.                         final String tpsString = (Double.parseDouble(tps) > 20) ? "20.00" : tps;
  178.  
  179.                         ItemStack itemStack = XMaterial.CRAFTING_TABLE.parseItem();
  180.                         ItemMeta im = itemStack.getItemMeta();
  181.                         im.setDisplayName(ColorUtils.translate("&cStatistics & Info"));
  182.                         ArrayList<String> lore = new ArrayList<String>() {{
  183.                             add(ColorUtils.translate("&aUUID: &7" + SC.getConfig().getString("ServerUUID").split("-")[0]));
  184.                             add(ColorUtils.translate("&aTPS: &7" + tpsString));
  185.                             add(ColorUtils.translate("&aOnline: &7" + Bukkit.getOnlinePlayers().size() + " players"));
  186.                             add(ColorUtils.translate("&aWhitelist: &7" + Bukkit.getServer().hasWhitelist()));
  187.                             add(ColorUtils.translate("&aBanned: &7" + Bukkit.getServer().getBannedPlayers().size() + " players"));
  188.                         }};
  189.                         im.setLore(lore);
  190.                         itemStack.setItemMeta(im);
  191.                         gui.setItem(10, itemStack);
  192.  
  193.                         lore.clear();
  194.  
  195.                         itemStack = XMaterial.CLOCK.parseItem();
  196.                         im = itemStack.getItemMeta();
  197.                         im.setDisplayName(ColorUtils.translate("&cPlugin Manager"));
  198.                         lore.add(ColorUtils.translate("&7Click on this menu in order"));
  199.                         lore.add(ColorUtils.translate("&7to install new plugins or"));
  200.                         lore.add(ColorUtils.translate("&7to remove existing ones."));
  201.                         im.setLore(lore);
  202.                         itemStack.setItemMeta(im);
  203.                         gui.setItem(16, itemStack);
  204.  
  205.                         lore.clear();
  206.  
  207.                         itemStack = XMaterial.WRITABLE_BOOK.parseItem();
  208.                         im = itemStack.getItemMeta();
  209.                         im.setDisplayName(ColorUtils.translate("&cSet MOTD"));
  210.                         lore.add(ColorUtils.translate("&7Click here to set Server MOTD"));
  211.                         im.setLore(lore);
  212.                         itemStack.setItemMeta(im);
  213.                         gui.setItem(12, itemStack);
  214.  
  215.                         lore.clear();
  216.  
  217.                         itemStack = XMaterial.PLAYER_HEAD.parseItem();
  218.                         SkullMeta playerheadmeta = (SkullMeta) itemStack.getItemMeta();
  219.                         playerheadmeta.setOwner(sender.getName());
  220.                         playerheadmeta.setDisplayName(ColorUtils.translate("&cSet Max Players"));
  221.                         lore.add(ColorUtils.translate("&7Click here to set max players count"));
  222.                         playerheadmeta.setLore(lore);
  223.                         itemStack.setItemMeta(playerheadmeta);
  224.                         // TODO: Make permissions for this
  225.                         gui.setItem(14, itemStack);
  226.  
  227.                         Bukkit.getScheduler().runTask(SC, () -> sender.openInventory(gui));
  228.                     }).start();
  229.  
  230.                     return true;
  231.                 case "config":
  232.                     final Inventory i = Bukkit.createInventory(sender, 6 * 9, ColorUtils.translate("&0Config Manager"));
  233.  
  234.                     sender.sendMessage(ColorUtils.translate("&9Config> &7Loading the GUI..."));
  235.  
  236.                     new Thread(() -> {
  237.                         final File f = new File(SC.getDataFolder().getParentFile().getPath());
  238.                         final ArrayList<ItemStack> al = new ArrayList<>();
  239.                         final FormattingUtils fu = new FormattingUtils(SC);
  240.  
  241.                         for (File file : f.listFiles()) {
  242.  
  243.                             if (SC.getConfig().getStringList("DisabledAccess").contains(file.getName())) continue;
  244.  
  245.                             if (file.isDirectory()) {
  246.                                 if (file.getName().equalsIgnoreCase("PSServerCore")) continue;
  247.                                 ItemStack is = XMaterial.CHEST.parseItem();
  248.                                 ItemMeta im = is.getItemMeta();
  249.                                 im.setDisplayName(file.getName());
  250.                                 im.setLore(fu.location(file.getPath()));
  251.                                 is.setItemMeta(im);
  252.                                 i.addItem(is);
  253.                             } else {
  254.                                 final ItemStack is =
  255.                                         (file.getName().contains(".yml") || file.getName().contains(".json") || file.getName().contains(".toml") || file.getName().contains(".sk"))
  256.                                                 ? XMaterial.WRITABLE_BOOK.parseItem() : XMaterial.BOOK.parseItem();
  257.                                 ItemMeta im = is.getItemMeta();
  258.                                 im.setDisplayName(file.getName());
  259.                                 is.setItemMeta(im);
  260.                                 al.add(is);
  261.                             }
  262.                         }
  263.  
  264.                         al.forEach(i::addItem);
  265.  
  266.                         Bukkit.getScheduler().runTask(SC, () -> sender.openInventory(i));
  267.                     }).start();
  268.  
  269.                     return true;
  270.             }
  271.         }
  272.  
  273.         return true;
  274.     }
  275.  
  276.     private boolean hasAccess(Player player) {
  277.         return SC.getConfig().getStringList("Access").contains(player.getName().toLowerCase())
  278.                 || SC.getConfig().getString("Owner").equalsIgnoreCase(player.getName())
  279.                 || SC.getConfig().getString("Owner").equalsIgnoreCase(player.getUniqueId().toString())
  280.                 || SC.getConfig().getStringList("Access").contains(player.getUniqueId().toString())
  281.                 || SC.getConfig().getString("Owner").equalsIgnoreCase(player.getUniqueId().toString())
  282.                 || SC.getConfig().getString("Owner").equalsIgnoreCase(player.getUniqueId().toString());
  283.     }
  284.  
  285. }
  286.