[1.12.2] Problem casting java.util.List to array
See original GitHub issueIntro:
Issue Description:
It seems like java.util.List isn’t properly casted to array in CraftTweaker when assigning a List to an array. Trying to iterate over this array will produce “Bad type on operand stack in array length” exception.
What happens:
For example, the getter IEntityLiving#activePotionEffects return a List instead of an array in the java source code. This piece of code is perfectly ok:
var effects = player.activePotionEffects;
for effect in effects {
player.sendChat(effect.potion.name + " Duration: " + effect.duration);
}
This works either:
var effects as IPotionEffect[] = player.activePotionEffects as IPotionEffect[];
for effect in effects {
player.sendChat(effect.potion.name + " Duration: " + effect.duration);
}
However, this doesn’t work and will produce an error when trying to iterate over it: (Note: /ct syntax can’t find this error)
var effects as IPotionEffect[] = player.activePotionEffects;
for effect in effects {
player.sendChat(effect.potion.name + " Duration: " + effect.duration);
}
What you expected to happen:
The 3rd piece of code should also work, as assigning a List to an array in ZenScript should be allowed and produce no problem.
Script used:
This is the entire script used for testing, although all other parts of the code has nothing to do with the problem. The errored code is already presented as code blocks above. https://paste.ubuntu.com/p/FmX467XYQH/
crafttweaker.log file:
This is the log with the error information on the 3rd piece of code. https://paste.ubuntu.com/p/jWwkns8nq6/
Environment:
- Minecraft Version:
- Forge Version: 14.23.5.2854
- CraftTweaker Version: CraftTweaker2-1.12-4.1.20.617
- Are you using a server: no
- If yes, does the client have the exact same scripts?
Game log:
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (1 by maintainers)
Top GitHub Comments
Closing this then.
As a general rule of thumb, if you are declaring and initializing variables in one step, always put the
as type
at least on the RightHandSide of the=
. As you have already seen you can also put it on both sides if you want to be sure, but the RHS usually should be enough. Using the cast on the LHS only is unchecked in that case, so only use it when you absolutely have to.To sum up:
In scenarios where you already checked the type. For example when in combination with an instanceof.
Think of something like