Skip to content

Commit

Permalink
Delete thread, enable the plugin on load and clear CommandManager on …
Browse files Browse the repository at this point in the history
…finish
  • Loading branch information
TonimatasDEV committed Nov 10, 2023
1 parent 8891723 commit 6257274
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,21 @@
import net.tonimatasdev.perworldplugins.util.UpdateChecker;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

public final class PerWorldPlugins extends JavaPlugin {
private static PerWorldPlugins instance;
private static final Thread thread = new Thread(() -> {
List<String> registered = new ArrayList<>();

while (true) {
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
// Detect if the plugin is registered.
if (registered.contains(plugin.getName())) continue;
// Detect if the plugin is enabled.
if (!plugin.isEnabled()) continue;
// Detect if the plugin is PerWorldPlugins.
if (plugin.equals(PerWorldPlugins.getInstance())) return;

// Add plugin commands to PerWorldPlugins command manager.
CommandManager.addPluginCommands(plugin);
// Add the plugin to registered list.
registered.add(plugin.getName());
}
}
});

public static PerWorldPlugins getInstance() {
return instance;
}

@Override
public void onLoad() {
// Start thread.
thread.start();
// Enable PerWorldPlugins
this.getPluginLoader().enablePlugin(this);
}

@Override
Expand Down Expand Up @@ -83,17 +60,6 @@ public void onEnable() {
// Register all converted commands.
CommandManager.init();

// Create individual sections for plugins in the config.
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if (!PerWorldPlugins.getInstance().getConfig().getStringList("plugins." + plugin.getName()).isEmpty()) continue;
PerWorldPlugins.getInstance().getConfig().set("plugins." + plugin.getName(), Collections.singletonList("Example"));
PerWorldPlugins.getInstance().saveConfig();
PerWorldPlugins.getInstance().reloadConfig();
}

// Stop the thread.
thread.interrupt();

// Send a message on finish all Events/Commands.
Bukkit.getConsoleSender().sendMessage("[PerWorldPlugins] " + ChatColor.GREEN + "Converted all Listeners/Commands correctly.");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.tonimatasdev.perworldplugins.util.PerWorldUtils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.Plugin;

import java.util.ArrayList;
Expand All @@ -24,7 +25,7 @@ public PerWorldCommand(Command command, Plugin plugin) {
setUsage(command.getUsage());

// Add plugin and disabled worlds.
this.plugin = plugin;
this.plugin = command instanceof PluginCommand ? ((PluginCommand) command).getPlugin() : plugin;
this.disabledWorlds = new ArrayList<>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,37 @@
import net.tonimatasdev.perworldplugins.PerWorldPlugins;
import net.tonimatasdev.perworldplugins.api.PerWorldCommand;
import net.tonimatasdev.perworldplugins.manager.CommandManager;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.server.PluginEnableEvent;
import org.bukkit.plugin.Plugin;

import java.util.Collections;
import java.util.Objects;

public class Listeners implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
public void onPluginEnable(PluginEnableEvent event) {
// Detect if the plugin is PerWorldPlugins.
if (event.getPlugin().equals(PerWorldPlugins.getInstance())) return;

// Add plugin commands to PerWorldPlugins command manager.
CommandManager.addPluginCommands(event.getPlugin());

// Create individual sections for plugins in the config.
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if (!PerWorldPlugins.getInstance().getConfig().getStringList("plugins." + plugin.getName()).isEmpty()) continue;
PerWorldPlugins.getInstance().getConfig().set("plugins." + plugin.getName(), Collections.singletonList("Example"));
PerWorldPlugins.getInstance().saveConfig();
PerWorldPlugins.getInstance().reloadConfig();
}
}

@EventHandler(priority = EventPriority.MONITOR)
public void onCommandPreProcess(PlayerCommandPreprocessEvent event) {
// Get command string.
Expand All @@ -25,7 +46,6 @@ public void onCommandPreProcess(PlayerCommandPreprocessEvent event) {
if (!(command instanceof PerWorldCommand)) return;
// Get PerWorldCommand.
PerWorldCommand perWorldCommand = (PerWorldCommand) command;
System.out.println(perWorldCommand.getPlugin().getName());
// Check if the player is in the disabled world
if (perWorldCommand.getDisabledWorlds().contains(event.getPlayer().getWorld().getName())) {
// Send block message to the player.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@

public class CommandManager implements Listener {
private static final List<String> defaultCommands = Arrays.asList("version", "timings", "reload", "plugins", "tps", "mspt", "paper", "spigot", "restart", "perworldplugins");

private static final Map<String, PerWorldCommand> perWorldCommands = new HashMap<>();
//private static final List<PerWorldCommand> perWorldCommands = new ArrayList<>();

public static void init() {
// Replace all Commands to PerWorldCommands.
perWorldCommands.keySet().forEach(key -> getCommands().replace(key, perWorldCommands.get(key)));

// Set the blocked worlds to the commands.
setWorldsToCommands();
perWorldCommands.clear();
}

public static void addPluginCommands(Plugin plugin) {
Expand All @@ -32,7 +31,7 @@ public static void addPluginCommands(Plugin plugin) {
Command command = getCommands().get(commandKey);
// Check if it is default command, PerWorldCommand or is a registered key.
if (defaultCommands.contains(command.getName()) || command instanceof PerWorldCommand || perWorldCommands.containsKey(commandKey)) continue;
// Get and add a PerWorldCommand to perWorldCommands list.
// Get and add a PerWorldCommand to perWorldCommands map.
perWorldCommands.put(commandKey, PerWorldCommand.get(command, plugin));
}
}
Expand Down

0 comments on commit 6257274

Please sign in to comment.