Skip to content

Commit

Permalink
refactor(utils): move CommandFeedbackHelper out of CommandUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Apr 11, 2022
1 parent 5bd884f commit 5ff74c7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 64 deletions.
@@ -0,0 +1,69 @@
package org.auioc.mcmod.arnicalib.utils.game;

import static org.auioc.mcmod.arnicalib.utils.game.TextUtils.EmptyText;
import static org.auioc.mcmod.arnicalib.utils.game.TextUtils.I18nText;
import java.util.function.Function;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.MutableComponent;

public class CommandFeedbackHelper {

private final MutableComponent prefix;
private final Function<String, String> i18n;

public CommandFeedbackHelper(MutableComponent prefix, Function<String, String> i18n) {
this.prefix = prefix;
this.i18n = i18n;
}

public CommandFeedbackHelper(Function<String, String> i18n) {
this(EmptyText(), i18n);
}

public MutableComponent createMessage(String key) {
return EmptyText().append(this.prefix).append(I18nText(this.i18n.apply(key)));
}

public MutableComponent createMessage(String key, Object... args) {
return EmptyText().append(this.prefix).append(I18nText(this.i18n.apply(key), args));
}

public MutableComponent successMessage(String key) {
return this.createMessage("command." + key + ".success");
}

public MutableComponent successMessage(String key, Object... args) {
return this.createMessage("command." + key + ".success", args);
}

public MutableComponent failureMessage(String key) {
return this.createMessage("command." + key + ".failure");
}

public MutableComponent failureMessage(String key, Object... args) {
return this.createMessage("command." + key + ".failure", args);
}

public int success(CommandContext<CommandSourceStack> ctx, String key) {
ctx.getSource().sendSuccess(this.successMessage(key), false);
return Command.SINGLE_SUCCESS;
}

public int success(CommandContext<CommandSourceStack> ctx, String key, Object... args) {
ctx.getSource().sendSuccess(this.successMessage(key, args), false);
return Command.SINGLE_SUCCESS;
}

public int failure(CommandContext<CommandSourceStack> ctx, String key) {
ctx.getSource().sendFailure(this.failureMessage(key));
return Command.SINGLE_SUCCESS;
}

public int failure(CommandContext<CommandSourceStack> ctx, String key, Object... args) {
ctx.getSource().sendFailure(this.failureMessage(key, args));
return Command.SINGLE_SUCCESS;
}

}
@@ -1,13 +1,10 @@
package org.auioc.mcmod.arnicalib.utils.game;

import static org.auioc.mcmod.arnicalib.ArnicaLib.i18n;
import static org.auioc.mcmod.arnicalib.utils.game.TextUtils.EmptyText;
import static org.auioc.mcmod.arnicalib.utils.game.TextUtils.I18nText;
import java.lang.reflect.Field;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.ParsedCommandNode;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
Expand All @@ -17,7 +14,6 @@
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.commands.CommandSource;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.MutableComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

Expand Down Expand Up @@ -88,64 +84,4 @@ static String joinLiteralNodes(List<ParsedCommandNode<CommandSourceStack>> nodes
return joinLiteralNodes(nodes, 0, nodes.size(), true);
}

public static class CommandFeedbackHelper {

private final MutableComponent prefix;
private final Function<String, String> i18n;

public CommandFeedbackHelper(MutableComponent prefix, Function<String, String> i18n) {
this.prefix = prefix;
this.i18n = i18n;
}

public CommandFeedbackHelper(Function<String, String> i18n) {
this(EmptyText(), i18n);
}

public MutableComponent createMessage(String key) {
return EmptyText().append(this.prefix).append(I18nText(this.i18n.apply(key)));
}

public MutableComponent createMessage(String key, Object... args) {
return EmptyText().append(this.prefix).append(I18nText(this.i18n.apply(key), args));
}

public MutableComponent successMessage(String key) {
return this.createMessage("command." + key + ".success");
}

public MutableComponent successMessage(String key, Object... args) {
return this.createMessage("command." + key + ".success", args);
}

public MutableComponent failureMessage(String key) {
return this.createMessage("command." + key + ".failure");
}

public MutableComponent failureMessage(String key, Object... args) {
return this.createMessage("command." + key + ".failure", args);
}

public int success(CommandContext<CommandSourceStack> ctx, String key) {
ctx.getSource().sendSuccess(this.successMessage(key), false);
return Command.SINGLE_SUCCESS;
}

public int success(CommandContext<CommandSourceStack> ctx, String key, Object... args) {
ctx.getSource().sendSuccess(this.successMessage(key, args), false);
return Command.SINGLE_SUCCESS;
}

public int failure(CommandContext<CommandSourceStack> ctx, String key) {
ctx.getSource().sendFailure(this.failureMessage(key));
return Command.SINGLE_SUCCESS;
}

public int failure(CommandContext<CommandSourceStack> ctx, String key, Object... args) {
ctx.getSource().sendFailure(this.failureMessage(key, args));
return Command.SINGLE_SUCCESS;
}

}

}

0 comments on commit 5ff74c7

Please sign in to comment.