|
| 1 | +--- |
| 2 | +title: Ambiguous Methods |
| 3 | +--- |
| 4 | + |
| 5 | +Sometimes in KJS, you run into ambiguous methods when calling functions. |
| 6 | +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 | +For example, when you do: |
| 9 | +```js |
| 10 | + |
| 11 | +GTCEuStartupEvents.registry('gtceu:machine', event => { |
| 12 | + event.create('unboxinator', 'multiblock') |
| 13 | + .tooltips(Component.literal("I am a multiblock")) |
| 14 | + // Rest of the multiblock |
| 15 | +}) |
| 16 | +``` |
| 17 | + |
| 18 | +you'd get the error: |
| 19 | +``` |
| 20 | +Error in 'GTCEuStartupEvents.registry': The choice of Java method com.gregtechceu.gtceu.api.registry.registrate.MultiblockMachineBuilder.tooltips matching JavaScript argument types (net.minecraft.network.chat.MutableComponent) is ambiguous; candidate methods are: |
| 21 | + class com.gregtechceu.gtceu.api.registry.registrate.MachineBuilder tooltips(java.util.List) |
| 22 | + class com.gregtechceu.gtceu.api.registry.registrate.MachineBuilder tooltips(net.minecraft.network.chat.Component[]) |
| 23 | +``` |
| 24 | + |
| 25 | +In this case, there's ambiguity between the following 2 java functions: |
| 26 | +```java |
| 27 | + public MachineBuilder<DEFINITION> tooltips(@Nullable Component... components) { |
| 28 | + return tooltips(Arrays.asList(components)); |
| 29 | + } |
| 30 | + |
| 31 | + public MachineBuilder<DEFINITION> tooltips(List<? extends @Nullable Component> components) { |
| 32 | + tooltips.addAll(components.stream().filter(Objects::nonNull).toList()); |
| 33 | + return this; |
| 34 | + } |
| 35 | +``` |
| 36 | + |
| 37 | +You would want to select one of the two, and this can be done in the following way: |
| 38 | +```js |
| 39 | +GTCEuStartupEvents.registry('gtceu:machine', event => { |
| 40 | + event.create('unboxinator', 'multiblock') |
| 41 | + ["tooltips(java.util.List)"]([Component.literal("I am a multiblock")]) |
| 42 | + // Rest of the multiblock |
| 43 | +}) |
| 44 | +``` |
| 45 | +or |
| 46 | +```js |
| 47 | +GTCEuStartupEvents.registry('gtceu:machine', event => { |
| 48 | + event.create('unboxinator', 'multiblock') |
| 49 | + ["tooltips(net.minecraft.network.chat.Component[])"]([Component.literal("I am a multiblock")]) |
| 50 | + // Rest of the multiblock |
| 51 | +}) |
| 52 | +``` |
| 53 | + |
| 54 | +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. |
| 55 | +!!! Note |
| 56 | + 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