Skip to content

Commit

Permalink
Merge pull request #18 from bermudalocket/fix-dispensers-bubble-columns
Browse files Browse the repository at this point in the history
Fix dispensers and bubble columns
  • Loading branch information
totemo authored Oct 6, 2019
2 parents b12fba4 + 7e81922 commit ffc76df
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>SafeBuckets</groupId>
<artifactId>SafeBuckets</artifactId>
<version>1.1.19</version>
<version>1.1.20</version>
<packaging>jar</packaging>
<name>SafeBuckets</name>
<description>Make water non-flowing for water from buckets</description>
Expand Down
2 changes: 1 addition & 1 deletion src/nu/nerd/SafeBuckets/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ private void bulkSafetyToggle(Player player, boolean state) {
for (int y = min.getBlockY(); y <= max.getBlockY(); y++) {
for (int z = min.getBlockZ(); z <= max.getBlockZ(); z++) {
Block block = world.getBlockAt(x, y, z);
if (block.getType() == Material.WATER || block.getType() == Material.LAVA || Util.isWaterlogged(block)) {
if (block.getType() == Material.BUBBLE_COLUMN || block.getType() == Material.WATER || block.getType() == Material.LAVA || Util.isWaterlogged(block)) {
if (state && block.getBlockData() instanceof Levelled) {
Levelled levelled = (Levelled) block.getBlockData();
if (levelled.getLevel() > 0) {
Expand Down
62 changes: 42 additions & 20 deletions src/nu/nerd/SafeBuckets/SafeBuckets.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.Levelled;
import org.bukkit.block.data.type.BubbleColumn;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
Expand Down Expand Up @@ -115,29 +118,48 @@ static boolean isSafe(Block block) {
* in the internal cache and BlockStore.
*
* @param block the block.
* @param state the safety status (true = safe).
* @param safe the safety status (true = safe).
*/
static void setSafe(Block block, boolean state) {
Bukkit.getScheduler().runTaskLater(PLUGIN, () -> {
if (state) {
sendDebugMessage("Safety change: " + Util.formatCoords(block.getLocation()) + " has been made §aSAFE");
CACHE.add(block.getLocation());
} else {
sendDebugMessage("Safety change: " + Util.formatCoords(block.getLocation()) + " has been made §cUNSAFE");
CACHE.remove(block.getLocation());
}

// check if there's an entry before spawning particles
if (CONFIG.SHOW_PARTICLES && BlockStoreApi.getBlockMeta(block, PLUGIN, METADATA_KEY) != null) {
Bukkit.getScheduler().runTaskLaterAsynchronously(PLUGIN, () -> Util.showParticles(block, state), 1);
}

BlockStoreApi.setBlockMeta(block, PLUGIN, METADATA_KEY, state);
static void setSafe(Block block, boolean safe) {
// update blockstore
if (safe) {
CACHE.add(block.getLocation());
BlockStoreApi.setBlockMeta(block, PLUGIN, METADATA_KEY, true);
} else {
CACHE.remove(block.getLocation());
BlockStoreApi.removeBlockMeta(block, PLUGIN, METADATA_KEY);
}

if (!state) {
Util.forceBlockUpdate(block);
// force block updates (note: can be simplified once Spigot adds new block update method
// see: https://hub.spigotmc.org/jira/browse/SPIGOT-4759
if (!safe) {
if (block.getType() == Material.BUBBLE_COLUMN) {
final boolean isDrag = ((BubbleColumn) block.getBlockData()).isDrag();
block.setType(Material.WATER, true);
Bukkit.getScheduler().runTask(PLUGIN, () -> {
block.setType(Material.BUBBLE_COLUMN, true);
((BubbleColumn) block.getBlockData()).setDrag(isDrag);
});
} else if (block.getType() == Material.WATER || block.getType() == Material.LAVA) {
Levelled levelled = (Levelled) block.getBlockData();
levelled.setLevel(1);
block.setBlockData(levelled);
Bukkit.getScheduler().runTask(PLUGIN, () -> {
levelled.setLevel(0);
block.setBlockData(levelled);
});
} else if (Util.isWaterlogged(block)) {
for (BlockFace blockFace : Util.ADJACENT_BLOCK_FACES) {
Block adjacentBlock = block.getRelative(blockFace);
Material type = adjacentBlock.getType();
if (type == Material.AIR || type == Material.CAVE_AIR) {
adjacentBlock.setType(Material.VOID_AIR, true);
Bukkit.getScheduler().runTask(PLUGIN, () -> adjacentBlock.setType(type));
break;
}
}
}
}, 1);
}
}

// ------------------------------------------------------------------------
Expand Down
10 changes: 5 additions & 5 deletions src/nu/nerd/SafeBuckets/SafeBucketsListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,20 @@ public class SafeBucketsListener implements Listener {
public void onBlockDispense(BlockDispenseEvent event) {
Material material = event.getItem().getType();
Dispenser dispenser = (Dispenser) event.getBlock().getState().getData();
Block dispensed = event.getBlock().getRelative(dispenser.getFacing());
Block adjacentBlock = event.getBlock().getRelative(dispenser.getFacing());

if (SafeBuckets.CONFIG.BUCKETS.contains(material)) { // filled bucket being dumped
if (SafeBuckets.CONFIG.DISPENSERS_ENABLED) {
if (SafeBuckets.CONFIG.DISPENSERS_SAFE && SafeBuckets.isSafe(event.getBlock())) {
SafeBuckets.setSafe(dispensed, true);
SafeBuckets.setSafe(adjacentBlock, true);
}
} else {
event.setCancelled(true);
}
} else if (material == Material.BUCKET) { // empty bucket picking up liquid block
if (SafeBuckets.CONFIG.LIQUID_BLOCKS.contains(dispensed.getType())) {
} else if (material == Material.BUCKET) { // empty bucket picking up liquid block or a waterloggable
if (Util.isWaterlogged(adjacentBlock) || SafeBuckets.CONFIG.LIQUID_BLOCKS.contains(adjacentBlock.getType())) {
if (SafeBuckets.CONFIG.DISPENSERS_ENABLED) {
SafeBuckets.removeSafe(dispensed);
SafeBuckets.removeSafe(adjacentBlock);
}
} else {
event.setCancelled(true);
Expand Down
2 changes: 1 addition & 1 deletion src/nu/nerd/SafeBuckets/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ static boolean limitKelpHeight(Block kelp) {
/**
* A set of BlockFaces directly adjacent to an abstract block.
*/
private static final HashSet<BlockFace> ADJACENT_BLOCK_FACES = new HashSet<>(Arrays.asList(
public static final HashSet<BlockFace> ADJACENT_BLOCK_FACES = new HashSet<>(Arrays.asList(
BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST, BlockFace.UP, BlockFace.DOWN
));

Expand Down

0 comments on commit ffc76df

Please sign in to comment.