#How do I change a Vanilla Rarity's color?

185 messages · Page 1 of 1 (latest)

eager nimbus
#

I have this (next message) script to make new rarities, but I wanna change pre-existing ones. is that doable with kubejs?

tawdry nicheBOT
#

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

eager nimbus
#
const rarityName = $Rarity.create("Name", Color.WHITE)
chrome cairn
#
var color = $Rarity.getClass().getDeclaredField("color"); // could be also directly  $Rarity.getDeclaredField("color")
color.setAccessible(true);
color.set(Rarity.COMMON, Color.WHITE); // if color is a chatformatting, idk you have to check
#

not tested, but reflection should work on enums too

eager nimbus
#

maneeeee,,, it ain wokr

river grail
#

??neversay

vernal stagBOT
# river grail ??neversay

Never say 'it crashed', 'it errored', or 'it didn't work' without providing the full crash report, log, and scripts!

eager nimbus
#

yea ik hold on im loading up the thjng gain

#
const $Rarity = Java.loadClass("net.minecraft.world.item.Rarity")
const rarityLegendary = $Rarity.create("Legendary", Color.GOLD)
const rarityMythical = $Rarity.create("Mythical", Color.RED)
const rarityUnknown = $Rarity.create("Unknown", Color.BLACK)


var color = $Rarity.getClass().getDeclaredField("color"); // could be also directly  $Rarity.getDeclaredField("color")
color.setAccessible(true);
color.set(Rarity.COMMON, Color.GRAY); // if color is a chatformatting, idk you have to check

// Rarity.set("COMMON", Color.GRAY); // if color is a chatformatting, idk you have to check

This is the full code

vernal stagBOT
#

Paste version of message.txt from @eager nimbus

chrome cairn
#

did you tried what I also wrote in the comment? $Rarity.getDeclaredField("color") ?

eager nimbus
#

ye also gave an error i think

chrome cairn
#

which error?

#

cmon please KekDoggo

eager nimbus
#

1 sec

river grail
vernal stagBOT
eager nimbus
#

??ok

vernal stagBOT
#

Paste version of message.txt from @eager nimbus

chrome cairn
#

Interesting. With this, the .getClass() should have worked at least hmm

#

can you do console.log(Object.keys($Rarity)) and give the the output from it?

eager nimbus
#

wwhat does it output tho

chrome cairn
#

nvm i got it

#

at least for fabric

#

forge has probably a patch for that hmm

eager nimbus
#

aw mane

#

if you figure it out let me know tho

uncut kestrel
#

Oh classic stuff
@chrome cairn I remember I had to load Class and later use $Class.forName()
to actually have the class

chrome cairn
#

but shouldnt load class load the class? susge

uncut kestrel
#

exactly

#

but it doesn't

#

maybe it is is some Rhino wrapped class?

chrome cairn
#

recipe.js#4: Failed to load Java class 'java.lang.reflect.Proxy': Class is not allowed by class filter!

chrome cairn
#

fuck you rhino. It does not let me cast Function to UnaryOperator

uncut kestrel
#

O.o

#

you can try a helper method

#

UtilsJS.makeFunction? or something

#

let me check

#

UtilsJS.makeFunctionProxy

chrome cairn
#

how do I get the ScriptType?

chrome cairn
#

oh yeah I forgot

#

lol

uncut kestrel
#

i wonder how you endup on unary stuff for this Rarity stuff pepethink

chrome cairn
#

Cannot find function makeFunctionProxy in object class dev.latvian.mods.kubejs.util.UtilsJS. fuck u rhino

uncut kestrel
#

the f?

#

did you miss the args?

chrome cairn
#

Oh wait I load utilsjs via forName. Maybe now it needs loadClass :')

uncut kestrel
#

yes, UtilsJS just do normally

#

also the class I passed as arg I also loaded normally

#

didnt use forName

chrome cairn
#

okay now fixing ambiguous calls because of type wrapping. U remember the format I have to put it to select specific methods?

uncut kestrel
#

grab from the error message and use

#

["what you grabbed"]

#

paste the ambiguous msg

chrome cairn
#
function setColor(rarity, color) {
    let field = rarity.getClass().getDeclaredField("styleModifier")
    field.setAccessible(true);
    field.set(rarity, $UtilsJS.makeFunctionProxy("server", UnaryOperator, (style) => {
        return style["withColor(net.minecraft.ChatFormatting)"](color);
    }));
}
#

lets test

uncut kestrel
chrome cairn
#

No errors but no color changes mmh

#

nvm

#

okay I made everything inside server, I should probably move it to a good client side event

uncut kestrel
#

if nothing works you/he can just loop on the items at like, item modification event and grab the rarity of common and just set the "new" common color

chrome cairn
#

I mean, I just modify classes directly. SHould be valid to not even use any event

#
const Rarity = Java.loadClass("net.minecraft.world.item.Rarity");
const UnaryOperator = Java.loadClass("java.util.function.UnaryOperator");
const UtilsJS = Java.loadClass("dev.latvian.mods.kubejs.util.UtilsJS");

function setColor(rarity, color) {
    let field = rarity.getClass().getDeclaredField("styleModifier")
    field.setAccessible(true);
    field.set(rarity, UtilsJS.makeFunctionProxy("client", UnaryOperator, (style) => {
        return style["withColor(net.minecraft.ChatFormatting)"](color);
    }));
}

setColor(Rarity.COMMON, Color.RED)
setColor(Rarity.UNCOMMON, Color.BLACK)
setColor(Rarity.RARE, Color.GOLD)
setColor(Rarity.EPIC, Color.BLUE)
#

just in client scripts @eager nimbus

#

ty @uncut kestrel

uncut kestrel
#

mad lad

chrome cairn
#

with the style thing you can also do italic, bold etc.

eager nimbus
#

Neat, Thank you. I will try it right now

chrome cairn
#
const Rarity = Java.loadClass("net.minecraft.world.item.Rarity");
const UnaryOperator = Java.loadClass("java.util.function.UnaryOperator");
const UtilsJS = Java.loadClass("dev.latvian.mods.kubejs.util.UtilsJS");

function colorStyle(style, color) {
    return style["withColor(net.minecraft.ChatFormatting)"](color);
}

function setStyle(rarity, styleCallback) {
    let field = rarity.getClass().getDeclaredField("styleModifier")
    field.setAccessible(true);
    field.set(rarity, UtilsJS.makeFunctionProxy("client", UnaryOperator, styleCallback));
}

function setColor(rarity, color) {
    setStyle(rarity, style => colorStyle(style, color));
}

setStyle(Rarity.COMMON, (style) => colorStyle(style, Color.RED).withObfuscated(true))
setColor(Rarity.UNCOMMON, Color.BLACK)
setColor(Rarity.RARE, Color.GOLD)
setColor(Rarity.EPIC, Color.BLUE)
eager nimbus
#

My game's crashing for some reason idk

vernal stagBOT
#

Paste version of crash-2024-01-24_16.58.25-server.txt from @eager nimbus

chrome cairn
#

how does your code looks like?

#

because u are passing an empty object somewhere

#

line 9

eager nimbus
#

oh

chrome cairn
#

show the full code, or did you just copied mine without changes?

eager nimbus
#

yeah..

#
const Rarity = Java.loadClass("net.minecraft.world.item.Rarity");
const UnaryOperator = Java.loadClass("java.util.function.UnaryOperator");
const UtilsJS = Java.loadClass("dev.latvian.mods.kubejs.util.UtilsJS");

function setColor(rarity, color) {
    let field = rarity.getClass().getDeclaredField("styleModifier")
    field.setAccessible(true);
    field.set(rarity, UtilsJS.makeFunctionProxy("client", UnaryOperator, (style) => {
        return style["withColor(net.minecraft.ChatFormatting)"](color);
    }));
}

setColor(Rarity.COMMON, Color.GRAY)
// setColor(Rarity.UNCOMMON, Color.BLACK)
// setColor(Rarity.RARE, Color.GOLD)
// setColor(Rarity.EPIC, Color.BLUE)```
chrome cairn
#

when does it crash? and is it a client script?

eager nimbus
river grail
#

I feel like it might be Color.GREY then hmmm

chrome cairn
#

Color exist from what I see

uncut kestrel
chrome cairn
#

oh it's GRAY

eager nimbus
#

its written gray

#

ye

river grail
#

nvm then heh

chrome cairn
#

is it some UK US thing?

eager nimbus
#

its... the one in minecraft

river grail
eager nimbus
#

why do them brits gotta try to be different!!

river grail
#

gray is in america and grey is in britain

manic lantern
#

grei

uncut kestrel
#

@eager nimbus kubejs version and rhino are all up to latest?

eager nimbus
#

Yes

#

Rhino 2001.2.2-build.18+forge

KubeJS 2001.6.4-build.127+forge

chrome cairn
#

does it work now?

eager nimbus
#

?

#

I didn't change anything

#

It was always gray

chrome cairn
#

oh

#

donald confuses me ASmadDannii

eager nimbus
#

@donald stop confusing him please

chrome cairn
#

my versions but I doubt thats the reason

manic lantern
#

its not because he doesnt have Color class imported is it?

#

or is that a constant already in base kubejs

chrome cairn
#

Color is bound

manic lantern
#

fair

uncut kestrel
#

I'm trying on my instance

chrome cairn
#

I mean im in dev environment so I dont have obfuscation. But I doubt this is the problem too because forge does not obfs classes

#

oh wait

#

but it obfuscates fields

uncut kestrel
#

yeah crashed too

#

when I was about to hover the item

chrome cairn
#

at least what I see from here

manic lantern
#

thats what i was gonna say next too^

#

style.withColor

chrome cairn
#

but wait², that shoul not matter because its a call through rhino. The reflection part is calling a forge field which should not be obfuscated

#

Otherwise the exception should be FieldNotFound

uncut kestrel
#

console.printObject(style)
doing this to test

#

to see wtf is this

chrome cairn
#

PRESS ENTER TO ENABLE THE NARRATOR

#

pls mojang

#

always heart attack

uncut kestrel
#
 === net.minecraft.network.chat.Style ===
 = toString() =
 > {}
 = hashCode() =
 > f449711f
 
 === net.minecraft.network.chat.Style ===
 = Parent class =
 > java.lang.Object
 = Variables and Functions =
 > function equals(Object): boolean
 > function hashCode(): int
 > function m_131135_(): TextColor
 > function m_131136_(Boolean): Style
 > function m_131138_(String): Style
 > function m_131140_(ChatFormatting): Style
 > function m_131142_(ClickEvent): Style
 > function m_131144_(HoverEvent): Style
 > function m_131146_(Style): Style
 > function m_131148_(TextColor): Style
 > function m_131150_(ResourceLocation): Style
 > function m_131152_(ChatFormatting): Style
 > function m_131154_(): boolean
 > function m_131155_(Boolean): Style
 > function m_131157_(ChatFormatting): Style
 > function m_131161_(): boolean
 > function m_131162_(Boolean): Style
 > function m_131164_(ChatFormatting): Style
 > function m_131168_(): boolean
 > function m_131171_(): boolean
 > function m_131176_(): boolean
 > function m_131179_(): boolean
 > function m_131182_(): ClickEvent
 > function m_131186_(): HoverEvent
 > function m_131189_(): String
 > function m_131192_(): ResourceLocation
 > function m_178520_(int): Style
 > function m_178522_(Boolean): Style
 > function m_178524_(Boolean): Style
 > function toString(): String
chrome cairn
#

why the heck is style {}

#

and why the fields are obfuscated

#

rhino should map them Madge

#

okay it can't even find m_131152_ in it

river grail
#

did you expect rhino to work pepelaugh

chrome cairn
#

am I dumb, what do i miss

#

class net.minecraft.network.chat.Style [java.lang.Class] its the correct class

#

interesting

#

style["withColor(net.minecraft.ChatFormatting)"](color); the typed call seems to be invalid here

#

style["withColor"](color); gives me back the ambig call, but using them will result into can't find NotLikeDuck

uncut kestrel
#

TypeError: Cannot find function m_131140_(net.minecraft.ChatFormatting) in object {}.

#

rhino pls

chrome cairn
#

also why it does cry only about the two methods, when there are more methods for color

#

wait is it because with this, rhino does not type wrap anymore?

uncut kestrel
chrome cairn
#

what was the error?

uncut kestrel
#

Rhino ? dunno

chrome cairn
#

but what did you change confused

uncut kestrel
#
const Rarity = Java.loadClass("net.minecraft.world.item.Rarity");
const UnaryOperator = Java.loadClass("java.util.function.UnaryOperator");
const UtilsJS = Java.loadClass("dev.latvian.mods.kubejs.util.UtilsJS");

function setColor(rarity, color) {
    
    let field = rarity.getClass().getDeclaredField("styleModifier")
    field.setAccessible(true);
    field.set(rarity, UtilsJS.makeFunctionProxy("client", UnaryOperator, (style) => {
        let method = style.getClass().getDeclaredMethod("m_131140_", color.getClass())
        return method.invoke(style, color);
    }));
}

setColor(Rarity.COMMON, Color.GRAY)
chrome cairn
#

yes was going to do the same. Hopefully style here is always a new style heh

#

otherwise u are changing some global stuff

#

Wrapped java.lang.NoSuchMethodException: net.minecraft.network.chat.Style.m_131140_() now it's joking

uncut kestrel
#

😐

chrome cairn
#
 public net.minecraft.network.chat.Style net.minecraft.network.chat.Style.m_131157_(net.minecraft.ChatFormatting) [java.lang.reflect.Method]
[14:44:04] [INFO] example.js#9: public net.minecraft.network.chat.Style net.minecraft.network.chat.Style.m_131152_(net.minecraft.ChatFormatting[]) [java.lang.reflect.Method]
public net.minecraft.network.chat.Style net.minecraft.network.chat.Style.m_131140_(net.minecraft.ChatFormatting) [java.lang.reflect.Method]
#

I wonder where the other two come from

#

ah it's applyFormat

uncut kestrel
#

i think it was applyFormat and some others

chrome cairn
#

applyFormat actually works

uncut kestrel
#

no reflection bs needed?

chrome cairn
#

function setColor(rarity, color) {
    let field = rarity.getClass().getDeclaredField("styleModifier")
    field.setAccessible(true);
    field.set(rarity, UtilsJS.makeFunctionProxy("client", UnaryOperator, (style) => {
        return style.applyFormat(color);
    }));
}
// setColor(Rarity.COMMON, Color.GRAY)

// let style = createStyle(ChatFormatting.GOLD);
setColor(Rarity.COMMON, Color.BLACK);
// setColor(Rarity.UNCOMMON, Color.BLACK)
// setColor(Rarity.RARE, Color.GOLD)
// setColor(Rarity.EPIC, Color.BLUE)
#

I would probably go with creating a new style directly and store it, instead of apply in the callback

const Rarity = Java.loadClass("net.minecraft.world.item.Rarity");
const UnaryOperator = Java.loadClass("java.util.function.UnaryOperator");
const UtilsJS = Java.loadClass("dev.latvian.mods.kubejs.util.UtilsJS");
const ChatFormatting = Java.loadClass("net.minecraft.ChatFormatting");
const Style = Java.loadClass("net.minecraft.network.chat.Style");

function setStyle(rarity, style) {
    let field = rarity.getClass().getDeclaredField("styleModifier")
    field.setAccessible(true);
    field.set(rarity, UtilsJS.makeFunctionProxy("client", UnaryOperator, ($) => {
        return style;
    }));
}


setStyle(Rarity.COMMON, Style.EMPTY.applyFormat(Color.BLUE));
setStyle(Rarity.UNCOMMON, Style.EMPTY.applyFormat(Color.GOLD).withObfuscated(true));
#

gives u a bit more freedom to directly apply more styles to it if wanted

uncut kestrel
#

👏

eager nimbus
#

pogy. thank you for the help

tawdry nicheBOT
#

Ticket closed!

acoustic wigeon
#

The startup seems to run into a roadblock when i use this script

const UnaryOperator = Java.loadClass("java.util.function.UnaryOperator");
const UtilsJS = Java.loadClass("dev.latvian.mods.kubejs.util.UtilsJS");
const Style = Java.loadClass("net.minecraft.network.chat.Style");

function setColor(rarity, color) {
    setStyle(rarity, Style.EMPTY.applyFormat(color));
}

function setStyle(rarity, style) {
    let field = rarity.getClass().getDeclaredField("styleModifier");
    field.setAccessible(true);
    field.set(
        rarity,
        UtilsJS.makeFunctionProxy("client", UnaryOperator, ($) => {
            return style;
        })
    );
}

setColor(Rarity.UNCOMMON, Color.GREEN);
setColor(Rarity.RARE, Color.BLUE);```
vernal stagBOT
#

Paste version of message.txt from @acoustic wigeon

acoustic wigeon
#

latest.log

uncut kestrel
#

@acoustic wigeon this is client only, not startup