diff --git a/modules/API/src/main/java/com/comphenix/protocol/events/PacketContainer.java b/modules/API/src/main/java/com/comphenix/protocol/events/PacketContainer.java index b7be62e0..e7edf8a5 100644 --- a/modules/API/src/main/java/com/comphenix/protocol/events/PacketContainer.java +++ b/modules/API/src/main/java/com/comphenix/protocol/events/PacketContainer.java @@ -42,8 +42,6 @@ import com.comphenix.protocol.utility.MinecraftReflection; import com.comphenix.protocol.utility.StreamSerializer; import com.comphenix.protocol.wrappers.*; import com.comphenix.protocol.wrappers.EnumWrappers.*; -import com.comphenix.protocol.wrappers.EnumWrappers.Difficulty; -import com.comphenix.protocol.wrappers.EnumWrappers.SoundCategory; import com.comphenix.protocol.wrappers.nbt.NbtBase; import com.comphenix.protocol.wrappers.nbt.NbtCompound; import com.comphenix.protocol.wrappers.nbt.NbtFactory; @@ -55,8 +53,10 @@ import com.google.common.collect.Sets; import io.netty.buffer.ByteBuf; import io.netty.buffer.UnpooledByteBufAllocator; -import org.bukkit.*; -import org.bukkit.Particle; +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.World; +import org.bukkit.WorldType; import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffectType; @@ -936,6 +936,17 @@ public class PacketContainer implements Serializable { MinecraftKey.getConverter()); } + /** + * Retrive a read/write structure for dimension IDs in 1.13.1+ + * @return A modifier for dimension IDs + */ + public StructureModifier getDimensions() { + return structureModifier.withType( + MinecraftReflection.getMinecraftClass("DimensionManager"), + BukkitConverters.getDimensionIDConverter() + ); + } + /** * Retrieve a read/write structure for the Map class. * @param keyConverter Converter for map keys diff --git a/modules/API/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java b/modules/API/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java index 7b341c27..55fe0046 100644 --- a/modules/API/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java +++ b/modules/API/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java @@ -1070,4 +1070,51 @@ public class BukkitConverters { } }); } + + private static MethodAccessor dimensionFromId = null; + private static MethodAccessor idFromDimension = null; + + public static EquivalentConverter getDimensionIDConverter() { + return new EquivalentConverter() { + + @Override + public Object getGeneric(Integer specific) { + if (dimensionFromId == null) { + Class clazz = MinecraftReflection.getMinecraftClass("DimensionManager"); + FuzzyReflection reflection = FuzzyReflection.fromClass(clazz, false); + FuzzyMethodContract contract = FuzzyMethodContract + .newBuilder() + .requireModifier(Modifier.STATIC) + .parameterExactType(int.class) + .returnTypeExact(clazz) + .build(); + dimensionFromId = Accessors.getMethodAccessor(reflection.getMethod(contract)); + } + + return dimensionFromId.invoke(null, (int) specific); + } + + @Override + public Integer getSpecific(Object generic) { + if (idFromDimension == null) { + Class clazz = MinecraftReflection.getMinecraftClass("DimensionManager"); + FuzzyReflection reflection = FuzzyReflection.fromClass(clazz, false); + FuzzyMethodContract contract = FuzzyMethodContract + .newBuilder() + .banModifier(Modifier.STATIC) + .returnTypeExact(int.class) + .parameterCount(0) + .build(); + idFromDimension = Accessors.getMethodAccessor(reflection.getMethod(contract)); + } + + return (Integer) idFromDimension.invoke(generic); + } + + @Override + public Class getSpecificType() { + return Integer.class; + } + }; + } } diff --git a/modules/ProtocolLib/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java b/modules/ProtocolLib/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java index c1047439..00bd2db9 100644 --- a/modules/ProtocolLib/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java +++ b/modules/ProtocolLib/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java @@ -467,6 +467,13 @@ public class PacketContainerTest { assertEquals(container.getEnumModifier(Action.class, PacketPlayOutBoss.Action.class).read(0), Action.UPDATE_PCT); } + @Test + public void testDimensionManager() { + PacketContainer container = new PacketContainer(PacketType.Play.Server.RESPAWN); + container.getDimensions().write(0, 1); + assertEquals((Object) 1, container.getDimensions().read(0)); + } + /** * Actions from the outbound Boss packet. Used for testing generic enums. * @author dmulloy2