Skip to content

Commit

Permalink
trying to fix bug #7 - waterlogged blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
tremor committed Jan 16, 2022
1 parent 3484b2a commit 99ab9d3
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 18 deletions.
74 changes: 62 additions & 12 deletions src/com/pwn9/PwnBuckets/EvaporateWaterTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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.*;

public class EvaporateWaterTask implements Runnable
{
Expand All @@ -17,41 +17,91 @@ public EvaporateWaterTask(Block block)
@Override
public void run()
{
if (isWater(this.block))
Block b = this.block;
Block n = block.getRelative(BlockFace.NORTH);
Block s = block.getRelative(BlockFace.SOUTH);
Block e = block.getRelative(BlockFace.EAST);
Block w = block.getRelative(BlockFace.WEST);
Block d = block.getRelative(BlockFace.DOWN);
Block u = block.getRelative(BlockFace.UP);

if (isWater(b))
{
this.block.setType(Material.AIR);
b.setType(Material.AIR);
}
if (isWater(this.block.getRelative(BlockFace.NORTH)))
if (isWater(n))
{
this.block.getRelative(BlockFace.NORTH).setType(Material.AIR);
n.setType(Material.AIR);
}
if (isWater(this.block.getRelative(BlockFace.SOUTH)))
if (isWater(s))
{
this.block.getRelative(BlockFace.SOUTH).setType(Material.AIR);
s.setType(Material.AIR);
}
if (isWater(this.block.getRelative(BlockFace.WEST)))
if (isWater(w))
{
this.block.getRelative(BlockFace.WEST).setType(Material.AIR);
w.setType(Material.AIR);
}
if (isWater(this.block.getRelative(BlockFace.EAST)))
if (isWater(e))
{
this.block.getRelative(BlockFace.EAST).setType(Material.AIR);
e.setType(Material.AIR);
}

// check if the block is waterlogged - this check should probably be done first, and separately, perhaps with a different routine?
if (isWaterLog(b))
{
Material m = b.getType();
PwnBuckets.logToFile("Removing waterlog status from block: " + m.toString());
//b.setType(Material.AIR);
b.setType(m);
}
if (isWaterLog(d))
{
Material m = d.getType();
PwnBuckets.logToFile("Removing waterlog status from block: " + m.toString());
//d.setType(Material.AIR);
d.setType(m);
}
if (isWaterLog(u))
{
Material m = u.getType();
PwnBuckets.logToFile("Removing waterlog status from block: " + m.toString());
//u.setType(Material.AIR);
u.setType(m);
}
}

// https://hub.spigotmc.org/javadocs/spigot/org/bukkit/block/data/Levelled.html
public boolean isWater(Block block)
{
if ((block.getType() == Material.WATER) && (block.getBlockData() instanceof Levelled))
{
Levelled levelledBlock = (Levelled) block.getBlockData();
int level = levelledBlock.getLevel();
//source block
// can a waterlogged block be a 0 level - source block?
if (level == 0)
{
return true;
}
}
return false;
}
}

// determine if the location is a waterlogged block
// https://hub.spigotmc.org/javadocs/spigot/org/bukkit/block/data/Waterlogged.html
public boolean isWaterLog(Block block)
{
if (block.getBlockData() instanceof Waterlogged)
{
Waterlogged wetBlock = (Waterlogged) block.getBlockData();
if (wetBlock.isWaterlogged())
{
// don't think this makes a difference
//wetBlock.setWaterlogged(false);
return true;
}
}
return false;
}

}
42 changes: 36 additions & 6 deletions src/com/pwn9/PwnBuckets/WaterListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.Directional;
import org.bukkit.block.data.Levelled;
import org.bukkit.block.data.*;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand Down Expand Up @@ -68,9 +67,21 @@ public void onPlayerEmptyBucket(PlayerBucketEmptyEvent event)

if(PwnBuckets.blockWaterBucket)
{
//TODO: this won't work for blocks that can be waterlogged
//BUG: seems to set some solid blocks to air...

Block block = event.getBlockClicked().getRelative(event.getBlockFace());

block.getBlockData();

// lets also get the block clicked
Block eblock = event.getBlockClicked();

if (isWaterLog(eblock))
{
// got a plan?
}



// If the block is already water then don't do anything, otherwise set it water then set it air for water effect.
if (!isWater(block))
{
Expand All @@ -79,8 +90,8 @@ public void onPlayerEmptyBucket(PlayerBucketEmptyEvent event)
plugin.getServer().getScheduler().runTaskLater(plugin, task, 30L);
}


// no need to cancel the event anymore, let it finish and evaporate the water

if (PwnBuckets.logEnabled)
{
PwnBuckets.logToFile("Blocked water source from bucket");
Expand Down Expand Up @@ -312,7 +323,26 @@ public boolean isWater(Block block)
}
}
return false;
}
}

// determine if the location is a waterlogged block
public boolean isWaterLog(Block block)
{
if (block.getBlockData() instanceof Waterlogged)
{
Waterlogged wetBlock = (Waterlogged) block.getBlockData();
if (wetBlock.isWaterlogged())
{
//wetBlock.setWaterlogged(false);
if (PwnBuckets.logEnabled)
{
PwnBuckets.logToFile("Placing bucket on waterlogged block.");
}
return true;
}
}
return false;
}

// to get a source lava not flowing lava
public boolean isLava(Block block)
Expand Down

0 comments on commit 99ab9d3

Please sign in to comment.