Skip to content

Commit

Permalink
Merge pull request #167 from HelpChat/issue/3
Browse files Browse the repository at this point in the history
Fix plugin reload only reloading main command.
  • Loading branch information
Kqliber authored Jan 12, 2025
2 parents ee7573d + 4a0e28b commit 44192ea
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 150 deletions.
5 changes: 3 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
java
id("com.github.johnrengelman.shadow") version("8.1.1")
id("com.github.ben-manes.versions") version "0.48.0"
id("com.gradleup.shadow") version("8.3.5")
id("com.github.ben-manes.versions") version("0.51.0")
}

// Change to true when releasing
Expand All @@ -18,6 +18,7 @@ repositories {
maven("https://repo.extendedclip.com/content/repositories/placeholderapi/")
maven("https://repo.glaremasters.me/repository/public/")
maven("https://nexus.phoenixdevt.fr/repository/maven-public/")
maven("https://repo.oraxen.com/releases")
maven("https://jitpack.io")
}

Expand Down
20 changes: 10 additions & 10 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
[versions]
# Compile only
spigot = "1.21.3-R0.1-SNAPSHOT"
spigot = "1.21.4-R0.1-SNAPSHOT"
vault = "1.7.1"
authlib = "1.5.25"
headdb = "1.3.1"
itemsadder = "3.2.5"
oraxen = "1.159.0"
mythiclib = "1.6.2-SNAPSHOT"
mmoitems = "6.9.5-SNAPSHOT"
headdb = "1.3.2"
itemsadder = "3.6.3-beta-14"
oraxen = "1.187.0"
mythiclib = "1.7.1-SNAPSHOT"
mmoitems = "6.10-SNAPSHOT"
papi = "2.11.6"
score = "4.23.10.8"
score = "4.24.3.5"

# Implementation
nashorn = "15.4"
nashorn = "15.6"
adventure-platform = "4.3.4"
adventure-minimessage = "4.17.0"
adventure-minimessage = "4.18.0"

[libraries]
# Compile only
Expand All @@ -23,7 +23,7 @@ vault = { module = "com.github.milkbowl:VaultAPI", version.ref = "vault" }
authlib = { module = "com.mojang:authlib", version.ref = "authlib" }
headdb = { module = "com.arcaniax:HeadDatabase-API", version.ref = "headdb" }
itemsadder = { module = "com.github.LoneDev6:api-itemsadder", version.ref = "itemsadder" }
oraxen = { module = "com.github.oraxen:oraxen", version.ref = "oraxen" }
oraxen = { module = "io.th0rgal:oraxen", version.ref = "oraxen" }
mythiclib = { module = "io.lumine:MythicLib-dist", version.ref = "mythiclib"}
mmoitems = { module = "net.Indyuce:MMOItems-API", version.ref = "mmoitems" }
papi = { module = "me.clip:placeholderapi", version.ref = "papi" }
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
16 changes: 8 additions & 8 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down Expand Up @@ -145,15 +145,15 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -202,11 +202,11 @@ fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.

set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
Expand Down
20 changes: 10 additions & 10 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand All @@ -57,11 +57,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.extendedclip.deluxemenus.action.ClickActionTask;
import com.extendedclip.deluxemenus.action.ClickHandler;
import com.extendedclip.deluxemenus.hooks.ItemHook;
import com.extendedclip.deluxemenus.menu.LoreAppendMode;
import com.extendedclip.deluxemenus.menu.options.LoreAppendMode;
import com.extendedclip.deluxemenus.menu.Menu;
import com.extendedclip.deluxemenus.menu.MenuHolder;
import com.extendedclip.deluxemenus.menu.MenuItem;
Expand Down
137 changes: 21 additions & 116 deletions src/main/java/com/extendedclip/deluxemenus/menu/Menu.java
Original file line number Diff line number Diff line change
@@ -1,62 +1,54 @@
package com.extendedclip.deluxemenus.menu;

import com.extendedclip.deluxemenus.DeluxeMenus;
import com.extendedclip.deluxemenus.menu.command.RegistrableMenuCommand;
import com.extendedclip.deluxemenus.menu.options.MenuOptions;
import com.extendedclip.deluxemenus.requirement.RequirementList;
import com.extendedclip.deluxemenus.utils.DebugLevel;
import com.extendedclip.deluxemenus.utils.StringUtils;

import java.lang.reflect.Field;
import java.util.*;
import java.util.Map.Entry;
import java.util.logging.Level;

import me.clip.placeholderapi.util.Msg;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class Menu extends Command {
public class Menu {

private static final Map<String, Menu> menus = new HashMap<>();
private static final Set<MenuHolder> menuHolders = new HashSet<>();
private static final Map<UUID, Menu> lastOpenedMenus = new HashMap<>();
private static CommandMap commandMap = null;

private final DeluxeMenus plugin;
private final MenuOptions options;
private final Map<Integer, TreeMap<Integer, MenuItem>> items;
// menu path starting from the plugin directory
private final String path;

private RegistrableMenuCommand command = null;

public Menu(
final @NotNull DeluxeMenus plugin,
final @NotNull MenuOptions options,
final @NotNull Map<Integer, TreeMap<Integer, MenuItem>> items,
final @NotNull String path
) {
super(options.commands().isEmpty() ? options.name() : options.commands().get(0));

this.plugin = plugin;
this.options = options;
this.items = items;
this.path = path;

if (this.options.registerCommands()) {
if (this.options.commands().size() > 1) {
this.setAliases(this.options.commands().subList(1, this.options.commands().size()));
}

addCommand();
this.command = new RegistrableMenuCommand(plugin, this);
this.command.register();
}

menus.put(this.options.name(), this);
}

Expand All @@ -67,12 +59,12 @@ public static void unload(final @NotNull DeluxeMenus plugin, final @NotNull Stri
}
}

Optional<Menu> menu = Menu.getMenuByName(name);
if (menu.isEmpty()) {
Optional<Menu> optionalMenu = Menu.getMenuByName(name);
if (optionalMenu.isEmpty()) {
return;
}

menu.get().removeCommand();
optionalMenu.get().unregisterCommand();
menus.remove(name);
}

Expand All @@ -83,13 +75,23 @@ public static void unload(final @NotNull DeluxeMenus plugin) {
}
}
for (Menu menu : Menu.getAllMenus()) {
menu.removeCommand();
menu.unregisterCommand();
}
menus.clear();
menuHolders.clear();
lastOpenedMenus.clear();
}

private void unregisterCommand() {
if (this.command != null) {
this.command.unregister();
}

// WARNING! A reference to the command is stored by CraftBukkit for their `/help` command. There is currently
// no way to remove this reference!
this.command = null;
}

public static void unloadForShutdown(final @NotNull DeluxeMenus plugin) {
for (Player player : Bukkit.getOnlinePlayers()) {
if (isInMenu(player)) {
Expand Down Expand Up @@ -208,103 +210,6 @@ public static void closeMenu(final @NotNull DeluxeMenus plugin, final @NotNull P
closeMenu(plugin, player, close, false);
}

private void addCommand() {
if (commandMap == null) {
try {
final Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap");
f.setAccessible(true);
commandMap = (CommandMap) f.get(Bukkit.getServer());
} catch (final @NotNull Exception exception) {
plugin.printStacktrace(
"Something went wrong while trying to register command: " + this.getName(),
exception
);
return;
}
}
boolean registered = commandMap.register("DeluxeMenus", this);
if (registered) {
plugin.debug(
DebugLevel.LOW,
Level.INFO,
"Registered command: " + this.getName() + " for menu: " + this.options.name()
);
}
}

private void removeCommand() {
if (commandMap != null && this.options.registerCommands()) {
Field cMap;
Field knownCommands;
try {
cMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
cMap.setAccessible(true);
knownCommands = SimpleCommandMap.class.getDeclaredField("knownCommands");
knownCommands.setAccessible(true);
((Map<String, Command>) knownCommands.get((SimpleCommandMap) cMap.get(Bukkit.getServer())))
.remove(this.getName());
boolean unregistered = this.unregister((CommandMap) cMap.get(Bukkit.getServer()));
this.unregister(commandMap);
if (unregistered) {
plugin.debug(
DebugLevel.HIGH,
Level.INFO,
"Successfully unregistered command: " + this.getName()
);
} else {
plugin.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Failed to unregister command: " + this.getName()
);
}
} catch (final @NotNull Exception exception) {
plugin.printStacktrace(
"Something went wrong while trying to unregister command: " + this.getName(),
exception
);
}
}
}

@Override
public boolean execute(final @NotNull CommandSender sender, final @NotNull String commandLabel, final @NotNull String[] typedArgs) {
if (!(sender instanceof Player)) {
Msg.msg(sender, "Menus can only be opened by players!");
return true;
}

Map<String, String> argMap = null;

if (!this.options.arguments().isEmpty()) {
plugin.debug(DebugLevel.LOWEST, Level.INFO, "has args");
if (typedArgs.length < this.options.arguments().size()) {
if (this.options.argumentsUsageMessage().isPresent()) {
Msg.msg(sender, this.options.argumentsUsageMessage().get());
}
return true;
}
argMap = new HashMap<>();
int index = 0;
for (String arg : this.options.arguments()) {
if (index + 1 == this.options.arguments().size()) {
String last = String.join(" ", Arrays.asList(typedArgs).subList(index, typedArgs.length));
plugin.debug(DebugLevel.LOWEST, Level.INFO, "arg: " + arg + " => " + last);
argMap.put(arg, last);
} else {
argMap.put(arg, typedArgs[index]);
plugin.debug(DebugLevel.LOWEST, Level.INFO, "arg: " + arg + " => " + typedArgs[index]);
}
index++;
}
}

Player player = (Player) sender;
plugin.debug(DebugLevel.LOWEST, Level.INFO, "opening menu: " + this.options.name());
openMenu(player, argMap, null);
return true;
}

private boolean hasOpenBypassPerm(final @NotNull Player viewer) {
return viewer.hasPermission("deluxemenus.openrequirement.bypass." + this.options.name())
|| viewer.hasPermission("deluxemenus.openrequirement.bypass.*");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.extendedclip.deluxemenus.DeluxeMenus;
import com.extendedclip.deluxemenus.hooks.ItemHook;
import com.extendedclip.deluxemenus.menu.options.HeadType;
import com.extendedclip.deluxemenus.menu.options.LoreAppendMode;
import com.extendedclip.deluxemenus.menu.options.MenuItemOptions;
import com.extendedclip.deluxemenus.nbt.NbtProvider;
import com.extendedclip.deluxemenus.utils.DebugLevel;
Expand Down
Loading

0 comments on commit 44192ea

Please sign in to comment.