Skip to content

Commit 49064f8

Browse files
author
lukas81298
committed
implement fluent access to Paper's additional chat component fields in chat packets
1 parent 82ab803 commit 49064f8

File tree

6 files changed

+149
-159
lines changed

6 files changed

+149
-159
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.comphenix.packetwrapper.util;
2+
3+
import com.comphenix.protocol.reflect.accessors.Accessors;
4+
import com.comphenix.protocol.reflect.accessors.MethodAccessor;
5+
import com.comphenix.protocol.utility.MinecraftReflection;
6+
import com.comphenix.protocol.wrappers.WrappedChatComponent;
7+
8+
public class ReflectiveAdventureComponentConverter {
9+
private static final Class<?> COMPONENT_CLASS = MinecraftReflection.getLibraryClass("net.kyori.adventure.text.Component");
10+
private static final Class<?> GSON_COMPONENT_SERIALIZER_CLASS = MinecraftReflection.getLibraryClass("net.kyori.adventure.text.serializer.gson.GsonComponentSerializer");
11+
12+
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+
16+
private static Object GSON_SERIALIZER;
17+
18+
private static Object getGsonSerializer() {
19+
if(GSON_SERIALIZER == null) {
20+
GSON_SERIALIZER = GET_GSON_SERIALIZER_METHOD.invoke(null);
21+
}
22+
return GSON_SERIALIZER;
23+
}
24+
25+
public static Object fromWrapper(WrappedChatComponent wrapper) {
26+
return DESERIALIZE_METHOD.invoke(getGsonSerializer(), wrapper.getJson());
27+
}
28+
29+
public static WrappedChatComponent fromComponent(Object component) {
30+
return WrappedChatComponent.fromJson((String)SERIALIZE_METHOD.invoke(getGsonSerializer(), component));
31+
}
32+
33+
public static String componentToString(Object component) {
34+
return (String)SERIALIZE_METHOD.invoke(getGsonSerializer(), component);
35+
}
36+
37+
public static Class<?> getComponentClass() {
38+
return COMPONENT_CLASS;
39+
}
40+
}
Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
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;
56
import com.comphenix.protocol.events.InternalStructure;
67
import com.comphenix.protocol.events.PacketContainer;
8+
import com.comphenix.protocol.reflect.StructureModifier;
9+
import com.comphenix.protocol.wrappers.ComponentConverter;
710
import com.comphenix.protocol.wrappers.WrappedChatComponent;
11+
import net.md_5.bungee.api.chat.BaseComponent;
812

913
public class WrapperPlayServerSetActionBarText extends AbstractPacket {
1014

@@ -22,61 +26,36 @@ public WrapperPlayServerSetActionBarText(PacketContainer packet) {
2226
}
2327

2428
/**
25-
* Retrieves the value of field 'text'
29+
* Get the action bar component
2630
*
27-
* @return 'text'
31+
* @return action bar components
2832
*/
2933
public WrappedChatComponent getText() {
30-
return this.handle.getChatComponents().read(0);
34+
WrappedChatComponent read = this.handle.getChatComponents().read(0);
35+
if(read != null) {
36+
return read;
37+
}
38+
Object adventure = this.handle.getStructures().read(1).getHandle();
39+
if(adventure != null) {
40+
return ReflectiveAdventureComponentConverter.fromComponent(adventure);
41+
}
42+
BaseComponent[] baseComponents = (BaseComponent[]) this.handle.getStructures().read(2).getHandle();
43+
return ComponentConverter.fromBaseComponent(baseComponents);
3144
}
3245

3346
/**
34-
* Sets the value of field 'text'
47+
* Sets the action bar component
3548
*
36-
* @param value New value for field 'text'
49+
* @param value New action bar component
3750
*/
3851
public void setText(WrappedChatComponent value) {
3952
this.handle.getChatComponents().write(0, value);
40-
}
41-
42-
/**
43-
* Retrieves the value of field 'adventure$text'
44-
* ProtocolLib currently does not provide a wrapper for this type. Access to this type is only provided by an InternalStructure
45-
*
46-
* @return 'adventure$text'
47-
*/
48-
public InternalStructure getAdventure$textInternal() {
49-
return this.handle.getStructures().read(1); // TODO: No specific modifier has been found for type interface net.kyori.adventure.text.Component Generic type: interface net.kyori.adventure.text.Component
50-
}
51-
52-
/**
53-
* Sets the value of field 'adventure$text'
54-
* ProtocolLib currently does not provide a wrapper for this type. Access to this type is only provided by an InternalStructure
55-
*
56-
* @param value New value for field 'adventure$text'
57-
*/
58-
public void setAdventure$textInternal(InternalStructure value) {
59-
this.handle.getStructures().write(1, value); // TODO: No specific modifier has been found for type interface net.kyori.adventure.text.Component Generic type: interface net.kyori.adventure.text.Component
60-
}
61-
62-
/**
63-
* Retrieves the value of field 'components'
64-
* ProtocolLib currently does not provide a wrapper for this type. Access to this type is only provided by an InternalStructure
65-
*
66-
* @return 'components'
67-
*/
68-
public InternalStructure getComponentsInternal() {
69-
return this.handle.getStructures().read(2); // TODO: No specific modifier has been found for type class [Lnet.md_5.bungee.api.chat.BaseComponent; Generic type: class [Lnet.md_5.bungee.api.chat.BaseComponent;
70-
}
71-
72-
/**
73-
* Sets the value of field 'components'
74-
* ProtocolLib currently does not provide a wrapper for this type. Access to this type is only provided by an InternalStructure
75-
*
76-
* @param value New value for field 'components'
77-
*/
78-
public void setComponentsInternal(InternalStructure value) {
79-
this.handle.getStructures().write(2, value); // TODO: No specific modifier has been found for type class [Lnet.md_5.bungee.api.chat.BaseComponent; Generic type: class [Lnet.md_5.bungee.api.chat.BaseComponent;
53+
StructureModifier<InternalStructure> structures = this.handle.getStructures();
54+
if(structures.size() > 1) {
55+
for(int i = 1; i < structures.size(); i++) {
56+
structures.write(i, null);
57+
}
58+
}
8059
}
8160

8261
}
Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
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;
56
import com.comphenix.protocol.events.InternalStructure;
67
import com.comphenix.protocol.events.PacketContainer;
8+
import com.comphenix.protocol.reflect.StructureModifier;
9+
import com.comphenix.protocol.wrappers.ComponentConverter;
710
import com.comphenix.protocol.wrappers.WrappedChatComponent;
11+
import net.md_5.bungee.api.chat.BaseComponent;
812

913
public class WrapperPlayServerSetSubtitleText extends AbstractPacket {
1014

@@ -22,61 +26,36 @@ public WrapperPlayServerSetSubtitleText(PacketContainer packet) {
2226
}
2327

2428
/**
25-
* Retrieves the value of field 'text'
29+
* Retrieves the subtitle as a chat component
2630
*
27-
* @return 'text'
31+
* @return 'text' subtitle component
2832
*/
2933
public WrappedChatComponent getText() {
30-
return this.handle.getChatComponents().read(0);
34+
WrappedChatComponent read = this.handle.getChatComponents().read(0);
35+
if(read != null) {
36+
return read;
37+
}
38+
Object adventure = this.handle.getStructures().read(1).getHandle();
39+
if(adventure != null) {
40+
return ReflectiveAdventureComponentConverter.fromComponent(adventure);
41+
}
42+
BaseComponent[] baseComponents = (BaseComponent[]) this.handle.getStructures().read(2).getHandle();
43+
return ComponentConverter.fromBaseComponent(baseComponents);
3144
}
3245

3346
/**
34-
* Sets the value of field 'text'
47+
* Sets the subtitle as a chat component
3548
*
3649
* @param value New value for field 'text'
3750
*/
3851
public void setText(WrappedChatComponent value) {
3952
this.handle.getChatComponents().write(0, value);
40-
}
41-
42-
/**
43-
* Retrieves the value of field 'adventure$text'
44-
* ProtocolLib currently does not provide a wrapper for this type. Access to this type is only provided by an InternalStructure
45-
*
46-
* @return 'adventure$text'
47-
*/
48-
public InternalStructure getAdventure$textInternal() {
49-
return this.handle.getStructures().read(1); // TODO: No specific modifier has been found for type interface net.kyori.adventure.text.Component Generic type: interface net.kyori.adventure.text.Component
50-
}
51-
52-
/**
53-
* Sets the value of field 'adventure$text'
54-
* ProtocolLib currently does not provide a wrapper for this type. Access to this type is only provided by an InternalStructure
55-
*
56-
* @param value New value for field 'adventure$text'
57-
*/
58-
public void setAdventure$textInternal(InternalStructure value) {
59-
this.handle.getStructures().write(1, value); // TODO: No specific modifier has been found for type interface net.kyori.adventure.text.Component Generic type: interface net.kyori.adventure.text.Component
60-
}
61-
62-
/**
63-
* Retrieves the value of field 'components'
64-
* ProtocolLib currently does not provide a wrapper for this type. Access to this type is only provided by an InternalStructure
65-
*
66-
* @return 'components'
67-
*/
68-
public InternalStructure getComponentsInternal() {
69-
return this.handle.getStructures().read(2); // TODO: No specific modifier has been found for type class [Lnet.md_5.bungee.api.chat.BaseComponent; Generic type: class [Lnet.md_5.bungee.api.chat.BaseComponent;
70-
}
71-
72-
/**
73-
* Sets the value of field 'components'
74-
* ProtocolLib currently does not provide a wrapper for this type. Access to this type is only provided by an InternalStructure
75-
*
76-
* @param value New value for field 'components'
77-
*/
78-
public void setComponentsInternal(InternalStructure value) {
79-
this.handle.getStructures().write(2, value); // TODO: No specific modifier has been found for type class [Lnet.md_5.bungee.api.chat.BaseComponent; Generic type: class [Lnet.md_5.bungee.api.chat.BaseComponent;
53+
StructureModifier<InternalStructure> structures = this.handle.getStructures();
54+
if(structures.size() > 1) {
55+
for(int i = 1; i < structures.size(); i++) {
56+
structures.write(i, null);
57+
}
58+
}
8059
}
8160

8261
}
Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
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;
56
import com.comphenix.protocol.events.InternalStructure;
67
import com.comphenix.protocol.events.PacketContainer;
8+
import com.comphenix.protocol.reflect.StructureModifier;
9+
import com.comphenix.protocol.wrappers.AdventureComponentConverter;
10+
import com.comphenix.protocol.wrappers.ComponentConverter;
711
import com.comphenix.protocol.wrappers.WrappedChatComponent;
12+
import net.md_5.bungee.api.chat.BaseComponent;
813

914
public class WrapperPlayServerSetTitleText extends AbstractPacket {
1015

@@ -22,61 +27,35 @@ public WrapperPlayServerSetTitleText(PacketContainer packet) {
2227
}
2328

2429
/**
25-
* Retrieves the value of field 'text'
30+
* Retrieves the main title as a chat component
2631
*
2732
* @return 'text'
2833
*/
2934
public WrappedChatComponent getText() {
30-
return this.handle.getChatComponents().read(0);
35+
WrappedChatComponent read = this.handle.getChatComponents().read(0);
36+
if(read != null) {
37+
return read;
38+
}
39+
Object adventure = this.handle.getStructures().read(1).getHandle();
40+
if(adventure != null) {
41+
return ReflectiveAdventureComponentConverter.fromComponent(adventure);
42+
}
43+
BaseComponent[] baseComponents = (BaseComponent[]) this.handle.getStructures().read(2).getHandle();
44+
return ComponentConverter.fromBaseComponent(baseComponents);
3145
}
3246

3347
/**
34-
* Sets the value of field 'text'
48+
* Sets the main title as a chat component
3549
*
3650
* @param value New value for field 'text'
3751
*/
3852
public void setText(WrappedChatComponent value) {
3953
this.handle.getChatComponents().write(0, value);
54+
StructureModifier<InternalStructure> structures = this.handle.getStructures();
55+
if(structures.size() > 1) {
56+
for(int i = 1; i < structures.size(); i++) {
57+
structures.write(i, null);
58+
}
59+
}
4060
}
41-
42-
/**
43-
* Retrieves the value of field 'adventure$text'
44-
* ProtocolLib currently does not provide a wrapper for this type. Access to this type is only provided by an InternalStructure
45-
*
46-
* @return 'adventure$text'
47-
*/
48-
public InternalStructure getAdventure$textInternal() {
49-
return this.handle.getStructures().read(1); // TODO: No specific modifier has been found for type interface net.kyori.adventure.text.Component Generic type: interface net.kyori.adventure.text.Component
50-
}
51-
52-
/**
53-
* Sets the value of field 'adventure$text'
54-
* ProtocolLib currently does not provide a wrapper for this type. Access to this type is only provided by an InternalStructure
55-
*
56-
* @param value New value for field 'adventure$text'
57-
*/
58-
public void setAdventure$textInternal(InternalStructure value) {
59-
this.handle.getStructures().write(1, value); // TODO: No specific modifier has been found for type interface net.kyori.adventure.text.Component Generic type: interface net.kyori.adventure.text.Component
60-
}
61-
62-
/**
63-
* Retrieves the value of field 'components'
64-
* ProtocolLib currently does not provide a wrapper for this type. Access to this type is only provided by an InternalStructure
65-
*
66-
* @return 'components'
67-
*/
68-
public InternalStructure getComponentsInternal() {
69-
return this.handle.getStructures().read(2); // TODO: No specific modifier has been found for type class [Lnet.md_5.bungee.api.chat.BaseComponent; Generic type: class [Lnet.md_5.bungee.api.chat.BaseComponent;
70-
}
71-
72-
/**
73-
* Sets the value of field 'components'
74-
* ProtocolLib currently does not provide a wrapper for this type. Access to this type is only provided by an InternalStructure
75-
*
76-
* @param value New value for field 'components'
77-
*/
78-
public void setComponentsInternal(InternalStructure value) {
79-
this.handle.getStructures().write(2, value); // TODO: No specific modifier has been found for type class [Lnet.md_5.bungee.api.chat.BaseComponent; Generic type: class [Lnet.md_5.bungee.api.chat.BaseComponent;
80-
}
81-
8261
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public WrapperPlayServerSetTitlesAnimation(PacketContainer packet) {
2020
}
2121

2222
/**
23-
* Retrieves the value of field 'fadeIn'
23+
* Retrieves the number of ticks the title is faded in
2424
*
2525
* @return 'fadeIn'
2626
*/
@@ -29,7 +29,7 @@ public int getFadeIn() {
2929
}
3030

3131
/**
32-
* Sets the value of field 'fadeIn'
32+
* Sets the number of ticks the title is faded in
3333
*
3434
* @param value New value for field 'fadeIn'
3535
*/
@@ -38,7 +38,7 @@ public void setFadeIn(int value) {
3838
}
3939

4040
/**
41-
* Retrieves the value of field 'stay'
41+
* Retrieves the number of ticks the title stays on screen
4242
*
4343
* @return 'stay'
4444
*/
@@ -47,7 +47,7 @@ public int getStay() {
4747
}
4848

4949
/**
50-
* Sets the value of field 'stay'
50+
* Sets the number of ticks the title stays on screen
5151
*
5252
* @param value New value for field 'stay'
5353
*/
@@ -56,7 +56,7 @@ public void setStay(int value) {
5656
}
5757

5858
/**
59-
* Retrieves the value of field 'fadeOut'
59+
* Retrieves the number of ticks to fade out the title
6060
*
6161
* @return 'fadeOut'
6262
*/
@@ -65,7 +65,7 @@ public int getFadeOut() {
6565
}
6666

6767
/**
68-
* Sets the value of field 'fadeOut'
68+
* Sets the number of ticks to fade out the title
6969
*
7070
* @param value New value for field 'fadeOut'
7171
*/

0 commit comments

Comments
 (0)