Add group/event difficulty

This commit is contained in:
Mathieu Strypsteen 2024-09-15 11:31:57 +02:00
parent 1fc7c9f1fc
commit 8d0acad407
8 changed files with 120 additions and 12 deletions

View file

@ -1,6 +1,7 @@
package gent.zeus.mc13dtl; package gent.zeus.mc13dtl;
import gent.zeus.mc13dtl.group.GroupAddCommand; import gent.zeus.mc13dtl.group.GroupAddCommand;
import gent.zeus.mc13dtl.group.GroupDifficultyCommand;
import gent.zeus.mc13dtl.group.GroupRemoveCommand; import gent.zeus.mc13dtl.group.GroupRemoveCommand;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -25,8 +26,9 @@ public class MC13DTL extends JavaPlugin {
manager.registerEventHandler(LifecycleEvents.COMMANDS, event -> { manager.registerEventHandler(LifecycleEvents.COMMANDS, event -> {
Commands commands = event.registrar(); Commands commands = event.registrar();
commands.register("start-game", "Start a game of 13 days to live", new GameStateCommand()); commands.register("start-game", "Start a game of 13 days to live", new GameStateCommand());
commands.register("groupadd", "Add player to team", new GroupAddCommand()); commands.register("group-add", "Add player to team", new GroupAddCommand());
commands.register("groupremove", "Remove player from team", new GroupRemoveCommand()); commands.register("group-remove", "Remove player from team", new GroupRemoveCommand());
commands.register("group-difficulty", "Set group difficulty", new GroupDifficultyCommand());
}); });
BukkitScheduler scheduler = this.getServer().getScheduler(); BukkitScheduler scheduler = this.getServer().getScheduler();
scheduler.runTaskTimer(this, new RandomEventRunner(), 0, 100); scheduler.runTaskTimer(this, new RandomEventRunner(), 0, 100);

View file

@ -16,6 +16,7 @@ public class TeamScoreBoard {
private Scoreboard scoreboard; private Scoreboard scoreboard;
private Objective objective; private Objective objective;
private Objective difficultyObjective;
public TeamScoreBoard() { public TeamScoreBoard() {
sm = Bukkit.getScoreboardManager(); sm = Bukkit.getScoreboardManager();
@ -27,6 +28,11 @@ public class TeamScoreBoard {
} }
objective.setDisplaySlot(DisplaySlot.SIDEBAR); 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) { public void setScore(Team team, int score) {
@ -46,6 +52,15 @@ public class TeamScoreBoard {
setScore(team, current_score + score); 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) { public Team getTeam(Integer team_index) {
Team team = scoreboard.getTeam(team_index.toString()); Team team = scoreboard.getTeam(team_index.toString());
@ -56,6 +71,7 @@ public class TeamScoreBoard {
team.prefix(Component.text("Team " + team_index.toString() + " ")); team.prefix(Component.text("Team " + team_index.toString() + " "));
team.setAllowFriendlyFire(false); team.setAllowFriendlyFire(false);
setScore(team, 0); setScore(team, 0);
setDifficulty(team, 1);
} }
return team; return team;

View file

@ -47,6 +47,10 @@ public abstract class Event implements Listener {
} }
} }
public static int thresholdDifficulty() {
return 1;
}
protected abstract String getMessage(); protected abstract String getMessage();
protected abstract int scoreAwarded(); protected abstract int scoreAwarded();

View file

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

View file

@ -0,0 +1,35 @@
package gent.zeus.mc13dtl.events.catalog;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.scoreboard.Team;
import gent.zeus.mc13dtl.events.Event;
public class TestEvent2 extends Event {
public TestEvent2(Team team) {
super(team);
}
@Override
protected String getMessage() {
return "Super difficult event!";
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getPlayer() == null) {
return;
}
eventSuccess();
}
@Override
protected int scoreAwarded() {
return 200;
}
public static int thresholdDifficulty() {
return 3;
}
}

View file

@ -24,10 +24,10 @@ public class GroupAddCommand implements BasicCommand {
commandSourceStack.getSender().sendMessage("Player " + args[0] + " does not exist."); commandSourceStack.getSender().sendMessage("Player " + args[0] + " does not exist.");
} }
} else { } else {
commandSourceStack.getSender().sendMessage("groupAdd usage: /groupadd <player> <index>"); commandSourceStack.getSender().sendMessage("usage: /group-add <player> <index>");
} }
} catch (NumberFormatException ex) { } catch (NumberFormatException ex) {
commandSourceStack.getSender().sendMessage("usage: /groupadd <player> <NUMBER>"); commandSourceStack.getSender().sendMessage("usage: /group-add <player> <NUMBER>");
} }
} }

View file

@ -0,0 +1,42 @@
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>");
}
}
}

View file

@ -17,7 +17,7 @@ public class GroupRemoveCommand implements BasicCommand {
if (player != null) { if (player != null) {
Team team = MC13DTL.board.getTeam(player); Team team = MC13DTL.board.getTeam(player);
if (team != null) { if (team != null) {
team.removeEntry(player.getName()); team.removeEntry(player.getUniqueId().toString());
player.sendMessage("You have been removed from your team"); player.sendMessage("You have been removed from your team");
} else { } else {
commandSourceStack.getSender().sendMessage("Player not in a team!"); commandSourceStack.getSender().sendMessage("Player not in a team!");
@ -26,7 +26,7 @@ public class GroupRemoveCommand implements BasicCommand {
commandSourceStack.getSender().sendMessage("Player " + args[0] + " does not exist."); commandSourceStack.getSender().sendMessage("Player " + args[0] + " does not exist.");
} }
} else { } else {
commandSourceStack.getSender().sendMessage("group remove usage: /groupremove <player>"); commandSourceStack.getSender().sendMessage("group remove usage: /group-remove <player>");
} }
} }