Skip to content

Commit

Permalink
Improvements to ProtectionPolicy
Browse files Browse the repository at this point in the history
- ProtectionType.CONTAINER_ACCESS now prevents hoppers from moving items into chests from outside a protected area
- Added static methods to ProtectionPolicy to register custom protections against multi-block and directional events
- Made methods on SQLCache synchronized
  • Loading branch information
boxbeam committed Nov 23, 2021
1 parent c8a136d commit 889d92d
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 17 deletions.
8 changes: 8 additions & 0 deletions javadoc/index-all.html
Original file line number Diff line number Diff line change
Expand Up @@ -3143,6 +3143,14 @@ <h2 class="title" id="I:R">R</h2>
<dd>
<div class="block">Registers a custom event to be protected using a specific ProtectionType</div>
</dd>
<dt><a href="redempt/redlib/protection/ProtectionPolicy.html#registerProtectionDirectional(java.lang.Class,redempt.redlib.protection.ProtectionPolicy.ProtectionType,java.util.function.Function,java.util.function.Function,java.util.function.Function)" class="member-name-link">registerProtectionDirectional(Class&lt;T&gt;, ProtectionPolicy.ProtectionType, Function&lt;T, Player&gt;, Function&lt;T, Block&gt;, Function&lt;T, List&lt;Block&gt;&gt;)</a> - Static method in class redempt.redlib.protection.<a href="redempt/redlib/protection/ProtectionPolicy.html" title="class in redempt.redlib.protection">ProtectionPolicy</a></dt>
<dd>
<div class="block">Registers a custom event to be protected using a specific ProtectionType, where there is a block acting and blocks being acted upon</div>
</dd>
<dt><a href="redempt/redlib/protection/ProtectionPolicy.html#registerProtectionMultiBlock(java.lang.Class,redempt.redlib.protection.ProtectionPolicy.ProtectionType,java.util.function.Function,java.util.function.BiConsumer,java.util.function.Function)" class="member-name-link">registerProtectionMultiBlock(Class&lt;T&gt;, ProtectionPolicy.ProtectionType, Function&lt;T, Player&gt;, BiConsumer&lt;T, Block&gt;, Function&lt;T, List&lt;Block&gt;&gt;)</a> - Static method in class redempt.redlib.protection.<a href="redempt/redlib/protection/ProtectionPolicy.html" title="class in redempt.redlib.protection">ProtectionPolicy</a></dt>
<dd>
<div class="block">Registers a custom event that modifies multiple blocks to be protected using a specific ProtectionType</div>
</dd>
<dt><a href="redempt/redlib/protection/ProtectionPolicy.html#registerProtectionNonCancellable(java.lang.Class,redempt.redlib.protection.ProtectionPolicy.ProtectionType,java.util.function.Function,java.util.function.Consumer,java.util.function.Function...)" class="member-name-link">registerProtectionNonCancellable(Class&lt;T&gt;, ProtectionPolicy.ProtectionType, Function&lt;T, Player&gt;, Consumer&lt;T&gt;, Function&lt;T, Block&gt;...)</a> - Static method in class redempt.redlib.protection.<a href="redempt/redlib/protection/ProtectionPolicy.html" title="class in redempt.redlib.protection">ProtectionPolicy</a></dt>
<dd>
<div class="block">Registers a custom event that cannot be cancelled using <code>Cancellable.setCancelled(boolean)</code> using a specific ProtectionType</div>
Expand Down
2 changes: 1 addition & 1 deletion javadoc/member-search-index.js

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions javadoc/redempt/redlib/protection/ProtectionPolicy.html
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,24 @@ <h2>Method Summary</h2>
<div class="col-last even-row-color method-summary-table method-summary-table-tab1 method-summary-table-tab4">
<div class="block">Registers a custom event to be protected using a specific ProtectionType</div>
</div>
<div class="col-first odd-row-color method-summary-table method-summary-table-tab1 method-summary-table-tab4"><code>static &lt;T extends org.bukkit.event.Event &amp; org.bukkit.event.Cancellable&gt;<br>void</code></div>
<div class="col-second odd-row-color method-summary-table method-summary-table-tab1 method-summary-table-tab4"><code><a href="#registerProtectionDirectional(java.lang.Class,redempt.redlib.protection.ProtectionPolicy.ProtectionType,java.util.function.Function,java.util.function.Function,java.util.function.Function)" class="member-name-link">registerProtectionDirectional</a><wbr>(<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Class.html" title="class or interface in java.lang" class="external-link">Class</a>&lt;T&gt;&nbsp;clazz,
<a href="ProtectionPolicy.ProtectionType.html" title="enum class in redempt.redlib.protection">ProtectionPolicy.ProtectionType</a>&nbsp;type,
<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/function/Function.html" title="class or interface in java.util.function" class="external-link">Function</a>&lt;T,<wbr>org.bukkit.entity.Player&gt;&nbsp;getPlayer,
<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/function/Function.html" title="class or interface in java.util.function" class="external-link">Function</a>&lt;T,<wbr>org.bukkit.block.Block&gt;&nbsp;getActingBlock,
<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/function/Function.html" title="class or interface in java.util.function" class="external-link">Function</a>&lt;T,<wbr><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/List.html" title="class or interface in java.util" class="external-link">List</a>&lt;org.bukkit.block.Block&gt;&gt;&nbsp;getSubjectBlocks)</code></div>
<div class="col-last odd-row-color method-summary-table method-summary-table-tab1 method-summary-table-tab4">
<div class="block">Registers a custom event to be protected using a specific ProtectionType, where there is a block acting and blocks being acted upon</div>
</div>
<div class="col-first even-row-color method-summary-table method-summary-table-tab1 method-summary-table-tab4"><code>static &lt;T extends org.bukkit.event.Event&gt;<br>void</code></div>
<div class="col-second even-row-color method-summary-table method-summary-table-tab1 method-summary-table-tab4"><code><a href="#registerProtectionMultiBlock(java.lang.Class,redempt.redlib.protection.ProtectionPolicy.ProtectionType,java.util.function.Function,java.util.function.BiConsumer,java.util.function.Function)" class="member-name-link">registerProtectionMultiBlock</a><wbr>(<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Class.html" title="class or interface in java.lang" class="external-link">Class</a>&lt;T&gt;&nbsp;clazz,
<a href="ProtectionPolicy.ProtectionType.html" title="enum class in redempt.redlib.protection">ProtectionPolicy.ProtectionType</a>&nbsp;type,
<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/function/Function.html" title="class or interface in java.util.function" class="external-link">Function</a>&lt;T,<wbr>org.bukkit.entity.Player&gt;&nbsp;getPlayer,
<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/function/BiConsumer.html" title="class or interface in java.util.function" class="external-link">BiConsumer</a>&lt;T,<wbr>org.bukkit.block.Block&gt;&nbsp;cancel,
<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/function/Function.html" title="class or interface in java.util.function" class="external-link">Function</a>&lt;T,<wbr><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/List.html" title="class or interface in java.util" class="external-link">List</a>&lt;org.bukkit.block.Block&gt;&gt;&nbsp;getBlocks)</code></div>
<div class="col-last even-row-color method-summary-table method-summary-table-tab1 method-summary-table-tab4">
<div class="block">Registers a custom event that modifies multiple blocks to be protected using a specific ProtectionType</div>
</div>
<div class="col-first odd-row-color method-summary-table method-summary-table-tab1 method-summary-table-tab4"><code>static &lt;T extends org.bukkit.event.Event&gt;<br>void</code></div>
<div class="col-second odd-row-color method-summary-table method-summary-table-tab1 method-summary-table-tab4"><code><a href="#registerProtectionNonCancellable(java.lang.Class,redempt.redlib.protection.ProtectionPolicy.ProtectionType,java.util.function.Function,java.util.function.Consumer,java.util.function.Function...)" class="member-name-link">registerProtectionNonCancellable</a><wbr>(<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Class.html" title="class or interface in java.lang" class="external-link">Class</a>&lt;T&gt;&nbsp;clazz,
<a href="ProtectionPolicy.ProtectionType.html" title="enum class in redempt.redlib.protection">ProtectionPolicy.ProtectionType</a>&nbsp;type,
Expand Down Expand Up @@ -373,6 +391,50 @@ <h3>registerProtectionNonCancellable</h3>
</section>
</li>
<li>
<section class="detail" id="registerProtectionMultiBlock(java.lang.Class,redempt.redlib.protection.ProtectionPolicy.ProtectionType,java.util.function.Function,java.util.function.BiConsumer,java.util.function.Function)">
<h3>registerProtectionMultiBlock</h3>
<div class="member-signature"><span class="modifiers">public static</span>&nbsp;<span class="type-parameters">&lt;T extends org.bukkit.event.Event&gt;</span>
<span class="return-type">void</span>&nbsp;<span class="element-name">registerProtectionMultiBlock</span><wbr><span class="parameters">(<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Class.html" title="class or interface in java.lang" class="external-link">Class</a>&lt;T&gt;&nbsp;clazz,
<a href="ProtectionPolicy.ProtectionType.html" title="enum class in redempt.redlib.protection">ProtectionPolicy.ProtectionType</a>&nbsp;type,
<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/function/Function.html" title="class or interface in java.util.function" class="external-link">Function</a>&lt;T,<wbr>org.bukkit.entity.Player&gt;&nbsp;getPlayer,
<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/function/BiConsumer.html" title="class or interface in java.util.function" class="external-link">BiConsumer</a>&lt;T,<wbr>org.bukkit.block.Block&gt;&nbsp;cancel,
<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/function/Function.html" title="class or interface in java.util.function" class="external-link">Function</a>&lt;T,<wbr><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/List.html" title="class or interface in java.util" class="external-link">List</a>&lt;org.bukkit.block.Block&gt;&gt;&nbsp;getBlocks)</span></div>
<div class="block">Registers a custom event that modifies multiple blocks to be protected using a specific ProtectionType</div>
<dl class="notes">
<dt>Type Parameters:</dt>
<dd><code>T</code> - The event type</dd>
<dt>Parameters:</dt>
<dd><code>clazz</code> - The event class</dd>
<dd><code>type</code> - The ProtectionType to protect against this event</dd>
<dd><code>getPlayer</code> - A function to get the player associated with the event - can return null</dd>
<dd><code>cancel</code> - A function to cancel the modification of a specific block in the event</dd>
<dd><code>getBlocks</code> - A vararg of functions to get the blocks associated with the event</dd>
</dl>
</section>
</li>
<li>
<section class="detail" id="registerProtectionDirectional(java.lang.Class,redempt.redlib.protection.ProtectionPolicy.ProtectionType,java.util.function.Function,java.util.function.Function,java.util.function.Function)">
<h3>registerProtectionDirectional</h3>
<div class="member-signature"><span class="modifiers">public static</span>&nbsp;<span class="type-parameters-long">&lt;T extends org.bukkit.event.Event &amp; org.bukkit.event.Cancellable&gt;</span>
<span class="return-type">void</span>&nbsp;<span class="element-name">registerProtectionDirectional</span><wbr><span class="parameters">(<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Class.html" title="class or interface in java.lang" class="external-link">Class</a>&lt;T&gt;&nbsp;clazz,
<a href="ProtectionPolicy.ProtectionType.html" title="enum class in redempt.redlib.protection">ProtectionPolicy.ProtectionType</a>&nbsp;type,
<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/function/Function.html" title="class or interface in java.util.function" class="external-link">Function</a>&lt;T,<wbr>org.bukkit.entity.Player&gt;&nbsp;getPlayer,
<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/function/Function.html" title="class or interface in java.util.function" class="external-link">Function</a>&lt;T,<wbr>org.bukkit.block.Block&gt;&nbsp;getActingBlock,
<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/function/Function.html" title="class or interface in java.util.function" class="external-link">Function</a>&lt;T,<wbr><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/List.html" title="class or interface in java.util" class="external-link">List</a>&lt;org.bukkit.block.Block&gt;&gt;&nbsp;getSubjectBlocks)</span></div>
<div class="block">Registers a custom event to be protected using a specific ProtectionType, where there is a block acting and blocks being acted upon</div>
<dl class="notes">
<dt>Type Parameters:</dt>
<dd><code>T</code> - The event type</dd>
<dt>Parameters:</dt>
<dd><code>clazz</code> - The event class</dd>
<dd><code>type</code> - The ProtectionType to protect against this event</dd>
<dd><code>getPlayer</code> - A function to get the player associated with the event - can be null</dd>
<dd><code>getActingBlock</code> - A function to get the block acting upon the other blocks, i.e. a piston pushing blocks or a water source spawning new water blocks</dd>
<dd><code>getSubjectBlocks</code> - A function to get the list of blocks being acted upon, i.e. the blocks being pushed by a piston or the water block being spawned</dd>
</dl>
</section>
</li>
<li>
<section class="detail" id="setProtectionTypes(redempt.redlib.protection.ProtectionPolicy.ProtectionType...)">
<h3>setProtectionTypes</h3>
<div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type">void</span>&nbsp;<span class="element-name">setProtectionTypes</span><wbr><span class="parameters">(<a href="ProtectionPolicy.ProtectionType.html" title="enum class in redempt.redlib.protection">ProtectionPolicy.ProtectionType</a>...&nbsp;protections)</span></div>
Expand Down
46 changes: 39 additions & 7 deletions src/redempt/redlib/protection/ProtectionPolicy.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redempt.redlib.protection;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
Expand All @@ -20,6 +21,7 @@
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.player.PlayerBucketEmptyEvent;
import org.bukkit.event.player.PlayerBucketFillEvent;
Expand All @@ -28,6 +30,7 @@
import org.bukkit.event.world.PortalCreateEvent;
import org.bukkit.event.world.PortalCreateEvent.CreateReason;
import org.bukkit.event.world.StructureGrowEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;

import org.bukkit.plugin.Plugin;
Expand Down Expand Up @@ -63,13 +66,7 @@ private static void registerProtections() {
}
return e.getClickedBlock();
});
ProtectionListener.protect(InventoryOpenEvent.class, ProtectionType.CONTAINER_ACCESS, e -> (Player) e.getPlayer(), e -> {
InventoryHolder holder = e.getInventory().getHolder();
if (holder instanceof BlockState) {
return ((BlockState) holder).getBlock();
}
return null;
});
ProtectionListener.protect(InventoryOpenEvent.class, ProtectionType.CONTAINER_ACCESS, e -> (Player) e.getPlayer(), e -> getBlock(e.getInventory()));
ProtectionListener.protectMultiBlock(EntityExplodeEvent.class, ProtectionType.ENTITY_EXPLOSION, e -> null, (e, b) -> e.blockList().remove(b), e -> e.blockList());
ProtectionListener.protectMultiBlock(BlockExplodeEvent.class, ProtectionType.BLOCK_EXPLOSION, e -> null, (e, b) -> e.blockList().remove(b), e -> e.blockList());
ProtectionListener.protect(PlayerBucketFillEvent.class, ProtectionType.USE_BUCKETS, e -> e.getPlayer(), e -> e.getBlockClicked());
Expand Down Expand Up @@ -128,6 +125,15 @@ private static void registerProtections() {
ProtectionListener.protectMultiBlock(StructureGrowEvent.class, ProtectionType.STRUCTURE_GROWTH, e -> null, (e, b) -> e.setCancelled(true), e -> e.getBlocks().stream().map(BlockState::getBlock).collect(Collectors.toList()));
ProtectionListener.protectDirectional(StructureGrowEvent.class, ProtectionType.STRUCTURE_GROWTH_IN, e -> null, e -> e.getLocation().getBlock(), e -> e.getBlocks().stream().map(BlockState::getBlock).collect(Collectors.toList()));
ProtectionListener.protect(EntityBlockFormEvent.class, ProtectionType.ENTITY_FORM_BLOCK, e -> e.getEntity() instanceof Player ? (Player) e.getEntity() : null, e -> e.getBlock());
ProtectionListener.protectDirectional(InventoryMoveItemEvent.class, ProtectionType.CONTAINER_ACCESS, e -> null, e -> getBlock(e.getSource()), e -> Collections.singletonList(getBlock(e.getDestination())));
}

private static Block getBlock(Inventory inv) {
InventoryHolder holder = inv.getHolder();
if (holder instanceof BlockState) {
return ((BlockState) holder).getBlock();
}
return null;
}

/**
Expand Down Expand Up @@ -155,6 +161,32 @@ public static <T extends Event> void registerProtectionNonCancellable(Class<T> c
ProtectionListener.protectNonCancellable(clazz, type, getPlayer, cancel, getBlocks);
}

/**
* Registers a custom event that modifies multiple blocks to be protected using a specific ProtectionType
* @param clazz The event class
* @param type The ProtectionType to protect against this event
* @param getPlayer A function to get the player associated with the event - can return null
* @param cancel A function to cancel the modification of a specific block in the event
* @param getBlocks A vararg of functions to get the blocks associated with the event
* @param <T> The event type
*/
public static <T extends Event> void registerProtectionMultiBlock(Class<T> clazz, ProtectionType type, Function<T, Player> getPlayer, BiConsumer<T, Block> cancel, Function<T, List<Block>> getBlocks) {
ProtectionListener.protectMultiBlock(clazz, type, getPlayer, cancel, getBlocks);
}

/**
* Registers a custom event to be protected using a specific ProtectionType, where there is a block acting and blocks being acted upon
* @param clazz The event class
* @param type The ProtectionType to protect against this event
* @param getPlayer A function to get the player associated with the event - can be null
* @param getActingBlock A function to get the block acting upon the other blocks, i.e. a piston pushing blocks or a water source spawning new water blocks
* @param getSubjectBlocks A function to get the list of blocks being acted upon, i.e. the blocks being pushed by a piston or the water block being spawned
* @param <T> The event type
*/
public static <T extends Event & Cancellable> void registerProtectionDirectional(Class<T> clazz, ProtectionType type, Function<T, Player> getPlayer, Function<T, Block> getActingBlock, Function<T, List<Block>> getSubjectBlocks) {
ProtectionListener.protectDirectional(clazz, type, getPlayer, getActingBlock, getSubjectBlocks);
}

private List<BypassPolicy> bypassPolicies = new ArrayList<>();
private Set<ProtectionType> protections = EnumSet.noneOf(ProtectionType.class);
private Map<ProtectionType, String> messages = new EnumMap<>(ProtectionType.class);
Expand Down
Loading

0 comments on commit 889d92d

Please sign in to comment.