Skip to content

Commit

Permalink
fix commands and use new api
Browse files Browse the repository at this point in the history
  • Loading branch information
Boxadactle committed Jun 8, 2024
1 parent 44c6e07 commit 4459b6a
Show file tree
Hide file tree
Showing 22 changed files with 238 additions and 405 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package dev.boxadactle.macrocraft;

import dev.boxadactle.boxlib.command.BCommandManager;
import dev.boxadactle.boxlib.config.BConfigClass;
import dev.boxadactle.boxlib.config.BConfigHandler;
import dev.boxadactle.boxlib.util.ModLogger;
import dev.boxadactle.macrocraft.command.MacroCommand;
import dev.boxadactle.macrocraft.config.MacroCraftConfig;
import dev.boxadactle.macrocraft.fs.MacroFile;
import dev.boxadactle.macrocraft.macro.MacroState;
Expand All @@ -17,7 +19,7 @@
public class MacroCraft {
public static final String MOD_NAME = "MacroCraft";
public static final String MOD_ID = "macrocraft";
public static final String VERSION = "3.0.0";
public static final String VERSION = "4.1.0";
public static final String VERSION_STRING = MOD_NAME + " v" + VERSION;

public static final ModLogger LOGGER = new ModLogger(MOD_NAME);
Expand All @@ -32,6 +34,9 @@ public static void init() {

LOGGER.info("Loading configuration...");
CONFIG = BConfigHandler.registerConfig(MacroCraftConfig.class);

LOGGER.info("Registering client commands...");
BCommandManager.register(MacroCommand.create());
}

public static String formatDate(long timestamp) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package dev.boxadactle.macrocraft.command;

import com.mojang.brigadier.context.CommandContext;
import dev.boxadactle.boxlib.command.BCommandSourceStack;
import dev.boxadactle.boxlib.command.api.BClientCommand;
import dev.boxadactle.boxlib.scheduling.Scheduling;
import dev.boxadactle.boxlib.util.ClientUtils;
import dev.boxadactle.macrocraft.config.MacroCraftConfigScreen;
import dev.boxadactle.macrocraft.gui.MacroListScreen;

public class MacroCommand {

public static BClientCommand create() {
return BClientCommand.create("macro", MacroCommand::openList)
.registerSubcommand("config", MacroCommand::openConfig)
.registerSubcommand(new PlaySubcommand())
.registerSubcommand(new RecordSubcommand())
.registerSubcommand(new SaveSubcommand());
}

public static int openList(CommandContext<BCommandSourceStack> ignored) {
// we have to delay because when the command is run it sets the screen to null
Scheduling.schedule(1, () -> ClientUtils.setScreen(new MacroListScreen(null)));

return 0;

}

public static int openConfig(CommandContext<BCommandSourceStack> ignored) {
Scheduling.schedule(1, () -> ClientUtils.setScreen(new MacroCraftConfigScreen(null)));

return 0;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dev.boxadactle.macrocraft.command;

import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import dev.boxadactle.boxlib.command.BCommandManager;
import dev.boxadactle.boxlib.command.BCommandSourceStack;
import dev.boxadactle.boxlib.command.api.BClientSubcommand;
import dev.boxadactle.boxlib.scheduling.Scheduling;
import dev.boxadactle.boxlib.util.ClientUtils;
import dev.boxadactle.macrocraft.MacroCraft;
import dev.boxadactle.macrocraft.gui.MacroPlayScreen;
import dev.boxadactle.macrocraft.macro.MacroState;
import net.minecraft.network.chat.Component;

public class PlaySubcommand implements BClientSubcommand {

@Override
public ArgumentBuilder<BCommandSourceStack, ?> getSubcommand() {
return BCommandManager.literal("play").executes(this::onPlayCommand);
}

@Override
public void build(ArgumentBuilder<BCommandSourceStack, ?> builder) {
builder.then(BCommandManager.literal("gui").executes(this::openPlayGui));
}

private int onPlayCommand(CommandContext<BCommandSourceStack> ignored) {
if (!MacroState.hasLoadedMacro()) {
MacroCraft.LOGGER.player.chat(Component.translatable("command.macrocraft.notLoaded"));
return 0;
}

MacroState.LOADED_MACRO.playMacro();

return 0;
}

private int openPlayGui(CommandContext<BCommandSourceStack> ignored) {
Scheduling.schedule(1, () -> ClientUtils.setScreen(new MacroPlayScreen(null)));

return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package dev.boxadactle.macrocraft.command;

import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import dev.boxadactle.boxlib.command.BCommandManager;
import dev.boxadactle.boxlib.command.BCommandSourceStack;
import dev.boxadactle.boxlib.command.api.BClientSubcommand;
import dev.boxadactle.boxlib.scheduling.Scheduling;
import dev.boxadactle.boxlib.util.ClientUtils;
import dev.boxadactle.macrocraft.MacroCraft;
import dev.boxadactle.macrocraft.gui.MacroRecordScreen;
import dev.boxadactle.macrocraft.macro.MacroState;
import net.minecraft.network.chat.Component;

public class RecordSubcommand implements BClientSubcommand {
@Override
public ArgumentBuilder<BCommandSourceStack, ?> getSubcommand() {
return BCommandManager.literal("record").executes(this::onRecordCommand);
}

@Override
public void build(ArgumentBuilder<BCommandSourceStack, ?> builder) {
builder.then(BCommandManager.literal("start").executes(this::onStartRecord));

builder.then(BCommandManager.literal("stop").executes(this::onStopRecord));

builder.then(BCommandManager.literal("pause").executes(this::onPauseRecord));
}

private int onRecordCommand(CommandContext<BCommandSourceStack> ignored) {
Scheduling.schedule(1, () -> ClientUtils.setScreen(new MacroRecordScreen(null)));

return 0;
}

private int onStartRecord(CommandContext<BCommandSourceStack> ignored) {
if (MacroState.IS_RECORDING) {
MacroCraft.LOGGER.player.chat(Component.translatable("command.macrocraft.recording.already"));

return 1;
}

if (MacroState.IS_PAUSED) {
MacroState.resumeRecording();

return 0;
}

MacroState.startRecording();

MacroCraft.LOGGER.player.chat(Component.translatable("command.macrocraft.recording.started"));

return 0;
}

private int onStopRecord(CommandContext<BCommandSourceStack> ignored) {
if (!MacroState.IS_RECORDING) {
MacroCraft.LOGGER.player.chat(Component.translatable("command.macrocraft.recording.not"));

return 1;
}

MacroState.stopRecording();

return 0;
}

private int onPauseRecord(CommandContext<BCommandSourceStack> ignored) {
if (!MacroState.IS_RECORDING) {
MacroCraft.LOGGER.player.chat(Component.translatable("command.macrocraft.recording.not"));

return 1;
}

MacroState.pauseRecording();

return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package dev.boxadactle.macrocraft.command;

import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import dev.boxadactle.boxlib.command.BCommandManager;
import dev.boxadactle.boxlib.command.BCommandSourceStack;
import dev.boxadactle.boxlib.command.api.BClientSubcommand;
import dev.boxadactle.boxlib.scheduling.Scheduling;
import dev.boxadactle.boxlib.util.ClientUtils;
import dev.boxadactle.macrocraft.MacroCraft;
import dev.boxadactle.macrocraft.fs.MacroFile;
import dev.boxadactle.macrocraft.gui.MacroSaveScreen;
import dev.boxadactle.macrocraft.macro.MacroState;
import net.minecraft.network.chat.Component;

public class SaveSubcommand implements BClientSubcommand {
@Override
public ArgumentBuilder<BCommandSourceStack, ?> getSubcommand() {
return BCommandManager.literal("save").executes(this::save);
}

@Override
public void build(ArgumentBuilder<BCommandSourceStack, ?> builder) {
builder.then(
BCommandManager.argument("filename", StringArgumentType.string())
.executes(this::saveFile)
);
}

private int save(CommandContext<BCommandSourceStack> ignored) {
if (MacroState.LOADED_MACRO == null) {
MacroCraft.LOGGER.player.chat(Component.translatable("command.macrocraft.notLoaded"));

return 1;
}

Scheduling.schedule(1, () -> ClientUtils.setScreen(new MacroSaveScreen(null)));

return 0;
}

private int saveFile(CommandContext<BCommandSourceStack> context) {
if (!MacroState.hasLoadedMacro()) {
MacroCraft.LOGGER.player.chat(Component.translatable("command.macrocraft.notLoaded"));
return 1;
}

String filename = StringArgumentType.getString(context, "filename");

if (filename.isBlank()) {
return save(context);
}

if (!filename.endsWith(MacroFile.MACRO_EXTENSION)) {
filename += MacroFile.MACRO_EXTENSION;
}

MacroCraft.LOGGER.player.chat(Component.translatable("command.macrocraft.saving"));

MacroFile.saveMacro(filename, MacroState.LOADED_MACRO);

MacroCraft.LOGGER.player.chat(Component.translatable("command.macrocraft.saved", filename));

return 0;
}
}
4 changes: 3 additions & 1 deletion common/src/main/resources/assets/macrocraft/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,7 @@
"command.macrocraft.recording.already": "You are already recording a macro",
"command.macrocraft.recording.not": "You are not recording a macro",
"command.macrocraft.recording.started": "Recording macro...",
"command.macrocraft.notLoaded": "No macro is loaded!"
"command.macrocraft.notLoaded": "No macro is loaded!",
"command.macrocraft.saving": "Saving macro...",
"command.macrocraft.saved": "Macro saved as \"%s\""
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package dev.boxadactle.macrocraft.fabric;

import dev.boxadactle.boxlib.fabric.command.BCommandManager;
import dev.boxadactle.macrocraft.MacroCraft;
import dev.boxadactle.macrocraft.MacroCraftKeybinds;
import dev.boxadactle.macrocraft.fabric.command.ConfigCommand;
import dev.boxadactle.macrocraft.fabric.command.PlayCommand;
import dev.boxadactle.macrocraft.fabric.command.RecordCommand;
import dev.boxadactle.macrocraft.fabric.command.SaveCommand;
import dev.boxadactle.macrocraft.gui.MacroListScreen;
import dev.boxadactle.macrocraft.hud.MacroPlayHud;
import dev.boxadactle.macrocraft.hud.MacroRecordHud;
Expand All @@ -28,7 +23,6 @@ public void onInitialize() {
HudRenderCallback.EVENT.register(this::renderMacroHud);

initKeybinds();
initCommands();
}

private void initKeybinds() {
Expand All @@ -39,15 +33,6 @@ private void initKeybinds() {
KeyBindingHelper.registerKeyBinding(MacroCraftKeybinds.openMacroList);
}

private void initCommands() {
BCommandManager.registerCommand("macro", (list) -> {
list.add(ConfigCommand::new);
list.add(PlayCommand::new);
list.add(RecordCommand::new);
list.add(SaveCommand::new);
});
}

private void tick(Minecraft client) {
MacroState.tick();

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 4459b6a

Please sign in to comment.