Files
LogBlock/src/main/java/de/diddiz/worldedit/WorldEditLoggingHook.java
T

121 lines
4.7 KiB
Java
Raw Normal View History

2014-04-03 23:22:26 -04:00
package de.diddiz.worldedit;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEdit;
2018-07-21 15:51:16 +02:00
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.BukkitWorld;
2014-04-03 23:22:26 -04:00
import com.sk89q.worldedit.event.extent.EditSessionEvent;
import com.sk89q.worldedit.extension.platform.Actor;
2018-07-21 15:51:16 +02:00
import com.sk89q.worldedit.extent.AbstractDelegateExtent;
2014-04-03 23:22:26 -04:00
import com.sk89q.worldedit.util.eventbus.Subscribe;
2018-07-21 15:51:16 +02:00
import com.sk89q.worldedit.world.block.BlockStateHolder;
2014-04-03 23:22:26 -04:00
import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging;
2018-07-31 02:19:40 +02:00
import de.diddiz.LogBlock.blockstate.BlockStateCodecs;
2014-04-03 23:22:26 -04:00
import de.diddiz.LogBlock.config.Config;
2018-07-27 17:19:15 +02:00
import de.diddiz.util.BukkitUtils;
2014-04-03 23:22:26 -04:00
import org.bukkit.Bukkit;
2018-07-21 15:51:16 +02:00
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
2018-07-21 15:51:16 +02:00
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
2014-04-03 23:22:26 -04:00
2015-03-22 20:15:04 +05:00
import java.util.logging.Level;
//...so they ALSO have a class called Actor... need to fully-qualify when we use ours
2014-04-03 23:22:26 -04:00
public class WorldEditLoggingHook {
2015-03-22 20:15:04 +05:00
private LogBlock plugin;
2014-04-03 23:22:26 -04:00
2015-03-22 20:15:04 +05:00
public WorldEditLoggingHook(LogBlock plugin) {
this.plugin = plugin;
}
2014-04-03 23:22:26 -04:00
2015-03-22 20:15:04 +05:00
// Convert WE Actor to LB Actor
private de.diddiz.LogBlock.Actor AtoA(Actor weActor) {
if (weActor.isPlayer()) {
return new de.diddiz.LogBlock.Actor(weActor.getName(), weActor.getUniqueId());
}
return new de.diddiz.LogBlock.Actor(weActor.getName());
}
2015-03-22 20:15:04 +05:00
private World adapt(com.sk89q.worldedit.world.World weWorld) {
if (weWorld == null) {
throw new NullPointerException("[Logblock-Worldedit] The provided world was null.");
}
if (weWorld instanceof BukkitWorld) {
return ((BukkitWorld) weWorld).getWorld();
}
World world = Bukkit.getServer().getWorld(weWorld.getName());
if (world == null) {
throw new IllegalArgumentException("Can't find a Bukkit world for " + weWorld);
}
return world;
}
2015-03-22 20:15:04 +05:00
public void hook() {
WorldEdit.getInstance().getEventBus().register(new Object() {
@Subscribe
public void wrapForLogging(final EditSessionEvent event) {
final Actor actor = event.getActor();
if (actor == null) {
return;
}
final de.diddiz.LogBlock.Actor lbActor = AtoA(actor);
2014-04-03 23:22:26 -04:00
2015-03-22 20:15:04 +05:00
// Check to ensure the world should be logged
final World world;
final com.sk89q.worldedit.world.World k = event.getWorld();
try {
world = adapt(k);
} catch (RuntimeException ex) {
plugin.getLogger().warning("Failed to register logging for WorldEdit!");
plugin.getLogger().log(Level.WARNING, ex.getMessage(), ex);
return;
}
2015-03-22 20:15:04 +05:00
// If config becomes reloadable, this check should be moved
if (!(Config.isLogging(world, Logging.WORLDEDIT))) {
return;
}
2014-04-03 23:22:26 -04:00
2018-07-21 15:51:16 +02:00
event.setExtent(new AbstractDelegateExtent(event.getExtent()) {
2015-03-22 20:15:04 +05:00
@Override
2018-07-21 15:51:16 +02:00
public final boolean setBlock(Vector position, @SuppressWarnings("rawtypes") BlockStateHolder block) throws WorldEditException {
onBlockChange(position, block);
return super.setBlock(position, block);
}
protected void onBlockChange(Vector pt, BlockStateHolder<?> block) {
2014-04-03 23:22:26 -04:00
2015-03-22 20:15:04 +05:00
if (event.getStage() != EditSession.Stage.BEFORE_CHANGE) {
return;
}
2014-04-03 23:22:26 -04:00
2018-07-21 15:51:16 +02:00
Location location = new Location(world, pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
Block origin = location.getBlock();
Material typeBefore = origin.getType();
// Check to see if we've broken a sign
2018-07-31 02:19:40 +02:00
if (BlockStateCodecs.hasCodec(typeBefore)) {
plugin.getConsumer().queueBlockBreak(lbActor, origin.getState());
2018-07-21 15:51:16 +02:00
} else if (!origin.isEmpty()) {
plugin.getConsumer().queueBlockBreak(lbActor, location, origin.getBlockData());
}
BlockData newBlock = BukkitAdapter.adapt(block);
2018-07-27 17:19:15 +02:00
if (newBlock != null && !BukkitUtils.isEmpty(newBlock.getMaterial())) {
2018-07-21 15:51:16 +02:00
plugin.getConsumer().queueBlockPlace(lbActor, location, newBlock);
}
2015-03-22 20:15:04 +05:00
}
});
}
});
}
2014-04-03 23:22:26 -04:00
}