Persist pluginCommands across plugin restarts, compile velocity with jdk 11
This commit is contained in:
parent
64cbb44184
commit
503e40e76f
4 changed files with 92 additions and 9 deletions
|
|
@ -3,17 +3,16 @@ package net.frankheijden.serverutils.velocity;
|
|||
import co.aikar.commands.CommandCompletions;
|
||||
import co.aikar.commands.VelocityCommandCompletionContext;
|
||||
import co.aikar.commands.VelocityCommandManager;
|
||||
import com.google.common.collect.HashMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.common.collect.Multimaps;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.name.Named;
|
||||
import com.velocitypowered.api.event.Subscribe;
|
||||
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
|
||||
import com.velocitypowered.api.event.proxy.ProxyShutdownEvent;
|
||||
import com.velocitypowered.api.plugin.Plugin;
|
||||
import com.velocitypowered.api.plugin.PluginContainer;
|
||||
import com.velocitypowered.api.plugin.annotation.DataDirectory;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import net.frankheijden.serverutils.common.ServerUtilsApp;
|
||||
import net.frankheijden.serverutils.common.config.Config;
|
||||
|
|
@ -21,6 +20,7 @@ import net.frankheijden.serverutils.common.config.Messenger;
|
|||
import net.frankheijden.serverutils.velocity.commands.CommandPlugins;
|
||||
import net.frankheijden.serverutils.velocity.commands.CommandServerUtils;
|
||||
import net.frankheijden.serverutils.velocity.entities.VelocityPlugin;
|
||||
import net.frankheijden.serverutils.velocity.managers.VelocityPluginCommandManager;
|
||||
import net.frankheijden.serverutils.velocity.managers.VelocityPluginManager;
|
||||
import net.frankheijden.serverutils.velocity.reflection.RVelocityCommandManager;
|
||||
import org.bstats.velocity.Metrics;
|
||||
|
|
@ -39,6 +39,7 @@ public class ServerUtils {
|
|||
private static ServerUtils instance;
|
||||
private static final String CONFIG_RESOURCE = "velocity-config.toml";
|
||||
private static final String MESSAGES_RESOURCE = "velocity-messages.toml";
|
||||
private static final String PLUGIN_COMMANDS_CACHE = ".pluginCommandsCache.json";
|
||||
|
||||
private VelocityPlugin plugin;
|
||||
private VelocityCommandManager commandManager;
|
||||
|
|
@ -60,17 +61,26 @@ public class ServerUtils {
|
|||
@Named("serverutils")
|
||||
private PluginContainer pluginContainer;
|
||||
|
||||
private final Multimap<String, String> pluginCommands = Multimaps.synchronizedSetMultimap(HashMultimap.create());
|
||||
private final VelocityPluginCommandManager pluginCommandManager;
|
||||
|
||||
/**
|
||||
* Initialises ServerUtils.
|
||||
*/
|
||||
@Inject
|
||||
public ServerUtils(ProxyServer proxy) {
|
||||
public ServerUtils(ProxyServer proxy, @DataDirectory Path dataDirectory) {
|
||||
try {
|
||||
this.pluginCommandManager = VelocityPluginCommandManager.load(dataDirectory.resolve(PLUGIN_COMMANDS_CACHE));
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
|
||||
RVelocityCommandManager.proxyRegistrars(
|
||||
proxy,
|
||||
getClass().getClassLoader(),
|
||||
(container, meta) -> pluginCommands.putAll(container.getDescription().getId(), meta.getAliases())
|
||||
(container, meta) -> pluginCommandManager.getPluginCommands().putAll(
|
||||
container.getDescription().getId(),
|
||||
meta.getAliases()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -102,6 +112,18 @@ public class ServerUtils {
|
|||
ServerUtilsApp.tryCheckForUpdates();
|
||||
}
|
||||
|
||||
/**
|
||||
* De-initialises and disables ServerUtils.
|
||||
*/
|
||||
@Subscribe
|
||||
public void onDisable(ProxyShutdownEvent event) {
|
||||
try {
|
||||
pluginCommandManager.save();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static ServerUtils getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
|
@ -126,8 +148,8 @@ public class ServerUtils {
|
|||
return plugin;
|
||||
}
|
||||
|
||||
public Multimap<String, String> getPluginCommands() {
|
||||
return pluginCommands;
|
||||
public VelocityPluginCommandManager getPluginCommandManager() {
|
||||
return pluginCommandManager;
|
||||
}
|
||||
|
||||
public void reload() {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,58 @@
|
|||
package net.frankheijden.serverutils.velocity.managers;
|
||||
|
||||
import com.google.common.collect.HashMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.common.collect.Multimaps;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
public class VelocityPluginCommandManager {
|
||||
|
||||
private static final Gson gson = new Gson();
|
||||
|
||||
private final Multimap<String, String> pluginCommands;
|
||||
private final Path path;
|
||||
|
||||
public VelocityPluginCommandManager(Path path) {
|
||||
this.pluginCommands = Multimaps.synchronizedSetMultimap(HashMultimap.create());
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads and constructs a new {@link VelocityPluginCommandManager} from the given {@link Path}.
|
||||
*/
|
||||
public static VelocityPluginCommandManager load(Path path) throws IOException {
|
||||
var manager = new VelocityPluginCommandManager(path);
|
||||
if (Files.exists(path)) {
|
||||
Map<String, Collection<String>> rawMap = gson.fromJson(
|
||||
Files.newBufferedReader(path),
|
||||
new TypeToken<Map<String, Collection<String>>>(){}.getType()
|
||||
);
|
||||
rawMap.forEach(manager.pluginCommands::putAll);
|
||||
}
|
||||
|
||||
return manager;
|
||||
}
|
||||
|
||||
public Multimap<String, String> getPluginCommands() {
|
||||
return pluginCommands;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the map to the {@link Path} it was loaded from.
|
||||
*/
|
||||
public void save() throws IOException {
|
||||
Files.writeString(
|
||||
path,
|
||||
gson.toJson(pluginCommands.asMap()),
|
||||
StandardOpenOption.CREATE,
|
||||
StandardOpenOption.TRUNCATE_EXISTING
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -217,7 +217,8 @@ public class VelocityPluginManager extends AbstractPluginManager<PluginContainer
|
|||
}
|
||||
|
||||
String pluginId = plugin.getDescription().getId();
|
||||
for (String alias : ServerUtils.getInstance().getPluginCommands().removeAll(pluginId)) {
|
||||
VelocityPluginCommandManager pluginCommandManager = ServerUtils.getInstance().getPluginCommandManager();
|
||||
for (String alias : pluginCommandManager.getPluginCommands().removeAll(pluginId)) {
|
||||
proxy.getCommandManager().unregister(alias);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue