Skip to content

Commit

Permalink
Log respawn anchor explosions
Browse files Browse the repository at this point in the history
  • Loading branch information
Brokkonaut committed Nov 3, 2022
1 parent 4faced9 commit a395f20
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/main/java/de/diddiz/LogBlock/Logging.java
Expand Up @@ -51,6 +51,7 @@ public enum Logging {
OXIDIZATION,
SCULKSPREAD(true),
SHULKER_BOX_CONTENT,
RESPAWNANCHOREXPLOSION(true),
PLAYER_COMMANDS,
COMMANDBLOCK_COMMANDS,
CONSOLE_COMMANDS;
Expand Down
74 changes: 62 additions & 12 deletions src/main/java/de/diddiz/LogBlock/listeners/ExplosionLogging.java
Expand Up @@ -10,6 +10,7 @@
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.data.type.RespawnAnchor;
import org.bukkit.entity.*;
import org.bukkit.entity.minecart.ExplosiveMinecart;
import org.bukkit.event.EventHandler;
Expand All @@ -18,6 +19,7 @@
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.projectiles.ProjectileSource;
import org.bukkit.scheduler.BukkitRunnable;

Expand All @@ -31,6 +33,8 @@ public class ExplosionLogging extends LoggingListener {

private UUID lastBedInteractionPlayer;
private Location lastBedInteractionLocation;
private UUID lastRespawnAnchorInteractionPlayer;
private Location lastRespawnAnchorInteractionLocation;

public ExplosionLogging(LogBlock lb) {
super(lb);
Expand Down Expand Up @@ -124,20 +128,48 @@ public void onEntityExplode(EntityExplodeEvent event) {

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.hasBlock() && BukkitUtils.isBed(event.getClickedBlock().getType())) {
if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.hasBlock()) {
Block block = event.getClickedBlock();
if (!Config.isLogging(block.getWorld(), Logging.BEDEXPLOSION)) {
return;
if (BukkitUtils.isBed(block.getType()) && !block.getWorld().isBedWorks()) {
if (!Config.isLogging(block.getWorld(), Logging.BEDEXPLOSION)) {
return;
}
lastBedInteractionPlayer = event.getPlayer().getUniqueId();
lastBedInteractionLocation = block.getLocation();
new BukkitRunnable() {
@Override
public void run() {
lastBedInteractionPlayer = null;
lastBedInteractionLocation = null;
}
}.runTask(LogBlock.getInstance());
} else if (block.getType() == Material.RESPAWN_ANCHOR && block.getBlockData() instanceof RespawnAnchor data) {
if (!Config.isLogging(block.getWorld(), Logging.RESPAWNANCHOREXPLOSION)) {
return;
}
ItemStack inHand = event.getItem();
int charges = data.getCharges();
if (charges < data.getMaximumCharges() && inHand != null && inHand.getType() == Material.GLOWSTONE) {
// charge
Actor actor = Actor.actorFromEntity(event.getPlayer());
RespawnAnchor blockNew = (RespawnAnchor) data.clone();
blockNew.setCharges(charges + 1);
consumer.queueBlockReplace(actor, block.getState(), blockNew);
} else if (charges > 0 && !block.getWorld().isRespawnAnchorWorks()) {
// explode
Actor actor = Actor.actorFromEntity(event.getPlayer());
consumer.queueBlockBreak(actor, block.getState());
lastRespawnAnchorInteractionPlayer = event.getPlayer().getUniqueId();
lastRespawnAnchorInteractionLocation = block.getLocation();
new BukkitRunnable() {
@Override
public void run() {
lastRespawnAnchorInteractionPlayer = null;
lastRespawnAnchorInteractionLocation = null;
}
}.runTask(LogBlock.getInstance());
}
}
lastBedInteractionPlayer = event.getPlayer().getUniqueId();
lastBedInteractionLocation = block.getLocation();
new BukkitRunnable() {
@Override
public void run() {
lastBedInteractionPlayer = null;
lastBedInteractionLocation = null;
}
}.runTask(LogBlock.getInstance());
}
}

Expand All @@ -150,6 +182,13 @@ public void onBlockExplode(BlockExplodeEvent event) {
bedCause = Bukkit.getPlayer(lastBedInteractionPlayer);
}
}
Player respawnAnchorCause = null;
if (lastRespawnAnchorInteractionPlayer != null && lastRespawnAnchorInteractionLocation != null) {
Location block = event.getBlock().getLocation();
if (lastRespawnAnchorInteractionLocation.equals(block)) {
respawnAnchorCause = Bukkit.getPlayer(lastRespawnAnchorInteractionPlayer);
}
}

for (final Block block : event.blockList()) {
final WorldConfig wcfg = getWorldConfig(block.getLocation().getWorld());
Expand All @@ -162,6 +201,17 @@ public void onBlockExplode(BlockExplodeEvent event) {
}
if (Config.logBedExplosionsAsPlayerWhoTriggeredThese) {
actor = Actor.actorFromEntity(bedCause);
} else {
actor = new Actor("BedExplosion");
}
} else if (respawnAnchorCause != null) {
if (!wcfg.isLogging(Logging.RESPAWNANCHOREXPLOSION)) {
return;
}
if (Config.logBedExplosionsAsPlayerWhoTriggeredThese) {
actor = Actor.actorFromEntity(respawnAnchorCause);
} else {
actor = new Actor("RespawnAnchorExplosion");
}
} else if (!wcfg.isLogging(Logging.MISCEXPLOSION)) {
return;
Expand Down

0 comments on commit a395f20

Please sign in to comment.