Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. public class CustomEntityType<T extends EntityLiving> {
  2.  
  3. @Nullable
  4. private static Field REGISTRY_MAT_MAP;
  5.  
  6. static {
  7. try {
  8. REGISTRY_MAT_MAP = RegistryMaterials.class.getDeclaredField("c");
  9. } catch (ReflectiveOperationException e) {
  10. e.printStackTrace();
  11. REGISTRY_MAT_MAP = null;
  12. // technically should only occur if server version changes or jar is modified in "weird" ways
  13. }
  14. }
  15.  
  16. private final MinecraftKey key;
  17. private final Class<T> clazz;
  18. private final EntityTypes.b<T> maker;
  19. private EntityTypes<? super T> parentType;
  20. private EntityTypes<T> entityType;
  21. private boolean registered;
  22.  
  23. public CustomEntityType(String name, Class<T> customEntityClass, EntityTypes<? super T> parentType, EntityTypes.b<T> maker) {
  24. this.key = MinecraftKey.a(name); // returns null if 256+ chars, non-alphanumeric, or contains uppercase chars
  25. this.clazz = customEntityClass;
  26. this.parentType = parentType;
  27. this.maker = maker;
  28. }
  29.  
  30. public org.bukkit.entity.Entity spawn(Location loc) {
  31. // Parameters: nmsWorld, initialNBT, customName, spawningPlayer, blockPos, spawnReason, fixPos, flag1(?)
  32. Entity entity = entityType.spawnCreature(((CraftWorld)loc.getWorld()).getHandle(),
  33. null, null, null, new BlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()),
  34. EnumMobSpawn.EVENT, true, false);
  35. return entity == null ? null : entity.getBukkitEntity();
  36. }
  37.  
  38. public void register() throws IllegalStateException {
  39. if (registered || IRegistry.ENTITY_TYPE.getOptional(key).isPresent()) {
  40. throw new IllegalStateException(String.format
  41. ("Unable to register entity with key '%s' as it is already registered.", key));
  42. }
  43. // Add custom entity to data fixers map with parent entity's data fixer
  44. Map<Object, Type<?>> dataTypes = (Map<Object, Type<?>>)DataConverterRegistry.a()
  45. .getSchema(DataFixUtils.makeKey(SharedConstants.getGameVersion().getWorldVersion()))
  46. .findChoiceType(DataConverterTypes.ENTITY_TREE).types(); // DataConverterTypes.ENTITY in < 1.15.2
  47. dataTypes.put(key.toString(), dataTypes.get(parentType.h().toString().replace("entity/", "")));
  48. // Add our custom entity to the entity registry map
  49. EntityTypes.a<T> a = EntityTypes.a.a(maker, EnumCreatureType.CREATURE);
  50. entityType = a.a(key.getKey());
  51. IRegistry.a(IRegistry.ENTITY_TYPE, key.getKey(), entityType);
  52. registered = true;
  53. }
  54.  
  55. @SuppressWarnings("unchecked")
  56. public void unregister() throws IllegalStateException {
  57. if (!registered) {
  58. throw new IllegalArgumentException(String.format
  59. ("Entity with key '%s' could not be unregistered, as it is not in the registry", key));
  60. }
  61. // Remove custom entity from data fixers map
  62. Map<Object, Type<?>> dataTypes = (Map<Object, Type<?>>)DataConverterRegistry.a()
  63. .getSchema(DataFixUtils.makeKey(SharedConstants.getGameVersion().getWorldVersion()))
  64. .findChoiceType(DataConverterTypes.ENTITY_TREE).types();
  65. dataTypes.remove(key.toString());
  66. try {
  67. // Remove our custom entity from entity registry map, which takes a little reflection in 1.15
  68. if (REGISTRY_MAT_MAP == null) {
  69. throw new ReflectiveOperationException("Field not initially found");
  70. }
  71. REGISTRY_MAT_MAP.setAccessible(true);
  72. Object o = REGISTRY_MAT_MAP.get(IRegistry.ENTITY_TYPE);
  73. ((BiMap<MinecraftKey, ?>)o).remove(key);
  74. REGISTRY_MAT_MAP.set(IRegistry.ENTITY_TYPE, o);
  75. REGISTRY_MAT_MAP.setAccessible(false);
  76. registered = false;
  77. } catch (ReflectiveOperationException e) {
  78. e.printStackTrace();
  79. }
  80. }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement