From a7693ee2f3f09adae0dafbe97eb2c7daa6353e08 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 13 May 2021 23:37:01 +0200 Subject: [PATCH] Remove code duplication --- .../fightsystem/event/HellsBells.java | 116 ++++++------------ 1 file changed, 38 insertions(+), 78 deletions(-) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/event/HellsBells.java b/FightSystem_Main/src/de/steamwar/fightsystem/event/HellsBells.java index ca0d4c1..64faefa 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/event/HellsBells.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/event/HellsBells.java @@ -7,20 +7,43 @@ import de.steamwar.fightsystem.countdown.SWSound; import de.steamwar.fightsystem.states.FightState; import de.steamwar.fightsystem.states.StateDependent; import de.steamwar.fightsystem.winconditions.Winconditions; -import java.util.Arrays; -import java.util.List; -import java.util.Random; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.EntityType; import org.bukkit.scheduler.BukkitRunnable; +import java.util.Arrays; +import java.util.List; +import java.util.Random; +import java.util.concurrent.atomic.AtomicInteger; + public class HellsBells { public static final Random random = new Random(); + private enum Direction { + NORTH(0, 0, 1, true), + SOUTH(0, 0, -1, true), + EAST(1, 0, 0, false), + WEST(-1, 0, 0, false); + + static final List DIRECTIONS = Arrays.asList(values()); + + int dx; + int dy; + int dz; + boolean zLength; + + Direction(int dx, int dy, int dz, boolean zLength) { + this.dx = dx; + this.dy = dy; + this.dz = dz; + this.zLength = zLength; + } + } + public HellsBells() { new StateDependent(Winconditions.HELLS_BELLS, FightState.Running) { @@ -93,26 +116,18 @@ public class HellsBells { } public void drop() { - if (random.nextBoolean()) { - sideRun(random.nextBoolean()); - } else { - frontRun(random.nextBoolean()); - } - } + Direction direction = Direction.DIRECTIONS.get(random.nextInt(Direction.DIRECTIONS.size())); - - private void sideRun(boolean invert) { - int length = 10 + random.nextInt(zLength - 10); + AtomicInteger length = new AtomicInteger(10 + random.nextInt((direction.zLength ? zLength : xLength) - 10)); int width = 5 + random.nextInt(5); - int xOffset = getWidthStart(xLength, width); - int zOffset = getLengthStart(zLength, length); + int xOffset = getWidthStart(direction.zLength ? xLength : zLength, width); + int zOffset = getLengthStart(direction.zLength ? zLength : xLength, length.get()); int yOffset = getHeightStart(); - Point redStart; Point blueStart; - if (invert) { + if (direction == Direction.NORTH || direction == Direction.WEST) { redStart = new Point(Config.RedPasteRegion.getMaxX() - xOffset, Config.RedExtendRegion.getMaxY() + yOffset, Config.RedPasteRegion.getMaxZ() - zOffset); blueStart = new Point(Config.BluePasteRegion.getMinX() + xOffset, Config.BlueExtendRegion.getMaxY() + yOffset, Config.BluePasteRegion.getMinZ() + zOffset); } else { @@ -120,79 +135,24 @@ public class HellsBells { blueStart = new Point(Config.BluePasteRegion.getMaxX() - xOffset, Config.BlueExtendRegion.getMaxY() + yOffset, Config.BluePasteRegion.getMaxZ() - zOffset); } - dropSideBombs(redStart, blueStart, width, new int[]{length}, invert); - } - - private void dropSideBombs(Point redStart, Point blueStart, int width, int[] x, boolean invert) { new BukkitRunnable() { @Override public void run() { for (int z = 0; z < width; z++) { - if (invert) { - world.spawnEntity(redStart.subtractAndToLocation(world, x[0], 0, z), EntityType.PRIMED_TNT); + if (direction == Direction.NORTH || direction == Direction.WEST) { + world.spawnEntity(redStart.subtractAndToLocation(world, direction.dx * length.get(), 0, direction.dz * length.get()), EntityType.PRIMED_TNT); - world.spawnEntity(blueStart.addAndToLocation(world, x[0], 0, z), EntityType.PRIMED_TNT); + world.spawnEntity(blueStart.addAndToLocation(world, direction.dx * length.get(), 0, direction.dz * length.get()), EntityType.PRIMED_TNT); } else { - world.spawnEntity(redStart.addAndToLocation(world, x[0], 0, z), EntityType.PRIMED_TNT); + world.spawnEntity(redStart.addAndToLocation(world, direction.dx * length.get(), 0, direction.dz * length.get()), EntityType.PRIMED_TNT); - world.spawnEntity(blueStart.subtractAndToLocation(world, x[0], 0, z), EntityType.PRIMED_TNT); + world.spawnEntity(blueStart.subtractAndToLocation(world, direction.dx * length.get(), 0, direction.dz * length.get()), EntityType.PRIMED_TNT); } } - if (x[0] < 2) { - this.cancel(); - return; + if (length.decrementAndGet() == 0) { + cancel(); } - - x[0] -= 2; - } - }.runTaskTimer(FightSystem.getPlugin(), 0, 4L); - } - - - private void frontRun(boolean invert) { - int length = 10 + random.nextInt(xLength - 10); - int width = 5 + random.nextInt(5); - int xOffset = getWidthStart(zLength, length); - int zOffset = getLengthStart(xLength, width); - int yOffset = getHeightStart(); - - Point redStart; - Point blueStart; - - if (invert) { - redStart = new Point(Config.RedPasteRegion.getMaxX() - xOffset, Config.RedExtendRegion.getMaxY() + yOffset, Config.RedPasteRegion.getMaxZ() - zOffset); - blueStart = new Point(Config.BluePasteRegion.getMinX() + xOffset, Config.BlueExtendRegion.getMaxY() + yOffset, Config.BluePasteRegion.getMinZ() + zOffset); - } else { - redStart = new Point(Config.RedPasteRegion.getMinX() + xOffset, Config.RedExtendRegion.getMaxY() + yOffset, Config.RedPasteRegion.getMinZ() + zOffset); - blueStart = new Point(Config.BluePasteRegion.getMaxX() - xOffset, Config.BlueExtendRegion.getMaxY() + yOffset, Config.BluePasteRegion.getMaxZ() - zOffset); - } - - dropFrontBombs(redStart, blueStart, width, new int[]{length}, invert); - } - - private void dropFrontBombs(Point redStart, Point blueStart, int width, int[] z, boolean invert) { - new BukkitRunnable() { - @Override - public void run() { - for (int x = 0; x < width; x++) { - if (invert) { - world.spawnEntity(redStart.subtractAndToLocation(world, x, 0, z[0]), EntityType.PRIMED_TNT); - - world.spawnEntity(blueStart.addAndToLocation(world, x, 0, z[0]), EntityType.PRIMED_TNT); - } else { - world.spawnEntity(redStart.addAndToLocation(world, x, 0, z[0]), EntityType.PRIMED_TNT); - - world.spawnEntity(blueStart.subtractAndToLocation(world, x, 0, z[0]), EntityType.PRIMED_TNT); - } - } - - if (z[0] < 2) { - this.cancel(); - return; - } - - z[0] -= 2; } }.runTaskTimer(FightSystem.getPlugin(), 0, 4L); }