Advertisement
TheMrJezza

EventIntake.java

Oct 25th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.27 KB | None | 0 0
  1. package au.TheMrJezza.HorseTpWithMe;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.UUID;
  9.  
  10. import org.bukkit.Chunk;
  11. import org.bukkit.Location;
  12. import org.bukkit.entity.Entity;
  13. import org.bukkit.entity.Player;
  14. import org.bukkit.event.Event;
  15. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
  16. import org.bukkit.event.player.PlayerTeleportEvent;
  17. import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
  18. import org.bukkit.event.vehicle.VehicleEnterEvent;
  19. import org.bukkit.event.vehicle.VehicleExitEvent;
  20. import org.bukkit.scheduler.BukkitRunnable;
  21.  
  22. import au.TheMrJezza.HorseTpWithMe.Events.AnimalTeleportEvent;
  23.  
  24. public class EventIntake {
  25.  
  26.     private static Main instance = Main.getMainInstance();
  27.  
  28.     private boolean isSneaking = false;
  29.     private List<Event> savedEvents = new ArrayList<>();
  30.     private Player player = null;
  31.     private static Map<UUID, EventIntake> intakes = new HashMap<>();
  32.     private String locationID;
  33.  
  34.     public static void add(Player player, Event evt) {
  35.         EventIntake intake;
  36.         if (intakes.containsKey(player.getUniqueId())) {
  37.             intake = intakes.get(player.getUniqueId());
  38.         } else {
  39.             intake = new EventIntake();
  40.             new BukkitRunnable() {
  41.                 @Override
  42.                 public void run() {
  43.                     intake.process();
  44.                     intake.kill();
  45.                 }
  46.             }.runTaskLater(instance, 1L);
  47.         }
  48.         if (player.isSneaking()) {
  49.             intake.isSneaking = true;
  50.         }
  51.         if (intake.player == null) {
  52.             intake.player = player;
  53.         }
  54.         intake.savedEvents.add(evt);
  55.         intakes.put(player.getUniqueId(), intake);
  56.     }
  57.  
  58.     protected void kill() {
  59.         intakes.remove(player.getUniqueId());
  60.     }
  61.  
  62.     public boolean hasLocationID(String string) {
  63.         return this.locationID != null ? this.locationID.equalsIgnoreCase(string) : false;
  64.     }
  65.  
  66.     public boolean hasLocationID(Chunk chunk) {
  67.         return hasLocationID(chunk.getWorld().getName() + ":" + chunk.getX() + ":" + chunk.getZ());
  68.     }
  69.  
  70.     private void process() {
  71.         TeleportCause cause = TeleportCause.UNKNOWN;
  72.         Location from = null, to = null;
  73.         Entity vehicle = null;
  74.         String commandMessage = null;
  75.         for (Event evt : savedEvents) {
  76.             if (evt instanceof VehicleEnterEvent) {
  77.                 return;
  78.             }
  79.             if (evt instanceof VehicleExitEvent) {
  80.                 VehicleExitEvent temp = (VehicleExitEvent) evt;
  81.                 if (vehicle == null) {
  82.                     vehicle = temp.getVehicle();
  83.                 }
  84.                 continue;
  85.             }
  86.             if (evt instanceof PlayerCommandPreprocessEvent) {
  87.                 commandMessage = ((PlayerCommandPreprocessEvent) evt).getMessage();
  88.             }
  89.             if (evt instanceof PlayerTeleportEvent) {
  90.                 PlayerTeleportEvent temp = (PlayerTeleportEvent) evt;
  91.                 if (!temp.getFrom().equals(temp.getTo()) && to == null) {
  92.                     from = temp.getFrom();
  93.                     to = temp.getTo();
  94.                 }
  95.                 if (temp.getCause() != TeleportCause.UNKNOWN) {
  96.                     cause = temp.getCause();
  97.                 }
  98.                 continue;
  99.             }
  100.         }
  101.  
  102.         if (isSneaking || from == null || to == null) {
  103.             return;
  104.         }
  105.  
  106.         Main.getMainInstance().intakeID.add(this.locationID);
  107.         if (cause == TeleportCause.UNKNOWN) {
  108.             Location loc0 = from.clone(), loc1 = to.clone();
  109.             if (loc0.getWorld().equals(loc1.getWorld())) {
  110.                 if (commandMessage == null) {
  111.                     loc0.setY(0);
  112.                     loc1.setY(0);
  113.                     if (loc0.distance(loc1) < 0.5) {
  114.                         return;
  115.                     }
  116.                 }
  117.             }
  118.         }
  119.  
  120.         if (cause == TeleportCause.CHORUS_FRUIT || cause == TeleportCause.SPECTATE) {
  121.             return;
  122.         }
  123.  
  124.         if (vehicle == null || vehicle.isDead() || !player.isValid()) {
  125.             return;
  126.         }
  127.  
  128.         /*String permission = Main.getVersion().getPermission(vehicle);
  129.         if (permission == null) {
  130.             return;
  131.         }*/
  132.  
  133.         {// The Key to all HTpWM Extensions..
  134.             AnimalTeleportEvent animalTpEvt = new AnimalTeleportEvent(from, to, vehicle, player, null);
  135.             instance.getServer().getPluginManager().callEvent(animalTpEvt);
  136.             if (animalTpEvt.isCancelled())
  137.                 return;
  138.  
  139.         }
  140.         List<Entity> passengers = vehicle.getPassengers();// Main.getVersion().getPassengers(vehicle);
  141.         if (passengers.contains(player))
  142.             passengers.remove(player);
  143.         for (Entity e : passengers) {
  144.             if (!(e instanceof Player))
  145.                 teleportTo(e, to, from);
  146.         }
  147.  
  148.         vehicle.setFallDistance(-2000000f);
  149.         teleportTo(vehicle, to, from);
  150.         final Entity veh = vehicle;
  151.         long dud = 5l;
  152.         if (from.getWorld().equals(to.getWorld())) {
  153.             final double distance = from.distance(to);
  154.             if (distance < 25) {
  155.                 dud = (long) Math.floor(Math.max(distance * 0.4, 4.0));
  156.             }
  157.         }
  158.         new BukkitRunnable() {
  159.             public void run() {
  160.                 veh.addPassenger(player);
  161.                 for (Entity e : passengers) {
  162.                     if (!(e instanceof Player)) {
  163.                         veh.addPassenger(e);
  164.                         // Main.getVersion().addPassenger(veh, player);
  165.                     }
  166.                 }
  167.             }
  168.         }.runTaskLater(instance, dud);
  169.     }
  170.  
  171.     private void teleportTo(Entity entity, Location to, Location from) {
  172.         if (from.getWorld().equals(to.getWorld())) {
  173.             entity.teleport(to);
  174.             return;
  175.         }
  176.        
  177.         entity.addScoreboardTag("HTWM_DESPAWN");
  178.         // This is where I do my cross world teleporting, no code here atm,
  179.         // it was in another class. I've since deleted the whole package
  180.         // that contained version independent Stuff/NMS code.
  181.         // You'll notice the references to getVersion() in some of the comments.
  182.     }
  183.  
  184.     public static Collection<EventIntake> getIntakes() {
  185.         return intakes.values();
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement