Skip to content

Commit

Permalink
Move remaining parsers from fabric
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Jan 15, 2024
1 parent 22eab0c commit 83138e1
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import cloud.commandframework.captions.CaptionProvider;
import cloud.commandframework.captions.StandardCaptionRegistry;
import cloud.commandframework.minecraft.modded.ModdedCaptionKeys;

/**
* Caption registry that uses bi-functions to produce messages.
Expand All @@ -35,15 +36,15 @@
public class FabricCaptionRegistry<C> extends StandardCaptionRegistry<C> {

/**
* Default caption for {@link FabricCaptionKeys#ARGUMENT_PARSE_FAILURE_REGISTRY_ENTRY_UNKNOWN_ENTRY}
* Default caption for {@link ModdedCaptionKeys#ARGUMENT_PARSE_FAILURE_REGISTRY_ENTRY_UNKNOWN_ENTRY}
*
* @since 1.5.0
*/
public static final String ARGUMENT_PARSE_FAILURE_REGISTRY_ENTRY_UNKNOWN_ENTRY =
"Could not find value with key '<id>' in registry '<registry>'.";

/**
* Default caption for {@link FabricCaptionKeys#ARGUMENT_PARSE_FAILURE_TEAM_UNKNOWN}
* Default caption for {@link ModdedCaptionKeys#ARGUMENT_PARSE_FAILURE_TEAM_UNKNOWN}
*
* @since 1.5.0
*/
Expand All @@ -55,10 +56,10 @@ protected FabricCaptionRegistry() {
this.registerProvider(
CaptionProvider.<C>constantProvider()
.putCaptions(
FabricCaptionKeys.ARGUMENT_PARSE_FAILURE_REGISTRY_ENTRY_UNKNOWN_ENTRY,
ModdedCaptionKeys.ARGUMENT_PARSE_FAILURE_REGISTRY_ENTRY_UNKNOWN_ENTRY,
ARGUMENT_PARSE_FAILURE_REGISTRY_ENTRY_UNKNOWN_ENTRY
).putCaptions(
FabricCaptionKeys.ARGUMENT_PARSE_FAILURE_TEAM_UNKNOWN,
ModdedCaptionKeys.ARGUMENT_PARSE_FAILURE_TEAM_UNKNOWN,
ARGUMENT_PARSE_FAILURE_TEAM_UNKNOWN
)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,21 @@
import cloud.commandframework.exceptions.NoPermissionException;
import cloud.commandframework.exceptions.NoSuchCommandException;
import cloud.commandframework.execution.ExecutionCoordinator;
import cloud.commandframework.fabric.argument.RegistryEntryParser;
import cloud.commandframework.fabric.argument.TeamParser;
import cloud.commandframework.minecraft.modded.internal.ModdedParserMappings;
import cloud.commandframework.minecraft.modded.internal.ModdedPreprocessor;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.serialization.Codec;
import io.leangen.geantyref.GenericTypeReflector;
import io.leangen.geantyref.TypeToken;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.util.HashSet;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.commands.arguments.ResourceKeyArgument;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentUtils;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.scores.PlayerTeam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apiguardian.api.API;
Expand All @@ -94,7 +77,6 @@ public abstract class FabricCommandManager<C, S extends SharedSuggestionProvider
BrigadierManagerHolder<C, S>, SenderMapperHolder<S, C> {

private static final Logger LOGGER = LogManager.getLogger();
private static final int MOD_PUBLIC_STATIC_FINAL = Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL;

private static final Component NEWLINE = Component.literal("\n");
private static final String MESSAGE_INTERNAL_ERROR = "An internal error occurred while attempting to perform this command.";
Expand Down Expand Up @@ -150,88 +132,13 @@ public abstract class FabricCommandManager<C, S extends SharedSuggestionProvider
this.senderMapper
);

this.registerNativeBrigadierMappings(this.brigadierManager);
ModdedParserMappings.register(this, this.brigadierManager);
this.captionRegistry(new FabricCaptionRegistry<>());
this.registerCommandPreProcessor(new ModdedPreprocessor<>(senderMapper));

((FabricCommandRegistrationHandler<C, S>) this.commandRegistrationHandler()).initialize(this);
}

private void registerNativeBrigadierMappings(final @NonNull CloudBrigadierManager<C, S> brigadier) {
this.registerRegistryEntryMappings();
brigadier.registerMapping(new TypeToken<TeamParser<C>>() {
}, builder -> builder.toConstant(net.minecraft.commands.arguments.TeamArgument.team()));
this.parserRegistry().registerParserSupplier(
TypeToken.get(PlayerTeam.class),
params -> new TeamParser<>()
);

ModdedParserMappings.register(this, brigadier);
}

@SuppressWarnings({"unchecked", "rawtypes"})
private void registerRegistryEntryMappings() {
this.brigadierManager.registerMapping(
new TypeToken<RegistryEntryParser<C, ?>>() {
},
builder -> {
builder.to(argument -> ResourceKeyArgument.key((ResourceKey) argument.registryKey()));
}
);

/* Find all fields of RegistryKey<? extends Registry<?>> and register those */
/* This only works for vanilla registries really, we'll have to do other things for non-vanilla ones */
final Set<Class<?>> seenClasses = new HashSet<>();
/* Some registries have types that are too generic... we'll skip those for now.
* Eventually, these could be resolved by using ParserParameters in some way? */
seenClasses.add(ResourceLocation.class);
seenClasses.add(Codec.class);
seenClasses.add(String.class); // avoid pottery pattern registry overriding default string parser
for (final Field field : Registries.class.getDeclaredFields()) {
if ((field.getModifiers() & MOD_PUBLIC_STATIC_FINAL) != MOD_PUBLIC_STATIC_FINAL) {
continue;
}
if (!field.getType().equals(ResourceKey.class)) {
continue;
}

final Type generic = field.getGenericType(); /* RegistryKey<? extends Registry<?>> */
if (!(generic instanceof ParameterizedType)) {
continue;
}

Type registryType = ((ParameterizedType) generic).getActualTypeArguments()[0];
while (registryType instanceof WildcardType) {
registryType = ((WildcardType) registryType).getUpperBounds()[0];
}

if (!(registryType instanceof ParameterizedType)) { /* expected: Registry<V> */
continue;
}

final ResourceKey<?> key;
try {
key = (ResourceKey<?>) field.get(null);
} catch (final IllegalAccessException ex) {
LOGGER.warn("Failed to access value of registry key in field {} of type {}", field.getName(), generic, ex);
continue;
}

final Type valueType = ((ParameterizedType) registryType).getActualTypeArguments()[0];
if (seenClasses.contains(GenericTypeReflector.erase(valueType))) {
LOGGER.debug("Encountered duplicate type in registry {}: type {}", key, valueType);
continue;
}
seenClasses.add(GenericTypeReflector.erase(valueType));

/* and now, finally, we can register */
this.parserRegistry().registerParserSupplier(
TypeToken.get(valueType),
params -> new RegistryEntryParser(key)
);
}
}

@Override
public final @NonNull SenderMapper<S, C> senderMapper() {
return this.senderMapper;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
import cloud.commandframework.arguments.suggestion.SuggestionProvider;
import cloud.commandframework.execution.ExecutionCoordinator;
import cloud.commandframework.fabric.FabricServerCommandManager;
import cloud.commandframework.fabric.argument.RegistryEntryParser;
import cloud.commandframework.fabric.testmod.mixin.GiveCommandAccess;
import cloud.commandframework.keys.CloudKey;
import cloud.commandframework.minecraft.modded.data.Coordinates;
import cloud.commandframework.minecraft.modded.data.Coordinates.ColumnCoordinates;
import cloud.commandframework.minecraft.modded.data.MultipleEntitySelector;
import cloud.commandframework.minecraft.modded.data.MultiplePlayerSelector;
import cloud.commandframework.minecraft.modded.parser.NamedColorParser;
import cloud.commandframework.minecraft.modded.parser.RegistryEntryParser;
import cloud.commandframework.minecraft.modded.parser.VanillaArgumentParsers;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import java.util.Collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.fabric;
package cloud.commandframework.minecraft.modded;

import cloud.commandframework.captions.Caption;
import java.util.Collection;
Expand All @@ -31,31 +31,25 @@

/**
* {@link Caption} instances for messages in cloud-fabric
*
* @since 1.5.0
*/
public final class FabricCaptionKeys {
public final class ModdedCaptionKeys {

private static final Collection<Caption> RECOGNIZED_CAPTIONS = new HashSet<>();

/**
* Variables: {@code <id>}, {@code <registry>}
*
* @since 1.5.0
*/
public static final Caption ARGUMENT_PARSE_FAILURE_REGISTRY_ENTRY_UNKNOWN_ENTRY = of(
"argument.parse.failure.registry_entry.unknown_entry"
"argument.parse.failure.registry_entry.unknown_entry"
);
/**
* Variables: {@code <input>}
*
* @since 1.5.0
*/
public static final Caption ARGUMENT_PARSE_FAILURE_TEAM_UNKNOWN = of(
"argument.parse.failure.team.unknown"
"argument.parse.failure.team.unknown"
);

private FabricCaptionKeys() {
private ModdedCaptionKeys() {
}

private static @NonNull Caption of(final @NonNull String key) {
Expand All @@ -70,7 +64,7 @@ private FabricCaptionKeys() {
* @return Immutable collection of keys
* @since 1.5.0
*/
public static @NonNull Collection<@NonNull Caption> fabricCaptionKeys() {
public static @NonNull Collection<@NonNull Caption> moddedCaptionKeys() {
return Collections.unmodifiableCollection(RECOGNIZED_CAPTIONS);
}
}
Loading

0 comments on commit 83138e1

Please sign in to comment.