Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package au.TheMrJezza.HorseTpWithMe;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.UUID;
- import org.bukkit.Chunk;
- import org.bukkit.Location;
- import org.bukkit.entity.Entity;
- import org.bukkit.entity.Player;
- import org.bukkit.event.Event;
- import org.bukkit.event.player.PlayerCommandPreprocessEvent;
- import org.bukkit.event.player.PlayerTeleportEvent;
- import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
- import org.bukkit.event.vehicle.VehicleEnterEvent;
- import org.bukkit.event.vehicle.VehicleExitEvent;
- import org.bukkit.scheduler.BukkitRunnable;
- import au.TheMrJezza.HorseTpWithMe.Events.AnimalTeleportEvent;
- public class EventIntake {
- private static Main instance = Main.getMainInstance();
- private boolean isSneaking = false;
- private List<Event> savedEvents = new ArrayList<>();
- private Player player = null;
- private static Map<UUID, EventIntake> intakes = new HashMap<>();
- private String locationID;
- public static void add(Player player, Event evt) {
- EventIntake intake;
- if (intakes.containsKey(player.getUniqueId())) {
- intake = intakes.get(player.getUniqueId());
- } else {
- intake = new EventIntake();
- new BukkitRunnable() {
- @Override
- public void run() {
- intake.process();
- intake.kill();
- }
- }.runTaskLater(instance, 1L);
- }
- if (player.isSneaking()) {
- intake.isSneaking = true;
- }
- if (intake.player == null) {
- intake.player = player;
- }
- intake.savedEvents.add(evt);
- intakes.put(player.getUniqueId(), intake);
- }
- protected void kill() {
- intakes.remove(player.getUniqueId());
- }
- public boolean hasLocationID(String string) {
- return this.locationID != null ? this.locationID.equalsIgnoreCase(string) : false;
- }
- public boolean hasLocationID(Chunk chunk) {
- return hasLocationID(chunk.getWorld().getName() + ":" + chunk.getX() + ":" + chunk.getZ());
- }
- private void process() {
- TeleportCause cause = TeleportCause.UNKNOWN;
- Location from = null, to = null;
- Entity vehicle = null;
- String commandMessage = null;
- for (Event evt : savedEvents) {
- if (evt instanceof VehicleEnterEvent) {
- return;
- }
- if (evt instanceof VehicleExitEvent) {
- VehicleExitEvent temp = (VehicleExitEvent) evt;
- if (vehicle == null) {
- vehicle = temp.getVehicle();
- }
- continue;
- }
- if (evt instanceof PlayerCommandPreprocessEvent) {
- commandMessage = ((PlayerCommandPreprocessEvent) evt).getMessage();
- }
- if (evt instanceof PlayerTeleportEvent) {
- PlayerTeleportEvent temp = (PlayerTeleportEvent) evt;
- if (!temp.getFrom().equals(temp.getTo()) && to == null) {
- from = temp.getFrom();
- to = temp.getTo();
- }
- if (temp.getCause() != TeleportCause.UNKNOWN) {
- cause = temp.getCause();
- }
- continue;
- }
- }
- if (isSneaking || from == null || to == null) {
- return;
- }
- Main.getMainInstance().intakeID.add(this.locationID);
- if (cause == TeleportCause.UNKNOWN) {
- Location loc0 = from.clone(), loc1 = to.clone();
- if (loc0.getWorld().equals(loc1.getWorld())) {
- if (commandMessage == null) {
- loc0.setY(0);
- loc1.setY(0);
- if (loc0.distance(loc1) < 0.5) {
- return;
- }
- }
- }
- }
- if (cause == TeleportCause.CHORUS_FRUIT || cause == TeleportCause.SPECTATE) {
- return;
- }
- if (vehicle == null || vehicle.isDead() || !player.isValid()) {
- return;
- }
- /*String permission = Main.getVersion().getPermission(vehicle);
- if (permission == null) {
- return;
- }*/
- {// The Key to all HTpWM Extensions..
- AnimalTeleportEvent animalTpEvt = new AnimalTeleportEvent(from, to, vehicle, player, null);
- instance.getServer().getPluginManager().callEvent(animalTpEvt);
- if (animalTpEvt.isCancelled())
- return;
- }
- List<Entity> passengers = vehicle.getPassengers();// Main.getVersion().getPassengers(vehicle);
- if (passengers.contains(player))
- passengers.remove(player);
- for (Entity e : passengers) {
- if (!(e instanceof Player))
- teleportTo(e, to, from);
- }
- vehicle.setFallDistance(-2000000f);
- teleportTo(vehicle, to, from);
- final Entity veh = vehicle;
- long dud = 5l;
- if (from.getWorld().equals(to.getWorld())) {
- final double distance = from.distance(to);
- if (distance < 25) {
- dud = (long) Math.floor(Math.max(distance * 0.4, 4.0));
- }
- }
- new BukkitRunnable() {
- public void run() {
- veh.addPassenger(player);
- for (Entity e : passengers) {
- if (!(e instanceof Player)) {
- veh.addPassenger(e);
- // Main.getVersion().addPassenger(veh, player);
- }
- }
- }
- }.runTaskLater(instance, dud);
- }
- private void teleportTo(Entity entity, Location to, Location from) {
- if (from.getWorld().equals(to.getWorld())) {
- entity.teleport(to);
- return;
- }
- entity.addScoreboardTag("HTWM_DESPAWN");
- // This is where I do my cross world teleporting, no code here atm,
- // it was in another class. I've since deleted the whole package
- // that contained version independent Stuff/NMS code.
- // You'll notice the references to getVersion() in some of the comments.
- }
- public static Collection<EventIntake> getIntakes() {
- return intakes.values();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement