Skip to content

Commit

Permalink
Merge branch 'master' into double-chest
Browse files Browse the repository at this point in the history
  • Loading branch information
RestartFU authored May 29, 2024
2 parents 25c4e59 + 67bccf4 commit c682161
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 19 deletions.
2 changes: 1 addition & 1 deletion server/block/banner.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (b Banner) EncodeNBT() map[string]any {
func (b Banner) DecodeNBT(m map[string]any) any {
b.Colour = invertColourID(int16(nbtconv.Int32(m, "Base")))
b.Illager = nbtconv.Int32(m, "Type") == 1
if patterns := nbtconv.Slice(m, "Patterns"); patterns != nil {
if patterns := nbtconv.Slice[any](m, "Patterns"); patterns != nil {
b.Patterns = make([]BannerPatternLayer, len(patterns))
for i, p := range b.Patterns {
b.Patterns[i] = p.DecodeNBT(patterns[i].(map[string]any)).(BannerPatternLayer)
Expand Down
2 changes: 1 addition & 1 deletion server/block/barrel.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (b Barrel) DecodeNBT(data map[string]any) any {
b = NewBarrel()
b.Facing = facing
b.CustomName = nbtconv.String(data, "CustomName")
nbtconv.InvFromNBT(b.inventory, nbtconv.Slice(data, "Items"))
nbtconv.InvFromNBT(b.inventory, nbtconv.Slice[any](data, "Items"))
return b
}

Expand Down
2 changes: 1 addition & 1 deletion server/block/blast_furnace.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (b BlastFurnace) DecodeNBT(data map[string]interface{}) interface{} {
b.Lit = lit
b.setExperience(xp)
b.setDurations(remaining, maximum, cook)
nbtconv.InvFromNBT(b.Inventory(), nbtconv.Slice(data, "Items"))
nbtconv.InvFromNBT(b.Inventory(), nbtconv.Slice[any](data, "Items"))
return b
}

Expand Down
4 changes: 2 additions & 2 deletions server/block/chest.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ func (c Chest) DecodeNBT(data map[string]any) any {
}
})

nbtconv.InvFromNBT(c.pairInv, nbtconv.Slice(data, "Items"))
nbtconv.InvFromNBT(c.pairInv, nbtconv.Slice[any](data, "Items"))
}

nbtconv.InvFromNBT(c.inventory, nbtconv.Slice(data, "Items"))
nbtconv.InvFromNBT(c.inventory, nbtconv.Slice[any](data, "Items"))
return c
}

Expand Down
4 changes: 2 additions & 2 deletions server/block/decorated_pot.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ func (p DecoratedPot) EncodeNBT() map[string]any {
// DecodeNBT ...
func (p DecoratedPot) DecodeNBT(data map[string]any) any {
p.Decorations = [4]PotDecoration{}
if sherds := nbtconv.Slice(data, "sherds"); sherds != nil {
if sherds := nbtconv.Slice[string](data, "sherds"); sherds != nil {
for i, name := range sherds {
it, ok := world.ItemByName(name.(string), 0)
it, ok := world.ItemByName(name, 0)
if !ok {
panic(fmt.Errorf("unknown item %s", name))
}
Expand Down
2 changes: 1 addition & 1 deletion server/block/furnace.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (f Furnace) DecodeNBT(data map[string]interface{}) interface{} {
f.Lit = lit
f.setExperience(xp)
f.setDurations(remaining, maximum, cook)
nbtconv.InvFromNBT(f.Inventory(), nbtconv.Slice(data, "Items"))
nbtconv.InvFromNBT(f.Inventory(), nbtconv.Slice[any](data, "Items"))
return f
}

Expand Down
2 changes: 1 addition & 1 deletion server/block/smoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (s Smoker) DecodeNBT(data map[string]interface{}) interface{} {
s.Lit = lit
s.setExperience(xp)
s.setDurations(remaining, maximum, cook)
nbtconv.InvFromNBT(s.Inventory(), nbtconv.Slice(data, "Items"))
nbtconv.InvFromNBT(s.Inventory(), nbtconv.Slice[any](data, "Items"))
return s
}

Expand Down
1 change: 0 additions & 1 deletion server/entity/lingering_potion.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type LingeringPotionType struct{}
func (LingeringPotionType) EncodeEntity() string {
return "minecraft:lingering_potion"
}
func (LingeringPotionType) Glint() bool { return true }
func (LingeringPotionType) BBox(world.Entity) cube.BBox {
return cube.Box(-0.125, 0, -0.125, 0.125, 0.25, 0.125)
}
Expand Down
1 change: 0 additions & 1 deletion server/entity/splash_potion.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ var splashPotionConf = ProjectileBehaviourConfig{
type SplashPotionType struct{}

func (SplashPotionType) EncodeEntity() string { return "minecraft:splash_potion" }
func (SplashPotionType) Glint() bool { return true }
func (SplashPotionType) BBox(world.Entity) cube.BBox {
return cube.Box(-0.125, 0, -0.125, 0.125, 0.25, 0.125)
}
Expand Down
8 changes: 4 additions & 4 deletions server/internal/nbtconv/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ func Float64(m map[string]any, k string) float64 {
return v
}

// Slice reads a []any value from a map at key k.
func Slice(m map[string]any, k string) []any {
v, _ := m[k].([]any)
// Slice reads a []T value from a map at key k.
func Slice[T any](m map[string]any, k string) []T {
v, _ := m[k].([]T)
return v
}

Expand Down Expand Up @@ -242,7 +242,7 @@ func readArmourTrim(m map[string]any, s *item.Stack) {
func readEnchantments(m map[string]any, s *item.Stack) {
enchantments, ok := m["ench"].([]map[string]any)
if !ok {
for _, e := range Slice(m, "ench") {
for _, e := range Slice[any](m, "ench") {
if v, ok := e.(map[string]any); ok {
enchantments = append(enchantments, v)
}
Expand Down
2 changes: 1 addition & 1 deletion server/item/creative/creative.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func init() {
st := item.NewStack(it, 1)
if len(data.NBT) > 0 {
var invalid bool
for _, e := range nbtconv.Slice(data.NBT, "ench") {
for _, e := range nbtconv.Slice[any](data.NBT, "ench") {
if v, ok := e.(map[string]any); ok {
t, ok := item.EnchantmentByID(int(nbtconv.Int16(v, "id")))
if !ok {
Expand Down
10 changes: 9 additions & 1 deletion server/item/inventory/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,15 @@ func (inv *Inventory) RemoveItemFunc(n int, comparable func(stack item.Stack) bo
if slotIt.Empty() || !comparable(slotIt) {
continue
}
f := inv.setItem(slot, slotIt.Grow(-n))
c := slotIt.Count() - n

var f func()
if c <= 0 {
f = inv.setItem(slot, item.Stack{})
} else {
f = inv.setItem(slot, slotIt.Grow(-n))
}

//noinspection GoDeferInLoop
defer f()

Expand Down
2 changes: 2 additions & 0 deletions server/item/smithing_template_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ func ArmourSmithingTemplateFromString(name string) ArmourSmithingTemplate {
return TemplateTide()
case "snout":
return TemplateSnout()
case "rib":
return TemplateRib()
case "eye":
return TemplateEye()
case "spire":
Expand Down
2 changes: 1 addition & 1 deletion server/session/enchantment_texts.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion server/world/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,25 @@ func (w *World) Block(pos cube.Pos) Block {
return air()
}
c := w.chunk(chunkPosFromBlockPos(pos))
defer c.Unlock()

rid := c.Block(uint8(pos[0]), int16(pos[1]), uint8(pos[2]), 0)
if nbtBlocks[rid] {
// The block was also a block entity, so we look it up in the block entity map.
if nbtB, ok := c.BlockEntities[pos]; ok {
c.Unlock()
return nbtB
}
b, _ := BlockByRuntimeID(rid)
nbtB := b.(NBTer).DecodeNBT(map[string]any{}).(Block)
c.BlockEntities[pos] = nbtB
viewers := slices.Clone(c.viewers)
c.Unlock()
for _, v := range viewers {
v.ViewBlockUpdate(pos, nbtB, 0)
}
return nbtB
}
c.Unlock()
b, _ := BlockByRuntimeID(rid)
return b
}
Expand Down

0 comments on commit c682161

Please sign in to comment.