Implemented CustomPlayerMoveEvent that only fires on certain conditions and only checks every 10ticks
This commit is contained in:
		
							parent
							
								
									c0917a1ab6
								
							
						
					
					
						commit
						ddb6525c40
					
				@ -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;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -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<CustomPlayerMoveEventListener> listeners = new ArrayList<>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    HashMap<UUID, Location> 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);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -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);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user