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.config.WorldConfig;
|
2013-01-08 14:39:28 +11:00
|
|
|
import org.bukkit.Location;
|
|
|
|
|
import org.bukkit.Material;
|
|
|
|
|
import org.bukkit.block.Block;
|
|
|
|
|
import org.bukkit.block.BlockFace;
|
2018-08-01 20:01:51 +02:00
|
|
|
import org.bukkit.block.BlockState;
|
2018-07-21 02:41:52 +02:00
|
|
|
import org.bukkit.block.data.BlockData;
|
|
|
|
|
import org.bukkit.block.data.Directional;
|
2019-06-23 05:23:31 +02:00
|
|
|
import org.bukkit.block.data.type.Bell;
|
|
|
|
|
import org.bukkit.block.data.type.Bell.Attachment;
|
|
|
|
|
import org.bukkit.block.data.type.Lantern;
|
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 {
|
|
|
|
|
|
2019-06-23 16:45:14 +02:00
|
|
|
public static void smartLogBlockPlace(Consumer consumer, Actor actor, BlockState replaced, BlockState placed) {
|
2018-08-01 20:01:51 +02:00
|
|
|
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) {
|
2018-08-21 06:17:24 +02:00
|
|
|
smartLogBlockReplace(consumer, actor, origin, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void smartLogBlockReplace(Consumer consumer, Actor actor, Block origin, BlockData replacedWith) {
|
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);
|
2019-06-23 05:23:31 +02:00
|
|
|
Material typeAbove = checkBlock.getType();
|
|
|
|
|
if (BukkitUtils.getRelativeTopBreakabls().contains(typeAbove)) {
|
|
|
|
|
if (typeAbove == Material.IRON_DOOR || BukkitUtils.isWoodenDoor(typeAbove)) {
|
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-08-03 01:04:38 +02:00
|
|
|
if (doorBlock.getType() == Material.IRON_DOOR || BukkitUtils.isWoodenDoor(doorBlock.getType())) {
|
2015-03-22 20:15:04 +05:00
|
|
|
consumer.queueBlockBreak(actor, doorBlock.getState());
|
|
|
|
|
}
|
|
|
|
|
consumer.queueBlockBreak(actor, checkBlock.getState());
|
|
|
|
|
}
|
2019-06-23 05:23:31 +02:00
|
|
|
} else if (BukkitUtils.isDoublePlant(typeAbove)) {
|
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());
|
|
|
|
|
}
|
2019-06-23 16:45:14 +02:00
|
|
|
} else if (typeAbove == Material.LANTERN) {
|
2019-06-23 05:23:31 +02:00
|
|
|
Lantern lantern = (Lantern) checkBlock.getBlockData();
|
2019-06-23 16:45:14 +02:00
|
|
|
if (!lantern.isHanging()) {
|
2019-06-23 05:23:31 +02:00
|
|
|
consumer.queueBlockBreak(actor, checkBlock.getState());
|
|
|
|
|
}
|
2019-06-23 16:45:14 +02:00
|
|
|
} else if (typeAbove == Material.BELL) {
|
2019-06-23 05:23:31 +02:00
|
|
|
Bell bell = (Bell) checkBlock.getBlockData();
|
2019-06-23 16:45:14 +02:00
|
|
|
if (bell.getAttachment() == Attachment.FLOOR) {
|
2019-06-23 05:23:31 +02:00
|
|
|
consumer.queueBlockBreak(actor, checkBlock.getState());
|
|
|
|
|
}
|
2015-03-22 20:15:04 +05:00
|
|
|
}
|
2019-06-23 16:45:14 +02:00
|
|
|
|
2019-06-23 05:23:31 +02:00
|
|
|
checkBlock = origin.getRelative(BlockFace.DOWN);
|
|
|
|
|
Material typeBelow = checkBlock.getType();
|
2019-06-23 16:45:14 +02:00
|
|
|
if (typeBelow == Material.LANTERN) {
|
2019-06-23 05:23:31 +02:00
|
|
|
Lantern lantern = (Lantern) checkBlock.getBlockData();
|
2019-06-23 16:45:14 +02:00
|
|
|
if (lantern.isHanging()) {
|
2019-06-23 05:23:31 +02:00
|
|
|
consumer.queueBlockBreak(actor, checkBlock.getState());
|
|
|
|
|
}
|
2019-06-23 16:45:14 +02:00
|
|
|
} else if (typeBelow == Material.BELL) {
|
2019-06-23 05:23:31 +02:00
|
|
|
Bell bell = (Bell) checkBlock.getBlockData();
|
2019-06-23 16:45:14 +02:00
|
|
|
if (bell.getAttachment() == Attachment.CEILING) {
|
|
|
|
|
consumer.queueBlockBreak(actor, checkBlock.getState());
|
2019-06-23 05:23:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-06-23 16:45:14 +02: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) {
|
2019-06-23 05:23:31 +02:00
|
|
|
if (blockData.getMaterial() == Material.BELL) {
|
|
|
|
|
if (((Bell) blockData).getAttachment() == Attachment.SINGLE_WALL) {
|
|
|
|
|
if (block.getRelative(((Bell) blockData).getFacing()).equals(origin)) {
|
|
|
|
|
consumer.queueBlockBreak(actor, block.getState());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (block.getRelative(((Directional) blockData).getFacing().getOppositeFace()).equals(origin)) {
|
|
|
|
|
consumer.queueBlockBreak(actor, block.getState());
|
|
|
|
|
}
|
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-08-03 01:04:38 +02:00
|
|
|
if (origin.getType() == Material.IRON_DOOR || BukkitUtils.isWoodenDoor(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-08-03 01:04:38 +02:00
|
|
|
if (doorBlock.getType() == Material.IRON_DOOR || BukkitUtils.isWoodenDoor(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
|
2018-08-21 06:17:24 +02:00
|
|
|
if (replacedWith == null) {
|
|
|
|
|
consumer.queueBlockBreak(actor, origin.getState());
|
|
|
|
|
} else {
|
|
|
|
|
consumer.queueBlockReplace(actor, origin.getState(), replacedWith);
|
|
|
|
|
}
|
2015-03-22 20:15:04 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|