🚧 Add random Commands

This commit is contained in:
Maxlego08 2024-03-09 11:50:06 +01:00
parent 0f4c74885a
commit e51ab17cf7
6 changed files with 125 additions and 16 deletions

View File

@ -1,5 +1,6 @@
# Unreleased
- Add random command on koth win
- Fix error with discord webhook on stop
- Fix problem with placeholder on koth spawn

View File

@ -16,6 +16,7 @@ import fr.maxlego08.koth.api.events.KothStopEvent;
import fr.maxlego08.koth.api.events.KothWinEvent;
import fr.maxlego08.koth.api.utils.HologramConfig;
import fr.maxlego08.koth.api.utils.PlayerResult;
import fr.maxlego08.koth.api.utils.RandomCommand;
import fr.maxlego08.koth.api.utils.ScoreboardConfiguration;
import fr.maxlego08.koth.hook.teams.NoneHook;
import fr.maxlego08.koth.save.Config;
@ -72,6 +73,9 @@ public class ZKoth extends ZUtils implements Koth {
private final KothLootType kothLootType;
private final List<String> blacklistTeamId;
private final ProgressBar progressBar;
private final List<RandomCommand> randomCommands;
private final int maxRandomCommands;
private final DiscordWebhookConfig discordWebhookConfig;
private List<ItemStack> itemStacks;
private String name;
private int captureSeconds;
@ -85,10 +89,9 @@ public class ZKoth extends ZUtils implements Koth {
private AtomicInteger remainingSeconds;
private TimerTask timerTask;
private TimerTask timerTaskStop;
private final DiscordWebhookConfig discordWebhookConfig;
private List<PlayerResult> playerResults = new ArrayList<>();
public ZKoth(KothPlugin plugin, String fileName, KothType kothType, String name, int captureSeconds, Location minLocation, Location maxLocation, List<String> startCommands, List<String> endCommands, ScoreboardConfiguration cooldownScoreboard, ScoreboardConfiguration startScoreboard, int cooldownStart, int stopAfterSeconds, boolean enableStartCapMessage, boolean enableLooseCapMessage, boolean enableEverySecondsCapMessage, HologramConfig hologramConfig, List<ItemStack> itemStacks, KothLootType kothLootType, DiscordWebhookConfig discordWebhookConfig, int randomItemStacks, List<String> blacklistTeamId, ProgressBar progressBar) {
public ZKoth(KothPlugin plugin, String fileName, KothType kothType, String name, int captureSeconds, Location minLocation, Location maxLocation, List<String> startCommands, List<String> endCommands, ScoreboardConfiguration cooldownScoreboard, ScoreboardConfiguration startScoreboard, int cooldownStart, int stopAfterSeconds, boolean enableStartCapMessage, boolean enableLooseCapMessage, boolean enableEverySecondsCapMessage, HologramConfig hologramConfig, List<ItemStack> itemStacks, KothLootType kothLootType, DiscordWebhookConfig discordWebhookConfig, int randomItemStacks, List<String> blacklistTeamId, ProgressBar progressBar, List<RandomCommand> randomCommands, int maxRandomCommands) {
this.plugin = plugin;
this.fileName = fileName;
this.kothType = kothType;
@ -112,6 +115,8 @@ public class ZKoth extends ZUtils implements Koth {
this.randomItemStacks = randomItemStacks;
this.blacklistTeamId = blacklistTeamId;
this.progressBar = progressBar;
this.randomCommands = randomCommands;
this.maxRandomCommands = maxRandomCommands;
}
public ZKoth(KothPlugin plugin, String fileName, KothType kothType, String name, int captureSeconds, Location minLocation, Location maxLocation) {
@ -136,6 +141,8 @@ public class ZKoth extends ZUtils implements Koth {
this.randomItemStacks = 0;
this.blacklistTeamId = new ArrayList<>();
this.progressBar = new ProgressBar(10, '-', "&a", "&7");
this.randomCommands = new ArrayList<>();
this.maxRandomCommands = 0;
}
@Override
@ -575,6 +582,19 @@ public class ZKoth extends ZUtils implements Koth {
});
}
private void dispatchCommand(String command, Player player) {
if (command.contains("%online-player%")) {
for (Player cPlayer : this.kothTeam.getOnlinePlayer(player)) {
String finaleCommand = replaceMessage(command);
finaleCommand = finaleCommand.replace("%online-player%", cPlayer.getName());
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), papi(finaleCommand, cPlayer));
}
} else {
command = replaceMessage(command);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), papi(command, player));
}
}
public void endKoth(TimerTask task, Cuboid cuboid, Player player) {
KothWinEvent kothWinEvent = new KothWinEvent(this, this.currentPlayer);
@ -590,18 +610,17 @@ public class ZKoth extends ZUtils implements Koth {
this.plugin.getKothHologram().end(this);
this.plugin.getScoreBoardManager().clearBoard();
this.endCommands.forEach(command -> {
if (command.contains("%online-player%")) {
for (Player cPlayer : this.kothTeam.getOnlinePlayer(player)) {
String finaleCommand = replaceMessage(command);
finaleCommand = finaleCommand.replace("%online-player%", cPlayer.getName());
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), papi(finaleCommand, cPlayer));
this.endCommands.forEach(command -> dispatchCommand(command, player));
if (this.maxRandomCommands != 0 && this.randomCommands.size() != 0) {
int executedCommands = 0;
while (executedCommands < maxRandomCommands) {
RandomCommand randomCommand = randomElement(this.randomCommands);
if (randomCommand.canUse()) {
randomCommand.getCommands().forEach(command -> dispatchCommand(command, player));
executedCommands++;
}
} else {
command = replaceMessage(command);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), papi(command, player));
}
});
}
Location center = cuboid.getCenter();
World world = center.getWorld();
@ -852,4 +871,14 @@ public class ZKoth extends ZUtils implements Koth {
public ProgressBar getProgressBar() {
return this.progressBar;
}
@Override
public List<RandomCommand> getRandomCommands() {
return this.randomCommands;
}
@Override
public int getMaxRandomCommands() {
return this.maxRandomCommands;
}
}

View File

@ -3,6 +3,7 @@ package fr.maxlego08.koth.api;
import fr.maxlego08.koth.api.discord.DiscordWebhookConfig;
import fr.maxlego08.koth.api.utils.HologramConfig;
import fr.maxlego08.koth.api.utils.PlayerResult;
import fr.maxlego08.koth.api.utils.RandomCommand;
import fr.maxlego08.koth.api.utils.ScoreboardConfiguration;
import fr.maxlego08.koth.zcore.utils.Cuboid;
import fr.maxlego08.koth.zcore.utils.ProgressBar;
@ -13,13 +14,12 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/**
* The Koth interface represents all the essential information and functionalities of a King of the Hill (KOTH) game.
* It includes methods for managing game settings, locations, statuses, and interactions. This interface is central to
* the KOTH plugin, providing a comprehensive set of operations for configuring and controlling KOTH games.
*
* <p>
* Implementing this interface allows for detailed customization and control over the KOTH games, including game dynamics,
* area definitions, scoreboard configurations, and more. It's designed to facilitate seamless integration with other game
* management components, such as teams, scoreboards, and holograms, enhancing the overall gameplay experience.
@ -300,5 +300,9 @@ public interface Koth {
List<String> getBlacklistTeamId();
ProgressBar getProgressBar();
List<RandomCommand> getRandomCommands();
int getMaxRandomCommands();
}

View File

@ -0,0 +1,28 @@
package fr.maxlego08.koth.api.utils;
import fr.maxlego08.koth.zcore.utils.ZUtils;
import java.util.List;
public class RandomCommand extends ZUtils {
private final int percent;
private final List<String> commands;
public RandomCommand(int percent, List<String> commands) {
this.percent = percent;
this.commands = commands;
}
public int getPercent() {
return percent;
}
public List<String> getCommands() {
return commands;
}
public boolean canUse() {
return percent >= getNumberBetween(0, 100);
}
}

View File

@ -7,6 +7,7 @@ import fr.maxlego08.koth.api.KothLootType;
import fr.maxlego08.koth.api.KothType;
import fr.maxlego08.koth.api.discord.DiscordWebhookConfig;
import fr.maxlego08.koth.api.utils.HologramConfig;
import fr.maxlego08.koth.api.utils.RandomCommand;
import fr.maxlego08.koth.api.utils.ScoreboardConfiguration;
import fr.maxlego08.koth.zcore.utils.ProgressBar;
import fr.maxlego08.koth.zcore.utils.ZUtils;
@ -17,7 +18,10 @@ import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class KothLoader extends ZUtils implements Loader<Koth> {
@ -74,8 +78,21 @@ public class KothLoader extends ZUtils implements Loader<Koth> {
ProgressBar progressBar = progressBarLoader.load(configuration, "progressBar.", file);
return new ZKoth(this.plugin, fileName, kothType, name, captureSeconds, minLocation, maxLocation, startCommands, endCommands, cooldownScoreboard,
startScoreboard, cooldownStart, stopAfterSeconds, enableStartCapMessage, enableLooseCapMessage, enableEverySecondsCapMessage, hologramConfig, itemStacks, kothLootType, discordWebhookConfig, randomItemStacks, blacklistTeamId, progressBar);
List<RandomCommand> randomCommands = new ArrayList<>();
int maxRandomCommands = configuration.getInt("randomEndCommands.commandAmount", 0);
List<?> list = configuration.getList("randomEndCommands.commands", null);
if (list != null) {
list.forEach(value -> {
if (value instanceof Map<?, ?>) {
Map<?, ?> map = (Map<?, ?>) value;
randomCommands.add(new RandomCommand(((Number) map.get("percent")).intValue(), (List<String>) map.get("commands")));
}
});
}
System.out.println("COMMANDS " + randomCommands + " - " + maxRandomCommands);
return new ZKoth(this.plugin, fileName, kothType, name, captureSeconds, minLocation, maxLocation, startCommands, endCommands, cooldownScoreboard, startScoreboard, cooldownStart, stopAfterSeconds, enableStartCapMessage, enableLooseCapMessage, enableEverySecondsCapMessage, hologramConfig, itemStacks, kothLootType, discordWebhookConfig, randomItemStacks, blacklistTeamId, progressBar, randomCommands, maxRandomCommands);
}
@Override
@ -88,6 +105,17 @@ public class KothLoader extends ZUtils implements Loader<Koth> {
configuration.set("stopAfterSeconds", koth.getStopAfterSeconds());
configuration.set("startCommands", koth.getStartCommands());
configuration.set("endCommands", koth.getEndCommands());
configuration.set("randomEndCommands.commandAmount", koth.getMaxRandomCommands());
List<Map<String, Object>> endRandomCommands = new ArrayList<>();
koth.getRandomCommands().forEach(randomCommand -> {
Map<String, Object> map = new HashMap<>();
map.put("percent", randomCommand.getPercent());
map.put("commands", randomCommand.getCommands());
endRandomCommands.add(map);
});
configuration.set("randomEndCommands.commands", endRandomCommands);
configuration.set("enableStartCapMessage", koth.isEnableStartCapMessage());
configuration.set("enableLooseCapMessage", koth.isEnableLooseCapMessage());
configuration.set("enableEverySecondsCapMessage", koth.isEnableEverySecondsCapMessage());

View File

@ -47,6 +47,25 @@ startCommands: []
# Commands that will be executed when the koth is won by a player
endCommands: []
# Allows to run random commands at the end of the koth
randomEndCommands:
# Number of commands to execute
commandAmount: 2
# List of possible commands
commands:
- percent: 20 # Command percent
commands: # Commands list
- "bc random command #1"
- percent: 20 # Command percent
commands: # Commands list
- "bc random command #2"
- percent: 10 # Command percent
commands: # Commands list
- "bc random command #3"
- percent: 40 # Command percent
commands: # Commands list
- "bc random command #4"
# First location
minLocation:
world: world