Minecraft Fabric Client 教程 #5 添加Event、Sprint和ToggleCommand

添加Event

下载event.zip

放在cn.enaium.excel

然后在Excel.java里面添加EventManager

public enum Excel {
    
    [...]
    public EventManager eventManager;

    public void onEnable() {
        eventManager = new EventManager();
        commandManager = new CommandManager();
        moduleManager = new ModuleManager();

        moduleManager.loadModules();
    }
    [...]

}

Module.java里面添加EventonEnable()onDisable()Toggle()


public class Module {
    
    [...]

    public void Toggle() {
        this.toggled = !this.toggled;
        if (this.toggled) {
            onEnable();
        } else {
            onDisable();
        }
    }

    public void onEnable() {
        Excel.INSTANCE.eventManager.register(this);
    }

    public void onDisable() {
        Excel.INSTANCE.eventManager.unregister(this);
    }

}

注入Mixin

ClientPlayerEntityMixin.java 里面添加以下内容

@Mixin(ClientPlayerEntity.class)
public class ClientPlayerEntityMixin {

    [...]

    @Inject(method = "tick", at = @At("HEAD"))
    private void preTick(CallbackInfo callbackInfo) {
        new EventUpdate().call();
    }
}

添加Sprint

cn.enaium.excel.module里面新建一个modules.movement

movement包里面新建Sprint

内容

package cn.enaium.excel.module.modules.movement;

import cn.enaium.excel.event.EventTarget;
import cn.enaium.excel.module.Category;
import cn.enaium.excel.module.Module;
import net.minecraft.client.MinecraftClient;

/**
 * @Author Enaium
 * @Date 2020/1/20 13:46
 */
public class Sprint extends Module {
    public Sprint() {
        super("Sprint", Category.MOVEMENT);
    }

    @EventTarget
    public void onUpdate(EventUpdate e) {
        MinecraftClient.getInstance().player.setSprinting(true);
    }
}

然后添加到ModuleManager.java里面

public class ModuleManager {
    [...]

    public void loadModules() {
        addModule(new Sprint());
    }
}

添加ToggleCommand

ModuleManager.java里面添加getModules()

public class ModuleManager {
    ArrayList<Module> modules;

    [...]

    public Module getModule(String name) {
        for (Module m : modules) {
            if (m.getName().equalsIgnoreCase(name))
                return m;
        }
        return null;
    }
}

cn.enaium.excel.command.commands包里面新建ToggleCommand

内容


package cn.enaium.excel.command.commands;

import cn.enaium.excel.Excel;
import cn.enaium.excel.command.Command;
import cn.enaium.excel.module.Module;
import cn.enaium.excel.utils.ChatUtils;

public class ToggleCommand implements Command {

    @Override
    public boolean run(String[] args) {

        if (args.length == 2) {

            Module module = Excel.INSTANCE.moduleManager.getModule(args[1]);

            if (module == null) {
                ChatUtils.message("The module with the name " + args[1] + " does not exist.");
                return true;
            }

            module.Toggle();

            return true;
        }


        return false;
    }

    @Override
    public String usage() {
        return "USAGE: -toggle [module]";
    }
}

然后添加到CommandManager.java里面


public class CommandManager {
    [...]
        public void loadCommands() {
        [...]
        commands.put(new String[]{"toggle","t"},new ToggleCommand());
    }
    [...]
}

完成!