Files
LogBlock/src/main/java/de/diddiz/util/LoggingUtil.java
T

211 lines
9.1 KiB
Java
Raw Normal View History

package de.diddiz.util;
import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.Consumer;
import de.diddiz.LogBlock.Logging;
import de.diddiz.LogBlock.config.WorldConfig;
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;
import org.bukkit.block.Sign;
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;
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) {
2015-03-22 20:15:04 +05:00
WorldConfig wcfg = getWorldConfig(origin.getWorld());
if (wcfg == null) {
return;
}
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());
while (checkBlock.getType().hasGravity()) {
2015-03-22 20:15:04 +05:00
// Record this block as falling
consumer.queueBlockBreak(actor, checkBlock.getState());
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())) {
consumer.queueBlockPlace(actor, finalLoc, checkBlock.getBlockData());
2015-03-22 20:15:04 +05:00
} else {
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);
}
}
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) {
2015-03-22 20:15:04 +05:00
WorldConfig wcfg = getWorldConfig(origin.getWorld());
if (wcfg == null) {
return;
}
2015-03-22 20:15:04 +05:00
Block checkBlock = origin.getRelative(BlockFace.UP);
if (BukkitUtils.getRelativeTopBreakabls().contains(checkBlock.getType())) {
if (wcfg.isLogging(Logging.SIGNTEXT) && checkBlock.getType() == Material.SIGN) {
2015-03-22 20:15:04 +05:00
consumer.queueSignBreak(actor, (Sign) checkBlock.getState());
2018-08-03 01:04:38 +02:00
} else if (checkBlock.getType() == Material.IRON_DOOR || BukkitUtils.isWoodenDoor(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.
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());
}
} 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.
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
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());
}
}
2015-03-22 20:15:04 +05:00
List<Location> relativeBreakables = BukkitUtils.getBlocksNearby(origin, BukkitUtils.getRelativeBreakables());
if (relativeBreakables.size() != 0) {
for (Location location : relativeBreakables) {
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
}
}
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?
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());
}
} else if (BukkitUtils.isDoublePlant(origin.getType())) { // Special double plant check
2015-03-22 20:15:04 +05:00
Block plantBlock = origin;
2015-03-22 20:15:04 +05:00
// Up or down?
if (!BukkitUtils.isTop(origin.getBlockData())) {
2015-03-22 20:15:04 +05:00
plantBlock = plantBlock.getRelative(BlockFace.UP);
} else {
plantBlock = plantBlock.getRelative(BlockFace.DOWN);
}
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]", "?");
}
}