Advertisement
Krystal_Amaia

Untitled

May 6th, 2020
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. public class TelepadRegistry {
  2.  
  3.     private HashMap<Player, ArrayList<Telepad>> registeredPads;
  4.  
  5.     public void register(Player p, Telepad tp) {
  6.         if(!registeredPads.containsKey(p)){
  7.             //to prevent possible npe's if the HashMap doesn't have the player registered, add the player to the Map,
  8.             //and add a new arraylist to that key index before putting the pad in the player's list.
  9.             registeredPads.put(p, new ArrayList<Telepad>());
  10.         }
  11.         registeredPads.get(p).add(tp);
  12.     }
  13.  
  14.     public Telepad getLastBuiltInRange(Telepad pad) {
  15.         Location padLoc = pad.getLocation();
  16.         Telepad currentChoice = pad;
  17.         ArrayList<Telepad> inRange = getInRange(pad);
  18.         if (inRange.isEmpty()) {
  19.             // nothing found do nothing
  20.         }
  21.         else {
  22.             currentChoice = inRange.get(0);
  23.             double currentDiffTime = pad.getBuildTime() - currentChoice.getBuildTime();
  24.             for (Telepad tp : inRange) {
  25.                 if(pad.getBuildTime() - tp.getBuildTime() < currentDiffTime) {
  26.                     currentChoice = tp;
  27.                 }
  28.             }
  29.         }
  30.         return currentChoice;
  31.     }
  32.  
  33.     private ArrayList<Telepad> getInRange(Telepad source) {
  34.         ArrayList<Telepad> retVal = new ArrayList<Telepad>();
  35.         Location sourceLoc = source.getLocation();
  36.         for (Telepad tp : registeredPads.get(source.getPlayerOwner())) {
  37.             Location destLoc = tp.getLocation();
  38.             double dx = sourceLoc.getX() - destLoc.getX();
  39.             double dy = sourceLoc.getY() - destLoc.getY();
  40.             double distance = Math.abs(Math.sqrt((dx * dx) + (dy * dy)));
  41.             if (distance < source.getRange()) {
  42.                 retVal.add(tp);
  43.             }
  44.         }
  45.         return retVal;
  46.     }
  47.    
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement