Skip to content

Commit

Permalink
Merge pull request #165 from KyoriPowered/bug/backport-163
Browse files Browse the repository at this point in the history
Backport #163 to 5.x
  • Loading branch information
zml2008 authored Oct 20, 2024
2 parents 85a2ed5 + 35bffb4 commit 26662dd
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,17 @@ public <T> Optional<T> visit(final ContentConsumer<T> visitor) {
public net.kyori.adventure.text.@NotNull Component asComponent() {
return this.wrapped;
}

@Override
public int hashCode() {
return this.deepConverted().hashCode();
}

@Override
public boolean equals(final Object obj) {
if (obj instanceof final WrappedComponent wrappedComponent) {
return this.wrapped().equals(wrappedComponent.wrapped());
}
return this.deepConverted().equals(obj);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* This file is part of adventure-platform-fabric, 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.fabric.impl.mixin.minecraft.network.chat;

import net.kyori.adventure.platform.fabric.impl.WrappedComponent;
import net.minecraft.network.chat.MutableComponent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(MutableComponent.class)
abstract class MutableComponentMixin {
@Inject(method = "equals", at = @At("HEAD"), cancellable = true)
private void hookEquals(final Object other, final CallbackInfoReturnable<Boolean> cir) {
if (other instanceof final WrappedComponent wrapped) {
cir.setReturnValue(wrapped.equals(this));
}
}
}
1 change: 1 addition & 0 deletions src/mixin/resources/adventure-platform-fabric.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"minecraft.network.chat.ComponentMixin$SerializerMixin",
"minecraft.network.chat.ComponentSerializationMixin",
"minecraft.network.chat.MessageSignatureMixin",
"minecraft.network.chat.MutableComponentMixin",
"minecraft.network.chat.PlayerChatMessageMixin",
"minecraft.network.protocol.game.ClientboundCommandsPacketMixin",
"minecraft.network.protocol.game.ClientboundCommandsPacketMixin$ArgumentNodeStubMixin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.stream.Stream;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.platform.fabric.impl.AdventureCommon;
import net.kyori.adventure.platform.fabric.impl.NonWrappingComponentSerializer;
import net.kyori.adventure.platform.fabric.impl.WrappedComponent;
import net.kyori.adventure.pointer.Pointered;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -114,6 +115,16 @@ void testSerializationEqualWhenWrappedNested(final Component input) {
assertJsonTreesEqual(serializedNative, serialized);
}

@TestOnComponents
void testComponentEquality(final Component input) {
final net.minecraft.network.chat.Component deepConverted = new NonWrappingComponentSerializer(BootstrappedTest::lookup).serialize(input);
final net.minecraft.network.chat.Component wrapped = this.toNativeWrapped(input);
final net.minecraft.network.chat.Component wrapped2 = this.toNativeWrapped(input);
assertEquals(wrapped, wrapped2, "two wrapped components should be equal");
assertEquals(deepConverted, wrapped, "deep should equal wrapped");
assertEquals(wrapped, deepConverted, "wrapped should equal deep");
}

private static void assertJsonTreesEqual(final JsonElement expected, final JsonElement actual) {
assertEquals(toStableString(expected), toStableString(actual));
}
Expand Down

0 comments on commit 26662dd

Please sign in to comment.