#Convert InputItem to JSON

32 messages · Page 1 of 1 (latest)

prime stag
#

Trying to represent an InputItem / Ingredient w/ amount in json, but running into strange issues. InputItem#toJson() returns in the form { ingredient: {...}, count: 1 } when I need it to be { item: 'foo:bar' , count: 1} or { tag: '#foo:bar' , count: 1}.

This is what I'm currently trying, however addProperty keeps adding the value as a string instead of a number

let test = InputItem.of('2x minecraft:gold_ingot')
let test2 = test.ingredient.toJson().getAsJsonObject()
test2.addProperty('count', test.count)
console.log(test2)
//log output
[17:02:57] [INFO] server_scripts:recipes/hexerei.js:27: {count="2", item="minecraft:gold_ingot"} [java.util.HashMap]
junior plazaBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

winged dirge
#

whats the end goal might i ask?

prime stag
#

for use in a json recipe

#

specifically malum's favor_of_the_void recipe type which takes an ingredient with an amount as input
i'm trying to write a helper method to make it easier to make recipes for this type

west charm
#

What about...

let test = InputItem.of('2x minecraft:gold_ingot')
let test2 = test.ingredient.toJson().getAsJsonObject()
test2.addProperty('count', 0 + test.count)
console.log(test2)
prime stag
#

[21:37:50] [INFO] server_scripts:example.js:8: {count="2.0", item="minecraft:gold_ingot"} [java.util.HashMap]

#

this is my biggest gripe with weakly typed languages

west charm
#

It's the type wrapper prioritizing certain object types over others

#

I have a cursed idea

#

What about...

const $BIG_INT = Java.loadClass('java.math.BigInteger');

let test = InputItem.of('2x minecraft:gold_ingot')
let test2 = test.ingredient.toJson().getAsJsonObject()
test2.addProperty('count', new $BIG_INT(test.count))
console.log(test2)
prime stag
#

ooo fun an error

#
[21:42:58] [ERROR] ! #9: Error loading KubeJS script: server_scripts:example.js': The choice of Java method com.google.gson.JsonObject.addProperty matching JavaScript argument types (string,java.math.BigInteger) is ambiguous; candidate methods are: 
    void addProperty(java.lang.String,java.lang.String)
    void addProperty(java.lang.String,java.lang.Number) (server_scripts:example.js#9)
#

why couldn't you just use like... Integer instead of BigInt

west charm
#

I figured BigInt wouldn't be a candidate for string conversion

#

But apparently it is

prime stag
#

using integer gives the same error

gilded yarrow
#

can't you just specify the method to use

west charm
#
const $BIG_INT = Java.loadClass('java.math.BigInteger');

let test = InputItem.of('2x minecraft:gold_ingot')
let test2 = test.ingredient.toJson().getAsJsonObject()
test2["addProperty(java.lang.String,java.lang.Number)"]('count', new $BIG_INT(test.count))
console.log(test2);
#

That's what I was just about to try

#

But I always forget the specific override syntax

prime stag
#

ohh thats what that syntax means

west charm
#

Yeah, it's a direct reference to a method within Java

prime stag
#

yeah ok that works, using java.lang.Integer

west charm
#

Cool

prime stag
#

lemme see if i can get rid of the reflection

west charm
#

Should be able to

prime stag
#
let test = InputItem.of('2x minecraft:gold_ingot')
let test2 = test.ingredient.toJson().getAsJsonObject()
test2['addProperty(java.lang.String,java.lang.Number)']('count', test.count)
console.log(test2)

yeah just had to specify explicitly the method

#

this finally works

#

javascript moment

west charm
#

Interpreter is the real culprit here

#

The reason you can pass a string as a resource location is the same reason it can't figure out which method to use.
Rhino can convert a number to a string, and a string to a number.
So it can't decide which one you want it to do.