added timer

This commit is contained in:
2025-12-19 14:04:36 +01:00
parent 215259c6b9
commit de112f7e13
6 changed files with 68 additions and 9 deletions

View File

@@ -19,6 +19,8 @@ import java.util.List;
public class Deathrun extends Appliance implements Event, Scorable { public class Deathrun extends Appliance implements Event, Scorable {
private final EventScoreboardBuilder scoreboardBuilder = new EventScoreboardBuilder(this, 3, 2, 3); private final EventScoreboardBuilder scoreboardBuilder = new EventScoreboardBuilder(this, 3, 2, 3);
private final double borderDistance = 100; private final double borderDistance = 100;
private final int borderVisibilityDistance = 8;
private long durationSeconds;
@Override @Override
public void onEnable() { public void onEnable() {
@@ -31,6 +33,10 @@ public class Deathrun extends Appliance implements Event, Scorable {
return this.borderDistance; return this.borderDistance;
} }
public int getBorderVisibilityDistance() {
return this.borderVisibilityDistance;
}
public void spawnParticleWall(Player p, double xMin, double xMax, double yMin, double yMax, double zMin, double zMax) { public void spawnParticleWall(Player p, double xMin, double xMax, double yMin, double yMax, double zMin, double zMax) {
Particle particle = Particle.WAX_ON; Particle particle = Particle.WAX_ON;
@@ -50,7 +56,8 @@ public class Deathrun extends Appliance implements Event, Scorable {
} }
@Override @Override
public void start() { public void start(long durationSeconds) {
this.durationSeconds = durationSeconds;
Title title = Title.title(Component.text("Start"), Component.text("Erreiche die höchste x-Koordinate!")); Title title = Title.title(Component.text("Start"), Component.text("Erreiche die höchste x-Koordinate!"));
Bukkit.getOnlinePlayers().forEach(player -> player.showTitle(title)); Bukkit.getOnlinePlayers().forEach(player -> player.showTitle(title));
@@ -73,6 +80,11 @@ public class Deathrun extends Appliance implements Event, Scorable {
}); });
} }
@Override
public long getDurationSeconds() {
return this.durationSeconds;
}
@Override @Override
public String getScoreboardName() { public String getScoreboardName() {
return "Deathrun"; return "Deathrun";

View File

@@ -13,19 +13,19 @@ public class DeathrunPlayerMoveListener extends ApplianceListener<Deathrun> {
double minX = spawnLocation.x() - this.getAppliance().getBorderDistance(); double minX = spawnLocation.x() - this.getAppliance().getBorderDistance();
double minZ = spawnLocation.z() - this.getAppliance().getBorderDistance(); double minZ = spawnLocation.z() - this.getAppliance().getBorderDistance();
double maxZ = spawnLocation.z() + this.getAppliance().getBorderDistance(); double maxZ = spawnLocation.z() + this.getAppliance().getBorderDistance();
if(event.getTo().x() < minX + 5) { if(event.getTo().x() < minX + this.getAppliance().getBorderVisibilityDistance()) {
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); 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) { if(event.getTo().x() < minX) {
event.setTo(event.getTo().clone().set(minX, event.getTo().y(), event.getTo().z())); event.setTo(event.getTo().clone().set(minX, event.getTo().y(), event.getTo().z()));
} }
} }
if(event.getTo().z() < minZ + 5) { if(event.getTo().z() < minZ + this.getAppliance().getBorderVisibilityDistance()) {
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); 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) { if(event.getTo().z() < minZ) {
event.setTo(event.getTo().clone().set(event.getTo().x(), event.getTo().y(), minZ)); event.setTo(event.getTo().clone().set(event.getTo().x(), event.getTo().y(), minZ));
} }
} }
if(event.getTo().z() > maxZ - 5) { if(event.getTo().z() > maxZ - this.getAppliance().getBorderVisibilityDistance()) {
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); 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) { if(event.getTo().z() > maxZ) {
event.setTo(event.getTo().clone().set(event.getTo().x(), event.getTo().y(), maxZ)); event.setTo(event.getTo().clone().set(event.getTo().x(), event.getTo().y(), maxZ));

View File

@@ -1,6 +1,7 @@
package eu.mhsl.craftattack.spawn.event.appliances.eventController; package eu.mhsl.craftattack.spawn.event.appliances.eventController;
public interface Event { public interface Event {
void start(); void start(long durationSeconds);
void stop(); void stop();
long getDurationSeconds();
} }

View File

@@ -4,6 +4,10 @@ import eu.mhsl.craftattack.spawn.core.Main;
import eu.mhsl.craftattack.spawn.core.appliance.Appliance; import eu.mhsl.craftattack.spawn.core.appliance.Appliance;
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceCommand; import eu.mhsl.craftattack.spawn.core.appliance.ApplianceCommand;
import eu.mhsl.craftattack.spawn.event.appliances.eventController.commands.EventCommand; import eu.mhsl.craftattack.spawn.event.appliances.eventController.commands.EventCommand;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.util.Ticks;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.List; import java.util.List;
@@ -11,6 +15,8 @@ import java.util.List;
public class EventController extends Appliance { public class EventController extends Appliance {
private List<Appliance> eventAppliances = null; private List<Appliance> eventAppliances = null;
private Event selectedEvent = null; private Event selectedEvent = null;
private long timerStart;
private int timerTaskId = -1;
@Override @Override
public void onEnable() { public void onEnable() {
@@ -41,15 +47,25 @@ public class EventController extends Appliance {
this.selectedEvent = null; this.selectedEvent = null;
} }
public void startEvent() { public void startEvent(long durationMinutes) {
if(this.selectedEvent == null) throw new ApplianceCommand.Error("There is no event selected!"); if(this.selectedEvent == null) throw new ApplianceCommand.Error("There is no event selected!");
this.selectedEvent.start(); this.selectedEvent.start(durationMinutes * 60);
if(this.selectedEvent instanceof Scorable scorable && scorable.automaticUpdates()) scorable.getScoreboardBuilder().startAutomaticUpdates(); if(this.selectedEvent instanceof Scorable scorable && scorable.automaticUpdates()) scorable.getScoreboardBuilder().startAutomaticUpdates();
// TODO: possibility for other dimensions
this.timerStart = Bukkit.getWorlds().getFirst().getFullTime();
this.timerTaskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(
Main.instance(),
this::updateTimer,
Ticks.TICKS_PER_SECOND,
Ticks.TICKS_PER_SECOND
);
} }
public void stopEvent() { public void stopEvent() {
if(this.selectedEvent == null) throw new ApplianceCommand.Error("There is no event selected!"); if(this.selectedEvent == null) throw new ApplianceCommand.Error("There is no event selected!");
this.selectedEvent.stop(); this.selectedEvent.stop();
if(this.timerTaskId != -1) Bukkit.getScheduler().cancelTask(this.timerTaskId);
this.timerTaskId = -1;
} }
public String getSelectedEvent() { public String getSelectedEvent() {
@@ -57,6 +73,25 @@ public class EventController extends Appliance {
return this.selectedEvent.getClass().getSimpleName().toLowerCase(); return this.selectedEvent.getClass().getSimpleName().toLowerCase();
} }
private void updateTimer() {
long ticksLeft = this.timerStart - (Bukkit.getWorlds().getFirst().getFullTime() - this.selectedEvent.getDurationSeconds() * Ticks.TICKS_PER_SECOND);
if(ticksLeft <= 0) {
if(this.timerTaskId != -1) Bukkit.getScheduler().cancelTask(this.timerTaskId);
this.timerTaskId = -1;
this.selectedEvent.stop();
Bukkit.getOnlinePlayers().forEach(player -> player.sendActionBar(Component.text("Fertig!")));
return;
}
Bukkit.getOnlinePlayers().forEach(player -> player.sendActionBar(
Component.text(formatSeconds(ticksLeft / Ticks.TICKS_PER_SECOND), NamedTextColor.GOLD)
));
}
public static String formatSeconds(long seconds){
return String.format("%02d:%02d:%02d", seconds / 3600, (seconds / 60) % 60, seconds % 60);
}
@Override @Override
protected @NotNull List<ApplianceCommand<?>> commands() { protected @NotNull List<ApplianceCommand<?>> commands() {
return List.of( return List.of(

View File

@@ -34,7 +34,17 @@ public class EventCommand extends ApplianceCommand<EventController> {
break; break;
} }
case "start": { case "start": {
this.getAppliance().startEvent(); if(args.length == 1) {
this.getAppliance().startEvent(60 * 2);
break;
}
if(args.length == 2) {
try {
this.getAppliance().startEvent(Long.parseLong(args[1]));
} catch(NumberFormatException e) {
throw new Error("Last argument has to be a long.");
}
}
break; break;
} }
case "stop": { case "stop": {
@@ -59,6 +69,7 @@ public class EventCommand extends ApplianceCommand<EventController> {
if(args.length == 2 && args[0].equals("load")) return this.getAppliance().getEventAppliances().stream() if(args.length == 2 && args[0].equals("load")) return this.getAppliance().getEventAppliances().stream()
.map(appliance -> appliance.getClass().getSimpleName().toLowerCase()) .map(appliance -> appliance.getClass().getSimpleName().toLowerCase())
.toList(); .toList();
if(args.length == 2 && args[0].equals("start")) return List.of("<minutes>");
return null; return null;
} }
} }

View File

@@ -42,7 +42,7 @@ public class EventScoreboardBuilder {
Objective objective = newScoreboard.registerNewObjective( Objective objective = newScoreboard.registerNewObjective(
"event", "dummy", "event", "dummy",
Component.text("Scoreboard %s".formatted(this.scorable.getScoreboardName()), NamedTextColor.GOLD) Component.text("Scoreboard %s".formatted(this.scorable.getScoreboardName()), NamedTextColor.DARK_PURPLE)
); );
objective.setDisplaySlot(DisplaySlot.SIDEBAR); objective.setDisplaySlot(DisplaySlot.SIDEBAR);
objective.numberFormat(NumberFormat.blank()); objective.numberFormat(NumberFormat.blank());