mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 14:38:52 +00:00
Add Paper Plugin support, Pl3xMap hook (#332)
This commit is contained in:
1 parent
17af935379
commit
bc421ae3d8
23 files changed
+770
-198
No files matched your search
@@ -15,7 +15,10 @@ import net.william278.huskhomes.database.Database;
|
||||
import net.william278.huskhomes.database.MySqlDatabase;
|
||||
import net.william278.huskhomes.database.SqLiteDatabase;
|
||||
import net.william278.huskhomes.event.BukkitEventDispatcher;
|
||||
import net.william278.huskhomes.hook.*;
|
||||
import net.william278.huskhomes.hook.Hook;
|
||||
import net.william278.huskhomes.hook.PlaceholderAPIHook;
|
||||
import net.william278.huskhomes.hook.RedisEconomyHook;
|
||||
import net.william278.huskhomes.hook.VaultEconomyHook;
|
||||
import net.william278.huskhomes.listener.BukkitEventListener;
|
||||
import net.william278.huskhomes.listener.EventListener;
|
||||
import net.william278.huskhomes.manager.Manager;
|
||||
@@ -30,11 +33,14 @@ import net.william278.huskhomes.user.BukkitUser;
|
||||
import net.william278.huskhomes.user.ConsoleUser;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import net.william278.huskhomes.user.SavedUser;
|
||||
import net.william278.huskhomes.util.*;
|
||||
import net.william278.huskhomes.util.BukkitAdapter;
|
||||
import net.william278.huskhomes.util.BukkitTaskRunner;
|
||||
import net.william278.huskhomes.util.UnsafeBlocks;
|
||||
import net.william278.huskhomes.util.Validator;
|
||||
import org.bstats.bukkit.Metrics;
|
||||
import org.bstats.charts.SimplePie;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChunkSnapshot;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.PluginDescriptionFile;
|
||||
@@ -46,7 +52,6 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@@ -84,6 +89,18 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
return instance;
|
||||
}
|
||||
|
||||
// Default public constructor
|
||||
@SuppressWarnings("unused")
|
||||
public BukkitHuskHomes() {
|
||||
super();
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
instance = this;
|
||||
@@ -106,9 +123,8 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
});
|
||||
|
||||
// Initialize the database
|
||||
final Database.Type databaseType = getSettings().getDatabaseType();
|
||||
initialize(databaseType.getDisplayName() + " database connection", (plugin) -> {
|
||||
this.database = switch (databaseType) {
|
||||
initialize(getSettings().getDatabaseType().getDisplayName() + " database connection", (plugin) -> {
|
||||
this.database = switch (getSettings().getDatabaseType()) {
|
||||
case MYSQL -> new MySqlDatabase(this);
|
||||
case SQLITE -> new SqLiteDatabase(this);
|
||||
};
|
||||
@@ -121,7 +137,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
|
||||
// Initialize the network messenger if proxy mode is enabled
|
||||
if (getSettings().doCrossServer()) {
|
||||
initialize("message broker", (plugin) -> {
|
||||
initialize(settings.getBrokerType().getDisplayName() + " message broker", (plugin) -> {
|
||||
broker = switch (settings.getBrokerType()) {
|
||||
case PLUGIN_MESSAGE -> new PluginMessageBroker(this);
|
||||
case REDIS -> new RedisBroker(this);
|
||||
@@ -133,13 +149,31 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
setRandomTeleportEngine(new NormalDistributionEngine(this));
|
||||
|
||||
// Register plugin hooks (Economy, Maps, Plan)
|
||||
initialize("hooks", (plugin) -> loadHooks());
|
||||
initialize("hooks", (plugin) -> {
|
||||
this.registerHooks();
|
||||
|
||||
if (hooks.size() > 0) {
|
||||
hooks.forEach(Hook::initialize);
|
||||
log(Level.INFO, "Registered " + hooks.size() + " plugin hooks: " + hooks.stream()
|
||||
.map(Hook::getName)
|
||||
.collect(Collectors.joining(", ")));
|
||||
}
|
||||
});
|
||||
|
||||
// Register events
|
||||
initialize("events", (plugin) -> this.eventListener = new BukkitEventListener(this));
|
||||
|
||||
// Register commands
|
||||
initialize("commands", (plugin) -> this.commands = Arrays.stream(BukkitCommand.Type.values())
|
||||
initialize("commands", (plugin) -> this.commands = registerCommands());
|
||||
|
||||
// Hook into bStats and check for updates
|
||||
initialize("metrics", (plugin) -> this.registerMetrics(METRICS_ID));
|
||||
this.checkForUpdates();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<Command> registerCommands() {
|
||||
return Arrays.stream(BukkitCommand.Type.values())
|
||||
.map(type -> {
|
||||
final Command command = type.getCommand();
|
||||
if (settings.isCommandDisabled(command)) {
|
||||
@@ -151,53 +185,27 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
}
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList()));
|
||||
|
||||
// Hook into bStats and check for updates
|
||||
initialize("metrics", (plugin) -> this.registerMetrics(METRICS_ID));
|
||||
this.checkForUpdates();
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private void initialize(@NotNull String name, @NotNull ThrowingConsumer<HuskHomes> runner) {
|
||||
log(Level.INFO, "Initializing " + name + "...");
|
||||
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.hooks = new ArrayList<>();
|
||||
@Override
|
||||
public void registerHooks() {
|
||||
HuskHomes.super.registerHooks();
|
||||
if (getSettings().doEconomy()) {
|
||||
if (Bukkit.getPluginManager().getPlugin("RedisEconomy") != null) {
|
||||
hooks.add(new RedisEconomyHook(this));
|
||||
} else if (Bukkit.getPluginManager().getPlugin("Vault") != null) {
|
||||
hooks.add(new VaultEconomyHook(this));
|
||||
if (isDependencyLoaded("RedisEconomy")) {
|
||||
getHooks().add(new RedisEconomyHook(this));
|
||||
} else if (isDependencyLoaded("Vault")) {
|
||||
getHooks().add(new VaultEconomyHook(this));
|
||||
}
|
||||
}
|
||||
if (getSettings().doMapHook()) {
|
||||
if (Bukkit.getPluginManager().getPlugin("Dynmap") != null) {
|
||||
hooks.add(new DynmapHook(this));
|
||||
} else if (Bukkit.getPluginManager().getPlugin("BlueMap") != null) {
|
||||
hooks.add(new BlueMapHook(this));
|
||||
}
|
||||
}
|
||||
if (Bukkit.getPluginManager().getPlugin("Plan") != null) {
|
||||
hooks.add(new PlanHook(this));
|
||||
}
|
||||
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
|
||||
hooks.add(new PlaceholderAPIHook(this));
|
||||
if (isDependencyLoaded("PlaceholderAPI")) {
|
||||
getHooks().add(new PlaceholderAPIHook(this));
|
||||
}
|
||||
}
|
||||
|
||||
if (hooks.size() > 0) {
|
||||
hooks.forEach(Hook::initialize);
|
||||
log(Level.INFO, "Registered " + hooks.size() + " plugin hooks: " + hooks.stream()
|
||||
.map(Hook::getName)
|
||||
.collect(Collectors.joining(", ")));
|
||||
}
|
||||
@Override
|
||||
public boolean isDependencyLoaded(@NotNull String name) {
|
||||
return Bukkit.getPluginManager().getPlugin(name) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -236,7 +244,9 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
@NotNull
|
||||
@Override
|
||||
public List<OnlineUser> getOnlineUsers() {
|
||||
return Bukkit.getOnlinePlayers().stream().map(player -> (OnlineUser) BukkitUser.adapt(player)).toList();
|
||||
return Bukkit.getOnlinePlayers().stream()
|
||||
.map(player -> (OnlineUser) BukkitUser.adapt(player))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -251,12 +261,22 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
return settings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSettings(@NotNull Settings settings) {
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Locales getLocales() {
|
||||
return locales;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLocales(@NotNull Locales locales) {
|
||||
this.locales = locales;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Database getDatabase() {
|
||||
@@ -279,7 +299,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
@Override
|
||||
public Broker getMessenger() {
|
||||
if (broker == null) {
|
||||
throw new IllegalStateException("Attempted to access network messenger when it was not initialized");
|
||||
throw new IllegalStateException("Attempted to access message broker when it was not initialized");
|
||||
}
|
||||
return broker;
|
||||
}
|
||||
@@ -296,10 +316,15 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Spawn> getLocalCachedSpawn() {
|
||||
public Optional<Spawn> getServerSpawn() {
|
||||
return Optional.ofNullable(serverSpawn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setServerSpawn(@NotNull Spawn spawn) {
|
||||
this.serverSpawn = spawn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setServerSpawn(@NotNull Location location) {
|
||||
try {
|
||||
@@ -322,15 +347,18 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Optional<Location>> resolveSafeGroundLocation(@NotNull Location location) {
|
||||
public void setHooks(@NotNull List<Hook> hooks) {
|
||||
this.hooks = hooks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Optional<Location>> findSafeGroundLocation(@NotNull Location location) {
|
||||
final org.bukkit.Location bukkitLocation = BukkitAdapter.adaptLocation(location).orElse(null);
|
||||
if (bukkitLocation == null || bukkitLocation.getWorld() == null) {
|
||||
return CompletableFuture.completedFuture(Optional.empty());
|
||||
}
|
||||
|
||||
final CompletableFuture<Optional<Location>> locationFuture = new CompletableFuture<>();
|
||||
Bukkit.getScheduler().runTask(this, () -> PaperLib.getChunkAtAsync(bukkitLocation).thenApply(chunk -> {
|
||||
final ChunkSnapshot snapshot = chunk.getChunkSnapshot();
|
||||
return PaperLib.getChunkAtAsync(bukkitLocation).thenApply(Chunk::getChunkSnapshot).thenApply(snapshot -> {
|
||||
final int chunkX = bukkitLocation.getBlockX() & 0xF;
|
||||
final int chunkZ = bukkitLocation.getBlockZ() & 0xF;
|
||||
|
||||
@@ -344,13 +372,17 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
final int y = snapshot.getHighestBlockYAt(x, z);
|
||||
final Material blockType = snapshot.getBlockType(chunkX, y, chunkZ);
|
||||
if (!isBlockUnsafe(blockType.getKey().toString())) {
|
||||
return Location.at((location.getX() + dX) + 0.5d, y + 1.25d, (location.getZ() + dZ) + 0.5d, location.getWorld());
|
||||
return Optional.of(Location.at(
|
||||
(location.getX() + dX) + 0.5d,
|
||||
y + 1.25d,
|
||||
(location.getZ() + dZ) + 0.5d,
|
||||
location.getWorld()
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}).thenAccept(resolved -> locationFuture.complete(Optional.ofNullable(resolved))));
|
||||
return locationFuture;
|
||||
return Optional.empty();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -383,6 +415,16 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
return server.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setServer(@NotNull Server server) {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUnsafeBlocks(@NotNull UnsafeBlocks unsafeBlocks) {
|
||||
this.unsafeBlocks = unsafeBlocks;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<World> getWorlds() {
|
||||
@@ -392,40 +434,6 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean loadConfigs() {
|
||||
try {
|
||||
// Load settings
|
||||
this.settings = Annotaml.create(new File(getDataFolder(), "config.yml"), Settings.class).get();
|
||||
|
||||
// Load locales from language preset default
|
||||
final Locales languagePresets = Annotaml.create(Locales.class, Objects.requireNonNull(getResource("locales/" + settings.getLanguage() + ".yml"))).get();
|
||||
this.locales = Annotaml.create(new File(getDataFolder(), "messages_" + settings.getLanguage() + ".yml"), languagePresets).get();
|
||||
|
||||
// Load server from file
|
||||
if (settings.doCrossServer()) {
|
||||
this.server = Annotaml.create(new File(getDataFolder(), "server.yml"), Server.class).get();
|
||||
} else {
|
||||
this.server = new Server(Server.getDefaultServerName());
|
||||
}
|
||||
|
||||
// Load spawn location from file
|
||||
final File spawnFile = new File(getDataFolder(), "spawn.yml");
|
||||
if (spawnFile.exists()) {
|
||||
this.serverSpawn = Annotaml.create(spawnFile, Spawn.class).get();
|
||||
}
|
||||
|
||||
// Load unsafe blocks from resources
|
||||
final InputStream blocksResource = getResource("safety/unsafe_blocks.yml");
|
||||
this.unsafeBlocks = Annotaml.create(new UnsafeBlocks(), Objects.requireNonNull(blocksResource)).get();
|
||||
|
||||
return true;
|
||||
} catch (IOException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
|
||||
log(Level.SEVERE, "Failed to reload HuskHomes config or messages file", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBlockUnsafe(@NotNull String blockId) {
|
||||
return unsafeBlocks.isUnsafe(blockId);
|
||||
@@ -467,7 +475,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
@Override
|
||||
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, byte[] message) {
|
||||
if (broker != null && broker instanceof PluginMessageBroker pluginMessenger
|
||||
&& getSettings().getBrokerType() == Broker.Type.PLUGIN_MESSAGE) {
|
||||
&& getSettings().getBrokerType() == Broker.Type.PLUGIN_MESSAGE) {
|
||||
pluginMessenger.onReceive(channel, BukkitUser.adapt(player), message);
|
||||
}
|
||||
}
|
||||
@@ -478,16 +486,4 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
return this;
|
||||
}
|
||||
|
||||
// Default constructor
|
||||
@SuppressWarnings("unused")
|
||||
public BukkitHuskHomes() {
|
||||
super();
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package net.william278.huskhomes.command;
|
||||
import me.lucko.commodore.CommodoreProvider;
|
||||
import me.lucko.commodore.file.CommodoreFileReader;
|
||||
import net.william278.huskhomes.BukkitHuskHomes;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -16,22 +15,22 @@ public class BrigadierUtil {
|
||||
* Uses commodore to register command completions
|
||||
*
|
||||
* @param plugin instance of the registering Bukkit plugin
|
||||
* @param pluginCommand the Bukkit PluginCommand to register completions for
|
||||
* @param bukkitCommand the Bukkit PluginCommand to register completions for
|
||||
* @param command the {@link Command} to register completions for
|
||||
*/
|
||||
protected static void registerCommodore(@NotNull BukkitHuskHomes plugin, @NotNull PluginCommand pluginCommand,
|
||||
protected static void registerCommodore(@NotNull BukkitHuskHomes plugin, @NotNull org.bukkit.command.Command bukkitCommand,
|
||||
@NotNull Command command) {
|
||||
final InputStream commandCommodore = plugin.getResource("commodore/" + pluginCommand.getName() + ".commodore");
|
||||
final InputStream commandCommodore = plugin.getResource("commodore/" + bukkitCommand.getName() + ".commodore");
|
||||
if (commandCommodore == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
CommodoreProvider.getCommodore(plugin).register(pluginCommand,
|
||||
CommodoreProvider.getCommodore(plugin).register(bukkitCommand,
|
||||
CommodoreFileReader.INSTANCE.parse(commandCommodore),
|
||||
player -> player.hasPermission(command.getPermission()));
|
||||
} catch (IOException e) {
|
||||
plugin.log(Level.SEVERE, "Failed to read command commodore completions for "
|
||||
+ pluginCommand.getName(), e);
|
||||
+ bukkitCommand.getName(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -54,14 +54,14 @@ public class BukkitCommand implements CommandExecutor, TabCompleter {
|
||||
}
|
||||
|
||||
// Register permissions
|
||||
addPermission(command.getPermission(), command.getUsage(), getPermissionDefault(command.isOperatorCommand()));
|
||||
addPermission(plugin, command.getPermission(), command.getUsage(), getPermissionDefault(command.isOperatorCommand()));
|
||||
final List<Permission> childNodes = command.getAdditionalPermissions()
|
||||
.entrySet().stream()
|
||||
.map((entry) -> addPermission(entry.getKey(), "", getPermissionDefault(entry.getValue())))
|
||||
.map((entry) -> addPermission(plugin, entry.getKey(), "", getPermissionDefault(entry.getValue())))
|
||||
.filter(Objects::nonNull)
|
||||
.toList();
|
||||
if (!childNodes.isEmpty()) {
|
||||
addPermission(command.getPermission("*"), command.getUsage(), PermissionDefault.FALSE,
|
||||
addPermission(plugin, command.getPermission("*"), command.getUsage(), PermissionDefault.FALSE,
|
||||
childNodes.toArray(new Permission[0]));
|
||||
}
|
||||
|
||||
@@ -78,7 +78,8 @@ public class BukkitCommand implements CommandExecutor, TabCompleter {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Permission addPermission(@NotNull String node, @NotNull String description, @NotNull PermissionDefault permissionDefault, @NotNull Permission... children) {
|
||||
protected static Permission addPermission(@NotNull BukkitHuskHomes plugin, @NotNull String node, @NotNull String description,
|
||||
@NotNull PermissionDefault permissionDefault, @NotNull Permission... children) {
|
||||
final Map<String, Boolean> childNodes = Arrays.stream(children)
|
||||
.map(Permission::getName)
|
||||
.collect(HashMap::new, (map, child) -> map.put(child, true), HashMap::putAll);
|
||||
@@ -100,7 +101,7 @@ public class BukkitCommand implements CommandExecutor, TabCompleter {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static PermissionDefault getPermissionDefault(boolean isOperatorCommand) {
|
||||
protected static PermissionDefault getPermissionDefault(boolean isOperatorCommand) {
|
||||
return isOperatorCommand ? PermissionDefault.OP : PermissionDefault.TRUE;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user