diff --git a/src/main/java/us/mytheria/bloblib/BlobLib.java b/src/main/java/us/mytheria/bloblib/BlobLib.java index 4c324442..fb37ea4c 100644 --- a/src/main/java/us/mytheria/bloblib/BlobLib.java +++ b/src/main/java/us/mytheria/bloblib/BlobLib.java @@ -1,9 +1,7 @@ package us.mytheria.bloblib; import org.bukkit.Bukkit; -import org.bukkit.World; import org.bukkit.plugin.java.JavaPlugin; -import org.jetbrains.annotations.NotNull; import us.mytheria.bloblib.command.BlobLibCmd; import us.mytheria.bloblib.disguises.DisguiseManager; import us.mytheria.bloblib.enginehub.EngineHubManager; @@ -20,6 +18,7 @@ import us.mytheria.bloblib.managers.*; import us.mytheria.bloblib.managers.fillermanager.FillerManager; import us.mytheria.bloblib.placeholderapi.TranslatablePH; +import us.mytheria.bloblib.placeholderapi.WorldGuardPH; import us.mytheria.bloblib.utilities.MinecraftVersion; import us.mytheria.bloblib.utilities.SerializationLib; import us.mytheria.bloblib.vault.VaultManager; @@ -138,10 +137,9 @@ public void onEnable() { return null; } String worldName = section.getString("World"); - @NotNull World world = SerializationLib.deserializeWorld(worldName); String id = section.getString("Id"); String display = section.getString("Display"); - return BlobTranslatableArea.of(key, locale, display, WorldGuardArea.of(world, id)); + return BlobTranslatableArea.of(key, locale, display, WorldGuardArea.of(worldName, id)); }, DataAssetType.TRANSLATABLE_AREA, section -> section.isString("World") && section.isString("Id")); @@ -166,6 +164,8 @@ public void onEnable() { Bukkit.getScheduler().runTask(this, () -> { TranslatablePH.getInstance(this); + if (engineHubManager.isWorldGuardInstalled()) + WorldGuardPH.getInstance(this); disguiseManager.load(); }); } @@ -186,6 +186,8 @@ public void reload() { translatableManager.reload(); translatableItemManager.reload(); translatablePositionableManager.reload(); + translatablePositionableManager.reload(); + translatableAreaManager.reload(); messageManager.reload(); actionManager.reload(); inventoryManager.reload(); diff --git a/src/main/java/us/mytheria/bloblib/entities/area/Area.java b/src/main/java/us/mytheria/bloblib/entities/area/Area.java index fa56c726..6def39cd 100644 --- a/src/main/java/us/mytheria/bloblib/entities/area/Area.java +++ b/src/main/java/us/mytheria/bloblib/entities/area/Area.java @@ -1,5 +1,6 @@ package us.mytheria.bloblib.entities.area; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.block.Block; @@ -10,7 +11,12 @@ public interface Area { @NotNull - World getWorld(); + String getWorldName(); + + @NotNull + default World getWorld() { + return Objects.requireNonNull(Bukkit.getWorld(getWorldName()), "World not found: " + getWorldName()); + } boolean isInside(@NotNull Location location); diff --git a/src/main/java/us/mytheria/bloblib/entities/area/WorldGuardArea.java b/src/main/java/us/mytheria/bloblib/entities/area/WorldGuardArea.java index 4f852889..3e6b72e1 100644 --- a/src/main/java/us/mytheria/bloblib/entities/area/WorldGuardArea.java +++ b/src/main/java/us/mytheria/bloblib/entities/area/WorldGuardArea.java @@ -9,29 +9,29 @@ import java.util.Objects; public class WorldGuardArea implements Area { - private final World world; + private final String worldName; private final String id; /** * Instances a WorldGuard Area. * It's implied that before calling this method, it needs to check that WorldGuard is installed. * - * @param world The world the ProtectedRegion belongs to - * @param id The ProtectedRegion's ID + * @param worldName The world the ProtectedRegion belongs to + * @param id The ProtectedRegion's ID * @return The WorldGuardArea */ public static WorldGuardArea of( - @NotNull World world, + @NotNull String worldName, @NotNull String id) { - Objects.requireNonNull(world, "'world' cannot be null"); + Objects.requireNonNull(worldName, "'worldName' cannot be null"); Objects.requireNonNull(id, "'id' cannot be null"); - return new WorldGuardArea(world, id); + return new WorldGuardArea(worldName, id); } private WorldGuardArea( - World world, + String worldName, String id) { - this.world = world; + this.worldName = worldName; this.id = id; } @@ -39,20 +39,25 @@ private WorldGuardArea( private ProtectedRegion getProtectedRegion() { Object result = EngineHubManager.getInstance() .getWorldGuardWorker() - .getRegion(world, id); - Objects.requireNonNull(result, "Couldn't find " + id + " in: " + world); + .getRegion(getWorld(), id); + Objects.requireNonNull(result, "Couldn't find " + id + " in: " + worldName); return (ProtectedRegion) result; } - @Override - public @NotNull World getWorld() { - return world; + @NotNull + public String getWorldName() { + return worldName; + } + + @NotNull + public String getId() { + return id; } @Override public boolean isInside(@NotNull Location location) { World locationWorld = location.getWorld(); - return locationWorld != null && locationWorld.getName().equals(world.getName()) + return locationWorld != null && locationWorld.getName().equals(worldName) && getProtectedRegion().contains(location.getBlockX(), location.getBlockY(), location.getBlockZ()); } } diff --git a/src/main/java/us/mytheria/bloblib/managers/DataAssetManager.java b/src/main/java/us/mytheria/bloblib/managers/DataAssetManager.java index 1d5efc7b..5b98070f 100644 --- a/src/main/java/us/mytheria/bloblib/managers/DataAssetManager.java +++ b/src/main/java/us/mytheria/bloblib/managers/DataAssetManager.java @@ -121,6 +121,8 @@ private void loadYamlConfiguration(File file) { if (filter.test(yamlConfiguration)) { try { T asset = readFunction.apply(yamlConfiguration, fileName); + if (asset == null) + return; addOrCreate(asset, fileName); } catch (Throwable throwable) { BlobLib.getInstance().getLogger().severe("At: " + file.getPath()); @@ -150,6 +152,8 @@ private void loadYamlConfiguration(File file, BlobPlugin plugin) { if (filter.test(yamlConfiguration)) { try { T asset = readFunction.apply(yamlConfiguration, fileName); + if (asset == null) + return; addOrCreate(asset, fileName); pluginAssets.get(plugin.getName()).add(fileName); } catch (Throwable throwable) { diff --git a/src/main/java/us/mytheria/bloblib/managers/LocalizableDataAssetManager.java b/src/main/java/us/mytheria/bloblib/managers/LocalizableDataAssetManager.java index 2fca750c..89dd7b1b 100644 --- a/src/main/java/us/mytheria/bloblib/managers/LocalizableDataAssetManager.java +++ b/src/main/java/us/mytheria/bloblib/managers/LocalizableDataAssetManager.java @@ -121,6 +121,8 @@ private void loadYamlConfiguration(File file) { if (filter.test(yamlConfiguration)) { try { T asset = readFunction.apply(yamlConfiguration, locale, fileName); + if (asset == null) + return; addOrCreateLocale(asset, fileName); } catch (Throwable throwable) { BlobLib.getInstance().getLogger().severe("At: " + file.getPath()); @@ -151,6 +153,8 @@ private void loadYamlConfiguration(File file, BlobPlugin plugin) { if (filter.test(yamlConfiguration)) { try { T asset = readFunction.apply(yamlConfiguration, locale, fileName); + if (asset == null) + return; addOrCreateLocale(asset, fileName); assets.get(plugin.getName()).add(fileName); } catch (Throwable throwable) { diff --git a/src/main/java/us/mytheria/bloblib/managers/PluginManager.java b/src/main/java/us/mytheria/bloblib/managers/PluginManager.java index 9e268ced..c67cf04a 100644 --- a/src/main/java/us/mytheria/bloblib/managers/PluginManager.java +++ b/src/main/java/us/mytheria/bloblib/managers/PluginManager.java @@ -157,6 +157,8 @@ public static void unloadAssets(BlobPlugin plugin) { TranslatableManager.unloadBlobPlugin(plugin); blobLib().getTranslatableItemManager().unload(plugin); blobLib().getTagSetManager().unload(plugin); + blobLib().getTranslatablePositionableManager().unload(plugin); + blobLib().getTranslatableAreaManager().unload(plugin); InventoryManager.unloadBlobPlugin(plugin); ActionManager.unloadBlobPlugin(plugin); MessageManager.unloadBlobPlugin(plugin); @@ -170,6 +172,8 @@ private static void loadAssets(@NotNull BlobPlugin plugin, TranslatableManager.loadBlobPlugin(plugin, director); blobLib().getTagSetManager().reload(plugin, director); blobLib().getTranslatableItemManager().reload(plugin, director); + blobLib().getTranslatablePositionableManager().reload(plugin, director); + blobLib().getTranslatableAreaManager().reload(plugin, director); SoundManager.loadBlobPlugin(plugin, director); MessageManager.loadBlobPlugin(plugin, director); ActionManager.loadBlobPlugin(plugin, director); diff --git a/src/main/java/us/mytheria/bloblib/placeholderapi/WorldGuardPH.java b/src/main/java/us/mytheria/bloblib/placeholderapi/WorldGuardPH.java new file mode 100644 index 00000000..3d1809f6 --- /dev/null +++ b/src/main/java/us/mytheria/bloblib/placeholderapi/WorldGuardPH.java @@ -0,0 +1,93 @@ +package us.mytheria.bloblib.placeholderapi; + +import com.sk89q.worldedit.math.BlockVector3; +import com.sk89q.worldguard.protection.ApplicableRegionSet; +import com.sk89q.worldguard.protection.managers.RegionManager; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; +import com.sk89q.worldguard.protection.regions.RegionQuery; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import us.mytheria.bloblib.BlobLib; +import us.mytheria.bloblib.api.BlobLibTranslatableAPI; +import us.mytheria.bloblib.enginehub.EngineHubManager; +import us.mytheria.bloblib.enginehub.worldedit.WorldEditWorker; +import us.mytheria.bloblib.entities.BlobPHExpansion; +import us.mytheria.bloblib.entities.area.WorldGuardArea; +import us.mytheria.bloblib.entities.translatable.TranslatableArea; + +import java.util.Iterator; +import java.util.Objects; + +public class WorldGuardPH { + private static WorldGuardPH instance; + private BlobPHExpansion expansion; + + @NotNull + public static WorldGuardPH getInstance(@NotNull BlobLib plugin) { + if (instance == null) { + Objects.requireNonNull(plugin, "injected dependency is null"); + instance = new WorldGuardPH(plugin); + } + return instance; + } + + private WorldGuardPH(@NotNull BlobLib plugin) { + instance = this; + if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") == null) { + BlobLib.getAnjoLogger().log("PlaceholderAPI not found, not registering WorldGuard PlaceholderAPI expansion"); + return; + } + BlobPHExpansion expansion = new BlobPHExpansion(plugin, "worldguard"); + this.expansion = expansion; + expansion.putSimple("translatable_area", offlinePlayer -> { + Player player = Bukkit.getPlayer(offlinePlayer.getUniqueId()); + if (player == null) + return ChatColor.RED + "Offline"; + ProtectedRegion region = getRegion(player.getLocation()); + if (region == null) + return BlobLibTranslatableAPI.getInstance().getTranslatableSnippet("BlobLib.Wilderness", player).get(); + WorldGuardArea worldGuardArea = null; + TranslatableArea translatableArea = null; + for (TranslatableArea translatable : BlobLibTranslatableAPI.getInstance().getTranslatableAreas(player.getLocale())) { + if (!(translatable.get() instanceof WorldGuardArea area)) + continue; + if (!area.getId().equals(region.getId())) + continue; + worldGuardArea = area; + translatableArea = translatable; + break; + } + if (worldGuardArea == null) + return BlobLibTranslatableAPI.getInstance().getTranslatableSnippet("BlobLib.Wilderness", player).get(); + return translatableArea.localize(player).getDisplay(); + }); + } + + @Nullable + private ProtectedRegion getRegion(Location location) { + if (location == null) + return null; + WorldEditWorker worldEditWorker = EngineHubManager.getInstance().getWorldEditWorker(); + Object blockVector3Object = worldEditWorker.blockVector3(location); + if (blockVector3Object == null) + return null; + BlockVector3 blockVector3 = (BlockVector3) blockVector3Object; + Object regionManagerObject = EngineHubManager.getInstance().getWorldGuardWorker().regionManager(location.getWorld()); + if (regionManagerObject == null) + return null; + RegionManager regionManager = (RegionManager) regionManagerObject; + ApplicableRegionSet applicableRegionSet = regionManager.getApplicableRegions(blockVector3, RegionQuery.QueryOption.SORT); + Iterator iterator = applicableRegionSet.getRegions().iterator(); + if (!iterator.hasNext()) + return null; + return iterator.next(); + } + + public BlobPHExpansion getExpansion() { + return expansion; + } +} diff --git a/src/main/resources/bloblib_translatable_snippets.yml b/src/main/resources/bloblib_translatable_snippets.yml index 3993c5d2..04c10745 100644 --- a/src/main/resources/bloblib_translatable_snippets.yml +++ b/src/main/resources/bloblib_translatable_snippets.yml @@ -5,3 +5,5 @@ BlobLib: Snippet: 'false' Player-Not-Online: Snippet: not online + Wilderness: + Snippet: Wilderness