|
2 | 2 | title: Ambiguous Methods |
3 | 3 | --- |
4 | 4 |
|
| 5 | +## Ambiguous Methods |
5 | 6 | Sometimes in KJS, you run into ambiguous methods when calling functions. |
6 | 7 | This happens when there's multiple overloads (e.g. methods with the same name but different types) and KubeJS isn't sure which function to call with your arguments. |
7 | 8 |
|
@@ -52,5 +53,48 @@ GTCEuStartupEvents.registry('gtceu:machine', event => { |
52 | 53 | ``` |
53 | 54 |
|
54 | 55 | Because of the way javascript indexing works, `.foo` and `["foo"]` are the same thing, so you can just keep chaning your functions afterward, since it's just a "normal" builder method, just called in a more specific way. |
| 56 | + |
| 57 | +## Ambiguous Constructors |
| 58 | +This same problem can occur when trying to call a constructor. |
| 59 | +For example, when you do |
| 60 | +```js |
| 61 | +GTCEuStartupEvents.registry("gtceu:recipe_type", event => { |
| 62 | + event.create("unboxinator") |
| 63 | + .setProgressBar( |
| 64 | + new ResourceTexture("kubejs:textures/gui/progress_bar/progress_bar_stone_oreifier.png"), |
| 65 | + FillDirection.LEFT_TO_RIGHT |
| 66 | + ) |
| 67 | + // Rest of the recipe type |
| 68 | +}) |
| 69 | +``` |
| 70 | +You'd get the following error: |
| 71 | +``` |
| 72 | +dev.latvian.mods.rhino.EvaluatorException: The choice of Java constructor com.lowdragmc.lowdraglib.gui.texture.ResourceTexture matching JavaScript argument types (string) is ambiguous; candidate constructors are: |
| 73 | + ResourceTexture(net.minecraft.resources.ResourceLocation) |
| 74 | + ResourceTexture(java.lang.String) (startup_scripts:example.js#17) |
| 75 | +``` |
| 76 | + |
| 77 | +You would want to select one of the two, and this can be done in the following way: |
| 78 | +```js |
| 79 | +GTCEuStartupEvents.registry("gtceu:recipe_type", event => { |
| 80 | + event.create("unboxinator") |
| 81 | + .setProgressBar( |
| 82 | + ResourceTexture["(java.lang.String)"]("kubejs:textures/gui/progress_bar/progress_bar_stone_oreifier.png"), |
| 83 | + FillDirection.LEFT_TO_RIGHT |
| 84 | + ) |
| 85 | + // Rest of the recipe type |
| 86 | +}) |
| 87 | +``` |
| 88 | +or |
| 89 | +```js |
| 90 | +GTCEuStartupEvents.registry("gtceu:recipe_type", event => { |
| 91 | + event.create("unboxinator") |
| 92 | + .setProgressBar( |
| 93 | + ResourceTexture["(net.minecraft.resources.ResourceLocation)"](new ResourceLocation("kubejs:textures/gui/progress_bar/progress_bar_stone_oreifier.png")), |
| 94 | + FillDirection.LEFT_TO_RIGHT |
| 95 | + ) |
| 96 | + // Rest of the recipe type |
| 97 | +}) |
| 98 | +``` |
55 | 99 | !!! Note |
56 | 100 | Generics don't exist in compiled code, so e.g. a call to `memoize(Supplier<T> delegate)` would turn into `["memoize(Supplier)"](...)` |
0 commit comments