Add modifier for dimensions

This commit is contained in:
Dan Mulloy 2018-09-18 17:01:55 -04:00
parent 7e12f57eda
commit 6fdf0f8255
3 changed files with 69 additions and 4 deletions

View File

@ -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<Integer> getDimensions() {
return structureModifier.withType(
MinecraftReflection.getMinecraftClass("DimensionManager"),
BukkitConverters.getDimensionIDConverter()
);
}
/**
* Retrieve a read/write structure for the Map class.
* @param keyConverter Converter for map keys

View File

@ -1070,4 +1070,51 @@ public class BukkitConverters {
}
});
}
private static MethodAccessor dimensionFromId = null;
private static MethodAccessor idFromDimension = null;
public static EquivalentConverter<Integer> getDimensionIDConverter() {
return new EquivalentConverter<Integer>() {
@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<Integer> getSpecificType() {
return Integer.class;
}
};
}
}

View File

@ -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