Skip to content

Commit

Permalink
dragonfly/server: Update for 1.21.60
Browse files Browse the repository at this point in the history
  • Loading branch information
TwistedAsylumMC committed Feb 5, 2025
1 parent bae2538 commit 56fdee2
Show file tree
Hide file tree
Showing 22 changed files with 247 additions and 111 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/go-gl/mathgl v1.2.0
github.com/google/uuid v1.6.0
github.com/pelletier/go-toml v1.9.5
github.com/sandertv/gophertunnel v1.43.1
github.com/sandertv/gophertunnel v1.43.2-0.20250205122318-98ae443c59fd
github.com/segmentio/fasthash v1.0.3
golang.org/x/exp v0.0.0-20250103183323-7d7fa50e5329
golang.org/x/mod v0.22.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sandertv/go-raknet v1.14.2 h1:UZLyHn5yQU2Dq2GVq/LlxwAUikaq4q4AA1rl/Pf3AXQ=
github.com/sandertv/go-raknet v1.14.2/go.mod h1:/yysjwfCXm2+2OY8mBazLzcxJ3irnylKCyG3FLgUPVU=
github.com/sandertv/gophertunnel v1.43.1 h1:wY6Fy8dRMKtpZUQzCR35o9k05135vOJZhrz0GF6OXFI=
github.com/sandertv/gophertunnel v1.43.1/go.mod h1:XuEJo+ARim+NKiD90Z56sQRcDtCOErz26e2bt3LEd9I=
github.com/sandertv/gophertunnel v1.43.2-0.20250205122318-98ae443c59fd h1:uLUw6XKdxx2gg0+USWUQQ98RJnRZqxrS4IQnt2wmK/g=
github.com/sandertv/gophertunnel v1.43.2-0.20250205122318-98ae443c59fd/go.mod h1:XuEJo+ARim+NKiD90Z56sQRcDtCOErz26e2bt3LEd9I=
github.com/segmentio/fasthash v1.0.3 h1:EI9+KE1EwvMLBWwjpRDc+fEM+prwxDYbslddQGtrmhM=
github.com/segmentio/fasthash v1.0.3/go.mod h1:waKX8l2N8yckOgmSsXJi7x1ZfdKZ4x7KRMzBtS3oedY=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down
9 changes: 1 addition & 8 deletions server/block/copper_door.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,21 +178,14 @@ func (d CopperDoor) EncodeItem() (name string, meta int16) {

// EncodeBlock ...
func (d CopperDoor) EncodeBlock() (name string, properties map[string]any) {
direction := d.Facing
if d.Facing == cube.East {
direction = cube.North
} else if d.Facing == cube.North {
direction = cube.East
}

name = "copper_door"
if d.Oxidation != UnoxidisedOxidation() {
name = d.Oxidation.String() + "_" + name
}
if d.Waxed {
name = "waxed_" + name
}
return "minecraft:" + name, map[string]any{"direction": int32(direction), "door_hinge_bit": d.Right, "open_bit": d.Open, "upper_block_bit": d.Top}
return "minecraft:" + name, map[string]any{"minecraft:cardinal_direction": d.Facing.String(), "door_hinge_bit": d.Right, "open_bit": d.Open, "upper_block_bit": d.Top}
}

// allCopperDoors returns a list of all copper door types
Expand Down
14 changes: 2 additions & 12 deletions server/block/wood_door.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,20 +137,10 @@ func (d WoodDoor) EncodeItem() (name string, meta int16) {

// EncodeBlock ...
func (d WoodDoor) EncodeBlock() (name string, properties map[string]any) {
direction := 3
switch d.Facing {
case cube.South:
direction = 1
case cube.West:
direction = 2
case cube.East:
direction = 0
}

if d.Wood == OakWood() {
return "minecraft:wooden_door", map[string]any{"direction": int32(direction), "door_hinge_bit": d.Right, "open_bit": d.Open, "upper_block_bit": d.Top}
return "minecraft:wooden_door", map[string]any{"minecraft:cardinal_direction": d.Facing.String(), "door_hinge_bit": d.Right, "open_bit": d.Open, "upper_block_bit": d.Top}
}
return "minecraft:" + d.Wood.String() + "_door", map[string]any{"direction": int32(direction), "door_hinge_bit": d.Right, "open_bit": d.Open, "upper_block_bit": d.Top}
return "minecraft:" + d.Wood.String() + "_door", map[string]any{"minecraft:cardinal_direction": d.Facing.String(), "door_hinge_bit": d.Right, "open_bit": d.Open, "upper_block_bit": d.Top}
}

// allDoors returns a list of all door types
Expand Down
4 changes: 2 additions & 2 deletions server/block/wood_fence_gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func (f WoodFenceGate) EncodeItem() (name string, meta int16) {
// EncodeBlock ...
func (f WoodFenceGate) EncodeBlock() (name string, properties map[string]any) {
if f.Wood == OakWood() {
return "minecraft:fence_gate", map[string]any{"direction": int32(horizontalDirection(f.Facing)), "open_bit": f.Open, "in_wall_bit": f.Lowered}
return "minecraft:fence_gate", map[string]any{"minecraft:cardinal_direction": f.Facing.String(), "open_bit": f.Open, "in_wall_bit": f.Lowered}
}
return "minecraft:" + f.Wood.String() + "_fence_gate", map[string]any{"direction": int32(horizontalDirection(f.Facing)), "open_bit": f.Open, "in_wall_bit": f.Lowered}
return "minecraft:" + f.Wood.String() + "_fence_gate", map[string]any{"minecraft:cardinal_direction": f.Facing.String(), "open_bit": f.Open, "in_wall_bit": f.Lowered}
}

// Model ...
Expand Down
37 changes: 37 additions & 0 deletions server/item/creative/category.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package creative

// Category represents a category of items in the creative inventory which are shown as different tabs.
type Category struct {
category
}

type category uint8

// ConstructionCategory is the construction category which contains only blocks that do not fall under
// any other category.
func ConstructionCategory() Category {
return Category{1}
}

// NatureCategory is the nature category which contains blocks and items that can be naturally found in the
// world.
func NatureCategory() Category {
return Category{2}
}

// EquipmentCategory is the equipment category which contains tools, armour, food and any other form of
// equipment.
func EquipmentCategory() Category {
return Category{3}
}

// ItemsCategory is the items category for all the miscellaneous items that do not fall under any other
// category.
func ItemsCategory() Category {
return Category{4}
}

// Uint8 returns the category type as a uint8.
func (s category) Uint8() uint8 {
return uint8(s)
}
163 changes: 117 additions & 46 deletions server/item/creative/creative.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package creative

import (
_ "embed"
"fmt"
"github.com/df-mc/dragonfly/server/internal/nbtconv"
// The following four imports are essential for this package: They make sure this package is loaded after
// all these imports. This ensures that all blocks and items are registered before the creative items are
Expand All @@ -12,90 +13,160 @@ import (
"github.com/sandertv/gophertunnel/minecraft/nbt"
)

// Item represents a registered item in the creative inventory. It holds a stack of the item and a group that
// the item is part of.
type Item struct {
// Stack is the stack of the item that is registered in the creative inventory.
Stack item.Stack
// Group is the name of the group that the item is part of. If two groups are registered with the same
// name, the item will always reside in the first group that was registered.
Group string
}

// Group represents a group of items in the creative inventory. Each group has a category, a name and an icon.
// If either the name or icon is empty, the group is considered an 'anonymous group' and will not group its
// contents together in the creative inventory.
type Group struct {
// Category is the category of the group. It determines the tab in which the group will be displayed in the
// creative inventory.
Category Category
// Name is the localised name of the group, i.e. "itemGroup.name.planks".
Name string
// Icon is the item that will be displayed as the icon of the group in the creative inventory.
Icon item.Stack
}

// Groups returns a list with all groups that have been registered as a creative group. These groups will be
// accessible by players in-game who have creative mode enabled.
func Groups() []Group {
return creativeGroups
}

// RegisterGroup registers a group as a creative group, exposing it in the creative inventory. It can then
// be referenced using its name when calling RegisterItem.
func RegisterGroup(group Group) {
creativeGroups = append(creativeGroups, group)
}

// Items returns a list with all items that have been registered as a creative item. These items will
// be accessible by players in-game who have creative mode enabled.
func Items() []item.Stack {
func Items() []Item {
return creativeItemStacks
}

// RegisterItem registers an item as a creative item, exposing it in the creative inventory.
func RegisterItem(item item.Stack) {
func RegisterItem(item Item) {
creativeItemStacks = append(creativeItemStacks, item)
}

var (
//go:embed creative_items.nbt
creativeItemData []byte

// creativeGroups holds a list of all groups that were registered to the creative inventory using
// RegisterGroup.
creativeGroups []Group
// creativeItemStacks holds a list of all item stacks that were registered to the creative inventory using
// RegisterItem.
creativeItemStacks []item.Stack
creativeItemStacks []Item
)

// creativeGroupEntry holds data of a creative group as present in the creative inventory.
type creativeGroupEntry struct {
Category int32 `nbt:"category"`
Name string `nbt:"name"`
Icon creativeItemEntry `nbt:"icon"`
}

// creativeItemEntry holds data of a creative item as present in the creative inventory.
type creativeItemEntry struct {
Name string `nbt:"name"`
Meta int16 `nbt:"meta"`
NBT map[string]any `nbt:"nbt,omitempty"`
BlockProperties map[string]any `nbt:"block_properties,omitempty"`
GroupIndex int32 `nbt:"group_index,omitempty"`
}

// init initialises the creative items, registering all creative items that have also been registered as
// normal items and are present in vanilla.
func init() {
var m []creativeItemEntry
var m struct {
Groups []creativeGroupEntry `nbt:"groups"`
Items []creativeItemEntry `nbt:"items"`
}
if err := nbt.Unmarshal(creativeItemData, &m); err != nil {
panic(err)
}
for _, data := range m {
var (
it world.Item
ok bool
)
if len(data.BlockProperties) > 0 {
// Item with a block, try parsing the block, then try asserting that to an item. Blocks no longer
// have their metadata sent, but we still need to get that metadata in order to be able to register
// different block states as different items.
if b, ok := world.BlockByName(data.Name, data.BlockProperties); ok {
if it, ok = b.(world.Item); !ok {
continue
}
}
} else {
if it, ok = world.ItemByName(data.Name, data.Meta); !ok {
// The item wasn't registered, so don't register it as a creative item.
continue
}
if _, resultingMeta := it.EncodeItem(); resultingMeta != data.Meta {
// We found an item registered with that ID and a meta of 0, but we only need items with strictly
// the same meta here.
continue
}
for i, group := range m.Groups {
name := group.Name
if name == "" {
name = fmt.Sprint("anon", i)
}
st, _ := itemStackFromEntry(group.Icon)
c := Category{category(group.Category)}
RegisterGroup(Group{Category: c, Name: name, Icon: st})
}
for _, data := range m.Items {
if data.GroupIndex >= int32(len(creativeGroups)) {
panic(fmt.Errorf("invalid group index %v for item %v", data.GroupIndex, data.Name))
}
st, ok := itemStackFromEntry(data)
if !ok {
continue
}
RegisterItem(Item{st, creativeGroups[data.GroupIndex].Name})
}
}

if n, ok := it.(world.NBTer); ok {
if len(data.NBT) > 0 {
it = n.DecodeNBT(data.NBT).(world.Item)
func itemStackFromEntry(data creativeItemEntry) (item.Stack, bool) {
var (
it world.Item
ok bool
)
if len(data.BlockProperties) > 0 {
// Item with a block, try parsing the block, then try asserting that to an item. Blocks no longer
// have their metadata sent, but we still need to get that metadata in order to be able to register
// different block states as different items.
if b, ok := world.BlockByName(data.Name, data.BlockProperties); ok {
if it, ok = b.(world.Item); !ok {
return item.Stack{}, false
}
}
} else {
if it, ok = world.ItemByName(data.Name, data.Meta); !ok {
// The item wasn't registered, so don't register it as a creative item.
return item.Stack{}, false
}
if _, resultingMeta := it.EncodeItem(); resultingMeta != data.Meta {
// We found an item registered with that ID and a meta of 0, but we only need items with strictly
// the same meta here.
return item.Stack{}, false
}
}

st := item.NewStack(it, 1)
if n, ok := it.(world.NBTer); ok {
if len(data.NBT) > 0 {
var invalid bool
for _, e := range nbtconv.Slice(data.NBT, "ench") {
if v, ok := e.(map[string]any); ok {
t, ok := item.EnchantmentByID(int(nbtconv.Int16(v, "id")))
if !ok {
invalid = true
break
}
st = st.WithEnchantments(item.NewEnchantment(t, int(nbtconv.Int16(v, "lvl"))))
it = n.DecodeNBT(data.NBT).(world.Item)
}
}

st := item.NewStack(it, 1)
if len(data.NBT) > 0 {
var invalid bool
for _, e := range nbtconv.Slice(data.NBT, "ench") {
if v, ok := e.(map[string]any); ok {
t, ok := item.EnchantmentByID(int(nbtconv.Int16(v, "id")))
if !ok {
invalid = true
break
}
st = st.WithEnchantments(item.NewEnchantment(t, int(nbtconv.Int16(v, "lvl"))))
}
if invalid {
// Invalid enchantment, skip this item.
continue
}
}
RegisterItem(st)
if invalid {
// Invalid enchantment, skip this item.
return item.Stack{}, false
}
}
return st, true
}
Binary file modified server/item/creative/creative_items.nbt
Binary file not shown.
Binary file modified server/item/recipe/crafting_data.nbt
Binary file not shown.
Binary file modified server/item/recipe/furnace_data.nbt
Binary file not shown.
Binary file modified server/item/recipe/potion_data.nbt
Binary file not shown.
18 changes: 16 additions & 2 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ type playerData struct {

cooldowns map[string]time.Time

speed float64
flightSpeed float64
speed float64
flightSpeed float64
verticalFlightSpeed float64

health *entity.HealthManager
experience *entity.ExperienceManager
Expand Down Expand Up @@ -473,6 +474,19 @@ func (p *Player) FlightSpeed() float64 {
return p.flightSpeed
}

// SetVerticalFlightSpeed sets the flight speed of the player on the Y axis. The value passed represents the
// base speed, which is the blocks/tick speed that the player will obtain while flying.
func (p *Player) SetVerticalFlightSpeed(flightSpeed float64) {
p.verticalFlightSpeed = flightSpeed
p.session().SendAbilities(p)
}

// VerticalFlightSpeed returns the flight speed of the player on the Y axis, with the value representing the
// base speed. The default vertical flight speed of a player is 1.0, which corresponds to 1 block/tick.
func (p *Player) VerticalFlightSpeed() float64 {
return p.verticalFlightSpeed
}

// Health returns the current health of the player. It will always be lower than Player.MaxHealth().
func (p *Player) Health() float64 {
return p.health.Health()
Expand Down
Loading

0 comments on commit 56fdee2

Please sign in to comment.