Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: World border shrinking after x time passed #33

Merged
merged 3 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void registerEvents() {
manager.registerEvents(new RemoveInvisOnDamage(), plugin);
manager.registerEvents(new SpecialItemCooldown(), plugin);
manager.registerEvents(new TrackerDistance(), plugin);
manager.registerEvents(new WorldBorderResize(), plugin);
//manager.registerEvents(new TieBreaker(), plugin);

// Spawners
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/me/metallicgoat/tweaksaddon/config/MainConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,18 @@ public class MainConfig {
@Config public static int friendly_villagers_range = 20;
@Config public static boolean friendly_villagers_check_visibility = true;

@Config(
description = {
"Resize the world border (scale = % of its original size) after a certain amount of time (in seconds) after the game starts",
"After start_time has passed, the border will resize for the given duration time (in seconds)",
"This only works if the arena has the type WORLD and if the match world has a vanilla world border (command: /minecraft:worldborder)"
}
)
public static boolean world_border_resize_enabled = false;
@Config public static int world_border_resize_start_time = 600;
@Config public static int world_border_resize_duration = 60;
@Config public static int world_border_resize_scale = 50;

// ===== PLACEHOLDER API
@SectionTitle(title = "PLACEHOLDER API")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package me.metallicgoat.tweaksaddon.tweaks.misc;

import de.marcely.bedwars.api.arena.Arena;
import de.marcely.bedwars.api.arena.ArenaPersistentStorage;
import de.marcely.bedwars.api.arena.ArenaStatus;
import de.marcely.bedwars.api.arena.RegenerationType;
import de.marcely.bedwars.api.event.arena.ArenaStatusChangeEvent;
import de.marcely.bedwars.api.event.arena.ArenaUnloadEvent;
import de.marcely.bedwars.api.event.arena.RoundStartEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import me.metallicgoat.tweaksaddon.MBedwarsTweaksPlugin;
import me.metallicgoat.tweaksaddon.config.MainConfig;
import org.bukkit.Bukkit;
import org.bukkit.WorldBorder;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.scheduler.BukkitTask;

public class WorldBorderResize implements Listener {

private static final String KEY_ORIGINAL_SIZE = "tweaks:world_border_original_size";

private Map<Arena, BukkitTask> activeTasks = new HashMap<>();

@EventHandler
public void onRoundStart(RoundStartEvent event) {
start(event.getArena());
}

@EventHandler(priority = EventPriority.MONITOR)
public void onRoundStop(ArenaStatusChangeEvent event) {
if (event.getNewStatus() != ArenaStatus.RUNNING)
cancel(event.getArena());
}

@EventHandler
public void onArenaUnload(ArenaUnloadEvent event) {
cancel(event.getArena());
}

private void start(Arena arena) {
if (!MainConfig.world_border_resize_enabled || arena.getRegenerationType() != RegenerationType.WORLD)
return;
if (arena.getGameWorld().getWorldBorder().getSize() >= 100_000) // probably vanilla border (disabled)
return;

cancel(arena);

final BukkitTask scheduler = Bukkit.getScheduler().runTaskLater(
MBedwarsTweaksPlugin.getInstance(),
() -> resize(arena),
20 * MainConfig.world_border_resize_start_time);

this.activeTasks.put(arena, scheduler);
}

private void resize(Arena arena) {
final WorldBorder border = arena.getGameWorld().getWorldBorder();

// store original size
{
final ArenaPersistentStorage storage = arena.getPersistentStorage();

storage.setSynchronizedFlag(KEY_ORIGINAL_SIZE, false);
storage.set(KEY_ORIGINAL_SIZE, border.getSize());
storage.saveAsync();
}

// tell bukkit to resize
border.setSize(
border.getSize()*(MainConfig.world_border_resize_scale/100D),
MainConfig.world_border_resize_duration);

// clean up
this.activeTasks.remove(arena);
}

private void cancel(Arena arena) {
if (!MainConfig.world_border_resize_enabled)
return;

// cancel scheduler
{
final BukkitTask scheduler = this.activeTasks.remove(arena);

if (scheduler != null)
scheduler.cancel();
}

// restore border size
{
final ArenaPersistentStorage storage = arena.getPersistentStorage();
final Optional<Double> originalSize = storage.getDouble(KEY_ORIGINAL_SIZE);

if (originalSize.isPresent()) {
final WorldBorder border = arena.getGameWorld().getWorldBorder();

border.setSize(originalSize.get(), 1);
storage.remove(KEY_ORIGINAL_SIZE);
}
}
}
}
Loading