fix(arena): improve containment and boss idle
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package games.dmg.spigottyrant;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Zombie;
|
||||
|
||||
final class ArenaBossState {
|
||||
private ArenaBossState() {
|
||||
}
|
||||
|
||||
static void idle(Zombie boss, Location center, double health) {
|
||||
boss.setAI(false);
|
||||
boss.setTarget(null);
|
||||
boss.setHealth(health);
|
||||
boss.teleport(center);
|
||||
}
|
||||
|
||||
static void engage(Zombie boss, Player challenger) {
|
||||
boss.setAI(true);
|
||||
boss.setTarget(challenger);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package games.dmg.spigottyrant;
|
||||
|
||||
final class ArenaBuildProtection {
|
||||
private ArenaBuildProtection() {
|
||||
}
|
||||
|
||||
static boolean isProtected(
|
||||
boolean fightActive,
|
||||
ArenaLocation center,
|
||||
double radius,
|
||||
String worldName,
|
||||
double x,
|
||||
double y,
|
||||
double z
|
||||
) {
|
||||
return fightActive && ArenaGeometry.contains(
|
||||
center, radius, worldName, x, y, z
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ public record PluginSettings(
|
||||
double tyrantRangeBlocks,
|
||||
double followerRangeBlocks,
|
||||
double vigilanteArenaRadiusBlocks,
|
||||
int arenaBarrierHeightBlocks,
|
||||
double vigilanteBossHealth,
|
||||
double vigilanteBossDamage,
|
||||
double vigilanteBossArmor,
|
||||
@@ -53,6 +54,11 @@ public record PluginSettings(
|
||||
requireFiniteRange(
|
||||
vigilanteArenaRadiusBlocks, 1.0, 512.0, "vigilante-arena-radius-blocks"
|
||||
);
|
||||
if (arenaBarrierHeightBlocks < 3 || arenaBarrierHeightBlocks > 128) {
|
||||
throw new IllegalArgumentException(
|
||||
"arena-barrier-height-blocks must be between 3 and 128"
|
||||
);
|
||||
}
|
||||
requireFiniteRange(vigilanteBossHealth, 1.0, 1024.0, "vigilante-boss-health");
|
||||
requireFiniteRange(vigilanteBossDamage, 0.1, 2048.0, "vigilante-boss-damage");
|
||||
requireFiniteRange(vigilanteBossArmor, 0.0, 30.0, "vigilante-boss-armor");
|
||||
@@ -100,6 +106,7 @@ public record PluginSettings(
|
||||
decimal(values, "tyrant-range-blocks", 50.0),
|
||||
decimal(values, "follower-range-blocks", 50.0),
|
||||
decimal(values, "vigilante-arena-radius-blocks", 10.0),
|
||||
integer(values, "arena-barrier-height-blocks", 12),
|
||||
decimal(values, "vigilante-boss-health", 80.0),
|
||||
decimal(values, "vigilante-boss-damage", 8.0),
|
||||
decimal(values, "vigilante-boss-armor", 10.0),
|
||||
|
||||
+9
-4
@@ -8,22 +8,27 @@ import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
|
||||
final class VigilanteArenaBarrier {
|
||||
final class RoleArenaBarrier {
|
||||
private final Map<Location, BlockData> replaced = new LinkedHashMap<>();
|
||||
private boolean built;
|
||||
|
||||
void build(World world, ArenaLocation center, double radius) {
|
||||
void build(
|
||||
World world,
|
||||
ArenaLocation center,
|
||||
double radius,
|
||||
int wallHeight
|
||||
) {
|
||||
if (built) {
|
||||
return;
|
||||
}
|
||||
built = true;
|
||||
int samples = Math.max(64, (int) Math.ceil(2.0 * Math.PI * radius * 2.0));
|
||||
int baseY = (int) Math.floor(center.y());
|
||||
int baseY = (int) Math.floor(center.y()) - 1;
|
||||
for (int index = 0; index < samples; index++) {
|
||||
double angle = 2.0 * Math.PI * index / samples;
|
||||
int x = (int) Math.floor(center.x() + Math.cos(angle) * radius);
|
||||
int z = (int) Math.floor(center.z() + Math.sin(angle) * radius);
|
||||
for (int y = baseY; y < baseY + 3; y++) {
|
||||
for (int y = baseY; y < baseY + wallHeight; y++) {
|
||||
Block block = world.getBlockAt(x, y, z);
|
||||
Location location = block.getLocation();
|
||||
if (replaced.containsKey(location) || !isAir(block.getType())) {
|
||||
@@ -26,6 +26,8 @@ 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;
|
||||
@@ -42,7 +44,7 @@ public final class RoleArenaController implements Listener, Runnable {
|
||||
new VigilanteArenaSuccessionService();
|
||||
private final TyrantArenaSuccessionService tyrantSuccession =
|
||||
new TyrantArenaSuccessionService();
|
||||
private final VigilanteArenaBarrier barrier = new VigilanteArenaBarrier();
|
||||
private final RoleArenaBarrier barrier = new RoleArenaBarrier();
|
||||
private final Set<UUID> relocating = new HashSet<>();
|
||||
private UUID bossId;
|
||||
private UUID challengerId;
|
||||
@@ -121,10 +123,13 @@ public final class RoleArenaController implements Listener, Runnable {
|
||||
)) {
|
||||
if (challengerId != null) {
|
||||
resetFight();
|
||||
} else {
|
||||
ArenaBossState.idle(
|
||||
boss, toBukkit(world, arena), settings.vigilanteBossHealth()
|
||||
);
|
||||
}
|
||||
boss.setTarget(null);
|
||||
} else {
|
||||
boss.setTarget(challenger);
|
||||
ArenaBossState.engage(boss, challenger);
|
||||
}
|
||||
for (Player player : world.getPlayers()) {
|
||||
enforcePosition(player, null, player.getLocation());
|
||||
@@ -193,6 +198,24 @@ public final class RoleArenaController implements Listener, Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
@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)) {
|
||||
@@ -274,12 +297,15 @@ public final class RoleArenaController implements Listener, Runnable {
|
||||
challengerId = player.getUniqueId();
|
||||
World world = server.getWorld(arena.worldName());
|
||||
if (world != null) {
|
||||
barrier.build(world, arena, settings.vigilanteArenaRadiusBlocks());
|
||||
barrier.build(
|
||||
world, arena, settings.vigilanteArenaRadiusBlocks(),
|
||||
settings.arenaBarrierHeightBlocks()
|
||||
);
|
||||
Zombie boss = boss(world);
|
||||
if (boss != null) {
|
||||
boss.setHealth(settings.vigilanteBossHealth());
|
||||
boss.teleport(toBukkit(world, arena));
|
||||
boss.setTarget(player);
|
||||
ArenaBossState.engage(boss, player);
|
||||
}
|
||||
}
|
||||
player.sendMessage(ChatColor.GOLD + "Defeat the arena boss to become "
|
||||
@@ -312,9 +338,10 @@ public final class RoleArenaController implements Listener, Runnable {
|
||||
World world = server.getWorld(configured.orElseThrow().worldName());
|
||||
Zombie boss = world == null ? null : boss(world);
|
||||
if (boss != null && !boss.isDead()) {
|
||||
boss.setHealth(settings.vigilanteBossHealth());
|
||||
boss.teleport(toBukkit(world, configured.orElseThrow()));
|
||||
boss.setTarget(null);
|
||||
ArenaBossState.idle(
|
||||
boss, toBukkit(world, configured.orElseThrow()),
|
||||
settings.vigilanteBossHealth()
|
||||
);
|
||||
} else {
|
||||
bossId = null;
|
||||
}
|
||||
@@ -349,6 +376,7 @@ public final class RoleArenaController implements Listener, Runnable {
|
||||
zombie.setCustomNameVisible(true);
|
||||
zombie.setRemoveWhenFarAway(false);
|
||||
zombie.setCanPickupItems(false);
|
||||
zombie.setAI(false);
|
||||
zombie.addScoreboardTag(BOSS_TAG);
|
||||
setAttribute(zombie, Attribute.MAX_HEALTH, settings.vigilanteBossHealth());
|
||||
setAttribute(zombie, Attribute.ATTACK_DAMAGE, settings.vigilanteBossDamage());
|
||||
@@ -442,29 +470,46 @@ public final class RoleArenaController implements Listener, Runnable {
|
||||
}
|
||||
|
||||
private void drawBoundary(World world, ArenaLocation arena, ArenaRole role) {
|
||||
if (barrier.active()) {
|
||||
return;
|
||||
}
|
||||
double radius = settings.vigilanteArenaRadiusBlocks();
|
||||
Color color = boundaryColor(role);
|
||||
Particle.DustOptions dust = color == null
|
||||
? null : new Particle.DustOptions(color, 1.25F);
|
||||
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() + 1.0,
|
||||
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);
|
||||
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 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(
|
||||
@@ -535,6 +580,18 @@ public final class RoleArenaController implements Listener, Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
@@ -4,6 +4,7 @@ follower-range-blocks: 50
|
||||
|
||||
# Shared Tyrant and Vigilante arena and boss
|
||||
vigilante-arena-radius-blocks: 10
|
||||
arena-barrier-height-blocks: 12
|
||||
vigilante-boss-health: 80
|
||||
vigilante-boss-damage: 8
|
||||
vigilante-boss-armor: 10
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package games.dmg.spigottyrant;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Zombie;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
final class ArenaBossStateTest {
|
||||
@Test
|
||||
void idleBossIsCenteredHealedUntargetedAndFrozen() {
|
||||
Zombie boss = mock(Zombie.class);
|
||||
Location center = mock(Location.class);
|
||||
|
||||
ArenaBossState.idle(boss, center, 80.0);
|
||||
|
||||
verify(boss).setAI(false);
|
||||
verify(boss).setTarget(null);
|
||||
verify(boss).setHealth(80.0);
|
||||
verify(boss).teleport(center);
|
||||
}
|
||||
|
||||
@Test
|
||||
void challengerEntryEnablesBossAndSetsTarget() {
|
||||
Zombie boss = mock(Zombie.class);
|
||||
Player challenger = mock(Player.class);
|
||||
|
||||
ArenaBossState.engage(boss, challenger);
|
||||
|
||||
verify(boss).setAI(true);
|
||||
verify(boss).setTarget(challenger);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package games.dmg.spigottyrant;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
final class ArenaBuildProtectionTest {
|
||||
private static final ArenaLocation CENTER = new ArenaLocation(
|
||||
"world", 0.0, 64.0, 0.0, 0.0F, 0.0F
|
||||
);
|
||||
|
||||
@Test
|
||||
void protectsOnlyInsideConfiguredArenaDuringFight() {
|
||||
assertTrue(ArenaBuildProtection.isProtected(
|
||||
true, CENTER, 10.0, "world", 5.0, 70.0, 5.0
|
||||
));
|
||||
assertFalse(ArenaBuildProtection.isProtected(
|
||||
false, CENTER, 10.0, "world", 5.0, 70.0, 5.0
|
||||
));
|
||||
assertFalse(ArenaBuildProtection.isProtected(
|
||||
true, CENTER, 10.0, "world", 11.0, 70.0, 0.0
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ final class DefaultConfigurationTest {
|
||||
assertEquals(50.0, settings.tyrantRangeBlocks());
|
||||
assertEquals(Duration.ofDays(7), settings.optOutDuration());
|
||||
assertEquals(Duration.ofMinutes(1), settings.roleMaintenanceInterval());
|
||||
assertEquals(12, settings.arenaBarrierHeightBlocks());
|
||||
assertEquals("FISHING_ROD", settings.tamerItem().material());
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ final class PluginSettingsTest {
|
||||
assertEquals(Duration.ofMinutes(10), settings.fixerEffectDuration());
|
||||
assertEquals(Duration.ofHours(24), settings.rosterIntelligenceCooldown());
|
||||
assertEquals(10.0, settings.vigilanteArenaRadiusBlocks());
|
||||
assertEquals(12, settings.arenaBarrierHeightBlocks());
|
||||
assertEquals(80.0, settings.vigilanteBossHealth());
|
||||
assertEquals(8.0, settings.vigilanteBossDamage());
|
||||
assertEquals(10.0, settings.vigilanteBossArmor());
|
||||
@@ -84,6 +85,10 @@ final class PluginSettingsTest {
|
||||
IllegalArgumentException.class,
|
||||
() -> PluginSettings.from(Map.of("vigilante-arena-radius-blocks", 0))
|
||||
);
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> PluginSettings.from(Map.of("arena-barrier-height-blocks", 2))
|
||||
);
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> PluginSettings.from(Map.of("vigilante-boss-health", Double.NaN))
|
||||
|
||||
+25
-4
@@ -1,5 +1,6 @@
|
||||
package games.dmg.spigottyrant;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.Mockito.atLeastOnce;
|
||||
@@ -8,14 +9,16 @@ import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.List;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
final class VigilanteArenaBarrierTest {
|
||||
final class RoleArenaBarrierTest {
|
||||
private static final ArenaLocation CENTER = new ArenaLocation(
|
||||
"world", 0.0, 64.0, 0.0, 0.0F, 0.0F
|
||||
);
|
||||
@@ -28,7 +31,7 @@ final class VigilanteArenaBarrierTest {
|
||||
when(solid.getLocation()).thenReturn(new Location(world, 1.0, 64.0, 0.0));
|
||||
when(solid.getType()).thenReturn(Material.CHEST);
|
||||
|
||||
new VigilanteArenaBarrier().build(world, CENTER, 10.0);
|
||||
new RoleArenaBarrier().build(world, CENTER, 10.0, 12);
|
||||
|
||||
verify(solid, never()).setType(Material.BARRIER, false);
|
||||
}
|
||||
@@ -45,12 +48,30 @@ final class VigilanteArenaBarrierTest {
|
||||
when(air.getType()).thenReturn(Material.AIR);
|
||||
when(air.getBlockData()).thenReturn(original);
|
||||
when(original.clone()).thenReturn(snapshot);
|
||||
VigilanteArenaBarrier barrier = new VigilanteArenaBarrier();
|
||||
RoleArenaBarrier barrier = new RoleArenaBarrier();
|
||||
|
||||
barrier.build(world, CENTER, 10.0);
|
||||
barrier.build(world, CENTER, 10.0, 12);
|
||||
barrier.clear();
|
||||
|
||||
verify(air, atLeastOnce()).setType(Material.BARRIER, false);
|
||||
verify(air).setBlockData(snapshot, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
void wallExtendsOneBelowFloorForTwelveBlocks() {
|
||||
World world = mock(World.class);
|
||||
Block solid = mock(Block.class);
|
||||
when(world.getBlockAt(anyInt(), anyInt(), anyInt())).thenReturn(solid);
|
||||
when(solid.getLocation()).thenReturn(new Location(world, 1.0, 64.0, 0.0));
|
||||
when(solid.getType()).thenReturn(Material.CHEST);
|
||||
RoleArenaBarrier barrier = new RoleArenaBarrier();
|
||||
|
||||
barrier.build(world, CENTER, 10.0, 12);
|
||||
|
||||
ArgumentCaptor<Integer> y = ArgumentCaptor.forClass(Integer.class);
|
||||
verify(world, atLeastOnce()).getBlockAt(anyInt(), y.capture(), anyInt());
|
||||
List<Integer> values = y.getAllValues();
|
||||
assertEquals(63, values.stream().mapToInt(Integer::intValue).min().orElseThrow());
|
||||
assertEquals(74, values.stream().mapToInt(Integer::intValue).max().orElseThrow());
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package games.dmg.spigottyrant;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.mockito.ArgumentMatchers.contains;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -29,6 +30,12 @@ final class RoleArenaControllerTest {
|
||||
assertEquals(Color.RED, RoleArenaController.boundaryColor(ArenaRole.TYRANT));
|
||||
assertEquals(Color.BLUE, RoleArenaController.boundaryColor(ArenaRole.VIGILANTE));
|
||||
assertNull(RoleArenaController.boundaryColor(null));
|
||||
assertArrayEquals(
|
||||
new int[] {1}, RoleArenaController.boundaryHeightOffsets(false, 12)
|
||||
);
|
||||
assertArrayEquals(
|
||||
new int[] {1, 4, 7, 10}, RoleArenaController.boundaryHeightOffsets(true, 12)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user