-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from MrEAlderson/master
Feature: World border shrinking after x time passed
- Loading branch information
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/main/java/me/metallicgoat/tweaksaddon/tweaks/misc/WorldBorderResize.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |