|
| 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 | +} |
0 commit comments