Add survival score

This commit is contained in:
Mathieu Strypsteen 2024-09-21 17:10:46 +02:00
parent b6407a6b59
commit ce5af1bffd
8 changed files with 71 additions and 9 deletions

View file

@ -4,6 +4,9 @@ import gent.zeus.mc13dtl.gamestate.*;
import gent.zeus.mc13dtl.group.GroupAddCommand;
import gent.zeus.mc13dtl.group.GroupRemoveCommand;
import gent.zeus.mc13dtl.group.SetScoreCommand;
import gent.zeus.mc13dtl.group.SurvivalHandler;
import gent.zeus.mc13dtl.group.SurvivalRunner;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;
@ -21,6 +24,7 @@ public class MC13DTL extends JavaPlugin {
public void onEnable() {
instance = this;
getServer().getPluginManager().registerEvents(new GameStateHandler(), this);
getServer().getPluginManager().registerEvents(new SurvivalHandler(), this);
LifecycleEventManager<Plugin> manager = this.getLifecycleManager();
manager.registerEventHandler(LifecycleEvents.COMMANDS, event -> {
Commands commands = event.registrar();
@ -36,6 +40,7 @@ public class MC13DTL extends JavaPlugin {
scheduler.runTask(this, () -> {
board = new TeamScoreBoard();
});
scheduler.runTaskTimer(this, new EventRunner(), 0, 200);
scheduler.runTaskTimer(this, new EventRunner(), 0, 100);
scheduler.runTaskTimer(this, new SurvivalRunner(), 0, 100);
}
}

View file

@ -16,6 +16,7 @@ public class TeamScoreBoard {
private final Scoreboard scoreboard;
private Objective objective;
private Objective daysAliveObjective;
public TeamScoreBoard() {
sm = Bukkit.getScoreboardManager();
@ -25,7 +26,10 @@ public class TeamScoreBoard {
if (objective == null) {
objective = scoreboard.registerNewObjective("scores", Criteria.DUMMY, Component.text("Scores"));
}
daysAliveObjective = scoreboard.getObjective("days-alive");
if (daysAliveObjective == null) {
daysAliveObjective = scoreboard.registerNewObjective("days-alive", Criteria.DUMMY, Component.text("Days alive"));
}
objective.setDisplaySlot(DisplaySlot.SIDEBAR);
}
@ -37,10 +41,19 @@ public class TeamScoreBoard {
score_obj.setScore(score);
}
public void setDaysAlive(Team team, int days) {
Score score_obj = daysAliveObjective.getScore(team.getName());
score_obj.setScore(days);
}
public int getScore(Team team) {
return objective.getScore(team.getName()).getScore();
}
public int getDaysAlive(Team team) {
return daysAliveObjective.getScore(team.getName()).getScore();
}
public void addScore(Team team, int score) {
int current_score = getScore(team);
setScore(team, current_score + score);

View file

@ -10,7 +10,6 @@ import org.bukkit.World;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.scoreboard.Team;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.OptionalDouble;

View file

@ -33,6 +33,7 @@ public class ClimbEvent extends Event {
Location location = new Location(Bukkit.getWorld("world"), player.getX(), -50, player.getZ());
location.getBlock().setType(Material.AIR);
location.add(0, -1, 0).getBlock().setType(Material.AIR);
location.add(0, -2, 0).getBlock().setType(Material.STONE);
for (Player p : MC13DTL.board.getPlayers(team)) {
p.teleport(location);

View file

@ -8,19 +8,16 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.scoreboard.Team;
import java.util.Map;
public class KillMobs1Event extends Event implements Listener {
public class KillMobs1Event extends Event {
private int killsLeft;
private final EntityType monster;
public KillMobs1Event(Team team, Difficulty difficulty, int scoreOnSuccess) {
super(team, difficulty, scoreOnSuccess);
Map<Difficulty, Pair<EntityType, Integer>> monsters = Map.of(
@ -28,8 +25,7 @@ public class KillMobs1Event extends Event implements Listener {
Difficulty.EASY, new Pair<>(EntityType.ZOMBIE, 3),
Difficulty.MEDIUM, new Pair<>(EntityType.ZOMBIE, 6),
Difficulty.HARD, new Pair<>(EntityType.SKELETON, 5),
Difficulty.IMPOSSIBLE, new Pair<>(EntityType.ENDERMAN, 4)
);
Difficulty.IMPOSSIBLE, new Pair<>(EntityType.ENDERMAN, 4));
monster = monsters.get(difficulty).getFirst();
killsLeft = monsters.get(difficulty).getSecond();
}

View file

@ -25,6 +25,8 @@ public class GameStateUtil {
world.getPersistentDataContainer().set(key, PersistentDataType.STRING, "lobby");
NamespacedKey eventKey = new NamespacedKey(MC13DTL.instance, "last-event-day");
world.getPersistentDataContainer().set(eventKey, PersistentDataType.LONG, -1L);
NamespacedKey survivalKey = new NamespacedKey(MC13DTL.instance, "last-survival-check-day");
world.getPersistentDataContainer().set(survivalKey, PersistentDataType.LONG, 0L);
}
String gameStateStr = world.getPersistentDataContainer().get(key, PersistentDataType.STRING);
switch (gameStateStr) {

View file

@ -0,0 +1,19 @@
package gent.zeus.mc13dtl.group;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.scoreboard.Team;
import gent.zeus.mc13dtl.MC13DTL;
public class SurvivalHandler implements Listener {
@EventHandler
public void onPlayerDeath(PlayerDeathEvent event) {
for (Team team : MC13DTL.board.getTeams().stream().toList()) {
if (MC13DTL.board.getPlayers(team).contains(event.getPlayer())) {
MC13DTL.board.setDaysAlive(team, 0);
}
}
}
}

View file

@ -0,0 +1,27 @@
package gent.zeus.mc13dtl.group;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.scoreboard.Team;
import gent.zeus.mc13dtl.MC13DTL;
public class SurvivalRunner implements Runnable {
@Override
public void run() {
World world = Bukkit.getWorld("world");
NamespacedKey eventKey = new NamespacedKey(MC13DTL.instance, "last-survival-check-day");
long lastRunDay = world.getPersistentDataContainer().get(eventKey, PersistentDataType.LONG);
long day = Bukkit.getWorld("world").getFullTime() / 24000;
if (day > lastRunDay) {
for (Team team : MC13DTL.board.getTeams().stream().toList()) {
int currentDays = MC13DTL.board.getDaysAlive(team) + 1;
MC13DTL.board.setDaysAlive(team, currentDays);
MC13DTL.board.addScore(team, (int) Math.pow(2, currentDays));
}
}
world.getPersistentDataContainer().set(eventKey, PersistentDataType.LONG, day);
}
}