Skip to content

Commit

Permalink
packet/crafting_data.go: Unified Marshal/Unmarshal
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandertv committed Mar 22, 2023
1 parent a2c7d94 commit af50825
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 67 deletions.
1 change: 1 addition & 0 deletions minecraft/protocol/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type IO interface {
ItemInstance(i *ItemInstance)
ItemDescriptorCount(i *ItemDescriptorCount)
MaterialReducer(x *MaterialReducer)
Recipe(x *Recipe)
GameRule(x *GameRule)
AbilityValue(x *any)

Expand Down
72 changes: 5 additions & 67 deletions minecraft/protocol/packet/crafting_data.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package packet

import (
"fmt"
"github.com/sandertv/gophertunnel/minecraft/protocol"
)

Expand Down Expand Up @@ -32,77 +31,16 @@ func (*CraftingData) ID() uint32 {

// Marshal ...
func (pk *CraftingData) Marshal(w *protocol.Writer) {
l := uint32(len(pk.Recipes))
w.Varuint32(&l)
for _, recipe := range pk.Recipes {
var c int32
switch recipe.(type) {
case *protocol.ShapelessRecipe:
c = protocol.RecipeShapeless
case *protocol.ShapedRecipe:
c = protocol.RecipeShaped
case *protocol.FurnaceRecipe:
c = protocol.RecipeFurnace
case *protocol.FurnaceDataRecipe:
c = protocol.RecipeFurnaceData
case *protocol.MultiRecipe:
c = protocol.RecipeMulti
case *protocol.ShulkerBoxRecipe:
c = protocol.RecipeShulkerBox
case *protocol.ShapelessChemistryRecipe:
c = protocol.RecipeShapelessChemistry
case *protocol.ShapedChemistryRecipe:
c = protocol.RecipeShapedChemistry
case *protocol.SmithingTransformRecipe:
c = protocol.RecipeSmithingTransform
default:
w.UnknownEnumOption(fmt.Sprintf("%T", recipe), "crafting recipe type")
}
w.Varint32(&c)
recipe.Marshal(w)
}
protocol.Slice(w, &pk.PotionRecipes)
protocol.Slice(w, &pk.PotionContainerChangeRecipes)
protocol.FuncSlice(w, &pk.MaterialReducers, w.MaterialReducer)
w.Bool(&pk.ClearRecipes)
pk.marshal(w)
}

// Unmarshal ...
func (pk *CraftingData) Unmarshal(r *protocol.Reader) {
var length uint32
r.Varuint32(&length)
pk.Recipes = make([]protocol.Recipe, length)
for i := uint32(0); i < length; i++ {
var recipeType int32
r.Varint32(&recipeType)
pk.marshal(r)
}

var recipe protocol.Recipe
switch recipeType {
case protocol.RecipeShapeless:
recipe = &protocol.ShapelessRecipe{}
case protocol.RecipeShaped:
recipe = &protocol.ShapedRecipe{}
case protocol.RecipeFurnace:
recipe = &protocol.FurnaceRecipe{}
case protocol.RecipeFurnaceData:
recipe = &protocol.FurnaceDataRecipe{}
case protocol.RecipeMulti:
recipe = &protocol.MultiRecipe{}
case protocol.RecipeShulkerBox:
recipe = &protocol.ShulkerBoxRecipe{}
case protocol.RecipeShapelessChemistry:
recipe = &protocol.ShapelessChemistryRecipe{}
case protocol.RecipeShapedChemistry:
recipe = &protocol.ShapedChemistryRecipe{}
case protocol.RecipeSmithingTransform:
recipe = &protocol.SmithingTransformRecipe{}
default:
r.UnknownEnumOption(recipeType, "crafting data recipe type")
return
}
recipe.Unmarshal(r)
pk.Recipes[i] = recipe
}
func (pk *CraftingData) marshal(r protocol.IO) {
protocol.FuncSlice(r, &pk.Recipes, r.Recipe)
protocol.Slice(r, &pk.PotionRecipes)
protocol.Slice(r, &pk.PotionContainerChangeRecipes)
protocol.FuncSlice(r, &pk.MaterialReducers, r.MaterialReducer)
Expand Down
31 changes: 31 additions & 0 deletions minecraft/protocol/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,37 @@ func (r *Reader) MaterialReducer(m *MaterialReducer) {
Slice(r, &m.Outputs)
}

// Recipe reads a Recipe from the reader.
func (r *Reader) Recipe(x *Recipe) {
var recipeType int32
r.Varint32(&recipeType)

switch recipeType {
case RecipeShapeless:
*x = &ShapelessRecipe{}
case RecipeShaped:
*x = &ShapedRecipe{}
case RecipeFurnace:
*x = &FurnaceRecipe{}
case RecipeFurnaceData:
*x = &FurnaceDataRecipe{}
case RecipeMulti:
*x = &MultiRecipe{}
case RecipeShulkerBox:
*x = &ShulkerBoxRecipe{}
case RecipeShapelessChemistry:
*x = &ShapelessChemistryRecipe{}
case RecipeShapedChemistry:
*x = &ShapedChemistryRecipe{}
case RecipeSmithingTransform:
*x = &SmithingTransformRecipe{}
default:
r.UnknownEnumOption(recipeType, "crafting data recipe type")
return
}
(*x).Unmarshal(r)
}

// AbilityValue reads an ability value from the reader.
func (r *Reader) AbilityValue(x *any) {
valType, boolVal, floatVal := uint8(0), false, float32(0)
Expand Down
29 changes: 29 additions & 0 deletions minecraft/protocol/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,35 @@ func (w *Writer) MaterialReducer(m *MaterialReducer) {
Slice(w, &m.Outputs)
}

// Recipe writes a Recipe to the writer.
func (w *Writer) Recipe(x *Recipe) {
var c int32
switch (*x).(type) {
case *ShapelessRecipe:
c = RecipeShapeless
case *ShapedRecipe:
c = RecipeShaped
case *FurnaceRecipe:
c = RecipeFurnace
case *FurnaceDataRecipe:
c = RecipeFurnaceData
case *MultiRecipe:
c = RecipeMulti
case *ShulkerBoxRecipe:
c = RecipeShulkerBox
case *ShapelessChemistryRecipe:
c = RecipeShapelessChemistry
case *ShapedChemistryRecipe:
c = RecipeShapedChemistry
case *SmithingTransformRecipe:
c = RecipeSmithingTransform
default:
w.UnknownEnumOption(fmt.Sprintf("%T", *x), "crafting recipe type")
}
w.Varint32(&c)
(*x).Marshal(w)
}

// AbilityValue writes an ability value to the writer.
func (w *Writer) AbilityValue(x *any) {
switch val := (*x).(type) {
Expand Down

0 comments on commit af50825

Please sign in to comment.