Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Figura compatibility #76

Open
wants to merge 2 commits into
base: 1.19
Choose a base branch
from
Open

Conversation

GrandpaScout
Copy link

Fabric expects a public constructor with no parameters to be the entrypoint.

Figura will later use build(Avatar) to run the private SwitchyFiguraApi(Avatar) constructor.

From my quick testing this does not cause any issues.
Should fix #75

Fabric expects a public constructor with no parameters to be the entrypoint.

Figura will later use `build(Avatar)` to run the `private SwitchyFiguraApi(Avatar)` constructor.

Should fix sisby-folk#75
@GrandpaScout
Copy link
Author

I made a pull request to the 1.19 branch specifically since it is listed as the main branch but from what I can see this affects all versions.

@GrandpaScout GrandpaScout marked this pull request as draft March 17, 2024 17:25
@GrandpaScout
Copy link
Author

Seems like there's more issues with Figura compat that need addressed.

This is done by first checking if the avatar has errored and returning if it has.
Then the function invocation is wrapped in a try-catch and any errors are redirected to the avatar's Lua runtime.
@GrandpaScout GrandpaScout changed the title Fix entrypoint for figura_api. Fix Figura compatibility Mar 17, 2024
@GrandpaScout
Copy link
Author

GrandpaScout commented Mar 17, 2024

Considering the idea of turning this compat into a true Figura Event instead. Then users could just do

events["SWITCHY.SWITCH"]:register(function(newpreset, oldpreset, enabledmodules)
  -- blah blah
end)

and it would Just Work™

edit: I have decided to go against that idea mostly because Java is a pain to work with.

@GrandpaScout GrandpaScout marked this pull request as ready for review March 17, 2024 18:57
@sisby-folk
Copy link
Owner

Looks good to us - can you send through the reference / code you had for how proper figura events are set up? we couldl take a look.

@GrandpaScout
Copy link
Author

GrandpaScout commented Mar 18, 2024

I tried to make the code in the code block below which works fine until it tries to compile .getUser(), after which it throws an error in gradle. Something about missing the Minecraft Entity class. I think it probably has something to do with mappings but I'm not familiar enough with Minecraft modding to be sure.
There's probably a fix for this that I'm not smart enough to see.

FiguraMC has an example of how an event should be set up at
https://github.com/FiguraMC/Example-Figura-Plugin/blob/main/common/src/main/java/org/figuramc/exampleplugin/ExampleEventPlugin.java

The code below defines two events: SWITCHY.SWITCH for when someone switches in render distance of the current client, and SWITCHY.WORLD_SWITCH when someone switches as long as their avatar is loaded in by the current client.
These events use different instruction limits, WORLD_SWITCH uses the WORLD_TICK limit of 128 instructions and is meant for global changes like nameplates and SWITCH uses the TICK limit of around 32k instructions and is meant for full model changes.
The other reason for both of them is that WORLD_SWITCH might not have the current player loaded while SWITCH will always have the current player loaded.

./compat/src/main/java/folk/sisby/switchy/client/FiguraSwitchyEvent.java

package folk.sisby.switchy.client;

import folk.sisby.switchy.client.api.SwitchyClientEvents;
import com.mojang.datafixers.util.Pair;
import org.figuramc.figura.entries.FiguraEvent;
import org.figuramc.figura.entries.annotations.FiguraEventPlugin;
import org.figuramc.figura.lua.LuaWhitelist;
import org.figuramc.figura.lua.api.event.LuaEvent;
import org.figuramc.figura.lua.docs.LuaFieldDoc;
import org.figuramc.figura.avatar.AvatarManager;
import org.figuramc.figura.avatar.Avatar;

import java.util.Collection;
import java.util.List;

@FiguraEventPlugin
public class FiguraSwitchyEvent implements FiguraEvent {
  @LuaWhitelist
  @LuaFieldDoc("events.switchy.world_switch")
  public static LuaEvent WORLD_SWITCH = new LuaEvent();
  @LuaWhitelist
  @LuaFieldDoc("events.switchy.switch")
  public static LuaEvent SWITCH = new LuaEvent();

  @Override
  public String getID() {
    return "switchy";
  }

  static {
    SwitchyClientEvents.SWITCH.register((event) -> {
      Avatar avatar = AvatarManager.getAvatarForPlayer(event.player());

      if (avatar != null) {
        String newPreset = event.currentPreset();
        String oldPreset = event.previousPreset();
        List<String> enabledModules = event.enabledModules();

        avatar.run(WORLD_SWITCH, avatar.worldTick, newPreset, oldPreset, enabledModules);

        if (avatar.loaded && avatar.luaRuntime != null && avatar.luaRuntime.getUser() != null)
          avatar.run(SWITCH, avatar.tick, newPreset, oldPreset, enabledModules);
      }
    });
  }

  @Override
  public Collection<Pair<String, LuaEvent>> getEvents() {
    return List.of(
      new Pair<>("SWITCH", SWITCH),
      new Pair<>("WORLD_SWITCH", WORLD_SWITCH)
    );
  }
}

@sisby-folk sisby-folk added compat An improvement or fix for switchy-compat enhancement A new feature or request labels Apr 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compat An improvement or fix for switchy-compat enhancement A new feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SwitchyFiguraAPI broken on Fabric
2 participants