Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
xNatsuri committed Dec 21, 2024
1 parent bbd3c19 commit 2a26e69
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
25 changes: 18 additions & 7 deletions server/item/crossbow.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package item
import (
"github.com/df-mc/dragonfly/server/item/potion"
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/sound"
"time"
_ "unsafe"
)
Expand All @@ -22,17 +23,22 @@ func (c Crossbow) Charge(releaser Releaser, tx *world.Tx, ctx *UseContext, durat
creative := releaser.GameMode().CreativeInventory()
held, left := releaser.HeldItems()

chargeDuration := time.Duration(1.25 * float64(time.Second))
chargeDuration, consume := time.Duration(1.25*float64(time.Second)), !creative
for _, enchant := range held.Enchantments() {
if q, ok := enchant.Type().(interface{ DurationReduction(int) time.Duration }); ok {
chargeDuration = min(chargeDuration, q.DurationReduction(enchant.Level()))
}
if i, ok := enchant.Type().(interface{ ConsumesArrows() bool }); ok && !i.ConsumesArrows() {
consume = false
}
}

if duration >= chargeDuration {
var projectileItem Stack
if !left.Empty() {
if _, isFirework := left.Item().(Firework); isFirework {
_, isFirework := left.Item().(Firework)
_, isArrow := left.Item().(Arrow)
if isFirework || isArrow {
projectileItem = left
}
}
Expand All @@ -53,13 +59,14 @@ func (c Crossbow) Charge(releaser Releaser, tx *world.Tx, ctx *UseContext, durat
}
}

c.Item = projectileItem.Grow(-projectileItem.Count() + 1)
if !creative {
ctx.Consume(c.Item)
c.Item = NewStack(projectileItem.Item(), 1)
if consume {
ctx.Consume(projectileItem.Grow(-projectileItem.Count() + 1))
}

crossbow := newCrossbowWith(held, c)
releaser.SetHeldItems(crossbow, left)
releaser.PlaySound(sound.CrossbowLoadingMiddle{})
return true
}
return false
Expand All @@ -79,10 +86,12 @@ func (c Crossbow) ReleaseCharge(releaser Releaser, tx *world.Tx, ctx *UseContext
createFirework := tx.World().EntityRegistry().Config().Firework
fireworkEntity := createFirework(world.EntitySpawnOpts{
Position: torsoPosition(releaser),
Velocity: dirVec.Mul(1.5),
Velocity: dirVec.Mul(0.5),
Rotation: rot,
}, firework, releaser, false)
tx.AddEntity(fireworkEntity)

ctx.DamageItem(3)
} else {
createArrow := tx.World().EntityRegistry().Config().Arrow
arrow := createArrow(world.EntitySpawnOpts{
Expand All @@ -91,13 +100,15 @@ func (c Crossbow) ReleaseCharge(releaser Releaser, tx *world.Tx, ctx *UseContext
Rotation: rot,
}, 9, releaser, true, false, !creative, 0, potion.Potion{})
tx.AddEntity(arrow)

ctx.DamageItem(1)
}

ctx.DamageItem(1)
c.Item = Stack{}
held, left := releaser.HeldItems()
crossbow := newCrossbowWith(held, c)
releaser.SetHeldItems(crossbow, left)
releaser.PlaySound(sound.CrossbowShoot{})
return true
}

Expand Down
3 changes: 2 additions & 1 deletion server/item/enchantment/infinity.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ func (infinity) CompatibleWithEnchantment(t item.EnchantmentType) bool {
// CompatibleWithItem ...
func (infinity) CompatibleWithItem(i world.Item) bool {
_, ok := i.(item.Bow)
return ok
_, ok2 := i.(item.Crossbow)
return ok || ok2
}
2 changes: 2 additions & 0 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,8 @@ func (p *Player) UseItem() {
if !p.usingItem {
if !chargeable.ReleaseCharge(p, p.tx, p.useContext()) {
// If the item was not charged yet, start charging.
p.PlaySound(sound.CrossbowLoadingStart{})

p.usingSince, p.usingItem = time.Now(), true
p.updateState()
}
Expand Down
2 changes: 0 additions & 2 deletions server/session/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,8 +718,6 @@ func (s *Session) playSound(pos mgl64.Vec3, t world.Sound, disableRelative bool)
pk.SoundType = packet.SoundEventCrossbowLoadingStart
case sound.CrossbowLoadingMiddle:
pk.SoundType = packet.SoundEventCrossbowLoadingMiddle
case sound.CrossbowLoadingEnd:
pk.SoundType = packet.SoundEventCrossbowLoadingEnd
case sound.ArrowHit:
pk.SoundType = packet.SoundEventBowHit
case sound.ItemThrow:
Expand Down
5 changes: 1 addition & 4 deletions server/world/sound/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,9 @@ type CrossbowShoot struct{ sound }
// CrossbowLoadingStart is a sound played when a crossbow is starting to load.
type CrossbowLoadingStart struct{ sound }

// CrossbowLoadingMiddle is a sound played when a crossbow is in the middle of load.
// CrossbowLoadingMiddle is a sound played while a crossbow is loading and when a crossbow stops loading.
type CrossbowLoadingMiddle struct{ sound }

// CrossbowLoadingEnd is a sound played when a crossbow is at the end of loading.
type CrossbowLoadingEnd struct{ sound }

// ArrowHit is a sound played when an arrow hits ground.
type ArrowHit struct{ sound }

Expand Down

0 comments on commit 2a26e69

Please sign in to comment.