Implement methods to convert between Component and Brigadier's Message (#5542)
This commit is contained in:
		
					parent
					
						
							
								c2b9df245c
							
						
					
				
			
			
				commit
				
					
						1da2f0afbc
					
				
			
		
					 3 changed files with 127 additions and 0 deletions
				
			
		| 
						 | 
				
			
			@ -0,0 +1,42 @@
 | 
			
		|||
package io.papermc.paper.brigadier;
 | 
			
		||||
 | 
			
		||||
import com.mojang.brigadier.Message;
 | 
			
		||||
import net.kyori.adventure.text.Component;
 | 
			
		||||
import net.kyori.adventure.text.ComponentLike;
 | 
			
		||||
import net.kyori.adventure.text.TextComponent;
 | 
			
		||||
import org.checkerframework.checker.nullness.qual.NonNull;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Helper methods to bridge the gaps between Brigadier and Paper-MojangAPI.
 | 
			
		||||
 */
 | 
			
		||||
public final class PaperBrigadier {
 | 
			
		||||
    private PaperBrigadier() {
 | 
			
		||||
        throw new RuntimeException("PaperBrigadier is not to be instantiated!");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Create a new Brigadier {@link Message} from a {@link ComponentLike}.
 | 
			
		||||
     *
 | 
			
		||||
     * <p>Mostly useful for creating rich suggestion tooltips in combination with other Paper-MojangAPI APIs.</p>
 | 
			
		||||
     *
 | 
			
		||||
     * @param componentLike The {@link ComponentLike} to use for the {@link Message} contents
 | 
			
		||||
     * @return A new Brigadier {@link Message}
 | 
			
		||||
     */
 | 
			
		||||
    public static @NonNull Message message(final @NonNull ComponentLike componentLike) {
 | 
			
		||||
        return PaperBrigadierProvider.instance().message(componentLike);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Create a new {@link Component} from a Brigadier {@link Message}.
 | 
			
		||||
     *
 | 
			
		||||
     * <p>If the {@link Message} was created from a {@link Component}, it will simply be
 | 
			
		||||
     * converted back, otherwise a new {@link TextComponent} will be created with the
 | 
			
		||||
     * content of {@link Message#getString()}</p>
 | 
			
		||||
     *
 | 
			
		||||
     * @param message The {@link Message} to create a {@link Component} from
 | 
			
		||||
     * @return The created {@link Component}
 | 
			
		||||
     */
 | 
			
		||||
    public static @NonNull Component componentFromMessage(final @NonNull Message message) {
 | 
			
		||||
        return PaperBrigadierProvider.instance().componentFromMessage(message);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,30 @@
 | 
			
		|||
package io.papermc.paper.brigadier;
 | 
			
		||||
 | 
			
		||||
import com.mojang.brigadier.Message;
 | 
			
		||||
import net.kyori.adventure.text.Component;
 | 
			
		||||
import net.kyori.adventure.text.ComponentLike;
 | 
			
		||||
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
 | 
			
		||||
import org.checkerframework.checker.nullness.qual.NonNull;
 | 
			
		||||
 | 
			
		||||
import static java.util.Objects.requireNonNull;
 | 
			
		||||
 | 
			
		||||
interface PaperBrigadierProvider {
 | 
			
		||||
    final class Holder {
 | 
			
		||||
        private static @MonotonicNonNull PaperBrigadierProvider INSTANCE;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static @NonNull PaperBrigadierProvider instance() {
 | 
			
		||||
        return requireNonNull(Holder.INSTANCE, "PaperBrigadierProvider has not yet been initialized!");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static void initialize(final @NonNull PaperBrigadierProvider instance) {
 | 
			
		||||
        if (Holder.INSTANCE != null) {
 | 
			
		||||
            throw new IllegalStateException("PaperBrigadierProvider has already been initialized!");
 | 
			
		||||
        }
 | 
			
		||||
        Holder.INSTANCE = instance;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @NonNull Message message(@NonNull ComponentLike componentLike);
 | 
			
		||||
 | 
			
		||||
    @NonNull Component componentFromMessage(@NonNull Message message);
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue