Skip to content

Commit

Permalink
fix command registration and ensure that helper classes are loaded in…
Browse files Browse the repository at this point in the history
… helper-js
  • Loading branch information
lucko committed Oct 26, 2017
1 parent 57f678f commit 831101a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import me.lucko.helper.Scheduler;
import me.lucko.helper.js.loader.SystemScriptLoader;
import me.lucko.helper.js.plugin.ScriptPlugin;
import me.lucko.helper.js.utils.EnsureLoad;
import me.lucko.helper.plugin.ExtendedJavaPlugin;

import org.bukkit.configuration.file.YamlConfiguration;
Expand All @@ -50,6 +51,12 @@ public class HelperJsPlugin extends ExtendedJavaPlugin implements ScriptPlugin {
private HelperScriptLoader loader;
private String scriptHeader;

@Override
protected void load() {
// ensure all helper classes are loaded in
EnsureLoad.ensure();
}

@Override
protected void enable() {

Expand Down
80 changes: 80 additions & 0 deletions helper-js/src/main/java/me/lucko/helper/js/utils/EnsureLoad.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* This file is part of helper, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package me.lucko.helper.js.utils;

import me.lucko.helper.function.Numbers;
import me.lucko.helper.gson.GsonProvider;
import me.lucko.helper.hologram.Hologram;
import me.lucko.helper.item.ItemStackBuilder;
import me.lucko.helper.menu.Gui;
import me.lucko.helper.menu.paginated.PaginatedGui;
import me.lucko.helper.menu.scheme.MenuScheme;
import me.lucko.helper.messaging.Messenger;
import me.lucko.helper.messaging.bungee.BungeeMessaging;
import me.lucko.helper.metadata.Metadata;
import me.lucko.helper.promise.Promise;
import me.lucko.helper.scoreboard.Scoreboard;
import me.lucko.helper.serialize.BlockPosition;
import me.lucko.helper.text.TextUtils;

public final class EnsureLoad {

@SuppressWarnings("ResultOfMethodCallIgnored")
public static void ensure() {
/*
* Forces the initialisation of classes. This only needs to be done once per package,
* and doesn't need to be performed for classes or packages which are naturally used by helper-js.
*
* This forces the resolveWildcardPackage function to return all helper packages.
*/
forceInit(Numbers.class);
forceInit(GsonProvider.class);
forceInit(Hologram.class);
forceInit(ItemStackBuilder.class);
forceInit(Gui.class);
forceInit(MenuScheme.class);
forceInit(PaginatedGui.class);
forceInit(Messenger.class);
forceInit(BungeeMessaging.class);
forceInit(Metadata.class);
forceInit(Promise.class);
forceInit(Scoreboard.class);
forceInit(BlockPosition.class);
forceInit(TextUtils.class);
}

// simply passing the class as a parameter is enough to load it
@SuppressWarnings("UnusedReturnValue")
private static <T> Class<T> forceInit(Class<T> clazz) {
// do nothing
return clazz;
}

private EnsureLoad() {
throw new UnsupportedOperationException("This class cannot be instantiated");
}

}
11 changes: 7 additions & 4 deletions helper/src/main/java/me/lucko/helper/utils/CommandMapUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public final class CommandMapUtil {
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
OWNING_PLUGIN_FIELD = null;
OWNING_PLUGIN_FIELD = owningPluginField;

Field knownCommandsField;
try {
Expand Down Expand Up @@ -158,9 +158,12 @@ public static <T extends CommandExecutor> T unregisterCommand(@Nonnull T command
Iterator<Command> iterator = knownCommands.values().iterator();
while (iterator.hasNext()) {
Command cmd = iterator.next();
if (cmd == command) {
cmd.unregister(map);
iterator.remove();
if (cmd instanceof PluginCommand) {
CommandExecutor executor = ((PluginCommand) cmd).getExecutor();
if (command == executor) {
cmd.unregister(map);
iterator.remove();
}
}
}
} catch (Exception e) {
Expand Down

0 comments on commit 831101a

Please sign in to comment.