修复了一些bug
This commit is contained in:
@@ -45,7 +45,12 @@ const CookTime = 200
|
||||
|
||||
// FuelTicks 燃料燃烧时长表(tick,物品与背包.md §6)。
|
||||
var FuelTicks = map[string]int{
|
||||
"coal": 1600,
|
||||
"coal": 1600,
|
||||
"oak_planks": 300,
|
||||
"oak_log": 300,
|
||||
"stick": 100,
|
||||
"crafting_table": 300,
|
||||
"chest": 300,
|
||||
}
|
||||
|
||||
// New 创建方块实体(按类型分配槽位)。
|
||||
@@ -118,7 +123,7 @@ func (e *Entity) CanInsert(slot int, s item.Stack, reg *item.Registry) bool {
|
||||
case SlotInput:
|
||||
return e.Slots[slot].Empty() || e.Slots[slot].Name == s.Name
|
||||
case SlotOutput:
|
||||
return e.Slots[slot].Empty() || e.Slots[slot].Name == s.Name
|
||||
return false
|
||||
}
|
||||
}
|
||||
return inventory.CanMerge(e.Slots[slot], s) || e.Slots[slot].Empty()
|
||||
@@ -159,6 +164,47 @@ func (e *Entity) Remove(slot, count int) item.Stack {
|
||||
return out
|
||||
}
|
||||
|
||||
// Snapshot 存档快照(存档格式.md §2 block_entities)。
|
||||
type Snapshot struct {
|
||||
X, Y, Z int32 `json:"x"`
|
||||
Kind string `json:"kind"`
|
||||
Slots []item.Stack `json:"slots,omitempty"`
|
||||
BurnTicks int `json:"burn_ticks,omitempty"`
|
||||
BurnMaxTicks int `json:"burn_max,omitempty"`
|
||||
CookTicks int `json:"cook_ticks,omitempty"`
|
||||
}
|
||||
|
||||
// Serialize 导出快照。
|
||||
func (e *Entity) Serialize() Snapshot {
|
||||
if e == nil {
|
||||
return Snapshot{}
|
||||
}
|
||||
kind := "chest"
|
||||
if e.Kind == KindFurnace {
|
||||
kind = "furnace"
|
||||
}
|
||||
slots := make([]item.Stack, len(e.Slots))
|
||||
copy(slots, e.Slots)
|
||||
return Snapshot{
|
||||
X: e.X, Y: e.Y, Z: e.Z, Kind: kind, Slots: slots,
|
||||
BurnTicks: e.BurnTicks, BurnMaxTicks: e.BurnMaxTicks, CookTicks: e.CookTicks,
|
||||
}
|
||||
}
|
||||
|
||||
// Deserialize 从快照重建。
|
||||
func Deserialize(s Snapshot) *Entity {
|
||||
kind := KindChest
|
||||
if s.Kind == "furnace" {
|
||||
kind = KindFurnace
|
||||
}
|
||||
e := New(kind, s.X, s.Y, s.Z)
|
||||
for i := 0; i < len(s.Slots) && i < len(e.Slots); i++ {
|
||||
e.Slots[i] = s.Slots[i]
|
||||
}
|
||||
e.BurnTicks, e.BurnMaxTicks, e.CookTicks = s.BurnTicks, s.BurnMaxTicks, s.CookTicks
|
||||
return e
|
||||
}
|
||||
|
||||
// String 描述(调试)。
|
||||
func (e *Entity) String() string {
|
||||
return fmt.Sprintf("blockentity(%d @%d,%d,%d)", e.Kind, e.X, e.Y, e.Z)
|
||||
|
||||
@@ -90,4 +90,19 @@ func TestFurnaceSlotRules(t *testing.T) {
|
||||
if !f.CanInsert(SlotFuel, item.Stack{Name: "coal", Count: 1}, reg) {
|
||||
t.Fatal("燃料槽应接受煤炭")
|
||||
}
|
||||
if !f.CanInsert(SlotFuel, item.Stack{Name: "oak_planks", Count: 1}, reg) {
|
||||
t.Fatal("燃料槽应接受木板")
|
||||
}
|
||||
if f.CanInsert(SlotOutput, item.Stack{Name: "iron_ingot", Count: 1}, reg) {
|
||||
t.Fatal("输出格不应放入")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSerializeRoundtrip(t *testing.T) {
|
||||
e := NewChest(1, 2, 3)
|
||||
e.Slots[0] = item.Stack{Name: "stick", Count: 7}
|
||||
got := Deserialize(e.Serialize())
|
||||
if got.Kind != KindChest || got.Slots[0].Count != 7 {
|
||||
t.Fatalf("箱子快照往返失败: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user