SteamWar/FightSystem
Archiviert
13
1

Remove code duplication

Dieser Commit ist enthalten in:
yoyosource 2021-05-13 23:37:01 +02:00
Ursprung bd224350a3
Commit a7693ee2f3

Datei anzeigen

@ -7,20 +7,43 @@ import de.steamwar.fightsystem.countdown.SWSound;
import de.steamwar.fightsystem.states.FightState; import de.steamwar.fightsystem.states.FightState;
import de.steamwar.fightsystem.states.StateDependent; import de.steamwar.fightsystem.states.StateDependent;
import de.steamwar.fightsystem.winconditions.Winconditions; 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.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.scheduler.BukkitRunnable; 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 class HellsBells {
public static final Random random = new Random(); 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<Direction> 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() { public HellsBells() {
new StateDependent(Winconditions.HELLS_BELLS, FightState.Running) { new StateDependent(Winconditions.HELLS_BELLS, FightState.Running) {
@ -93,26 +116,18 @@ public class HellsBells {
} }
public void drop() { public void drop() {
if (random.nextBoolean()) { Direction direction = Direction.DIRECTIONS.get(random.nextInt(Direction.DIRECTIONS.size()));
sideRun(random.nextBoolean());
} else {
frontRun(random.nextBoolean());
}
}
AtomicInteger length = new AtomicInteger(10 + random.nextInt((direction.zLength ? zLength : xLength) - 10));
private void sideRun(boolean invert) {
int length = 10 + random.nextInt(zLength - 10);
int width = 5 + random.nextInt(5); int width = 5 + random.nextInt(5);
int xOffset = getWidthStart(xLength, width); int xOffset = getWidthStart(direction.zLength ? xLength : zLength, width);
int zOffset = getLengthStart(zLength, length); int zOffset = getLengthStart(direction.zLength ? zLength : xLength, length.get());
int yOffset = getHeightStart(); int yOffset = getHeightStart();
Point redStart; Point redStart;
Point blueStart; 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); 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); blueStart = new Point(Config.BluePasteRegion.getMinX() + xOffset, Config.BlueExtendRegion.getMaxY() + yOffset, Config.BluePasteRegion.getMinZ() + zOffset);
} else { } else {
@ -120,79 +135,24 @@ public class HellsBells {
blueStart = new Point(Config.BluePasteRegion.getMaxX() - xOffset, Config.BlueExtendRegion.getMaxY() + yOffset, Config.BluePasteRegion.getMaxZ() - zOffset); 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() { new BukkitRunnable() {
@Override @Override
public void run() { public void run() {
for (int z = 0; z < width; z++) { for (int z = 0; z < width; z++) {
if (invert) { if (direction == Direction.NORTH || direction == Direction.WEST) {
world.spawnEntity(redStart.subtractAndToLocation(world, x[0], 0, z), EntityType.PRIMED_TNT); 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 { } 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) { if (length.decrementAndGet() == 0) {
this.cancel(); cancel();
return;
} }
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); }.runTaskTimer(FightSystem.getPlugin(), 0, 4L);
} }