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
|
|
@ -7,6 +7,8 @@ String dependencyDir = group + '.dependencies'
|
||||||
version = rootProject.version
|
version = rootProject.version
|
||||||
archivesBaseName = rootProject.name + '-Velocity'
|
archivesBaseName = rootProject.name + '-Velocity'
|
||||||
|
|
||||||
|
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_11
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
maven { url 'https://nexus.velocitypowered.com/repository/maven-public/' }
|
maven { url 'https://nexus.velocitypowered.com/repository/maven-public/' }
|
||||||
maven { url 'https://libraries.minecraft.net' }
|
maven { url 'https://libraries.minecraft.net' }
|
||||||
|
|
|
||||||
|
|
@ -3,17 +3,16 @@ package net.frankheijden.serverutils.velocity;
|
||||||
import co.aikar.commands.CommandCompletions;
|
import co.aikar.commands.CommandCompletions;
|
||||||
import co.aikar.commands.VelocityCommandCompletionContext;
|
import co.aikar.commands.VelocityCommandCompletionContext;
|
||||||
import co.aikar.commands.VelocityCommandManager;
|
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.Inject;
|
||||||
import com.google.inject.name.Named;
|
import com.google.inject.name.Named;
|
||||||
import com.velocitypowered.api.event.Subscribe;
|
import com.velocitypowered.api.event.Subscribe;
|
||||||
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
|
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.Plugin;
|
||||||
import com.velocitypowered.api.plugin.PluginContainer;
|
import com.velocitypowered.api.plugin.PluginContainer;
|
||||||
import com.velocitypowered.api.plugin.annotation.DataDirectory;
|
import com.velocitypowered.api.plugin.annotation.DataDirectory;
|
||||||
import com.velocitypowered.api.proxy.ProxyServer;
|
import com.velocitypowered.api.proxy.ProxyServer;
|
||||||
|
import java.io.IOException;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import net.frankheijden.serverutils.common.ServerUtilsApp;
|
import net.frankheijden.serverutils.common.ServerUtilsApp;
|
||||||
import net.frankheijden.serverutils.common.config.Config;
|
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.CommandPlugins;
|
||||||
import net.frankheijden.serverutils.velocity.commands.CommandServerUtils;
|
import net.frankheijden.serverutils.velocity.commands.CommandServerUtils;
|
||||||
import net.frankheijden.serverutils.velocity.entities.VelocityPlugin;
|
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.managers.VelocityPluginManager;
|
||||||
import net.frankheijden.serverutils.velocity.reflection.RVelocityCommandManager;
|
import net.frankheijden.serverutils.velocity.reflection.RVelocityCommandManager;
|
||||||
import org.bstats.velocity.Metrics;
|
import org.bstats.velocity.Metrics;
|
||||||
|
|
@ -39,6 +39,7 @@ public class ServerUtils {
|
||||||
private static ServerUtils instance;
|
private static ServerUtils instance;
|
||||||
private static final String CONFIG_RESOURCE = "velocity-config.toml";
|
private static final String CONFIG_RESOURCE = "velocity-config.toml";
|
||||||
private static final String MESSAGES_RESOURCE = "velocity-messages.toml";
|
private static final String MESSAGES_RESOURCE = "velocity-messages.toml";
|
||||||
|
private static final String PLUGIN_COMMANDS_CACHE = ".pluginCommandsCache.json";
|
||||||
|
|
||||||
private VelocityPlugin plugin;
|
private VelocityPlugin plugin;
|
||||||
private VelocityCommandManager commandManager;
|
private VelocityCommandManager commandManager;
|
||||||
|
|
@ -60,17 +61,26 @@ public class ServerUtils {
|
||||||
@Named("serverutils")
|
@Named("serverutils")
|
||||||
private PluginContainer pluginContainer;
|
private PluginContainer pluginContainer;
|
||||||
|
|
||||||
private final Multimap<String, String> pluginCommands = Multimaps.synchronizedSetMultimap(HashMultimap.create());
|
private final VelocityPluginCommandManager pluginCommandManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialises ServerUtils.
|
* Initialises ServerUtils.
|
||||||
*/
|
*/
|
||||||
@Inject
|
@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(
|
RVelocityCommandManager.proxyRegistrars(
|
||||||
proxy,
|
proxy,
|
||||||
getClass().getClassLoader(),
|
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();
|
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() {
|
public static ServerUtils getInstance() {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
@ -126,8 +148,8 @@ public class ServerUtils {
|
||||||
return plugin;
|
return plugin;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Multimap<String, String> getPluginCommands() {
|
public VelocityPluginCommandManager getPluginCommandManager() {
|
||||||
return pluginCommands;
|
return pluginCommandManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reload() {
|
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();
|
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);
|
proxy.getCommandManager().unregister(alias);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue