ProtocolLib

3M Downloads

getEntityConverter() does not convert between NMS and Bukkit Entites

chrisliebaer opened this issue ยท 2 comments

commented

Not sure what's the idea behind this converter, but according to the Javadoc it should be converting between NMS and Bukkit classes which in this case it doesn't. It converts between an integer and the Bukkit entity, sharing the same id, which is midly confusing, even if intented. I would propse chaning either die documentation or the implementation.

/**
* Retrieve a converter for NMS entities and Bukkit entities.
* @param world - the current world.
* @return A converter between the underlying NMS entity and Bukkit's wrapper.
*/
public static EquivalentConverter<Entity> getEntityConverter(World world) {
final WeakReference<ProtocolManager> managerRef = new WeakReference<>(ProtocolLibrary.getProtocolManager());
return new WorldSpecificConverter<Entity>(world) {
@Override
public Object getGeneric(Entity specific) {
// Simple enough
return specific.getEntityId();
}
@Override
public Entity getSpecific(Object generic) {
try {
Integer id = (Integer) generic;
ProtocolManager manager = managerRef.get();
// Use the entity ID to get a reference to the entity
if (id != null && manager != null) {
return manager.getEntityFromID(world, id);
} else {
return null;
}
} catch (FieldAccessException e) {
throw new RuntimeException("Cannot retrieve entity from ID.", e);
}
}
@Override
public Class<Entity> getSpecificType() {
return Entity.class;
}
};
}

commented

It kind of converts between the two. The use-case for this converter is packet.getEntities().read, which makes sense when being used. I guess I could change the documentation for that converter, but for most people they're probably just looking at the packet container.

commented

I guess most people use ProtocolLib for packets only but I found it very easy do other things with it's helper classes as well. I really got used to the converter classes so I tried to use it :)

I think changing the documentation would be enough. The type error that comes up if you throw in an NMS entity instead of an integer is very confusing.