Skip to content

Commit

Permalink
Add helper for registering adventure argument types on NeoForge
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Aug 2, 2024
1 parent 9367145 commit 26ecc28
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This file is part of adventure-platform-mod, licensed under the MIT License.
*
* Copyright (c) 2024 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.platform.neoforge;

import net.kyori.adventure.platform.neoforge.impl.AdventureNeoforgeCommon;

/**
* Helper for registering {@link net.kyori.adventure.platform.modcommon.ComponentArgumentType} and
* {@link net.kyori.adventure.platform.modcommon.KeyArgumentType}.
*/
public final class AdventureArgumentTypes {
private AdventureArgumentTypes() {
}

private static boolean registered = false;

/**
* Registers the Adventure {@link com.mojang.brigadier.arguments.ArgumentType}s.
*
* <p>This is not done by default as it requires the client to also register the types.</p>
*/
public static synchronized void register() {
if (registered) {
return;
}

registered = true;

AdventureNeoforgeCommon.registerArgumentTypes();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import net.kyori.adventure.permission.PermissionChecker;
import net.kyori.adventure.platform.modcommon.ComponentArgumentType;
import net.kyori.adventure.platform.modcommon.KeyArgumentType;
import net.kyori.adventure.platform.modcommon.impl.AdventureCommon;
import net.kyori.adventure.platform.modcommon.impl.ClickCallbackRegistry;
import net.kyori.adventure.platform.modcommon.impl.ComponentArgumentTypeSerializer;
import net.kyori.adventure.platform.modcommon.impl.LocaleHolderBridge;
import net.kyori.adventure.platform.modcommon.impl.PlatformHooks;
import net.kyori.adventure.platform.modcommon.impl.SidedProxy;
Expand All @@ -45,22 +48,29 @@
import net.kyori.adventure.pointer.Pointered;
import net.kyori.adventure.pointer.Pointers;
import net.kyori.adventure.util.TriState;
import net.minecraft.commands.synchronization.ArgumentTypeInfo;
import net.minecraft.commands.synchronization.ArgumentTypeInfos;
import net.minecraft.commands.synchronization.SingletonArgumentInfo;
import net.minecraft.core.registries.Registries;
import net.minecraft.server.level.ServerPlayer;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.loading.FMLLoader;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.RegisterCommandsEvent;
import net.neoforged.neoforge.event.entity.player.ClientInformationUpdatedEvent;
import net.neoforged.neoforge.registries.DeferredRegister;
import net.neoforged.neoforge.server.permission.PermissionAPI;
import net.neoforged.neoforge.server.permission.nodes.PermissionNode;
import net.neoforged.neoforge.server.permission.nodes.PermissionTypes;
import org.jetbrains.annotations.Nullable;

@Mod("adventure_platform_neoforge")
@SuppressWarnings("checkstyle:HideUtilityClassConstructor") // Not a utility class, this is our main mod class.
public class AdventureNeoforgeCommon {
public final class AdventureNeoforgeCommon {
public static SidedProxy SIDE_PROXY;
private static final DeferredRegister<ArgumentTypeInfo<?, ?>> COMMAND_ARGUMENT_TYPES = DeferredRegister.create(Registries.COMMAND_ARGUMENT_TYPE, "adventure");

static {
if (FMLLoader.getDist() == Dist.DEDICATED_SERVER) {
Expand All @@ -83,7 +93,20 @@ public class AdventureNeoforgeCommon {
PlainTextComponentSerializerProviderImpl.DELEGATE = new net.kyori.adventure.platform.modcommon.impl.service.PlainTextComponentSerializerProviderImpl();
}

public AdventureNeoforgeCommon() {
public static void registerArgumentTypes() {
COMMAND_ARGUMENT_TYPES.register("component", () -> ArgumentTypeInfos.registerByClass(
ComponentArgumentType.class,
ComponentArgumentTypeSerializer.INSTANCE
));
COMMAND_ARGUMENT_TYPES.register("key", () -> ArgumentTypeInfos.registerByClass(
KeyArgumentType.class,
SingletonArgumentInfo.contextFree(KeyArgumentType::key)
));
}

public AdventureNeoforgeCommon(final IEventBus bus) {
COMMAND_ARGUMENT_TYPES.register(bus);

NeoForge.EVENT_BUS.addListener((ClientInformationUpdatedEvent e) -> {
if (!Objects.equals(e.getOldInformation().language(), e.getUpdatedInformation().language())) {
AdventureCommon.HOOKS.onLocaleChange(e.getEntity(), LocaleHolderBridge.toLocale(e.getUpdatedInformation().language()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@
import net.kyori.adventure.inventory.Book;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.platform.modcommon.AdventureCommandSourceStack;
import net.kyori.adventure.platform.modcommon.ComponentArgumentType;
import net.kyori.adventure.platform.modcommon.KeyArgumentType;
import net.kyori.adventure.platform.modcommon.MinecraftAudiences;
import net.kyori.adventure.platform.modcommon.MinecraftServerAudiences;
import net.kyori.adventure.platform.modcommon.impl.ComponentArgumentTypeSerializer;
import net.kyori.adventure.platform.neoforge.AdventureArgumentTypes;
import net.kyori.adventure.pointer.Pointered;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.Component;
Expand All @@ -67,24 +65,17 @@
import net.kyori.adventure.translation.GlobalTranslator;
import net.kyori.adventure.translation.TranslationRegistry;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.commands.synchronization.ArgumentTypeInfo;
import net.minecraft.commands.synchronization.ArgumentTypeInfos;
import net.minecraft.commands.synchronization.SingletonArgumentInfo;
import net.minecraft.commands.synchronization.SuggestionProviders;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.ComponentUtils;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.RegisterCommandsEvent;
import net.neoforged.neoforge.event.server.ServerStartingEvent;
import net.neoforged.neoforge.event.server.ServerStoppedEvent;
import net.neoforged.neoforge.registries.DeferredRegister;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -136,19 +127,8 @@ private static Key advKey(final String location) {
return requireNonNull(this.platform, "Tried to access Fabric platform without a running server");
}

// TODO - add registration helper for neo
private static final DeferredRegister<ArgumentTypeInfo<?, ?>> COMMAND_ARGUMENT_TYPES = DeferredRegister.create(Registries.COMMAND_ARGUMENT_TYPE, "adventure");
private static final Holder<ArgumentTypeInfo<?, ?>> COMPONENT_ARGUMENT_TYPE = COMMAND_ARGUMENT_TYPES.register("component", () -> ArgumentTypeInfos.registerByClass(
ComponentArgumentType.class,
ComponentArgumentTypeSerializer.INSTANCE
));
private static final Holder<ArgumentTypeInfo<?, ?>> KEY_ARGUMENT_TYPE = COMMAND_ARGUMENT_TYPES.register("key", () -> ArgumentTypeInfos.registerByClass(
KeyArgumentType.class,
SingletonArgumentInfo.contextFree(KeyArgumentType::key)
));

public AdventureNeoTester(final IEventBus modBus) {
COMMAND_ARGUMENT_TYPES.register(modBus);
public AdventureNeoTester() {
AdventureArgumentTypes.register();

// Register localizations
final TranslationRegistry testmodRegistry = TranslationRegistry.create(advKey("testmod_localizations"));
Expand Down

0 comments on commit 26ecc28

Please sign in to comment.