added deathrun listener for border; fixed listener problem when disabling appliance

This commit is contained in:
2025-12-13 16:02:23 +01:00
parent ec262710ec
commit 914aaff10b
3 changed files with 68 additions and 5 deletions

View File

@@ -7,22 +7,42 @@ import eu.mhsl.craftattack.spawn.event.appliances.eventController.scoreboard.Eve
import net.kyori.adventure.text.Component;
import net.kyori.adventure.title.Title;
import org.bukkit.Bukkit;
import org.bukkit.Particle;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;
import java.util.List;
@Appliance.Flags(autoload = false)
public class Deathrun extends Appliance implements Event, Scorable {
private final EventScoreboardBuilder scoreboardBuilder = new EventScoreboardBuilder(this, 3, 2, 3);
private final double borderDistance = 100;
@Override
public void initialize(@NotNull JavaPlugin plugin) {
public void onEnable() {
World world = Bukkit.getWorlds().getFirst();
world.getWorldBorder().setCenter(world.getSpawnLocation());
world.getWorldBorder().setSize(20);
}
public double getBorderDistance() {
return this.borderDistance;
}
public void spawnParticleWall(Player p, double xMin, double xMax, double yMin, double yMax, double zMin, double zMax) {
Particle particle = Particle.WAX_ON;
for (double y = yMin; y <= yMax; y += 0.5) {
for (double z = zMin; z <= zMax; z += 0.5) {
for (double x = xMin; x <= xMax; x += 0.5) {
p.spawnParticle(particle, x, y, z, 1, 0.05, 0.05, 0.05, 0);
}
}
}
}
@Override
public void onDisable() {
World world = Bukkit.getWorlds().getFirst();
@@ -64,4 +84,11 @@ public class Deathrun extends Appliance implements Event, Scorable {
public EventScoreboardBuilder getScoreboardBuilder() {
return this.scoreboardBuilder;
}
@Override
protected @NotNull List<Listener> listeners() {
return List.of(
new DeathrunPlayerMoveListener()
);
}
}

View File

@@ -0,0 +1,35 @@
package eu.mhsl.craftattack.spawn.event.appliances.deathrun;
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceListener;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerMoveEvent;
public class DeathrunPlayerMoveListener extends ApplianceListener<Deathrun> {
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
Location spawnLocation = Bukkit.getWorlds().getFirst().getSpawnLocation();
double minX = spawnLocation.x() - this.getAppliance().getBorderDistance();
double minZ = spawnLocation.z() - this.getAppliance().getBorderDistance();
double maxZ = spawnLocation.z() + this.getAppliance().getBorderDistance();
if(event.getTo().x() < minX + 5) {
this.getAppliance().spawnParticleWall(event.getPlayer(), minX-0.2, minX-0.2, event.getTo().y()-0.5, event.getTo().y()+2.5, event.getTo().z()-1.5, event.getTo().z()+1.5);
if(event.getTo().x() < minX) {
event.setTo(event.getTo().clone().set(minX, event.getTo().y(), event.getTo().z()));
}
}
if(event.getTo().z() < minZ + 5) {
this.getAppliance().spawnParticleWall(event.getPlayer(), event.getTo().x()-1.5, event.getTo().x()+1.5, event.getTo().y()-0.5, event.getTo().y()+2.5, minZ-0.2, minZ-0.2);
if(event.getTo().z() < minZ) {
event.setTo(event.getTo().clone().set(event.getTo().x(), event.getTo().y(), minZ));
}
}
if(event.getTo().z() > maxZ - 5) {
this.getAppliance().spawnParticleWall(event.getPlayer(), event.getTo().x()-1.5, event.getTo().x()+1.5, event.getTo().y()-0.5, event.getTo().y()+2.5, maxZ+0.2, maxZ+0.2);
if(event.getTo().z() > maxZ) {
event.setTo(event.getTo().clone().set(event.getTo().x(), event.getTo().y(), maxZ));
}
}
}
}

View File

@@ -25,10 +25,11 @@ public class EventController extends Appliance {
}
public void loadEvent(Appliance appliance) {
if(!(appliance instanceof Event event)) throw new IllegalArgumentException("Appliance has to implement Event.");
if(!(appliance instanceof Event)) throw new IllegalArgumentException("Appliance has to implement Event.");
this.unloadEvent();
Main.instance().restartAppliance(appliance.getClass());
this.selectedEvent = event;
Appliance newAppliance = Main.instance().restartAppliance(appliance.getClass());
if(!(newAppliance instanceof Event newEvent)) throw new IllegalArgumentException("Appliance has to implement Event.");
this.selectedEvent = newEvent;
}
public void unloadEvent() {