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 1fa7c8a
Show file tree
Hide file tree
Showing 19 changed files with 189 additions and 12 deletions.
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.pre-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 cancelledReason() {
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
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public String load(String name) {
PreLoadPluginEvent preloadEvent = new PreLoadPluginEvent(pluginFile.toPath(), description);
Bukkit.getPluginManager().callEvent(preloadEvent);
if (preloadEvent.isCancelled())
return null;
return preloadEvent.getCancelledReason();

try {
target = Bukkit.getPluginManager().loadPlugin(pluginFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ public String load(String name) {
PreLoadPluginEvent preloadEvent = new PreLoadPluginEvent(pluginFile.toPath(), description);
Bukkit.getPluginManager().callEvent(preloadEvent);
if (preloadEvent.isCancelled())
return null;
return preloadEvent.getCancelledReason();

try {
Class paper = Class.forName("io.papermc.paper.plugin.manager.PaperPluginManagerImpl");
Expand Down Expand Up @@ -488,6 +488,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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
public class PreLoadPluginEvent extends Event implements Cancellable {
private @NotNull final Path pluginPath;
private @NotNull final String pluginName;

private String cancelledReason;
private boolean isCancelled;

public PreLoadPluginEvent(@NotNull Path pluginPath, @NotNull String pluginName) {
Expand All @@ -25,6 +27,18 @@ public PreLoadPluginEvent(@NotNull Path pluginPath, @NotNull String pluginName)
return pluginName;
}

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

public @NotNull String getCancelledReason() {
if (this.cancelledReason == null) {
// default message
return "§cPlugin §4"+pluginName+"§c load has been canceled.";
}
return cancelledReason;
}

@Override
public boolean isCancelled() {
return isCancelled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
public class PreReloadPluginEvent extends Event implements Cancellable {
private @NotNull final Plugin plugin;
private @NotNull final String pluginName;

private String cancelledReason;
private boolean isCancelled;

public PreReloadPluginEvent(@NotNull Plugin plugin, @NotNull String pluginName) {
Expand All @@ -33,4 +35,16 @@ public void setCancelled(boolean cancelled) {
public @NotNull String getPluginName() {
return pluginName;
}

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

public @NotNull String getCancelledReason() {
if (this.cancelledReason == null) {
// default message
return "§cPlugin §4"+pluginName+"§c reload has been canceled.";
}
return cancelledReason;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
public class PreUnloadPluginEvent extends Event implements Cancellable {
private @NotNull final Plugin plugin;
private @NotNull final String pluginName;

private String cancelledReason;
private boolean isCancelled;

public PreUnloadPluginEvent(@NotNull Plugin plugin, @NotNull String pluginName) {
Expand All @@ -26,6 +28,18 @@ public void setCancelled(boolean cancelled) {
this.isCancelled = cancelled;
}

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

public @NotNull String getCancelledReason() {
if (this.cancelledReason == null) {
// default message
return "§cPlugin §4"+pluginName+"§c unload has been canceled.";
}
return cancelledReason;
}

public @NotNull Plugin getPlugin() {
return plugin;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public void execute(CommandSender sender, String[] args) {
}

PreLoadPluginEvent result = PlugManBungee.getInstance().getProxy().getPluginManager().callEvent(new PreLoadPluginEvent(file.toPath(), filename));
if (result.isCancelled()) return;
if (result.isCancelled()) {
sendMessage(sender, result.getCancelledReason());
return;
};

PluginResult pluginResult = BungeePluginUtil.loadPlugin(file);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public void execute(CommandSender sender, String[] args) {

Plugin plugin = pluginManager.getPlugin(pluginName);
PreReloadPluginEvent result = PlugManBungee.getInstance().getProxy().getPluginManager().callEvent(new PreReloadPluginEvent(plugin, pluginName));
if (result.isCancelled()) return;
if (result.isCancelled()) {
sendMessage(sender, result.getCancelledReason());
return;
};

Map.Entry<PluginResult, PluginResult> pluginResults = BungeePluginUtil.reloadPlugin(plugin);
this.sendMessage(sender, pluginResults.getKey().getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ public void execute(CommandSender sender, String[] args) {

Plugin plugin = pluginManager.getPlugin(pluginName);
PreUnloadPluginEvent result = PlugManBungee.getInstance().getProxy().getPluginManager().callEvent(new PreUnloadPluginEvent(plugin, pluginName));
if (result.isCancelled()) return;
if (result.isCancelled()) {
sendMessage(sender, result.getCancelledReason());
return;
};

PluginResult pluginResult = BungeePluginUtil.unloadPlugin(plugin);
sendMessage(sender, pluginResult.getMessage());
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ check:
unknown: '&cVersion information is unknown.'
unknown-player: '&cUnknown (Installed): {0}'
not-available: '&cVersion information for {0} is unavailable.'
cancel:
disable:
one: '§9The deactivation of plugin §4"{0}"§9 has been canceled.'
all: '§9The deactivation of all plugins has been canceled.'
enable: '§9The activation of plugin §4"{0}"§9 has been canceled.'
load: '§9The loading of plugin §4"{0}"§9 has been canceled.'
reload: '§9The reloading of plugin §4"{0}"§9 has been canceled.'
unload: '§9The unloading of plugin §4"{0}"§9 has been canceled.'
disable:
all: '&9All plugins have been disabled (excluding PlugMan).'
already-disabled: '&c{0} is already disabled.'
Expand Down
Loading

0 comments on commit 1fa7c8a

Please sign in to comment.