diff --git a/projects/common/src/main/java/dan200/computercraft/shared/util/ArgumentHelpers.java b/projects/common/src/main/java/dan200/computercraft/shared/util/ArgumentHelpers.java index d26173050..8ab5b7fe1 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/util/ArgumentHelpers.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/util/ArgumentHelpers.java @@ -19,18 +19,6 @@ public final class ArgumentHelpers { private ArgumentHelpers() { } - public static void assertBetween(double value, double min, double max, String message) throws LuaException { - if (value < min || value > max || Double.isNaN(value)) { - throw new LuaException(String.format(message, "between " + min + " and " + max)); - } - } - - public static void assertBetween(int value, int min, int max, String message) throws LuaException { - if (value < min || value > max) { - throw new LuaException(String.format(message, "between " + min + " and " + max)); - } - } - public static T getRegistryEntry(String name, String typeName, RegistryWrappers.RegistryWrapper registry) throws LuaException { ResourceLocation id; try { diff --git a/projects/core/src/main/java/dan200/computercraft/core/lua/CobaltLuaMachine.java b/projects/core/src/main/java/dan200/computercraft/core/lua/CobaltLuaMachine.java index 877e2f7a1..5f25b8efd 100644 --- a/projects/core/src/main/java/dan200/computercraft/core/lua/CobaltLuaMachine.java +++ b/projects/core/src/main/java/dan200/computercraft/core/lua/CobaltLuaMachine.java @@ -30,9 +30,6 @@ import java.io.Serial; import java.nio.ByteBuffer; import java.util.*; -import static org.squiddev.cobalt.ValueFactory.valueOf; -import static org.squiddev.cobalt.ValueFactory.varargsOf; - public class CobaltLuaMachine implements ILuaMachine { private static final Logger LOG = LoggerFactory.getLogger(CobaltLuaMachine.class); @@ -76,8 +73,8 @@ public class CobaltLuaMachine implements ILuaMachine { var globals = state.getMainThread().getfenv(); CoreLibraries.debugGlobals(state); Bit32Lib.add(state, globals); - globals.rawset("_HOST", valueOf(environment.hostString())); - globals.rawset("_CC_DEFAULT_SETTINGS", valueOf(CoreConfig.defaultComputerSettings)); + globals.rawset("_HOST", ValueFactory.valueOf(environment.hostString())); + globals.rawset("_CC_DEFAULT_SETTINGS", ValueFactory.valueOf(CoreConfig.defaultComputerSettings)); if (CoreConfig.disableLua51Features) globals.rawset("_CC_DISABLE_LUA51_FEATURES", Constants.TRUE); // Add default APIs @@ -121,7 +118,7 @@ public class CobaltLuaMachine implements ILuaMachine { } try { - var resumeArgs = eventName == null ? Constants.NONE : varargsOf(valueOf(eventName), toValues(arguments)); + var resumeArgs = eventName == null ? Constants.NONE : ValueFactory.varargsOf(ValueFactory.valueOf(eventName), toValues(arguments)); // Resume the current thread, or the main one when first starting off. var thread = state.getCurrentThread(); @@ -189,14 +186,14 @@ public class CobaltLuaMachine implements ILuaMachine { private LuaValue toValue(@Nullable Object object, @Nullable IdentityHashMap values) { if (object == null) return Constants.NIL; - if (object instanceof Number num) return valueOf(num.doubleValue()); - if (object instanceof Boolean bool) return valueOf(bool); - if (object instanceof String str) return valueOf(str); - if (object instanceof byte[] b) return valueOf(Arrays.copyOf(b, b.length)); + if (object instanceof Number num) return ValueFactory.valueOf(num.doubleValue()); + if (object instanceof Boolean bool) return ValueFactory.valueOf(bool); + if (object instanceof String str) return ValueFactory.valueOf(str); + if (object instanceof byte[] b) return ValueFactory.valueOf(Arrays.copyOf(b, b.length)); if (object instanceof ByteBuffer b) { var bytes = new byte[b.remaining()]; b.get(bytes); - return valueOf(bytes); + return ValueFactory.valueOf(bytes); } if (values == null) values = new IdentityHashMap<>(1); @@ -261,7 +258,7 @@ public class CobaltLuaMachine implements ILuaMachine { var object = objects[i]; values[i] = toValue(object, result); } - return varargsOf(values); + return ValueFactory.varargsOf(values); } @Nullable diff --git a/projects/core/src/main/java/dan200/computercraft/core/util/ArgumentHelpers.java b/projects/core/src/main/java/dan200/computercraft/core/util/ArgumentHelpers.java new file mode 100644 index 000000000..5036a48e8 --- /dev/null +++ b/projects/core/src/main/java/dan200/computercraft/core/util/ArgumentHelpers.java @@ -0,0 +1,27 @@ +// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers +// +// SPDX-License-Identifier: MPL-2.0 + +package dan200.computercraft.core.util; + +import dan200.computercraft.api.lua.LuaException; + +/** + * A few helpers for working with arguments. + *

+ * This should really be moved into the public API. However, until I have settled on a suitable format, we'll keep it + * where it is used. + */ +public class ArgumentHelpers { + public static void assertBetween(double value, double min, double max, String message) throws LuaException { + if (value < min || value > max || Double.isNaN(value)) { + throw new LuaException(String.format(message, "between " + min + " and " + max)); + } + } + + public static void assertBetween(int value, int min, int max, String message) throws LuaException { + if (value < min || value > max) { + throw new LuaException(String.format(message, "between " + min + " and " + max)); + } + } +} diff --git a/projects/fabric/src/main/java/dan200/computercraft/shared/peripheral/generic/methods/InventoryMethods.java b/projects/fabric/src/main/java/dan200/computercraft/shared/peripheral/generic/methods/InventoryMethods.java index fa15b1dd9..527e62dfe 100644 --- a/projects/fabric/src/main/java/dan200/computercraft/shared/peripheral/generic/methods/InventoryMethods.java +++ b/projects/fabric/src/main/java/dan200/computercraft/shared/peripheral/generic/methods/InventoryMethods.java @@ -30,7 +30,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Optional; -import static dan200.computercraft.shared.util.ArgumentHelpers.assertBetween; +import static dan200.computercraft.core.util.ArgumentHelpers.assertBetween; /** * Methods for interacting with inventories. This mirrors the Forge version. diff --git a/projects/forge/src/main/java/dan200/computercraft/shared/peripheral/generic/methods/InventoryMethods.java b/projects/forge/src/main/java/dan200/computercraft/shared/peripheral/generic/methods/InventoryMethods.java index 7aedf5d83..3ca480232 100644 --- a/projects/forge/src/main/java/dan200/computercraft/shared/peripheral/generic/methods/InventoryMethods.java +++ b/projects/forge/src/main/java/dan200/computercraft/shared/peripheral/generic/methods/InventoryMethods.java @@ -25,7 +25,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Optional; -import static dan200.computercraft.shared.util.ArgumentHelpers.assertBetween; +import static dan200.computercraft.core.util.ArgumentHelpers.assertBetween; /** * Methods for interacting with inventories.