Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite #358

Merged
merged 1 commit into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .factorypath
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<factorypath>
<factorypathentry kind="EXTJAR" id="/home/rugmj/.gradle/caches/modules-2/files-2.1/org.checkerframework/checker-qual/3.12.0/d5692f0526415fcc6de94bb5bfbd3afd9dd3b3e5/checker-qual-3.12.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/rugmj/.gradle/caches/modules-2/files-2.1/io.github.llamalad7/mixinextras-fabric/0.3.2/33c53c7014a170a9cdfbc801f7a77a0eefd3bd00/mixinextras-fabric-0.3.2.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/rugmj/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/31.1-jre/60458f877d055d0c9114d9e1a2efb737b4bc282c/guava-31.1-jre.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/rugmj/.gradle/caches/modules-2/files-2.1/com.google.guava/failureaccess/1.0.1/1dcf1de382a0bf95a3d8b0849546c88bac1292c9/failureaccess-1.0.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/rugmj/.gradle/caches/modules-2/files-2.1/net.fabricmc/fabric-mixin-compile-extensions/0.6.0/6fda0c85c419f0a259da017a1424f27507eb30f0/fabric-mixin-compile-extensions-0.6.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/rugmj/.gradle/caches/modules-2/files-2.1/com.google.auto.service/auto-service/1.1.0/f2d5d13c2f812fafef11152949b0b117624ce553/auto-service-1.1.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/rugmj/.gradle/caches/modules-2/files-2.1/com.google.guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/b421526c5f297295adef1c886e5246c39d4ac629/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/rugmj/.gradle/caches/modules-2/files-2.1/org.ow2.asm/asm-commons/9.6/f1a9e5508eff490744144565c47326c8648be309/asm-commons-9.6.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/rugmj/.gradle/caches/modules-2/files-2.1/org.ow2.asm/asm-analysis/9.6/9ce6c7b174bd997fc2552dff47964546bd7a5ec3/asm-analysis-9.6.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/rugmj/.gradle/caches/modules-2/files-2.1/com.google.auto/auto-common/1.2.1/f6da26895f759010f5f170c8044e84c1b17ef83e/auto-common-1.2.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/rugmj/.gradle/caches/modules-2/files-2.1/org.ow2.asm/asm/9.6/aa205cf0a06dbd8e04ece91c0b37c3f5d567546a/asm-9.6.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/rugmj/.gradle/caches/modules-2/files-2.1/org.ow2.asm/asm-tree/9.6/c0cdda9d211e965d2a4448aa3fd86110f2f8c2de/asm-tree-9.6.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/rugmj/.gradle/caches/modules-2/files-2.1/com.google.auto.service/auto-service-annotations/1.1.0/d22db50d06b26c9882a046ea597f43b174dd3374/auto-service-annotations-1.1.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/rugmj/.gradle/caches/modules-2/files-2.1/com.google.j2objc/j2objc-annotations/1.3/ba035118bc8bac37d7eff77700720999acd9986d/j2objc-annotations-1.3.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/rugmj/.gradle/caches/modules-2/files-2.1/com.google.code.findbugs/jsr305/3.0.2/25ea2e8b0c338a877313bd4672d3fe056ea78f0d/jsr305-3.0.2.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/rugmj/.gradle/caches/modules-2/files-2.1/com.google.errorprone/error_prone_annotations/2.11.0/c5a0ace696d3f8b1c1d8cc036d8c03cc0cbe6b69/error_prone_annotations-2.11.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/rugmj/.gradle/caches/modules-2/files-2.1/net.fabricmc/sponge-mixin/0.12.5+mixin.0.8.5/8d31fb97c3e0cd7c8dad3441851c523bcfae6d8e/sponge-mixin-0.12.5+mixin.0.8.5.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/rugmj/.gradle/caches/modules-2/files-2.1/net.fabricmc/mapping-io/0.2.1/2a1aeedce104191bbb9e39e26abfbfeaf9b0590b/mapping-io-0.2.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/rugmj/.gradle/caches/modules-2/files-2.1/org.ow2.asm/asm-util/9.6/f77caf84eb93786a749b2baa40865b9613e3eaee/asm-util-9.6.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/rugmj/.gradle/caches/fabric-loom/1.20.4/net.fabricmc.yarn.1_20_4.1.20.4+build.3-v2/mappings.jar" enabled="true" runInBatchMode="false"/>
</factorypath>
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
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.command.argument.serialize.ArgumentSerializer;
import net.minecraft.util.Identifier;

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

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

import java.lang.reflect.InvocationTargetException;
import java.nio.file.Path;

public class RedstoneToolsClient implements ClientModInitializer {
Expand All @@ -31,6 +36,32 @@ public void onInitializeClient() {
// Register game rules
RedstoneToolsGameRules.register();

// Register arguments
ReflectionUtils.getAllArguments().forEach(argument -> {
var nestedClasses = (Class<ArgumentSerializer>[]) argument
.getDeclaredClasses();

if (nestedClasses.length == 0) {
LOGGER.error("Failed to register {} because no serializer nested class was found",
argument.getSimpleName());
return;
}

Identifier id = new Identifier(MOD_ID, argument.getSimpleName().toLowerCase());

try {
var serializer = nestedClasses[0].getDeclaredConstructor().newInstance();

ArgumentTypeRegistry.registerArgumentType(
id,
argument, serializer);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
LOGGER.error("Failed to register argument type {}. Skipping registration.",
argument.getName());
}
});

// Register features
ReflectionUtils.getFeatures().forEach(feature ->

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

import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.serialize.ArgumentSerializer;
import tools.redstone.redstonetools.utils.BlockColor;

public class BlockColorSerializer extends EnumSerializer<BlockColor> {
Expand All @@ -12,4 +14,27 @@ private BlockColorSerializer() {
public static BlockColorSerializer blockColor() {
return INSTANCE;
}

public static class Serializer
extends GenericArgumentType.Serializer<BlockColorSerializer, Serializer.Properties> {

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

@Override
public BlockColorSerializer createType(CommandRegistryAccess var1) {
return blockColor();
}

@Override
public ArgumentSerializer<BlockColorSerializer, ?> getSerializer() {
return new Serializer();
}
}

@Override
public Properties getArgumentTypeProperties(BlockColorSerializer var1) {
return new Properties();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.BoolArgumentType;

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

public class BoolSerializer extends StringBrigadierSerializer<Boolean> {

private static final BoolSerializer INSTANCE = new BoolSerializer(BoolArgumentType.bool());
Expand All @@ -20,4 +23,27 @@ public String serialize(Boolean value) {
return String.valueOf(value);
}

public static class Serializer
extends GenericArgumentType.Serializer<BoolSerializer, Serializer.Properties> {

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

@Override
public BoolSerializer createType(CommandRegistryAccess var1) {
return bool();
}

@Override
public ArgumentSerializer<BoolSerializer, ?> getSerializer() {
return new Serializer();
}
}

@Override
public Properties getArgumentTypeProperties(BoolSerializer serializer) {
return new Properties();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;

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

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
Expand All @@ -18,8 +21,7 @@
* @param <C> The collection type.
*/
public class CollectionSerializer<E, C extends Collection<E>>
extends GenericArgumentType<C, List<Object>>
{
extends GenericArgumentType<C, List<Object>> {

public static <E> CollectionSerializer<E, List<E>> listOf(GenericArgumentType<E, ?> element) {
return new CollectionSerializer<>(List.class, element, ArrayList::new);
Expand All @@ -39,8 +41,8 @@ public static <E> CollectionSerializer<E, Set<E>> setOf(GenericArgumentType<E, ?

@SuppressWarnings("unchecked")
protected CollectionSerializer(Class<?> clazz,
GenericArgumentType<E, ?> elementType,
Function<Collection<E>, C> collectionFactory) {
GenericArgumentType<E, ?> elementType,
Function<Collection<E>, C> collectionFactory) {
super((Class<C>) clazz);
this.elementType = (GenericArgumentType<E, Object>) elementType;
this.collectionFactory = collectionFactory;
Expand Down Expand Up @@ -116,7 +118,8 @@ public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> cont
int oldCursor = inputParser.getCursor();
try {
elementType.deserialize(inputParser);
} catch (CommandSyntaxException ignored) { }
} catch (CommandSyntaxException ignored) {
}
if (oldCursor == inputParser.getCursor())
break;
inputParser.skipWhitespace();
Expand Down Expand Up @@ -173,4 +176,28 @@ public List<Object> serialize(C value) {
.toList();
}

public static class Serializer
extends GenericArgumentType.Serializer<CollectionSerializer<?, ?>, Serializer.Properties> {

public final class Properties
implements ArgumentSerializer.ArgumentTypeProperties<CollectionSerializer<?, ?>> {

@Override
public CollectionSerializer<?, ?> createType(CommandRegistryAccess var1) {
// TODO: Actually make this work, this is currently a work around to get it to
// compile
return listOf(IntegerSerializer.integer());
}

@Override
public ArgumentSerializer<CollectionSerializer<?, ?>, ?> getSerializer() {
return new Serializer();
}
}

@Override
public Properties getArgumentTypeProperties(CollectionSerializer<?, ?> serializer) {
return new Properties();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.serialize.ArgumentSerializer;
import tools.redstone.redstonetools.utils.ColoredBlockType;

public class ColoredBlockTypeSerializer extends EnumSerializer<ColoredBlockType> {
Expand All @@ -12,4 +14,27 @@ private ColoredBlockTypeSerializer() {
public static ColoredBlockTypeSerializer coloredBlockType() {
return INSTANCE;
}

public static class Serializer
extends GenericArgumentType.Serializer<ColoredBlockTypeSerializer, Serializer.Properties> {

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

@Override
public ColoredBlockTypeSerializer createType(CommandRegistryAccess var1) {
return coloredBlockType();
}

@Override
public ArgumentSerializer<ColoredBlockTypeSerializer, ?> getSerializer() {
return new Serializer();
}
}

@Override
public Properties getArgumentTypeProperties(ColoredBlockTypeSerializer serializer) {
return new Properties();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.serialize.ArgumentSerializer;
import tools.redstone.redstonetools.utils.DirectionArgument;

public class DirectionSerializer extends EnumSerializer<DirectionArgument> {
Expand All @@ -12,4 +14,27 @@ private DirectionSerializer() {
public static DirectionSerializer direction() {
return INSTANCE;
}

public static class Serializer
extends GenericArgumentType.Serializer<DirectionSerializer, Serializer.Properties> {

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

@Override
public DirectionSerializer createType(CommandRegistryAccess var1) {
return direction();
}

@Override
public ArgumentSerializer<DirectionSerializer, ?> getSerializer() {
return new Serializer();
}
}

@Override
public Properties getArgumentTypeProperties(DirectionSerializer serializer) {
return new Properties();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.DoubleArgumentType;

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

public class DoubleSerializer extends StringBrigadierSerializer<Double> {

private static final DoubleSerializer INSTANCE = new DoubleSerializer(DoubleArgumentType.doubleArg());
Expand All @@ -28,4 +31,26 @@ public String serialize(Double value) {
return String.valueOf(value);
}

public static class Serializer
extends GenericArgumentType.Serializer<DoubleSerializer, Serializer.Properties> {

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

@Override
public DoubleSerializer createType(CommandRegistryAccess var1) {
return doubleArg();
}

@Override
public ArgumentSerializer<DoubleSerializer, ?> getSerializer() {
return new Serializer();
}
}

@Override
public Properties getArgumentTypeProperties(DoubleSerializer serializer) {
return new Properties();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.FloatArgumentType;

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

public class FloatSerializer extends StringBrigadierSerializer<Float> {

private static final FloatSerializer INSTANCE = new FloatSerializer(FloatArgumentType.floatArg());
Expand All @@ -28,4 +31,26 @@ public String serialize(Float value) {
return String.valueOf(value);
}

public static class Serializer
extends GenericArgumentType.Serializer<FloatSerializer, Serializer.Properties> {

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

@Override
public FloatSerializer createType(CommandRegistryAccess var1) {
return floatArg();
}

@Override
public ArgumentSerializer<FloatSerializer, ?> getSerializer() {
return new Serializer();
}
}

@Override
public Properties getArgumentTypeProperties(FloatSerializer serializer) {
return new Properties();
}
}
}
Loading