-
Notifications
You must be signed in to change notification settings - Fork 31
ProtectionPolicy
The concept of ProtectionPolicy is simple: Sometimes you want to protect blocks from being changed. However, there are a lot of ways for blocks to change in Minecraft. ProtectionPolicy makes it easier to control which ways you want blocks to be able to be changed in.
To protect a section of blocks, all you have to do is instantiate a ProtectionPolicy
by passing it a lambda to determine which blocks it protects, and a list of ProtectionType
s. Each ProtectionType
represents a way that that modifications to the world can happen, and there are quite a few of them. To make things easier, ProtectionType
has some helpful fields and methods. For example, ProtectionType.ALL
represents every type of protection, and will protect a region from every type of modification. ProtectionType.DIRECT_PLAYERS
represents the ways players can directly modify the blocks, while ProtectionType.INDIRECT_PLAYERS
are generally ways that players attempt to modify blocks indirectly: Through explosions, redstone, and such. ProtectionType.allExcept(ProtectionType...)
will allow you to select all protection types except the ones you want to allow.
And of course, sometimes you only want to protect blocks from certain players. That's why you can add bypass policies to a ProtectionPolicy
. You can call addBypassPolicy
and add a lambda that takes either just a player and a ProtectionType
, or a player, ProtectionType
, and Block
, and returns whether the player should be allowed to bypass the protection. You can also set what messages are sent to players when their actions are prevented by a ProtectionPolicy
by calling setDenyMessage(ProtectionType, String)
.
In general, you shouldn't need to instantiate the ProtectionPolicy
yourself, though. Region
has a method, protect(ProtectionType...)
, which allows you to protect it with given ProtectionType
s. Calling it returns a ProtectedRegion
, which is effectively just a wrapper to hold the Region
and ProtectionPolicy
.
One important thing to note is that there are two ways to construct a ProtectionPolicy. If you do need to construct one yourself, you should always use new ProtectionPolicy(Region, Predicate<Block>, ProtectionType...)
if possible, to bound the ProtectionPolicy to a finite region, as this uses a RegionMap and will be more optimized than using new ProtectionPolicy(Predicate<Block>, ProtectionType...)
.