Skip to content

Commit

Permalink
block/decorated_pot.go: Add animation/sound/particles
Browse files Browse the repository at this point in the history
  • Loading branch information
DaPigGuy committed Jan 7, 2025
1 parent cbf4bf8 commit e113782
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
8 changes: 8 additions & 0 deletions server/block/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ type ContinueCrackAction struct {
// StopCrackAction is a world.BlockAction to make the cracks forming in a block stop and disappear.
type StopCrackAction struct{ action }

// DecoratedPotWobbleAction is a world.BlockAction to make a decorated pot wobble when interacted with.
type DecoratedPotWobbleAction struct {
action
DecoratedPot DecoratedPot
// Success is whether an item was successfully inserted into the decorated pot.
Success bool
}

// action implements the Action interface. Structures in this package may embed it to gets its functionality
// out of the box.
type action struct{}
Expand Down
18 changes: 18 additions & 0 deletions server/block/decorated_pot.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/df-mc/dragonfly/server/internal/nbtconv"
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/particle"
"github.com/df-mc/dragonfly/server/world/sound"
"github.com/go-gl/mathgl/mgl64"
)

Expand Down Expand Up @@ -66,10 +68,25 @@ func (p DecoratedPot) InsertItem(h Hopper, pos cube.Pos, tx *world.Tx) bool {
return false
}

// wobble ...
func (p DecoratedPot) wobble(pos cube.Pos, tx *world.Tx, success bool) {
for _, v := range tx.Viewers(pos.Vec3Centre()) {
v.ViewBlockAction(pos, DecoratedPotWobbleAction{DecoratedPot: p, Success: success})
}

if success {
tx.AddParticle(pos.Vec3Middle().Add(mgl64.Vec3{0, 1.2}), particle.DustPlume{})
tx.PlaySound(pos.Vec3Centre(), sound.DecoratedPotInserted{Progress: float64(p.Item.Count()) / float64(p.Item.MaxCount())})
} else {
tx.PlaySound(pos.Vec3Centre(), sound.DecoratedPotInsertFailed{})
}
}

// Activate ...
func (p DecoratedPot) Activate(pos cube.Pos, _ cube.Face, tx *world.Tx, u item.User, ctx *item.UseContext) bool {
held, _ := u.HeldItems()
if held.Empty() || !p.Item.Comparable(held) || p.Item.Count() == p.Item.MaxCount() {
p.wobble(pos, tx, false)
return false
}

Expand All @@ -79,6 +96,7 @@ func (p DecoratedPot) Activate(pos cube.Pos, _ cube.Face, tx *world.Tx, u item.U
p.Item = p.Item.Grow(1)
}
tx.SetBlock(pos, p, nil)
p.wobble(pos, tx, true)
ctx.SubtractFromCount(1)
return true
}
Expand Down
23 changes: 23 additions & 0 deletions server/session/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,11 @@ func (s *Session) ViewParticle(pos mgl64.Vec3, p world.Particle) {
EventType: packet.LevelEventParticleLegacyEvent | 10,
Position: vec64To32(pos),
})
case particle.DustPlume:
s.writePacket(&packet.LevelEvent{
EventType: packet.LevelEventParticleLegacyEvent | 88,
Position: vec64To32(pos),
})
}
}

Expand Down Expand Up @@ -810,6 +815,16 @@ func (s *Session) playSound(pos mgl64.Vec3, t world.Sound, disableRelative bool)
EventType: packet.LevelEventSoundTotemUsed,
Position: vec64To32(pos),
})
case sound.DecoratedPotInserted:
s.writePacket(&packet.PlaySound{
SoundName: "block.decorated_pot.insert",
Position: vec64To32(pos),
Volume: 1,
Pitch: 0.7 + 0.5*float32(so.Progress),
})
return
case sound.DecoratedPotInsertFailed:
pk.SoundType = packet.SoundEventDecoratedPotInsertFail
}
s.writePacket(pk)
}
Expand Down Expand Up @@ -1152,6 +1167,14 @@ func (s *Session) ViewBlockAction(pos cube.Pos, a world.BlockAction) {
Position: vec64To32(pos.Vec3()),
EventData: int32(65535 / (t.BreakTime.Seconds() * 20)),
})
case block.DecoratedPotWobbleAction:
nbt := t.DecoratedPot.EncodeNBT()
nbt["x"], nbt["y"], nbt["z"] = blockPos.X(), blockPos.Y(), blockPos.Z()
nbt["animation"] = boolByte(t.Success) + 1
s.writePacket(&packet.BlockActorData{
Position: blockPos,
NBTData: nbt,
})
}
}

Expand Down
3 changes: 3 additions & 0 deletions server/world/particle/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ type LavaDrip struct{ particle }
// Lava is a particle that shows up randomly above lava.
type Lava struct{ particle }

// DustPlume is a particle that shows up when an item is successfully inserted into a decorated pot.
type DustPlume struct{ particle }

// particle serves as a base for all particles in this package.
type particle struct{}

Expand Down
10 changes: 10 additions & 0 deletions server/world/sound/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,16 @@ type WaxRemoved struct{ sound }
// CopperScraped is a sound played when a player scrapes a copper block to reduce its oxidation level.
type CopperScraped struct{ sound }

// DecoratedPotInserted is a sound played when an item is successfully inserted into a decorated pot.
type DecoratedPotInserted struct {
sound
// Progress is how much of the decorated pot has been filled.
Progress float64
}

// DecoratedPotInsertFailed is a sound played when an item fails to be inserted into a decorated pot.
type DecoratedPotInsertFailed struct{ sound }

// sound implements the world.Sound interface.
type sound struct{}

Expand Down

0 comments on commit e113782

Please sign in to comment.