Skip to content

Commit

Permalink
entity/tnt.go: Drop all items in TNT explosions.
Browse files Browse the repository at this point in the history
Resolves #969.
  • Loading branch information
Sandertv committed Dec 23, 2024
1 parent 0c82bfe commit 386cf86
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
12 changes: 8 additions & 4 deletions server/block/explosion.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ type ExplosionConfig struct {
// SpawnFire will cause the explosion to randomly start fires in 1/3 of all destroyed air blocks that are
// above opaque blocks.
SpawnFire bool
// DisableItemDrops, when set to true, will prevent any item entities from dropping as a result of blocks being
// destroyed.
DisableItemDrops bool
// ItemDropChance specifies how item drops should be handled. By default,
// the item drop chance is 1/Size. If negative, no items will be dropped by
// the explosion. If set to 1 or higher, all items are dropped.
ItemDropChance float64

// Sound is the sound to play when the explosion is created. If set to nil, this will default to the sound of a
// regular explosion.
Expand Down Expand Up @@ -79,6 +80,9 @@ func (c ExplosionConfig) Explode(tx *world.Tx, explosionPos mgl64.Vec3) {
if c.Size == 0 {
c.Size = 4
}
if c.ItemDropChance == 0 {
c.ItemDropChance = 1.0 / c.Size
}

r, d := rand.New(c.Rand), c.Size*2
box := cube.Box(
Expand Down Expand Up @@ -131,7 +135,7 @@ func (c ExplosionConfig) Explode(tx *world.Tx, explosionPos mgl64.Vec3) {
explodable.Explode(explosionPos, pos, tx, c)
} else if breakable, ok := bl.(Breakable); ok {
tx.SetBlock(pos, nil, nil)
if !c.DisableItemDrops && 1/c.Size > r.Float64() {
if c.ItemDropChance > r.Float64() {
for _, drop := range breakable.BreakInfo().Drops(item.ToolNone{}, nil) {
dropItem(tx, drop, pos.Vec3Centre())
}
Expand Down
3 changes: 1 addition & 2 deletions server/entity/tnt.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ var tntConf = PassiveBehaviourConfig{

// explodeTNT creates an explosion at the position of e.
func explodeTNT(e *Ent, tx *world.Tx) {
var config block.ExplosionConfig
config.Explode(tx, e.Position())
block.ExplosionConfig{ItemDropChance: 1}.Explode(tx, e.Position())
}

// TNTType is a world.EntityType implementation for TNT.
Expand Down

0 comments on commit 386cf86

Please sign in to comment.