mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 06:28:49 +00:00
New player list caching system, further refinements and fixes
This commit is contained in:
59 files changed
+445
-317
No files matched your search
@@ -50,8 +50,6 @@ import java.io.InputStream;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -73,15 +71,12 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
private UnsafeBlocks unsafeBlocks;
|
||||
private Set<PluginHook> pluginHooks;
|
||||
private List<Command> commands;
|
||||
private Map<String, List<String>> globalPlayerList;
|
||||
private Server server;
|
||||
|
||||
@Nullable
|
||||
private Broker broker;
|
||||
|
||||
// Adventure audience
|
||||
private BukkitAudiences audiences;
|
||||
|
||||
// Instance of the plugin
|
||||
private static BukkitHuskHomes instance;
|
||||
|
||||
public static BukkitHuskHomes getInstance() {
|
||||
@@ -90,40 +85,37 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
// Set the instance
|
||||
instance = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// Initialize HuskHomes
|
||||
final AtomicBoolean initialized = new AtomicBoolean(true);
|
||||
// Create adventure audience
|
||||
this.audiences = BukkitAudiences.create(this);
|
||||
this.validator = new Validator(this);
|
||||
this.manager = new Manager(this);
|
||||
this.globalPlayerList = new HashMap<>();
|
||||
|
||||
// Load settings and locales
|
||||
log(Level.INFO, "Loading plugin configuration settings & locales...");
|
||||
initialized.set(reload());
|
||||
if (initialized.get()) {
|
||||
log(Level.INFO, "Successfully loaded plugin configuration settings & locales");
|
||||
} else {
|
||||
throw new IllegalStateException("Failed to load plugin configuration settings and/or locales");
|
||||
}
|
||||
initialize("plugin config & locale files", (plugin) -> {
|
||||
if (!loadConfigs()) {
|
||||
throw new IllegalStateException("Failed to load config files. Please check the console for errors");
|
||||
}
|
||||
});
|
||||
|
||||
// Initialize the database
|
||||
log(Level.INFO, "Attempting to establish connection to the database...");
|
||||
final Database.DatabaseType databaseType = settings.getDatabaseType();
|
||||
this.database = switch (databaseType == null ? Database.DatabaseType.MYSQL : databaseType) {
|
||||
case MYSQL -> new MySqlDatabase(this);
|
||||
case SQLITE -> new SqLiteDatabase(this);
|
||||
};
|
||||
initialized.set(this.database.initialize());
|
||||
if (initialized.get()) {
|
||||
log(Level.INFO, "Successfully established a connection to the database");
|
||||
} else {
|
||||
throw new IllegalStateException("Failed to establish a connection to the database. " +
|
||||
"Please check the supplied database credentials in the config file");
|
||||
}
|
||||
final Database.Type databaseType = getSettings().getDatabaseType();
|
||||
initialize(databaseType.getDisplayName() + " database connection", (plugin) -> {
|
||||
this.database = switch (databaseType) {
|
||||
case MYSQL -> new MySqlDatabase(this);
|
||||
case SQLITE -> new SqLiteDatabase(this);
|
||||
};
|
||||
|
||||
if (!database.initialize()) {
|
||||
throw new IllegalStateException("Failed to establish a connection to the database. " +
|
||||
"Please check the supplied database credentials in the config file");
|
||||
}
|
||||
});
|
||||
|
||||
// Initialize the network messenger if proxy mode is enabled
|
||||
if (getSettings().isCrossServer()) {
|
||||
@@ -136,50 +128,11 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
});
|
||||
}
|
||||
|
||||
// Prepare the validator
|
||||
this.validator = new Validator(this);
|
||||
this.cache = new Cache(this);
|
||||
this.manager = new Manager(this);
|
||||
|
||||
// Initialize the RTP engine with the default normal distribution engine
|
||||
this.cache = new Cache(this); // todo remove
|
||||
setRandomTeleportEngine(new NormalDistributionEngine(this));
|
||||
|
||||
// Register plugin hooks (Economy, Maps, Plan)
|
||||
initialize("hooks", (plugin) -> {
|
||||
this.pluginHooks = new HashSet<>();
|
||||
if (settings.doEconomy()) {
|
||||
if (Bukkit.getPluginManager().getPlugin("RedisEconomy") != null) {
|
||||
pluginHooks.add(new RedisEconomyHook(this));
|
||||
} else if (Bukkit.getPluginManager().getPlugin("Vault") != null) {
|
||||
pluginHooks.add(new VaultEconomyHook(this));
|
||||
}
|
||||
}
|
||||
if (settings.doMapHook()) {
|
||||
switch (settings.getMappingPlugin()) {
|
||||
case DYNMAP -> {
|
||||
final Plugin dynmapPlugin = Bukkit.getPluginManager().getPlugin("Dynmap");
|
||||
if (dynmapPlugin != null) {
|
||||
pluginHooks.add(new DynMapHook(this, dynmapPlugin));
|
||||
}
|
||||
}
|
||||
case BLUEMAP -> {
|
||||
if (Bukkit.getPluginManager().getPlugin("BlueMap") != null) {
|
||||
pluginHooks.add(new BlueMapHook(this));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Bukkit.getPluginManager().getPlugin("Plan") != null) {
|
||||
pluginHooks.add(new PlanHook(this));
|
||||
}
|
||||
|
||||
if (pluginHooks.size() > 0) {
|
||||
pluginHooks.forEach(PluginHook::initialize);
|
||||
log(Level.INFO, "Registered " + pluginHooks.size() + " plugin hooks: " + pluginHooks.stream()
|
||||
.map(PluginHook::getHookName)
|
||||
.collect(Collectors.joining(", ")));
|
||||
}
|
||||
});
|
||||
initialize("hooks", (plugin) -> loadHooks());
|
||||
|
||||
// Register events
|
||||
initialize("events", (plugin) -> this.eventListener = new BukkitEventListener(this));
|
||||
@@ -199,22 +152,58 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList()));
|
||||
|
||||
// Hook into bStats metrics
|
||||
// Hook into bStats and check for updates
|
||||
initialize("metrics", (plugin) -> this.registerMetrics(METRICS_ID));
|
||||
|
||||
// Check for updates
|
||||
if (settings.doCheckForUpdates()) {
|
||||
log(Level.INFO, "Checking for updates...");
|
||||
getLatestVersionIfOutdated().thenAccept(newestVersion -> newestVersion.ifPresent(newVersion -> log(Level.WARNING, "An update is available for HuskHomes, v" + newVersion + " (Currently running v" + getVersion() + ")")));
|
||||
}
|
||||
this.checkForUpdates();
|
||||
}
|
||||
|
||||
private void initialize(@NotNull String name, @NotNull Consumer<HuskHomes> runner) {
|
||||
private void initialize(@NotNull String name, @NotNull ThrowingConsumer<HuskHomes> runner) {
|
||||
log(Level.INFO, "Initializing " + name + "...");
|
||||
runner.accept(this);
|
||||
try {
|
||||
runner.accept(this);
|
||||
} catch (Exception e) {
|
||||
Bukkit.getPluginManager().disablePlugin(this);
|
||||
throw new IllegalStateException("Failed to initialize " + name, e);
|
||||
}
|
||||
log(Level.INFO, "Successfully initialized " + name);
|
||||
}
|
||||
|
||||
private void loadHooks() {
|
||||
this.pluginHooks = new HashSet<>();
|
||||
if (settings.doEconomy()) {
|
||||
if (Bukkit.getPluginManager().getPlugin("RedisEconomy") != null) {
|
||||
pluginHooks.add(new RedisEconomyHook(this));
|
||||
} else if (Bukkit.getPluginManager().getPlugin("Vault") != null) {
|
||||
pluginHooks.add(new VaultEconomyHook(this));
|
||||
}
|
||||
}
|
||||
if (settings.doMapHook()) {
|
||||
switch (settings.getMappingPlugin()) {
|
||||
case DYNMAP -> {
|
||||
final Plugin dynmapPlugin = Bukkit.getPluginManager().getPlugin("Dynmap");
|
||||
if (dynmapPlugin != null) {
|
||||
pluginHooks.add(new DynMapHook(this, dynmapPlugin));
|
||||
}
|
||||
}
|
||||
case BLUEMAP -> {
|
||||
if (Bukkit.getPluginManager().getPlugin("BlueMap") != null) {
|
||||
pluginHooks.add(new BlueMapHook(this));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Bukkit.getPluginManager().getPlugin("Plan") != null) {
|
||||
pluginHooks.add(new PlanHook(this));
|
||||
}
|
||||
|
||||
if (pluginHooks.size() > 0) {
|
||||
pluginHooks.forEach(PluginHook::initialize);
|
||||
log(Level.INFO, "Registered " + pluginHooks.size() + " plugin hooks: " + pluginHooks.stream()
|
||||
.map(PluginHook::getHookName)
|
||||
.collect(Collectors.joining(", ")));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
if (this.eventListener != null) {
|
||||
@@ -325,7 +314,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
bukkitLocation.getWorld().setSpawnLocation(bukkitLocation);
|
||||
});
|
||||
} catch (IOException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
|
||||
log(Level.WARNING, "Failed to save server spawn to disk", e);
|
||||
log(Level.WARNING, "Failed to save the server spawn.yml file", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -379,6 +368,12 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
return commands;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Map<String, List<String>> getGlobalPlayerList() {
|
||||
return globalPlayerList;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getServerName() {
|
||||
@@ -395,7 +390,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean reload() {
|
||||
public boolean loadConfigs() {
|
||||
try {
|
||||
// Load settings
|
||||
this.settings = Annotaml.create(new File(getDataFolder(), "config.yml"), Settings.class).get();
|
||||
@@ -442,7 +437,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
metrics.addCustomChart(new SimplePie("messenger_type", () -> getSettings().getBrokerType().getDisplayName()));
|
||||
}
|
||||
metrics.addCustomChart(new SimplePie("language", () -> getSettings().getLanguage().toLowerCase()));
|
||||
metrics.addCustomChart(new SimplePie("database_type", () -> getSettings().getDatabaseType().displayName));
|
||||
metrics.addCustomChart(new SimplePie("database_type", () -> getSettings().getDatabaseType().getDisplayName()));
|
||||
metrics.addCustomChart(new SimplePie("using_economy", () -> Boolean.toString(getSettings().doEconomy())));
|
||||
metrics.addCustomChart(new SimplePie("using_map", () -> Boolean.toString(getSettings().doMapHook())));
|
||||
if (getSettings().doMapHook()) {
|
||||
@@ -489,7 +484,6 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
}
|
||||
|
||||
// Super constructor for unit testing
|
||||
@SuppressWarnings("unused")
|
||||
protected BukkitHuskHomes(@NotNull JavaPluginLoader loader, @NotNull PluginDescriptionFile description, @NotNull File dataFolder, @NotNull File file) {
|
||||
super(loader, description, dataFolder, file);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package net.william278.huskhomes.api;
|
||||
|
||||
import net.william278.huskhomes.BukkitHuskHomes;
|
||||
import net.william278.huskhomes.user.BukkitUser;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import net.william278.huskhomes.user.User;
|
||||
import net.william278.huskhomes.position.Location;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.position.Server;
|
||||
import net.william278.huskhomes.user.BukkitUser;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import net.william278.huskhomes.user.User;
|
||||
import net.william278.huskhomes.util.BukkitAdapter;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -4,7 +4,10 @@ import me.lucko.commodore.CommodoreProvider;
|
||||
import net.william278.huskhomes.BukkitHuskHomes;
|
||||
import net.william278.huskhomes.teleport.TeleportRequest;
|
||||
import net.william278.huskhomes.user.BukkitUser;
|
||||
import org.bukkit.command.*;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.permissions.Permission;
|
||||
import org.bukkit.permissions.PermissionDefault;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package net.william278.huskhomes.event;
|
||||
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import net.william278.huskhomes.position.Home;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import org.bukkit.event.Cancellable;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
||||
@@ -3,9 +3,9 @@ package net.william278.huskhomes.hook;
|
||||
import net.william278.huskhomes.BukkitHuskHomes;
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.command.BukkitCommand;
|
||||
import net.william278.huskhomes.user.User;
|
||||
import net.william278.huskhomes.position.Home;
|
||||
import net.william278.huskhomes.position.Warp;
|
||||
import net.william278.huskhomes.user.User;
|
||||
import org.apache.commons.lang.StringEscapeUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package net.william278.huskhomes.listener;
|
||||
|
||||
import net.william278.huskhomes.BukkitHuskHomes;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.user.BukkitUser;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.util.BukkitAdapter;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
|
||||
@@ -35,9 +35,19 @@ public interface BukkitTaskRunner extends TaskRunner {
|
||||
return taskId.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
default int runLater(@NotNull Runnable runnable, long delay) {
|
||||
return Bukkit.getScheduler().runTaskLater((BukkitHuskHomes) getPlugin(), runnable, delay).getTaskId();
|
||||
}
|
||||
|
||||
@Override
|
||||
default void cancelTask(int taskId) {
|
||||
Bukkit.getScheduler().cancelTask(taskId);
|
||||
}
|
||||
|
||||
@Override
|
||||
default void cancelAllTasks() {
|
||||
Bukkit.getScheduler().cancelTasks((BukkitHuskHomes) getPlugin());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user