769 lines
29 KiB
Java
769 lines
29 KiB
Java
package games.dmg.spigottyrant;
|
|
|
|
import java.util.HashSet;
|
|
import java.util.Optional;
|
|
import java.util.Set;
|
|
import java.util.UUID;
|
|
import org.bukkit.ChatColor;
|
|
import org.bukkit.Color;
|
|
import org.bukkit.Location;
|
|
import org.bukkit.Particle;
|
|
import org.bukkit.Server;
|
|
import org.bukkit.World;
|
|
import org.bukkit.attribute.Attribute;
|
|
import org.bukkit.entity.Entity;
|
|
import org.bukkit.entity.LivingEntity;
|
|
import org.bukkit.entity.Mob;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.entity.Projectile;
|
|
import org.bukkit.entity.Zombie;
|
|
import org.bukkit.event.EventHandler;
|
|
import org.bukkit.event.EventPriority;
|
|
import org.bukkit.event.Listener;
|
|
import org.bukkit.event.entity.CreatureSpawnEvent;
|
|
import org.bukkit.event.entity.EntityCombustEvent;
|
|
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
|
import org.bukkit.event.entity.EntityDeathEvent;
|
|
import org.bukkit.event.entity.EntityTargetLivingEntityEvent;
|
|
import org.bukkit.event.entity.PlayerDeathEvent;
|
|
import org.bukkit.event.block.BlockBreakEvent;
|
|
import org.bukkit.event.block.BlockPlaceEvent;
|
|
import org.bukkit.event.player.PlayerJoinEvent;
|
|
import org.bukkit.event.player.PlayerMoveEvent;
|
|
import org.bukkit.event.player.PlayerQuitEvent;
|
|
import org.bukkit.event.player.PlayerRespawnEvent;
|
|
import org.bukkit.event.player.PlayerTeleportEvent;
|
|
import org.bukkit.event.world.ChunkUnloadEvent;
|
|
import org.bukkit.event.world.EntitiesLoadEvent;
|
|
import org.bukkit.event.world.EntitiesUnloadEvent;
|
|
|
|
public final class RoleArenaController implements Listener, Runnable {
|
|
private static final String BOSS_TAG = "spigottyrant-role-arena-boss";
|
|
private final TyrantStateManager stateManager;
|
|
private final Server server;
|
|
private final ArenaLocationStore locations;
|
|
private final PluginSettings settings;
|
|
private final java.util.function.BiConsumer<Zombie, PluginSettings> configureBossAttributes;
|
|
private final VigilanteArenaSuccessionService vigilanteSuccession =
|
|
new VigilanteArenaSuccessionService();
|
|
private final TyrantArenaSuccessionService tyrantSuccession =
|
|
new TyrantArenaSuccessionService();
|
|
private final RoleArenaBarrier barrier = new RoleArenaBarrier();
|
|
private final Set<UUID> relocating = new HashSet<>();
|
|
private UUID bossId;
|
|
private Zombie bossEntity;
|
|
private UUID challengerId;
|
|
private Zombie spawningBoss;
|
|
private int spawnRetrySweeps;
|
|
private boolean spawnWarning;
|
|
private ArenaRole previousOpenRole;
|
|
private ArenaRole bossRole;
|
|
private ArenaRole warnedMissingRole;
|
|
private ArenaLocation observedLocation;
|
|
private boolean reconciledLoadedBosses;
|
|
|
|
public RoleArenaController(
|
|
TyrantStateManager stateManager,
|
|
Server server,
|
|
ArenaLocationStore locations,
|
|
PluginSettings settings
|
|
) {
|
|
this(stateManager, server, locations, settings, RoleArenaController::configureBossAttributes);
|
|
}
|
|
|
|
// Isolate registry-backed attributes from the lifecycle adapter's headless tests.
|
|
RoleArenaController(
|
|
TyrantStateManager stateManager, Server server, ArenaLocationStore locations,
|
|
PluginSettings settings,
|
|
java.util.function.BiConsumer<Zombie, PluginSettings> configureBossAttributes
|
|
) {
|
|
this.stateManager = stateManager;
|
|
this.server = server;
|
|
this.locations = locations;
|
|
this.settings = settings;
|
|
this.configureBossAttributes = configureBossAttributes;
|
|
}
|
|
|
|
@Override
|
|
public void run() {
|
|
if (spawnRetrySweeps > 0) {
|
|
spawnRetrySweeps--;
|
|
}
|
|
barrier.restorePending();
|
|
if (!reconciledLoadedBosses) {
|
|
server.getWorlds().forEach(world -> removeStaleBosses(world.getEntities()));
|
|
reconciledLoadedBosses = true;
|
|
}
|
|
Optional<ArenaRole> openRole = RoleArenaPolicy.openRole(stateManager.game());
|
|
Optional<ArenaLocation> configured = locations.location();
|
|
if (configured.isEmpty()) {
|
|
closeFight();
|
|
observedLocation = null;
|
|
warnIfOpenWithoutLocation(openRole);
|
|
previousOpenRole = null;
|
|
return;
|
|
}
|
|
ArenaLocation arena = configured.orElseThrow();
|
|
if (observedLocation != null && !observedLocation.equals(arena)) {
|
|
closeFight();
|
|
}
|
|
observedLocation = arena;
|
|
World world = server.getWorld(arena.worldName());
|
|
if (world == null) {
|
|
closeFight();
|
|
warnIfOpenWithoutLocation(openRole);
|
|
previousOpenRole = null;
|
|
return;
|
|
}
|
|
|
|
warnedMissingRole = null;
|
|
ArenaRole role = openRole.orElse(null);
|
|
if (role != previousOpenRole) {
|
|
closeFight();
|
|
if (role != null) {
|
|
announceVacancy(arena, role);
|
|
}
|
|
}
|
|
previousOpenRole = role;
|
|
drawBoundary(world, arena, role);
|
|
int chunkX = ((int) Math.floor(arena.x())) >> 4;
|
|
int chunkZ = ((int) Math.floor(arena.z())) >> 4;
|
|
if (!world.isChunkLoaded(chunkX, chunkZ)
|
|
|| !world.getChunkAt(chunkX, chunkZ).isEntitiesLoaded()) {
|
|
closeFight();
|
|
return;
|
|
}
|
|
removeOtherMobs(world, arena);
|
|
if (role == null || !hasNearbyPlayer(world, arena)) {
|
|
closeFight();
|
|
return;
|
|
}
|
|
|
|
Zombie boss = boss(world);
|
|
if (boss == null || bossRole != role) {
|
|
closeFight();
|
|
if (spawnRetrySweeps > 0) {
|
|
return;
|
|
}
|
|
boss = spawnBoss(world, arena, role);
|
|
}
|
|
if (boss == null) {
|
|
return;
|
|
}
|
|
boss.setFireTicks(0);
|
|
if (!contains(arena, boss.getLocation())) {
|
|
boss.teleport(toBukkit(world, arena));
|
|
}
|
|
Player challenger = challengerId == null ? null : server.getPlayer(challengerId);
|
|
if (challenger == null || !challenger.isOnline() || challenger.isDead()
|
|
|| !withinBossRange(arena, challenger.getLocation())
|
|
|| !contains(arena, challenger.getLocation())
|
|
|| !RoleArenaPolicy.isEligible(
|
|
stateManager.game(), role, playerState(challenger)
|
|
)) {
|
|
if (challengerId != null) {
|
|
resetFight();
|
|
} else {
|
|
ArenaBossState.idle(
|
|
boss, toBukkit(world, arena), settings.vigilanteBossHealth()
|
|
);
|
|
}
|
|
} else {
|
|
ArenaBossState.engage(boss, challenger);
|
|
}
|
|
for (Player player : world.getPlayers()) {
|
|
enforcePosition(player, null, player.getLocation());
|
|
}
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
|
public void onMove(PlayerMoveEvent event) {
|
|
if (event.getTo() != null && movedHorizontally(event.getFrom(), event.getTo())
|
|
&& enforcePosition(event.getPlayer(), event.getFrom(), event.getTo())) {
|
|
event.setCancelled(true);
|
|
}
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
|
public void onTeleport(PlayerTeleportEvent event) {
|
|
if (event.getTo() != null
|
|
&& enforcePosition(event.getPlayer(), event.getFrom(), event.getTo())) {
|
|
event.setCancelled(true);
|
|
}
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.HIGHEST)
|
|
public void onRespawn(PlayerRespawnEvent event) {
|
|
Optional<ArenaLocation> configured = locations.location();
|
|
if (configured.isPresent() && contains(
|
|
configured.orElseThrow(), event.getRespawnLocation())) {
|
|
event.setRespawnLocation(outside(configured.orElseThrow(), event.getRespawnLocation()));
|
|
}
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR)
|
|
public void onPlayerDeath(PlayerDeathEvent event) {
|
|
if (event.getEntity().getUniqueId().equals(challengerId)) {
|
|
resetFight();
|
|
}
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR)
|
|
public void onQuit(PlayerQuitEvent event) {
|
|
if (event.getPlayer().getUniqueId().equals(challengerId)) {
|
|
resetFight();
|
|
}
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR)
|
|
public void onJoin(PlayerJoinEvent event) {
|
|
Optional<ArenaRole> role = RoleArenaPolicy.openRole(stateManager.game());
|
|
if (role.isEmpty()) {
|
|
return;
|
|
}
|
|
PlayerState state = playerState(event.getPlayer());
|
|
locations.location().filter(location -> RoleArenaPolicy.isEligible(
|
|
stateManager.game(), role.orElseThrow(), state
|
|
)).ifPresent(location -> event.getPlayer().sendMessage(
|
|
vacancyMessage(location, role.orElseThrow())
|
|
));
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
|
public void onChunkUnload(ChunkUnloadEvent event) {
|
|
Zombie current = bossEntity;
|
|
boolean bossChunk = current != null && current.getWorld() == event.getWorld()
|
|
&& (current.getLocation().getBlockX() >> 4) == event.getChunk().getX()
|
|
&& (current.getLocation().getBlockZ() >> 4) == event.getChunk().getZ();
|
|
if (bossChunk || barrier.occupies(event.getChunk())) {
|
|
// Restore walls while block chunks are still loaded, before entities unload.
|
|
closeFight();
|
|
}
|
|
}
|
|
|
|
@EventHandler
|
|
public void onEntitiesLoad(EntitiesLoadEvent event) {
|
|
// Event payloads may not yet be visible through world/server entity lookups.
|
|
removeStaleBosses(event.getEntities());
|
|
}
|
|
|
|
@EventHandler
|
|
public void onEntitiesUnload(EntitiesUnloadEvent event) {
|
|
removeStaleBosses(event.getEntities());
|
|
if (event.getEntities().stream().anyMatch(entity -> entity.getUniqueId().equals(bossId))) {
|
|
closeFight();
|
|
}
|
|
}
|
|
|
|
private void removeStaleBosses(Iterable<Entity> entities) {
|
|
for (Entity entity : entities) {
|
|
if (entity.getScoreboardTags().contains(BOSS_TAG) && !entity.getUniqueId().equals(bossId)) {
|
|
entity.setPersistent(false);
|
|
entity.remove();
|
|
}
|
|
}
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
|
public void onCreatureSpawn(CreatureSpawnEvent event) {
|
|
Optional<ArenaLocation> configured = locations.location();
|
|
if (event.getEntity() != spawningBoss && configured.isPresent()
|
|
&& contains(configured.orElseThrow(), event.getLocation())) {
|
|
event.setCancelled(true);
|
|
}
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
|
public void onBlockPlace(BlockPlaceEvent event) {
|
|
if (protects(event.getBlock().getLocation())) {
|
|
event.setCancelled(true);
|
|
event.getPlayer().sendMessage(ChatColor.RED
|
|
+ "Blocks cannot be changed during an arena fight.");
|
|
}
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
|
public void onBlockBreak(BlockBreakEvent event) {
|
|
if (protects(event.getBlock().getLocation())) {
|
|
event.setCancelled(true);
|
|
event.getPlayer().sendMessage(ChatColor.RED
|
|
+ "Blocks cannot be changed during an arena fight.");
|
|
}
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
|
public void onCombust(EntityCombustEvent event) {
|
|
if (event.getEntity().getUniqueId().equals(bossId)) {
|
|
event.setCancelled(true);
|
|
}
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
|
public void onTarget(EntityTargetLivingEntityEvent event) {
|
|
if (!event.getEntity().getUniqueId().equals(bossId)) {
|
|
return;
|
|
}
|
|
Entity target = event.getTarget();
|
|
if (target != null && (challengerId == null
|
|
|| !target.getUniqueId().equals(challengerId))) {
|
|
event.setCancelled(true);
|
|
}
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
|
public void onDamage(EntityDamageByEntityEvent event) {
|
|
if (!event.getEntity().getUniqueId().equals(bossId)) {
|
|
return;
|
|
}
|
|
Player attacker = attackingPlayer(event.getDamager());
|
|
if (attacker == null || challengerId == null
|
|
|| !attacker.getUniqueId().equals(challengerId)) {
|
|
event.setCancelled(true);
|
|
}
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR)
|
|
public void onBossDeath(EntityDeathEvent event) {
|
|
if (!event.getEntity().getUniqueId().equals(bossId)) {
|
|
return;
|
|
}
|
|
UUID defeatedBoss = bossId;
|
|
Zombie defeatedEntity = bossEntity;
|
|
ArenaRole defeatedRole = bossRole;
|
|
bossId = null;
|
|
bossEntity = null;
|
|
bossRole = null;
|
|
Player killer = event.getEntity().getKiller();
|
|
if (killer != null && challengerId != null && defeatedRole != null
|
|
&& killer.getUniqueId().equals(challengerId)
|
|
&& RoleArenaPolicy.isEligible(
|
|
stateManager.game(), defeatedRole, playerState(killer)
|
|
)) {
|
|
assignRole(challengerId, defeatedRole);
|
|
return;
|
|
}
|
|
bossId = defeatedBoss;
|
|
bossEntity = defeatedEntity;
|
|
bossRole = defeatedRole;
|
|
resetFight();
|
|
}
|
|
|
|
public void close() {
|
|
closeFight();
|
|
}
|
|
|
|
private boolean enforcePosition(Player player, Location from, Location to) {
|
|
Optional<ArenaLocation> configured = locations.location();
|
|
if (configured.isEmpty() || !player.isOnline() || player.isDead()
|
|
|| relocating.contains(player.getUniqueId())) {
|
|
return false;
|
|
}
|
|
ArenaLocation arena = configured.orElseThrow();
|
|
boolean inside = contains(arena, to);
|
|
if (player.getUniqueId().equals(challengerId)) {
|
|
if (!inside || !withinBossRange(arena, to)) {
|
|
resetFight();
|
|
}
|
|
return false;
|
|
}
|
|
if (!inside) {
|
|
return false;
|
|
}
|
|
PlayerState state = playerState(player);
|
|
if (bossId == null && withinBossRange(arena, to)) {
|
|
Optional<ArenaRole> openRole = RoleArenaPolicy.openRole(stateManager.game());
|
|
if (openRole.isPresent() && RoleArenaPolicy.isEligible(
|
|
stateManager.game(), openRole.orElseThrow(), state)) {
|
|
// Allow an eligible arrival to commit; the sweep summons/admits, not a
|
|
// proposed teleport which another listener could still cancel.
|
|
return false;
|
|
}
|
|
}
|
|
ArenaRole role = bossRole;
|
|
if (challengerId == null && bossId != null && role != null && withinBossRange(arena, to)
|
|
&& RoleArenaPolicy.isEligible(stateManager.game(), role, state)) {
|
|
World world = server.getWorld(arena.worldName());
|
|
Zombie boss = world == null ? null : boss(world);
|
|
if (boss == null || !barrier.build(
|
|
world, arena, settings.vigilanteArenaRadiusBlocks(),
|
|
settings.arenaBarrierHeightBlocks())) {
|
|
return false;
|
|
}
|
|
challengerId = player.getUniqueId();
|
|
boss.setHealth(settings.vigilanteBossHealth());
|
|
boss.teleport(toBukkit(world, arena));
|
|
ArenaBossState.engage(boss, player);
|
|
player.sendMessage(ChatColor.GOLD + "Defeat the arena boss to become "
|
|
+ readable(role) + "!");
|
|
return false;
|
|
}
|
|
bounce(player, from, to, arena);
|
|
return true;
|
|
}
|
|
|
|
private void bounce(Player player, Location from, Location attempted, ArenaLocation arena) {
|
|
Location destination = from != null && !contains(arena, from)
|
|
? from.clone() : outside(arena, attempted);
|
|
relocating.add(player.getUniqueId());
|
|
try {
|
|
player.teleport(destination, PlayerTeleportEvent.TeleportCause.PLUGIN);
|
|
} finally {
|
|
relocating.remove(player.getUniqueId());
|
|
}
|
|
player.sendMessage(ChatColor.RED + "Only the active challenger may enter the arena.");
|
|
}
|
|
|
|
private void resetFight() {
|
|
challengerId = null;
|
|
barrier.clear();
|
|
Optional<ArenaLocation> configured = locations.location();
|
|
if (configured.isEmpty()) {
|
|
return;
|
|
}
|
|
World world = server.getWorld(configured.orElseThrow().worldName());
|
|
Zombie boss = world == null ? null : boss(world);
|
|
if (boss != null && !boss.isDead()) {
|
|
ArenaBossState.idle(
|
|
boss, toBukkit(world, configured.orElseThrow()),
|
|
settings.vigilanteBossHealth()
|
|
);
|
|
} else {
|
|
bossId = null;
|
|
}
|
|
}
|
|
|
|
private void closeFight() {
|
|
challengerId = null;
|
|
barrier.clear();
|
|
Zombie retiring = bossEntity;
|
|
bossEntity = null;
|
|
bossId = null;
|
|
bossRole = null;
|
|
if (retiring != null) {
|
|
bossParticles(retiring.getLocation());
|
|
retiring.remove();
|
|
}
|
|
}
|
|
|
|
private Zombie spawnBoss(World world, ArenaLocation arena, ArenaRole role) {
|
|
try {
|
|
Zombie boss = world.spawn(toBukkit(world, arena), Zombie.class, zombie -> {
|
|
spawningBoss = zombie;
|
|
zombie.setPersistent(false);
|
|
zombie.addScoreboardTag(BOSS_TAG);
|
|
zombie.setAdult();
|
|
zombie.setCustomName(role == ArenaRole.TYRANT
|
|
? ChatColor.RED + "Tyrant's Trial"
|
|
: ChatColor.BLUE + "Vigilante's Trial");
|
|
zombie.setCustomNameVisible(true);
|
|
zombie.setRemoveWhenFarAway(false);
|
|
zombie.setCanPickupItems(false);
|
|
zombie.setAI(false);
|
|
configureBossAttributes.accept(zombie, settings);
|
|
zombie.setHealth(settings.vigilanteBossHealth());
|
|
});
|
|
spawningBoss = boss;
|
|
// Reassert after spawn listeners and never own a cancelled/removed candidate.
|
|
boss.setPersistent(false);
|
|
if (!boss.isValid() || boss.isDead()) {
|
|
boss.remove();
|
|
spawnFailed();
|
|
return null;
|
|
}
|
|
spawnWarning = false;
|
|
bossEntity = boss;
|
|
bossId = boss.getUniqueId();
|
|
bossRole = role;
|
|
bossParticles(boss.getLocation());
|
|
return boss;
|
|
} catch (RuntimeException exception) {
|
|
bossEntity = null;
|
|
bossId = null;
|
|
bossRole = null;
|
|
if (spawningBoss != null) {
|
|
spawningBoss.remove();
|
|
}
|
|
spawnFailed();
|
|
return null;
|
|
} finally {
|
|
spawningBoss = null;
|
|
}
|
|
}
|
|
|
|
private void spawnFailed() {
|
|
spawnRetrySweeps = 10; // Five seconds at the controller's ten-tick cadence.
|
|
if (!spawnWarning) {
|
|
server.getLogger().warning("Tyrant arena boss spawn failed or was cancelled; retrying in five seconds.");
|
|
spawnWarning = true;
|
|
}
|
|
}
|
|
|
|
private Zombie boss(World world) {
|
|
return bossEntity != null && bossEntity.getWorld().equals(world)
|
|
&& bossEntity.isValid() && !bossEntity.isDead() ? bossEntity : null;
|
|
}
|
|
|
|
void assignRole(UUID playerId, ArenaRole role) {
|
|
LifecycleState assigned;
|
|
try {
|
|
assigned = role == ArenaRole.TYRANT
|
|
? tyrantSuccession.claim(stateManager.snapshot(), playerId)
|
|
: vigilanteSuccession.claim(stateManager.snapshot(), playerId);
|
|
} catch (IllegalStateException exception) {
|
|
resetFight();
|
|
return;
|
|
}
|
|
stateManager.replaceState(assigned);
|
|
stateManager.saveIfDirty();
|
|
challengerId = null;
|
|
barrier.clear();
|
|
Player winner = server.getPlayer(playerId);
|
|
moveWinnerOutside(winner);
|
|
if (role == ArenaRole.VIGILANTE) {
|
|
if (winner != null) {
|
|
winner.sendMessage(ChatColor.YELLOW
|
|
+ "You defeated the arena boss and became the Vigilante!");
|
|
}
|
|
return;
|
|
}
|
|
String name = Optional.ofNullable(winner)
|
|
.map(Player::getName).orElse(playerId.toString());
|
|
String message = ChatColor.YELLOW + name + " defeated the arena boss and is the "
|
|
+ readable(role) + "!";
|
|
server.getOnlinePlayers().forEach(player -> player.sendMessage(message));
|
|
}
|
|
|
|
private void moveWinnerOutside(Player winner) {
|
|
Optional<ArenaLocation> configured = locations.location();
|
|
if (winner == null || configured.isEmpty()) {
|
|
return;
|
|
}
|
|
ArenaLocation arena = configured.orElseThrow();
|
|
Location winnerLocation = winner.getLocation();
|
|
if (!contains(arena, winnerLocation)) {
|
|
return;
|
|
}
|
|
relocating.add(winner.getUniqueId());
|
|
try {
|
|
winner.teleport(
|
|
outside(arena, winnerLocation), PlayerTeleportEvent.TeleportCause.PLUGIN
|
|
);
|
|
} finally {
|
|
relocating.remove(winner.getUniqueId());
|
|
}
|
|
}
|
|
|
|
private void announceVacancy(ArenaLocation arena, ArenaRole role) {
|
|
String message = vacancyMessage(arena, role);
|
|
for (Player player : server.getOnlinePlayers()) {
|
|
PlayerState state = playerState(player);
|
|
if (RoleArenaPolicy.isEligible(stateManager.game(), role, state)) {
|
|
player.sendMessage(message);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void warnIfOpenWithoutLocation(Optional<ArenaRole> openRole) {
|
|
if (openRole.isEmpty()) {
|
|
warnedMissingRole = null;
|
|
return;
|
|
}
|
|
ArenaRole role = openRole.orElseThrow();
|
|
if (role == warnedMissingRole) {
|
|
return;
|
|
}
|
|
warnedMissingRole = role;
|
|
server.getConsoleSender().sendMessage(ChatColor.RED
|
|
+ "The " + readable(role)
|
|
+ " role is vacant, but no valid arena location is configured. "
|
|
+ "Use /tyrantadmin arena set.");
|
|
for (Player player : server.getOnlinePlayers()) {
|
|
if (player.hasPermission("spigottyrant.admin")) {
|
|
player.sendMessage(ChatColor.RED
|
|
+ "The shared role arena location is not configured.");
|
|
}
|
|
}
|
|
}
|
|
|
|
private String vacancyMessage(ArenaLocation arena, ArenaRole role) {
|
|
return ChatColor.YELLOW + "The " + readable(role)
|
|
+ " position is open! Challenge the boss in " + arena.display() + ".";
|
|
}
|
|
|
|
private void removeOtherMobs(World world, ArenaLocation arena) {
|
|
Location center = toBukkit(world, arena);
|
|
double radius = settings.vigilanteArenaRadiusBlocks();
|
|
for (Entity entity : world.getNearbyEntities(center, radius, world.getMaxHeight(), radius)) {
|
|
if (entity instanceof Mob && !entity.getUniqueId().equals(bossId)
|
|
&& contains(arena, entity.getLocation())) {
|
|
entity.remove();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void drawBoundary(World world, ArenaLocation arena, ArenaRole role) {
|
|
double radius = settings.vigilanteArenaRadiusBlocks();
|
|
Color color = boundaryColor(role);
|
|
Particle.DustOptions dust = color == null
|
|
? null : new Particle.DustOptions(color, 1.25F);
|
|
int[] heightOffsets = boundaryHeightOffsets(
|
|
barrier.active(), settings.arenaBarrierHeightBlocks()
|
|
);
|
|
for (int heightOffset : heightOffsets) {
|
|
for (int index = 0; index < 48; index++) {
|
|
double angle = 2.0 * Math.PI * index / 48.0;
|
|
Location point = new Location(
|
|
world,
|
|
arena.x() + Math.cos(angle) * radius,
|
|
arena.y() + heightOffset,
|
|
arena.z() + Math.sin(angle) * radius
|
|
);
|
|
if (dust == null) {
|
|
world.spawnParticle(
|
|
Particle.END_ROD, point, 1, 0.0, 0.0, 0.0, 0.0
|
|
);
|
|
} else {
|
|
world.spawnParticle(
|
|
Particle.DUST, point, 1, 0.0, 0.0, 0.0, 0.0, dust
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void bossParticles(Location location) {
|
|
World world = location.getWorld();
|
|
if (world != null && world.isChunkLoaded(location.getBlockX() >> 4, location.getBlockZ() >> 4)) {
|
|
world.spawnParticle(Particle.PORTAL, location.clone().add(0, 1, 0),
|
|
60, 0.6, 1.0, 0.6, 0.05);
|
|
}
|
|
}
|
|
|
|
private boolean hasNearbyPlayer(World world, ArenaLocation arena) {
|
|
return world.getPlayers().stream().anyMatch(player -> player.isOnline() && !player.isDead()
|
|
&& withinBossRange(arena, player.getLocation()));
|
|
}
|
|
|
|
private boolean withinBossRange(ArenaLocation arena, Location location) {
|
|
World world = location.getWorld();
|
|
double range = settings.vigilanteArenaRadiusBlocks() + 8.0;
|
|
return world != null && world.getName().equals(arena.worldName())
|
|
&& location.distanceSquared(toBukkit(world, arena)) <= range * range;
|
|
}
|
|
|
|
private boolean protects(Location location) {
|
|
Optional<ArenaLocation> configured = locations.location();
|
|
World world = location.getWorld();
|
|
return configured.isPresent() && world != null
|
|
&& ArenaBuildProtection.isProtected(
|
|
barrier.active(), configured.orElseThrow(),
|
|
settings.vigilanteArenaRadiusBlocks(), world.getName(),
|
|
location.getX(), location.getY(), location.getZ()
|
|
);
|
|
}
|
|
|
|
private boolean contains(ArenaLocation arena, Location location) {
|
|
World world = location.getWorld();
|
|
return world != null && ArenaGeometry.contains(
|
|
arena, settings.vigilanteArenaRadiusBlocks(), world.getName(),
|
|
location.getX(), location.getY(), location.getZ()
|
|
);
|
|
}
|
|
|
|
private Location outside(ArenaLocation arena, Location attempted) {
|
|
ArenaPoint point = ArenaGeometry.outside(
|
|
arena, settings.vigilanteArenaRadiusBlocks(), attempted.getX(),
|
|
attempted.getY(), attempted.getZ()
|
|
);
|
|
World world = server.getWorld(arena.worldName());
|
|
if (world == null) {
|
|
return attempted;
|
|
}
|
|
int x = (int) Math.floor(point.x());
|
|
int z = (int) Math.floor(point.z());
|
|
int preferredY = (int) Math.floor(point.y());
|
|
for (int offset = 0; offset <= 8; offset++) {
|
|
int[] candidates = offset == 0
|
|
? new int[] {preferredY} : new int[] {preferredY + offset, preferredY - offset};
|
|
for (int y : candidates) {
|
|
if (y > world.getMinHeight() && y + 1 < world.getMaxHeight()
|
|
&& world.getBlockAt(x, y, z).isPassable()
|
|
&& world.getBlockAt(x, y + 1, z).isPassable()
|
|
&& !world.getBlockAt(x, y - 1, z).isPassable()) {
|
|
return new Location(world, x + 0.5, y, z + 0.5,
|
|
attempted.getYaw(), attempted.getPitch());
|
|
}
|
|
}
|
|
}
|
|
int surfaceY = Math.min(
|
|
world.getMaxHeight() - 2,
|
|
Math.max(world.getMinHeight() + 1, world.getHighestBlockYAt(x, z) + 1)
|
|
);
|
|
return new Location(world, x + 0.5, surfaceY, z + 0.5,
|
|
attempted.getYaw(), attempted.getPitch());
|
|
}
|
|
|
|
private PlayerState playerState(Player player) {
|
|
PlayerState existing = stateManager.players().get(player.getUniqueId());
|
|
return existing == null
|
|
? stateManager.player(player.getUniqueId(), player.getName()) : existing;
|
|
}
|
|
|
|
private static Location toBukkit(World world, ArenaLocation arena) {
|
|
return new Location(
|
|
world, arena.x(), arena.y(), arena.z(), arena.yaw(), arena.pitch()
|
|
);
|
|
}
|
|
|
|
private static Player attackingPlayer(Entity damager) {
|
|
if (damager instanceof Player player) {
|
|
return player;
|
|
}
|
|
if (damager instanceof Projectile projectile
|
|
&& projectile.getShooter() instanceof Player player) {
|
|
return player;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private static void configureBossAttributes(Zombie zombie, PluginSettings settings) {
|
|
setAttribute(zombie, Attribute.MAX_HEALTH, settings.vigilanteBossHealth());
|
|
setAttribute(zombie, Attribute.ATTACK_DAMAGE, settings.vigilanteBossDamage());
|
|
setAttribute(zombie, Attribute.ARMOR, settings.vigilanteBossArmor());
|
|
}
|
|
|
|
private static void setAttribute(Zombie zombie, Attribute attribute, double value) {
|
|
if (zombie.getAttribute(attribute) != null) {
|
|
zombie.getAttribute(attribute).setBaseValue(value);
|
|
}
|
|
}
|
|
|
|
static int[] boundaryHeightOffsets(boolean fightActive, int wallHeight) {
|
|
if (!fightActive) {
|
|
return new int[] {1};
|
|
}
|
|
int count = (wallHeight - 2) / 3 + 1;
|
|
int[] offsets = new int[count];
|
|
for (int index = 0; index < count; index++) {
|
|
offsets[index] = 1 + index * 3;
|
|
}
|
|
return offsets;
|
|
}
|
|
|
|
static Color boundaryColor(ArenaRole role) {
|
|
if (role == null) {
|
|
return null;
|
|
}
|
|
return role == ArenaRole.TYRANT ? Color.RED : Color.BLUE;
|
|
}
|
|
|
|
private static String readable(ArenaRole role) {
|
|
return role == ArenaRole.TYRANT ? "Tyrant" : "Vigilante";
|
|
}
|
|
|
|
private static boolean movedHorizontally(Location from, Location to) {
|
|
return from.getWorld() != to.getWorld()
|
|
|| from.getX() != to.getX() || from.getZ() != to.getZ();
|
|
}
|
|
}
|