Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.deinserver.zone;
- import org.bukkit.Bukkit;
- import org.bukkit.World;
- import org.bukkit.WorldBorder;
- import org.bukkit.block.Biome;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandExecutor;
- import org.bukkit.command.CommandSender;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.scheduler.BukkitRunnable;
- import org.bukkit.scheduler.BukkitTask;
- import java.util.Random;
- public final class ZonePlugin extends JavaPlugin implements CommandExecutor {
- private boolean zoneActive = false;
- private double currentX = 0;
- private double currentZ = 0;
- private double currentSize = 2000;
- private BukkitTask activeTask = null;
- private final double START_SIZE = 2000;
- private final double MAX_RADIUS = 800;
- private final double P1_SIZE = 1000;
- private final double P2_SIZE = 400;
- private final double P3_SIZE = 50;
- @Override
- public void onEnable() {
- saveDefaultConfig();
- getConfig().options().copyDefaults(true);
- saveConfig();
- if (getCommand("zonestart") != null) {
- getCommand("zonestart").setExecutor(this);
- }
- if (getCommand("zonereset") != null) {
- getCommand("zonereset").setExecutor(this);
- }
- }
- @Override
- public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
- if (command.getName().equalsIgnoreCase("zonestart")) {
- if (!sender.hasPermission("zone.start")) return true;
- startZoneGame();
- return true;
- }
- if (command.getName().equalsIgnoreCase("zonereset")) {
- if (!sender.hasPermission("zone.reset")) return true;
- resetZone();
- return true;
- }
- return false;
- }
- private void resetZone() {
- zoneActive = false;
- if (activeTask != null) {
- activeTask.cancel();
- }
- World world = Bukkit.getWorld("world");
- if (world != null) {
- WorldBorder border = world.getWorldBorder();
- border.setSize(10000);
- border.setCenter(0, 0);
- }
- currentX = 0;
- currentZ = 0;
- Bukkit.broadcastMessage("§c[Zone] §fThe WorldBorder was stopped and fully reset.");
- }
- private void startZoneGame() {
- zoneActive = true;
- currentX = 0;
- currentZ = 0;
- currentSize = START_SIZE;
- World world = Bukkit.getWorld("world");
- if (world == null) return;
- WorldBorder border = world.getWorldBorder();
- border.setDamageBuffer(0);
- border.setDamageAmount(2);
- border.setSize(START_SIZE);
- border.setCenter(0, 0);
- Bukkit.broadcastMessage("§c[Zone] §aThe game has started! The Zone size is set to §e"
- + (int) START_SIZE + "x" + (int) START_SIZE + " §ablocks.");
- int startWaitSeconds = getConfiguredSeconds("timings.start-wait-seconds", 60, true);
- if (startWaitSeconds == 0) {
- runPhase1();
- return;
- }
- activeTask = new BukkitRunnable() {
- int waitCounter = startWaitSeconds;
- @Override
- public void run() {
- if (!zoneActive) {
- cancel();
- return;
- }
- waitCounter--;
- if (waitCounter <= 0) {
- cancel();
- runPhase1();
- }
- }
- }.runTaskTimer(this, 20L, 20L);
- }
- private void runPhase1() {
- World world = Bukkit.getWorld("world");
- if (world == null) return;
- double[] target = getRandomNonOceanLocation(world, currentX, currentZ, MAX_RADIUS);
- double targetX = target[0];
- double targetZ = target[1];
- long duration = getConfiguredSeconds("timings.phase-1-seconds", 2700, false);
- Bukkit.broadcastMessage("§c[Zone] §fPhase 1 starting! The Zone is continuously moving and shrinking over "
- + duration + " seconds.");
- runMovingPhase(world, targetX, targetZ, P1_SIZE, duration, () -> {
- int breakSeconds = getConfiguredSeconds("timings.break-between-phases-seconds", 120, true);
- Bukkit.broadcastMessage("§c[Zone] §aPhase 1 complete! " + breakSeconds + " seconds break.");
- runPauseThen(breakSeconds, () -> runPhase2());
- });
- }
- private void runPhase2() {
- World world = Bukkit.getWorld("world");
- if (world == null) return;
- double maxDev = currentSize / 2;
- double[] target = getRandomNonOceanLocation(world, currentX, currentZ, maxDev);
- double targetX = target[0];
- double targetZ = target[1];
- long duration = getConfiguredSeconds("timings.phase-2-seconds", 1800, false);
- Bukkit.broadcastMessage("§c[Zone] §6WARNING! §fPhase 2 starting! The Zone moves to a new location over "
- + duration + " seconds.");
- runMovingPhase(world, targetX, targetZ, P2_SIZE, duration, () -> {
- int breakSeconds = getConfiguredSeconds("timings.break-between-phases-seconds", 120, true);
- Bukkit.broadcastMessage("§c[Zone] §aPhase 2 complete! " + breakSeconds
- + " seconds break before the finale.");
- runPauseThen(breakSeconds, () -> runPhase3());
- });
- }
- private void runPhase3() {
- World world = Bukkit.getWorld("world");
- if (world == null) return;
- double maxDev = currentSize / 2;
- double[] target = getRandomNonOceanLocation(world, currentX, currentZ, maxDev);
- double targetX = target[0];
- double targetZ = target[1];
- long duration = getConfiguredSeconds("timings.phase-3-seconds", 900, false);
- Bukkit.broadcastMessage("§c[Zone] §4FINAL PHASE! §fShrinking down to §e50 blocks§f over "
- + duration + " seconds!");
- runMovingPhase(world, targetX, targetZ, P3_SIZE, duration, () -> {
- Bukkit.broadcastMessage("§c[Zone] §4The Zone reached 50 blocks and will now move endlessly!");
- runEndlessPhase();
- });
- }
- private void runEndlessPhase() {
- World world = Bukkit.getWorld("world");
- if (world == null || !zoneActive) return;
- double[] target = getRandomNonOceanLocation(world, currentX, currentZ, 100);
- double targetX = target[0];
- double targetZ = target[1];
- long duration = getConfiguredSeconds("timings.endless-move-seconds", 300, false);
- runMovingPhase(world, targetX, targetZ, P3_SIZE, duration, () -> {
- if (zoneActive) {
- runEndlessPhase();
- }
- });
- }
- private void runMovingPhase(World world, double targetX, double targetZ, double targetSize,
- long durationSeconds, Runnable nextStep) {
- WorldBorder border = world.getWorldBorder();
- double startX = currentX;
- double startZ = currentZ;
- double startSize = currentSize;
- long durationTicks = Math.max(1L, durationSeconds * 20L);
- activeTask = new BukkitRunnable() {
- private long currentTick = 0;
- @Override
- public void run() {
- if (!zoneActive) {
- cancel();
- return;
- }
- currentTick++;
- double progress = Math.min(1.0, (double) currentTick / durationTicks);
- currentX = startX + ((targetX - startX) * progress);
- currentZ = startZ + ((targetZ - startZ) * progress);
- currentSize = startSize + ((targetSize - startSize) * progress);
- border.setCenter(currentX, currentZ);
- border.setSize(currentSize);
- if (currentTick >= durationTicks) {
- cancel();
- currentX = targetX;
- currentZ = targetZ;
- currentSize = targetSize;
- border.setCenter(targetX, targetZ);
- border.setSize(targetSize);
- nextStep.run();
- }
- }
- }.runTaskTimer(this, 1L, 1L);
- }
- private void runPauseThen(int seconds, Runnable nextStep) {
- if (seconds <= 0) {
- nextStep.run();
- return;
- }
- activeTask = new BukkitRunnable() {
- int secLeft = seconds;
- @Override
- public void run() {
- if (!zoneActive) {
- cancel();
- return;
- }
- secLeft--;
- if (secLeft <= 0) {
- cancel();
- nextStep.run();
- }
- }
- }.runTaskTimer(this, 20L, 20L);
- }
- private int getConfiguredSeconds(String path, int defaultValue, boolean allowZero) {
- int value = getConfig().getInt(path, defaultValue);
- int minimumValue = allowZero ? 0 : 1;
- if (value < minimumValue) {
- getLogger().warning("Invalid value for '" + path + "'. Using " + defaultValue + " seconds.");
- return defaultValue;
- }
- return value;
- }
- private double[] getRandomNonOceanLocation(World world, double originX, double originZ, double radius) {
- Random rand = new Random();
- double x, z;
- Biome biome;
- int attempts = 0;
- do {
- x = originX + (rand.nextDouble() * radius * 2 - radius);
- z = originZ + (rand.nextDouble() * radius * 2 - radius);
- biome = world.getBiome((int) x, 64, (int) z);
- attempts++;
- if (attempts > 100) break;
- } while (biome.getKey().getKey().contains("ocean"));
- return new double[]{x, z};
- }
- }
Advertisement