22
33import org .bukkit .Location ;
44import org .bukkit .OfflinePlayer ;
5+ import org .bukkit .World ;
56import org .bukkit .entity .Player ;
7+ import org .bukkit .event .Event ;
68import org .bukkit .plugin .Plugin ;
79
810import io .github .bakedlibs .dough .protection .Interaction ;
911import io .github .bakedlibs .dough .protection .ProtectionModule ;
1012
13+ import me .ryanhamshire .GriefPrevention .ClaimPermission ;
1114import me .ryanhamshire .GriefPrevention .Claim ;
12- import me .ryanhamshire .GriefPrevention .DataStore ;
1315import me .ryanhamshire .GriefPrevention .GriefPrevention ;
1416
1517import javax .annotation .Nonnull ;
1618
1719public 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