Add modifier for EntityTypes

Fixes #710
This commit is contained in:
Dan Mulloy 2019-10-29 16:52:30 -04:00
parent 9a108af219
commit a76ceb94cc
3 changed files with 71 additions and 0 deletions

View File

@ -58,6 +58,7 @@ import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.WorldType;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.Vector;
@ -436,6 +437,16 @@ public class PacketContainer implements Serializable {
Preconditions.checkNotNull(event, "event cannot be NULL.");
return getEntityModifier(event.getPlayer().getWorld());
}
/**
* Retrieves a read/write structure for entity types
* @return A modifier for an EntityType.
*/
public StructureModifier<EntityType> getEntityTypeModifier() {
return structureModifier.withType(
MinecraftReflection.getMinecraftClass("EntityTypes"),
BukkitConverters.getEntityTypeConverter());
}
/**
* Retrieves a read/write structure for chunk positions.

View File

@ -49,12 +49,15 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import net.minecraft.server.v1_14_R1.EntityTypes;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.WorldType;
import org.bukkit.advancement.Advancement;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
@ -655,6 +658,54 @@ public class BukkitConverters {
}
};
}
private static MethodAccessor getEntityTypeName;
private static MethodAccessor entityTypeFromName;
public static EquivalentConverter<EntityType> getEntityTypeConverter() {
return ignoreNull(new EquivalentConverter<EntityType>() {
@Override
public Object getGeneric(EntityType specific) {
if (entityTypeFromName == null) {
Class<?> entityTypesClass = MinecraftReflection.getMinecraftClass("EntityTypes");
entityTypeFromName = Accessors.getMethodAccessor(
FuzzyReflection
.fromClass(entityTypesClass, false)
.getMethod(FuzzyMethodContract
.newBuilder()
.returnDerivedOf(Optional.class)
.parameterExactArray(new Class<?>[]{ String.class })
.build()));
}
Optional<?> opt = (Optional<?>) entityTypeFromName.invoke(null, specific.getName());
return opt.orElse(null);
}
@Override
public EntityType getSpecific(Object generic) {
if (getEntityTypeName == null) {
Class<?> entityTypesClass = MinecraftReflection.getMinecraftClass("EntityTypes");
getEntityTypeName = Accessors.getMethodAccessor(
FuzzyReflection
.fromClass(entityTypesClass, false)
.getMethod(FuzzyMethodContract
.newBuilder()
.returnTypeExact(MinecraftReflection.getMinecraftKeyClass())
.parameterExactArray(new Class<?>[]{ entityTypesClass })
.build()));
}
MinecraftKey key = MinecraftKey.fromHandle(getEntityTypeName.invoke(null, generic));
return EntityType.fromName(key.getKey());
}
@Override
public Class<EntityType> getSpecificType() {
return EntityType.class;
}
});
}
/**
* Retrieve the converter used to convert NMS ItemStacks to Bukkit's ItemStack.

View File

@ -46,6 +46,7 @@ import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.WorldType;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffect;
@ -289,6 +290,14 @@ public class PacketContainerTest {
// @Test
// public void testGetPositionModifier() { }
@Test
public void testEntityTypeModifier() {
PacketContainer packet = new PacketContainer(PacketType.Play.Server.SPAWN_ENTITY);
packet.getEntityTypeModifier().write(0, EntityType.ARROW);
assertEquals(packet.getEntityTypeModifier().read(0), EntityType.ARROW);
}
@Test
public void testGetPositionCollectionModifier() {
PacketContainer explosionPacket = new PacketContainer(PacketType.Play.Server.EXPLOSION);