Skip to content

Commit 2e4f6b4

Browse files
Merge pull request #248 from Jikoo/dev/gp-performance
Update GriefPrevention module
2 parents d448602 + 3ca52c6 commit 2e4f6b4

File tree

2 files changed

+63
-12
lines changed

2 files changed

+63
-12
lines changed

dough-protection/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118

119119
<!-- GriefPrevention -->
120120
<dependency>
121-
<groupId>com.github.TechFortress</groupId>
121+
<groupId>com.github.GriefPrevention</groupId>
122122
<artifactId>GriefPrevention</artifactId>
123123
<version>16.18.2</version>
124124
<scope>provided</scope>

dough-protection/src/main/java/io/github/bakedlibs/dough/protection/modules/GriefPreventionProtectionModule.java

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@
22

33
import org.bukkit.Location;
44
import org.bukkit.OfflinePlayer;
5+
import org.bukkit.World;
56
import org.bukkit.entity.Player;
7+
import org.bukkit.event.Event;
68
import org.bukkit.plugin.Plugin;
79

810
import io.github.bakedlibs.dough.protection.Interaction;
911
import io.github.bakedlibs.dough.protection.ProtectionModule;
1012

13+
import me.ryanhamshire.GriefPrevention.ClaimPermission;
1114
import me.ryanhamshire.GriefPrevention.Claim;
12-
import me.ryanhamshire.GriefPrevention.DataStore;
1315
import me.ryanhamshire.GriefPrevention.GriefPrevention;
1416

1517
import javax.annotation.Nonnull;
1618

1719
public class GriefPreventionProtectionModule implements ProtectionModule {
1820

19-
private DataStore dataStore;
21+
private GriefPrevention griefPrevention;
22+
private boolean useClaimPermission = false;
23+
2024
private final Plugin plugin;
2125

2226
public GriefPreventionProtectionModule(@Nonnull Plugin plugin) {
@@ -30,32 +34,79 @@ public Plugin getPlugin() {
3034

3135
@Override
3236
public void load() {
33-
dataStore = GriefPrevention.instance.dataStore;
37+
griefPrevention = GriefPrevention.instance;
38+
39+
try {
40+
Claim.class.getDeclaredMethod("checkPermission", Player.class, ClaimPermission.class, Event.class);
41+
useClaimPermission = true;
42+
} catch (NoSuchMethodException ignored) {
43+
// Very dated GP version, use legacy methods.
44+
}
3445
}
3546

3647
@Override
3748
public boolean hasPermission(OfflinePlayer p, Location l, Interaction action) {
38-
Claim claim = dataStore.getClaimAt(l, true, null);
49+
World world = l.getWorld();
50+
51+
// Check if GP is handling the world at all.
52+
if (world == null || !griefPrevention.claimsEnabledForWorld(world)) {
53+
return true;
54+
}
55+
56+
// Fetch claim. Using player's cached claim is unlikely to speed up the process for a generalized check.
57+
Claim claim = griefPrevention.dataStore.getClaimAt(l, true, null);
3958

4059
if (claim == null) {
4160
return true;
61+
} else if (action == Interaction.ATTACK_PLAYER) {
62+
return !griefPrevention.claimIsPvPSafeZone(claim);
4263
} else if (p.getUniqueId().equals(claim.ownerID)) {
4364
return true;
44-
} else if (!(p instanceof Player)) {
65+
}
66+
67+
if (useClaimPermission) {
68+
// If Claim#checkPermission is available, prefer it.
69+
return checkPermission(claim, p, action);
70+
}
71+
72+
// Otherwise, legacy method requires an online player.
73+
if (!(p instanceof Player)) {
4574
return false;
4675
}
4776

77+
return checkLegacy(claim, (Player) p, action, l);
78+
}
79+
80+
private boolean checkPermission(@Nonnull Claim claim, @Nonnull OfflinePlayer offline, @Nonnull Interaction action) {
81+
// Do our best to translate Interaction to ClaimPermission.
82+
ClaimPermission permission;
83+
if (action == Interaction.INTERACT_BLOCK || action == Interaction.ATTACK_ENTITY) {
84+
permission = ClaimPermission.Inventory;
85+
} else if (action == Interaction.BREAK_BLOCK || action == Interaction.PLACE_BLOCK) {
86+
permission = ClaimPermission.Build;
87+
} else {
88+
permission = ClaimPermission.Access;
89+
}
90+
91+
// If the player is online, support permission-based allowance.
92+
if (offline instanceof Player) {
93+
return claim.checkPermission((Player) offline, permission, null) == null;
94+
}
95+
96+
// Fall through to explicit allowance for offline players.
97+
return claim.checkPermission(offline.getUniqueId(), permission, null) == null;
98+
}
99+
100+
private boolean checkLegacy(@Nonnull Claim claim, @Nonnull Player player, @Nonnull Interaction action, @Nonnull Location location) {
48101
switch (action) {
49102
case INTERACT_BLOCK:
50-
return claim.allowContainers((Player) p) == null;
51-
case ATTACK_PLAYER:
52-
return claim.siegeData == null || claim.siegeData.attacker == null;
103+
return claim.allowContainers(player) == null;
53104
case BREAK_BLOCK:
54-
return claim.allowBreak((Player) p, l.getBlock().getType()) == null;
105+
return claim.allowBreak(player, location.getBlock().getType()) == null;
55106
case PLACE_BLOCK:
56-
return claim.allowBuild((Player) p, l.getBlock().getType()) == null;
107+
return claim.allowBuild(player, location.getBlock().getType()) == null;
57108
default:
58-
return claim.allowAccess((Player) p) == null;
109+
return claim.allowAccess(player) == null;
59110
}
60111
}
61112

0 commit comments

Comments
 (0)