Skip to content
This repository was archived by the owner on Apr 15, 2026. It is now read-only.

Commit dc0964d

Browse files
committed
Allow basic items to be registered
1 parent 20405f4 commit dc0964d

9 files changed

Lines changed: 112 additions & 14 deletions

File tree

src/main/kotlin/org/sandboxpowered/fabric/api/PolyglotRecipeManager.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import net.minecraft.util.registry.Registry
88
import org.graalvm.polyglot.HostAccess.Export
99
import org.graalvm.polyglot.Value
1010
import org.sandboxpowered.fabric.Main
11-
import org.sandboxpowered.fabric.util.getMemberValue
11+
import org.sandboxpowered.fabric.util.getMemberValueStr
1212
import org.sandboxpowered.fabric.util.removeIf
1313
import org.sandboxpowered.fabric.util.toJSON
1414
import java.util.function.BiPredicate
@@ -55,10 +55,10 @@ class PolyglotRecipeManager(private val map: MutableMap<Identifier, JsonElement>
5555

5656
var predicate: BiPredicate<Identifier, JsonElement>? = null
5757

58-
val id = value.getMemberValue("id")
59-
val output = value.getMemberValue("output")
60-
val domain = value.getMemberValue("domain")
61-
val type = value.getMemberValue("type")
58+
val id = value.getMemberValueStr("id")
59+
val output = value.getMemberValueStr("output")
60+
val domain = value.getMemberValueStr("domain")
61+
val type = value.getMemberValueStr("type")
6262

6363
if (id != null) {
6464
val idPredicate = BiPredicate<Identifier, JsonElement> { identifier, _ -> identifier.toString() == id }

src/main/kotlin/org/sandboxpowered/fabric/api/SandboxResourcePolyglotContext.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class SandboxResourcePolyglotContext(private val resource: String, private val s
2323
@Export
2424
fun emit(event: String, vararg args: Any) {
2525
if (event.contains(':')) scriptLoader.emitEventToAll(event, *args)
26-
else scriptLoader.emitEventTo(resource, event, *args)
26+
else scriptLoader.emitEventTo(resource, event, args = args)
2727
}
2828

2929
@Export
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.sandboxpowered.fabric.api.item
2+
3+
import com.google.common.collect.ImmutableMap
4+
import net.minecraft.item.Item
5+
import net.minecraft.util.Identifier
6+
import net.minecraft.util.Rarity
7+
import net.minecraft.util.registry.Registry
8+
import org.graalvm.polyglot.Value
9+
import org.sandboxpowered.fabric.impl.item.PolyglotItem
10+
import org.sandboxpowered.fabric.util.getMemberValue
11+
import org.sandboxpowered.fabric.util.getMemberValueInt
12+
import org.sandboxpowered.fabric.util.getMemberValueStr
13+
import org.sandboxpowered.fabric.util.set
14+
15+
class PolyglotGlobalItemManager {
16+
private val archetypeMap: Map<String, (Value) -> Item>
17+
18+
init {
19+
val builder = ImmutableMap.builder<String, (Value) -> Item>()
20+
21+
builder["item"] = { PolyglotItem(itemPropertiesToSettings(it), it) }
22+
23+
archetypeMap = builder.build()
24+
}
25+
26+
private fun itemPropertiesToSettings(value: Value): Item.Settings {
27+
val settings = Item.Settings()
28+
if (!value.hasMember("properties")) return settings
29+
val properties = value.getMember("properties")
30+
properties.getMemberValueInt("maxDamage")?.let(settings::maxDamage)
31+
properties.getMemberValueInt("maxCount")?.let(settings::maxCount)
32+
properties.getMemberValueStr("rarity")?.let {
33+
settings.rarity(stringToRarity(it))
34+
}
35+
return settings
36+
}
37+
38+
private fun stringToRarity(rarity: String): Rarity = when (rarity) {
39+
"uncommon" -> Rarity.UNCOMMON
40+
"rare" -> Rarity.RARE
41+
"epic" -> Rarity.EPIC
42+
else -> Rarity.COMMON
43+
}
44+
45+
fun addItem(identifier: Identifier, value: Value) {
46+
val archetype = value.getMemberValue("archetype", "item")
47+
if (archetype !in archetypeMap) throw UnsupportedOperationException("Unknown item archetype [$archetype]")
48+
val item = archetypeMap[archetype]!!(value)
49+
Registry.register(Registry.ITEM, identifier, item)
50+
}
51+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.sandboxpowered.fabric.api.item
2+
3+
import net.minecraft.util.Identifier
4+
import org.graalvm.polyglot.HostAccess.Export
5+
import org.graalvm.polyglot.Value
6+
7+
class PolyglotItemManager(private val domain: String, private val global: PolyglotGlobalItemManager) {
8+
@Export
9+
fun add(id: String, value: Value) {
10+
if (!value.hasMembers()) throw UnsupportedOperationException("Unsupported value for item registration")
11+
global.addItem(Identifier(domain, id), value)
12+
}
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.sandboxpowered.fabric.impl.item
2+
3+
import net.minecraft.item.Item
4+
import org.graalvm.polyglot.Value
5+
6+
class PolyglotItem(settings: Settings, value: Value) : Item(settings) {
7+
8+
}

src/main/kotlin/org/sandboxpowered/fabric/loading/SandboxLoader.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ class SandboxLoader {
8787

8888
log.info("Loaded ${addons.size} resources")
8989

90+
resourceContent.keys.forEach {
91+
polyglotLoader.emitEventTo(it, "items", false, polyglotLoader.getItemManager(it))
92+
}
93+
9094
if (side == Side.SERVER) {
9195
val json = JsonObject()
9296

src/main/kotlin/org/sandboxpowered/fabric/scripting/PolyglotScriptLoader.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ import org.graalvm.polyglot.Context
44
import org.graalvm.polyglot.HostAccess
55
import org.graalvm.polyglot.Source
66
import org.sandboxpowered.fabric.api.SandboxResourcePolyglotContext
7+
import org.sandboxpowered.fabric.api.item.PolyglotGlobalItemManager
8+
import org.sandboxpowered.fabric.api.item.PolyglotItemManager
79
import org.sandboxpowered.fabric.scripting.polyglot.PolyglotFileSystem
810
import java.util.concurrent.Executors
911
import java.util.concurrent.TimeUnit
1012
import java.util.concurrent.TimeoutException
1113

1214
class PolyglotScriptLoader {
1315
private val executor = Executors.newSingleThreadExecutor()
14-
private val scriptContext: MutableMap<String, MutableMap<String, Context>> = HashMap()
15-
private val polyglotContext: MutableMap<String, SandboxResourcePolyglotContext> = HashMap()
16+
private val scriptContext: MutableMap<String, MutableMap<String, Context>> = hashMapOf()
17+
private val polyglotContext: MutableMap<String, SandboxResourcePolyglotContext> = hashMapOf()
18+
private val globalItemManager = PolyglotGlobalItemManager()
19+
private val itemManager: MutableMap<String, PolyglotItemManager> = hashMapOf()
1620

1721
private fun buildContext(): Context = Context.newBuilder("js", "python")
1822
.allowExperimentalOptions(true)
@@ -24,19 +28,22 @@ class PolyglotScriptLoader {
2428
return scriptContext.computeIfAbsent(resource) { HashMap() }
2529
}
2630

27-
private fun getPolyglotContext(resource: String): SandboxResourcePolyglotContext {
28-
return polyglotContext.computeIfAbsent(resource) { SandboxResourcePolyglotContext(it, this) }
29-
}
31+
private fun getPolyglotContext(resource: String): SandboxResourcePolyglotContext =
32+
polyglotContext.computeIfAbsent(resource) { SandboxResourcePolyglotContext(it, this) }
33+
34+
fun getItemManager(resource: String): PolyglotItemManager =
35+
itemManager.computeIfAbsent(resource) { PolyglotItemManager(it, globalItemManager) }
3036

3137
fun emitEventToAll(event: String, vararg args: Any) {
3238
polyglotContext.values.forEach { context ->
3339
context.event(event) { it(args) }
3440
}
3541
}
3642

37-
fun emitEventTo(resource: String, event: String, vararg args: Any) {
43+
fun emitEventTo(resource: String, event: String, emitToAll: Boolean = true, vararg args: Any) {
3844
polyglotContext[resource]?.event(event) { it(args) }
39-
emitEventToAll(resource, "$resource:$event", *args)
45+
if (emitToAll)
46+
emitEventToAll(resource, "$resource:$event", *args)
4047
}
4148

4249
fun loadScriptContext(resource: String, scriptSource: Source) {

src/main/kotlin/org/sandboxpowered/fabric/util/PolyglotUtil.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,18 @@ import com.google.gson.*
44
import org.graalvm.polyglot.Value
55

66

7-
fun Value.getMemberValue(identifier: String): String? =
7+
fun Value.getMemberValueStr(identifier: String): String? =
88
if (hasMember(identifier)) getMember(identifier).asString() else null
99

10+
fun Value.getMemberValue(identifier: String, default: String): String =
11+
if (hasMember(identifier)) getMember(identifier).asString() else default
12+
13+
fun Value.getMemberValueInt(identifier: String): Int? =
14+
if (hasMember(identifier)) getMember(identifier).asInt() else null
15+
16+
fun Value.getMemberValue(identifier: String, default: Int): Int =
17+
if (hasMember(identifier)) getMember(identifier).asInt() else default
18+
1019
fun Value.toJSON(): JsonElement = when {
1120
hasArrayElements() -> JsonArray().apply {
1221
(0 until arraySize)

src/main/kotlin/org/sandboxpowered/fabric/util/map.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.sandboxpowered.fabric.util
22

3+
import com.google.common.collect.ImmutableMap
4+
35
/**
46
* Removes elements from this map if the predicate returns true
57
*/
@@ -9,4 +11,8 @@ inline fun <K, V> MutableMap<K, V>.removeIf(predicate: (K, V) -> Boolean) {
911
val (k, v) = iter.next()
1012
if (predicate(k, v)) iter.remove()
1113
}
14+
}
15+
16+
operator fun <K, V> ImmutableMap.Builder<K, V>.set(key: K, value: V): ImmutableMap.Builder<K, V> {
17+
return put(key, value)
1218
}

0 commit comments

Comments
 (0)