Created
March 11, 2024 13:06
-
-
Save Densamisten/bfc0c2f7ce13bd156f46b7404149e4e6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package io.github.densamisten.command; | |
| import com.mojang.brigadier.CommandDispatcher; | |
| import com.mojang.brigadier.arguments.StringArgumentType; | |
| import net.minecraft.commands.CommandSourceStack; | |
| import net.minecraft.commands.Commands; | |
| import net.minecraft.network.chat.Component; | |
| public class AtbashCipherCommand { | |
| public AtbashCipherCommand(CommandDispatcher<CommandSourceStack> dispatcher) { | |
| dispatcher.register(Commands.literal("cipher") | |
| .then(Commands.literal("atbash") | |
| .then(Commands.literal("encrypt") | |
| .then(Commands.argument("text", StringArgumentType.string()) | |
| .executes(context -> { | |
| String inputText = context.getArgument("text", String.class); | |
| String encryptedText = encrypt(inputText); | |
| context.getSource().sendSuccess(() -> Component.literal("Encrypted text: " + encryptedText), false); | |
| return 1; | |
| }) | |
| ) | |
| ) | |
| .then(Commands.literal("decrypt") | |
| .then(Commands.argument("text", StringArgumentType.string()) | |
| .executes(context -> { | |
| String inputText = context.getArgument("text", String.class); | |
| String decryptedText = decrypt(inputText); | |
| context.getSource().sendSuccess(() -> Component.literal("Decrypted text: " + decryptedText), false); | |
| return 1; | |
| }) | |
| ) | |
| ) | |
| ) | |
| ); | |
| } | |
| private static String encrypt(String text) { | |
| StringBuilder encryptedText = new StringBuilder(); | |
| for (char ch : text.toCharArray()) { | |
| if (Character.isLetter(ch)) { | |
| char encryptedChar = (char) ('Z' - (Character.toUpperCase(ch) - 'A')); | |
| encryptedText.append(Character.isLowerCase(ch) ? Character.toLowerCase(encryptedChar) : encryptedChar); | |
| } else { | |
| encryptedText.append(ch); | |
| } | |
| } | |
| return encryptedText.toString(); | |
| } | |
| private static String decrypt(String text) { | |
| return encrypt(text); // In Atbash cipher, encryption and decryption are the same | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment