add ability to input seeds as strings

This commit is contained in:
C10udburst
2021-08-26 10:58:37 +02:00
parent cc9847138d
commit 2b79b81c92
2 changed files with 21 additions and 14 deletions

View File

@@ -1,6 +1,7 @@
package cloudburst.rejects.commands; package cloudburst.rejects.commands;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import net.minecraft.command.CommandSource; import net.minecraft.command.CommandSource;
@@ -16,8 +17,6 @@ import meteordevelopment.meteorclient.utils.Utils;
import static com.mojang.brigadier.Command.SINGLE_SUCCESS; import static com.mojang.brigadier.Command.SINGLE_SUCCESS;
import com.mojang.brigadier.arguments.LongArgumentType;
public class SeedCommand extends Command { public class SeedCommand extends Command {
private final static SimpleCommandExceptionType NO_SEED = new SimpleCommandExceptionType(new LiteralText("No seed for current world saved.")); private final static SimpleCommandExceptionType NO_SEED = new SimpleCommandExceptionType(new LiteralText("No seed for current world saved."));
@@ -54,15 +53,15 @@ public class SeedCommand extends Command {
return SINGLE_SUCCESS; return SINGLE_SUCCESS;
})); }));
builder.then(argument("seed", LongArgumentType.longArg()).executes(ctx -> { builder.then(argument("seed", StringArgumentType.string()).executes(ctx -> {
Seeds.get().setSeed(LongArgumentType.getLong(ctx, "seed")); Seeds.get().setSeed(StringArgumentType.getString(ctx, "seed"));
return SINGLE_SUCCESS; return SINGLE_SUCCESS;
})); }));
builder.then(argument("seed", LongArgumentType.longArg()).then(argument("version", EnumArgumentType.enumArgument(MCVersion.latest())).executes(ctx -> { builder.then(argument("seed", StringArgumentType.string()).then(argument("version", EnumArgumentType.enumArgument(MCVersion.latest())).executes(ctx -> {
Seeds.get().setSeed(LongArgumentType.getLong(ctx, "seed"), EnumArgumentType.getEnum(ctx, "version", MCVersion.latest())); Seeds.get().setSeed(StringArgumentType.getString(ctx, "seed"), EnumArgumentType.getEnum(ctx, "version", MCVersion.latest()));
return SINGLE_SUCCESS; return SINGLE_SUCCESS;
}))); })));
} }
} }

View File

@@ -41,17 +41,17 @@ public class Seeds extends System<Seeds> {
version = MCVersion.latest(); version = MCVersion.latest();
return new Seed(mc.getServer().getOverworld().getSeed(), version); return new Seed(mc.getServer().getOverworld().getSeed(), version);
} }
return seeds.get(Utils.getWorldName()); return seeds.get(Utils.getWorldName());
} }
public void setSeed(long seed, MCVersion version) { public void setSeed(String seed, MCVersion version) {
if (mc.isIntegratedServerRunning()) return; if (mc.isIntegratedServerRunning()) return;
seeds.put(Utils.getWorldName(), new Seed(seed, version)); seeds.put(Utils.getWorldName(), new Seed(toSeed(seed), version));
} }
public void setSeed(long seed) { public void setSeed(String seed) {
ServerInfo server = mc.getCurrentServerEntry(); ServerInfo server = mc.getCurrentServerEntry();
MCVersion ver = null; MCVersion ver = null;
if (server != null) if (server != null)
@@ -62,7 +62,6 @@ public class Seeds extends System<Seeds> {
sendInvalidVersionWarning(seed, targetVer); sendInvalidVersionWarning(seed, targetVer);
ver = MCVersion.latest(); ver = MCVersion.latest();
} }
setSeed(seed, ver); setSeed(seed, ver);
} }
@@ -84,9 +83,18 @@ public class Seeds extends System<Seeds> {
return this; return this;
} }
private static void sendInvalidVersionWarning(long seed, String targetVer) { // https://minecraft.fandom.com/wiki/Seed_(level_generation)#Java_Edition
private static long toSeed(String inSeed) {
try {
return Long.parseLong(inSeed);
} catch (NumberFormatException e) {
return inSeed.hashCode();
}
}
private static void sendInvalidVersionWarning(String seed, String targetVer) {
BaseText msg = new LiteralText(String.format("Couldn't resolve minecraft version \"%s\". Using %s instead. If you wish to change the version run: ", targetVer, MCVersion.latest().name)); BaseText msg = new LiteralText(String.format("Couldn't resolve minecraft version \"%s\". Using %s instead. If you wish to change the version run: ", targetVer, MCVersion.latest().name));
String cmd = String.format("%sseed %d ", Config.get().prefix, seed); String cmd = String.format("%sseed %s ", Config.get().prefix, seed);
BaseText cmdText = new LiteralText(cmd+"<version>"); BaseText cmdText = new LiteralText(cmd+"<version>");
cmdText.setStyle(cmdText.getStyle() cmdText.setStyle(cmdText.getStyle()
.withUnderline(true) .withUnderline(true)