Skip to content

Commit

Permalink
The reason why the event is canceled
Browse files Browse the repository at this point in the history
  • Loading branch information
404Setup committed Oct 9, 2024
1 parent 50ba482 commit 86b3e9b
Show file tree
Hide file tree
Showing 23 changed files with 204 additions and 32 deletions.
12 changes: 4 additions & 8 deletions src/main/java/com/rylinaux/plugman/PlugMan.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,7 @@ public void onEnable() {
for (File file : Arrays.stream(new File("plugins").listFiles()).filter(File::isFile).filter(file -> file.getName().toLowerCase(Locale.ROOT).endsWith(".jar")).collect(Collectors.toList()))
if (!this.fileHashMap.containsKey(file.getName())) {
Bukkit.getScheduler().runTask(this, () -> {
String message = PlugMan.getInstance().getPluginUtil().load(file.getName().replace(".jar", ""));
if (message != null) Bukkit.getConsoleSender().sendMessage(message);
Bukkit.getConsoleSender().sendMessage(PlugMan.getInstance().getPluginUtil().load(file.getName().replace(".jar", "")));
});
String hash = null;
try {
Expand Down Expand Up @@ -312,8 +311,7 @@ public void onEnable() {
if (PlugMan.getInstance().getPluginUtil().isIgnored(plugin)) continue;
this.fileHashMap.remove(fileName);
Bukkit.getScheduler().runTask(this, () -> {
String message = PlugMan.getInstance().getPluginUtil().unload(plugin);
if (message != null) Bukkit.getConsoleSender().sendMessage(message);
Bukkit.getConsoleSender().sendMessage(PlugMan.getInstance().getPluginUtil().unload(plugin));
});
}
}, this.getConfig().getLong("auto-unload.check-every-seconds") * 20, this.getConfig().getLong("auto-unload.check-every-seconds") * 20);
Expand Down Expand Up @@ -351,10 +349,8 @@ public void onEnable() {
this.fileHashMap.put(file.getName(), hash);

Bukkit.getScheduler().runTask(this, () -> {
String message1 = PlugMan.getInstance().getPluginUtil().unload(plugin);
if (message1 != null) Bukkit.getConsoleSender().sendMessage(message1);
String message2 = PlugMan.getInstance().getPluginUtil().load(plugin.getName());
if (message2 != null) Bukkit.getConsoleSender().sendMessage(message2);
Bukkit.getConsoleSender().sendMessage(PlugMan.getInstance().getPluginUtil().unload(plugin));
Bukkit.getConsoleSender().sendMessage(PlugMan.getInstance().getPluginUtil().load(plugin.getName()));
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rylinaux.plugman.api.event;

import com.rylinaux.plugman.PlugMan;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
Expand All @@ -9,13 +10,18 @@

public class PreDisablePluginEvent extends Event implements Cancellable {
private static final HandlerList HANDLERS_LIST = new HandlerList();

/**If all plugin, then this item is null*/
private @Nullable final Plugin plugin;
private final boolean disableAll;
/**Determine whether you are ready to disable all plugin*/
private final boolean isDisableAll;

private String cancelledReason;
private boolean isCancelled;

public PreDisablePluginEvent(@Nullable Plugin plugin, boolean disableAll) {
public PreDisablePluginEvent(@Nullable Plugin plugin, boolean isDisableAll) {
this.plugin = plugin;
this.disableAll = disableAll;
this.isDisableAll = isDisableAll;
this.isCancelled = false;
}

Expand All @@ -38,12 +44,28 @@ public static HandlerList getHandlerList() {
return HANDLERS_LIST;
}

public void setCancelledReason(@NotNull String reason) {
this.cancelledReason = reason;
}

public @NotNull String getCancelledReason() {
if (this.cancelledReason == null) {
// default message
if (plugin != null) {
return PlugMan.getInstance().getMessageFormatter().format("cancel.disable.one", plugin.getName());
}
return PlugMan.getInstance().getMessageFormatter().format("cancel.disable.all");
}
return cancelledReason;
}

/**If all plugin, then this item is null*/
public @Nullable Plugin getPlugin() {
return plugin;
}

public boolean getDisableAll() {
return disableAll;
/**Determine whether you are ready to disable all plugin*/
public boolean isDisableAll() {
return isDisableAll;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.rylinaux.plugman.api.event;

import com.rylinaux.plugman.PlugMan;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class PreEnablePluginEvent extends Event implements Cancellable {
private static final HandlerList HANDLERS_LIST = new HandlerList();

private @NotNull final Plugin plugin;

private String cancelledReason;
private boolean isCancelled;

public PreEnablePluginEvent(@NotNull Plugin plugin) {
Expand All @@ -35,6 +40,18 @@ public static HandlerList getHandlerList() {
return HANDLERS_LIST;
}

public void setCancelledReason(@NotNull String reason) {
this.cancelledReason = reason;
}

public @NotNull String getCancelledReason() {
if (this.cancelledReason == null) {
// default message
return PlugMan.getInstance().getMessageFormatter().format("cancel.enable", plugin.getName());
}
return cancelledReason;
}

public @NotNull Plugin getPlugin() {
return plugin;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rylinaux.plugman.api.event;

import com.rylinaux.plugman.PlugMan;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
Expand All @@ -10,8 +11,11 @@

public class PreLoadPluginEvent extends Event implements Cancellable {
private static final HandlerList HANDLERS_LIST = new HandlerList();

private final @NotNull Path pluginPath;
private final @NotNull PluginDescriptionFile desc;

private String cancelledReason;
private boolean isCancelled;

public PreLoadPluginEvent(@NotNull Path pluginPath, @NotNull PluginDescriptionFile desc) {
Expand Down Expand Up @@ -39,6 +43,18 @@ public void setCancelled(boolean cancelled) {
return HANDLERS_LIST;
}

public void setCancelledReason(@NotNull String reason) {
this.cancelledReason = reason;
}

public @NotNull String getCancelledReason() {
if (this.cancelledReason == null) {
// default message
return PlugMan.getInstance().getMessageFormatter().format("cancel.load");
}
return cancelledReason;
}

public @NotNull Path getPluginPath() {
return pluginPath;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rylinaux.plugman.api.event;

import com.rylinaux.plugman.PlugMan;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
Expand All @@ -8,7 +9,10 @@

public class PreReloadPluginEvent extends Event implements Cancellable {
private static final HandlerList HANDLERS_LIST = new HandlerList();

private @NotNull final Plugin plugin;

private String cancelledReason;
private boolean isCancelled;

public PreReloadPluginEvent(@NotNull Plugin plugin) {
Expand All @@ -35,6 +39,18 @@ public static HandlerList getHandlerList() {
return HANDLERS_LIST;
}

public void setCancelledReason(@NotNull String reason) {
this.cancelledReason = reason;
}

public @NotNull String cancelledReason() {
if (this.cancelledReason == null) {
// default message
return PlugMan.getInstance().getMessageFormatter().format("cancel.reload", plugin.getName());
}
return cancelledReason;
}

public @NotNull Plugin getPlugin() {
return plugin;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rylinaux.plugman.api.event;

import com.rylinaux.plugman.PlugMan;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
Expand All @@ -8,7 +9,10 @@

public class PreUnloadPluginEvent extends Event implements Cancellable {
private static final HandlerList HANDLERS_LIST = new HandlerList();

private @NotNull final Plugin plugin;

private String cancelledReason;
private boolean isCancelled;

public PreUnloadPluginEvent(@NotNull Plugin plugin) {
Expand All @@ -35,6 +39,18 @@ public static HandlerList getHandlerList() {
return HANDLERS_LIST;
}

public void setCancelledReason(@NotNull String reason) {
this.cancelledReason = reason;
}

public @NotNull String getCancelledReason() {
if (this.cancelledReason == null) {
// default message
return PlugMan.getInstance().getMessageFormatter().format("cancel.unload", plugin.getName());
}
return cancelledReason;
}

public @NotNull Plugin getPlugin() {
return plugin;
}
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/rylinaux/plugman/command/DisableCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ public void execute(CommandSender sender, Command command, String label, String[
if (this.hasPermission("all")) {
PreDisablePluginEvent preDisableEvent = new PreDisablePluginEvent(null, true);
Bukkit.getPluginManager().callEvent(preDisableEvent);
if (preDisableEvent.isCancelled()) return;
if (preDisableEvent.isCancelled()) {
sender.sendMessage(preDisableEvent.getCancelledReason());
return;
};
PlugMan.getInstance().getPluginUtil().disableAll();
sender.sendMessage(PlugMan.getInstance().getMessageFormatter().format("disable.all"));
} else sender.sendMessage(PlugMan.getInstance().getMessageFormatter().format("error.no-permission"));
Expand Down Expand Up @@ -131,7 +134,10 @@ public void execute(CommandSender sender, Command command, String label, String[

PreDisablePluginEvent preDisableEvent = new PreDisablePluginEvent(target, false);
Bukkit.getPluginManager().callEvent(preDisableEvent);
if (preDisableEvent.isCancelled()) return;
if (preDisableEvent.isCancelled()) {
sender.sendMessage(preDisableEvent.getCancelledReason());
return;
}

PlugMan.getInstance().getPluginUtil().disable(target);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ public void execute(CommandSender sender, Command command, String label, String[

PreEnablePluginEvent preEnableEvent = new PreEnablePluginEvent(target);
Bukkit.getPluginManager().callEvent(preEnableEvent);
if (preEnableEvent.isCancelled()) return;
if (preEnableEvent.isCancelled()) {
sender.sendMessage(preEnableEvent.getCancelledReason());
return;
};

PlugMan.getInstance().getPluginUtil().enable(target);

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/rylinaux/plugman/command/LoadCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ public void execute(CommandSender sender, Command command, String label, String[
return;
}

String message = PlugMan.getInstance().getPluginUtil().load(name);
if (message != null) sender.sendMessage(message);
sender.sendMessage(PlugMan.getInstance().getPluginUtil().load(name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ public void execute(CommandSender sender, Command command, String label, String[
return;
}

String message = PlugMan.getInstance().getPluginUtil().unload(target);
if (message != null) sender.sendMessage(message);
sender.sendMessage(PlugMan.getInstance().getPluginUtil().unload(target));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,7 @@ public String load(String name) {

PreLoadPluginEvent preloadEvent = new PreLoadPluginEvent(pluginFile.toPath(), description);
Bukkit.getPluginManager().callEvent(preloadEvent);
if (preloadEvent.isCancelled())
return null;
if (preloadEvent.isCancelled()) return preloadEvent.getCancelledReason();

try {
target = Bukkit.getPluginManager().loadPlugin(pluginFile);
Expand Down Expand Up @@ -500,6 +499,7 @@ public void reload(Plugin plugin) {
PreReloadPluginEvent preReloadEvent = new PreReloadPluginEvent(plugin);
Bukkit.getPluginManager().callEvent(preReloadEvent);
if (preReloadEvent.isCancelled()) return;

this.unload(plugin);
this.load(plugin);
}
Expand All @@ -525,7 +525,7 @@ public void reloadAll() {
public synchronized String unload(Plugin plugin) {
PreUnloadPluginEvent preUnloadEvent = new PreUnloadPluginEvent(plugin);
Bukkit.getPluginManager().callEvent(preUnloadEvent);
if (preUnloadEvent.isCancelled()) return null;
if (preUnloadEvent.isCancelled()) return preUnloadEvent.getCancelledReason();

String name = plugin.getName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ModernPaperPluginManager(BukkitPluginManager bukkitPluginManager) {
public String unload(Plugin plugin) {
PreUnloadPluginEvent preUnloadEvent = new PreUnloadPluginEvent(plugin);
Bukkit.getPluginManager().callEvent(preUnloadEvent);
if (preUnloadEvent.isCancelled()) return null;
if (preUnloadEvent.isCancelled()) return preUnloadEvent.getCancelledReason();

String name = plugin.getName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import com.rylinaux.plugman.api.event.PreUnloadPluginEvent;
import com.rylinaux.plugman.util.BukkitCommandWrapUseless;
import com.rylinaux.plugman.util.StringUtil;
import com.tcoded.folialib.FoliaLib;
import io.papermc.paper.plugin.configuration.PluginMeta;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
Expand Down Expand Up @@ -402,8 +401,7 @@ public String load(String name) {

PreLoadPluginEvent preloadEvent = new PreLoadPluginEvent(pluginFile.toPath(), description);
Bukkit.getPluginManager().callEvent(preloadEvent);
if (preloadEvent.isCancelled())
return null;
if (preloadEvent.isCancelled()) return preloadEvent.getCancelledReason();

try {
Class paper = Class.forName("io.papermc.paper.plugin.manager.PaperPluginManagerImpl");
Expand Down Expand Up @@ -444,7 +442,7 @@ public String load(String name) {
Plugin finalTarget = target;

if (this.isFolia()) {
FoliaLib foliaLib = new FoliaLib(PlugMan.getInstance());
com.tcoded.folialib.FoliaLib foliaLib = new com.tcoded.folialib.FoliaLib(PlugMan.getInstance());

foliaLib.getImpl().runLater(() -> {
this.loadCommands(finalTarget);
Expand Down Expand Up @@ -488,6 +486,7 @@ public void reload(Plugin plugin) {
PreReloadPluginEvent preReloadEvent = new PreReloadPluginEvent(plugin);
Bukkit.getPluginManager().callEvent(preReloadEvent);
if (preReloadEvent.isCancelled()) return;

this.unload(plugin);
this.load(plugin);
}
Expand All @@ -513,7 +512,7 @@ public void reloadAll() {
public String unload(Plugin plugin) {
PreUnloadPluginEvent preUnloadEvent = new PreUnloadPluginEvent(plugin);
Bukkit.getPluginManager().callEvent(preUnloadEvent);
if (preUnloadEvent.isCancelled()) return null;
if (preUnloadEvent.isCancelled()) return preUnloadEvent.getCancelledReason();

String name = plugin.getName();

Expand Down
Loading

0 comments on commit 86b3e9b

Please sign in to comment.