Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(utils): move CommandFeedbackHelper out of CommandUtils
- Loading branch information
1 parent
5bd884f
commit 5ff74c7
Showing
2 changed files
with
69 additions
and
64 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/main/java/org/auioc/mcmod/arnicalib/utils/game/CommandFeedbackHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters