#How do I change a Vanilla Rarity's color?
185 messages · Page 1 of 1 (latest)
Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!
const rarityName = $Rarity.create("Name", Color.WHITE)
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
maneeeee,,, it ain wokr
??neversay
Never say 'it crashed', 'it errored', or 'it didn't work' without providing the full crash report, log, and scripts!
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
Paste version of message.txt from @eager nimbus
did you tried what I also wrote in the comment? $Rarity.getDeclaredField("color") ?
ye also gave an error i think
1 sec
??neversay 
Never say 'it crashed', 'it errored', or 'it didn't work' without providing the full crash report, log, and scripts!
Paste version of message.txt from @eager nimbus
Interesting. With this, the .getClass() should have worked at least 
can you do console.log(Object.keys($Rarity)) and give the the output from it?
yop
wwhat does it output tho
Oh classic stuff
@chrome cairn I remember I had to load Class and later use $Class.forName()
to actually have the class
but shouldnt load class load the class? 
recipe.js#4: Failed to load Java class 'java.lang.reflect.Proxy': Class is not allowed by class filter!

lemme try dis
fuck you rhino. It does not let me cast Function to UnaryOperator
O.o
you can try a helper method
UtilsJS.makeFunction? or something
let me check
UtilsJS.makeFunctionProxy
how do I get the ScriptType?
i wonder how you endup on unary stuff for this Rarity stuff 
Cannot find function makeFunctionProxy in object class dev.latvian.mods.kubejs.util.UtilsJS. fuck u rhino
Oh wait I load utilsjs via forName. Maybe now it needs loadClass :')
yes, UtilsJS just do normally
also the class I passed as arg I also loaded normally
didnt use forName
okay now fixing ambiguous calls because of type wrapping. U remember the format I have to put it to select specific methods?
grab from the error message and use
["what you grabbed"]
paste the ambiguous msg
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

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
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
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
with the style thing you can also do italic, bold etc.
Neat, Thank you. I will try it right now
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)
Paste version of crash-2024-01-24_16.58.25-server.txt from @eager nimbus
how does your code looks like?
because u are passing an empty object somewhere
line 9
show the full code, or did you just copied mine without changes?
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)```
when does it crash? and is it a client script?
World loading and yes
Color exist from what I see

idk bout dat
oh it's GRAY
nvm then 
is it some UK US thing?
its... the one in minecraft
ye, like color/colour 
why do them brits gotta try to be different!!
gray is in america and grey is in britain
grei
@eager nimbus kubejs version and rhino are all up to latest?
does it work now?
@donald stop confusing him please
my versions but I doubt thats the reason
my bad 
its not because he doesnt have Color class imported is it?
or is that a constant already in base kubejs
Color is bound
fair
I'm trying on my instance
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
can u try using style.method_10977(color), then u also dont need to handle ambigthingy
at least what I see from here
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
=== 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
why the heck is style {}
and why the fields are obfuscated
rhino should map them 
okay it can't even find m_131152_ in it
did you expect rhino to work 
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 
TypeError: Cannot find function m_131140_(net.minecraft.ChatFormatting) in object {}.
rhino pls
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?
what was the error?
Rhino ? dunno
but what did you change 
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)
yes was going to do the same. Hopefully style here is always a new style 
otherwise u are changing some global stuff
Wrapped java.lang.NoSuchMethodException: net.minecraft.network.chat.Style.m_131140_() now it's joking
😐
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
i think it was applyFormat and some others
applyFormat actually works
no reflection bs needed?
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
👏
pogy. thank you for the help
Ticket closed!
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);```
Paste version of message.txt from @acoustic wigeon
latest.log





