diff --git a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/customPlayerMoveEvent/CustomPlayerMoveEvent.java b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/customPlayerMoveEvent/CustomPlayerMoveEvent.java new file mode 100644 index 0000000..18a4b3c --- /dev/null +++ b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/customPlayerMoveEvent/CustomPlayerMoveEvent.java @@ -0,0 +1,35 @@ +package eu.ruekov.ruakij.LinkedBeaconTeleporters.customPlayerMoveEvent; + +import org.bukkit.Location; +import org.bukkit.entity.Player; + +import java.io.InvalidObjectException; + +public class CustomPlayerMoveEvent { + + Player p; + Location loc; + Location oldLoc; + protected double distance; + public CustomPlayerMoveEvent(Player p, Location loc, Location oldLoc){ + this.p = p; + this.loc = loc; + this.oldLoc = oldLoc; + } + + public Player player(){ + return p; + } + + public Location location(){ + return loc; + } + + public Location oldLocation(){ + return oldLoc; + } + + public double distance(){ + return distance; + } +} diff --git a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/customPlayerMoveEvent/CustomPlayerMoveEventHandler.java b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/customPlayerMoveEvent/CustomPlayerMoveEventHandler.java new file mode 100644 index 0000000..4dd3065 --- /dev/null +++ b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/customPlayerMoveEvent/CustomPlayerMoveEventHandler.java @@ -0,0 +1,89 @@ +package eu.ruekov.ruakij.LinkedBeaconTeleporters.customPlayerMoveEvent; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.UUID; + +public class CustomPlayerMoveEventHandler { + + List listeners = new ArrayList<>(); + + HashMap oldPlayerLoc = new HashMap<>(); + + boolean locationChangeOnBlockChange; + public CustomPlayerMoveEventHandler(Plugin plugin, boolean locationChangeOnBlockChange){ + this.locationChangeOnBlockChange = locationChangeOnBlockChange; + + startRunnable(plugin); + } + public CustomPlayerMoveEventHandler(Plugin plugin){ + this(plugin, false); + } + + void startRunnable(Plugin plugin){ + + Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () -> { + // Dont do anything without listeners + if(listeners.size() == 0) return; + + // Check all players + for(Player p : Bukkit.getOnlinePlayers()){ + UUID uuid = p.getUniqueId(); + Location loc = p.getLocation(); + + // Get old-location + Location oldLoc = oldPlayerLoc.get(uuid); + + // Create data-obj + CustomPlayerMoveEvent e = new CustomPlayerMoveEvent(p, loc, oldLoc); + + // When player has an old-location + if(oldLoc != null){ + if(!locationChangeOnBlockChange){ + double distance = loc.distance(oldLoc); + e.distance = distance; + + if(distance > 0) + locationChangeListeners(e); + } + else + locationChangeListeners(e); + } + + tickListeners(e); + + // Save old location + oldPlayerLoc.put(uuid, loc); + } + + }, 20, 10); + } + + boolean blockLocationChanged(Location loc, Location oldLoc){ + return loc.getBlockX() != oldLoc.getBlockX() || + loc.getBlockY() != oldLoc.getBlockY() || + loc.getBlockZ() != oldLoc.getBlockZ(); + } + + void tickListeners(CustomPlayerMoveEvent e){ + for(CustomPlayerMoveEventListener listener : listeners){ + listener.tick(e); + } + } + void locationChangeListeners(CustomPlayerMoveEvent e){ + for(CustomPlayerMoveEventListener listener : listeners){ + listener.locationChange(e); + } + } + + + public void registerListener(CustomPlayerMoveEventListener listener){ + listeners.add(listener); + } +} diff --git a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/customPlayerMoveEvent/CustomPlayerMoveEventListener.java b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/customPlayerMoveEvent/CustomPlayerMoveEventListener.java new file mode 100644 index 0000000..7ff77f1 --- /dev/null +++ b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/customPlayerMoveEvent/CustomPlayerMoveEventListener.java @@ -0,0 +1,10 @@ +package eu.ruekov.ruakij.LinkedBeaconTeleporters.customPlayerMoveEvent; + +import org.bukkit.entity.Player; + +public interface CustomPlayerMoveEventListener { + + void tick(CustomPlayerMoveEvent e); + + void locationChange(CustomPlayerMoveEvent e); +}