MySoulIsCry

Untitled

May 21st, 2022
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. import net.citizensnpcs.api.CitizensAPI;
  2. import net.citizensnpcs.api.ai.Navigator;
  3. import net.citizensnpcs.api.npc.NPC;
  4. import net.citizensnpcs.trait.LookClose;
  5. import net.citizensnpcs.trait.ScoreboardTrait;
  6. import net.citizensnpcs.trait.SkinTrait;
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.ChatColor;
  9. import org.bukkit.Location;
  10. import org.bukkit.entity.*;
  11. import org.bukkit.event.EventHandler;
  12. import org.bukkit.event.Listener;
  13. import org.bukkit.event.entity.EntityDeathEvent;
  14. import org.bukkit.event.player.PlayerInteractAtEntityEvent;
  15. import org.bukkit.plugin.Plugin;
  16.  
  17. import java.util.HashSet;
  18. import java.util.Set;
  19. import java.util.function.Consumer;
  20.  
  21. public class CustomNPC {
  22.  
  23. public static final Set<CustomNPC> ALL_CUSTOM_NPC = new HashSet<>();
  24.  
  25. private final Location spawned;
  26. private final NPC npc;
  27. private ArmorStand sitStand;
  28. private Consumer<PlayerInteractAtEntityEvent> onInteract;
  29. private Consumer<EntityDeathEvent> onDeath;
  30. private static boolean isListenersSet = false;
  31.  
  32. //nickname - ник который будет отображатся над головой
  33. //location место спавна
  34. public CustomNPC(Location location, String nickname) {
  35. this.npc = CitizensAPI.getNPCRegistry().createNPC(EntityType.PLAYER, nickname);
  36. this.spawned = location;
  37.  
  38. npc.data().set("player-skin-use-latest-skin", false);
  39.  
  40. ALL_CUSTOM_NPC.add(this);
  41. }
  42.  
  43. //Заставить NPC следить за игроком
  44. public CustomNPC lookClose(boolean look) {
  45. npc.getOrAddTrait(LookClose.class).lookClose(look);
  46. return this;
  47. }
  48. //Заспавнить NPC в мире
  49. public CustomNPC spawn() {
  50. npc.spawn(spawned);
  51. return this;
  52. }
  53. //Установить скин по нику
  54. //ВАЖНО устанавливаются только те скины что есть на официальных аккаунтах
  55. //+ если ты играешь с пиратки то у тебя он может не отображатся или отображатся не коректно
  56. public CustomNPC skin(String name) {
  57. SkinTrait skinTrait = npc.getTrait(SkinTrait.class);
  58. skinTrait.setSkinName(name);
  59. npc.addTrait(skinTrait);
  60. return this;
  61. }
  62. //Заставить NPC сесть на той локации на которой он нахоится
  63. public CustomNPC sit(boolean sit) {
  64. if (sit) {
  65. if (getEntity() == null) return this;
  66. if (sitStand != null) sitStand.remove();
  67. sitStand = (ArmorStand) npc.getEntity().getWorld().spawnEntity(npc.getEntity().getLocation().clone().subtract(0, 1, 0), EntityType.ARMOR_STAND);
  68. sitStand.setGravity(false);
  69. sitStand.setRemoveWhenFarAway(false);
  70. sitStand.setVisible(false);
  71. sitStand.addPassenger(npc.getEntity());
  72.  
  73. } else {
  74. if (sitStand != null) {
  75. sitStand.getPassengers().remove(0);
  76. sitStand.remove();
  77. }
  78. }
  79. return this;
  80. }
  81. //Заставить NPC светится
  82. public CustomNPC glow(boolean glow) {
  83. npc.data().setPersistent("glowing", glow);
  84. return this;
  85. }
  86. //Установить цвет свечения
  87. public CustomNPC glowColor(ChatColor color) {
  88. npc.getOrAddTrait(ScoreboardTrait.class).setColor(color);
  89. return this;
  90. }
  91. //Получить Сущьность NPC
  92. public LivingEntity getEntity() {
  93. return (LivingEntity) npc.getEntity();
  94. }
  95. //Получить Navigator NPC
  96. /*
  97. Примеры использования:
  98. npc.getNavigator().setTarget(Entity, true); - заставить NPC бегать за Entity и атаковать его true false - атаковать или нет
  99. npc.getNavigator().setTarget(Location); - просто заставить NPC бежать на локацию
  100.  
  101. */
  102. public Navigator getNavigator() {
  103. return npc.getNavigator();
  104. }
  105. //Создать клона NPC
  106. public static CustomNPC of(Location spawn, String nickname) {
  107. return new CustomNPC(spawn, nickname);
  108. }
  109. //Поставить обработчик нажатий на NPC
  110. /*
  111. Пример
  112. npc.onInteractListener((event)->{
  113. event.getPlayer();
  114. });
  115. */
  116. public CustomNPC onInteractListener(Consumer<PlayerInteractAtEntityEvent> e) {
  117. onInteract = e;
  118. return this;
  119. }
  120. //Тоже самое что и и onInteractListener только ивент смерти
  121. public CustomNPC onDeathListener(Consumer<EntityDeathEvent> e) {
  122. onDeath = e;
  123. return this;
  124. }
  125. private static void onNPCInteract(PlayerInteractAtEntityEvent e) {
  126. if (!CitizensAPI.getNPCRegistry().isNPC(e.getRightClicked())) return;
  127. for (CustomNPC npc : ALL_CUSTOM_NPC) {
  128. if (npc.getEntity() == null) continue;
  129. if (npc.getEntity() == e.getRightClicked()) {
  130. if (npc.onInteract == null) return;
  131. npc.onInteract.accept(e);
  132. }
  133. }
  134. }
  135. private static void onNPCDeath(EntityDeathEvent e) {
  136. if (!CitizensAPI.getNPCRegistry().isNPC(e.getEntity())) return;
  137. for (CustomNPC npc : ALL_CUSTOM_NPC) {
  138. if (npc.getEntity() == null) continue;
  139. if (npc.getEntity() == e.getEntity()) {
  140. if (npc.onDeath == null) return;
  141. npc.onDeath.accept(e);
  142. }
  143. }
  144. }
  145. //Этот статический метод нужно вызвать чтобы работали обработчики у NPC
  146. public static void setListeners(Plugin plugin) {
  147. if (isListenersSet) return;
  148. Bukkit.getPluginManager().registerEvents(new Listener() {
  149. @EventHandler
  150. public void onInteract(PlayerInteractAtEntityEvent e) {
  151. onNPCInteract(e);
  152. }
  153.  
  154. @EventHandler
  155. public void onDeath(EntityDeathEvent e) {
  156. onNPCDeath(e);
  157. }
  158.  
  159. }, plugin);
  160. }
  161. //Этот статический метод нужно вызвать при onDisable чтобы все NPC которых ты создал удалились и не сохранялись в память Citizens
  162. public static void disable() {
  163. ALL_CUSTOM_NPC.forEach(n -> n.npc.destroy());
  164. }
  165.  
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment