Skip to content

Commit c9bc275

Browse files
author
lukas81298
committed
Fixed combined accessors for adventure components on Paper
1 parent 49064f8 commit c9bc275

File tree

8 files changed

+83
-62
lines changed

8 files changed

+83
-62
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.comphenix.packetwrapper</groupId>
66
<artifactId>PacketWrapper</artifactId>
7-
<version>1.19.4-2.1.1-SNAPSHOT</version>
7+
<version>1.19.4-2.1.1</version>
88

99
<name>PacketWrapper</name>
1010

src/main/java/com/comphenix/packetwrapper/util/ReflectiveAdventureComponentConverter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.comphenix.packetwrapper.util;
22

3+
import com.comphenix.protocol.reflect.FuzzyReflection;
34
import com.comphenix.protocol.reflect.accessors.Accessors;
45
import com.comphenix.protocol.reflect.accessors.MethodAccessor;
56
import com.comphenix.protocol.utility.MinecraftReflection;
@@ -8,10 +9,11 @@
89
public class ReflectiveAdventureComponentConverter {
910
private static final Class<?> COMPONENT_CLASS = MinecraftReflection.getLibraryClass("net.kyori.adventure.text.Component");
1011
private static final Class<?> GSON_COMPONENT_SERIALIZER_CLASS = MinecraftReflection.getLibraryClass("net.kyori.adventure.text.serializer.gson.GsonComponentSerializer");
12+
private static final Class<?> COMPONENT_SERIALIZER_CLASS = MinecraftReflection.getLibraryClass("net.kyori.adventure.text.serializer.ComponentSerializer");
1113

1214
private static final MethodAccessor GET_GSON_SERIALIZER_METHOD = Accessors.getMethodAccessor(GSON_COMPONENT_SERIALIZER_CLASS, "gson");
13-
private static final MethodAccessor DESERIALIZE_METHOD = Accessors.getMethodAccessor(GSON_COMPONENT_SERIALIZER_CLASS, "deserialize", String.class);
14-
private static final MethodAccessor SERIALIZE_METHOD = Accessors.getMethodAccessor(GSON_COMPONENT_SERIALIZER_CLASS, "serialize", COMPONENT_CLASS);
15+
private static final MethodAccessor DESERIALIZE_METHOD = Accessors.getMethodAccessor(FuzzyReflection.fromClass(COMPONENT_SERIALIZER_CLASS, false).getMethodByName("deserializeOrNull"));
16+
private static final MethodAccessor SERIALIZE_METHOD = Accessors.getMethodAccessor(FuzzyReflection.fromClass(COMPONENT_SERIALIZER_CLASS, false).getMethodByName("serialize"));
1517

1618
private static Object GSON_SERIALIZER;
1719

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.comphenix.packetwrapper.wrappers.play.clientbound;
22

3+
import com.comphenix.packetwrapper.util.ReflectiveAdventureComponentConverter;
34
import com.comphenix.packetwrapper.wrappers.AbstractPacket;
45
import com.comphenix.protocol.PacketType;
5-
import com.comphenix.protocol.events.InternalStructure;
66
import com.comphenix.protocol.events.PacketContainer;
7-
import com.comphenix.protocol.wrappers.AdventureComponentConverter;
87
import com.comphenix.protocol.wrappers.WrappedChatComponent;
98

9+
import java.sql.Ref;
10+
11+
/**
12+
* Sents by server to client to update the header and footer in the tab list
13+
*/
1014
public class WrapperPlayServerPlayerListHeaderFooter extends AbstractPacket {
1115

1216
public static final PacketType TYPE = PacketType.Play.Server.PLAYER_LIST_HEADER_FOOTER;
@@ -22,80 +26,68 @@ public WrapperPlayServerPlayerListHeaderFooter(PacketContainer packet) {
2226
super(packet, TYPE);
2327
}
2428

29+
private boolean isUsingPaper() {
30+
return this.handle.getModifier().size() > 2;
31+
}
32+
2533
/**
26-
* Retrieves the value of field 'header'
34+
* Retrieves the component that will be displayed above the tab list
2735
*
2836
* @return 'header'
2937
*/
3038
public WrappedChatComponent getHeader() {
31-
return this.handle.getChatComponents().read(0);
39+
WrappedChatComponent read = this.handle.getChatComponents().read(0);
40+
if (read != null) {
41+
return read;
42+
}
43+
return ReflectiveAdventureComponentConverter.fromComponent(this.handle.getModifier().read(2));
3244
}
3345

3446
/**
35-
* Sets the value of field 'header'
47+
* Sets the component that will be displayed above the tab list
3648
*
3749
* @param value New value for field 'header'
3850
*/
3951
public void setHeader(WrappedChatComponent value) {
4052
this.handle.getChatComponents().write(0, value);
53+
if (isUsingPaper()) {
54+
this.handle.getModifier().write(2, null);
55+
Object footerNms = this.handle.getModifier().read(3);
56+
if (footerNms != null) {
57+
this.handle.getChatComponents().write(1, ReflectiveAdventureComponentConverter.fromComponent(footerNms));
58+
}
59+
this.handle.getModifier().write(3, null);
60+
}
4161
}
4262

4363
/**
44-
* Retrieves the value of field 'footer'
64+
* Retrieves the component that will be displayed below the tab list
4565
*
4666
* @return 'footer'
4767
*/
4868
public WrappedChatComponent getFooter() {
49-
return this.handle.getChatComponents().read(1);
69+
WrappedChatComponent read = this.handle.getChatComponents().read(1);
70+
if (read != null) {
71+
return read;
72+
}
73+
return ReflectiveAdventureComponentConverter.fromComponent(this.handle.getModifier().read(3));
5074
}
5175

5276
/**
53-
* Sets the value of field 'footer'
77+
* Sets the component that will be displayed below the tab list
5478
*
5579
* @param value New value for field 'footer'
5680
*/
5781
public void setFooter(WrappedChatComponent value) {
5882
this.handle.getChatComponents().write(1, value);
59-
}
60-
61-
/**
62-
* Retrieves the value of field 'adventure$header'
63-
* ProtocolLib currently does not provide a wrapper for this type. Access to this type is only provided by an InternalStructure
64-
*
65-
* @return 'adventure$header'
66-
*/
67-
public InternalStructure getAdventure$headerInternal() {
68-
return this.handle.getStructures().read(2); // TODO: No specific modifier has been found for type interface net.kyori.adventure.text.Component Generic type: interface net.kyori.adventure.text.Component
69-
}
70-
71-
/**
72-
* Sets the value of field 'adventure$header'
73-
* ProtocolLib currently does not provide a wrapper for this type. Access to this type is only provided by an InternalStructure
74-
*
75-
* @param value New value for field 'adventure$header'
76-
*/
77-
public void setAdventure$headerInternal(InternalStructure value) {
78-
this.handle.getStructures().write(2, value); // TODO: No specific modifier has been found for type interface net.kyori.adventure.text.Component Generic type: interface net.kyori.adventure.text.Component
79-
}
80-
81-
/**
82-
* Retrieves the value of field 'adventure$footer'
83-
* ProtocolLib currently does not provide a wrapper for this type. Access to this type is only provided by an InternalStructure
84-
*
85-
* @return 'adventure$footer'
86-
*/
87-
public InternalStructure getAdventure$footerInternal() {
88-
return this.handle.getStructures().read(3); // TODO: No specific modifier has been found for type interface net.kyori.adventure.text.Component Generic type: interface net.kyori.adventure.text.Component
89-
}
90-
91-
/**
92-
* Sets the value of field 'adventure$footer'
93-
* ProtocolLib currently does not provide a wrapper for this type. Access to this type is only provided by an InternalStructure
94-
*
95-
* @param value New value for field 'adventure$footer'
96-
*/
97-
public void setAdventure$footerInternal(InternalStructure value) {
98-
this.handle.getStructures().write(3, value); // TODO: No specific modifier has been found for type interface net.kyori.adventure.text.Component Generic type: interface net.kyori.adventure.text.Component
83+
if (isUsingPaper()) {
84+
this.handle.getModifier().write(3, null);
85+
Object headerNms = this.handle.getModifier().read(2);
86+
if(headerNms != null) {
87+
this.handle.getChatComponents().write(0, ReflectiveAdventureComponentConverter.fromComponent(headerNms));
88+
}
89+
this.handle.getModifier().write(2, null);
90+
}
9991
}
10092

10193
}

src/main/java/com/comphenix/packetwrapper/wrappers/play/clientbound/WrapperPlayServerSetActionBarText.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public WrappedChatComponent getText() {
3535
if(read != null) {
3636
return read;
3737
}
38-
Object adventure = this.handle.getStructures().read(1).getHandle();
38+
Object adventure = this.handle.getModifier().read(1);
3939
if(adventure != null) {
4040
return ReflectiveAdventureComponentConverter.fromComponent(adventure);
4141
}
42-
BaseComponent[] baseComponents = (BaseComponent[]) this.handle.getStructures().read(2).getHandle();
42+
BaseComponent[] baseComponents = (BaseComponent[]) this.handle.getModifier().read(2);
4343
return ComponentConverter.fromBaseComponent(baseComponents);
4444
}
4545

@@ -50,7 +50,7 @@ public WrappedChatComponent getText() {
5050
*/
5151
public void setText(WrappedChatComponent value) {
5252
this.handle.getChatComponents().write(0, value);
53-
StructureModifier<InternalStructure> structures = this.handle.getStructures();
53+
StructureModifier<Object> structures = this.handle.getModifier();
5454
if(structures.size() > 1) {
5555
for(int i = 1; i < structures.size(); i++) {
5656
structures.write(i, null);

src/main/java/com/comphenix/packetwrapper/wrappers/play/clientbound/WrapperPlayServerSetSubtitleText.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public WrappedChatComponent getText() {
3535
if(read != null) {
3636
return read;
3737
}
38-
Object adventure = this.handle.getStructures().read(1).getHandle();
38+
Object adventure = this.handle.getModifier().read(1);
3939
if(adventure != null) {
4040
return ReflectiveAdventureComponentConverter.fromComponent(adventure);
4141
}
42-
BaseComponent[] baseComponents = (BaseComponent[]) this.handle.getStructures().read(2).getHandle();
42+
BaseComponent[] baseComponents = (BaseComponent[]) this.handle.getModifier().read(2);
4343
return ComponentConverter.fromBaseComponent(baseComponents);
4444
}
4545

@@ -50,7 +50,7 @@ public WrappedChatComponent getText() {
5050
*/
5151
public void setText(WrappedChatComponent value) {
5252
this.handle.getChatComponents().write(0, value);
53-
StructureModifier<InternalStructure> structures = this.handle.getStructures();
53+
StructureModifier<Object> structures = this.handle.getModifier();
5454
if(structures.size() > 1) {
5555
for(int i = 1; i < structures.size(); i++) {
5656
structures.write(i, null);

src/main/java/com/comphenix/packetwrapper/wrappers/play/clientbound/WrapperPlayServerSetTitleText.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ public WrappedChatComponent getText() {
3636
if(read != null) {
3737
return read;
3838
}
39-
Object adventure = this.handle.getStructures().read(1).getHandle();
39+
Object adventure = this.handle.getModifier().read(1);
4040
if(adventure != null) {
4141
return ReflectiveAdventureComponentConverter.fromComponent(adventure);
4242
}
43-
BaseComponent[] baseComponents = (BaseComponent[]) this.handle.getStructures().read(2).getHandle();
43+
BaseComponent[] baseComponents = (BaseComponent[]) this.handle.getModifier().read(2);
4444
return ComponentConverter.fromBaseComponent(baseComponents);
4545
}
4646

@@ -51,7 +51,7 @@ public WrappedChatComponent getText() {
5151
*/
5252
public void setText(WrappedChatComponent value) {
5353
this.handle.getChatComponents().write(0, value);
54-
StructureModifier<InternalStructure> structures = this.handle.getStructures();
54+
StructureModifier<Object> structures = this.handle.getModifier();
5555
if(structures.size() > 1) {
5656
for(int i = 1; i < structures.size(); i++) {
5757
structures.write(i, null);

src/main/java/com/comphenix/packetwrapper/wrappers/play/clientbound/WrapperPlayServerSystemChat.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public String getContent() {
4444
public void setContent(String value) {
4545
if(isUsingPaper()) {
4646
// We are using paper. Remove adventure component
47-
this.handle.getStructures().write(0, null);
47+
this.handle.getModifier().write(0, null);
4848
}
4949
this.handle.getStrings().write(0, value);
5050
}
@@ -68,7 +68,7 @@ public void setContentComponent(WrappedChatComponent component) {
6868
}
6969

7070
private boolean isUsingPaper() {
71-
return !(this.handle.getStructures().read(0).getHandle() instanceof String);
71+
return !(this.handle.getModifier().read(0) instanceof String);
7272
}
7373

7474
/**
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.comphenix.packetwrapper.wrappers.play.clientbound;
2+
3+
import com.comphenix.packetwrapper.BaseTestInitialization;
4+
import com.comphenix.protocol.wrappers.WrappedChatComponent;
5+
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.api.extension.ExtendWith;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
/**
11+
* @author Lukas Alt
12+
* @since 31.05.2023
13+
*/
14+
@ExtendWith(BaseTestInitialization.class)
15+
class WrapperPlayServerPlayerListHeaderFooterTest {
16+
@Test
17+
public void test() {
18+
WrapperPlayServerPlayerListHeaderFooter dummy = new WrapperPlayServerPlayerListHeaderFooter();
19+
20+
WrappedChatComponent header = WrappedChatComponent.fromText("Header");
21+
WrappedChatComponent footer = WrappedChatComponent.fromText("Footer");
22+
dummy.setHeader(header);
23+
dummy.setFooter(footer);
24+
assertEquals(header, dummy.getHeader());
25+
assertEquals(footer, dummy.getFooter());
26+
}
27+
}

0 commit comments

Comments
 (0)