Advertisement
thevipershowita

Untitled

Nov 12th, 2020
1,246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.10 KB | None | 0 0
  1. package me.thevipershow.bedwars.listeners.game;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import me.thevipershow.bedwars.bedwars.objects.BedwarsTeam;
  7. import me.thevipershow.bedwars.config.objects.SpawnPosition;
  8. import me.thevipershow.bedwars.events.TrapTriggerEvent;
  9. import me.thevipershow.bedwars.game.ActiveGame;
  10. import me.thevipershow.bedwars.game.objects.BedwarsPlayer;
  11. import me.thevipershow.bedwars.game.objects.PlayerState;
  12. import me.thevipershow.bedwars.game.objects.TeamData;
  13. import me.thevipershow.bedwars.game.upgrades.ActiveTrap;
  14. import org.bukkit.Location;
  15. import org.bukkit.Material;
  16. import org.bukkit.entity.Player;
  17. import org.bukkit.inventory.ItemStack;
  18. import org.bukkit.scheduler.BukkitTask;
  19.  
  20. public final class GameTrapTriggerer {
  21.  
  22.     private final ActiveGame activeGame;
  23.     private BukkitTask task = null;
  24.  
  25.     public GameTrapTriggerer(final ActiveGame activeGame) {
  26.         this.activeGame = activeGame;
  27.     }
  28.  
  29.     public final void start() {
  30.         if (task != null) {
  31.             return;
  32.         }
  33.  
  34.         task = activeGame.getPlugin().getServer().getScheduler().runTaskTimer(activeGame.getPlugin(), () -> {
  35.  
  36.  
  37.             teamLabel:
  38.             for (final Map.Entry<BedwarsTeam, LinkedList<ActiveTrap>> teamActiveTraps : activeGame.getTrapsManager().getActiveTraps().entrySet()) {
  39.                 final LinkedList<ActiveTrap> traps = teamActiveTraps.getValue();  // Getting all active traps lists
  40.                 if (traps.isEmpty()) {                                            // and looping through each one of them
  41.                     continue;
  42.                 }
  43.  
  44.                 final BedwarsTeam trapTeamOwner = teamActiveTraps.getKey();
  45.                 for (final Map.Entry<BedwarsTeam, ? extends TeamData<?>> entry : activeGame.getTeamManager().getDataMap().entrySet()) {
  46.                     final BedwarsTeam bedwarsTeam = entry.getKey();
  47.                     if (trapTeamOwner != bedwarsTeam) {
  48.                         for (BedwarsPlayer bedwarsPlayer : entry.getValue().getAll()) {
  49.  
  50.  
  51.                             if (bedwarsPlayer.isOnline() && bedwarsPlayer.getPlayerState() != PlayerState.DEAD && bedwarsPlayer.getPlayerState() != PlayerState.RESPAWNING) {
  52.  
  53.                                 final SpawnPosition spawnPos = activeGame.getCachedGameData().getCachedTeamSpawnPositions().get(trapTeamOwner);
  54.                                 final Location pLoc = bedwarsPlayer.getLocation();
  55.  
  56.                                 final double xDist = spawnPos.xDistance(pLoc);
  57.                                 final double zDist = spawnPos.zDistance(pLoc);
  58.                                 final double yDist = spawnPos.yDistance(pLoc);
  59.  
  60.                                 final boolean isPlayerImmune = bedwarsPlayer.isImmuneToTraps(); // Checking if the player
  61.                                 // is still immune to traps
  62.  
  63.                                 final long lastTeamTrapActivation = activeGame.getTrapsManager().getTrapsActivationTime().get(bedwarsTeam); // getting the last time that
  64.                                 // a trap owned by this team
  65.                                 // has been activated.
  66.  
  67.                                 if ((System.currentTimeMillis() - lastTeamTrapActivation) / 1e3 < 15.0) { // 15.0 seconds have not passed
  68.                                     continue teamLabel;                                             // skipping this team checks.
  69.                                 }
  70.  
  71.                                 if (xDist <= 20.0 && zDist <= 20.0 && yDist <= 5.01) { // Player has entered island's
  72.                                     // box region [±20.0, ±6.01, ±20.0]
  73.  
  74.                                     if (isPlayerImmune) { // We should not trigger trap
  75.                                         continue;         // when player is immune
  76.                                     }
  77.  
  78.                                     final TrapTriggerEvent trapTriggerEvent = new TrapTriggerEvent(bedwarsPlayer, traps.getFirst(), trapTeamOwner);
  79.                                     activeGame.getPlugin().getServer().getPluginManager().callEvent(trapTriggerEvent);
  80.                                     if (trapTriggerEvent.isCancelled()) {
  81.                                         return;
  82.                                     }
  83.  
  84.                                     teamActiveTraps.getValue().pollFirst().trigger(bedwarsPlayer); // removing
  85.  
  86.                                     // Updating last activation time:
  87.                                     activeGame.getTrapsManager().getTrapsActivationTime().compute(trapTeamOwner, (k, v) -> v = System.currentTimeMillis());
  88.  
  89.                                     // Updating team GUIs: TODO: Reimplement for refactoring
  90.                                     //activeGame.getTeamPlayers(trapTeamOwner)
  91.                                     //        .forEach(p -> activeGame.getAssociatedTrapsGUI().get(p.getUniqueId()).setItem(30 + teamActiveTraps.getValue().size(), new ItemStack(Material.STAINED_GLASS_PANE, 1 + teamActiveTraps.getValue().size())));
  92.  
  93.                                     //activeGame.getTeamPlayers(trapTeamOwner)
  94.                                     //        .forEach(p -> activeGame.getAssociatedUpgradeGUI().get(p.getUniqueId()).setItem(30 + teamActiveTraps.getValue().size(), new ItemStack(Material.STAINED_GLASS_PANE, 1 + teamActiveTraps.getValue().size())));
  95.  
  96.                                     continue teamLabel; // skipping to next team's traps
  97.  
  98.                                 } else { // here the player is out of the island box
  99.                                     if (isPlayerImmune) {                                // Removing the player from
  100.                                         bedwarsPlayer.setImmuneToTraps(false);           // the immune status, because he left island.
  101.                                     }
  102.                                 }
  103.                             }
  104.                         }
  105.                     }
  106.                 }
  107.             }
  108.         }, 20L, 20L);
  109.     }
  110.  
  111.     public final void stop() {
  112.         if (task != null) {
  113.             task.cancel();
  114.         }
  115.     }
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement