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

Implemented the Curse of Vanishing enchantment #661

Merged
merged 14 commits into from
Aug 29, 2022
6 changes: 6 additions & 0 deletions server/item/enchantment/enchantment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package enchantment

// Curse represents an enchantment that may be a curse enchantment.
type Curse interface {
JustTalDevelops marked this conversation as resolved.
Show resolved Hide resolved
Curse() bool
}
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, Vanishing{})
// 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"
)

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

// Name ...
func (Vanishing) Name() string {
return "Vanishing"
JustTalDevelops marked this conversation as resolved.
Show resolved Hide resolved
}

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

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

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

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

// CompatibleWithItem ...
func (Vanishing) CompatibleWithItem(i world.Item) bool {
_, dur := i.(item.Durable)
_, com := i.(item.Compass)
DaPigGuy marked this conversation as resolved.
Show resolved Hide resolved
_, arm := i.(item.Armour)
// TODO: Recovery Compass
JustTalDevelops marked this conversation as resolved.
Show resolved Hide resolved
// TODO: Carrot on a Stick
// TODO: Warped Fungus on a Stick
return arm || com || dur
}

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

// Treasure ...
func (Vanishing) 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 @@ -914,6 +915,9 @@ func (p *Player) dropContents() {
w.AddEntity(orb)
}
for _, it := range append(p.inv.Items(), append(p.armour.Items(), p.offHand.Items()...)...) {
if _, ok := it.Enchantment(enchantment.Vanishing{}); 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
14 changes: 10 additions & 4 deletions server/session/handler_grindstone.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ 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/df-mc/dragonfly/server/item/enchantment"
"github.com/go-gl/mathgl/mgl64"
"github.com/sandertv/gophertunnel/minecraft/protocol"
"math"
"math/rand"
)

const (
Expand Down Expand Up @@ -78,7 +80,9 @@ func (h *ItemStackRequestHandler) handleGrindstoneCraft(s *Session) error {
func experienceFromEnchantments(stack item.Stack) int {
var totalCost int
for _, enchant := range stack.Enchantments() {
// TODO: Don't include curses.
if _, ok := enchant.Type().(enchantment.Curse); ok {
continue
}
cost, _ := enchant.Type().Cost(enchant.Level())
totalCost += cost
}
Expand All @@ -94,7 +98,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().(enchantment.Curse); ok {
continue
}
stack = stack.WithoutEnchantments(enchant.Type())
}
return stack
Expand Down
9 changes: 5 additions & 4 deletions server/session/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package session
import (
"encoding/json"
"fmt"
"math"
"net"
"time"
_ "unsafe" // Imported for compiler directives.

"github.com/df-mc/atomic"
"github.com/df-mc/dragonfly/server/block"
"github.com/df-mc/dragonfly/server/entity"
Expand All @@ -19,10 +24,6 @@ import (
"github.com/google/uuid"
"github.com/sandertv/gophertunnel/minecraft/protocol"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
"math"
"net"
"time"
_ "unsafe" // Imported for compiler directives.
)

// StopShowingEntity stops showing a world.Entity to the Session. It will be completely invisible until a call to
Expand Down