Skip to content

Commit

Permalink
Worked on switching some Serializer classes to ArgumentType classes a…
Browse files Browse the repository at this point in the history
…nd adding the serializers as subclasses.
  • Loading branch information
maghedo243 committed Jan 10, 2024
1 parent 64a7d47 commit 10d9b82
Show file tree
Hide file tree
Showing 18 changed files with 198 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
package tools.redstone.redstonetools;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.command.v2.ArgumentTypeRegistry;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.command.argument.serialize.ArgumentSerializer;
import net.minecraft.command.argument.serialize.ConstantArgumentSerializer;
import net.minecraft.util.Identifier;

import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mojang.brigadier.arguments.ArgumentType;

import rip.hippo.inject.Doctor;
import rip.hippo.inject.Injector;
import tools.redstone.redstonetools.features.arguments.serializers.IntegerSerializer;
import tools.redstone.redstonetools.features.arguments.serializers.TypeSerializer;
import tools.redstone.redstonetools.utils.DependencyLookup;
import tools.redstone.redstonetools.utils.ReflectionUtils;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package tools.redstone.redstonetools.features.arguments;

import tools.redstone.redstonetools.features.arguments.serializers.TypeSerializer;
import tools.redstone.redstonetools.features.arguments.serializers.GenericArgumentType;
import com.mojang.brigadier.context.CommandContext;

public class Argument<T> {
private String name;
private final TypeSerializer<T, ?> type;
private final GenericArgumentType<T, ?> type;
private boolean optional = false;
private volatile T value;
private T defaultValue;

private Argument(TypeSerializer<T, ?> type) {
private Argument(GenericArgumentType<T, ?> type) {
this.type = type;
}

public static <T> Argument<T> ofType(TypeSerializer<T, ?> type) {
public static <T> Argument<T> ofType(GenericArgumentType<T, ?> type) {
return new Argument<>(type);
}

Expand Down Expand Up @@ -48,7 +48,7 @@ public String getName() {
return name;
}

public TypeSerializer<T, ?> getType() {
public GenericArgumentType<T, ?> getType() {
return type;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.serialize.ArgumentSerializer;

import java.math.BigInteger;
import java.util.Optional;

public class BigIntegerArgumentType extends IntLikeArgumentType<BigInteger> {
private static final BigIntegerArgumentType INSTANCE = new BigIntegerArgumentType(null, null);

public static BigIntegerArgumentType bigInteger() {
return INSTANCE;
}

public static BigIntegerArgumentType bigInteger(BigInteger min) {
return new BigIntegerArgumentType(min, null);
}

public static BigIntegerArgumentType bigInteger(BigInteger min, BigInteger max) {
return new BigIntegerArgumentType(min, max);
}

private BigIntegerArgumentType(BigInteger min, BigInteger max) {
super(BigInteger.class, min, max);

}

@Override
protected Optional<BigInteger> tryParseOptional(String string, int radix) {
try {
return Optional.of(new BigInteger(string, radix));
} catch (NumberFormatException ignored) {
return Optional.empty();
}
}

public static class BigIntegerSerializer extends Serializer<BigIntegerArgumentType, ArgumentSerializer.ArgumentTypeProperties<BigIntegerArgumentType>>{

@Override
public ArgumentTypeProperties<BigIntegerArgumentType> getArgumentTypeProperties(BigIntegerArgumentType argumentType) {
return new Properties(argumentType.max,argumentType.min);
}

public final class Properties
implements ArgumentSerializer.ArgumentTypeProperties<BigIntegerArgumentType>{
final BigInteger max, min;

public Properties(BigInteger max, BigInteger min) {
this.max = max;
this.min = min;
}

@Override
public BigIntegerArgumentType createType(CommandRegistryAccess commandRegistryAccess) {
return new BigIntegerArgumentType(this.max,this.min);
}

@Override
public ArgumentSerializer<BigIntegerArgumentType, ?> getSerializer() {
return BigIntegerSerializer.this;
}
}
}




}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.BlockStateArgument;
import net.minecraft.command.argument.BlockStateArgumentType;
import net.minecraft.command.argument.serialize.ArgumentSerializer;
import net.minecraft.registry.Registries;

public class BlockStateArgumentSerializer extends BrigadierSerializer<BlockStateArgument, String> {
public class BlockStateArgumentType extends BrigadierArgumentType<BlockStateArgument, String> {

private static BlockStateArgumentSerializer INSTANCE;
private static BlockStateArgumentType INSTANCE;

private BlockStateArgumentSerializer(CommandRegistryAccess registryAccess) {
super(BlockStateArgument.class, BlockStateArgumentType.blockState(registryAccess));
private BlockStateArgumentType(CommandRegistryAccess registryAccess) {
super(BlockStateArgument.class, net.minecraft.command.argument.BlockStateArgumentType.blockState(registryAccess));
}

public static BlockStateArgumentSerializer blockState(CommandRegistryAccess registryAccess) {
public static BlockStateArgumentType blockState(CommandRegistryAccess registryAccess) {
if (INSTANCE == null) {
INSTANCE = new BlockStateArgumentSerializer(registryAccess);
INSTANCE = new BlockStateArgumentType(registryAccess);
}

return INSTANCE;
Expand Down Expand Up @@ -62,4 +62,26 @@ public String serialize(BlockStateArgument value) {
return builder.toString();
}

public static class BlockStateArgumentSerializer extends Serializer<BlockStateArgumentType, ArgumentSerializer.ArgumentTypeProperties<BlockStateArgumentType>>{

@Override
public ArgumentTypeProperties<BlockStateArgumentType> getArgumentTypeProperties(BlockStateArgumentType argumentType) {
return new Properties();
}

public final class Properties
implements ArgumentSerializer.ArgumentTypeProperties<BlockStateArgumentType>{

@Override
public BlockStateArgumentType createType(CommandRegistryAccess commandRegistryAccess) {
return new BlockStateArgumentType(commandRegistryAccess);
}

@Override
public ArgumentSerializer<BlockStateArgumentType, ?> getSerializer() {
return BlockStateArgumentSerializer.this;
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.minecraft.command.argument.serialize.ArgumentSerializer;

import java.util.Collection;
import java.util.concurrent.CompletableFuture;

public abstract class BrigadierSerializer<T, S> extends TypeSerializer<T, S> {
public abstract class BrigadierArgumentType<T, S> extends GenericArgumentType<T, S> {

// the wrapped brigadier argument type
private final ArgumentType<T> argumentType;

public BrigadierSerializer(Class<T> clazz, ArgumentType<T> argumentType) {
public BrigadierArgumentType(Class<T> clazz, ArgumentType<T> argumentType) {
super(clazz);
this.argumentType = argumentType;
}
Expand All @@ -35,4 +36,9 @@ public Collection<String> getExamples() {
return argumentType.getExamples();
}

public abstract class BrigadierSerializer extends Serializer<BrigadierArgumentType<T,S>,ArgumentSerializer.ArgumentTypeProperties<BrigadierArgumentType<T,S>>>{

public abstract ArgumentTypeProperties<BrigadierArgumentType<T,S>> getArgumentTypeProperties(BrigadierArgumentType<T,S> argumentType);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
* @param <C> The collection type.
*/
public class CollectionSerializer<E, C extends Collection<E>>
extends TypeSerializer<C, List<Object>>
extends GenericArgumentType<C, List<Object>>
{

public static <E> CollectionSerializer<E, List<E>> listOf(TypeSerializer<E, ?> element) {
public static <E> CollectionSerializer<E, List<E>> listOf(GenericArgumentType<E, ?> element) {
return new CollectionSerializer<>(List.class, element, ArrayList::new);
}

public static <E> CollectionSerializer<E, Set<E>> setOf(TypeSerializer<E, ?> element) {
public static <E> CollectionSerializer<E, Set<E>> setOf(GenericArgumentType<E, ?> element) {
return new CollectionSerializer<>(Set.class, element, HashSet::new);
}

final TypeSerializer<E, Object> elementType;
final GenericArgumentType<E, Object> elementType;
final Function<Collection<E>, C> collectionFactory;

// cache example because
Expand All @@ -39,10 +39,10 @@ public static <E> CollectionSerializer<E, Set<E>> setOf(TypeSerializer<E, ?> ele

@SuppressWarnings("unchecked")
protected CollectionSerializer(Class<?> clazz,
TypeSerializer<E, ?> elementType,
GenericArgumentType<E, ?> elementType,
Function<Collection<E>, C> collectionFactory) {
super((Class<C>) clazz);
this.elementType = (TypeSerializer<E, Object>) elementType;
this.elementType = (GenericArgumentType<E, Object>) elementType;
this.collectionFactory = collectionFactory;

// build example
Expand All @@ -55,7 +55,7 @@ protected CollectionSerializer(Class<?> clazz,
this.example = b.delete(b.length() - 3, b.length()).append("]").toString();
}

public TypeSerializer<E, ?> getElementType() {
public GenericArgumentType<E, ?> getElementType() {
return elementType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@

import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

public abstract class EnumSerializer<T extends Enum<T>>
extends TypeSerializer<T, String> {
extends GenericArgumentType<T, String> {

protected EnumSerializer(Class<T> clazz) {
super(clazz);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.stream.JsonReader;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;

import net.minecraft.command.argument.serialize.ArgumentSerializer;
import net.minecraft.command.argument.serialize.ArgumentSerializer.ArgumentTypeProperties;
import net.minecraft.network.PacketByteBuf;

import java.util.Collection;
import java.util.concurrent.CompletableFuture;

import com.google.common.reflect.TypeToken;
import com.google.gson.JsonObject;
import com.google.gson.stream.JsonReader;
import com.google.gson.Gson;

/**
* Base class for the 'wrapped' argument type.
*
* @param <T> The value type.
* @param <S> The serialized type.
*/
public abstract class TypeSerializer<T, S> implements ArgumentType<T> {
public abstract class GenericArgumentType<T, S> implements ArgumentType<T> {

protected final Class<T> clazz;

// TODO: Consider moving this constructor to enum serializer as it's the only
// class that uses the clazz field
protected TypeSerializer(Class<T> clazz) {
protected GenericArgumentType(Class<T> clazz) {
this.clazz = clazz;
}

Expand All @@ -58,7 +56,6 @@ public final T parse(StringReader reader) throws CommandSyntaxException {
public abstract <R> CompletableFuture<Suggestions> listSuggestions(CommandContext<R> context,
SuggestionsBuilder builder);

public abstract Serializer<?, ?> getSerializer();

public static abstract class Serializer<A extends ArgumentType<?>, T extends ArgumentTypeProperties<A>>
implements ArgumentSerializer<A, T> {
Expand Down
Loading

0 comments on commit 10d9b82

Please sign in to comment.