Log falling blocks properly

This commit is contained in:
Ammar Askar
2012-09-22 15:44:20 +05:00
committed by md_5
parent 89fe3b8817
commit 18c86818fb
2 changed files with 52 additions and 10 deletions

View File

@@ -2,6 +2,8 @@ package de.diddiz.LogBlock.listeners;
import static de.diddiz.LogBlock.config.Config.getWorldConfig; import static de.diddiz.LogBlock.config.Config.getWorldConfig;
import static de.diddiz.LogBlock.config.Config.isLogging; import static de.diddiz.LogBlock.config.Config.isLogging;
import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@@ -11,6 +13,7 @@ import org.bukkit.event.player.PlayerBucketEmptyEvent;
import de.diddiz.LogBlock.LogBlock; import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging; import de.diddiz.LogBlock.Logging;
import de.diddiz.LogBlock.config.WorldConfig; import de.diddiz.LogBlock.config.WorldConfig;
import de.diddiz.util.BukkitUtils;
public class BlockPlaceLogging extends LoggingListener public class BlockPlaceLogging extends LoggingListener
{ {
@@ -26,8 +29,32 @@ public class BlockPlaceLogging extends LoggingListener
final BlockState before = event.getBlockReplacedState(); final BlockState before = event.getBlockReplacedState();
final BlockState after = event.getBlockPlaced().getState(); final BlockState after = event.getBlockPlaced().getState();
final String playerName = event.getPlayer().getName(); final String playerName = event.getPlayer().getName();
//Handle falling blocks
if (event.getBlock().getType() == Material.SAND || event.getBlock().getType() == Material.GRAVEL) {
Location loc = event.getBlock().getLocation();
int x = loc.getBlockX();
int y = loc.getBlockY();
int z = loc.getBlockZ();
while (y > 0 && BukkitUtils.canFall(loc.getWorld(), x, (y - 1), z)) {
y--;
}
// If y is 0 then the sand block fell out of the world :(
if (y != 0) {
Location finalLoc = new Location(loc.getWorld(), x, y, z);
if (finalLoc.getBlock().getType() == Material.AIR) {
consumer.queueBlockPlace(playerName, finalLoc, type, event.getBlock().getData());
} else {
consumer.queueBlockReplace(playerName, finalLoc, finalLoc.getBlock().getTypeId(), finalLoc.getBlock().getData(), type, event.getBlock().getData());
}
}
return;
}
//Sign logging is handled elsewhere
if (wcfg.isLogging(Logging.SIGNTEXT) && (type == 63 || type == 68)) if (wcfg.isLogging(Logging.SIGNTEXT) && (type == 63 || type == 68))
return; return;
//Delay queuing by one tick to allow data to be updated //Delay queuing by one tick to allow data to be updated
LogBlock.getInstance().getServer().getScheduler().scheduleSyncDelayedTask(LogBlock.getInstance(), new Runnable() LogBlock.getInstance().getServer().getScheduler().scheduleSyncDelayedTask(LogBlock.getInstance(), new Runnable()
{ {

View File

@@ -12,6 +12,7 @@ import java.util.Set;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Chunk; import org.bukkit.Chunk;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
@@ -76,15 +77,15 @@ public class BukkitUtils
*/ */
public static List<Location> getBlocksNearby(org.bukkit.block.Block block, Set<Integer> type) { public static List<Location> getBlocksNearby(org.bukkit.block.Block block, Set<Integer> type) {
ArrayList<Location> blocks = new ArrayList<Location>(); ArrayList<Location> blocks = new ArrayList<Location>();
if(type.contains(block.getRelative(BlockFace.EAST).getTypeId())) if (type.contains(block.getRelative(BlockFace.EAST).getTypeId()))
blocks.add(block.getRelative(BlockFace.EAST).getLocation()); blocks.add(block.getRelative(BlockFace.EAST).getLocation());
if(type.contains(block.getRelative(BlockFace.WEST).getTypeId())) if (type.contains(block.getRelative(BlockFace.WEST).getTypeId()))
blocks.add(block.getRelative(BlockFace.WEST).getLocation()); blocks.add(block.getRelative(BlockFace.WEST).getLocation());
if(type.contains(block.getRelative(BlockFace.NORTH).getTypeId())) if (type.contains(block.getRelative(BlockFace.NORTH).getTypeId()))
blocks.add(block.getRelative(BlockFace.NORTH).getLocation()); blocks.add(block.getRelative(BlockFace.NORTH).getLocation());
if(type.contains(block.getRelative(BlockFace.SOUTH).getTypeId())) if (type.contains(block.getRelative(BlockFace.SOUTH).getTypeId()))
blocks.add(block.getRelative(BlockFace.SOUTH).getLocation()); blocks.add(block.getRelative(BlockFace.SOUTH).getLocation());
if(type.contains(block.getRelative(BlockFace.UP).getTypeId())) if (type.contains(block.getRelative(BlockFace.UP).getTypeId()))
blocks.add(block.getRelative(BlockFace.UP).getLocation()); blocks.add(block.getRelative(BlockFace.UP).getLocation());
return blocks; return blocks;
} }
@@ -252,6 +253,20 @@ public class BukkitUtils
return 0; return 0;
} }
public static boolean canFall(World world, int x, int y, int z) {
Material mat = world.getBlockAt(x, y, z).getType();
// Air
if (mat == Material.AIR) {
return true;
} else if (mat == Material.WATER || mat == Material.STATIONARY_WATER || mat == Material.LAVA || mat == Material.STATIONARY_LAVA) { // Fluids
return true;
} else if (mat == Material.SIGN || mat == Material.FIRE) { // Misc.
return true;
}
return false;
}
public static class ItemStackComparator implements Comparator<ItemStack> public static class ItemStackComparator implements Comparator<ItemStack>
{ {
@Override @Override