Skip to content

Commit

Permalink
Add several scoreboard type wrappers (#2819)
Browse files Browse the repository at this point in the history
  • Loading branch information
vytskalt authored and dmulloy2 committed Jun 7, 2024
1 parent 77ada09 commit 616431c
Show file tree
Hide file tree
Showing 13 changed files with 877 additions and 23 deletions.
49 changes: 49 additions & 0 deletions src/main/java/com/comphenix/protocol/events/AbstractStructure.java
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,55 @@ public StructureModifier<EnumWrappers.ChatType> getChatTypes() {
EnumWrappers.getChatTypeConverter());
}

/**
* Retrieve a read/write structure for the DisplaySlot enum in 1.20.2.
* @return A modifier for DisplaySlot enum fields.
*/
public StructureModifier<EnumWrappers.DisplaySlot> getDisplaySlots() {
return structureModifier.withType(
EnumWrappers.getDisplaySlotClass(),
EnumWrappers.getDisplaySlotConverter());
}

/**
* Retrieve a read/write structure for the RenderType enum.
* @return A modifier for RenderType enum fields.
*/
public StructureModifier<EnumWrappers.RenderType> getRenderTypes() {
return structureModifier.withType(
EnumWrappers.getRenderTypeClass(),
EnumWrappers.getRenderTypeConverter());
}

/**
* Retrieve a read/write structure for the ChatFormatting enum.
* @return A modifier for ChatFormatting enum fields.
*/
public StructureModifier<EnumWrappers.ChatFormatting> getChatFormattings() {
return structureModifier.withType(
EnumWrappers.getChatFormattingClass(),
EnumWrappers.getChatFormattingConverter());
}

/**
* Retrieve a read/write structure for optional team parameters in 1.17+.
* @return A modifier for optional team parameters fields.
*/
public StructureModifier<Optional<WrappedTeamParameters>> getOptionalTeamParameters() {
return getOptionals(BukkitConverters.getWrappedTeamParametersConverter());
}

/**
* Retrieve a read/write structure for the NumberFormat class in 1.20.4+.
* @return A modifier for NumberFormat fields.
*/
public StructureModifier<WrappedNumberFormat> getNumberFormats() {
return structureModifier.withType(
MinecraftReflection.getNumberFormatClass().orElse(null),
BukkitConverters.getWrappedNumberFormatConverter());
}


/**
* Retrieve a read/write structure for the MinecraftKey class.
* @return A modifier for MinecraftKey fields.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,25 @@

package com.comphenix.protocol.events;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import com.comphenix.protocol.reflect.accessors.Accessors;
import com.comphenix.protocol.reflect.accessors.MethodAccessor;
import com.comphenix.protocol.utility.ByteBuddyFactory;
import com.comphenix.protocol.utility.Util;

import net.bytebuddy.description.ByteCodeElement;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
Expand All @@ -35,27 +50,19 @@
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;
import org.bukkit.*;
import org.bukkit.BanEntry;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.Statistic;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.profile.PlayerProfile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
* Represents a player object that can be serialized by Java.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@ public class StructureCache {

public static Object newPacket(Class<?> packetClass) {
Supplier<Object> packetConstructor = PACKET_INSTANCE_CREATORS.computeIfAbsent(packetClass, packetClassKey -> {
PacketCreator creator = PacketCreator.forPacket(packetClassKey);
if (creator.get() != null) {
return creator;
try {
PacketCreator creator = PacketCreator.forPacket(packetClassKey);
if (creator.get() != null) {
return creator;
}
} catch (Exception ignored) {
}

WrappedStreamCodec streamCodec = PacketRegistry.getStreamCodec(packetClassKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,16 @@ public static Class<?> getChatSerializerClass() {
return getMinecraftClass("network.chat.IChatBaseComponent$ChatSerializer", "network.chat.Component$Serializer", "IChatBaseComponent$ChatSerializer");
}

/**
* Retrieve the component style serializer class.
*
* @return The serializer class.
*/
public static Class<?> getStyleSerializerClass() {
return getMinecraftClass("network.chat.ChatModifier$ChatModifierSerializer", "ChatModifier$ChatModifierSerializer");
}


/**
* Retrieve the ServerPing class.
*
Expand Down Expand Up @@ -1020,6 +1030,60 @@ public static Class<?> getTileEntityClass() {
return getMinecraftClass("world.level.block.entity.TileEntity", "world.level.block.entity.BlockEntity", "TileEntity");
}

/**
* Retrieve the NMS team parameters class.
*
* @return The team parameters class.
*/
public static Optional<Class<?>> getTeamParametersClass() {
return getOptionalNMS("network.protocol.game.PacketPlayOutScoreboardTeam$b");
}

/**
* Retrieve the NMS component style class.
*
* @return The component style class.
*/
public static Class<?> getComponentStyleClass() {
return getMinecraftClass("network.chat.ChatModifier", "ChatModifier");
}

/**
* Retrieve the NMS NumberFormat class.
*
* @return The NumberFormat class.
*/
public static Optional<Class<?>> getNumberFormatClass() {
return getOptionalNMS("network.chat.numbers.NumberFormat");
}

/**
* Retrieve the NMS BlankFormat class.
*
* @return The FixedFormat class.
*/
public static Optional<Class<?>> getBlankFormatClass() {
return getOptionalNMS("network.chat.numbers.BlankFormat");
}

/**
* Retrieve the NMS FixedFormat class.
*
* @return The FixedFormat class.
*/
public static Optional<Class<?>> getFixedFormatClass() {
return getOptionalNMS("network.chat.numbers.FixedFormat");
}

/**
* Retrieve the NMS StyledFormat class.
*
* @return The StyledFormat class.
*/
public static Optional<Class<?>> getStyledFormatClass() {
return getOptionalNMS("network.chat.numbers.StyledFormat");
}

/**
* Retrieve the Gson class used by Minecraft.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
*/
package com.comphenix.protocol.wrappers;

import com.comphenix.protocol.utility.MinecraftVersion;
import com.google.gson.JsonObject;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;

/**
Expand All @@ -25,7 +28,16 @@
* Note: The Adventure API Component is not included in CraftBukkit, Bukkit or Spigot and but is present in PaperMC.
*/
public class AdventureComponentConverter {

private static final GsonComponentSerializer SERIALIZER;

static {
if (MinecraftVersion.NETHER_UPDATE.atOrAbove()) {
SERIALIZER = GsonComponentSerializer.gson();
} else {
SERIALIZER = GsonComponentSerializer.colorDownsamplingGson();
}
}

private AdventureComponentConverter() {
}

Expand All @@ -35,7 +47,7 @@ private AdventureComponentConverter() {
* @return Component
*/
public static Component fromWrapper(WrappedChatComponent wrapper) {
return GsonComponentSerializer.gson().deserialize(wrapper.getJson());
return SERIALIZER.deserialize(wrapper.getJson());
}

/**
Expand Down Expand Up @@ -71,11 +83,29 @@ public static Object fromJsonAsObject(final String json) {
* @return ProtocolLib wrapper
*/
public static WrappedChatComponent fromComponent(Component component) {
return WrappedChatComponent.fromJson(GsonComponentSerializer.gson().serialize(component));
return WrappedChatComponent.fromJson(SERIALIZER.serialize(component));
}

/**
* Converts a {@link WrappedComponentStyle} into a {@link Style}
* @param wrapper ProtocolLib wrapper
* @return Style
*/
public static Style fromWrapper(WrappedComponentStyle wrapper) {
return SERIALIZER.serializer().fromJson(wrapper.getJson(), Style.class);
}

/**
* Converts a {@link Style} into a ProtocolLib wrapper
* @param style Style
* @return ProtocolLib wrapper
*/
public static WrappedComponentStyle fromStyle(Style style) {
return WrappedComponentStyle.fromJson((JsonObject) SERIALIZER.serializer().toJsonTree(style));
}

public static Class<?> getComponentClass() {
return Component.class;
return Component.class;
}

public static Component clone(Object component) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,14 @@ public static EquivalentConverter<WrappedLevelChunkData.LightData> getWrappedLig
return ignoreNull(handle(WrappedLevelChunkData.LightData::getHandle, WrappedLevelChunkData.LightData::new, WrappedLevelChunkData.LightData.class));
}

public static EquivalentConverter<WrappedTeamParameters> getWrappedTeamParametersConverter() {
return ignoreNull(handle(WrappedTeamParameters::getHandle, WrappedTeamParameters::new, WrappedTeamParameters.class));
}

public static EquivalentConverter<WrappedNumberFormat> getWrappedNumberFormatConverter() {
return ignoreNull(handle(WrappedNumberFormat::getHandle, WrappedNumberFormat::fromHandle, WrappedNumberFormat.class));
}

public static EquivalentConverter<PacketContainer> getPacketContainerConverter() {
return ignoreNull(handle(PacketContainer::getHandle, PacketContainer::fromPacket, PacketContainer.class));
}
Expand Down
Loading

0 comments on commit 616431c

Please sign in to comment.