CraftTweaker

CraftTweaker

151M Downloads

CraftTweaker fails to use arrays properly in some cases

twothe opened this issue ยท 8 comments

commented

Issue Description:

When attempting to check if a string is in an array, CraftTweaker fails with an error message.

What happens:

In the script below I am trying to make Hoppers unplaceable. The script works fine until I include the has check for blacklisted blocks, at which point the script fails with an error message.

Note that using a direct string comparison like if ("minecraft:hopper" == blockID) works fine. My assumption is that the inline function cannot see the outside blacklist definition, while it should.

What you expected to happen:

That the script would actually work.

Script used (Please Pastebin or gist your script, posting an unpasted or ungist'd script will automatically close this issue):

val blacklistedBlocks as string[] = [
  "minecraft:hopper"
] as string[];

events.onBlockPlace(function(event as crafttweaker.event.BlockPlaceEvent) {
  val blockID as string = (event.current.block.definition.id + (event.current.block.meta == 0? "" : ":"+event.current.block.meta)) as string;
  if (blacklistedBlocks has blockID) {
    event.cancel();
    event.player.sendChat("This block cannot be placed.");
  }
});

crafttweaker.log file (Please Pastebin or gist your file, posting an unpasted or ungist'd file will automatically close this issue):

[INITIALIZATION][CLIENT][INFO] [crafttweaker | SIDE_CLIENT]: Loading Script: {[0:crafttweaker]: 02_rules\Unplaceables.zs}
[INITIALIZATION][CLIENT][ERROR] [crafttweaker]: Error executing {[0:crafttweaker]: 02_rules\Unplaceables.zs}: Bad local variable type
Exception Details:
  Location:
    ZenClassCrafttweaker02_rules\Unplaceables5.handle(Lcrafttweaker/api/event/BlockPlaceEvent;)V @109: aload
  Reason:
    Type top (current frame, locals[5]) is not assignable to reference type
  Current Frame:
    bci: @109
    flags: { }
    locals: { 'ZenClassCrafttweaker02_rules\Unplaceables5', 'crafttweaker/api/event/BlockPlaceEvent', top, 'java/lang/String' }
    stack: { }
  Bytecode:
    0x0000000: bb00 0f59 b700 102b b900 1601 00b9 001c
    0x0000010: 0100 b900 2201 00b9 0028 0100 b600 2c2b
    0x0000020: b900 1601 00b9 001c 0100 b900 3001 0012
    0x0000030: 319f 0007 03a7 0004 0499 0008 1233 a700
    0x0000040: 28bb 000f 59b7 0010 1235 b600 2c2b b900
    0x0000050: 1601 00b9 001c 0100 b900 3001 00b8 003b
    0x0000060: b600 2cb6 003d b600 2cb6 003d 4e19 052d
    0x0000070: b800 4399 0016 2bb9 0048 0100 2bb9 004c
    0x0000080: 0100 124e b900 5402 00b1               
  Stackmap Table:
    same_locals_1_stack_item_frame(@56,Object[#15])
    full_frame(@57,{Object[#2],Object[#18]},{Object[#15],Integer})
    same_locals_1_stack_item_frame(@65,Object[#15])
    full_frame(@102,{Object[#2],Object[#18]},{Object[#15],Object[#86]})
    append_frame(@137,Top,Object[#86])

java.lang.VerifyError: Bad local variable type
Exception Details:
  Location:
    ZenClassCrafttweaker02_rules\Unplaceables5.handle(Lcrafttweaker/api/event/BlockPlaceEvent;)V @109: aload
  Reason:
    Type top (current frame, locals[5]) is not assignable to reference type
  Current Frame:
    bci: @109
    flags: { }
    locals: { 'ZenClassCrafttweaker02_rules\Unplaceables5', 'crafttweaker/api/event/BlockPlaceEvent', top, 'java/lang/String' }
    stack: { }
  Bytecode:
    0x0000000: bb00 0f59 b700 102b b900 1601 00b9 001c
    0x0000010: 0100 b900 2201 00b9 0028 0100 b600 2c2b
    0x0000020: b900 1601 00b9 001c 0100 b900 3001 0012
    0x0000030: 319f 0007 03a7 0004 0499 0008 1233 a700
    0x0000040: 28bb 000f 59b7 0010 1235 b600 2c2b b900
    0x0000050: 1601 00b9 001c 0100 b900 3001 00b8 003b
    0x0000060: b600 2cb6 003d b600 2cb6 003d 4e19 052d
    0x0000070: b800 4399 0016 2bb9 0048 0100 2bb9 004c
    0x0000080: 0100 124e b900 5402 00b1               
  Stackmap Table:
    same_locals_1_stack_item_frame(@56,Object[#15])
    full_frame(@57,{Object[#2],Object[#18]},{Object[#15],Integer})
    same_locals_1_stack_item_frame(@65,Object[#15])
    full_frame(@102,{Object[#2],Object[#18]},{Object[#15],Object[#86]})
    append_frame(@137,Top,Object[#86])

	at 02_rules\Unplaceables.__script__(02_rules\Unplaceables.zs:6)
	at __ZenMain__.run(02_rules\Unplaceables)
	at crafttweaker.runtime.CrTTweaker.loadScript(CrTTweaker.java:225)
	at crafttweaker.runtime.CrTTweaker.loadScript(CrTTweaker.java:105)
	at crafttweaker.mc1120.events.CommonEventHandler.registerRecipes(CommonEventHandler.java:73)
	at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_376_CommonEventHandler_registerRecipes_Register.invoke(.dynamic)
	at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90)
	at net.minecraftforge.fml.common.eventhandler.EventBus$1.invoke(EventBus.java:144)
	at com.github.terminatornl.laggoggles.tickcentral.EventBusTransformer.redirectEvent(EventBusTransformer.java:67)
	at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:182)
	at net.minecraftforge.registries.GameData.fireRegistryEvents(GameData.java:857)
	at net.minecraftforge.common.crafting.CraftingHelper.loadRecipes(CraftingHelper.java:636)
	at net.minecraftforge.fml.common.Loader.initializeMods(Loader.java:747)
	at net.minecraftforge.fml.client.FMLClientHandler.finishMinecraftLoading(FMLClientHandler.java:336)
	at net.minecraft.client.Minecraft.func_71384_a(Minecraft.java:535)
	at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:378)
	at net.minecraft.client.main.Main.main(SourceFile:123)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
	at net.minecraft.launchwrapper.Launch.main(Launch.java:28)

Affected Versions (Do not use "latest"):

  • Minecraft: 1.12
  • Forge: 2854
  • Crafttweaker: 4.1.20.570
  • Using a server: no
  • If yes, does the client have the exact same scripts?

Your most recent log file where the issue was present:

crafttweaker.log

commented

Your issue is that you are accessing a variable that was declared outside of the function.
Atm, that issue would arise even for a normal var, if you moved it otuside the event handler.

Let me just change something that allows capturing in generic lambdas as well...

commented

In that case, I will use static, instead of val.

commented

That would fix it.
However, that issue should be resolved in the latest CF release.

Could you check that and give feedback here?

commented

I thought function can't use variables outside of function is a feature...
And script still can't load suceessfully...

Crafttweaker : 4.1.20.573
I use @twothe 's script above.

Crafttweaker.log
https://gist.github.com/friendlyhj/28d3fd1ca0bda2e63920abd039990174

commented

Eh...
I maybe should've pushed this to the correct branches lol

Capturing was added a while back as the errors it threw were well... that.

commented

Ok, it works.
But I will still use static (

commented

Thank you for the quick fix. =)