silk icon indicating copy to clipboard operation
silk copied to clipboard

[Proposal] Refactor for nbt builder

Open SettingDust opened this issue 3 years ago • 6 comments
trafficstars

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")) }
    } }
}

SettingDust avatar Oct 14 '22 07:10 SettingDust

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.

jakobkmar avatar Oct 22 '22 16:10 jakobkmar

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

SettingDust avatar Oct 24 '22 03:10 SettingDust

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

jakobkmar avatar Nov 26 '22 17:11 jakobkmar

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

SettingDust avatar Nov 27 '22 02:11 SettingDust

Wouldn't be possible to have

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

using context receivers?

Amejonah1200 avatar Nov 28 '22 11:11 Amejonah1200

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

SettingDust avatar Nov 28 '22 13:11 SettingDust