-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve plugman/serverutil compatibility (#253)
Added reflections to deal with the issue of commands being handled in a way where they would always require a server restart to properly enable/disable commands or break entirely when reloaded by plugman/serverutils.
- Loading branch information
Showing
16 changed files
with
529 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 88 additions & 12 deletions
100
AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/commands/AEFCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,100 @@ | ||
package me.xginko.aef.commands; | ||
|
||
import me.xginko.aef.commands.aef.AEFCmd; | ||
import com.google.common.collect.ImmutableSet; | ||
import me.xginko.aef.AnarchyExploitFixes; | ||
import me.xginko.aef.utils.models.ConditionalEnableable; | ||
import me.xginko.aef.utils.models.Disableable; | ||
import me.xginko.aef.utils.models.Enableable; | ||
import org.bukkit.command.Command; | ||
import org.bukkit.command.PluginIdentifiableCommand; | ||
import org.bukkit.plugin.Plugin; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.reflections.Reflections; | ||
import org.reflections.scanners.Scanners; | ||
|
||
import java.lang.reflect.Modifier; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.stream.Collectors; | ||
|
||
public interface AEFCommand extends Enableable { | ||
public abstract class AEFCommand extends Command implements PluginIdentifiableCommand, ConditionalEnableable, Disableable { | ||
|
||
boolean shouldEnable(); | ||
protected static final Set<Class<AEFCommand>> AVAILABLE_COMMANDS; | ||
protected static final Set<AEFCommand> ENABLED_COMMANDS; | ||
|
||
static void registerCommands() { | ||
for (AEFCommand command : Set.of( | ||
new AEFCmd(), | ||
new ToggleConnectionMsgsCmd(), | ||
new SayCmd(), | ||
new HelpCmd() | ||
)) { | ||
if (command.shouldEnable()) { | ||
command.enable(); | ||
static { | ||
AVAILABLE_COMMANDS = new Reflections(AEFCommand.class.getPackage().getName()) | ||
.get(Scanners.SubTypes.of(AEFCommand.class).asClass()) | ||
.stream() | ||
.filter(clazz -> !clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers())) | ||
.map(clazz -> (Class<AEFCommand>) clazz) | ||
.collect(Collectors.collectingAndThen(Collectors.toList(), ImmutableSet::copyOf)); | ||
ENABLED_COMMANDS = new HashSet<>(AVAILABLE_COMMANDS.size()); | ||
} | ||
|
||
private final AnarchyExploitFixes plugin; | ||
|
||
protected AEFCommand(@NotNull String name, @NotNull String description, @NotNull String usageMessage, @NotNull List<String> aliases) { | ||
super(name, description, usageMessage, aliases); | ||
this.plugin = AnarchyExploitFixes.getInstance(); | ||
} | ||
|
||
@Override | ||
public @NotNull Plugin getPlugin() { | ||
return plugin; | ||
} | ||
|
||
@Override | ||
public boolean shouldEnable() { | ||
return true; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("UnstableApiUsage") | ||
public void enable() { | ||
plugin.getServer().getCommandMap().register(plugin.getPluginMeta().getName().toLowerCase(), this); | ||
} | ||
|
||
@Override | ||
public void disable() { | ||
Map<String, Command> knownCommands = new ConcurrentHashMap<>(plugin.getServer().getCommandMap().getKnownCommands()); | ||
for (Map.Entry<String, Command> entry : knownCommands.entrySet()) { | ||
if (entry.getValue() == this) { | ||
entry.getValue().unregister(plugin.getServer().getCommandMap()); | ||
BukkitCommandWrap.getInstance().unwrap(entry.getKey()); | ||
knownCommands.remove(entry.getKey()); | ||
} | ||
} | ||
BukkitCommandWrap.getInstance().setKnownCommands(knownCommands); | ||
BukkitCommandWrap.getInstance().sync(); | ||
} | ||
|
||
public static void disableAll() { | ||
ENABLED_COMMANDS.forEach(Disableable::disable); | ||
ENABLED_COMMANDS.clear(); | ||
} | ||
|
||
public static void reloadCommands() { | ||
disableAll(); | ||
|
||
for (Class<AEFCommand> cmdClass : AVAILABLE_COMMANDS) { | ||
try { | ||
AEFCommand aefCommand = cmdClass.getDeclaredConstructor().newInstance(); | ||
if (aefCommand.shouldEnable()) { | ||
ENABLED_COMMANDS.add(aefCommand); | ||
} | ||
} catch (Throwable t) { | ||
if (t.getCause() instanceof NoClassDefFoundError) { | ||
AnarchyExploitFixes.prefixedLogger().info("Dependencies for command class {} missing, not enabling.", cmdClass.getSimpleName()); | ||
} else { | ||
AnarchyExploitFixes.prefixedLogger().warn("Failed initialising command class '{}'.", cmdClass.getSimpleName(), t); | ||
} | ||
} | ||
} | ||
|
||
ENABLED_COMMANDS.forEach(Enableable::enable); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.