Skip to content

Commit

Permalink
enchantments/vanishing.go: implement Curse of Vanishing enchantment (#…
Browse files Browse the repository at this point in the history
…661)

Co-authored-by: DaPigGuy <mcpepig123@gmail.com>
  • Loading branch information
eminarican and DaPigGuy committed Aug 29, 2022
1 parent e73095c commit 00a8340
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 11 deletions.
2 changes: 1 addition & 1 deletion server/item/enchantment/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func init() {
// TODO: (25) Frost Walker.
item.RegisterEnchantment(26, Mending{})
// TODO: (27) Curse of Binding.
// TODO: (28) Curse of Vanishing.
item.RegisterEnchantment(28, CurseOfVanishing{})
// TODO: (29) Impaling.
// TODO: (30) Riptide.
// TODO: (31) Loyalty.
Expand Down
55 changes: 55 additions & 0 deletions server/item/enchantment/vanishing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package enchantment

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

// CurseOfVanishing is an enchantment that causes the item to disappear on death.
type CurseOfVanishing struct{}

// Name ...
func (CurseOfVanishing) Name() string {
return "Curse of Vanishing"
}

// MaxLevel ...
func (CurseOfVanishing) MaxLevel() int {
return 1
}

// Cost ...
func (CurseOfVanishing) Cost(int) (int, int) {
return 25, 50
}

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

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

// CompatibleWithItem ...
func (CurseOfVanishing) CompatibleWithItem(i world.Item) bool {
_, arm := i.(item.Armour)
_, com := i.(item.Compass)
_, dur := i.(item.Durable)
_, rec := i.(item.RecoveryCompass)
// TODO: Carrot on a Stick
// TODO: Warped Fungus on a Stick
return arm || com || dur || rec
}

// Treasure ...
func (CurseOfVanishing) Treasure() bool {
return true
}

// Curse ...
func (CurseOfVanishing) Curse() bool {
return true
}
16 changes: 10 additions & 6 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ package player

import (
"fmt"
"math"
"math/rand"
"net"
"strings"
"sync"
"time"

"github.com/df-mc/atomic"
"github.com/df-mc/dragonfly/server/block"
"github.com/df-mc/dragonfly/server/block/cube"
Expand Down Expand Up @@ -30,12 +37,6 @@ import (
"github.com/google/uuid"
"golang.org/x/exp/maps"
"golang.org/x/text/language"
"math"
"math/rand"
"net"
"strings"
"sync"
"time"
)

// Player is an implementation of a player entity. It has methods that implement the behaviour that players
Expand Down Expand Up @@ -918,6 +919,9 @@ func (p *Player) dropContents() {

p.session().EmptyUIInventory()
for _, it := range append(p.inv.Clear(), append(p.armour.Clear(), p.offHand.Clear()...)...) {
if _, ok := it.Enchantment(enchantment.CurseOfVanishing{}); ok {
continue
}
ent := entity.NewItem(it, pos)
ent.SetVelocity(mgl64.Vec3{rand.Float64()*0.2 - 0.1, 0.2, rand.Float64()*0.2 - 0.1})
w.AddEntity(ent)
Expand Down
18 changes: 14 additions & 4 deletions server/session/handler_grindstone.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package session

import (
"fmt"
"math"
"math/rand"

"github.com/df-mc/dragonfly/server/block"
"github.com/df-mc/dragonfly/server/entity"
"github.com/df-mc/dragonfly/server/item"
"github.com/go-gl/mathgl/mgl64"
"github.com/sandertv/gophertunnel/minecraft/protocol"
"math"
"math/rand"
)

const (
Expand Down Expand Up @@ -74,11 +75,18 @@ func (h *ItemStackRequestHandler) handleGrindstoneCraft(s *Session) error {
return h.createResults(s, stripPossibleEnchantments(resultStack))
}

// curseEnchantment represents an enchantment that may be a curse enchantment.
type curseEnchantment interface {
Curse() bool
}

// experienceFromEnchantments returns the amount of experience that is gained from the enchantments on the given stack.
func experienceFromEnchantments(stack item.Stack) int {
var totalCost int
for _, enchant := range stack.Enchantments() {
// TODO: Don't include curses.
if _, ok := enchant.Type().(curseEnchantment); ok {
continue
}
cost, _ := enchant.Type().Cost(enchant.Level())
totalCost += cost
}
Expand All @@ -94,7 +102,9 @@ func experienceFromEnchantments(stack item.Stack) int {
// stripPossibleEnchantments strips all enchantments possible, excluding curses.
func stripPossibleEnchantments(stack item.Stack) item.Stack {
for _, enchant := range stack.Enchantments() {
// TODO: Don't remove curses.
if _, ok := enchant.Type().(curseEnchantment); ok {
continue
}
stack = stack.WithoutEnchantments(enchant.Type())
}
return stack
Expand Down

0 comments on commit 00a8340

Please sign in to comment.