Skip to content

Commit

Permalink
Merge pull request #146 from jpenilla/fix/145
Browse files Browse the repository at this point in the history
Use server's registry access in server audience component conversions
  • Loading branch information
zml2008 authored Jul 24, 2024
2 parents 6705596 + b9b2a89 commit d488655
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of adventure-platform-fabric, licensed under the MIT License.
*
* Copyright (c) 2020-2022 KyoriPowered
* Copyright (c) 2020-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
Expand All @@ -24,6 +24,7 @@
package net.kyori.adventure.platform.fabric.impl.client;

import java.util.function.Function;
import net.kyori.adventure.platform.fabric.impl.NonWrappingComponentSerializer;
import net.kyori.adventure.platform.fabric.impl.SidedProxy;
import net.kyori.adventure.platform.fabric.impl.WrappedComponent;
import net.kyori.adventure.pointer.Pointered;
Expand All @@ -47,7 +48,8 @@ public void contributeFlattenerElements(
public @NotNull WrappedComponent createWrappedComponent(
final @NotNull Component wrapped,
final @Nullable Function<Pointered, ?> partition,
final @Nullable ComponentRenderer<Pointered> renderer
final @Nullable ComponentRenderer<Pointered> renderer,
final @Nullable NonWrappingComponentSerializer nonWrappingSerializer
) {
return new ClientWrappedComponent(wrapped, partition, renderer);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of adventure-platform-fabric, licensed under the MIT License.
*
* Copyright (c) 2020-2023 KyoriPowered
* Copyright (c) 2020-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
Expand Down Expand Up @@ -33,7 +33,7 @@

public final class ClientWrappedComponent extends WrappedComponent {
public ClientWrappedComponent(final Component wrapped, final @Nullable Function<Pointered, ?> partition, final @Nullable ComponentRenderer<Pointered> renderer) {
super(wrapped, partition, renderer);
super(wrapped, partition, renderer, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public interface FabricAudiences {
partition = null;
renderer = null;
}
return AdventureCommon.SIDE_PROXY.createWrappedComponent(modified, partition, renderer);
return AdventureCommon.SIDE_PROXY.createWrappedComponent(modified, partition, renderer, null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private NonWrappingComponentSerializer() {
this(Suppliers.ofInstance(RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY)));
}

private NonWrappingComponentSerializer(final @NotNull Supplier<HolderLookup.@NotNull Provider> provider) {
public NonWrappingComponentSerializer(final @NotNull Supplier<HolderLookup.@NotNull Provider> provider) {
this.holderProvider = provider;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of adventure-platform-fabric, licensed under the MIT License.
*
* Copyright (c) 2020-2022 KyoriPowered
* Copyright (c) 2020-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
Expand Down Expand Up @@ -37,6 +37,7 @@ public interface SidedProxy {
@NotNull WrappedComponent createWrappedComponent(
final @NotNull Component wrapped,
final @Nullable Function<Pointered, ?> partition,
final @Nullable ComponentRenderer<Pointered> renderer
final @Nullable ComponentRenderer<Pointered> renderer,
final @Nullable NonWrappingComponentSerializer nonWrappingSerializer
);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of adventure-platform-fabric, licensed under the MIT License.
*
* Copyright (c) 2020-2023 KyoriPowered
* Copyright (c) 2020-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
Expand Down Expand Up @@ -32,6 +32,7 @@
import net.kyori.adventure.pointer.Pointers;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.renderer.ComponentRenderer;
import net.kyori.adventure.text.serializer.ComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentContents;
Expand All @@ -49,13 +50,20 @@ public class WrappedComponent implements Component {
private final net.kyori.adventure.text.Component wrapped;
private final @Nullable Function<Pointered, ?> partition;
private final @Nullable ComponentRenderer<Pointered> renderer;
private final @Nullable NonWrappingComponentSerializer nonWrappingSerializer;
private @Nullable Object lastData;
private @Nullable WrappedComponent lastRendered;

public WrappedComponent(final net.kyori.adventure.text.Component wrapped, final @Nullable Function<Pointered, ?> partition, final @Nullable ComponentRenderer<Pointered> renderer) {
public WrappedComponent(
final net.kyori.adventure.text.Component wrapped,
final @Nullable Function<Pointered, ?> partition,
final @Nullable ComponentRenderer<Pointered> renderer,
final @Nullable NonWrappingComponentSerializer nonWrappingComponentSerializer
) {
this.wrapped = wrapped;
this.partition = partition;
this.renderer = renderer;
this.nonWrappingSerializer = nonWrappingComponentSerializer;
}

/**
Expand Down Expand Up @@ -86,13 +94,17 @@ public synchronized WrappedComponent rendered(final Pointered ptr) {
return this.lastRendered;
}
this.lastData = data;
return this.lastRendered = this.renderer == null ? this : AdventureCommon.SIDE_PROXY.createWrappedComponent(this.renderer.render(this.wrapped, ptr), null, null);
return this.lastRendered = this.renderer == null ? this : AdventureCommon.SIDE_PROXY.createWrappedComponent(this.renderer.render(this.wrapped, ptr), null, null, this.nonWrappingSerializer);
}

public Component deepConverted() {
Component converted = this.converted;
if (converted == null || this.deepConvertedLocalized != null) {
converted = this.converted = FabricAudiences.nonWrappingSerializer().serialize(this.wrapped);
ComponentSerializer<net.kyori.adventure.text.Component, net.kyori.adventure.text.Component, Component> serializer = this.nonWrappingSerializer;
if (serializer == null) {
serializer = FabricAudiences.nonWrappingSerializer();
}
converted = this.converted = serializer.serialize(this.wrapped);
this.deepConvertedLocalized = null;
}
return converted;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of adventure-platform-fabric, licensed under the MIT License.
*
* Copyright (c) 2020-2022 KyoriPowered
* Copyright (c) 2020-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
Expand All @@ -24,6 +24,7 @@
package net.kyori.adventure.platform.fabric.impl.server;

import java.util.function.Function;
import net.kyori.adventure.platform.fabric.impl.NonWrappingComponentSerializer;
import net.kyori.adventure.platform.fabric.impl.SidedProxy;
import net.kyori.adventure.platform.fabric.impl.WrappedComponent;
import net.kyori.adventure.pointer.Pointered;
Expand All @@ -43,8 +44,9 @@ public void contributeFlattenerElements(final ComponentFlattener.@NotNull Builde
public @NotNull WrappedComponent createWrappedComponent(
final @NotNull Component wrapped,
final @Nullable Function<Pointered, ?> partition,
final @Nullable ComponentRenderer<Pointered> renderer
final @Nullable ComponentRenderer<Pointered> renderer,
final @Nullable NonWrappingComponentSerializer nonWrappingSerializer
) {
return new WrappedComponent(wrapped, partition, renderer);
return new WrappedComponent(wrapped, partition, renderer, nonWrappingSerializer);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of adventure-platform-fabric, licensed under the MIT License.
*
* Copyright (c) 2020-2023 KyoriPowered
* Copyright (c) 2020-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
Expand Down Expand Up @@ -38,6 +38,7 @@
import net.kyori.adventure.platform.fabric.impl.AdventureCommandSourceStackInternal;
import net.kyori.adventure.platform.fabric.impl.AdventureCommon;
import net.kyori.adventure.platform.fabric.impl.FabricAudiencesInternal;
import net.kyori.adventure.platform.fabric.impl.NonWrappingComponentSerializer;
import net.kyori.adventure.pointer.Pointered;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.flattener.ComponentFlattener;
Expand Down Expand Up @@ -76,12 +77,14 @@ public static void forEachInstance(final Consumer<FabricServerAudiencesImpl> act
}

private final MinecraftServer server;
private final NonWrappingComponentSerializer nonWrappingSerializer;
private final Function<Pointered, ?> partition;
private final ComponentRenderer<Pointered> renderer;
final ServerBossBarListener bossBars;

public FabricServerAudiencesImpl(final MinecraftServer server, final Function<Pointered, ?> partition, final ComponentRenderer<Pointered> renderer) {
this.server = server;
this.nonWrappingSerializer = new NonWrappingComponentSerializer(this::registryAccess);
this.partition = partition;
this.renderer = renderer;
this.bossBars = new ServerBossBarListener(this);
Expand Down Expand Up @@ -179,7 +182,12 @@ private Iterable<Audience> audiences(final Iterable<? extends ServerPlayer> play
public net.minecraft.network.chat.@NotNull Component toNative(final @NotNull Component adventure) {
if (adventure == Component.empty()) return net.minecraft.network.chat.Component.empty();

return AdventureCommon.SIDE_PROXY.createWrappedComponent(requireNonNull(adventure, "adventure"), this.partition, this.renderer);
return AdventureCommon.SIDE_PROXY.createWrappedComponent(requireNonNull(adventure, "adventure"), this.partition, this.renderer, this.nonWrappingSerializer);
}

@Override
public @NotNull Component toAdventure(final net.minecraft.network.chat.@NotNull Component vanilla) {
return this.nonWrappingSerializer.deserialize(vanilla);
}

public ServerBossBarListener bossBars() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,6 @@ private static String toStableString(final JsonElement stringable) {

private WrappedComponent toNativeWrapped(final Component component) {
final Function<Pointered, Locale> partition = AdventureCommon.localePartition();
return new WrappedComponent(component, partition, GlobalTranslator.renderer().mapContext(partition));
return new WrappedComponent(component, partition, GlobalTranslator.renderer().mapContext(partition), null);
}
}

0 comments on commit d488655

Please sign in to comment.