Skip to content

Commit

Permalink
feat: improved tnt (#399)
Browse files Browse the repository at this point in the history
Adapted code from PowerNukkitX with a few fixes.
  • Loading branch information
KoshakMineDEV authored Feb 17, 2025
1 parent b27aef6 commit 6a1a23d
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 95 deletions.
45 changes: 45 additions & 0 deletions src/main/java/cn/nukkit/block/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,10 @@ public boolean isSolid() {
return true;
}

public boolean isSolid(BlockFace side) {
return isSideFull(side);
}

public boolean diffusesSkyLight() {
return false;
}
Expand Down Expand Up @@ -1161,6 +1165,47 @@ public MovingObjectPosition calculateIntercept(Vector3 pos1, Vector3 pos2) {
return MovingObjectPosition.fromBlock((int) this.x, (int) this.y, (int) this.z, f, vector.add(this.x, this.y, this.z));
}

public boolean isSideFull(BlockFace face) {
AxisAlignedBB boundingBox = getBoundingBox();
if (boundingBox == null) {
return false;
}

if (face.getAxis().getPlane() == BlockFace.Plane.HORIZONTAL) {
if (boundingBox.getMinY() != getY() || boundingBox.getMaxY() != getY() + 1) {
return false;
}
int offset = face.getXOffset();
if (offset < 0) {
return boundingBox.getMinX() == getX()
&& boundingBox.getMinZ() == getZ() && boundingBox.getMaxZ() == getZ() + 1;
} else if (offset > 0) {
return boundingBox.getMaxX() == getX() + 1
&& boundingBox.getMaxZ() == getZ() + 1 && boundingBox.getMinZ() == getZ();
}

offset = face.getZOffset();
if (offset < 0) {
return boundingBox.getMinZ() == getZ()
&& boundingBox.getMinX() == getX() && boundingBox.getMaxX() == getX() + 1;
}

return boundingBox.getMaxZ() == getZ() + 1
&& boundingBox.getMaxX() == getX() + 1 && boundingBox.getMinX() == getX();
}

if (boundingBox.getMinX() != getX() || boundingBox.getMaxX() != getX() + 1 ||
boundingBox.getMinZ() != getZ() || boundingBox.getMaxZ() != getZ() + 1) {
return false;
}

if (face.getYOffset() < 0) {
return boundingBox.getMinY() == getY();
}

return boundingBox.getMaxY() == getY() + 1;
}

public String getSaveId() {
String name = getClass().getName();
return name.substring(16);
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/cn/nukkit/entity/item/EntityEndCrystal.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import cn.nukkit.event.entity.EntityExplosionPrimeEvent;
import cn.nukkit.level.Explosion;
import cn.nukkit.level.GameRule;
import cn.nukkit.level.Position;
import cn.nukkit.level.format.FullChunk;
import cn.nukkit.nbt.tag.CompoundTag;

Expand Down Expand Up @@ -92,7 +93,7 @@ public void setShowBase(boolean value) {
@Override
public void explode() {
this.close();
if (!this.detonated && this.level.getGameRules().getBoolean(GameRule.MOB_GRIEFING)) {
if (!this.detonated) {
this.detonated = true;

EntityExplosionPrimeEvent ev = new EntityExplosionPrimeEvent(this, 6);
Expand All @@ -101,18 +102,17 @@ public void explode() {
return;
}

Explosion explode = new Explosion(this.add(0, this.getHeight() / 2, 0), (float) ev.getForce(), this);
Position pos = this.getPosition();
Explosion explode = new Explosion(pos, 6, this);

int floor = this.getFloorY();
int down = this.level.getBlockIdAt(this.chunk, this.getFloorX(), floor - 1, this.getFloorZ());
if (down == Block.BEDROCK || down == Block.OBSIDIAN) {
explode.setMinHeight(floor);
}
this.close();

if (ev.isBlockBreaking()) {
if (this.level.getGameRules().getBoolean(GameRule.MOB_GRIEFING)) {
explode.explodeA();
explode.explodeB();
} else {
explode.explodeB();
}
explode.explodeB();
}
}

Expand Down
26 changes: 22 additions & 4 deletions src/main/java/cn/nukkit/event/block/BlockExplodeEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cn.nukkit.level.Position;

import java.util.List;
import java.util.Set;

public class BlockExplodeEvent extends BlockEvent implements Cancellable {

Expand All @@ -16,8 +17,11 @@ public static HandlerList getHandlers() {
}

protected final Position position;
protected List<Block> blocks;
protected final double fireChance;

protected double yield;
protected Set<Block> blocks;
protected Set<Block> ignitions;

/**
* Block explode event is called when a block explodes (For example a bed in nether)
Expand All @@ -26,22 +30,24 @@ public static HandlerList getHandlers() {
* @param blocks Blocks affected by the explosion
* @param yield Explosion yield
*/
public BlockExplodeEvent(Block block, Position position, List<Block> blocks, double yield) {
public BlockExplodeEvent(Block block, Position position, Set<Block> blocks, Set<Block> ignitions, double yield, double fireChance) {
super(block);
this.position = position;
this.blocks = blocks;
this.yield = yield;
this.ignitions = ignitions;
this.fireChance = fireChance;
}

public Position getPosition() {
return this.position;
}

public List<Block> getBlockList() {
public Set<Block> getAffectedBlocks() {
return this.blocks;
}

public void setBlockList(List<Block> blocks) {
public void setAffectedBlocks(Set<Block> blocks) {
this.blocks = blocks;
}

Expand All @@ -52,4 +58,16 @@ public double getYield() {
public void setYield(double yield) {
this.yield = yield;
}

public Set<Block> getIgnitions() {
return ignitions;
}

public void setIgnitions(Set<Block> ignitions) {
this.ignitions = ignitions;
}

public double getFireChance() {
return fireChance;
}
}
11 changes: 11 additions & 0 deletions src/main/java/cn/nukkit/event/entity/EntityExplodeEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import cn.nukkit.level.Position;

import java.util.List;
import java.util.Set;

/**
* @author Angelic47
Expand All @@ -22,6 +23,8 @@ public static HandlerList getHandlers() {

protected final Position position;
protected List<Block> blocks;

protected Set<Block> ignitions;
protected double yield;

public EntityExplodeEvent(Entity entity, Position position, List<Block> blocks, double yield) {
Expand Down Expand Up @@ -50,4 +53,12 @@ public double getYield() {
public void setYield(double yield) {
this.yield = yield;
}

public Set<Block> getIgnitions() {
return ignitions;
}

public void setIgnitions(Set<Block> ignitions) {
this.ignitions = ignitions;
}
}
Loading

0 comments on commit 6a1a23d

Please sign in to comment.