PageRenderTime 149ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Bukkit/Commons/src/meteor/util/entity/utils/UtilEntity.java

https://gitlab.com/Meteor-MC/plugins
Java | 295 lines | 250 code | 42 blank | 3 comment | 22 complexity | 8a43ab1f4b47b6beea0dae7d3f2aa830 MD5 | raw file
  1. package meteor.util.entity.utils;// Written by Creeplays on 17.06.2016.
  2. import com.google.common.collect.Sets;
  3. import meteor.cosmetics.cosmetics.pets.Pet;
  4. import meteor.cosmetics.cosmetics.pets.PlayerFollower;
  5. import meteor.cosmetics.cosmetics.treasurechests.ChestType;
  6. import meteor.cosmetics.cosmetics.treasurechests.TreasureChestDesign;
  7. import meteor.cosmetics.util.PacketSender;
  8. import meteor.cosmetics.util.Particles;
  9. import meteor.cosmetics.util.UtilParticles;
  10. import meteor.util.entity.metadata.MetadataKey;
  11. import meteor.util.entity.metadata.MetadataType;
  12. import meteor.util.entity.metadata.MyDataWatcher;
  13. import meteor.util.entity.patchfinders.CustomPathFinderGoalPanic;
  14. import meteor.util.utils.UtilCore;
  15. import meteor.util.utils.UtilMath;
  16. import meteor.util.utils.UtilRandom;
  17. import net.minecraft.server.v1_10_R1.*;
  18. import org.bukkit.Bukkit;
  19. import org.bukkit.Location;
  20. import org.bukkit.craftbukkit.v1_10_R1.CraftWorld;
  21. import org.bukkit.craftbukkit.v1_10_R1.entity.*;
  22. import org.bukkit.craftbukkit.v1_10_R1.inventory.CraftInventory;
  23. import org.bukkit.craftbukkit.v1_10_R1.inventory.CraftItemStack;
  24. import org.bukkit.entity.Creature;
  25. import org.bukkit.entity.Horse;
  26. import org.bukkit.entity.Player;
  27. import org.bukkit.entity.Wither;
  28. import org.bukkit.inventory.Inventory;
  29. import org.bukkit.plugin.Plugin;
  30. import org.bukkit.util.Vector;
  31. import java.lang.reflect.Field;
  32. import java.util.*;
  33. public class UtilEntity {
  34. private static Map<Player, List<EntityArmorStand>> fakeArmorStandsMap = new HashMap<>();
  35. private static Map<Player, List<org.bukkit.entity.Entity>> cooldownJumpMap = new HashMap<>();
  36. public static void setData(org.bukkit.entity.Entity entity, Integer index, Object value, MetadataType type){
  37. new MyDataWatcher(entity).set(new MetadataKey<>(index,type),value);
  38. }
  39. public static Object getData(org.bukkit.entity.Entity e, Integer index, MetadataType metadataType){
  40. return new MyDataWatcher(e).get(new MetadataKey<>(index,metadataType));
  41. }
  42. public static PlayerFollower newPlayerFollower(Pet pet, Player player) {
  43. return new PlayerFollower(pet,player);
  44. }
  45. public static net.minecraft.server.v1_10_R1.Entity getHandle(org.bukkit.entity.Entity entity){
  46. return ((CraftEntity)entity).getHandle();
  47. }
  48. public static DataWatcher getDataWatcher(net.minecraft.server.v1_10_R1.Entity entity){
  49. return new DataWatcher(entity);
  50. }
  51. public static DataWatcher getDataWatcher(org.bukkit.entity.Entity entity){
  52. return getDataWatcher(getHandle(entity));
  53. }
  54. public static void setPassenger(org.bukkit.entity.Entity vehicle, org.bukkit.entity.Entity passenger) {
  55. vehicle.setPassenger(passenger);
  56. }
  57. public static void resetWitherSize(Wither wither) {
  58. ((CraftWither) wither).getHandle().g(600);
  59. }
  60. public static void setHorseSpeed(Horse horse, double speed) {
  61. ((CraftHorse) horse).getHandle().getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).setValue(speed);
  62. }
  63. public static void removePathFinders(org.bukkit.entity.Entity entity) {
  64. net.minecraft.server.v1_10_R1.Entity nmsEntity = ((CraftEntity) entity).getHandle();
  65. try {
  66. Field bField = PathfinderGoalSelector.class.getDeclaredField("b");
  67. bField.setAccessible(true);
  68. Field cField = PathfinderGoalSelector.class.getDeclaredField("c");
  69. cField.setAccessible(true);
  70. bField.set(((EntityInsentient) nmsEntity).goalSelector, Sets.newLinkedHashSet());
  71. bField.set(((EntityInsentient) nmsEntity).targetSelector, Sets.newLinkedHashSet());
  72. cField.set(((EntityInsentient) nmsEntity).goalSelector, Sets.newLinkedHashSet());
  73. cField.set(((EntityInsentient) nmsEntity).targetSelector, Sets.newLinkedHashSet());
  74. } catch (Exception exc) {
  75. exc.printStackTrace();
  76. }
  77. }
  78. public static void startHandChanging(final Plugin plugin){
  79. //setData(p,5,false,MetadataType.BOOLEAN); //No gravity
  80. //setData(p,10,20,MetadataType.VAR_INT); //Arrow count
  81. //setData(p,14,(byte)0,MetadataType.BYTE); //Main hand
  82. Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
  83. boolean hand=false;
  84. @Override
  85. public void run() {
  86. for (Player player : plugin.getServer().getOnlinePlayers()) {
  87. setData(player,14,hand?0:1,MetadataType.BYTE);
  88. hand=!hand;
  89. }
  90. }
  91. },40,40);
  92. }
  93. public static void sendBlizzard(final Player player, Location loc, boolean affectPlayers, Vector v) {
  94. if (!fakeArmorStandsMap.containsKey(player))
  95. fakeArmorStandsMap.put(player, new ArrayList<EntityArmorStand>());
  96. if (!cooldownJumpMap.containsKey(player))
  97. cooldownJumpMap.put(player, new ArrayList<org.bukkit.entity.Entity>());
  98. final List<EntityArmorStand> fakeArmorStands = fakeArmorStandsMap.get(player);
  99. final List<org.bukkit.entity.Entity> cooldownJump = cooldownJumpMap.get(player);
  100. final EntityArmorStand as = new EntityArmorStand(((CraftWorld) player.getWorld()).getHandle());
  101. as.setInvisible(true);
  102. as.setSmall(true);
  103. as.setNoGravity(true);
  104. as.setArms(true);
  105. as.setHeadPose(new Vector3f((float) (UtilRandom.nextInt(360)),
  106. (float) (UtilRandom.nextInt(360)),
  107. (float) (UtilRandom.nextInt(360))));
  108. as.setLocation(loc.getX() + UtilMath.randomDouble(-1.5, 1.5), loc.getY() + UtilMath.randomDouble(0, .5) - 0.75, loc.getZ() + UtilMath.randomDouble(-1.5, 1.5), 0, 0);
  109. fakeArmorStands.add(as);
  110. for (Player players : player.getWorld().getPlayers()) {
  111. PacketSender.send(players, new PacketPlayOutSpawnEntityLiving(as));
  112. PacketSender.send(players, new PacketPlayOutEntityEquipment(as.getId(), EnumItemSlot.HEAD, CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(org.bukkit.Material.PACKED_ICE))));
  113. }
  114. UtilParticles.display(Particles.CLOUD, loc.clone().add(UtilMath.randomDouble(-1.5, 1.5), UtilMath.randomDouble(0, .5) - 0.75, UtilMath.randomDouble(-1.5, 1.5)), 2, 0.4f);
  115. Bukkit.getScheduler().runTaskLater(UtilCore.getPlugin(), new Runnable() {
  116. @Override
  117. public void run() {
  118. for (Player pl : player.getWorld().getPlayers())
  119. PacketSender.send(pl, new PacketPlayOutEntityDestroy(as.getId()));
  120. fakeArmorStands.remove(as);
  121. }
  122. }, 20);
  123. if (affectPlayers)
  124. for (final org.bukkit.entity.Entity ent : as.getBukkitEntity().getNearbyEntities(0.5, 0.5, 0.5)) {
  125. if (!cooldownJump.contains(ent) && ent != player) {
  126. UtilMath.applyVelocity(ent, new Vector(0, 1, 0).add(v));
  127. cooldownJump.add(ent);
  128. Bukkit.getScheduler().runTaskLater(UtilCore.getPlugin(), new Runnable() {
  129. @Override
  130. public void run() {
  131. cooldownJump.remove(ent);
  132. }
  133. }, 20);
  134. }
  135. }
  136. }
  137. public static void clearBlizzard(Player player) {
  138. if (!fakeArmorStandsMap.containsKey(player)) return;
  139. for (EntityArmorStand as : fakeArmorStandsMap.get(player))
  140. for (Player pl : player.getWorld().getPlayers())
  141. PacketSender.send(pl, new PacketPlayOutEntityDestroy(as.getId()));
  142. fakeArmorStandsMap.remove(player);
  143. cooldownJumpMap.remove(player);
  144. }
  145. public static void clearPathfinders(org.bukkit.entity.Entity entity) {
  146. net.minecraft.server.v1_10_R1.Entity nmsEntity = ((CraftEntity) entity).getHandle();
  147. try {
  148. Field bField = PathfinderGoalSelector.class.getDeclaredField("b");
  149. bField.setAccessible(true);
  150. Field cField = PathfinderGoalSelector.class.getDeclaredField("c");
  151. cField.setAccessible(true);
  152. bField.set(((EntityInsentient) nmsEntity).goalSelector, Sets.newLinkedHashSet());
  153. bField.set(((EntityInsentient) nmsEntity).targetSelector, Sets.newLinkedHashSet());
  154. cField.set(((EntityInsentient) nmsEntity).goalSelector, Sets.newLinkedHashSet());
  155. cField.set(((EntityInsentient) nmsEntity).targetSelector, Sets.newLinkedHashSet());
  156. } catch (Exception exc) {
  157. exc.printStackTrace();
  158. }
  159. }
  160. public static void makePanic(org.bukkit.entity.Entity entity) {
  161. EntityInsentient insentient = (EntityInsentient) ((CraftEntity) entity).getHandle();
  162. insentient.goalSelector.a(3, new CustomPathFinderGoalPanic((EntityCreature) insentient, 0.4d));
  163. }
  164. public static void sendDestroyPacket(Player player, org.bukkit.entity.Entity entity) {
  165. PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(((CraftEntity) entity).getHandle().getId());
  166. ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
  167. }
  168. public static void move(Creature creature, Location loc) {
  169. EntityCreature ec = ((CraftCreature) creature).getHandle();
  170. ec.P = 1;
  171. if (loc == null) return;
  172. ec.getNavigation().a(loc.getX(), loc.getY(), loc.getZ(), (1.0D + 2.0D * 0.5d) * 1.0D);
  173. }
  174. public static void moveDragon(Player player, Vector vector, org.bukkit.entity.Entity entity) {
  175. EntityEnderDragon ec = ((CraftEnderDragon) entity).getHandle();
  176. ec.hurtTicks = -1;
  177. ec.getBukkitEntity().setVelocity(vector);
  178. ec.pitch = player.getLocation().getPitch();
  179. ec.yaw = player.getLocation().getYaw() - 180;
  180. Vector v = ec.getBukkitEntity().getLocation().getDirection();
  181. Vector v1 = ec.getBukkitEntity().getLocation().getDirection().multiply(-1);
  182. ec.move(v1.getX(), v.getY(), v1.getZ());
  183. }
  184. public static void setClimb(org.bukkit.entity.Entity entity) {
  185. ((CraftEntity) entity).getHandle().P = 1;
  186. }
  187. public static void moveShip(Player player, org.bukkit.entity.Entity entity, Vector vector) {
  188. EntityBoat ec = ((CraftBoat) entity).getHandle();
  189. ec.getBukkitEntity().setVelocity(vector);
  190. ec.pitch = player.getLocation().getPitch();
  191. ec.yaw = player.getLocation().getYaw() - 180;
  192. ec.move(1, 0, 0);
  193. }
  194. public static void playChestAnimation(org.bukkit.block.Block b, boolean open, boolean isEnderChest) {
  195. Location location = b.getLocation();
  196. WorldServer world = ((CraftWorld) location.getWorld()).getHandle();
  197. BlockPosition position = new BlockPosition(location.getX(), location.getY(), location.getZ());
  198. if (isEnderChest) {
  199. TileEntityEnderChest tileChest = (TileEntityEnderChest) world.getTileEntity(position);
  200. world.playBlockAction(position, tileChest.getBlock(), 1, open ? 1 : 0);
  201. } else {
  202. TileEntityChest tileChest = (TileEntityChest) world.getTileEntity(position);
  203. world.playBlockAction(position, tileChest.getBlock(), 1, open ? 1 : 0);
  204. }
  205. }
  206. public static void playChestAnimation(org.bukkit.block.Block b, boolean open, TreasureChestDesign design){
  207. playChestAnimation(b,open,design.getChestType() == ChestType.ENDER);
  208. }
  209. public static org.bukkit.entity.Entity spawnItem(org.bukkit.inventory.ItemStack itemStack, Location blockLocation) {
  210. EntityItem ei = new EntityItem(
  211. ((CraftWorld) blockLocation.clone().add(0.5D, 1.2D, 0.5D).getWorld()).getHandle(),
  212. blockLocation.clone().add(0.5D, 1.2D, 0.5D).getX(),
  213. blockLocation.clone().add(0.5D, 1.2D, 0.5D).getY(),
  214. blockLocation.clone().add(0.5D, 1.2D, 0.5D).getZ(),
  215. CraftItemStack.asNMSCopy(itemStack)) {
  216. private boolean a(EntityItem entityitem) {
  217. return false;
  218. }
  219. };
  220. ei.getBukkitEntity().setVelocity(new Vector(0.0D, 0.25D, 0.0D));
  221. ei.pickupDelay = 2147483647;
  222. ei.getBukkitEntity().setCustomName(UUID.randomUUID().toString());
  223. ei.pickupDelay = 20;
  224. ((CraftWorld) blockLocation.clone().add(0.5D, 1.2D, 0.5D).getWorld()).getHandle().addEntity(ei);
  225. return ei.getBukkitEntity();
  226. }
  227. public static boolean isSameInventory(Inventory first, Inventory second) {
  228. return ((CraftInventory) first).getInventory().equals(((CraftInventory) second).getInventory());
  229. }
  230. public static void follow(org.bukkit.entity.Entity toFollow, org.bukkit.entity.Entity follower) {
  231. net.minecraft.server.v1_10_R1.Entity pett = ((CraftEntity) follower).getHandle();
  232. ((EntityInsentient) pett).getNavigation().a(2);
  233. Object petf = ((CraftEntity) follower).getHandle();
  234. Location targetLocation = toFollow.getLocation();
  235. PathEntity path;
  236. path = ((EntityInsentient) petf).getNavigation().a(targetLocation.getX() + 1, targetLocation.getY(), targetLocation.getZ() + 1);
  237. if (path != null) {
  238. ((EntityInsentient) petf).getNavigation().a(path, 1.05D);
  239. ((EntityInsentient) petf).getNavigation().a(1.05D);
  240. }
  241. }
  242. public static void chickenFall(Player player) {
  243. EntityPlayer entityPlayer = ((CraftPlayer) player).getHandle();
  244. if (!entityPlayer.onGround && entityPlayer.motY < 0.0D) {
  245. Vector v = player.getVelocity();
  246. player.setVelocity(v);
  247. entityPlayer.motY *= 0.85;
  248. }
  249. }
  250. public static void sendTeleportPacket(Player player, org.bukkit.entity.Entity entity) {
  251. ((CraftPlayer) player).getHandle().playerConnection.sendPacket(new PacketPlayOutEntityTeleport(((CraftEntity) entity).getHandle()));
  252. }
  253. }