Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.7.6 hotfix #254

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void onEnable() {
" https://ci.codemc.io/job/retrooper/job/packetevents/ ",
" "
).forEach(prefixedLogger::error);
getServer().getPluginManager().disablePlugin(this);
getServer().shutdown(); // Don't allow plugman/serverutil users to hot-load this plugin if packetevents is missing.
return;
}

Expand All @@ -104,8 +104,8 @@ public void onEnable() {
ServerVersion serverVersion = PacketEvents.getAPI().getServerManager().getVersion();
prefixedLogger.info("Detected {} {}", PlatformUtil.getServerType().niceName(),
serverVersion.name().replace("V_", "").replace('_', '.'));
if (serverVersion.isOlderThanOrEquals(ServerVersion.V_1_19_3) ||
serverVersion.equals(ServerVersion.V_1_19_4) && !PlatformUtil.isFolia()) {
if (serverVersion.isOlderThan(ServerVersion.V_1_19_4) ||
(serverVersion.equals(ServerVersion.V_1_19_4) && !PlatformUtil.isFolia())) {
prefixedLogger.error("This plugin jar is incompatible with your Server. Please use the Legacy jar.");
getServer().getPluginManager().disablePlugin(this);
return;
Expand All @@ -131,9 +131,6 @@ public void onEnable() {
prefixedLogger.info("Loading Translations");
reloadLang();

prefixedLogger.info("Registering Commands");
AEFCommand.registerCommands();

prefixedLogger.info("Loading NBT-API");
// Hide all messages with a log level lower than WARNING because of the same reason as Reflections logging.
Logger.getLogger("NBTAPI").setLevel(java.util.logging.Level.WARNING);
Expand All @@ -150,6 +147,7 @@ public void onDisable() {
AEFPermission.unregisterAll();
if (isPacketEventsInstalled) {
AEFModule.disableAll();
AEFCommand.disableAll();
}
if (languageCacheMap != null) {
languageCacheMap.clear();
Expand Down Expand Up @@ -221,6 +219,7 @@ private void reloadConfiguration() {
if (tickReporter != null) tickReporter.disable();
tickReporter = TickReporter.create(this, config.tickData_cache_duration);
AEFModule.reloadModules();
AEFCommand.reloadCommands();
config.saveConfig();
} catch (Throwable t) {
prefixedLogger.error("Failed while loading config!", t);
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import me.xginko.aef.AnarchyExploitFixes;
import me.xginko.aef.utils.permissions.AEFPermission;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.List;

public class HelpCmd extends Command implements AEFCommand {
public class HelpCmd extends AEFCommand {

public HelpCmd() {
super("help", "Command help overview", "/help", Collections.emptyList());
Expand All @@ -21,13 +20,6 @@ public boolean shouldEnable() {
return AnarchyExploitFixes.config().cmd_help_enabled;
}

@Override
@SuppressWarnings("UnstableApiUsage")
public void enable() {
AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance();
plugin.getServer().getCommandMap().register(plugin.getPluginMeta().getName().toLowerCase(), this);
}

@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args)
throws CommandException, IllegalArgumentException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package me.xginko.aef.commands;

import me.xginko.aef.AnarchyExploitFixes;
import me.xginko.aef.utils.permissions.AEFPermission;
import me.xginko.aef.utils.CommandUtil;
import me.xginko.aef.utils.permissions.AEFPermission;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.List;

public class SayCmd extends Command implements AEFCommand {
public class SayCmd extends AEFCommand {

public SayCmd() {
super("say", "Custom say command", "/say", Collections.emptyList());
Expand All @@ -24,13 +23,6 @@ public boolean shouldEnable() {
return AnarchyExploitFixes.config().cmd_say_enabled;
}

@Override
@SuppressWarnings("UnstableApiUsage")
public void enable() {
AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance();
plugin.getServer().getCommandMap().register(plugin.getPluginMeta().getName().toLowerCase(), this);
}

@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args)
throws CommandException, IllegalArgumentException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import me.xginko.aef.utils.permissions.AEFPermission;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
Expand All @@ -16,7 +15,7 @@
import java.util.Collections;
import java.util.List;

public class ToggleConnectionMsgsCmd extends Command implements AEFCommand {
public class ToggleConnectionMsgsCmd extends AEFCommand {

public ToggleConnectionMsgsCmd() {
super("toggleconnectionmsgs", "Toggle connection messages", "/toggleconnectionmsgs", Collections.singletonList("tcmsgs"));
Expand All @@ -27,13 +26,6 @@ public boolean shouldEnable() {
return AnarchyExploitFixes.config().cmd_toggleConMsgs_enabled;
}

@Override
@SuppressWarnings("UnstableApiUsage")
public void enable() {
AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance();
plugin.getServer().getCommandMap().register(plugin.getPluginMeta().getName().toLowerCase(), this);
}

@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args)
throws CommandException, IllegalArgumentException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import me.xginko.aef.AnarchyExploitFixes;
import me.xginko.aef.commands.AEFCommand;
import me.xginko.aef.commands.SubCommand;
import me.xginko.aef.commands.aef.subcommands.DisableSubCmd;
Expand All @@ -13,7 +12,6 @@
import me.xginko.aef.commands.aef.subcommands.VersionSubCmd;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
Expand All @@ -25,7 +23,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class AEFCmd extends Command implements AEFCommand {
public class AEFCmd extends AEFCommand {

private final @NotNull Set<SubCommand> subCommands;
private final @NotNull List<String> tabCompletes;
Expand Down Expand Up @@ -64,18 +62,6 @@ public AEFCmd() {
.collect(Collectors.collectingAndThen(Collectors.toList(), ImmutableList::copyOf));
}

@Override
public boolean shouldEnable() {
return true;
}

@Override
@SuppressWarnings("UnstableApiUsage")
public void enable() {
AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance();
plugin.getServer().getCommandMap().register(plugin.getPluginMeta().getName().toLowerCase(), this);
}

@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args)
throws CommandException, IllegalArgumentException
Expand Down
Loading
Loading