Skip to content

Commit

Permalink
Add use-shop worldguard flag - required foy buying/selling
Browse files Browse the repository at this point in the history
  • Loading branch information
Brokkonaut committed Mar 29, 2022
1 parent c0494b6 commit 0715bc0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/Acrobot/ChestShop/Dependencies.java
Expand Up @@ -50,7 +50,7 @@ public static void initializePluginsOnLoad() {
Plugin plugin = pluginManager.getPlugin("WorldGuard");

if (plugin != null) {
WorldGuardFlags.ENABLE_SHOP.getName(); // force the static code to run
WorldGuardFlags.ALLOW_SHOP.getName(); // force the static code to run
}
}

Expand Down Expand Up @@ -85,7 +85,7 @@ private static void initializePluginOnEnable(String name, Plugin plugin) { // Re
}

if (Properties.WORLDGUARD_INTEGRATION) {
listener = new WorldGuardBuilding();
listener = new WorldGuardBuilding(worldGuard);
}

break;
Expand Down
Expand Up @@ -2,29 +2,51 @@

import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;

import static com.Acrobot.ChestShop.Events.PreTransactionEvent.TransactionOutcome.INVALID_SHOP;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.PreTransactionEvent;
import com.Acrobot.ChestShop.Events.Protection.BuildPermissionEvent;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.ApplicableRegionSet;

/**
* @author Acrobot
*/
public class WorldGuardBuilding implements Listener {
private WorldGuardPlugin worldGuard;

public WorldGuardBuilding(WorldGuardPlugin worldGuard) {
this.worldGuard = worldGuard;
}

@EventHandler
public void canBuild(BuildPermissionEvent event) {
ApplicableRegionSet regions = getApplicableRegions(event.getSign().getBlock().getLocation());

if (Properties.WORLDGUARD_USE_FLAG) {
event.allow(regions.testState(null, WorldGuardFlags.ENABLE_SHOP));
event.allow(regions.testState(worldGuard.wrapPlayer(event.getPlayer()), WorldGuardFlags.ALLOW_SHOP));
} else {
event.allow(regions.size() != 0);
}
}

@EventHandler(priority = EventPriority.LOWEST)
public void canUse(PreTransactionEvent event) {
if (event.isCancelled()) {
return;
}

ApplicableRegionSet regions = getApplicableRegions(event.getSign().getBlock().getLocation());
if (!regions.testState(worldGuard.wrapPlayer(event.getClient()), WorldGuardFlags.USE_SHOP)) {
event.setCancelled(INVALID_SHOP);
return;
}
}

private ApplicableRegionSet getApplicableRegions(Location location) {
return WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(location.getWorld())).getApplicableRegions(BukkitAdapter.asBlockVector(location));
}
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/Acrobot/ChestShop/Plugins/WorldGuardFlags.java
Expand Up @@ -5,7 +5,8 @@
import com.sk89q.worldguard.protection.flags.registry.FlagConflictException;

public class WorldGuardFlags {
public static final StateFlag ENABLE_SHOP;
public static final StateFlag ALLOW_SHOP;
public static final StateFlag USE_SHOP;

static {
StateFlag enableShop;
Expand All @@ -16,6 +17,15 @@ public class WorldGuardFlags {
} catch (FlagConflictException | IllegalStateException e) {
enableShop = (StateFlag) WorldGuard.getInstance().getFlagRegistry().get("allow-shop");
}
ENABLE_SHOP = enableShop;
ALLOW_SHOP = enableShop;
StateFlag useShop;
try {
StateFlag flag = new StateFlag("use-shop", true);
WorldGuard.getInstance().getFlagRegistry().register(flag);
useShop = flag;
} catch (FlagConflictException | IllegalStateException e) {
useShop = (StateFlag) WorldGuard.getInstance().getFlagRegistry().get("use-shop");
}
USE_SHOP = useShop;
}
}

0 comments on commit 0715bc0

Please sign in to comment.