Skip to content

Commit 616431c

Browse files
vytskaltdmulloy2
authored andcommitted
Add several scoreboard type wrappers (#2819)
1 parent 77ada09 commit 616431c

13 files changed

+877
-23
lines changed

src/main/java/com/comphenix/protocol/events/AbstractStructure.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,55 @@ public StructureModifier<EnumWrappers.ChatType> getChatTypes() {
870870
EnumWrappers.getChatTypeConverter());
871871
}
872872

873+
/**
874+
* Retrieve a read/write structure for the DisplaySlot enum in 1.20.2.
875+
* @return A modifier for DisplaySlot enum fields.
876+
*/
877+
public StructureModifier<EnumWrappers.DisplaySlot> getDisplaySlots() {
878+
return structureModifier.withType(
879+
EnumWrappers.getDisplaySlotClass(),
880+
EnumWrappers.getDisplaySlotConverter());
881+
}
882+
883+
/**
884+
* Retrieve a read/write structure for the RenderType enum.
885+
* @return A modifier for RenderType enum fields.
886+
*/
887+
public StructureModifier<EnumWrappers.RenderType> getRenderTypes() {
888+
return structureModifier.withType(
889+
EnumWrappers.getRenderTypeClass(),
890+
EnumWrappers.getRenderTypeConverter());
891+
}
892+
893+
/**
894+
* Retrieve a read/write structure for the ChatFormatting enum.
895+
* @return A modifier for ChatFormatting enum fields.
896+
*/
897+
public StructureModifier<EnumWrappers.ChatFormatting> getChatFormattings() {
898+
return structureModifier.withType(
899+
EnumWrappers.getChatFormattingClass(),
900+
EnumWrappers.getChatFormattingConverter());
901+
}
902+
903+
/**
904+
* Retrieve a read/write structure for optional team parameters in 1.17+.
905+
* @return A modifier for optional team parameters fields.
906+
*/
907+
public StructureModifier<Optional<WrappedTeamParameters>> getOptionalTeamParameters() {
908+
return getOptionals(BukkitConverters.getWrappedTeamParametersConverter());
909+
}
910+
911+
/**
912+
* Retrieve a read/write structure for the NumberFormat class in 1.20.4+.
913+
* @return A modifier for NumberFormat fields.
914+
*/
915+
public StructureModifier<WrappedNumberFormat> getNumberFormats() {
916+
return structureModifier.withType(
917+
MinecraftReflection.getNumberFormatClass().orElse(null),
918+
BukkitConverters.getWrappedNumberFormatConverter());
919+
}
920+
921+
873922
/**
874923
* Retrieve a read/write structure for the MinecraftKey class.
875924
* @return A modifier for MinecraftKey fields.

src/main/java/com/comphenix/protocol/events/SerializedOfflinePlayer.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,25 @@
1717

1818
package com.comphenix.protocol.events;
1919

20+
import java.io.IOException;
21+
import java.io.ObjectInputStream;
22+
import java.io.ObjectOutputStream;
23+
import java.io.Serializable;
24+
import java.lang.reflect.Constructor;
25+
import java.lang.reflect.InvocationTargetException;
26+
import java.lang.reflect.Method;
27+
import java.time.Duration;
28+
import java.time.Instant;
29+
import java.util.Date;
30+
import java.util.Map;
31+
import java.util.UUID;
32+
import java.util.concurrent.ConcurrentHashMap;
33+
2034
import com.comphenix.protocol.reflect.accessors.Accessors;
2135
import com.comphenix.protocol.reflect.accessors.MethodAccessor;
2236
import com.comphenix.protocol.utility.ByteBuddyFactory;
2337
import com.comphenix.protocol.utility.Util;
38+
2439
import net.bytebuddy.description.ByteCodeElement;
2540
import net.bytebuddy.description.modifier.Visibility;
2641
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
@@ -35,27 +50,19 @@
3550
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
3651
import net.bytebuddy.matcher.ElementMatcher;
3752
import net.bytebuddy.matcher.ElementMatchers;
38-
import org.bukkit.*;
53+
import org.bukkit.BanEntry;
54+
import org.bukkit.Bukkit;
55+
import org.bukkit.Location;
56+
import org.bukkit.Material;
57+
import org.bukkit.OfflinePlayer;
58+
import org.bukkit.Statistic;
59+
import org.bukkit.World;
3960
import org.bukkit.entity.EntityType;
4061
import org.bukkit.entity.Player;
4162
import org.bukkit.profile.PlayerProfile;
4263
import org.jetbrains.annotations.NotNull;
4364
import org.jetbrains.annotations.Nullable;
4465

45-
import java.io.IOException;
46-
import java.io.ObjectInputStream;
47-
import java.io.ObjectOutputStream;
48-
import java.io.Serializable;
49-
import java.lang.reflect.Constructor;
50-
import java.lang.reflect.InvocationTargetException;
51-
import java.lang.reflect.Method;
52-
import java.time.Duration;
53-
import java.time.Instant;
54-
import java.util.Date;
55-
import java.util.Map;
56-
import java.util.UUID;
57-
import java.util.concurrent.ConcurrentHashMap;
58-
5966
/**
6067
* Represents a player object that can be serialized by Java.
6168
*

src/main/java/com/comphenix/protocol/injector/StructureCache.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,12 @@ public class StructureCache {
6969

7070
public static Object newPacket(Class<?> packetClass) {
7171
Supplier<Object> packetConstructor = PACKET_INSTANCE_CREATORS.computeIfAbsent(packetClass, packetClassKey -> {
72-
PacketCreator creator = PacketCreator.forPacket(packetClassKey);
73-
if (creator.get() != null) {
74-
return creator;
72+
try {
73+
PacketCreator creator = PacketCreator.forPacket(packetClassKey);
74+
if (creator.get() != null) {
75+
return creator;
76+
}
77+
} catch (Exception ignored) {
7578
}
7679

7780
WrappedStreamCodec streamCodec = PacketRegistry.getStreamCodec(packetClassKey);

src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,16 @@ public static Class<?> getChatSerializerClass() {
679679
return getMinecraftClass("network.chat.IChatBaseComponent$ChatSerializer", "network.chat.Component$Serializer", "IChatBaseComponent$ChatSerializer");
680680
}
681681

682+
/**
683+
* Retrieve the component style serializer class.
684+
*
685+
* @return The serializer class.
686+
*/
687+
public static Class<?> getStyleSerializerClass() {
688+
return getMinecraftClass("network.chat.ChatModifier$ChatModifierSerializer", "ChatModifier$ChatModifierSerializer");
689+
}
690+
691+
682692
/**
683693
* Retrieve the ServerPing class.
684694
*
@@ -1020,6 +1030,60 @@ public static Class<?> getTileEntityClass() {
10201030
return getMinecraftClass("world.level.block.entity.TileEntity", "world.level.block.entity.BlockEntity", "TileEntity");
10211031
}
10221032

1033+
/**
1034+
* Retrieve the NMS team parameters class.
1035+
*
1036+
* @return The team parameters class.
1037+
*/
1038+
public static Optional<Class<?>> getTeamParametersClass() {
1039+
return getOptionalNMS("network.protocol.game.PacketPlayOutScoreboardTeam$b");
1040+
}
1041+
1042+
/**
1043+
* Retrieve the NMS component style class.
1044+
*
1045+
* @return The component style class.
1046+
*/
1047+
public static Class<?> getComponentStyleClass() {
1048+
return getMinecraftClass("network.chat.ChatModifier", "ChatModifier");
1049+
}
1050+
1051+
/**
1052+
* Retrieve the NMS NumberFormat class.
1053+
*
1054+
* @return The NumberFormat class.
1055+
*/
1056+
public static Optional<Class<?>> getNumberFormatClass() {
1057+
return getOptionalNMS("network.chat.numbers.NumberFormat");
1058+
}
1059+
1060+
/**
1061+
* Retrieve the NMS BlankFormat class.
1062+
*
1063+
* @return The FixedFormat class.
1064+
*/
1065+
public static Optional<Class<?>> getBlankFormatClass() {
1066+
return getOptionalNMS("network.chat.numbers.BlankFormat");
1067+
}
1068+
1069+
/**
1070+
* Retrieve the NMS FixedFormat class.
1071+
*
1072+
* @return The FixedFormat class.
1073+
*/
1074+
public static Optional<Class<?>> getFixedFormatClass() {
1075+
return getOptionalNMS("network.chat.numbers.FixedFormat");
1076+
}
1077+
1078+
/**
1079+
* Retrieve the NMS StyledFormat class.
1080+
*
1081+
* @return The StyledFormat class.
1082+
*/
1083+
public static Optional<Class<?>> getStyledFormatClass() {
1084+
return getOptionalNMS("network.chat.numbers.StyledFormat");
1085+
}
1086+
10231087
/**
10241088
* Retrieve the Gson class used by Minecraft.
10251089
*

src/main/java/com/comphenix/protocol/wrappers/AdventureComponentConverter.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
*/
1717
package com.comphenix.protocol.wrappers;
1818

19+
import com.comphenix.protocol.utility.MinecraftVersion;
20+
import com.google.gson.JsonObject;
1921
import net.kyori.adventure.text.Component;
22+
import net.kyori.adventure.text.format.Style;
2023
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
2124

2225
/**
@@ -25,7 +28,16 @@
2528
* Note: The Adventure API Component is not included in CraftBukkit, Bukkit or Spigot and but is present in PaperMC.
2629
*/
2730
public class AdventureComponentConverter {
28-
31+
private static final GsonComponentSerializer SERIALIZER;
32+
33+
static {
34+
if (MinecraftVersion.NETHER_UPDATE.atOrAbove()) {
35+
SERIALIZER = GsonComponentSerializer.gson();
36+
} else {
37+
SERIALIZER = GsonComponentSerializer.colorDownsamplingGson();
38+
}
39+
}
40+
2941
private AdventureComponentConverter() {
3042
}
3143

@@ -35,7 +47,7 @@ private AdventureComponentConverter() {
3547
* @return Component
3648
*/
3749
public static Component fromWrapper(WrappedChatComponent wrapper) {
38-
return GsonComponentSerializer.gson().deserialize(wrapper.getJson());
50+
return SERIALIZER.deserialize(wrapper.getJson());
3951
}
4052

4153
/**
@@ -71,11 +83,29 @@ public static Object fromJsonAsObject(final String json) {
7183
* @return ProtocolLib wrapper
7284
*/
7385
public static WrappedChatComponent fromComponent(Component component) {
74-
return WrappedChatComponent.fromJson(GsonComponentSerializer.gson().serialize(component));
86+
return WrappedChatComponent.fromJson(SERIALIZER.serialize(component));
87+
}
88+
89+
/**
90+
* Converts a {@link WrappedComponentStyle} into a {@link Style}
91+
* @param wrapper ProtocolLib wrapper
92+
* @return Style
93+
*/
94+
public static Style fromWrapper(WrappedComponentStyle wrapper) {
95+
return SERIALIZER.serializer().fromJson(wrapper.getJson(), Style.class);
96+
}
97+
98+
/**
99+
* Converts a {@link Style} into a ProtocolLib wrapper
100+
* @param style Style
101+
* @return ProtocolLib wrapper
102+
*/
103+
public static WrappedComponentStyle fromStyle(Style style) {
104+
return WrappedComponentStyle.fromJson((JsonObject) SERIALIZER.serializer().toJsonTree(style));
75105
}
76106

77107
public static Class<?> getComponentClass() {
78-
return Component.class;
108+
return Component.class;
79109
}
80110

81111
public static Component clone(Object component) {

src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,14 @@ public static EquivalentConverter<WrappedLevelChunkData.LightData> getWrappedLig
640640
return ignoreNull(handle(WrappedLevelChunkData.LightData::getHandle, WrappedLevelChunkData.LightData::new, WrappedLevelChunkData.LightData.class));
641641
}
642642

643+
public static EquivalentConverter<WrappedTeamParameters> getWrappedTeamParametersConverter() {
644+
return ignoreNull(handle(WrappedTeamParameters::getHandle, WrappedTeamParameters::new, WrappedTeamParameters.class));
645+
}
646+
647+
public static EquivalentConverter<WrappedNumberFormat> getWrappedNumberFormatConverter() {
648+
return ignoreNull(handle(WrappedNumberFormat::getHandle, WrappedNumberFormat::fromHandle, WrappedNumberFormat.class));
649+
}
650+
643651
public static EquivalentConverter<PacketContainer> getPacketContainerConverter() {
644652
return ignoreNull(handle(PacketContainer::getHandle, PacketContainer::fromPacket, PacketContainer.class));
645653
}

0 commit comments

Comments
 (0)