Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/java/net/coreprotect/bukkit/BukkitAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.bukkit.entity.EntityType;
import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.Event;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerInteractEvent;
Expand Down Expand Up @@ -316,6 +318,17 @@ public boolean isWaxed(Sign sign) {
return false;
}

@Override
public boolean shouldLogExplosion(Event event){
return true;
}

@Override
public Material getExplodedBlock(BlockExplodeEvent event){
// accoding to the Bukkit docs this will always return air
return event.getBlock().getType();
}

@Override
public void setGlowing(Sign sign, boolean isFront, boolean isGlowing) {
// Base implementation does nothing
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/net/coreprotect/bukkit/BukkitInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.Event;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerInteractEvent;
Expand Down Expand Up @@ -413,6 +415,27 @@ public interface BukkitInterface {
*/
boolean isSignFront(SignChangeEvent event);



/**
* Checks whether an explosion event should be logged or not. (i.e. wind charge explosions)
*
* @param event
* The explosion event (Block or Entity ExplodeEvent)
* @return true if the explosion should affect blocks
*/
boolean shouldLogExplosion(Event event);


/**
* Gets the material of the block that exploded
*
* @param event
* The block explosion event
* @return the material of the block that caused the explosion
*/
Material getExplodedBlock(BlockExplodeEvent event);

// --------------------------------------------------------------------------
// Registry methods
// --------------------------------------------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/net/coreprotect/bukkit/Bukkit_v1_20.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.bukkit.block.Sign;
import org.bukkit.block.sign.Side;
import org.bukkit.entity.Arrow;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -297,4 +298,10 @@ else if (Boolean.FALSE.equals(hasBasePotionType)) {
return super.getArrowMeta(arrow, itemStack);
}
}

@Override
public Material getExplodedBlock(BlockExplodeEvent event){
// accoding to the Bukkit docs this will always return air
return event.getExplodedBlockState().getType();
}
}
19 changes: 19 additions & 0 deletions src/main/java/net/coreprotect/bukkit/Bukkit_v1_21.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
import java.util.Set;

import org.bukkit.Bukkit;
import org.bukkit.ExplosionResult;
import org.bukkit.Keyed;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Tag;
import org.bukkit.entity.EntityType;
import org.bukkit.event.Event;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.inventory.InventoryType;


import net.coreprotect.model.BlockGroup;

/**
Expand Down Expand Up @@ -218,4 +223,18 @@ public Set<Material> shelfMaterials() {

return SHELVES;
}

@Override
public boolean shouldLogExplosion(Event event){
ExplosionResult result = null;

if (event instanceof EntityExplodeEvent){
result = ((EntityExplodeEvent)event).getExplosionResult();
} else if (event instanceof BlockExplodeEvent){
result = ((BlockExplodeEvent)event).getExplosionResult();
}
return !(result == ExplosionResult.KEEP ||
result == ExplosionResult.TRIGGER_BLOCK
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,135 +29,142 @@

public final class BlockExplodeListener extends Queue implements Listener {

public static void processBlockExplode(String user, World world, List<Block> blockList) {
HashMap<Location, Block> blockMap = new HashMap<>();

for (Block block : blockList) {
blockMap.put(block.getLocation(), block);
}

if (Config.getConfig(world).NATURAL_BREAK) {
for (Entry<Location, Block> data : new HashMap<>(blockMap).entrySet()) {
Block block = data.getValue();
int x = block.getX();
int y = block.getY();
int z = block.getZ();

Location[] locationMap = new Location[5];
locationMap[0] = new Location(world, (x + 1), y, z);
locationMap[1] = new Location(world, (x - 1), y, z);
locationMap[2] = new Location(world, x, y, (z + 1));
locationMap[3] = new Location(world, x, y, (z - 1));
locationMap[4] = new Location(world, x, (y + 1), z);

int scanMin = 0;
int scanMax = 5;
while (scanMin < scanMax) {
Location location = locationMap[scanMin];
if (blockMap.get(location) == null) {
Block scanBlock = world.getBlockAt(location);
Material scanType = scanBlock.getType();
if (BlockGroup.TRACK_ANY.contains(scanType) || BlockGroup.TRACK_TOP.contains(scanType) || BlockGroup.TRACK_TOP_BOTTOM.contains(scanType) || BlockGroup.TRACK_BOTTOM.contains(scanType) || BlockGroup.TRACK_SIDE.contains(scanType)) {
blockMap.put(location, scanBlock);

// Properly log double blocks, such as doors
BlockData blockData = scanBlock.getBlockData();
if (blockData instanceof Bisected) {
Bisected bisected = (Bisected) blockData;
Location bisectLocation = location.clone();
if (bisected.getHalf() == Half.TOP) {
bisectLocation.setY(bisectLocation.getY() - 1);
}
else {
bisectLocation.setY(bisectLocation.getY() + 1);
}

int worldMaxHeight = world.getMaxHeight();
int worldMinHeight = BukkitAdapter.ADAPTER.getMinHeight(world);
if (bisectLocation.getBlockY() >= worldMinHeight && bisectLocation.getBlockY() < worldMaxHeight && blockMap.get(bisectLocation) == null) {
blockMap.put(bisectLocation, world.getBlockAt(bisectLocation));
}
}
}
else if (scanType.hasGravity() && Config.getConfig(world).BLOCK_MOVEMENT) {
// log the top-most sand/gravel block as being removed
int scanY = location.getBlockY() + 1;
boolean topFound = false;
while (!topFound) {
Block topBlock = world.getBlockAt(location.getBlockX(), scanY, location.getBlockZ());
Material topMaterial = topBlock.getType();
if (!topMaterial.hasGravity()) {
location = new Location(world, location.getBlockX(), (scanY - 1), location.getBlockZ());
topFound = true;

// log block attached to top as being removed
if (BlockGroup.TRACK_ANY.contains(topMaterial) || BlockGroup.TRACK_TOP.contains(topMaterial) || BlockGroup.TRACK_TOP_BOTTOM.contains(topMaterial)) {
blockMap.put(topBlock.getLocation(), topBlock);
}
}
scanY++;
}

Block gravityBlock = location.getBlock();
blockMap.put(location, gravityBlock);
Queue.queueBlockGravityValidate(user, location, gravityBlock, scanType, 0);
}
}
scanMin++;
}
}
}

for (Map.Entry<Location, Block> entry : blockMap.entrySet()) {
Block block = entry.getValue();
Material blockType = block.getType();
BlockState blockState = block.getState();
if (BukkitAdapter.ADAPTER.isSign(blockType) && Config.getConfig(world).SIGN_TEXT) {
try {
Location location = blockState.getLocation();
Sign sign = (Sign) blockState;
String line1 = PaperAdapter.ADAPTER.getLine(sign, 0);
String line2 = PaperAdapter.ADAPTER.getLine(sign, 1);
String line3 = PaperAdapter.ADAPTER.getLine(sign, 2);
String line4 = PaperAdapter.ADAPTER.getLine(sign, 3);
String line5 = PaperAdapter.ADAPTER.getLine(sign, 4);
String line6 = PaperAdapter.ADAPTER.getLine(sign, 5);
String line7 = PaperAdapter.ADAPTER.getLine(sign, 6);
String line8 = PaperAdapter.ADAPTER.getLine(sign, 7);

boolean isFront = true;
int color = BukkitAdapter.ADAPTER.getColor(sign, isFront);
int colorSecondary = BukkitAdapter.ADAPTER.getColor(sign, !isFront);
boolean frontGlowing = BukkitAdapter.ADAPTER.isGlowing(sign, isFront);
boolean backGlowing = BukkitAdapter.ADAPTER.isGlowing(sign, !isFront);
boolean isWaxed = BukkitAdapter.ADAPTER.isWaxed(sign);

Queue.queueSignText(user, location, 0, color, colorSecondary, frontGlowing, backGlowing, isWaxed, isFront, line1, line2, line3, line4, line5, line6, line7, line8, 5);
}
catch (Exception e) {
e.printStackTrace();
}
}

Database.containerBreakCheck(user, blockType, block, null, block.getLocation());
Queue.queueBlockBreak(user, blockState, blockType, blockState.getBlockData().getAsString(), 0);
}
}

Check notice on line 146 in src/main/java/net/coreprotect/listener/block/BlockExplodeListener.java

View check run for this annotation

codefactor.io / CodeFactor

src/main/java/net/coreprotect/listener/block/BlockExplodeListener.java#L32-L146

Complex Method
@EventHandler(priority = EventPriority.MONITOR)
protected void onBlockExplode(BlockExplodeEvent event) {
Block eventBlock = event.getBlock();
World world = eventBlock.getLocation().getWorld();
String user = "";
if (!eventBlock.getType().equals(Material.AIR) && !eventBlock.getType().equals(Material.CAVE_AIR)) {
user = eventBlock.getType().name().toLowerCase(Locale.ROOT);
}
if (user.contains("tnt")) {
user = "#tnt";
Material eventMaterial = BukkitAdapter.ADAPTER.getExplodedBlock(event);
World world = event.getBlock().getLocation().getWorld();

if (!BukkitAdapter.ADAPTER.shouldLogExplosion(event)){
return;
}
else if (user.contains("end_crystal")) {
user = "#end_crystal";

String user = "";
if (!eventMaterial.equals(Material.AIR) && !eventMaterial.equals(Material.CAVE_AIR)) {
user = eventMaterial.name().toLowerCase(Locale.ROOT);

if (user.contains("respawn_anchor")) {
user = "#respawn_anchor";
}
else if (user.contains("_bed")) {
user = "#bed";
}
}

if (!user.startsWith("#")) {
user = "#explosion";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.coreprotect.listener.entity;

import org.bukkit.ExplosionResult;
import org.bukkit.World;
import org.bukkit.entity.Creeper;
import org.bukkit.entity.EnderCrystal;
Expand All @@ -15,55 +16,57 @@
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityExplodeEvent;

import net.coreprotect.bukkit.BukkitAdapter;
import net.coreprotect.config.Config;
import net.coreprotect.consumer.Queue;
import net.coreprotect.listener.block.BlockExplodeListener;

public final class EntityExplodeListener extends Queue implements Listener {

@EventHandler(priority = EventPriority.MONITOR)
protected void onEntityExplode(EntityExplodeEvent event) {
Entity entity = event.getEntity();
if (entity.getType().name().equals("WIND_CHARGE") || entity.getType().name().equals("BREEZE_WIND_CHARGE")) {

if (!BukkitAdapter.ADAPTER.shouldLogExplosion(event)){
return;
}

World world = event.getLocation().getWorld();
String user = "#explosion";

if (entity instanceof TNTPrimed) {
user = "#tnt";
}
else if (entity instanceof Minecart) {
String name = entity.getType().name();
if (name.contains("TNT")) {
user = "#tnt";
}
}
else if (entity instanceof Creeper) {
user = "#creeper";
}
else if (entity instanceof EnderDragon || entity instanceof EnderDragonPart) {
user = "#enderdragon";
}
else if (entity instanceof Wither || entity instanceof WitherSkull) {
user = "#wither";
}
else if (entity instanceof EnderCrystal) {
user = "#end_crystal";
}

boolean log = false;
if (Config.getConfig(world).EXPLOSIONS) {
log = true;
}

if ((user.equals("#enderdragon") || user.equals("#wither")) && !Config.getConfig(world).ENTITY_CHANGE) {
log = false;
}

if (!event.isCancelled() && log) {
BlockExplodeListener.processBlockExplode(user, world, event.blockList());
}
}

Check notice on line 71 in src/main/java/net/coreprotect/listener/entity/EntityExplodeListener.java

View check run for this annotation

codefactor.io / CodeFactor

src/main/java/net/coreprotect/listener/entity/EntityExplodeListener.java#L26-L71

Complex Method
}
Loading