Skip to content

Commit

Permalink
the future is now old man
Browse files Browse the repository at this point in the history
  • Loading branch information
PoolloverNathan committed Apr 5, 2024
1 parent 32dc32b commit b31ffdd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import org.figuramc.figura.FiguraMod;
import org.figuramc.figura.lua.api.data.FiguraFuture;
import org.figuramc.figura.lua.docs.LuaMethodOverload;
import org.figuramc.figura.utils.ColorUtils;
import org.luaj.vm2.LuaError;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.*;
import org.figuramc.figura.avatar.Avatar;
import org.figuramc.figura.config.Configs;
import org.figuramc.figura.lua.LuaWhitelist;
import org.figuramc.figura.lua.docs.LuaFieldDoc;
import org.figuramc.figura.lua.docs.LuaMethodDoc;
import org.figuramc.figura.lua.docs.LuaTypeDoc;
import org.figuramc.figura.permissions.Permissions;
import org.luaj.vm2.lib.OneArgFunction;
import org.luaj.vm2.lib.ZeroArgFunction;

import java.io.*;
import java.net.MalformedURLException;
Expand All @@ -25,6 +27,7 @@
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Locale;
import java.util.function.Consumer;

@LuaWhitelist
@LuaTypeDoc(
Expand Down Expand Up @@ -238,6 +241,36 @@ public String toString() {
}
}

@LuaWhitelist
@LuaMethodDoc("net.new_future")
public Varargs newFuture(LuaFunction k) {
final var fut = new FiguraFuture<LuaValue>(owner);
final var mgr = owner.luaRuntime.typeManager;
final var ret = mgr.javaToLua(fut).arg1();
final var onc = new OneArgFunction() {
@Override
public LuaValue call(LuaValue val) {
fut.complete(val);
return ret;
}
};
final var one = new OneArgFunction() {
@Override
public LuaValue call(LuaValue err) {
fut.error(new LuaError(err));
return ret;
}
};
if (k != null) {
try {
k.call(onc, one);
} catch (Throwable t) {
fut.error(t);
}
}
return LuaValue.varargsOf(new LuaValue[] {ret, onc, one});
}

@Override
public String toString() {
return "NetworkingAPI";
Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/assets/figura/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -1794,6 +1794,7 @@
"figura.docs.net.socket": "Instance of SocketAPI",
"figura.docs.net.is_networking_allowed": "Checks if your avatar can use networking features. Always false if networking is OFF in settings",
"figura.docs.net.is_link_allowed": "Checks if specified link allowed for usage in networking api",
"figura.docs.net.new_future": "Creates a new future controlled by Lua code\nReturns 3 values: the created future, a function to complete the future, and a function to make the future error\nIf an (optional) function is supplied, it will be called with the latter two functions, like JavaScript\nAny error that this function throws will automatically cause the future to fail",
"figura.docs.http": "A global API that contains HTTP related features",
"figura.docs.http.request": "Creates request builder for specified URI",
"figura.docs.http_request_builder": "A builder for HTTP request",
Expand Down

0 comments on commit b31ffdd

Please sign in to comment.