Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't allow dispensing items if redstone is disallowed #110

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bukkit/src/main/java/org/popcraft/bolt/BoltPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.popcraft.bolt.listeners.InventoryListener;
import org.popcraft.bolt.listeners.PlayerListener;
import org.popcraft.bolt.listeners.adapter.AnvilDamagedListener;
import org.popcraft.bolt.listeners.adapter.BlockPreDispenseListener;
import org.popcraft.bolt.listeners.adapter.PlayerRecipeBookClickListener;
import org.popcraft.bolt.matcher.Match;
import org.popcraft.bolt.matcher.block.AmethystClusterMatcher;
Expand Down Expand Up @@ -410,6 +411,7 @@ private void registerEvents() {
pluginManager.registerEvents(blockListener, this);
if (PaperUtil.isPaper()) {
pluginManager.registerEvents(new PlayerRecipeBookClickListener(blockListener::onPlayerRecipeBookClick), this);
pluginManager.registerEvents(new BlockPreDispenseListener(blockListener::onBlockPreDispense), this);
}
pluginManager.registerEvents(new EntityListener(this), this);
final InventoryListener inventoryListener = new InventoryListener(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockDispenseEvent;
import org.bukkit.event.block.BlockEvent;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.block.BlockFadeEvent;
import org.bukkit.event.block.BlockFormEvent;
Expand Down Expand Up @@ -653,6 +654,10 @@ public void onBlockDispense(final BlockDispenseEvent e) {
if (existingProtection == null) {
return;
}
if (!plugin.canAccess(existingProtection, REDSTONE_SOURCE_RESOLVER, Permission.REDSTONE)) {
e.setCancelled(true);
return;
}
final BlockData blockData = block.getBlockData();
if (!(blockData instanceof final Directional directional)) {
return;
Expand All @@ -675,6 +680,20 @@ public void onBlockDispense(final BlockDispenseEvent e) {
});
}

public void onBlockPreDispense(final BlockEvent e) {
if (!(e instanceof Cancellable cancellable)) {
return;
}
final Block block = e.getBlock();
final Protection existingProtection = plugin.findProtection(block);
if (existingProtection == null) {
return;
}
if (!plugin.canAccess(existingProtection, REDSTONE_SOURCE_RESOLVER, Permission.REDSTONE)) {
cancellable.setCancelled(true);
}
}

@EventHandler
public void onBlockReceiveGameEvent(final BlockReceiveGameEvent e) {
if (!(e.getEntity() instanceof final Player player)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.popcraft.bolt.listeners.adapter;

import io.papermc.paper.event.block.BlockPreDispenseEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockEvent;

import java.util.function.Consumer;

public record BlockPreDispenseListener(Consumer<BlockEvent> handler) implements Listener {
@EventHandler
public void onBlockPreDispense(final BlockPreDispenseEvent e) {
handler.accept(e);
}
}
Loading