Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thorns enchantment #553

Merged
merged 23 commits into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
fc8942c
e
HashimTheArab Jul 4, 2022
5db9322
e 2
HashimTheArab Jul 4, 2022
794e5b9
Merge branch 'df-mc:master' into master
HashimTheArab Jul 7, 2022
6e8538a
Merge branch 'df-mc:master' into master
HashimTheArab Jul 8, 2022
b58bd50
Merge https://github.com/df-mc/dragonfly
HashimTheArab Jul 11, 2022
0f0bfc1
pog
HashimTheArab Jul 11, 2022
90235e3
fix
HashimTheArab Jul 11, 2022
a51301c
forgot to add the thorns enchant!
HashimTheArab Jul 11, 2022
3fae4bc
fix import cycle
HashimTheArab Jul 11, 2022
6c57b86
Merge branch 'df-mc:master' into master
HashimTheArab Jul 11, 2022
530a19c
Merge branch 'master' into enchantment/thorns
HashimTheArab Jul 11, 2022
5acef99
Update player.go
HashimTheArab Jul 11, 2022
40b4cfb
Merge branch 'enchantment/thorns' of https://github.com/grapedevs/dra…
HashimTheArab Jul 11, 2022
893a78f
Update thorns.go
HashimTheArab Jul 11, 2022
0f99ddb
Update source.go
HashimTheArab Jul 11, 2022
91f302d
Merge branch 'df-mc:master' into master
HashimTheArab Jul 14, 2022
7ef4dc6
Update server/player/player.go
HashimTheArab Jul 15, 2022
6bdff3e
Update server/player/player.go
HashimTheArab Jul 15, 2022
99353e8
Merge branch 'df-mc:master' into master
HashimTheArab Jul 15, 2022
8fa670c
Merge branch 'master' of https://github.com/grapedevs/dragonfly into …
HashimTheArab Jul 15, 2022
8c88b9e
Merge branch 'enchantment/thorns' of https://github.com/grapedevs/dra…
HashimTheArab Jul 15, 2022
ec67700
fixes
HashimTheArab Jul 15, 2022
f11dddb
more stuff
HashimTheArab Jul 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion server/entity/damage/source.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package damage

import "github.com/df-mc/dragonfly/server/world"
import (
"github.com/df-mc/dragonfly/server/world"
)

type (
// Source represents the source of the damage dealt to an entity. This source may be passed to the Hurt()
Expand Down Expand Up @@ -68,6 +70,12 @@ type (
Projectile, Owner world.Entity
}

// SourceThorns is used for damage caused by thorns.
SourceThorns struct {
// Owner holds the entity wearing the thorns armour.
Owner world.Entity
}

// SourceBlock is used for damage caused by a block, such as an anvil.
SourceBlock struct {
// Block is the block that caused the damage.
Expand Down Expand Up @@ -103,5 +111,7 @@ func (SourceLava) ReducedByResistance() bool { return true }
func (SourceLava) ReducedByArmour() bool { return true }
func (SourceProjectile) ReducedByResistance() bool { return true }
func (SourceProjectile) ReducedByArmour() bool { return true }
func (SourceThorns) ReducedByResistance() bool { return true }
func (SourceThorns) ReducedByArmour() bool { return false }
func (SourceBlock) ReducedByResistance() bool { return true }
func (SourceBlock) ReducedByArmour() bool { return true }
2 changes: 1 addition & 1 deletion server/item/enchantment/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func init() {
item.RegisterEnchantment(2, FeatherFalling{})
// TODO: (3) Blast Protection.
item.RegisterEnchantment(4, ProjectileProtection{})
// TODO: (5) Thorns.
item.RegisterEnchantment(5, Thorns{})
item.RegisterEnchantment(6, Respiration{})
item.RegisterEnchantment(7, DepthStrider{})
item.RegisterEnchantment(8, AquaAffinity{})
Expand Down
35 changes: 35 additions & 0 deletions server/item/enchantment/thorns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package enchantment

import (
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/world"
)

// Thorns is an enchantment that inflicts damage on attackers.
type Thorns struct{}
HashimTheArab marked this conversation as resolved.
Show resolved Hide resolved

// Name ...
func (Thorns) Name() string {
return "Thorns"
}

// MaxLevel ...
func (Thorns) MaxLevel() int {
return 3
}

// Rarity ...
func (Thorns) Rarity() item.EnchantmentRarity {
return item.EnchantmentRarityVeryRare
}

// CompatibleWithEnchantment ...
func (Thorns) CompatibleWithEnchantment(item.EnchantmentType) bool {
return true
}

// CompatibleWithItem ...
func (Thorns) CompatibleWithItem(i world.Item) bool {
_, ok := i.(item.Armour)
return ok
}
28 changes: 28 additions & 0 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,11 +557,39 @@ func (p *Player) Hurt(dmg float64, source damage.Source) (float64, bool) {
p.Exhaust(0.1)

damageToArmour := int(math.Max(math.Floor(dmg/4), 1))
var damageToAttacker int
thornsArmour := map[int]item.Stack{}
for slot, it := range p.armour.Slots() {
if _, ok := it.Item().(item.Durable); ok {
if e, ok := it.Enchantment(enchantment.Thorns{}); ok && rand.Float64() < float64(e.Level())*0.15 {
damageToArmour++
thornsArmour[slot] = it
if e.Level() > 10 {
damageToAttacker += e.Level() - 10
} else {
damageToAttacker += 1 + rand.Intn(4)
}
}
_ = p.armour.Inventory().SetItem(slot, p.damageItem(it, damageToArmour))
}
}
// Damage a random thorns piece that the user is wearing.
for slot, item := range thornsArmour {
_ = p.armour.Inventory().SetItem(slot, p.damageItem(item, 2))

if damageToAttacker > 0 {
var attacker world.Entity
if s, ok := source.(damage.SourceEntityAttack); ok {
attacker = s.Attacker
} else if s, ok := source.(damage.SourceProjectile); ok {
attacker = s.Owner
}
if l, ok := attacker.(entity.Living); ok {
l.Hurt(float64(damageToAttacker), damage.SourceThorns{Owner: attacker})
}
}
break
}
HashimTheArab marked this conversation as resolved.
Show resolved Hide resolved
}

w, pos := p.World(), p.Position()
Expand Down