Skip to content

Commit

Permalink
implement entity property saving
Browse files Browse the repository at this point in the history
  • Loading branch information
olebeck committed Jul 23, 2024
1 parent a9b5fde commit ada90d4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 26 deletions.
17 changes: 17 additions & 0 deletions handlers/worlds/worldstate/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,23 @@ func (s *EntityState) ToServerEntity(links []int64, properties []behaviourpack.E
}
s.toNBT(e.EntityType.NBT)

if len(s.Properties.FloatProperties)+len(s.Properties.IntegerProperties) > 0 {
nbtProperties := map[string]any{}
for _, prop := range s.Properties.FloatProperties {
propDef := properties[prop.Index]
nbtProperties[propDef.Name] = prop.Value
}
for _, prop := range s.Properties.IntegerProperties {
propDef := properties[prop.Index]
if propDef.Type == behaviourpack.PropertyTypeBool {
nbtProperties[propDef.Name] = prop.Value == 1
} else {
nbtProperties[propDef.Name] = prop.Value
}
}
e.EntityType.NBT["properties"] = nbtProperties
}

var linksTag []map[string]any
for i, el := range links {
linksTag = append(linksTag, map[string]any{
Expand Down
37 changes: 26 additions & 11 deletions handlers/worlds/worldstate/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ type worldStateInterface interface {
GetEntity(id EntityRuntimeID) *EntityState
AddEntityLink(el protocol.EntityLink)
}

type resourcePackDependency struct {
UUID string `json:"pack_id"`
Version [3]int `json:"version"`
}

type World struct {
// called when a chunk is added
ChunkFunc func(world.ChunkPos, *chunk.Chunk)
Expand All @@ -66,7 +72,7 @@ type World struct {

ResourcePacks []resource.Pack
resourcePacksDone chan error
resourcePackDependencies []resource.Dependency
resourcePackDependencies []resourcePackDependency

// closed when this world is done
finish chan struct{}
Expand Down Expand Up @@ -188,7 +194,7 @@ func (w *World) storeMemToProvider() error {
return nil
}

func addPacksJSON(fs utils.WriterFS, name string, deps []resource.Dependency) error {
func addPacksJSON(fs utils.WriterFS, name string, deps []resourcePackDependency) error {
f, err := fs.Create(name)
if err != nil {
return err
Expand All @@ -200,10 +206,20 @@ func addPacksJSON(fs utils.WriterFS, name string, deps []resource.Dependency) er
return nil
}

var removeSpace = strings.NewReplacer(" ", "")

func formatPackName(packName string) string {
packName = text.Clean(packName)
packName, _ = filenamify.FilenamifyV2(packName)
packName = removeSpace.Replace(packName)
packName = packName[:10]
return packName
}

func (w *World) addResourcePacks() error {
packNames := make(map[string][]string)
for _, pack := range w.ResourcePacks {
packName := pack.Name()
packName := formatPackName(pack.Name())
packNames[packName] = append(packNames[packName], pack.UUID())
}

Expand All @@ -213,30 +229,29 @@ func (w *World) addResourcePacks() error {
log.Warn("Cant add is encrypted")
continue
}
logrus.Infof(locale.Loc("adding_pack", locale.Strmap{"Name": pack.Name()}))
logrus.Infof(locale.Loc("adding_pack", locale.Strmap{"Name": text.Clean(pack.Name())}))

messages.Router.Handle(&messages.Message{
Source: "subcommand",
Target: "ui",
Data: messages.ProcessingWorldUpdate{
Name: w.Name,
State: "Adding Resourcepack " + pack.Name(),
State: "Adding Resourcepack " + text.Clean(pack.Name()),
},
})

packName := pack.Name()
packName := formatPackName(pack.Name())
if packIds := packNames[packName]; len(packIds) > 1 {
packName = fmt.Sprintf("%s_%d", packName, slices.Index(packIds, pack.UUID()))
packName = fmt.Sprintf("%s_%d", packName[:8], slices.Index(packIds, pack.UUID()))
}
packName = text.Clean(packName)
packName, _ = filenamify.FilenamifyV2(packName)

err := utils.CopyFS(pack, utils.SubFS(utils.OSWriter{Base: w.Folder}, path.Join("resource_packs", packName)))
if err != nil {
log.Error(err)
continue
}

w.resourcePackDependencies = append(w.resourcePackDependencies, resource.Dependency{
w.resourcePackDependencies = append(w.resourcePackDependencies, resourcePackDependency{
UUID: pack.Manifest().Header.UUID,
Version: pack.Manifest().Header.Version,
})
Expand Down Expand Up @@ -287,7 +302,7 @@ func (w *World) FinalizePacks(serverName string) error {
return err
}

err = addPacksJSON(fs, "world_behavior_packs.json", []resource.Dependency{{
err = addPacksJSON(fs, "world_behavior_packs.json", []resourcePackDependency{{
UUID: w.BehaviorPack.Manifest.Header.UUID,
Version: w.BehaviorPack.Manifest.Header.Version,
}})
Expand Down
30 changes: 15 additions & 15 deletions utils/behaviourpack/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ type EntityPropertyJson struct {
ClientSync bool `json:"client_sync"`
}

const (
PropertyTypeInt = iota
PropertyTypeFloat
PropertyTypeBool
PropertyTypeEnum
)

func (bp *Pack) AddEntity(entity EntityIn) {
ns, _ := ns_name_split(entity.Identifier)
if ns == "minecraft" {
Expand Down Expand Up @@ -130,18 +137,18 @@ func (bp *Pack) AddEntity(entity EntityIn) {
var prop EntityPropertyJson
prop.ClientSync = true
switch v.Type {
case propertyTypeInt:
case PropertyTypeInt:
prop.Type = "int"
prop.Range = []any{int(v.Min), int(v.Max)}
prop.Default = int(v.Min)
case propertyTypeFloat:
case PropertyTypeFloat:
prop.Type = "float"
prop.Range = []any{v.Min, v.Max}
prop.Default = v.Min
case propertyTypeBool:
case PropertyTypeBool:
prop.Type = "bool"
prop.Default = false
case propertyTypeEnum:
case PropertyTypeEnum:
prop.Type = "enum"
prop.Values = v.Enum
prop.Default = v.Enum[0]
Expand All @@ -154,13 +161,6 @@ func (bp *Pack) AddEntity(entity EntityIn) {
bp.entities[entity.Identifier] = entry
}

const (
propertyTypeInt = iota
propertyTypeFloat
propertyTypeBool
propertyTypeEnum
)

func (bp *Pack) GetEntityTypeProperties(entityType string) []EntityProperty {
return bp.entityProperties[entityType]
}
Expand Down Expand Up @@ -192,7 +192,7 @@ func (bp *Pack) SyncActorProperty(pk *packet.SyncActorProperty) {
prop.Type = propertyType

switch propertyType {
case propertyTypeInt:
case PropertyTypeInt:
min, ok := property["min"].(int32)
if !ok {
continue
Expand All @@ -203,7 +203,7 @@ func (bp *Pack) SyncActorProperty(pk *packet.SyncActorProperty) {
}
prop.Min = float32(min)
prop.Max = float32(max)
case propertyTypeFloat:
case PropertyTypeFloat:
min, ok := property["min"].(int32)
if !ok {
continue
Expand All @@ -214,8 +214,8 @@ func (bp *Pack) SyncActorProperty(pk *packet.SyncActorProperty) {
}
prop.Min = float32(min)
prop.Max = float32(max)
case propertyTypeBool:
case propertyTypeEnum:
case PropertyTypeBool:
case PropertyTypeEnum:
prop.Enum, _ = property["enum"].([]any)
default:
fmt.Printf("Unknown property type %d", propertyType)
Expand Down
12 changes: 12 additions & 0 deletions utils/behaviourpack/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ func (bp *Pack) ApplyComponentEntries(entries []protocol.ItemComponentEntry) {
continue
}
if components, ok := ice.Data["components"].(map[string]any); ok {
if _, ok := components["minecraft:icon"]; !ok {
if item_properties, ok := components["item_properties"].(map[string]any); ok {
components["minecraft:icon"] = item_properties["minecraft:icon"]
}
}

if icon, ok := components["minecraft:icon"].(map[string]any); ok {
if textures, ok := icon["textures"].(map[string]any); ok {
icon["textures"] = textures["default"]
}
}

item.MinecraftItem.Components = components
}
}
Expand Down

0 comments on commit ada90d4

Please sign in to comment.