Skip to content

Commit

Permalink
Fix problems with some plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
TonimatasDEV committed Dec 6, 2023
1 parent f350858 commit 49125cc
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ public void onEnable() {
getServer().getScheduler().scheduleSyncDelayedTask(this, () -> {
// Convert listeners and set worlds to commands.
ListenerManager.convert();
// Register all converted commands.
CommandManager.setWorldsToCommands();

// Send a message on finish all Events/Commands.
Bukkit.getConsoleSender().sendMessage("[PerWorldPlugins] " + ChatColor.GREEN + "Converted all Listeners/Commands correctly.");
Bukkit.getConsoleSender().sendMessage("[PerWorldPlugins] " + ChatColor.GREEN + "Converted all Listeners correctly.");
});
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import net.tonimatasdev.perworldplugins.PerWorldPlugins;
import net.tonimatasdev.perworldplugins.config.GroupsYML;
import net.tonimatasdev.perworldplugins.manager.CommandManager;
import net.tonimatasdev.perworldplugins.manager.ListenerManager;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
Expand Down Expand Up @@ -41,7 +40,6 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)
PerWorldPlugins.getInstance().reloadConfig();
GroupsYML.reload();
ListenerManager.setWorldsToEvents();
CommandManager.setWorldsToCommands();
sender.sendMessage(getPrefix(ChatColor.DARK_GREEN) + "The plugin has been reloaded.");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package net.tonimatasdev.perworldplugins.listener;

import net.tonimatasdev.perworldplugins.PerWorldPlugins;
import net.tonimatasdev.perworldplugins.api.PerWorldCommand;
import net.tonimatasdev.perworldplugins.manager.CommandManager;
import net.tonimatasdev.perworldplugins.util.PerWorldUtils;
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;
Expand Down Expand Up @@ -40,12 +41,13 @@ public void onCommandPreProcess(PlayerCommandPreprocessEvent event) {
Command command = CommandManager.getCommandMap().getCommand(commandString);
// Detects if the command is null.
if (command == null) return;
// Detects if the command is PerWorldCommand.
if (!(command instanceof PerWorldCommand)) return;
// Get PerWorldCommand.
PerWorldCommand perWorldCommand = (PerWorldCommand) command;
// Get the plugin of the command.
Plugin plugin = CommandManager.pluginMap.get(command);
// Detects if the plugin is null.
if (plugin == null) return;

// Check if the player is in the disabled world
if (perWorldCommand.getDisabledWorlds().contains(event.getPlayer().getWorld().getName())) {
if (PerWorldUtils.getDisabledWorlds(plugin).contains(event.getPlayer().getWorld().getName())) {
// Send block message to the player.
event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', Objects.requireNonNull(PerWorldPlugins.getInstance().getConfig().getString("disabledCommandMessage"))));
// Cancel the event.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.tonimatasdev.perworldplugins.manager;

import net.tonimatasdev.perworldplugins.api.PerWorldCommand;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.SimpleCommandMap;
Expand All @@ -9,54 +8,22 @@
import org.bukkit.plugin.SimplePluginManager;

import java.lang.reflect.Field;
import java.util.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public static void addPluginCommands(Plugin plugin) {
// Get all keys.
List<Command> replaced = new ArrayList<>();

for (String key : getCommands().keySet()) {
// Get all commands
for (Command command : getCommands().values()) {
// Detect if the command is a default command or is already registered
if (defaultCommands.contains(command.getName()) || pluginMap.containsKey(command)) continue;
// Get PerWorldCommand and add to perWorldCommands list.
Command command = getCommands().get(key);

// If a default command or PerWorldCommand, continue.
if (defaultCommands.contains(command.getName()) || command instanceof PerWorldCommand) continue;
// If the command are registered, continue.
if (replaced.contains(command)) continue;

// Replace command and add to replaced list.
replace(PerWorldCommand.get(command, plugin));

replaced.add(command);
}
}

public static void setWorldsToCommands() {
// Get all command map values.
for (Command command : getCommandMap().getCommands()) {
// Check if is a PerWorldCommand.
if (command instanceof PerWorldCommand) {
// Set disabled worlds to the command.
((PerWorldCommand) command).setDisabledWorlds();
}
}
}

public static void replace(PerWorldCommand command) {
// Replace command name.
getCommands().replace(command.getName(), command);
// Replace plugin + command name.
getCommands().replace(command.getPlugin().getName().toLowerCase(Locale.ENGLISH) + ":" + command.getName(), command);

// Replace all aliases.
for (String alias : command.getAliases()) {
// Replace alias.
getCommands().replace(alias, command);
// Replace plugin + alias.
getCommands().replace(command.getPlugin().getName().toLowerCase(Locale.ENGLISH) + ":" + alias, command);
pluginMap.put(command, plugin);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.tonimatasdev.perworldplugins.PerWorldPlugins;
import net.tonimatasdev.perworldplugins.config.GroupsYML;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.plugin.Plugin;

Expand Down Expand Up @@ -33,7 +34,12 @@ public static List<String> getDisabledWorlds(Plugin plugin) {
if (PerWorldPlugins.getInstance().getConfig().getBoolean("blacklist")) {
return withGroupWorlds;
} else {
List<String> serverWorlds = Bukkit.getWorlds().stream().map(World::getName).collect(Collectors.toList());
List<String> serverWorlds = new ArrayList<>();
try {
serverWorlds.addAll(Bukkit.getWorlds().stream().map(World::getName).collect(Collectors.toList()));
} catch (Exception e) {
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Error on get blocked worlds of: " + plugin.getName());
}
serverWorlds.removeAll(withGroupWorlds);
return serverWorlds;
}
Expand Down

0 comments on commit 49125cc

Please sign in to comment.