Inspired by 2020's April Fools' 20w14infinite Snapshot, this mod brings endless randomly generated dimensions into Minecraft.
at master 131 lines 5.8 kB view raw
1package net.lerariemann.infinity.registry.var; 2 3import com.mojang.brigadier.arguments.IntegerArgumentType; 4import com.mojang.brigadier.arguments.StringArgumentType; 5import com.mojang.brigadier.builder.LiteralArgumentBuilder; 6import com.mojang.brigadier.builder.RequiredArgumentBuilder; 7import com.mojang.brigadier.context.CommandContext; 8import com.mojang.brigadier.exceptions.CommandSyntaxException; 9import com.mojang.brigadier.exceptions.DynamicCommandExceptionType; 10import dev.architectury.event.events.common.CommandRegistrationEvent; 11import net.lerariemann.infinity.compat.ModCompat; 12import net.lerariemann.infinity.util.InfinityMethods; 13import net.lerariemann.infinity.util.teleport.WarpLogic; 14import net.minecraft.commands.CommandSourceStack; 15import net.minecraft.commands.Commands; 16import net.minecraft.commands.arguments.DimensionArgument; 17import net.minecraft.commands.arguments.EntityArgument; 18import net.minecraft.network.chat.Component; 19import net.minecraft.resources.ResourceLocation; 20import net.minecraft.server.level.ServerPlayer; 21 22import java.util.Collection; 23import java.util.Collections; 24import java.util.Random; 25 26import static net.minecraft.commands.Commands.argument; 27import static net.minecraft.commands.Commands.literal; 28 29public class ModCommands { 30 public static final DynamicCommandExceptionType MALFORM_IDENTIFIER_EXCEPTION = makeException("error.infinity.warp.malformed_identifier"); 31 public static final DynamicCommandExceptionType TIMEBOMBED_EXCEPTION = makeException("error.infinity.warp.timebombed"); 32 33 public static DynamicCommandExceptionType makeException(String key) { 34 return new DynamicCommandExceptionType( 35 id -> Component. 36 //? if >1.21 { 37 translatableEscape 38 //?} else { 39 /*translatable 40 *///?} 41 ("error.infinity.warp.malformed_identifier", id) 42 ); 43 } 44 45 public static void registerCommands() { 46 String warp; 47 if (ModCompat.ESSENTIALS) // FTB/Fabric Essentials add their own warp command that plays havoc with ours. 48 warp = "dimwarp"; 49 else { 50 warp = "warp"; 51 } 52 CommandRegistrationEvent.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(literal(warp) 53 .requires(source -> source.hasPermission(2)) 54 .then( 55 Commands.argument("targets", EntityArgument.players()) 56 .then(randomArgument(true)) 57 .then(existingArgument(true)) 58 .then(idArgument(true)) 59 .then(textArgument(true)) 60 ) 61 .then(randomArgument(false)) 62 .then(existingArgument(false)) 63 .then(idArgument(false)) 64 .then(textArgument(false)) 65 )); 66 CommandRegistrationEvent.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(literal("respawn") 67 .requires(source -> source.hasPermission(2)).executes(context -> { 68 WarpLogic.respawnAlive(context.getSource().getPlayer()); 69 return 1; 70 }))); 71 } 72 73 private static RequiredArgumentBuilder<CommandSourceStack, String> textArgument(boolean b) { 74 return argument("text", StringArgumentType.string()) 75 .executes(context -> { 76 final String text = StringArgumentType.getString(context, "text"); 77 WarpLogic.requestWarpByText(context, players(b, context), text); 78 return 1; 79 }); 80 } 81 82 private static LiteralArgumentBuilder<CommandSourceStack> idArgument(boolean b) { 83 return Commands.literal("id").then( 84 argument("id", IntegerArgumentType.integer()) 85 .executes(context -> { 86 final int value = IntegerArgumentType.getInteger(context, "id"); 87 WarpLogic.requestWarpById(players(b, context), value); 88 return 1; 89 }) 90 ); 91 } 92 93 private static LiteralArgumentBuilder<CommandSourceStack> existingArgument(boolean b) { 94 return Commands.literal("existing").then( 95 argument("existing", DimensionArgument.dimension()).executes(context -> { 96 final ResourceLocation identifier = context.getArgument("existing", ResourceLocation.class); 97 WarpLogic.requestWarpToExisting(context, players(b, context), identifier); 98 return 1; 99 }) 100 ); 101 } 102 103 private static LiteralArgumentBuilder<CommandSourceStack> randomArgument(boolean b) { 104 return Commands.literal("random").executes((context -> { 105 final long text = InfinityMethods.getRandomSeed(new Random()); 106 Collection<ServerPlayer> players = players(b, context); 107 WarpLogic.requestWarpById(players, text); 108 return 1; 109 })); 110 } 111 112 private static Collection<ServerPlayer> players(boolean hasPlayerArgument, CommandContext<CommandSourceStack> context) { 113 if (hasPlayerArgument) { 114 return players(context); 115 } else { 116 return player(context); 117 } 118 } 119 120 private static Collection<ServerPlayer> player(CommandContext<CommandSourceStack> context) { 121 return Collections.singleton(context.getSource().getPlayer()); 122 } 123 124 private static Collection<ServerPlayer> players(CommandContext<CommandSourceStack> context) { 125 try { 126 return EntityArgument.getPlayers(context, "targets"); 127 } catch (CommandSyntaxException e) { 128 return Collections.singleton(context.getSource().getPlayer()); 129 } 130 } 131}