2222import com .sk89q .worldguard .LocalPlayer ;
2323import com .sk89q .worldguard .WorldGuard ;
2424import com .sk89q .worldguard .bukkit .WorldGuardPlugin ;
25- import com .sk89q .worldguard .protection .association .DelayedRegionOverlapAssociation ;
2625import com .sk89q .worldguard .protection .flags .Flag ;
2726import com .sk89q .worldguard .protection .flags .Flags ;
2827import com .sk89q .worldguard .protection .flags .StateFlag ;
2928import com .sk89q .worldguard .protection .flags .registry .FlagRegistry ;
3029import com .sk89q .worldguard .protection .regions .RegionContainer ;
3130import com .sk89q .worldguard .protection .regions .RegionQuery ;
31+ import com .volmit .adapt .Adapt ;
3232import com .volmit .adapt .AdaptConfig ;
3333import com .volmit .adapt .api .adaptation .Adaptation ;
3434import com .volmit .adapt .api .protection .Protector ;
4141public class WorldGuardProtector implements Protector {
4242
4343 private final RegionContainer container = WorldGuard .getInstance ().getPlatform ().getRegionContainer ();
44- private final StateFlag flag ;
45-
46- public WorldGuardProtector () {
47- this .flag = new StateFlag ("use-adaptations" , false );
48- FlagRegistry registry = WorldGuard .getInstance ().getFlagRegistry ();
49-
50- try {
51- // Access the flags field of the registry
52- Field field = registry .getClass ().getDeclaredField ("flags" );
53- // This line makes the private field accessible
54- field .setAccessible (true );
55- // Get the flags from the registry
56- ConcurrentMap <String , Flag <?>> flags = (ConcurrentMap <String , Flag <?>>) field .get (registry );
57- // Add it to the registry
58- flags .put (flag .getName ().toLowerCase (), flag );
59- } catch (NoSuchFieldException | IllegalAccessException e ) {
60- e .printStackTrace ();
61- }
62- }
44+ private final StateFlag flag = registerFlag ();
6345
6446
6547 @ Override
@@ -69,44 +51,37 @@ public boolean checkRegion(Player player, Location location, Adaptation<?> adapt
6951
7052 @ Override
7153 public boolean canBlockBreak (Player player , Location blockLocation , Adaptation <?> adaptation ) {
72- return checkRegion (player , blockLocation , adaptation ) && checkPerm (blockLocation , Flags .BLOCK_BREAK );
54+ return checkRegion (player , blockLocation , adaptation ) && checkPerm (player , blockLocation , Flags .BLOCK_BREAK );
7355 }
7456
7557 @ Override
7658 public boolean canBlockPlace (Player player , Location blockLocation , Adaptation <?> adaptation ) {
77- return checkRegion (player , blockLocation , adaptation ) && checkPerm (blockLocation , Flags .BLOCK_PLACE );
59+ return checkRegion (player , blockLocation , adaptation ) && checkPerm (player , blockLocation , Flags .BLOCK_PLACE );
7860 }
7961
8062 @ Override
8163 public boolean canPVP (Player player , Location entityLocation , Adaptation <?> adaptation ) {
82- return checkRegion (player , entityLocation , adaptation ) && checkPerm (entityLocation , Flags .PVP );
64+ return checkRegion (player , entityLocation , adaptation ) && checkPerm (player , entityLocation , Flags .PVP );
8365 }
8466
8567 @ Override
8668 public boolean canPVE (Player player , Location entityLocation , Adaptation <?> adaptation ) {
87- return checkRegion (player , entityLocation , adaptation ) && checkPerm (entityLocation , Flags .DAMAGE_ANIMALS );
69+ return checkRegion (player , entityLocation , adaptation ) && checkPerm (player , entityLocation , Flags .DAMAGE_ANIMALS );
8870 }
8971
9072 @ Override
9173 public boolean canInteract (Player player , Location targetLocation , Adaptation <?> adaptation ) {
92- return checkRegion (player , targetLocation , adaptation ) && checkPerm (targetLocation , Flags .INTERACT );
74+ return checkRegion (player , targetLocation , adaptation ) && checkPerm (player , targetLocation , Flags .INTERACT );
9375 }
9476
9577 @ Override
9678 public boolean canAccessChest (Player player , Location chestLocation , Adaptation <?> adaptation ) {
97- return checkRegion (player , chestLocation , adaptation ) && checkPerm (chestLocation , Flags .CHEST_ACCESS );
98- }
99-
100- private boolean checkPerm (Location location , StateFlag flag ) {
101- return checkPerm (null , location , flag );
79+ return checkRegion (player , chestLocation , adaptation ) && checkPerm (player , chestLocation , Flags .CHEST_ACCESS );
10280 }
10381
10482 private boolean checkPerm (Player player , Location location , StateFlag flag ) {
10583 RegionQuery regionQuery = container .createQuery ();
10684 com .sk89q .worldedit .util .Location loc = BukkitAdapter .adapt (location );
107- if (player == null ) {
108- return regionQuery .queryState (loc , new DelayedRegionOverlapAssociation (regionQuery , loc ), flag ) != StateFlag .State .DENY ;
109- }
11085 if (!hasBypass (player , location ))
11186 return regionQuery .queryState (loc , WorldGuardPlugin .inst ().wrapPlayer (player ), flag ) != StateFlag .State .DENY ;
11287 return true ;
@@ -127,4 +102,30 @@ private boolean hasBypass(Player p, Location l) {
127102 com .sk89q .worldedit .world .World world = BukkitAdapter .adapt (l .getWorld ());
128103 return WorldGuard .getInstance ().getPlatform ().getSessionManager ().hasBypass (localPlayer , world );
129104 }
105+
106+ public static StateFlag registerFlag () {
107+ FlagRegistry registry = WorldGuard .getInstance ().getFlagRegistry ();
108+ StateFlag flag = (StateFlag ) registry .get ("use-adaptations" );
109+ if (flag != null ) return flag ;
110+ flag = new StateFlag ("use-adaptations" , false );
111+
112+ try {
113+ registry .register (flag );
114+ } catch (IllegalStateException ignored ) {
115+ Adapt .warn ("WorldGuard flag was not registered! Injecting it now..." );
116+ try {
117+ // Access the flags field of the registry
118+ Field field = registry .getClass ().getDeclaredField ("flags" );
119+ // This line makes the private field accessible
120+ field .setAccessible (true );
121+ // Get the flags from the registry
122+ ConcurrentMap <String , Flag <?>> flags = (ConcurrentMap <String , Flag <?>>) field .get (registry );
123+ // Add it to the registry
124+ flags .put (flag .getName ().toLowerCase (), flag );
125+ } catch (NoSuchFieldException | IllegalAccessException e ) {
126+ e .printStackTrace ();
127+ }
128+ }
129+ return flag ;
130+ }
130131}
0 commit comments