Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide macro output and add /silent command #298

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package tools.redstone.redstonetools.features.commands;

import com.google.auto.service.AutoService;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import tools.redstone.redstonetools.features.AbstractFeature;
import tools.redstone.redstonetools.features.Feature;
@AutoService(AbstractFeature.class)
@Feature(name = "Silent", description = "allows you to use commands without output", command = "silent")
public class SilentCommandFeature extends AbstractFeature{

@Override
protected void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher, boolean dedicated) {
var commandManager = CommandManager.literal("silent").redirect(dispatcher.getRoot()).executes( context -> {
return 1;
});
dispatcher.register(commandManager);
}
}
23 changes: 16 additions & 7 deletions src/main/java/tools/redstone/redstonetools/macros/Macro.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,31 @@
public class Macro {

public static Macro buildEmpty() {
return new Macro("",true,InputUtil.UNKNOWN_KEY,new ArrayList<>());
return new Macro("",true,InputUtil.UNKNOWN_KEY, new ArrayList<>(), true);
}

private KeyBinding keyBinding;
public String name;
private Key key;
public boolean enabled;
public boolean output;
public List<Action> actions;

private final Macro original;

public Macro(String name, boolean enabled, Key key, List<Action> actions) {
this(name,enabled,key,actions,null);
public Macro(String name, boolean enabled, Key key, List<Action> actions, boolean output) {
this(name,enabled,key,actions,output,null);
keyBinding = new KeyBinding("macro." + System.nanoTime(),-1,"macros");
registerKeyBinding();
changeKeyBindingKeyCode();
}

public Macro(String name, boolean enabled, Key key, List<Action> actions, Macro original) {
public Macro(String name, boolean enabled, Key key, List<Action> actions, boolean output, Macro original) {
this.name = name;
this.enabled = enabled;
this.key = key;
this.actions = actions;
this.output = output;
this.original = original;
}

Expand All @@ -59,7 +61,13 @@ public void run() {
}

for (Action action : actions) {
action.run();
if (output){
action.run();
}
else{
action.runSilent();
}

}
}

Expand All @@ -70,6 +78,7 @@ public void applyChangesToOriginal() {
original.enabled = enabled;
original.setKey(key);
original.actions = new ArrayList<>(actions);
original.output = output;
}

public boolean isCopy(){
Expand Down Expand Up @@ -99,7 +108,7 @@ public Key getKey(){
}

public Macro createCopy() {
return new Macro(name,enabled,key,new ArrayList<>(actions),this);
return new Macro(name,enabled,key,new ArrayList<>(actions),output,this);
}

public void unregisterKeyBinding(){
Expand All @@ -123,7 +132,7 @@ public boolean equals(Object obj) {
if (!actions.get(i).equals(macro.actions.get(i))) return false;
}

return macro.name.equals(name) && macro.key.equals(key) && macro.enabled == enabled;
return macro.name.equals(name) && macro.key.equals(key) && macro.enabled == enabled && macro.output == output;
}

return super.equals(obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.util.InputUtil;
import tools.redstone.redstonetools.RedstoneToolsClient;
import tools.redstone.redstonetools.macros.actions.Action;
import tools.redstone.redstonetools.macros.actions.CommandAction;

Expand Down Expand Up @@ -104,6 +105,7 @@ private JsonObject getMacroJson(Macro macro) {
return Json.createObjectBuilder()
.add("name", macro.name)
.add("enabled", macro.enabled)
.add("output", macro.output)
.add("key", macro.getKey().getTranslationKey())
.add("actions", actionsJson)
.build();
Expand Down Expand Up @@ -141,7 +143,7 @@ private Macro createCommandMacro(String name, String[] commands) {
actions[i] = new CommandAction(commands[i]);
}

return new Macro(name, true, InputUtil.UNKNOWN_KEY, List.of(actions));
return new Macro(name, true, InputUtil.UNKNOWN_KEY, List.of(actions), true);
}

private List<Macro> getMacrosFromJson(JsonArray macrosJson) {
Expand All @@ -157,10 +159,11 @@ private List<Macro> getMacrosFromJson(JsonArray macrosJson) {
private Macro getMacroFromJson(JsonObject macroJson) {
var name = macroJson.getString("name");
var enabled = macroJson.getBoolean("enabled");
var output = macroJson.getBoolean("output");
var key = macroJson.getString("key");
var actions = getActionsFromJson(macroJson.getJsonArray("actions"));

return new Macro(name, enabled, InputUtil.fromTranslationKey(key), actions);
return new Macro(name, enabled, InputUtil.fromTranslationKey(key), actions, output);
}

private List<Action> getActionsFromJson(JsonArray actionsJson) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

public abstract class Action {
public abstract void run();
public abstract void runSilent();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tools.redstone.redstonetools.macros.actions;

import net.minecraft.client.MinecraftClient;
import net.royawesome.jlibnoise.module.combiner.Min;
import tools.redstone.redstonetools.utils.CommandSourceUtils;

public class CommandAction extends Action {
public String command;
Expand All @@ -17,6 +19,22 @@ public void run() {
player.sendChatMessage(command.startsWith("/") ? command : "/" + command);
}

@Override
public void runSilent(){
MinecraftClient client = MinecraftClient.getInstance();
if (!client.isIntegratedServerRunning()){
run();
return;
}
var player = MinecraftClient.getInstance().player;
assert player != null;

if (command.startsWith("/")){
command = command.replace("/", "");
}
player.sendChatMessage("/" + "silent " + command);
}

@Override
public boolean equals(Object obj) {
if (obj instanceof CommandAction commandAction) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package tools.redstone.redstonetools.macros.gui.screen;

import net.minecraft.client.gui.screen.ConfirmScreen;
import net.minecraft.client.gui.widget.ClickableWidget;
import net.minecraft.util.math.MathHelper;
import net.minecraft.client.gui.widget.*;
import net.minecraft.loot.function.EnchantRandomlyLootFunction;
import tools.redstone.redstonetools.macros.Macro;
import tools.redstone.redstonetools.macros.MacroManager;
import tools.redstone.redstonetools.macros.actions.Action;
Expand All @@ -14,8 +14,6 @@
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ScreenTexts;
import net.minecraft.client.gui.screen.option.GameOptionsScreen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.client.option.GameOptions;
import net.minecraft.client.util.InputUtil;
import net.minecraft.client.util.InputUtil.Key;
Expand All @@ -39,7 +37,7 @@ public class MacroEditScreen extends GameOptionsScreen {
private TextFieldWidget nameField;
private ButtonWidget doneButton;
private ButtonWidget keyBindButton;

private CheckboxWidget outputToggle;
private boolean overlapped = false;
private boolean detectingKeycodeKey = false;

Expand Down Expand Up @@ -94,7 +92,7 @@ public void init() {
if (keyCode == InputUtil.UNKNOWN_KEY) text = Text.of("");
if ( KeyBindingUtils.isKeyAlreadyBound(keyCode) ) { text = new LiteralText(text.getString()).formatted(Formatting.RED); }

keyBindButton = new ButtonWidget(this.width / 2 + 26, 55, 75, 20, text, (button) -> {
keyBindButton = new ButtonWidget(this.width / 2 - 80, 55, 75, 20, text, (button) -> {
detectingKeycodeKey = true;
keyBindButton.setMessage((new LiteralText("> ")).append(keyBindButton.getMessage().shallowCopy().formatted(Formatting.YELLOW)).append(" <").formatted(Formatting.YELLOW));
});
Expand Down Expand Up @@ -130,6 +128,8 @@ public void init() {
commandList.setScrollAmount(scrollAmount);

this.addSelectableChild(commandList);
outputToggle = new CheckboxWidget(this.width / 2 + 168 - 20, 55, 20, 20, null, macro.output, false);
this.addDrawableChild(outputToggle);
}

private boolean canClickDone() {
Expand All @@ -149,7 +149,8 @@ public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {

drawCenteredText(matrices, this.textRenderer, this.title, this.width / 2, 8, 16777215);

drawCenteredText(matrices, this.textRenderer, "Key Bind", width / 2 - (99 - textRenderer.getWidth("Key Bind") / 2), 55 + textRenderer.fontHeight / 2, 16777215);
drawCenteredText(matrices, this.textRenderer, "Key Bind", width / 2 - (168 - textRenderer.getWidth("Key Bind") / 2), 55 + textRenderer.fontHeight / 2, 16777215);
drawCenteredText(matrices, this.textRenderer, "Command Output", width / 2 + (textRenderer.getWidth("Command Output") / 2) + 5, 55+ textRenderer.fontHeight / 2, 16777215);
nameField.render(matrices, mouseX, mouseY, delta);

if (nameField.getText().isEmpty() && !nameField.isFocused()) {
Expand All @@ -163,6 +164,7 @@ public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
public void tick() {
nameField.tick();
commandList.tick();
macro.output = outputToggle.isChecked();

super.tick();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package tools.redstone.redstonetools.mixin.macros;


import net.minecraft.entity.Entity;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(CommandManager.class)
public abstract class DisableCommandOutputMixin {

@Shadow public abstract int execute(ServerCommandSource commandSource, String command);

@Inject(method = "execute", at=@At("HEAD"), cancellable = true)
public void execute(ServerCommandSource commandSource, String command, CallbackInfoReturnable<Integer> cir){
if (command.contains("/silent ")) {
command = command.replace("/silent ", "");
System.out.println("silent ");
System.out.println(command);
command = command;
commandSource = commandSource.withSilent();

execute(commandSource, command);
cir.cancel();
}
}


}
3 changes: 3 additions & 0 deletions src/main/resources/redstonetools.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"features.CopyStateMixin",
"features.ItemBindMixin$ItemStackMixin",
"gamerules.DoContainerDropsMixin",
"macros.DisableCommandOutputMixin",
"telemetry.SendCommandMixin",
"telemetry.ShowTelemetryPopupMixin"
],
"client": [
"accessors.MinecraftClientAccessor",
Expand Down