SilkMC / silk

Silk is a Minecraft API for Kotlin - targetting Fabric, Quilt and Paper
https://silkmc.net/silk/docs/
GNU General Public License v3.0
100 stars 12 forks source link

[Proposal] Refactor for nbt builder #41

Open SettingDust opened 1 year ago

SettingDust commented 1 year ago

Current:

nbtCompound {
    put("id", "minecraft:grass")
    put("Count", 5.toByte())
    put("tag", nbtCompound { put("foo", "bar") })
}

nbtList {
  add(1)
  add("foo")
}

// Should be renamed to `toTag`
1.toByte().toNbt()
"foo".toNbt()

New:

StringTag("foo")
IntTag(1)

Provide a compound tag builder scope. Return a compound tag

compoundTag {
    "id" to "minecraft:air"
    "Count" to ByteTag(5)
    "tag" to compoundTag {
        "type" to "stone"
        "items" to listTag {
            add(StringTag("minecraft:grass"))
        }
        "exclude" to listTag(StringTag("minecraft:stone"))
    }
}

Or(have to set JvmName for functions):

compoundTag {
    "id"("minecraft:air")
    "Count" { ByteTag(5) }
    "tag" { compoundTag {
        "type"("stone")
        "items" { 
          listTag {
              +"minecraft:grass"
              +StringTag("minecraft:grass")
          }
        }
        "exclude" { ListTag(StringTag("minecraft:stone")) }
    } }
}
jakobkmar commented 1 year ago

Mh, I much prefer the current DSL, I am not the biggest fan of these implicit APIs hiding the actual function calls - the only place where I would actually do this might be a simpler version of the text builder.

SettingDust commented 1 year ago

How about the functions for constructing tags just like ByteTag()?

jakobkmar commented 1 year ago

What would be advantage over the currently provided extension functions which do the same?

SettingDust commented 1 year ago

What would be advantage over the currently provided extension functions which do the same?

toNbt need the data type exactly same. Such as 5.toByte().toNbt(). Maybe we can provide two methods at same time

Amejonah1200 commented 1 year ago

Wouldn't be possible to have

nbtCompound { 
  "id" to "minecraft:grass"
  "Count" to 5.toByte()
  "tag" {
    "foo" to "bar"
  }
 }

using context receivers?

SettingDust commented 1 year ago

Why can't? I use the extension functions implemented I don't looking deep into context receiver. But I think it's possible