add run to spawn event

This commit is contained in:
Xander 2024-09-22 17:54:23 +02:00
parent a855e61bcf
commit 6fcd67f8a2
No known key found for this signature in database
GPG key ID: 79979C7BA303E003
3 changed files with 117 additions and 3 deletions

View file

@ -37,7 +37,7 @@ public abstract class Event implements Listener, Runnable {
} }
} }
private void eventFinished() { protected void eventFinished() {
HandlerList.unregisterAll(this); HandlerList.unregisterAll(this);
MC13DTL.instance.getServer().getScheduler().cancelTask(taskId); MC13DTL.instance.getServer().getScheduler().cancelTask(taskId);
EventRunner.currentEvents.remove(this); EventRunner.currentEvents.remove(this);

View file

@ -15,7 +15,7 @@ import gent.zeus.mc13dtl.MC13DTL;
public class EventRunner implements Runnable { public class EventRunner implements Runnable {
static List<Event> currentEvents = new ArrayList<>(); static List<Event> currentEvents = new ArrayList<>();
public static List<Integer> startTimes = List.of(5000, 13000, 1000, 1000, 13000, 100, 1000, 500, 10000, 5000, 100); public static List<Integer> startTimes = List.of(5000, 13000, 1000, 1000, 13000, 100, 1000, 500, 10000, 5000, 13000, 100);
private long day; private long day;
@ -50,7 +50,8 @@ public class EventRunner implements Runnable {
case 7 -> new StructureEvent(team, difficulty, getDayScore()); case 7 -> new StructureEvent(team, difficulty, getDayScore());
case 8 -> new KillSurvivingTeamEvent(team, difficulty, getDayScore()); case 8 -> new KillSurvivingTeamEvent(team, difficulty, getDayScore());
case 9 -> new KillMobs2Event(team, difficulty, getDayScore()); case 9 -> new KillMobs2Event(team, difficulty, getDayScore());
case 10 -> new EndEvent(team, difficulty, getDayScore()); // TODO: Move to last day case 10 -> new RunToSpawnEvent(team, difficulty, getDayScore());
case 11 -> new EndEvent(team, difficulty, getDayScore()); // TODO: Move to last day
default -> throw new IllegalStateException("Unexpected day: " + (int) day); default -> throw new IllegalStateException("Unexpected day: " + (int) day);
}; };
event.start(); event.start();

View file

@ -0,0 +1,113 @@
package gent.zeus.mc13dtl.events.catalog;
import gent.zeus.mc13dtl.MC13DTL;
import gent.zeus.mc13dtl.events.Difficulty;
import gent.zeus.mc13dtl.events.Event;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Zombie;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scoreboard.Team;
import java.util.Random;
public class RunToSpawnEvent extends Event {
private final int maxRate;
private final int maxDistanceSquared = 800000;
private final int maxZombieDistance = 50;
private final Location spawn = Bukkit.getWorld("world").getSpawnLocation();
private final Random ran = new Random();
private int timeLeft = 60;
public RunToSpawnEvent(Team team, Difficulty difficulty, int scoreOnSuccess) {
super(team, difficulty, scoreOnSuccess);
maxRate = switch (difficulty) {
case BABY -> 1;
case EASY -> 5;
case MEDIUM -> 15;
case HARD -> 25;
case SWEAT -> 50;
case IMPOSSIBLE -> 100;
};
}
@Override
public void start() {
super.start();
for (Player player : MC13DTL.board.getPlayers(team)) {
player.getInventory().addItem(new ItemStack(Material.COMPASS, 1));
player.setCompassTarget(Bukkit.getWorld("world").getSpawnLocation());
player.sendMessage(Component.text("You have been given a compass").color(NamedTextColor.BLUE));
}
}
@Override
public void run() {
if (timeLeft > 0) {
timeLeft--;
return;
}
World world = Bukkit.getWorld("world");
boolean teamSpawnReached = false;
for (Player player : MC13DTL.board.getPlayers(team)) {
if (player.getWorld().getEnvironment().equals(World.Environment.NORMAL)) {
Location location = player.getLocation();
if (location.distanceSquared(spawn) < 100) {
player.sendMessage(Component.text("You have reached spawn, you should wait for your team members").color(NamedTextColor.BLUE));
teamSpawnReached = true;
} else {
teamSpawnReached = false;
double p = Math.max(0.2, (maxDistanceSquared - location.distanceSquared(spawn)) / maxDistanceSquared);
double zombies = player.getNearbyEntities(maxZombieDistance / 2.0, maxZombieDistance / 2.0, maxZombieDistance / 2.0).stream().filter(e -> e.getType().equals(EntityType.ZOMBIE)).count();
System.out.println(zombies);
for (int i = 0; i < p * maxRate - zombies; i++) {
Location zombieLocation = new Location(world, ran.nextInt(-maxZombieDistance, maxZombieDistance) + location.x(),
location.y() + 5,
ran.nextInt(-maxZombieDistance, maxZombieDistance) + location.z()
);
world.spawnEntity(zombieLocation, EntityType.ZOMBIE);
}
}
}
}
if (teamSpawnReached) {
eventSuccess();
}
}
@Override
public void eventFinished() {
for (Player player : MC13DTL.board.getPlayers(team)) {
for (Entity entity : player.getNearbyEntities(200, 200, 200)) {
if (entity.getType().equals(EntityType.ZOMBIE)) {
entity.remove();
}
}
}
super.eventFinished();
}
@Override
public void eventTimeEnded() {
Bukkit.getWorld("world").getEntitiesByClasses(Zombie.class).forEach(Entity::remove);
super.eventTimeEnded();
}
@Override
protected String getMessage() {
return "Withing " + timeLeft + " seconds, zombies will start spawning near you. Your full team needs to reach spawn before dawn without dying.";
}
}