|
| 1 | +package org.cyclops.integratedscripting.gametest; |
| 2 | + |
| 3 | +import net.minecraft.core.BlockPos; |
| 4 | +import net.minecraft.gametest.framework.GameTest; |
| 5 | +import net.minecraft.gametest.framework.GameTestHelper; |
| 6 | +import net.minecraft.world.item.ItemStack; |
| 7 | +import net.neoforged.neoforge.gametest.GameTestHolder; |
| 8 | +import net.neoforged.neoforge.gametest.PrefixGameTestTemplate; |
| 9 | +import org.apache.commons.lang3.tuple.Pair; |
| 10 | +import org.cyclops.integrateddynamics.core.evaluate.operator.Operators; |
| 11 | +import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypeInteger; |
| 12 | +import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypes; |
| 13 | +import org.cyclops.integrateddynamics.part.PartTypePanelDisplay; |
| 14 | +import org.cyclops.integratedscripting.Reference; |
| 15 | +import org.cyclops.integratedscripting.api.network.IScriptingData; |
| 16 | +import org.cyclops.integratedscripting.core.network.ScriptingNetworkHelpers; |
| 17 | + |
| 18 | +import java.nio.file.Path; |
| 19 | + |
| 20 | +import static org.cyclops.integrateddynamics.gametest.GameTestHelpersIntegratedDynamics.*; |
| 21 | +import static org.cyclops.integratedscripting.gametest.GameTestHelpersIntegratedScripting.createBasicNetwork; |
| 22 | +import static org.cyclops.integratedscripting.gametest.GameTestHelpersIntegratedScripting.createVariableForScript; |
| 23 | + |
| 24 | +@GameTestHolder(Reference.MOD_ID) |
| 25 | +@PrefixGameTestTemplate(false) |
| 26 | +public class GameTestsScripts { |
| 27 | + |
| 28 | + public static final String TEMPLATE_EMPTY = "empty10"; |
| 29 | + public static final int TIMEOUT = 2000; |
| 30 | + public static final BlockPos POS = BlockPos.ZERO.offset(2, 0, 2); |
| 31 | + |
| 32 | + @GameTest(template = TEMPLATE_EMPTY, timeoutTicks = TIMEOUT) |
| 33 | + public void testScriptsDisplayScriptConst(GameTestHelper helper) { |
| 34 | + GameTestHelpersIntegratedScripting.NetworkPositions positions = createBasicNetwork(helper, POS); |
| 35 | + |
| 36 | + // Write script |
| 37 | + ScriptingNetworkHelpers.getScriptingData().setScript(positions.diskId(), Path.of("script0.js"), "const abc = 3", IScriptingData.ChangeLocation.MEMORY); |
| 38 | + |
| 39 | + // Create variable from script |
| 40 | + ItemStack variableScript = createVariableForScript(helper.getLevel(), positions.diskId(), Path.of("script0.js"), "abc"); |
| 41 | + |
| 42 | + // Place variable in display |
| 43 | + Pair<PartTypePanelDisplay, PartTypePanelDisplay.State> partAndState = placeVariableInDisplayPanel(helper.getLevel(), positions.displayPanel(), variableScript); |
| 44 | + |
| 45 | + helper.succeedWhen(() -> { |
| 46 | + assertValueEqual(partAndState.getRight().getDisplayValue(), ValueTypeInteger.ValueInteger.of(3)); |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + @GameTest(template = TEMPLATE_EMPTY, timeoutTicks = TIMEOUT) |
| 51 | + public void testScriptsDisplayScriptApplied(GameTestHelper helper) { |
| 52 | + GameTestHelpersIntegratedScripting.NetworkPositions positions = createBasicNetwork(helper, POS); |
| 53 | + |
| 54 | + // Write script |
| 55 | + ScriptingNetworkHelpers.getScriptingData().setScript(positions.diskId(), Path.of("script0.js"), "function abc(a, b) { return a + b; }", IScriptingData.ChangeLocation.MEMORY); |
| 56 | + |
| 57 | + // Create variable from script |
| 58 | + ItemStack variableScript = createVariableForScript(helper.getLevel(), positions.diskId(), Path.of("script0.js"), "abc"); |
| 59 | + |
| 60 | + // Create constants as input to the script's function |
| 61 | + ItemStack variableConst1 = createVariableForValue(helper.getLevel(), ValueTypes.INTEGER, ValueTypeInteger.ValueInteger.of(3)); |
| 62 | + ItemStack variableConst2 = createVariableForValue(helper.getLevel(), ValueTypes.INTEGER, ValueTypeInteger.ValueInteger.of(8)); |
| 63 | + |
| 64 | + // Insert all variables into the variable store |
| 65 | + positions.variableStore().getInventory().setItem(0, variableScript); |
| 66 | + positions.variableStore().getInventory().setItem(1, variableConst1); |
| 67 | + positions.variableStore().getInventory().setItem(2, variableConst2); |
| 68 | + |
| 69 | + // Create variable card for applying the function |
| 70 | + ItemStack variableAdded = createVariableForOperator(helper.getLevel(), Operators.OPERATOR_APPLY_2, new int[]{ |
| 71 | + getVariableFacade(helper.getLevel(), variableScript).getId(), |
| 72 | + getVariableFacade(helper.getLevel(), variableConst1).getId(), |
| 73 | + getVariableFacade(helper.getLevel(), variableConst2).getId() |
| 74 | + }); |
| 75 | + |
| 76 | + // Place variable in display |
| 77 | + Pair<PartTypePanelDisplay, PartTypePanelDisplay.State> partAndState = placeVariableInDisplayPanel(helper.getLevel(), positions.displayPanel(), variableAdded); |
| 78 | + |
| 79 | + helper.succeedWhen(() -> { |
| 80 | + assertValueEqual(partAndState.getRight().getDisplayValue(), ValueTypeInteger.ValueInteger.of(11)); |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments