Switch to fixed events

This commit is contained in:
Mathieu Strypsteen 2024-09-15 13:27:32 +02:00
parent db1dd18051
commit 4835fac6a7
7 changed files with 17 additions and 108 deletions

View file

@ -1,13 +1,12 @@
package gent.zeus.mc13dtl;
import gent.zeus.mc13dtl.group.GroupAddCommand;
import gent.zeus.mc13dtl.group.GroupDifficultyCommand;
import gent.zeus.mc13dtl.group.GroupRemoveCommand;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;
import gent.zeus.mc13dtl.events.RandomEventRunner;
import gent.zeus.mc13dtl.events.EventRunner;
import gent.zeus.mc13dtl.gamestate.GameStateHandler;
import gent.zeus.mc13dtl.gamestate.StartGameCommand;
import gent.zeus.mc13dtl.gamestate.TogglePauseCommand;
@ -30,10 +29,9 @@ public class MC13DTL extends JavaPlugin {
commands.register("toggle-pause", "Toggle paused state", new TogglePauseCommand());
commands.register("group-add", "Add player to team", new GroupAddCommand());
commands.register("group-remove", "Remove player from team", new GroupRemoveCommand());
commands.register("group-difficulty", "Set group difficulty", new GroupDifficultyCommand());
});
BukkitScheduler scheduler = this.getServer().getScheduler();
scheduler.runTaskTimer(this, new RandomEventRunner(), 0, 100);
scheduler.runTaskTimer(this, new EventRunner(), 0, 100);
scheduler.runTask(this, () -> {
board = new TeamScoreBoard();
});

View file

@ -16,7 +16,6 @@ public class TeamScoreBoard {
private Scoreboard scoreboard;
private Objective objective;
private Objective difficultyObjective;
public TeamScoreBoard() {
sm = Bukkit.getScoreboardManager();
@ -28,11 +27,6 @@ public class TeamScoreBoard {
}
objective.setDisplaySlot(DisplaySlot.SIDEBAR);
difficultyObjective = scoreboard.getObjective("difficulty");
if (difficultyObjective == null) {
difficultyObjective = scoreboard.registerNewObjective("difficulty", Criteria.DUMMY, Component.text("Difficulty"));
}
}
public void setScore(Team team, int score) {
@ -52,15 +46,6 @@ public class TeamScoreBoard {
setScore(team, current_score + score);
}
public void setDifficulty(Team team, int difficulty) {
Score score_obj = difficultyObjective.getScore(team.getName());
score_obj.setScore(difficulty);
}
public int getDifficulty(Team team) {
return difficultyObjective.getScore(team.getName()).getScore();
}
public Team getTeam(Integer team_index) {
Team team = scoreboard.getTeam(team_index.toString());
@ -71,7 +56,6 @@ public class TeamScoreBoard {
team.prefix(Component.text("Team " + team_index.toString() + " "));
team.setAllowFriendlyFire(false);
setScore(team, 0);
setDifficulty(team, 1);
}
return team;

View file

@ -18,7 +18,7 @@ public abstract class Event implements Listener {
this.team = team;
MC13DTL.board.sendMessageToTeam(team, Component.text(getMessage()).color(NamedTextColor.GRAY).decorate(TextDecoration.ITALIC));
MC13DTL.instance.getServer().getPluginManager().registerEvents(this, MC13DTL.instance);
RandomEventExecutor.currentEvents.add(this);
EventRunner.currentEvents.add(this);
if (MC13DTL.board.getPlayers(team).isEmpty()) {
eventFailed();
}
@ -26,7 +26,7 @@ public abstract class Event implements Listener {
private void eventFinished() {
HandlerList.unregisterAll(this);
RandomEventExecutor.currentEvents.remove(this);
EventRunner.currentEvents.remove(this);
}
protected void eventSuccess() {
@ -47,10 +47,6 @@ public abstract class Event implements Listener {
}
}
public static int thresholdDifficulty() {
return 1;
}
protected abstract String getMessage();
protected abstract int scoreAwarded();

View file

@ -5,10 +5,16 @@ import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.scoreboard.Team;
import java.util.List;
import java.util.ArrayList;
import gent.zeus.mc13dtl.MC13DTL;
import gent.zeus.mc13dtl.events.catalog.TestEvent;
import gent.zeus.mc13dtl.events.catalog.TestEvent2;
public class EventRunner implements Runnable {
static List<Event> currentEvents = new ArrayList<>();
public class RandomEventRunner implements Runnable {
@Override
public void run() {
World world = Bukkit.getWorld("world");
@ -16,12 +22,15 @@ public class RandomEventRunner implements Runnable {
long lastRunDay = world.getPersistentDataContainer().get(eventKey, PersistentDataType.LONG);
long day = Bukkit.getWorld("world").getFullTime() / 24000;
if (day > lastRunDay && Bukkit.getWorld("world").getTime() >= 14000) {
while (!RandomEventExecutor.currentEvents.isEmpty()) {
Event event = RandomEventExecutor.currentEvents.get(0);
while (!currentEvents.isEmpty()) {
Event event = currentEvents.get(0);
event.eventFailed();
}
for (Team i : MC13DTL.board.getTeams()) {
RandomEventExecutor.executeRandomEvent(i);
switch ((int) day) {
case 0 -> new TestEvent(i);
case 1 -> new TestEvent2(i);
}
}
world.getPersistentDataContainer().set(eventKey, PersistentDataType.LONG, day);
}

View file

@ -1,33 +0,0 @@
package gent.zeus.mc13dtl.events;
import org.bukkit.scoreboard.Team;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import java.util.Arrays;
import gent.zeus.mc13dtl.MC13DTL;
import gent.zeus.mc13dtl.events.catalog.TestEvent;
import gent.zeus.mc13dtl.events.catalog.TestEvent2;
public class RandomEventExecutor {
static List<Class<? extends Event>> events = Arrays.asList(TestEvent.class, TestEvent2.class);
static List<Event> currentEvents = new ArrayList<>();
public static void executeRandomEvent(Team team) {
Random random = new Random();
while (true) {
int i = random.nextInt(events.size());
Class<? extends Event> event = events.get(i);
try {
if ((int) event.getMethod("thresholdDifficulty").invoke(null) <= MC13DTL.board.getDifficulty(team)) {
event.getConstructor(Team.class).newInstance(team);
return;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

View file

@ -29,7 +29,4 @@ public class TestEvent2 extends Event {
return 200;
}
public static int thresholdDifficulty() {
return 3;
}
}

View file

@ -1,42 +0,0 @@
package gent.zeus.mc13dtl.group;
import gent.zeus.mc13dtl.MC13DTL;
import io.papermc.paper.command.brigadier.BasicCommand;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Team;
import org.jetbrains.annotations.NotNull;
public class GroupDifficultyCommand implements BasicCommand {
@Override
public void execute(@NotNull CommandSourceStack commandSourceStack, @NotNull String[] args) {
if (!(commandSourceStack.getSender() instanceof Player)) {
return;
}
try {
Player player = (Player) commandSourceStack.getSender();
Team team = MC13DTL.board.getTeam(player);
if (team != null) {
if (args.length == 1) {
int difficulty = Integer.parseInt(args[0]);
if (difficulty < 1 || difficulty > 5) {
commandSourceStack.getSender().sendMessage(Component.text("Difficulty must be between 1 and 5!").color(NamedTextColor.RED));
return;
}
MC13DTL.board.setDifficulty(team, Integer.parseInt(args[0]));
MC13DTL.board.sendMessageToTeam(team, Component.text("Difficulty has been set to " + difficulty).color(NamedTextColor.GREEN));
} else {
commandSourceStack.getSender().sendMessage("Current difficulty: " + MC13DTL.board.getDifficulty(team));
}
} else {
commandSourceStack.getSender().sendMessage(Component.text("Join a team first!").color(NamedTextColor.RED));
}
} catch (NumberFormatException ex) {
commandSourceStack.getSender().sendMessage("usage: group-difficulty usage: /group-difficulty <difficulty>");
}
}
}