Skip to content

Commit

Permalink
Update the comments
Browse files Browse the repository at this point in the history
  • Loading branch information
TonimatasDEV committed Mar 8, 2024
1 parent 9307c60 commit 6e4d805
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static PerWorldPlugins getInstance() {

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

Expand Down Expand Up @@ -62,8 +62,9 @@ public void onEnable() {

@Override
public void onDisable() {
// Save config and disable the plugin.
// Reload the config.
this.reloadConfig();
// Save the config.
this.saveConfig();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@ public class PerWorldRegisteredListener extends RegisteredListener {

public PerWorldRegisteredListener(Listener listener, EventExecutor executor, EventPriority priority, Plugin plugin, boolean ignoreCancelled) {
super(listener, executor, priority, plugin, ignoreCancelled);
this.disabledWorlds = new ArrayList<>();
this.disabledWorlds = new ArrayList<>(); // Set empty list.
}

@SuppressWarnings("NullableProblems")
public void callEvent(Event event) {
// If X-Events get player and detect if blocked so that it is not run.

if (PerWorldUtils.checkEvent(event, this.disabledWorlds)) return;

try {
// Execute the event if it is not blocked.
super.callEvent(event);
} catch (Throwable ex) {
// Send error message.
Bukkit.getServer().getLogger().log(Level.SEVERE, "Could not pass event " + event.getEventName() + " to " + getPlugin().getName() + " v" + getPlugin().getDescription().getVersion(), ex);
}
}

// Method to set all disabled worlds.
public void setDisabledWorlds() {
this.disabledWorlds = PerWorldUtils.getDisabledWorlds(getPlugin());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class PerWorldTimedRegisteredListener extends TimedRegisteredListener {

public PerWorldTimedRegisteredListener(Listener listener, EventExecutor executor, EventPriority priority, Plugin plugin, boolean ignoreCancelled) {
super(listener, executor, priority, plugin, ignoreCancelled);
this.disabledWorlds = new ArrayList<>();
this.disabledWorlds = new ArrayList<>(); // Set empty list.
}

@SuppressWarnings("NullableProblems")
Expand All @@ -30,10 +30,12 @@ public void callEvent(Event event) {
// Execute the event if it is not blocked.
super.callEvent(event);
} catch (Throwable ex) {
// Send error message.
Bukkit.getServer().getLogger().log(Level.SEVERE, "Could not pass event " + event.getEventName() + " to " + getPlugin().getName() + " v" + getPlugin().getDescription().getVersion(), ex);
}
}

// Method to set all disabled worlds.
public void setDisabledWorlds() {
this.disabledWorlds = PerWorldUtils.getDisabledWorlds(getPlugin());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@SuppressWarnings("NullableProblems")
public class PrimaryCommand extends Command {
Expand Down Expand Up @@ -51,9 +50,11 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)

// Method to detect if the player has permissions or not with a message in false case.
private boolean hasPermission(CommandSender sender, String permission) {
// Check if the sender has permissions.
if (sender.hasPermission(permission)) {
return true;
} else {
// Send a message if sender not has permissions.
sender.sendMessage(getPrefix(ChatColor.DARK_RED) + "You don't have permissions for execute this command");
return false;
}
Expand All @@ -67,20 +68,21 @@ private String getPrefix(ChatColor chatColor) {
// Command tab completer.
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
// If the command name is "perworldplugins"
// If the command name is "perworldplugins".
if (alias.equalsIgnoreCase("perworldplugins")) {
// Create a list for arguments
// Create a list for arguments.
List<String> argList = new ArrayList<>();

// If argument is 1.
if (Objects.requireNonNull(args).length == 1) {
// Add the possible arguments.
argList.add("version");
argList.add("reload");
return argList.stream().filter(a -> a.startsWith(args[0])).collect(Collectors.toList());
return argList;
}
}

// Return an empty list if nothing works.
return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ public void onPluginEnable(PluginEnableEvent event) {
CommandManager.addPluginCommands(event.getPlugin());

// Create individual sections for plugins in the config.

if (PerWorldPlugins.getInstance().getConfig().getStringList("plugins." + event.getPlugin().getName()).isEmpty()) {
PerWorldPlugins.getInstance().getConfig().set("plugins." + event.getPlugin().getName(), Collections.singletonList("Example"));
PerWorldPlugins.getInstance().saveConfig();
PerWorldPlugins.getInstance().reloadConfig();
PerWorldPlugins.getInstance().getConfig().set("plugins." + event.getPlugin().getName(), Collections.singletonList("Example")); // Add default list.
PerWorldPlugins.getInstance().saveConfig(); // Save config.
PerWorldPlugins.getInstance().reloadConfig(); // Reload config.
}
}

Expand All @@ -53,6 +52,7 @@ public void onCommandPreProcess(PlayerCommandPreprocessEvent event) {
Objects.requireNonNull(Objects.requireNonNull(PerWorldPlugins.getInstance().getConfig().getString("disabledCommandMessage"))
.replaceAll("\\{world}", event.getPlayer().getWorld().getName())
.replaceAll("\\{player}", event.getPlayer().getName()))));

// Cancel the event.
event.setCancelled(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class CommandManager implements Listener {
public static void addPluginCommands(Plugin plugin) {
// Get all commands
for (Command command : getCommands().values()) {
// Detect if the command is a default command or is already registered
// 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.
pluginMap.put(command, plugin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,32 +58,33 @@ public static void convert() {
}

public static void setWorldsToEvents() {
// Get PerWorldPlugins
// Get PerWorldPlugins.
Plugin perWorldPlugins = PerWorldPlugins.getInstance();

for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
// Check if the plugin is PerWorldPlugins
// Check if the plugin is PerWorldPlugins.
if (plugin.equals(perWorldPlugins)) continue;

for (RegisteredListener registeredListener : HandlerList.getRegisteredListeners(plugin)) {

// Create handled variable
// Create handled variable.
boolean handled = false;

// Use try because, some performance server softwares delete TimedRegisteredListener class
// Use try because, some performance server softwares delete TimedRegisteredListener class.
try {
if (registeredListener instanceof PerWorldRegisteredListener) {
// RegisteredListener to PerWorldTimedRegisteredListener.
((PerWorldRegisteredListener) registeredListener).setDisabledWorlds();
// Change handled to true
// Change handled to true.
handled = true;
}
} catch (Exception ignore) {
// Do nothing
// Do nothing.
}

// Check if it is handled or is a PerWorldTimeRegisteredListener
// Check if it is handled or is a PerWorldTimeRegisteredListener.
if (!handled && registeredListener instanceof PerWorldTimedRegisteredListener){
// Set disabled worlds.
((PerWorldTimedRegisteredListener) registeredListener).setDisabledWorlds();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ public class PerWorldUtils {
public static List<String> getDisabledWorlds(Plugin plugin) {
List<String> worldList = PerWorldPlugins.getInstance().getConfig().getStringList("plugins." + plugin.getName());

// Check if it has and :ignore.
if (worldList.contains(":ignore") || worldList.isEmpty()) return new ArrayList<>();

List<String> withGroupWorlds = new ArrayList<>(worldList);
// Check if any group contains the world
// Check if any group contains the world.
for (String var : worldList) {
// Detects if the string starts with "g:".
if (var.startsWith("g:")) {
Expand All @@ -40,22 +41,26 @@ public static List<String> getDisabledWorlds(Plugin plugin) {
}
}

// Check if the config is a blacklist or whitelist.
if (PerWorldPlugins.getInstance().getConfig().getBoolean("blacklist")) {
return withGroupWorlds;
return withGroupWorlds; // Return blacklist worlds.
} else {
List<String> serverWorlds = new ArrayList<>();
try {
// Add all world names.
serverWorlds.addAll(Bukkit.getWorlds().stream().map(World::getName).collect(Collectors.toList()));
} catch (Exception e) {
// Send error message on console.
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Error on get blocked worlds of: " + plugin.getName());
}
// Remove all whitelisted worlds.
serverWorlds.removeAll(withGroupWorlds);
return serverWorlds;
return serverWorlds; // Return not whitelisted worlds.
}
}

public static boolean checkEvent(Event event, List<String> disabledWorlds) {
// Get world for every type of Event
// Get world for every type of Event.
if (event instanceof PlayerEvent) {
return disabledWorlds.contains(((PlayerEvent) event).getPlayer().getWorld().getName());
} else if (event instanceof EntityEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static void check() {
Bukkit.getConsoleSender().sendMessage(PerWorldPlugins.getInstance().getName() + ChatColor.RED + " You can download it at: " + ChatColor.WHITE + "https://www.spigotmc.org/resources/perworldplugins.96161/");
}
} catch (Exception var3) {
// Send the error message.
Bukkit.getConsoleSender().sendMessage(PerWorldPlugins.getInstance().getName() + ChatColor.RED + " Error while checking update.");
}
}
Expand Down

0 comments on commit 6e4d805

Please sign in to comment.