Skip to content

Commit

Permalink
Adds WorldGuardPH
Browse files Browse the repository at this point in the history
  • Loading branch information
anjoismysign committed Aug 12, 2024
1 parent b762e1c commit 6b4b697
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 19 deletions.
10 changes: 6 additions & 4 deletions src/main/java/us/mytheria/bloblib/BlobLib.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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"));
Expand All @@ -166,6 +164,8 @@ public void onEnable() {

Bukkit.getScheduler().runTask(this, () -> {
TranslatablePH.getInstance(this);
if (engineHubManager.isWorldGuardInstalled())
WorldGuardPH.getInstance(this);
disguiseManager.load();
});
}
Expand All @@ -186,6 +186,8 @@ public void reload() {
translatableManager.reload();
translatableItemManager.reload();
translatablePositionableManager.reload();
translatablePositionableManager.reload();
translatableAreaManager.reload();
messageManager.reload();
actionManager.reload();
inventoryManager.reload();
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/us/mytheria/bloblib/entities/area/Area.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);

Expand Down
33 changes: 19 additions & 14 deletions src/main/java/us/mytheria/bloblib/entities/area/WorldGuardArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,55 @@
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;
}

@NotNull
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/us/mytheria/bloblib/managers/PluginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
93 changes: 93 additions & 0 deletions src/main/java/us/mytheria/bloblib/placeholderapi/WorldGuardPH.java
Original file line number Diff line number Diff line change
@@ -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<ProtectedRegion> iterator = applicableRegionSet.getRegions().iterator();
if (!iterator.hasNext())
return null;
return iterator.next();
}

public BlobPHExpansion getExpansion() {
return expansion;
}
}
2 changes: 2 additions & 0 deletions src/main/resources/bloblib_translatable_snippets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ BlobLib:
Snippet: 'false'
Player-Not-Online:
Snippet: not online
Wilderness:
Snippet: Wilderness

0 comments on commit 6b4b697

Please sign in to comment.