2013-01-08 14:39:28 +11:00
|
|
|
package de.diddiz.util;
|
|
|
|
|
|
2015-02-14 13:52:06 +00:00
|
|
|
import de.diddiz.LogBlock.Actor;
|
|
|
|
|
import de.diddiz.LogBlock.Consumer;
|
|
|
|
|
import de.diddiz.LogBlock.Logging;
|
|
|
|
|
import de.diddiz.LogBlock.config.WorldConfig;
|
2013-01-08 14:39:28 +11:00
|
|
|
import org.bukkit.Location;
|
|
|
|
|
import org.bukkit.Material;
|
2018-07-21 02:41:52 +02:00
|
|
|
import org.bukkit.Tag;
|
2013-01-08 14:39:28 +11:00
|
|
|
import org.bukkit.block.Block;
|
|
|
|
|
import org.bukkit.block.BlockFace;
|
2018-08-01 20:01:51 +02:00
|
|
|
import org.bukkit.block.BlockState;
|
2013-01-08 14:39:28 +11:00
|
|
|
import org.bukkit.block.Sign;
|
2018-07-21 02:41:52 +02:00
|
|
|
import org.bukkit.block.data.BlockData;
|
|
|
|
|
import org.bukkit.block.data.Directional;
|
2015-03-22 20:15:04 +05:00
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import static de.diddiz.LogBlock.config.Config.getWorldConfig;
|
|
|
|
|
import static de.diddiz.LogBlock.config.Config.mb4;
|
2013-01-08 14:39:28 +11:00
|
|
|
|
|
|
|
|
public class LoggingUtil {
|
|
|
|
|
|
2018-08-01 20:01:51 +02:00
|
|
|
public static void smartLogBlockPlace(Consumer consumer, Actor actor, BlockState replaced, BlockState placed) {
|
|
|
|
|
Location loc = replaced.getLocation();
|
|
|
|
|
if (!placed.getType().hasGravity() || !BukkitUtils.canDirectlyFallIn(replaced.getBlock().getRelative(BlockFace.DOWN).getType())) {
|
|
|
|
|
if (BukkitUtils.isEmpty(replaced.getType())) {
|
|
|
|
|
consumer.queueBlockPlace(actor, placed);
|
|
|
|
|
} else {
|
|
|
|
|
consumer.queueBlockReplace(actor, replaced, placed);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int x = loc.getBlockX();
|
|
|
|
|
int initialy = loc.getBlockY();
|
|
|
|
|
int y = initialy;
|
|
|
|
|
int z = loc.getBlockZ();
|
|
|
|
|
while (y > 0 && BukkitUtils.canFallIn(loc.getWorld(), x, (y - 1), z)) {
|
|
|
|
|
y--;
|
|
|
|
|
}
|
|
|
|
|
if (initialy != y && !BukkitUtils.isEmpty(replaced.getType())) {
|
|
|
|
|
// this is not the final location but the block got removed (vines etc)
|
|
|
|
|
consumer.queueBlockBreak(actor, replaced);
|
|
|
|
|
}
|
|
|
|
|
// If y is 0 then the block fell out of the world :(
|
|
|
|
|
if (y != 0) {
|
|
|
|
|
// Run this check to avoid false positives
|
|
|
|
|
Location finalLoc = new Location(loc.getWorld(), x, y, z);
|
|
|
|
|
if (y == initialy || !BukkitUtils.getFallingEntityKillers().contains(finalLoc.getBlock().getType())) {
|
|
|
|
|
if (BukkitUtils.isEmpty(finalLoc.getBlock().getType())) {
|
|
|
|
|
consumer.queueBlockPlace(actor, finalLoc, placed.getBlockData());
|
|
|
|
|
} else {
|
|
|
|
|
consumer.queueBlockReplace(actor, finalLoc.getBlock().getState(), placed.getBlockData());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-22 20:15:04 +05:00
|
|
|
public static void smartLogFallables(Consumer consumer, Actor actor, Block origin) {
|
2013-01-08 14:39:28 +11:00
|
|
|
|
2015-03-22 20:15:04 +05:00
|
|
|
WorldConfig wcfg = getWorldConfig(origin.getWorld());
|
|
|
|
|
if (wcfg == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-01-08 14:39:28 +11:00
|
|
|
|
2015-03-22 20:15:04 +05:00
|
|
|
//Handle falling blocks
|
|
|
|
|
Block checkBlock = origin.getRelative(BlockFace.UP);
|
|
|
|
|
int up = 0;
|
|
|
|
|
final int highestBlock = checkBlock.getWorld().getHighestBlockYAt(checkBlock.getLocation());
|
2018-07-21 02:41:52 +02:00
|
|
|
while (checkBlock.getType().hasGravity()) {
|
2013-01-08 14:39:28 +11:00
|
|
|
|
2015-03-22 20:15:04 +05:00
|
|
|
// Record this block as falling
|
|
|
|
|
consumer.queueBlockBreak(actor, checkBlock.getState());
|
2013-01-08 14:39:28 +11:00
|
|
|
|
2015-03-22 20:15:04 +05:00
|
|
|
// Guess where the block is going (This could be thrown of by explosions, but it is better than nothing)
|
|
|
|
|
Location loc = origin.getLocation();
|
|
|
|
|
int x = loc.getBlockX();
|
|
|
|
|
int y = loc.getBlockY();
|
|
|
|
|
int z = loc.getBlockZ();
|
2018-08-01 20:01:51 +02:00
|
|
|
while (y > 0 && BukkitUtils.canFallIn(loc.getWorld(), x, (y - 1), z)) {
|
2015-03-22 20:15:04 +05:00
|
|
|
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);
|
|
|
|
|
// Run this check to avoid false positives
|
|
|
|
|
if (!BukkitUtils.getFallingEntityKillers().contains(finalLoc.getBlock().getType())) {
|
|
|
|
|
finalLoc.add(0, up, 0); // Add this here after checking for block breakers
|
2018-07-27 17:19:15 +02:00
|
|
|
if (BukkitUtils.isEmpty(finalLoc.getBlock().getType())) {
|
2018-07-21 02:41:52 +02:00
|
|
|
consumer.queueBlockPlace(actor, finalLoc, checkBlock.getBlockData());
|
2015-03-22 20:15:04 +05:00
|
|
|
} else {
|
2018-07-21 02:41:52 +02:00
|
|
|
consumer.queueBlockReplace(actor, finalLoc, finalLoc.getBlock().getBlockData(), checkBlock.getBlockData());
|
2015-03-22 20:15:04 +05:00
|
|
|
}
|
|
|
|
|
up++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (checkBlock.getY() >= highestBlock) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
checkBlock = checkBlock.getRelative(BlockFace.UP);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-01-08 14:39:28 +11:00
|
|
|
|
2015-03-22 20:15:04 +05:00
|
|
|
public static void smartLogBlockBreak(Consumer consumer, Actor actor, Block origin) {
|
2013-01-08 14:39:28 +11:00
|
|
|
|
2015-03-22 20:15:04 +05:00
|
|
|
WorldConfig wcfg = getWorldConfig(origin.getWorld());
|
|
|
|
|
if (wcfg == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-01-08 14:39:28 +11:00
|
|
|
|
2015-03-22 20:15:04 +05:00
|
|
|
Block checkBlock = origin.getRelative(BlockFace.UP);
|
|
|
|
|
if (BukkitUtils.getRelativeTopBreakabls().contains(checkBlock.getType())) {
|
2018-07-21 02:41:52 +02:00
|
|
|
if (wcfg.isLogging(Logging.SIGNTEXT) && checkBlock.getType() == Material.SIGN) {
|
2015-03-22 20:15:04 +05:00
|
|
|
consumer.queueSignBreak(actor, (Sign) checkBlock.getState());
|
2018-07-21 02:41:52 +02:00
|
|
|
} else if (checkBlock.getType() == Material.IRON_DOOR || Tag.WOODEN_DOORS.isTagged(checkBlock.getType())) {
|
2015-03-22 20:15:04 +05:00
|
|
|
Block doorBlock = checkBlock;
|
|
|
|
|
// If the doorBlock is the top half a door the player simply punched a door
|
|
|
|
|
// this will be handled later.
|
2018-07-21 02:41:52 +02:00
|
|
|
if (!BukkitUtils.isTop(doorBlock.getBlockData())) {
|
2015-03-22 20:15:04 +05:00
|
|
|
doorBlock = doorBlock.getRelative(BlockFace.UP);
|
|
|
|
|
// Fall back check just in case the top half wasn't a door
|
2018-07-21 02:41:52 +02:00
|
|
|
if (doorBlock.getType() == Material.IRON_DOOR || Tag.WOODEN_DOORS.isTagged(doorBlock.getType())) {
|
2015-03-22 20:15:04 +05:00
|
|
|
consumer.queueBlockBreak(actor, doorBlock.getState());
|
|
|
|
|
}
|
|
|
|
|
consumer.queueBlockBreak(actor, checkBlock.getState());
|
|
|
|
|
}
|
2018-07-21 02:41:52 +02:00
|
|
|
} else if (BukkitUtils.isDoublePlant(checkBlock.getType())) {
|
2015-03-22 20:15:04 +05:00
|
|
|
Block plantBlock = checkBlock;
|
|
|
|
|
// If the plantBlock is the top half of a double plant the player simply
|
|
|
|
|
// punched the plant this will be handled later.
|
2018-07-21 02:41:52 +02:00
|
|
|
if (!BukkitUtils.isTop(plantBlock.getBlockData())) {
|
2015-03-22 20:15:04 +05:00
|
|
|
plantBlock = plantBlock.getRelative(BlockFace.UP);
|
|
|
|
|
// Fall back check just in case the top half wasn't a plant
|
2018-07-21 02:41:52 +02:00
|
|
|
if (BukkitUtils.isDoublePlant(plantBlock.getType())) {
|
2015-03-22 20:15:04 +05:00
|
|
|
consumer.queueBlockBreak(actor, plantBlock.getState());
|
|
|
|
|
}
|
|
|
|
|
consumer.queueBlockBreak(actor, checkBlock.getState());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
consumer.queueBlockBreak(actor, checkBlock.getState());
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-01-08 14:39:28 +11:00
|
|
|
|
2015-03-22 20:15:04 +05:00
|
|
|
List<Location> relativeBreakables = BukkitUtils.getBlocksNearby(origin, BukkitUtils.getRelativeBreakables());
|
|
|
|
|
if (relativeBreakables.size() != 0) {
|
|
|
|
|
for (Location location : relativeBreakables) {
|
2018-07-21 02:41:52 +02:00
|
|
|
Block block = location.getBlock();
|
|
|
|
|
BlockData blockData = block.getBlockData();
|
|
|
|
|
if (blockData instanceof Directional) {
|
|
|
|
|
if (block.getRelative(((Directional) blockData).getFacing()).equals(origin)) {
|
|
|
|
|
if (wcfg.isLogging(Logging.SIGNTEXT) && block.getType() == Material.WALL_SIGN) {
|
|
|
|
|
consumer.queueSignBreak(actor, (Sign) block.getState());
|
|
|
|
|
} else {
|
|
|
|
|
consumer.queueBlockBreak(actor, block.getState());
|
2015-03-22 20:15:04 +05:00
|
|
|
}
|
2018-07-21 02:41:52 +02:00
|
|
|
}
|
2015-03-22 20:15:04 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-08 14:25:44 -04:00
|
|
|
|
2015-03-22 20:15:04 +05:00
|
|
|
// Special door check
|
2018-07-21 02:41:52 +02:00
|
|
|
if (origin.getType() == Material.IRON_DOOR || Tag.WOODEN_DOORS.isTagged(origin.getType())) {
|
2015-03-22 20:15:04 +05:00
|
|
|
Block doorBlock = origin;
|
2013-04-08 14:25:44 -04:00
|
|
|
|
2015-03-22 20:15:04 +05:00
|
|
|
// Up or down?
|
2018-07-21 02:41:52 +02:00
|
|
|
if (!BukkitUtils.isTop(doorBlock.getBlockData())) {
|
2015-03-22 20:15:04 +05:00
|
|
|
doorBlock = doorBlock.getRelative(BlockFace.UP);
|
|
|
|
|
} else {
|
|
|
|
|
doorBlock = doorBlock.getRelative(BlockFace.DOWN);
|
|
|
|
|
}
|
2013-04-08 14:25:44 -04:00
|
|
|
|
2018-07-21 02:41:52 +02:00
|
|
|
if (doorBlock.getType() == Material.IRON_DOOR || Tag.WOODEN_DOORS.isTagged(doorBlock.getType())) {
|
2015-03-22 20:15:04 +05:00
|
|
|
consumer.queueBlockBreak(actor, doorBlock.getState());
|
|
|
|
|
}
|
2018-07-21 02:41:52 +02:00
|
|
|
} else if (BukkitUtils.isDoublePlant(origin.getType())) { // Special double plant check
|
2015-03-22 20:15:04 +05:00
|
|
|
Block plantBlock = origin;
|
2013-12-06 01:19:04 -05:00
|
|
|
|
2015-03-22 20:15:04 +05:00
|
|
|
// Up or down?
|
2018-07-21 02:41:52 +02:00
|
|
|
if (!BukkitUtils.isTop(origin.getBlockData())) {
|
2015-03-22 20:15:04 +05:00
|
|
|
plantBlock = plantBlock.getRelative(BlockFace.UP);
|
|
|
|
|
} else {
|
|
|
|
|
plantBlock = plantBlock.getRelative(BlockFace.DOWN);
|
|
|
|
|
}
|
2013-12-06 01:19:04 -05:00
|
|
|
|
2018-07-21 02:41:52 +02:00
|
|
|
if (BukkitUtils.isDoublePlant(plantBlock.getType())) {
|
2015-03-22 20:15:04 +05:00
|
|
|
consumer.queueBlockBreak(actor, plantBlock.getState());
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-08 14:25:44 -04:00
|
|
|
|
2015-03-22 20:15:04 +05:00
|
|
|
// Do this down here so that the block is added after blocks sitting on it
|
|
|
|
|
consumer.queueBlockBreak(actor, origin.getState());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String checkText(String text) {
|
|
|
|
|
if (text == null) {
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
if (mb4) {
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
return text.replaceAll("[^\\u0000-\\uFFFF]", "?");
|
|
|
|
|
}
|
2013-01-08 14:39:28 +11:00
|
|
|
}
|