Skip to content

Commit c7898c7

Browse files
committed
second commit because i forgor to select all of the changed files bruh
1 parent 3f9c5c2 commit c7898c7

5 files changed

Lines changed: 115 additions & 10 deletions

File tree

src/main/java/io/github/techstreet/dfscript/DFScript.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.google.gson.Gson;
44
import com.google.gson.GsonBuilder;
55
import io.github.techstreet.dfscript.commands.CommandManager;
6+
import io.github.techstreet.dfscript.commands.arguments.StringFuncArgumentType;
7+
import io.github.techstreet.dfscript.commands.arguments.serializers.StringFuncArgumentSerializer;
68
import io.github.techstreet.dfscript.features.AuthHandler;
79
import io.github.techstreet.dfscript.features.UpdateAlerts;
810
import io.github.techstreet.dfscript.loader.Loader;
@@ -27,9 +29,13 @@
2729
import io.github.techstreet.dfscript.util.chat.ChatType;
2830
import io.github.techstreet.dfscript.util.chat.ChatUtil;
2931
import net.fabricmc.api.ModInitializer;
32+
import net.fabricmc.fabric.api.command.v2.ArgumentTypeRegistry;
3033
import net.fabricmc.loader.api.FabricLoader;
3134
import net.minecraft.client.MinecraftClient;
3235
import net.minecraft.client.util.Session;
36+
import net.minecraft.command.argument.serialize.ConstantArgumentSerializer;
37+
import net.minecraft.command.argument.serialize.StringArgumentSerializer;
38+
import net.minecraft.util.Identifier;
3339
import org.apache.commons.codec.binary.Base64;
3440
import org.apache.commons.lang3.RandomStringUtils;
3541
import org.apache.logging.log4j.LogManager;
@@ -66,6 +72,8 @@ public void onInitialize() {
6672

6773
MOD_VERSION = FabricLoader.getInstance().getModContainer(MOD_ID).get().getMetadata().getVersion().getFriendlyString();
6874

75+
ArgumentTypeRegistry.registerArgumentType(new Identifier("tutorial", "uuid"), StringFuncArgumentType.class, new StringFuncArgumentSerializer());
76+
6977
Loader loader = Loader.getInstance();
7078
loader.load(new CommandManager());
7179
loader.load(new AuthHandler());
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.github.techstreet.dfscript.commands.arguments;
2+
3+
import io.github.techstreet.dfscript.script.Script;
4+
import io.github.techstreet.dfscript.script.ScriptManager;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.function.Function;
9+
10+
public enum StringFuncArgumentFunctions {
11+
SCRIPTS((v) -> {
12+
List<String> possible = new ArrayList<>();
13+
for (Script s : ScriptManager.getInstance().getScripts()) {
14+
possible.add(s.getName().replaceAll(" ", "_"));
15+
}
16+
return possible;
17+
});
18+
19+
StringFuncArgumentFunctions(Function<Void, List<String>> func) {
20+
this.func = func;
21+
}
22+
23+
public Function<Void, List<String>> getFunction() {
24+
return func;
25+
}
26+
27+
Function<Void, List<String>> func;
28+
}

src/main/java/io/github/techstreet/dfscript/commands/arguments/StringFuncArgumentType.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
import java.util.List;
1010
import java.util.concurrent.CompletableFuture;
1111
import java.util.function.Function;
12+
1213
import net.minecraft.command.CommandSource;
1314

1415
public class StringFuncArgumentType implements ArgumentType<String> {
1516

16-
Function<Void,List<String>> func;
17+
StringFuncArgumentFunctions func;
1718
boolean greedy;
1819

19-
public StringFuncArgumentType(Function<Void, List<String>> func, boolean greedy) {
20+
public StringFuncArgumentType(StringFuncArgumentFunctions func, boolean greedy) {
2021
this.func = func;
2122
this.greedy = greedy;
2223
}
@@ -25,7 +26,7 @@ public StringFuncArgumentType(Function<Void, List<String>> func, boolean greedy)
2526
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context,
2627
SuggestionsBuilder builder) {
2728

28-
List<String> suggestions = func.apply(null);
29+
List<String> suggestions = func.getFunction().apply(null);
2930

3031
if (context.getSource() instanceof CommandSource) {
3132
return CommandSource.suggestMatching(suggestions, builder);
@@ -46,4 +47,13 @@ public String parse(StringReader reader) throws CommandSyntaxException {
4647

4748
return reader.getString().substring(i, reader.getCursor());
4849
}
50+
51+
public StringFuncArgumentFunctions getFunction()
52+
{
53+
return func;
54+
}
55+
56+
public boolean isGreedy() {
57+
return greedy;
58+
}
4959
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package io.github.techstreet.dfscript.commands.arguments.serializers;
2+
3+
import com.google.gson.JsonObject;
4+
import com.mojang.brigadier.arguments.StringArgumentType;
5+
import io.github.techstreet.dfscript.commands.arguments.StringFuncArgumentFunctions;
6+
import io.github.techstreet.dfscript.commands.arguments.StringFuncArgumentType;
7+
import net.minecraft.command.CommandRegistryAccess;
8+
import net.minecraft.command.argument.serialize.ArgumentSerializer;
9+
import net.minecraft.network.PacketByteBuf;
10+
11+
import java.util.List;
12+
import java.util.function.Function;
13+
14+
public class StringFuncArgumentSerializer implements ArgumentSerializer<StringFuncArgumentType, StringFuncArgumentSerializer.StringFuncArgumentProperties> {
15+
16+
@Override
17+
public void writePacket(StringFuncArgumentProperties properties, PacketByteBuf buf) {
18+
buf.writeBoolean(properties.greedy);
19+
buf.writeEnumConstant(properties.func);
20+
}
21+
22+
@Override
23+
public StringFuncArgumentProperties fromPacket(PacketByteBuf buf) {
24+
boolean greedy = buf.readBoolean();
25+
StringFuncArgumentFunctions func = buf.readEnumConstant(StringFuncArgumentFunctions.class);
26+
27+
return new StringFuncArgumentProperties(greedy, func);
28+
}
29+
30+
@Override
31+
public void writeJson(StringFuncArgumentProperties properties, JsonObject json) {
32+
json.addProperty("greedy", properties.greedy);
33+
34+
json.addProperty("function", properties.func.name());
35+
}
36+
37+
@Override
38+
public StringFuncArgumentProperties getArgumentTypeProperties(StringFuncArgumentType argumentType) {
39+
return new StringFuncArgumentProperties(argumentType.isGreedy(), argumentType.getFunction());
40+
}
41+
42+
public final class StringFuncArgumentProperties implements ArgumentTypeProperties<StringFuncArgumentType> {
43+
44+
boolean greedy;
45+
StringFuncArgumentFunctions func;
46+
47+
public StringFuncArgumentProperties(boolean greedy, StringFuncArgumentFunctions func)
48+
{
49+
this.greedy = greedy;
50+
this.func = func;
51+
}
52+
53+
@Override
54+
public StringFuncArgumentType createType(CommandRegistryAccess commandRegistryAccess) {
55+
return new StringFuncArgumentType(func, greedy);
56+
}
57+
58+
@Override
59+
public ArgumentSerializer<StringFuncArgumentType, ?> getSerializer() {
60+
return StringFuncArgumentSerializer.this;
61+
}
62+
}
63+
}

src/main/java/io/github/techstreet/dfscript/commands/misc/ScriptsCommand.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.util.Map.Entry;
1616
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
1717

18+
import static io.github.techstreet.dfscript.commands.arguments.StringFuncArgumentFunctions.SCRIPTS;
19+
1820
public class ScriptsCommand implements Command {
1921

2022
@Override
@@ -33,13 +35,7 @@ public void register(CommandDispatcher<FabricClientCommandSource> cd) {
3335
})
3436
)
3537
.then(literal("vars")
36-
.then(argument("script", new StringFuncArgumentType((v) -> {
37-
List<String> possible = new ArrayList<>();
38-
for (Script s : ScriptManager.getInstance().getScripts()) {
39-
possible.add(s.getName().replaceAll(" ", "_"));
40-
}
41-
return possible;
42-
}, false)
38+
.then(argument("script", new StringFuncArgumentType(SCRIPTS, false)
4339
)
4440
.executes(ctx -> {
4541
listVars(ctx.getArgument("script", String.class), "");

0 commit comments

Comments
 (0)