-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add get-exception command and /execute parameter
- Loading branch information
Showing
10 changed files
with
302 additions
and
3 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/main/java/dev/upcraft/mesh/mixin/impl/command/MixinCommandElement.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,47 @@ | ||
/* | ||
* Mesh | ||
* Copyright (C) 2019-2021 UpcraftLP | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; If not, see <https://www.gnu.org/licenses>. | ||
*/ | ||
package dev.upcraft.mesh.mixin.impl.command; | ||
|
||
import com.mojang.brigadier.CommandDispatcher; | ||
import com.mojang.brigadier.ParseResults; | ||
import dev.upcraft.mesh.util.command.CommandHelper; | ||
import net.minecraft.server.command.ServerCommandSource; | ||
import net.minecraft.server.function.CommandFunction; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Redirect; | ||
|
||
@Mixin(CommandFunction.CommandElement.class) | ||
public abstract class MixinCommandElement { | ||
|
||
@Redirect(method = "execute(Lnet/minecraft/server/function/CommandFunctionManager;Lnet/minecraft/server/command/ServerCommandSource;)I", at = @At(value = "INVOKE", target = "Lcom/mojang/brigadier/CommandDispatcher;execute(Lcom/mojang/brigadier/ParseResults;)I")) | ||
private int extractCommandException(CommandDispatcher<ServerCommandSource> commandDispatcher, ParseResults<ServerCommandSource> parse) throws Exception { | ||
int value = 0; | ||
Exception ex = null; | ||
String command = parse.getReader().getString(); | ||
try { | ||
value = commandDispatcher.execute(parse); | ||
} catch (Exception e) { | ||
ex = e; | ||
throw e; | ||
} finally { | ||
CommandHelper.recordCommand(command, value, ex); | ||
} | ||
return value; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/dev/upcraft/mesh/mixin/impl/command/MixinCommandManager.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,48 @@ | ||
/* | ||
* Mesh | ||
* Copyright (C) 2019-2021 UpcraftLP | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; If not, see <https://www.gnu.org/licenses>. | ||
*/ | ||
package dev.upcraft.mesh.mixin.impl.command; | ||
|
||
import com.mojang.brigadier.CommandDispatcher; | ||
import com.mojang.brigadier.StringReader; | ||
import dev.upcraft.mesh.util.command.CommandHelper; | ||
import net.minecraft.server.command.CommandManager; | ||
import net.minecraft.server.command.ServerCommandSource; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Redirect; | ||
|
||
@Mixin(CommandManager.class) | ||
public abstract class MixinCommandManager { | ||
|
||
@Redirect(method = "execute", at = @At(value = "INVOKE", target = "Lcom/mojang/brigadier/CommandDispatcher;execute(Lcom/mojang/brigadier/StringReader;Ljava/lang/Object;)I")) | ||
private int extractCommandException(CommandDispatcher<ServerCommandSource> commandDispatcher, StringReader input, Object commandSourceObj, ServerCommandSource source, String command) throws Exception { | ||
int value = 0; | ||
Exception ex = null; | ||
try { | ||
value = commandDispatcher.execute(input, source); | ||
} | ||
catch (Exception e) { | ||
ex = e; | ||
throw e; | ||
} | ||
finally { | ||
CommandHelper.recordCommand(command, value, ex); | ||
} | ||
return value; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/dev/upcraft/mesh/mixin/impl/command/MixinExecuteCommand.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,58 @@ | ||
/* | ||
* Mesh | ||
* Copyright (C) 2019-2021 UpcraftLP | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; If not, see <https://www.gnu.org/licenses>. | ||
*/ | ||
package dev.upcraft.mesh.mixin.impl.command; | ||
|
||
import com.mojang.brigadier.arguments.StringArgumentType; | ||
import com.mojang.brigadier.builder.ArgumentBuilder; | ||
import com.mojang.brigadier.builder.LiteralArgumentBuilder; | ||
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; | ||
import com.mojang.brigadier.tree.CommandNode; | ||
import dev.upcraft.mesh.util.command.CommandHelper; | ||
import net.minecraft.server.command.CommandManager; | ||
import net.minecraft.server.command.ExecuteCommand; | ||
import net.minecraft.server.command.ServerCommandSource; | ||
import org.spongepowered.asm.mixin.Final; | ||
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.CallbackInfoReturnable; | ||
|
||
import java.util.Locale; | ||
|
||
@Mixin(ExecuteCommand.class) | ||
public abstract class MixinExecuteCommand { | ||
|
||
@Shadow | ||
private static ArgumentBuilder<ServerCommandSource, ?> addConditionLogic(CommandNode<ServerCommandSource> root, ArgumentBuilder<ServerCommandSource, ?> builder, boolean positive, ExecuteCommand.Condition condition) { | ||
return null; | ||
} | ||
|
||
@Shadow @Final private static SimpleCommandExceptionType CONDITIONAL_FAIL_EXCEPTION; | ||
|
||
@Inject(method = "addConditionArguments", at = @At("HEAD")) | ||
private static void injectExceptionCondition(CommandNode<ServerCommandSource> root, LiteralArgumentBuilder<ServerCommandSource> argumentBuilder, boolean positive, CallbackInfoReturnable<ArgumentBuilder<ServerCommandSource, ?>> cir) { | ||
argumentBuilder.then(CommandManager.literal("exception").then(addConditionLogic(root, CommandManager.argument("exception_message", StringArgumentType.string()), positive, context -> { | ||
String msg = StringArgumentType.getString(context, "exception_message"); | ||
if(msg.isBlank()) { | ||
throw CONDITIONAL_FAIL_EXCEPTION.create(); | ||
} | ||
return CommandHelper.getLastCommandException().filter(ex -> ex.getMessage().toLowerCase(Locale.ROOT).contains(msg)).isPresent(); | ||
}))); | ||
} | ||
} |
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
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
85 changes: 85 additions & 0 deletions
85
src/main/java/dev/upcraft/mesh/util/command/mesh/GetExceptionCommand.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,85 @@ | ||
/* | ||
* Mesh | ||
* Copyright (C) 2019-2021 UpcraftLP | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; If not, see <https://www.gnu.org/licenses>. | ||
*/ | ||
package dev.upcraft.mesh.util.command.mesh; | ||
|
||
import com.mojang.brigadier.arguments.BoolArgumentType; | ||
import com.mojang.brigadier.builder.LiteralArgumentBuilder; | ||
import com.mojang.brigadier.context.CommandContext; | ||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
import dev.upcraft.mesh.util.command.CommandHelper; | ||
import net.minecraft.command.CommandException; | ||
import net.minecraft.server.command.CommandManager; | ||
import net.minecraft.server.command.ServerCommandSource; | ||
import net.minecraft.text.*; | ||
|
||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.util.Optional; | ||
|
||
public class GetExceptionCommand { | ||
|
||
public static LiteralArgumentBuilder<ServerCommandSource> append(LiteralArgumentBuilder<ServerCommandSource> $) { | ||
return $.then(CommandManager.literal("exception").requires(source -> source.hasPermissionLevel(2)).then(CommandManager.literal("get").executes(ctx -> getException(ctx, false)).then(CommandManager.argument("clearException", BoolArgumentType.bool()).executes(ctx -> { | ||
boolean clear = BoolArgumentType.getBool(ctx, "clearException"); | ||
return getException(ctx, clear); | ||
}))).then(CommandManager.literal("clear").executes(ctx -> { | ||
ctx.getSource().sendFeedback(new TranslatableText("command.mesh.get_exception.clear"), true); | ||
return CommandHelper.clearException() ? 1 : 0; | ||
}))); | ||
} | ||
|
||
private static int getException(CommandContext<ServerCommandSource> ctx, boolean clear) { | ||
Optional<Exception> opt = CommandHelper.getLastCommandException(); | ||
if(opt.isEmpty()) { | ||
ctx.getSource().sendError(new TranslatableText("command.mesh.get_exception.empty")); | ||
return 0; | ||
} | ||
else { | ||
Exception ex = opt.get(); | ||
MutableText text; | ||
if(ex instanceof CommandException cme) { | ||
text = cme.getTextMessage() instanceof MutableText mtxt ? mtxt : cme.getTextMessage().shallowCopy(); | ||
} | ||
else if(ex instanceof CommandSyntaxException cse) { | ||
Text exceptionMessage = Texts.toText(cse.getRawMessage()); | ||
text = exceptionMessage instanceof MutableText mtxt ? mtxt : exceptionMessage.shallowCopy(); | ||
} | ||
else { | ||
String exMessage = ex.getMessage(); | ||
if(exMessage.isBlank()) { | ||
exMessage = "Unknown Exception"; | ||
} | ||
text = new LiteralText(exMessage); | ||
} | ||
|
||
StringWriter sw = new StringWriter(); | ||
try (PrintWriter printer = new PrintWriter(sw)) { | ||
ex.printStackTrace(printer); | ||
} | ||
|
||
text.styled(it -> it.withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, sw.toString())).withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, CommandHelper.getClickToCopyToClipboardText()))); | ||
|
||
ctx.getSource().sendFeedback(text, false); | ||
|
||
if(clear) { | ||
CommandHelper.clearException(); | ||
} | ||
return 1; | ||
} | ||
} | ||
} |
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
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
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
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