From 7f6ca4e6d18f9b583bfed119fbbe3d11c1c6a4f3 Mon Sep 17 00:00:00 2001 From: 4P5 Date: Tue, 2 Jan 2024 00:49:52 +1300 Subject: [PATCH 1/3] world.getHeightmap() - Added world.getHeightmap() - Takes a string and a position - Returns the highest Y coordinate at the position according to the given heightmap --- .../figura/lua/api/world/WorldAPI.java | 35 +++++++++++++++++++ .../figura/lua/docs/FiguraListDocs.java | 8 ++++- .../resources/assets/figura/lang/en_us.json | 2 ++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/org/figuramc/figura/lua/api/world/WorldAPI.java b/common/src/main/java/org/figuramc/figura/lua/api/world/WorldAPI.java index df60494a6..7a4457f41 100644 --- a/common/src/main/java/org/figuramc/figura/lua/api/world/WorldAPI.java +++ b/common/src/main/java/org/figuramc/figura/lua/api/world/WorldAPI.java @@ -9,10 +9,12 @@ import net.minecraft.core.BlockPos; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.ClipContext; import net.minecraft.world.level.Level; import net.minecraft.world.level.LightLayer; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.levelgen.Heightmap; import org.figuramc.figura.avatar.Avatar; import org.figuramc.figura.avatar.AvatarManager; import org.figuramc.figura.lua.LuaNotNil; @@ -23,6 +25,7 @@ import org.figuramc.figura.lua.docs.LuaMethodDoc; import org.figuramc.figura.lua.docs.LuaMethodOverload; import org.figuramc.figura.lua.docs.LuaTypeDoc; +import org.figuramc.figura.math.vector.FiguraVec2; import org.figuramc.figura.math.vector.FiguraVec3; import org.figuramc.figura.utils.EntityUtils; import org.figuramc.figura.utils.LuaUtils; @@ -329,6 +332,38 @@ public static Integer getBlockLightLevel(Object x, Double y, Double z) { return world.getBrightness(LightLayer.BLOCK, blockPos); } + @LuaWhitelist + @LuaMethodDoc( + overloads = { + @LuaMethodOverload( + argumentTypes = {String.class, FiguraVec2.class}, + argumentNames = {"heightmap", "pos"} + ), + @LuaMethodOverload( + argumentTypes = {String.class, Double.class, Double.class}, + argumentNames = {"heightmap", "x", "z"} + ) + }, + value = "world.get_heightmap" + ) + public static Integer getHeightmap(String heightmap, Object x, Double z) { + FiguraVec2 pos = LuaUtils.parseVec2("getHeightmap", x, z); + Level world = getCurrentWorld(); + + BlockPos blockPos = new BlockPos((int) pos.x(), 0, (int) pos.y()); + if (world.getChunkAt(blockPos) == null) + return null; + + Heightmap.Types heightmapType; + try { + heightmapType = heightmap != null ? Heightmap.Types.valueOf(heightmap.toUpperCase()) : Heightmap.Types.MOTION_BLOCKING; + } catch (IllegalArgumentException e) { + throw new LuaError("Invalid heightmap type provided"); + } + + return world.getHeight(heightmapType, (int) pos.x(), (int) pos.y()); + } + @LuaWhitelist @LuaMethodDoc( overloads = { diff --git a/common/src/main/java/org/figuramc/figura/lua/docs/FiguraListDocs.java b/common/src/main/java/org/figuramc/figura/lua/docs/FiguraListDocs.java index 24f118c9c..965f4a5e4 100644 --- a/common/src/main/java/org/figuramc/figura/lua/docs/FiguraListDocs.java +++ b/common/src/main/java/org/figuramc/figura/lua/docs/FiguraListDocs.java @@ -15,6 +15,7 @@ import net.minecraft.world.item.UseAnim; import net.minecraft.world.level.ClipContext; +import net.minecraft.world.level.levelgen.Heightmap; import org.figuramc.figura.FiguraMod; import org.figuramc.figura.animation.Animation; import org.figuramc.figura.mixin.input.KeyMappingAccessor; @@ -108,6 +109,10 @@ public class FiguraListDocs { for (ClipContext.Fluid value : ClipContext.Fluid.values()) add(value.name()); }}; + private static final LinkedHashSet HEIGHTMAP_TYPE = new LinkedHashSet<>() {{ + for (Heightmap.Types value : Heightmap.Types.values()) + add(value.name()); + }}; private enum ListDoc { KEYBINDS(() -> FiguraListDocs.KEYBINDS, "Keybinds", "keybinds", 2), @@ -126,7 +131,8 @@ private enum ListDoc { RENDER_MODES(() -> FiguraListDocs.RENDER_MODES, "RenderModes", "render_modes", 1), STRING_ENCODINGS(() -> FiguraListDocs.STRING_ENCODINGS, "StringEncodings", "string_encodings", 1), BLOCK_RAYCAST_TYPE(() -> FiguraListDocs.BLOCK_RAYCAST_TYPE, "BlockRaycastTypes", "block_raycast_types", 1), - FLUID_RAYCAST_TYPE(() -> FiguraListDocs.FLUID_RAYCAST_TYPE, "FluidRaycastTypes", "fluid_raycast_types", 1); + FLUID_RAYCAST_TYPE(() -> FiguraListDocs.FLUID_RAYCAST_TYPE, "FluidRaycastTypes", "fluid_raycast_types", 1), + HEIGHTMAP_TYPE(() -> FiguraListDocs.HEIGHTMAP_TYPE, "HeightmapTypes", "heightmap_types", 1); private final Supplier supplier; private final String name, id; diff --git a/common/src/main/resources/assets/figura/lang/en_us.json b/common/src/main/resources/assets/figura/lang/en_us.json index 5d6f42990..b646ac3e4 100644 --- a/common/src/main/resources/assets/figura/lang/en_us.json +++ b/common/src/main/resources/assets/figura/lang/en_us.json @@ -633,6 +633,7 @@ "figura.docs.enum.string_encodings": "List of valid string encodings\nUsed within Buffers", "figura.docs.enum.block_raycast_types": "List of valid BlockRaycastTypes\nUsed to determine how raycast.block handles blocks", "figura.docs.enum.fluid_raycast_types": "List of valid FluidRaycastTypes\nUsed to determine how raycast.block handles fluids", + "figura.docs.enum.heightmap_types": "List of valid HeightmapTypes\nUsed in world.getHeightmap to select the type of heightmap", "figura.docs.globals": "Documentation for the various things Figura adds to the global lua state", "figura.docs.globals.vec": "An alias for \"vectors.vec\"", "figura.docs.globals.require": "The require() function takes the name of one of your scripts, without the .lua extension\nIf this script has not been already run before, it will run that script and return the value that script returns\nIf it has been run before, then it will not run the file again, but it will return the same thing as the first time\nIf a required script has no returns, then require() will return true\nIf the name you give isn't any of your scripts, it will error\nScripts can be accessed relative to the executing script using `./` and `../`", @@ -1646,6 +1647,7 @@ "figura.docs.world.get_sky_light_level": "Gets the skylight level of the block at the given position", "figura.docs.world.get_block_light_level": "Gets the block light level of the block at the given position", "figura.docs.world.is_open_sky": "Gets whether or not the sky is open at the given position", + "figura.docs.world.get_heightmap": "Returns the highest point in the world according to the provided heightmap", "figura.docs.world.get_dimension": "Gets the dimension name of this world", "figura.docs.world.get_entity": "Returns an EntityAPI object from this UUID's entity, or nil if no entity was found", "figura.docs.world.get_players": "Returns a table containing instances of Player for all players in the world\nThe players are indexed by their names", From 852faea2c903f9b9dc8c053464d0b36e605afc59 Mon Sep 17 00:00:00 2001 From: 4P5 Date: Sat, 6 Jan 2024 01:55:39 +1300 Subject: [PATCH 2/3] client.getRegistry() - Added client.getRegistry() - Added the registries enum --- .../figuramc/figura/lua/api/ClientAPI.java | 22 +++++++++++++++++++ .../figura/lua/docs/FiguraListDocs.java | 8 ++++++- .../resources/assets/figura/lang/en_us.json | 2 ++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/org/figuramc/figura/lua/api/ClientAPI.java b/common/src/main/java/org/figuramc/figura/lua/api/ClientAPI.java index c633e39b4..ae04e8316 100644 --- a/common/src/main/java/org/figuramc/figura/lua/api/ClientAPI.java +++ b/common/src/main/java/org/figuramc/figura/lua/api/ClientAPI.java @@ -10,7 +10,9 @@ import net.minecraft.client.multiplayer.PlayerInfo; import net.minecraft.client.multiplayer.ServerData; import net.minecraft.client.server.IntegratedServer; +import net.minecraft.core.Registry; import net.minecraft.core.UUIDUtil; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.packs.repository.Pack; @@ -36,6 +38,7 @@ import java.text.SimpleDateFormat; import java.util.*; import java.util.function.Supplier; +import java.util.stream.Collectors; @LuaWhitelist @LuaTypeDoc( @@ -620,6 +623,25 @@ public static String getTranslatedString(@LuaNotNil String text, LuaValue args) return component.getString(); } + @LuaWhitelist + @LuaMethodDoc( + overloads = { + @LuaMethodOverload(argumentTypes = String.class, argumentNames = "registryName"), + }, + value = "client.get_registry" + ) + public static List getRegistry(@LuaNotNil String registryName) { + Registry registry = BuiltInRegistries.REGISTRY.get(new ResourceLocation(registryName)); + + if (registry != null) { + return registry.keySet().stream() + .map(ResourceLocation::toString) + .collect(Collectors.toList()); + } else { + throw new LuaError("Registry " + registryName + " does not exist"); + } + } + @Override public String toString() { return "ClientAPI"; diff --git a/common/src/main/java/org/figuramc/figura/lua/docs/FiguraListDocs.java b/common/src/main/java/org/figuramc/figura/lua/docs/FiguraListDocs.java index 965f4a5e4..804a048c9 100644 --- a/common/src/main/java/org/figuramc/figura/lua/docs/FiguraListDocs.java +++ b/common/src/main/java/org/figuramc/figura/lua/docs/FiguraListDocs.java @@ -5,6 +5,7 @@ import com.google.gson.JsonObject; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import net.minecraft.ChatFormatting; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.locale.Language; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; @@ -113,6 +114,10 @@ public class FiguraListDocs { for (Heightmap.Types value : Heightmap.Types.values()) add(value.name()); }}; + private static final LinkedHashSet REGISTRIES = new LinkedHashSet<>() {{ + for (ResourceLocation resourceLocation : BuiltInRegistries.REGISTRY.keySet()) + add(resourceLocation.getPath()); + }}; private enum ListDoc { KEYBINDS(() -> FiguraListDocs.KEYBINDS, "Keybinds", "keybinds", 2), @@ -132,7 +137,8 @@ private enum ListDoc { STRING_ENCODINGS(() -> FiguraListDocs.STRING_ENCODINGS, "StringEncodings", "string_encodings", 1), BLOCK_RAYCAST_TYPE(() -> FiguraListDocs.BLOCK_RAYCAST_TYPE, "BlockRaycastTypes", "block_raycast_types", 1), FLUID_RAYCAST_TYPE(() -> FiguraListDocs.FLUID_RAYCAST_TYPE, "FluidRaycastTypes", "fluid_raycast_types", 1), - HEIGHTMAP_TYPE(() -> FiguraListDocs.HEIGHTMAP_TYPE, "HeightmapTypes", "heightmap_types", 1); + HEIGHTMAP_TYPE(() -> FiguraListDocs.HEIGHTMAP_TYPE, "HeightmapTypes", "heightmap_types", 1), + REGISTRIES(() -> FiguraListDocs.REGISTRIES, "Registries", "registries", 1); private final Supplier supplier; private final String name, id; diff --git a/common/src/main/resources/assets/figura/lang/en_us.json b/common/src/main/resources/assets/figura/lang/en_us.json index b646ac3e4..591413288 100644 --- a/common/src/main/resources/assets/figura/lang/en_us.json +++ b/common/src/main/resources/assets/figura/lang/en_us.json @@ -634,6 +634,7 @@ "figura.docs.enum.block_raycast_types": "List of valid BlockRaycastTypes\nUsed to determine how raycast.block handles blocks", "figura.docs.enum.fluid_raycast_types": "List of valid FluidRaycastTypes\nUsed to determine how raycast.block handles fluids", "figura.docs.enum.heightmap_types": "List of valid HeightmapTypes\nUsed in world.getHeightmap to select the type of heightmap", + "figura.docs.enum.registries": "A list of valid registries.\nUsed in client.getRegistry to select the type of registry", "figura.docs.globals": "Documentation for the various things Figura adds to the global lua state", "figura.docs.globals.vec": "An alias for \"vectors.vec\"", "figura.docs.globals.require": "The require() function takes the name of one of your scripts, without the .lua extension\nIf this script has not been already run before, it will run that script and return the value that script returns\nIf it has been run before, then it will not run the file again, but it will return the same thing as the first time\nIf a required script has no returns, then require() will return true\nIf the name you give isn't any of your scripts, it will error\nScripts can be accessed relative to the executing script using `./` and `../`", @@ -926,6 +927,7 @@ "figura.docs.client.get_camera_entity": "Returns the entity the camera is currently targeting, so returns the entity you are currently spectating, including yourself", "figura.docs.client.get_server_data": "Returns a table with information on the currently connected server (also for singleplayer worlds)", "figura.docs.client.get_date": "Returns a table with information about the client's current time", + "figura.docs.client.get_registry": "Returns a list of all values in the specified registry\nSee the `registries` enum for a list of valid registries", "figura.docs.client.get_frame_time": "Returns the current fraction between the last tick and the next tick\nThis is the value used as \"delta\" in the RENDER event", "figura.docs.client.list_atlases": "Returns a list of all registered atlases paths", "figura.docs.client.get_atlas": "Returns a TextureAtlasAPI object with information about the given atlas\nReturns nil if the atlas was not found", From 5a5e70be98bec99cf1d30211a5ee14d2fea3dc4f Mon Sep 17 00:00:00 2001 From: 4P5 Date: Mon, 8 Jan 2024 23:18:03 +1300 Subject: [PATCH 3/3] Rename to world.getHeight - Renamed to world.getHeight and put the position first, making the heightmap string optional (or, more optional than it was before. Optionaler. Yeah.) --- .../figuramc/figura/lua/api/world/WorldAPI.java | 15 +++++++-------- .../main/resources/assets/figura/lang/en_us.json | 4 ++-- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/common/src/main/java/org/figuramc/figura/lua/api/world/WorldAPI.java b/common/src/main/java/org/figuramc/figura/lua/api/world/WorldAPI.java index 7a4457f41..a1101099c 100644 --- a/common/src/main/java/org/figuramc/figura/lua/api/world/WorldAPI.java +++ b/common/src/main/java/org/figuramc/figura/lua/api/world/WorldAPI.java @@ -9,7 +9,6 @@ import net.minecraft.core.BlockPos; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.ClipContext; import net.minecraft.world.level.Level; import net.minecraft.world.level.LightLayer; import net.minecraft.world.level.block.Blocks; @@ -336,18 +335,18 @@ public static Integer getBlockLightLevel(Object x, Double y, Double z) { @LuaMethodDoc( overloads = { @LuaMethodOverload( - argumentTypes = {String.class, FiguraVec2.class}, - argumentNames = {"heightmap", "pos"} + argumentTypes = {FiguraVec2.class, String.class}, + argumentNames = {"pos", "heightmap"} ), @LuaMethodOverload( - argumentTypes = {String.class, Double.class, Double.class}, - argumentNames = {"heightmap", "x", "z"} + argumentTypes = {Double.class, Double.class, String.class}, + argumentNames = {"x", "z", "heightmap"} ) }, - value = "world.get_heightmap" + value = "world.get_height" ) - public static Integer getHeightmap(String heightmap, Object x, Double z) { - FiguraVec2 pos = LuaUtils.parseVec2("getHeightmap", x, z); + public static Integer getHeight(Object x, Double z, String heightmap) { + FiguraVec2 pos = LuaUtils.parseVec2("getHeight", x, z); Level world = getCurrentWorld(); BlockPos blockPos = new BlockPos((int) pos.x(), 0, (int) pos.y()); diff --git a/common/src/main/resources/assets/figura/lang/en_us.json b/common/src/main/resources/assets/figura/lang/en_us.json index 591413288..97ab6cebf 100644 --- a/common/src/main/resources/assets/figura/lang/en_us.json +++ b/common/src/main/resources/assets/figura/lang/en_us.json @@ -633,7 +633,7 @@ "figura.docs.enum.string_encodings": "List of valid string encodings\nUsed within Buffers", "figura.docs.enum.block_raycast_types": "List of valid BlockRaycastTypes\nUsed to determine how raycast.block handles blocks", "figura.docs.enum.fluid_raycast_types": "List of valid FluidRaycastTypes\nUsed to determine how raycast.block handles fluids", - "figura.docs.enum.heightmap_types": "List of valid HeightmapTypes\nUsed in world.getHeightmap to select the type of heightmap", + "figura.docs.enum.heightmap_types": "List of valid HeightmapTypes\nUsed in world.getHeight to select the type of heightmap", "figura.docs.enum.registries": "A list of valid registries.\nUsed in client.getRegistry to select the type of registry", "figura.docs.globals": "Documentation for the various things Figura adds to the global lua state", "figura.docs.globals.vec": "An alias for \"vectors.vec\"", @@ -1649,7 +1649,7 @@ "figura.docs.world.get_sky_light_level": "Gets the skylight level of the block at the given position", "figura.docs.world.get_block_light_level": "Gets the block light level of the block at the given position", "figura.docs.world.is_open_sky": "Gets whether or not the sky is open at the given position", - "figura.docs.world.get_heightmap": "Returns the highest point in the world according to the provided heightmap", + "figura.docs.world.get_height": "Returns the highest point at the given position according to the provided heightmap\nDefaults to MOTION_BLOCKING if no heightmap is provided", "figura.docs.world.get_dimension": "Gets the dimension name of this world", "figura.docs.world.get_entity": "Returns an EntityAPI object from this UUID's entity, or nil if no entity was found", "figura.docs.world.get_players": "Returns a table containing instances of Player for all players in the world\nThe players are indexed by their names",