diff --git a/projects/common/src/main/java/dan200/computercraft/data/LanguageProvider.java b/projects/common/src/main/java/dan200/computercraft/data/LanguageProvider.java index c5dcdb70d..9a12a77e1 100644 --- a/projects/common/src/main/java/dan200/computercraft/data/LanguageProvider.java +++ b/projects/common/src/main/java/dan200/computercraft/data/LanguageProvider.java @@ -214,6 +214,7 @@ public final class LanguageProvider implements DataProvider { addConfigEntry(ConfigSpec.defaultComputerSettings, "Default Computer settings"); addConfigEntry(ConfigSpec.logComputerErrors, "Log computer errors"); addConfigEntry(ConfigSpec.commandRequireCreative, "Command computers require creative"); + addConfigEntry(ConfigSpec.disabledGenericMethods, "Disabled generic methods"); addConfigGroup(ConfigSpec.serverSpec, "execution", "Execution"); addConfigEntry(ConfigSpec.computerThreads, "Computer threads"); diff --git a/projects/common/src/main/java/dan200/computercraft/impl/GenericSources.java b/projects/common/src/main/java/dan200/computercraft/impl/GenericSources.java index 0ba250c9c..253cc03e2 100644 --- a/projects/common/src/main/java/dan200/computercraft/impl/GenericSources.java +++ b/projects/common/src/main/java/dan200/computercraft/impl/GenericSources.java @@ -6,10 +6,12 @@ package dan200.computercraft.impl; import dan200.computercraft.api.lua.GenericSource; import dan200.computercraft.core.asm.GenericMethod; +import dan200.computercraft.shared.config.ConfigSpec; import java.util.Collection; import java.util.LinkedHashSet; import java.util.Objects; +import java.util.Set; /** * The global registry for {@link GenericSource}s. @@ -29,6 +31,11 @@ public final class GenericSources { } public static Collection getAllMethods() { - return sources.stream().flatMap(GenericMethod::getMethods).toList(); + var disabledMethods = Set.copyOf(ConfigSpec.disabledGenericMethods.get()); + return sources.stream() + .filter(x -> !disabledMethods.contains(x.id())) + .flatMap(GenericMethod::getMethods) + .filter(x -> !disabledMethods.contains(x.id())) + .toList(); } } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/config/ConfigSpec.java b/projects/common/src/main/java/dan200/computercraft/shared/config/ConfigSpec.java index 347f7fff0..205b10310 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/config/ConfigSpec.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/config/ConfigSpec.java @@ -35,6 +35,7 @@ public final class ConfigSpec { public static final ConfigFile.Value logComputerErrors; public static final ConfigFile.Value commandRequireCreative; public static final ConfigFile.Value uploadMaxSize; + public static final ConfigFile.Value> disabledGenericMethods; public static final ConfigFile.Value computerThreads; public static final ConfigFile.Value maxMainGlobalTime; @@ -139,6 +140,19 @@ public final class ConfigSpec { Require players to be in creative mode and be opped in order to interact with command computers. This is the default behaviour for vanilla's Command blocks.""") .define("command_require_creative", Config.commandRequireCreative); + + disabledGenericMethods = builder + .comment(""" + A list of generic methods or method sources to disable. Generic methods are + methods added to a block/block entity when there is no explicit peripheral + provider. This includes inventory methods (i.e. inventory.getItemDetail, + inventory.pushItems), and (if on Forge), the fluid_storage and energy_storage + methods. + Methods in this list can either be a whole group of methods (computercraft:inventory) + or a single method (computercraft:inventory#pushItems). + """) + .worldRestart() + .defineList("disabled_generic_methods", List.of(), x -> x instanceof String); } { diff --git a/projects/core/src/main/java/dan200/computercraft/core/asm/GenericMethod.java b/projects/core/src/main/java/dan200/computercraft/core/asm/GenericMethod.java index 1e18b0a0b..ff81bc65e 100644 --- a/projects/core/src/main/java/dan200/computercraft/core/asm/GenericMethod.java +++ b/projects/core/src/main/java/dan200/computercraft/core/asm/GenericMethod.java @@ -24,18 +24,24 @@ import java.util.stream.Stream; public final class GenericMethod { private static final Logger LOG = LoggerFactory.getLogger(GenericMethod.class); + final GenericSource source; final Method method; final LuaFunction annotation; final Class target; final @Nullable PeripheralType peripheralType; - private GenericMethod(Method method, LuaFunction annotation, Class target, @Nullable PeripheralType peripheralType) { + private GenericMethod(GenericSource source, Method method, LuaFunction annotation, Class target, @Nullable PeripheralType peripheralType) { + this.source = source; this.method = method; this.annotation = annotation; this.target = target; this.peripheralType = peripheralType; } + public String id() { + return source.id() + "#" + name(); + } + public String name() { return method.getName(); } @@ -69,7 +75,7 @@ public final class GenericMethod { var target = Reflect.getRawType(method, types[0], false); if (target == null) return null; - return new GenericMethod(method, annotation, target, type); + return new GenericMethod(source, method, annotation, target, type); }) .filter(Objects::nonNull); } diff --git a/projects/fabric/src/generated/resources/assets/computercraft/lang/en_us.json b/projects/fabric/src/generated/resources/assets/computercraft/lang/en_us.json index 77fe60ee0..ca14707a5 100644 --- a/projects/fabric/src/generated/resources/assets/computercraft/lang/en_us.json +++ b/projects/fabric/src/generated/resources/assets/computercraft/lang/en_us.json @@ -81,6 +81,8 @@ "gui.computercraft.config.default_computer_settings.tooltip": "A comma separated list of default system settings to set on new computers.\nExample: \"shell.autocomplete=false,lua.autocomplete=false,edit.autocomplete=false\"\nwill disable all autocompletion.", "gui.computercraft.config.disable_lua51_features": "Disable Lua 5.1 features", "gui.computercraft.config.disable_lua51_features.tooltip": "Set this to true to disable Lua 5.1 functions that will be removed in a future\nupdate. Useful for ensuring forward compatibility of your programs now.", + "gui.computercraft.config.disabled_generic_methods": "Disabled generic methods", + "gui.computercraft.config.disabled_generic_methods.tooltip": "A list of generic methods or method sources to disable. Generic methods are\nmethods added to a block/block entity when there is no explicit peripheral\nprovider. This includes inventory methods (i.e. inventory.getItemDetail,\ninventory.pushItems), and (if on Forge), the fluid_storage and energy_storage\nmethods.\nMethods in this list can either be a whole group of methods (computercraft:inventory)\nor a single method (computercraft:inventory#pushItems).\n", "gui.computercraft.config.execution": "Execution", "gui.computercraft.config.execution.computer_threads": "Computer threads", "gui.computercraft.config.execution.computer_threads.tooltip": "Set the number of threads computers can run on. A higher number means more\ncomputers can run at once, but may induce lag. Please note that some mods may\nnot work with a thread count higher than 1. Use with caution.\nRange: > 1", diff --git a/projects/forge/src/generated/resources/assets/computercraft/lang/en_us.json b/projects/forge/src/generated/resources/assets/computercraft/lang/en_us.json index 77fe60ee0..ca14707a5 100644 --- a/projects/forge/src/generated/resources/assets/computercraft/lang/en_us.json +++ b/projects/forge/src/generated/resources/assets/computercraft/lang/en_us.json @@ -81,6 +81,8 @@ "gui.computercraft.config.default_computer_settings.tooltip": "A comma separated list of default system settings to set on new computers.\nExample: \"shell.autocomplete=false,lua.autocomplete=false,edit.autocomplete=false\"\nwill disable all autocompletion.", "gui.computercraft.config.disable_lua51_features": "Disable Lua 5.1 features", "gui.computercraft.config.disable_lua51_features.tooltip": "Set this to true to disable Lua 5.1 functions that will be removed in a future\nupdate. Useful for ensuring forward compatibility of your programs now.", + "gui.computercraft.config.disabled_generic_methods": "Disabled generic methods", + "gui.computercraft.config.disabled_generic_methods.tooltip": "A list of generic methods or method sources to disable. Generic methods are\nmethods added to a block/block entity when there is no explicit peripheral\nprovider. This includes inventory methods (i.e. inventory.getItemDetail,\ninventory.pushItems), and (if on Forge), the fluid_storage and energy_storage\nmethods.\nMethods in this list can either be a whole group of methods (computercraft:inventory)\nor a single method (computercraft:inventory#pushItems).\n", "gui.computercraft.config.execution": "Execution", "gui.computercraft.config.execution.computer_threads": "Computer threads", "gui.computercraft.config.execution.computer_threads.tooltip": "Set the number of threads computers can run on. A higher number means more\ncomputers can run at once, but may induce lag. Please note that some mods may\nnot work with a thread count higher than 1. Use with caution.\nRange: > 1",