From 8d0acad4079a9732f46b4c1915a475a4f0040276 Mon Sep 17 00:00:00 2001 From: Mathieu Strypsteen Date: Sun, 15 Sep 2024 11:31:57 +0200 Subject: [PATCH] Add group/event difficulty --- src/main/java/gent/zeus/mc13dtl/MC13DTL.java | 6 ++- .../gent/zeus/mc13dtl/TeamScoreBoard.java | 16 +++++++ .../java/gent/zeus/mc13dtl/events/Event.java | 4 ++ .../mc13dtl/events/RandomEventExecutor.java | 21 +++++++--- .../mc13dtl/events/catalog/TestEvent2.java | 35 ++++++++++++++++ .../zeus/mc13dtl/group/GroupAddCommand.java | 4 +- .../mc13dtl/group/GroupDifficultyCommand.java | 42 +++++++++++++++++++ .../mc13dtl/group/GroupRemoveCommand.java | 4 +- 8 files changed, 120 insertions(+), 12 deletions(-) create mode 100644 src/main/java/gent/zeus/mc13dtl/events/catalog/TestEvent2.java create mode 100644 src/main/java/gent/zeus/mc13dtl/group/GroupDifficultyCommand.java diff --git a/src/main/java/gent/zeus/mc13dtl/MC13DTL.java b/src/main/java/gent/zeus/mc13dtl/MC13DTL.java index 44540c8..d85dd51 100644 --- a/src/main/java/gent/zeus/mc13dtl/MC13DTL.java +++ b/src/main/java/gent/zeus/mc13dtl/MC13DTL.java @@ -1,6 +1,7 @@ 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; @@ -25,8 +26,9 @@ public class MC13DTL extends JavaPlugin { manager.registerEventHandler(LifecycleEvents.COMMANDS, event -> { Commands commands = event.registrar(); 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("groupremove", "Remove player from team", new GroupRemoveCommand()); + 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); diff --git a/src/main/java/gent/zeus/mc13dtl/TeamScoreBoard.java b/src/main/java/gent/zeus/mc13dtl/TeamScoreBoard.java index 1a64f2a..cb0e5a7 100644 --- a/src/main/java/gent/zeus/mc13dtl/TeamScoreBoard.java +++ b/src/main/java/gent/zeus/mc13dtl/TeamScoreBoard.java @@ -16,6 +16,7 @@ public class TeamScoreBoard { private Scoreboard scoreboard; private Objective objective; + private Objective difficultyObjective; public TeamScoreBoard() { sm = Bukkit.getScoreboardManager(); @@ -27,6 +28,11 @@ 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) { @@ -46,6 +52,15 @@ 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()); @@ -56,6 +71,7 @@ public class TeamScoreBoard { team.prefix(Component.text("Team " + team_index.toString() + " ")); team.setAllowFriendlyFire(false); setScore(team, 0); + setDifficulty(team, 1); } return team; diff --git a/src/main/java/gent/zeus/mc13dtl/events/Event.java b/src/main/java/gent/zeus/mc13dtl/events/Event.java index c21fffa..30b583c 100644 --- a/src/main/java/gent/zeus/mc13dtl/events/Event.java +++ b/src/main/java/gent/zeus/mc13dtl/events/Event.java @@ -47,6 +47,10 @@ public abstract class Event implements Listener { } } + public static int thresholdDifficulty() { + return 1; + } + protected abstract String getMessage(); protected abstract int scoreAwarded(); diff --git a/src/main/java/gent/zeus/mc13dtl/events/RandomEventExecutor.java b/src/main/java/gent/zeus/mc13dtl/events/RandomEventExecutor.java index 00e3b60..e5533c5 100644 --- a/src/main/java/gent/zeus/mc13dtl/events/RandomEventExecutor.java +++ b/src/main/java/gent/zeus/mc13dtl/events/RandomEventExecutor.java @@ -6,19 +6,28 @@ 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> events = Arrays.asList(TestEvent.class); + static List> events = Arrays.asList(TestEvent.class, TestEvent2.class); static List currentEvents = new ArrayList<>(); public static void executeRandomEvent(Team team) { Random random = new Random(); - int i = random.nextInt(events.size()); - Class event = events.get(i); - try { - event.getConstructor(Team.class).newInstance(team); - } catch (Exception e) { + while (true) { + int i = random.nextInt(events.size()); + Class 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(); + } } } } diff --git a/src/main/java/gent/zeus/mc13dtl/events/catalog/TestEvent2.java b/src/main/java/gent/zeus/mc13dtl/events/catalog/TestEvent2.java new file mode 100644 index 0000000..032a804 --- /dev/null +++ b/src/main/java/gent/zeus/mc13dtl/events/catalog/TestEvent2.java @@ -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; + } +} diff --git a/src/main/java/gent/zeus/mc13dtl/group/GroupAddCommand.java b/src/main/java/gent/zeus/mc13dtl/group/GroupAddCommand.java index 697b832..da2f69b 100644 --- a/src/main/java/gent/zeus/mc13dtl/group/GroupAddCommand.java +++ b/src/main/java/gent/zeus/mc13dtl/group/GroupAddCommand.java @@ -24,10 +24,10 @@ public class GroupAddCommand implements BasicCommand { commandSourceStack.getSender().sendMessage("Player " + args[0] + " does not exist."); } } else { - commandSourceStack.getSender().sendMessage("groupAdd usage: /groupadd "); + commandSourceStack.getSender().sendMessage("usage: /group-add "); } } catch (NumberFormatException ex) { - commandSourceStack.getSender().sendMessage("usage: /groupadd "); + commandSourceStack.getSender().sendMessage("usage: /group-add "); } } diff --git a/src/main/java/gent/zeus/mc13dtl/group/GroupDifficultyCommand.java b/src/main/java/gent/zeus/mc13dtl/group/GroupDifficultyCommand.java new file mode 100644 index 0000000..b9e592c --- /dev/null +++ b/src/main/java/gent/zeus/mc13dtl/group/GroupDifficultyCommand.java @@ -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 "); + } + } +} diff --git a/src/main/java/gent/zeus/mc13dtl/group/GroupRemoveCommand.java b/src/main/java/gent/zeus/mc13dtl/group/GroupRemoveCommand.java index 557917e..0698a41 100644 --- a/src/main/java/gent/zeus/mc13dtl/group/GroupRemoveCommand.java +++ b/src/main/java/gent/zeus/mc13dtl/group/GroupRemoveCommand.java @@ -17,7 +17,7 @@ public class GroupRemoveCommand implements BasicCommand { if (player != null) { Team team = MC13DTL.board.getTeam(player); if (team != null) { - team.removeEntry(player.getName()); + team.removeEntry(player.getUniqueId().toString()); player.sendMessage("You have been removed from your team"); } else { 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."); } } else { - commandSourceStack.getSender().sendMessage("group remove usage: /groupremove "); + commandSourceStack.getSender().sendMessage("group remove usage: /group-remove "); } }