tinycodinglm / mc /mc-0030.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame Contribute Delete
3.49 kB
opencode trace
System You are an expert SpongePowered Mixin internals developer. You understand how mixins work at the bytecode level using ASM, and can debug, write, and optimize mixins.

You are an expert SpongePowered Mixin internals developer. You understand how mixins work at the bytecode level using ASM, and can debug, write, and optimize mixins.

User

How do I write a MixinPlugin to conditionally apply mixins based on runtime conditions?

Assistant

IMixinConfigPlugin controls whether mixin configs are applied at runtime. Here's the implementation:

Plugin class:

java
public class ConditionalMixinPlugin implements IMixinConfigPlugin {
    private boolean fabricApiLoaded;
    private boolean isClient;
    
    @Override
    public void onLoad(String mixinPackage) {
        // Called when the plugin is loaded
        fabricApiLoaded = isClassAvailable("net.fabricmc.loader.api.FabricLoader");
        isClient = fabricApiLoaded && 
            FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT;
    }
    
    @Override
    public String getRefMapperConfig() {
        return null; // Use default refmap
    }
    
    @Override
    public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
        // Control which mixins are applied
        if (mixinClassName.contains("Client")) {
            return isClient;
        }
        if (mixinClassName.contains("Server")) {
            return !isClient;
        }
        if (mixinClassName.contains("FabricApi")) {
            return fabricApiLoaded;
        }
        return true; // Apply by default
    }
    
    @Override
    public void acceptTargets(Set<String> myTargets, Set<String> otherTargets) { }
    
    @Override
    public List<String> getMixins() {
        // Dynamically add/remove mixins from the config
        List<String> mixins = new ArrayList<>();
        if (isClient) {
            mixins.add("ClientPlayerMixin");
        }
        if (fabricApiLoaded) {
            mixins.add("FabricCompatMixin");
        }
        return mixins;
    }
    
    @Override
    public void preApply(String targetClassName, ClassNode targetClass, 
                         String mixinClassName, IMixinInfo mixinInfo) { }
    
    @Override
    public void postApply(String targetClassName, ClassNode targetClass, 
                          String mixinClassName, IMixinInfo mixinInfo) { }
    
    private boolean isClassAvailable(String className) {
        try {
            Class.forName(className, false, getClass().getClassLoader());
            return true;
        } catch (ClassNotFoundException e) {
            return false;
        }
    }
}

Mixin config JSON:

json
{
  "required": false,
  "plugin": "com.example.ConditionalMixinPlugin",
  "mixins": [],
  "client": [
    "ClientOnlyMixin"
  ],
  "server": [
    "ServerOnlyMixin"
  ]
}

fabric.mod.json reference:

json
{
  "mixins": [
    "mymod.mixins.json"
  ],
  "depends": {
    "fabricloader": ">=0.14.0"
  }
}

The getMixins() method can dynamically add mixins to the config, while shouldApplyMixin() filters individual mixins. The plugin is called during mixin bootstrap, before any transformations occur.