Skip to content

Commit

Permalink
- Fix bug with NPE in EntityPickupItemEvent
Browse files Browse the repository at this point in the history
- Add custom WG flags to fix a bug where users couldn't open shops in their own claims
  • Loading branch information
Sven65 committed Jul 27, 2020
1 parent 6fd6243 commit e7fd85d
Show file tree
Hide file tree
Showing 23 changed files with 125 additions and 35 deletions.
2 changes: 1 addition & 1 deletion modules/1_10_R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>xyz.mackan</groupId>
<artifactId>Slabbo-parent</artifactId>
<version>1.0.11</version>
<version>1.0.12</version>
<relativePath>../../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion modules/1_11_R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>xyz.mackan</groupId>
<artifactId>Slabbo-parent</artifactId>
<version>1.0.11</version>
<version>1.0.12</version>
<relativePath>../../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion modules/1_12_R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>xyz.mackan</groupId>
<artifactId>Slabbo-parent</artifactId>
<version>1.0.11</version>
<version>1.0.12</version>
<relativePath>../../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion modules/1_13_R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>xyz.mackan</groupId>
<artifactId>Slabbo-parent</artifactId>
<version>1.0.11</version>
<version>1.0.12</version>
<relativePath>../../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion modules/1_13_R2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>xyz.mackan</groupId>
<artifactId>Slabbo-parent</artifactId>
<version>1.0.11</version>
<version>1.0.12</version>
<relativePath>../../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion modules/1_14_R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>xyz.mackan</groupId>
<artifactId>Slabbo-parent</artifactId>
<version>1.0.11</version>
<version>1.0.12</version>
<relativePath>../../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion modules/1_15_R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>xyz.mackan</groupId>
<artifactId>Slabbo-parent</artifactId>
<version>1.0.11</version>
<version>1.0.12</version>
<relativePath>../../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion modules/1_16_R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>xyz.mackan</groupId>
<artifactId>Slabbo-parent</artifactId>
<version>1.0.11</version>
<version>1.0.12</version>
<relativePath>../../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion modules/1_8_R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>xyz.mackan</groupId>
<artifactId>Slabbo-parent</artifactId>
<version>1.0.11</version>
<version>1.0.12</version>
<relativePath>../../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion modules/1_8_R2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>xyz.mackan</groupId>
<artifactId>Slabbo-parent</artifactId>
<version>1.0.11</version>
<version>1.0.12</version>
<relativePath>../../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion modules/1_8_R3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>xyz.mackan</groupId>
<artifactId>Slabbo-parent</artifactId>
<version>1.0.11</version>
<version>1.0.12</version>
<relativePath>../../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion modules/1_9_R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>xyz.mackan</groupId>
<artifactId>Slabbo-parent</artifactId>
<version>1.0.11</version>
<version>1.0.12</version>
<relativePath>../../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion modules/1_9_R2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>xyz.mackan</groupId>
<artifactId>Slabbo-parent</artifactId>
<version>1.0.11</version>
<version>1.0.12</version>
<relativePath>../../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion modules/Dist/dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>Slabbo-parent</artifactId>
<groupId>xyz.mackan</groupId>
<version>1.0.11</version>
<version>1.0.12</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion modules/Dist/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>xyz.mackan</groupId>
<artifactId>Slabbo-parent</artifactId>
<version>1.0.11</version>
<version>1.0.12</version>
<relativePath>../../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion modules/Plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<artifactId>Slabbo-parent</artifactId>
<groupId>xyz.mackan</groupId>
<version>1.0.11</version>
<version>1.0.12</version>
<relativePath>../../</relativePath>
</parent>

Expand Down
9 changes: 9 additions & 0 deletions modules/Plugin/src/main/java/xyz/mackan/Slabbo/Slabbo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import xyz.mackan.Slabbo.commands.SlabboCommandCompletions;
import xyz.mackan.Slabbo.listeners.*;
import xyz.mackan.Slabbo.pluginsupport.EnabledPlugins;
import xyz.mackan.Slabbo.pluginsupport.PluginSupport;
import xyz.mackan.Slabbo.pluginsupport.WorldguardSupport;
import xyz.mackan.Slabbo.types.BukkitVersion;
import xyz.mackan.Slabbo.types.Shop;
import xyz.mackan.Slabbo.types.ShopLimit;
Expand Down Expand Up @@ -58,6 +60,13 @@ public class Slabbo extends JavaPlugin {
public static EnabledPlugins enabledPlugins = new EnabledPlugins();


@Override
public void onLoad () {
if (getServer().getPluginManager().getPlugin("WorldGuard") != null) {
WorldguardSupport.registerFlags();
}
}

@Override
public void onEnable () {
dataPath = this.getDataFolder().getPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ public void onListAllRadius (Player player, double radius, @Optional String page
}
}

sendShopList(player, shopsInRadius, listPage, "slabbo list all radius "+radius);
sendShopList(player, shopsInRadius, listPage, "/slabbo list all radius "+radius);
}

@Subcommand("all")
Expand All @@ -747,12 +747,12 @@ public void onListAll (Player player, @Optional String page) {

List<Shop> shops = new ArrayList<Shop>(Slabbo.shopUtil.shops.values());

sendShopList(player, shops, listPage, "slabbo list all");
sendShopList(player, shops, listPage, "/slabbo list all");
}

@Subcommand("mine radius")
@Description("Lists all the Slabbo shops you own in a radius")
@CommandPermission("slabbo.list.mine")
@CommandPermission("slabbo.list.self")
public void onListMineRadius (Player player, double radius, @Optional String page) {
int listPage = 1;

Expand All @@ -776,12 +776,12 @@ public void onListMineRadius (Player player, double radius, @Optional String pag
}
}

sendShopList(player, shopsInRadius, listPage, "slabbo list mine radius "+radius);
sendShopList(player, shopsInRadius, listPage, "/slabbo list mine radius "+radius);
}

@Subcommand("mine")
@Description("Lists all the Slabbo shops you own")
@CommandPermission("slabbo.list.mine")
@CommandPermission("slabbo.list.self")
public void onListMine (Player player, @Optional String page) {
int listPage = 1;

Expand All @@ -793,7 +793,7 @@ public void onListMine (Player player, @Optional String page) {

List<Shop> myShops = shops.stream().filter(shop -> shop.ownerId.equals(player.getUniqueId())).collect(Collectors.toList());

sendShopList(player, myShops, listPage, "slabbo list mine");
sendShopList(player, myShops, listPage, "/slabbo list mine");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ public ShopAction getRightClickAction (ItemStack itemInHand, Block clickedBlock,
return new ShopAction(ShopActionType.CREATE);
}

if (holdingStick && shopExists && canUseShop) {
if (holdingStick && shopExists && (canUseShop || canCreateShop)) {
if (isShopOwner) {
return new ShopAction(ShopActionType.OPEN_DELETION_GUI, shop);
} else {
return new ShopAction(ShopActionType.OPEN_CLIENT_GUI, shop);
}
}

if (!holdingStick && shopExists && canUseShop) {
if (!holdingStick && shopExists && (canUseShop || canCreateShop)) {
if (isShopOwner) {
return new ShopAction(ShopActionType.OPEN_ADMIN_GUI, shop);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,115 @@
package xyz.mackan.Slabbo.pluginsupport;

import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.flags.Flags;
import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.flags.registry.FlagConflictException;
import com.sk89q.worldguard.protection.flags.registry.FlagRegistry;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.protection.regions.RegionContainer;
import com.sk89q.worldguard.protection.regions.RegionQuery;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import xyz.mackan.Slabbo.Slabbo;

import java.util.LinkedList;
import java.util.Set;

public class WorldguardSupport {
public static StateFlag CREATE_SHOPS;
public static StateFlag USE_SHOPS;

public static void registerFlags () {
FlagRegistry registry = WorldGuard.getInstance().getFlagRegistry();

try {
StateFlag createShopsFlag = new StateFlag("slabbo-others-create-shops", true);
StateFlag useShopsFlag = new StateFlag("slabbo-others-use-shops", true);

registry.register(createShopsFlag);
registry.register(useShopsFlag);

CREATE_SHOPS = createShopsFlag;
USE_SHOPS = useShopsFlag;
} catch (FlagConflictException e) {
Bukkit.getLogger().severe("One or more flags conflict!");
}
}

public static boolean canBypass (Player player) {
LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);

return WorldGuard.getInstance().getPlatform().getSessionManager().hasBypass(localPlayer, localPlayer.getWorld());
}

public static Set<ProtectedRegion> getRegions(final Location location) {
final RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();

final RegionManager manager = container.get(BukkitAdapter.adapt(location.getWorld()));

final ApplicableRegionSet regions = manager.getApplicableRegions(BlockVector3.at(location.getX(), location.getY(), location.getZ()));

return regions.getRegions();
}

public static boolean canCreateShop (Location location, Player player) {
if (canBypass(player)) return true;

LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);

com.sk89q.worldedit.util.Location weLocation = BukkitAdapter.adapt(location);
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
Set<ProtectedRegion> regions = getRegions(location);

boolean isOwner = true;
boolean canOthersCreate = true;

for (ProtectedRegion region : regions) {
if (!region.isOwner(localPlayer)) {
isOwner = false;
}

Object createShopsValue = region.getFlag(CREATE_SHOPS);

if (createShopsValue == null) continue;

RegionQuery query = container.createQuery();
if (createShopsValue.equals("DENY")) {
canOthersCreate = false;
}
}

return query.testState(weLocation, localPlayer, Flags.BUILD);
return isOwner || canOthersCreate;
}

public static boolean canUseShop (Location location, Player player) {
if (canBypass(player)) return true;

LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);

com.sk89q.worldedit.util.Location weLocation = BukkitAdapter.adapt(location);
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
Set<ProtectedRegion> regions = getRegions(location);

boolean isOwner = true;
boolean canOthersUse = true;

for (ProtectedRegion region : regions) {
if (!region.isOwner(localPlayer)) {
isOwner = false;
}

Object useShopsValue = region.getFlag(USE_SHOPS);

if (useShopsValue == null) continue;

RegionQuery query = container.createQuery();
if (useShopsValue.equals("DENY")) {
canOthersUse = false;
}
}

return query.testState(weLocation, localPlayer, Flags.INTERACT);
return isOwner || canOthersUse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ public static ItemStack setContainerStringValue(ItemStack itemStack, NamespacedK
}

public static int getContainerIntValue (ItemStack itemStack, NamespacedKey key) {
if (itemStack == null || key == null) return -1;

if (!itemStack.hasItemMeta()) return -1;

ItemMeta itemMeta = itemStack.getItemMeta();
Expand All @@ -193,6 +195,8 @@ public static int getContainerIntValue (ItemStack itemStack, NamespacedKey key)

if (container == null) return -1;

if (!container.has(key, PersistentDataType.INTEGER)) return -1;

return container.get(key, PersistentDataType.INTEGER);
}

Expand Down
10 changes: 9 additions & 1 deletion modules/Plugin/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,12 @@ permissions:

slabbo.save:
default: op
description: Saves the slabbo shops to disk
description: Saves the slabbo shops to disk

slabbo.list.all:
default: op
description: Lets a user list all shops on the server

slabbo.list.self:
default: op
description: Lets a user list all their shops
Loading

0 comments on commit e7fd85d

Please sign in to comment.