Advertisement
thevipershowita

GameTrapTriggererRefactored.java

Nov 26th, 2020
1,347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.70 KB | None | 0 0
  1. package me.thevipershow.bedwars.game.runnables;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.function.Consumer;
  7. import me.thevipershow.bedwars.api.TrapTriggerEvent;
  8. import me.thevipershow.bedwars.bedwars.objects.BedwarsTeam;
  9. import me.thevipershow.bedwars.config.objects.SpawnPosition;
  10. import me.thevipershow.bedwars.game.ActiveGame;
  11. import me.thevipershow.bedwars.game.ActiveGameState;
  12. import me.thevipershow.bedwars.game.data.game.BedwarsPlayer;
  13. import me.thevipershow.bedwars.game.data.game.enums.PlayerState;
  14. import me.thevipershow.bedwars.game.managers.TeamManager;
  15. import me.thevipershow.bedwars.game.managers.TrapsManager;
  16. import me.thevipershow.bedwars.game.upgrades.traps.ActiveTrap;
  17. import org.bukkit.Location;
  18. import org.bukkit.Material;
  19. import org.bukkit.inventory.ItemStack;
  20. import org.bukkit.plugin.Plugin;
  21. import org.bukkit.scheduler.BukkitTask;
  22. import org.jetbrains.annotations.Nullable;
  23.  
  24. public final class GameTrapTriggerer {
  25.  
  26.     private final ActiveGame activeGame;
  27.     private final Runnable gameTrapTriggererRunnable;
  28.     private BukkitTask task = null;
  29.     private final static long ONE_SECOND = 20L;
  30.     private final static float TRAP_ACTIVATION_DELAY_SECONDS = 15.0f;
  31.     private final static float BOUNDING_BOX_X = 20f, BOUNDING_BOX_Z = 20f, BOUNDING_BOX_Y = 5.01f;
  32.  
  33.     private static boolean isValidTrapTarget(BedwarsPlayer bedwarsPlayer) {
  34.         return bedwarsPlayer.isOnline() && !bedwarsPlayer.isImmuneToTraps() && bedwarsPlayer.getPlayerState() != PlayerState.DEAD && bedwarsPlayer.getPlayerState() != PlayerState.RESPAWNING;
  35.     }
  36.  
  37.     @Nullable
  38.     private BedwarsTeam hasEnteredIslandBoundingBox(BedwarsPlayer bedwarsPlayer) {
  39.         Location currentPlayerLocation = bedwarsPlayer.getLocation();
  40.         BedwarsTeam playerTeam = bedwarsPlayer.getBedwarsTeam();
  41.         Map<BedwarsTeam, SpawnPosition> teamSpawnPositionMap = activeGame.getCachedGameData().getCachedTeamSpawnPositions();
  42.  
  43.         for (Map.Entry<BedwarsTeam, SpawnPosition> entry : teamSpawnPositionMap.entrySet()) {
  44.             BedwarsTeam ownerTeam = entry.getKey();
  45.             SpawnPosition teamPos = entry.getValue();
  46.             if (playerTeam == ownerTeam) {
  47.                 continue;
  48.             }
  49.  
  50.             double xDistance = teamPos.xDistance(currentPlayerLocation);
  51.             double zDistance = teamPos.zDistance(currentPlayerLocation);
  52.             double yDistance = teamPos.yDistance(currentPlayerLocation);
  53.             if (xDistance <= BOUNDING_BOX_X && yDistance <= BOUNDING_BOX_Y && zDistance <= BOUNDING_BOX_Z) {
  54.                 return ownerTeam;
  55.             }
  56.         }
  57.  
  58.         return null;
  59.     }
  60.  
  61.     private boolean enoughTimePassedSinceLastActivation(BedwarsTeam team) {
  62.         final long lastTeamTrapActivation = activeGame.getTrapsManager().getTrapsActivationTime().get(team); // getting the last time that
  63.         // a trap owned by this team
  64.         // has been activated.
  65.  
  66.         // 15.0 seconds have not passed
  67.         return (System.currentTimeMillis() - lastTeamTrapActivation) / 1e3 >= TRAP_ACTIVATION_DELAY_SECONDS;
  68.     }
  69.  
  70.     private void callEventAndProceedWhenUncancelled(BedwarsPlayer bedwarsPlayer, ActiveTrap first, Runnable action) {
  71.         TrapTriggerEvent trapTriggerEvent = new TrapTriggerEvent(activeGame, bedwarsPlayer, first, bedwarsPlayer.getBedwarsTeam());
  72.         activeGame.callGameEvent(trapTriggerEvent);
  73.         if (!trapTriggerEvent.isCancelled()) {
  74.             action.run();
  75.         }
  76.     }
  77.  
  78.     private void updateTrapsGUI(TeamManager<?> teamManager, BedwarsTeam activatedTeamTrap, List<ActiveTrap> enemyTraps) {
  79.         teamManager.dataOfTeam(activatedTeamTrap)
  80.                 .perform(p -> activeGame.getGameInventoriesManager().getAssociatedTrapsGUI().get(p.getUniqueId()).setItem(30 + enemyTraps.size(), new ItemStack(Material.STAINED_GLASS_PANE, 1 + enemyTraps.size())));
  81.     }
  82.  
  83.     private void activateTrapLogic(Map<BedwarsTeam, LinkedList<ActiveTrap>> activeTrapMap, BedwarsTeam activatedTeamTrap, BedwarsPlayer triggerCause, TrapsManager trapsManager, TeamManager<?> teamManager) {
  84.         final LinkedList<ActiveTrap> enemyTraps = activeTrapMap.get(activatedTeamTrap);
  85.         if (enemyTraps != null) {
  86.             ActiveTrap first = enemyTraps.pollFirst();
  87.             callEventAndProceedWhenUncancelled(triggerCause, first, () -> {
  88.                 first.trigger(triggerCause);
  89.                 trapsManager.getTrapsActivationTime().put(activatedTeamTrap, System.currentTimeMillis());
  90.                 this.updateTrapsGUI(teamManager, activatedTeamTrap, enemyTraps);
  91.             });
  92.         }
  93.     }
  94.  
  95.     private Consumer<? super BedwarsPlayer> checkTrapLogicForAll(TeamManager<?> teamManager, Map<BedwarsTeam, LinkedList<ActiveTrap>> activeTrapMap, TrapsManager trapsManager) {
  96.         return bedwarsPlayer -> {
  97.             BedwarsTeam activatedTeamTrap = this.hasEnteredIslandBoundingBox(bedwarsPlayer);
  98.             boolean isImmuneToTraps = bedwarsPlayer.isImmuneToTraps();
  99.             if (activatedTeamTrap == null && isImmuneToTraps) {
  100.                 bedwarsPlayer.setImmuneToTraps(false);
  101.             } else if (activatedTeamTrap != null && !isImmuneToTraps && isValidTrapTarget(bedwarsPlayer) && enoughTimePassedSinceLastActivation(activatedTeamTrap)) {
  102.                 this.activateTrapLogic(activeTrapMap, activatedTeamTrap, bedwarsPlayer, trapsManager, teamManager);
  103.             }
  104.         };
  105.     }
  106.  
  107.     private Runnable getGameTrapTriggererRunnable() {
  108.         return () -> {
  109.             if (activeGame.getGameState() != ActiveGameState.STARTED) {
  110.                 this.stop();
  111.             }
  112.  
  113.             TrapsManager trapsManager = activeGame.getTrapsManager();
  114.             Map<BedwarsTeam, LinkedList<ActiveTrap>> activeTrapMap = trapsManager.getActiveTraps();
  115.  
  116.             if (activeTrapMap.isEmpty()) {
  117.                 return;
  118.             }
  119.  
  120.             TeamManager<?> teamManager = activeGame.getTeamManager();
  121.  
  122.             teamManager.performAll(this.checkTrapLogicForAll(teamManager, activeTrapMap, trapsManager));
  123.         };
  124.     }
  125.  
  126.     public GameTrapTriggerer(final ActiveGame activeGame) {
  127.         this.activeGame = activeGame;
  128.         this.gameTrapTriggererRunnable = getGameTrapTriggererRunnable();
  129.     }
  130.  
  131.     private BukkitTask generateTask() {
  132.         Plugin plugin = activeGame.getPlugin();
  133.         return plugin.getServer().getScheduler().runTaskTimer(plugin, this.gameTrapTriggererRunnable, ONE_SECOND, ONE_SECOND);
  134.     }
  135.  
  136.     public final void start() {
  137.         if (task != null) {
  138.             return;
  139.         }
  140.         this.task = generateTask();
  141.     }
  142.  
  143.     public final void stop() {
  144.         if (task != null) {
  145.             task.cancel();
  146.         }
  147.     }
  148. }
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement