Skip to content

Commit 0491edf

Browse files
committed
need to fix require
1 parent cef9ecc commit 0491edf

16 files changed

Lines changed: 172 additions & 52 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ this mod uses the FiguraMC implementation of Lua.
2525
- [x] entity
2626
- [x] block
2727
- [x] item
28-
- [ ] world/server
2928
- [x] math (vectors, mainly)
3029
- other
3130
- [x] autogen documentation (actively working on)
31+
- [ ] support `require` function (and therefore libraries)
3232

3333
## what im not worried about right now
3434
- scripting language independence; im just focusing on lua

autodocs/autodoc.lua

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
--#region Information
44
-- GENERATED AUTODOC
5-
-- Generated: 2026-01-29T15:36:57.191277400
5+
-- Generated: 2026-01-29T20:35:30.443182800
66
-- Luafy Version: 2.0.0
77
-- Format: Lua LS library file
88
--#endregion
@@ -148,6 +148,10 @@ function Entity.get_name() end
148148
---@return string
149149
function Entity.get_type_id() end
150150

151+
--- Gets the the entity type that this entity is.
152+
---@return EntityType
153+
function Entity.get_type() end
154+
151155
--- Returns true if this entity is a LivingEntity.
152156
---@return boolean
153157
function Entity.is_living() end
@@ -157,11 +161,11 @@ function Entity.is_living() end
157161
function Entity.as_living() end
158162

159163
--- Returns true if this entity is a PlayerEntity.
160-
---@return LivingEntity
164+
---@return boolean
161165
function Entity.is_player() end
162166

163167
--- Return this entity as a PlayerEntity.
164-
---@return LivingEntity
168+
---@return Player
165169
function Entity.as_player() end
166170

167171
--- Execute a commmand as this entity.
@@ -218,7 +222,7 @@ function Player.tell(msg) end
218222
--- Prints a text component to this player's chat.
219223
---@param msg TextComponent Component to display.
220224
---@return nil
221-
function Player.tell(msg) end
225+
function Player.tell_component(msg) end
222226

223227
--- Gives this player this stack.
224228
---@param stack ItemStack Stack to give.
@@ -255,10 +259,14 @@ function EntityType.is_fire_immune() end
255259

256260
--- Spawns a new instance of this entity.
257261
---@param pos Vec3d Position to spawn at.
258-
---@param dimension string Id of dimension to spawn in.
262+
---@param dimension string | nil Id of dimension to spawn in.
259263
---@return Entity
260264
function EntityType.spawn(pos, dimension) end
261265

266+
--- Returns the id of this entity type.
267+
---@return string
268+
function EntityType.get_id() end
269+
262270

263271
--- An item stack.
264272
---@class ItemStack
@@ -352,15 +360,15 @@ minecraft = {}
352360
---@return string
353361
function minecraft.get_version() end
354362

355-
--- Prints an unformatted line to the server chat, visible to all players. (similar to /tellraw). Also prints to the console.
363+
--- Prints an unformatted line to the command source.
356364
---@param message string Message to be printed.
357365
---@return nil
358-
function minecraft.print(message) end
366+
function minecraft.feedback(message) end
359367

360-
--- Prints a text component to the server chat, visible to all players. (similar to /tellraw). Also prints the raw string to the console.
368+
--- Prints a text component to the command source.
361369
---@param message string Message to be printed.
362370
---@return nil
363-
function minecraft.print_component(message) end
371+
function minecraft.feedback_component(message) end
364372

365373
--- Executes the given command from the server command source. Returns the result of the command.
366374
---@param command string Command to be executed.
@@ -511,6 +519,19 @@ function text.atlas_sprite(atlas, sprite) end
511519
---@return TextComponent
512520
function text.compound(elements) end
513521

522+
nbtstorage = {}
523+
524+
--- Reads abstract NBT data from storage.
525+
---@param id string Storage id to read.
526+
---@return table
527+
function nbtstorage.read(id) end
528+
529+
--- Writes abstract NBT data to storage.
530+
---@param id string Storage id to write to.
531+
---@param table table Data to write.
532+
---@return nil
533+
function nbtstorage.write(id, table) end
534+
514535
--#endregion
515536

516537
--#region Script Event

build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ loom {
3232
sourceSet sourceSets.client
3333
}
3434
}
35-
3635
}
3736

3837
dependencies {

src/main/java/dev/diamond/luafy/LuafyCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ private static class ScriptIdsSuggestionProvider implements SuggestionProvider<C
219219
public CompletableFuture<Suggestions> getSuggestions(CommandContext<CommandSourceStack> ctx, SuggestionsBuilder builder) throws CommandSyntaxException {
220220

221221
for (String id : Luafy.SCRIPT_MANAGER.getScriptIdStrings()) {
222+
if (Luafy.SCRIPT_MANAGER.get(Identifier.parse(id)).onLibPath) continue; // dont suggest libraries
222223
builder.suggest(id);
223224
}
224225

src/main/java/dev/diamond/luafy/lua/ScriptFunction.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,13 @@ public <T, X> T nextMap(T def, Function<LuaValue, X> getter, Function<X, T> conv
190190
return map(next(), def, getter, convert);
191191
}
192192

193+
public <T, X> T map(LuaValue value, Function<LuaValue, X> getter, Function<X, T> convert) {
194+
return convert.apply(getter.apply(value));
195+
}
196+
197+
public <T, X> T nextMap(Function<LuaValue, X> getter, Function<X, T> convert) {
198+
return map(next(), getter, convert);
199+
}
200+
193201
}
194202
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package dev.diamond.luafy.mixin;
2+
3+
import net.minecraft.commands.CommandSource;
4+
import net.minecraft.commands.CommandSourceStack;
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.gen.Accessor;
7+
8+
@Mixin(CommandSourceStack.class)
9+
public interface CommandSourceStackAccessor {
10+
@Accessor("source")
11+
CommandSource accessCommandSource();
12+
}

src/main/java/dev/diamond/luafy/registry/ScriptPlugins.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class ScriptPlugins {
3333
public static ApiScriptPlugin<MathApi> MATH = new ApiScriptPlugin<>(MathApi::new);
3434
public static ApiScriptPlugin<FabricApi> FABRIC = new ApiScriptPlugin<>(FabricApi::new);
3535
public static ApiScriptPlugin<TextApi> TEXT = new ApiScriptPlugin<>(TextApi::new);
36+
public static ApiScriptPlugin<NbtStorageApi> NBT_STORAGE = new ApiScriptPlugin<>(NbtStorageApi::new);
3637

3738
public static void registerAll() {
3839
Registry.register(LuafyRegistries.SCRIPT_PLUGINS, Luafy.id_luaj("jse_base"), LUAJ_JSE_BASE);
@@ -53,5 +54,6 @@ public static void registerAll() {
5354
Registry.register(LuafyRegistries.SCRIPT_PLUGINS, Luafy.id("math"), MATH);
5455
Registry.register(LuafyRegistries.SCRIPT_PLUGINS, Luafy.id("fabric"), FABRIC);
5556
Registry.register(LuafyRegistries.SCRIPT_PLUGINS, Luafy.id("text"), TEXT);
57+
Registry.register(LuafyRegistries.SCRIPT_PLUGINS, Luafy.id("nbt_storage"), NBT_STORAGE);
5658
}
5759
}

src/main/java/dev/diamond/luafy/resource/ScriptResourceLoader.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
public class ScriptResourceLoader implements SimpleSynchronousResourceReloadListener {
1414

1515
private static final String PATH = "luafy/scripts";
16+
private static final String LIBRARY_PATH = "lib";
1617
private static final String EXT = ".lua";
1718

1819
@Override
@@ -33,11 +34,11 @@ public void onResourceManagerReload(ResourceManager manager) {
3334
String s = new String(bytes, StandardCharsets.UTF_8);
3435

3536

36-
String fixedPath = loc.getPath().substring(PATH.length() + 1, loc.getPath().length() - EXT.length());
37-
Identifier id = Identifier.fromNamespaceAndPath(loc.getNamespace(), fixedPath);
37+
Identifier id = idFromBadPath(loc.getPath(), loc.getNamespace());
38+
3839

3940
// get script
40-
Luafy.SCRIPT_MANAGER.loadScript(id, new LuaScript(s));
41+
Luafy.SCRIPT_MANAGER.loadScript(id, new LuaScript(s), id.getPath().startsWith(LIBRARY_PATH + "/"));
4142
count += 1;
4243

4344
} catch (Exception e) {
@@ -48,4 +49,9 @@ public void onResourceManagerReload(ResourceManager manager) {
4849

4950
Luafy.LOGGER.info("Loaded {} scripts", count);
5051
}
52+
53+
public static Identifier idFromBadPath(String badPath, String namespace) {
54+
String fixedPath = badPath.substring(PATH.length() + 1, badPath.length() - EXT.length());
55+
return Identifier.fromNamespaceAndPath(namespace, fixedPath);
56+
}
5157
}

src/main/java/dev/diamond/luafy/script/LuaScript.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@
22

33
import dev.diamond.luafy.Luafy;
44
import dev.diamond.luafy.lua.LuaTableBuilder;
5+
import dev.diamond.luafy.mixin.CommandSourceStackAccessor;
56
import dev.diamond.luafy.registry.LuafyRegistries;
7+
import dev.diamond.luafy.resource.ScriptResourceLoader;
68
import dev.diamond.luafy.script.enumeration.ScriptEnum;
79
import net.minecraft.core.Registry;
10+
import net.minecraft.network.chat.Component;
811
import net.minecraft.resources.Identifier;
912
import net.minecraft.resources.ResourceKey;
13+
import net.minecraft.server.level.ServerPlayer;
1014
import org.jetbrains.annotations.NotNull;
1115
import org.jetbrains.annotations.Nullable;
12-
import org.luaj.vm2.Globals;
13-
import org.luaj.vm2.LuaError;
14-
import org.luaj.vm2.LuaTable;
15-
import org.luaj.vm2.LuaValue;
16+
import org.luaj.vm2.*;
1617

18+
import java.io.BufferedInputStream;
19+
import java.io.ByteArrayInputStream;
20+
import java.io.FileInputStream;
21+
import java.io.StringReader;
22+
import java.nio.charset.StandardCharsets;
1723
import java.util.HashMap;
1824
import java.util.concurrent.Future;
1925
import net.minecraft.commands.CommandSourceStack;
@@ -23,6 +29,8 @@ public class LuaScript {
2329
public static final String CONTEXT_KEY = "ctx";
2430

2531
private final Globals globals;
32+
private final String source;
33+
public boolean onLibPath;
2634
private LuaValue script;
2735
private String compilationError;
2836
private CommandSourceStack src;
@@ -31,6 +39,8 @@ public class LuaScript {
3139

3240
public LuaScript(String source) {
3341
this.globals = new Globals();
42+
this.onLibPath = false;
43+
this.source = source;
3444
injectSources();
3545

3646
this.unserializableDataReferences = new HashMap<>();
@@ -86,11 +96,22 @@ public void setSource(@NotNull CommandSourceStack src) {
8696
this.src = src.withSuppressedOutput();
8797
}
8898

99+
public void sendSourceMessage(Component c) {
100+
ServerPlayer serverPlayer = this.src.getPlayer();
101+
if (serverPlayer != null) {
102+
serverPlayer.sendSystemMessage(c);
103+
} else {
104+
((CommandSourceStackAccessor)this.src).accessCommandSource().sendSystemMessage(c);
105+
}
106+
}
107+
89108
private void injectSources() {
90109
for (ScriptPlugin plugin : LuafyRegistries.SCRIPT_PLUGINS) {
91110
plugin.apply(this);
92111
}
93112

113+
modifyRequire(this);
114+
94115
for (ScriptEnum<?> e : LuafyRegistries.SCRIPT_ENUMS) {
95116
this.globals.set(e.getArgtypeString(), LuaTableBuilder.provide(b -> {
96117
for (var key : e.getEnumKeys()) b.add(key, key);
@@ -116,5 +137,13 @@ private ScriptExecutionResult executor(@NotNull CommandSourceStack src, @Nullabl
116137
}
117138
}
118139

140+
private static void modifyRequire(LuaScript script) {
141+
script.globals.finder = s -> {
142+
String namespace = ""; // todo get somehow
143+
Identifier id = ScriptResourceLoader.idFromBadPath(s, namespace);
144+
return new ByteArrayInputStream(Luafy.SCRIPT_MANAGER.get(id).source.getBytes(StandardCharsets.UTF_8));
145+
};
146+
}
147+
119148

120149
}

src/main/java/dev/diamond/luafy/script/ScriptManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public Future<ScriptExecutionResult> submitExecution(Callable<ScriptExecutionRes
2525
return this.scriptExecutor.submit(scriptExecution);
2626
}
2727

28-
public void loadScript(Identifier id, LuaScript script) {
28+
public void loadScript(Identifier id, LuaScript script, boolean libraryPath) {
29+
script.onLibPath = libraryPath;
2930
this.scripts.put(id, script);
3031
}
3132

0 commit comments

Comments
 (0)