zKoth/src/fr/maxlego08/koth/ZKoth.java

885 lines
31 KiB
Java
Raw Normal View History

2024-02-22 11:33:50 +01:00
package fr.maxlego08.koth;
import fr.maxlego08.koth.api.Koth;
2024-02-22 19:04:09 +01:00
import fr.maxlego08.koth.api.KothEvent;
2024-02-22 19:43:52 +01:00
import fr.maxlego08.koth.api.KothLootType;
2024-02-22 14:31:23 +01:00
import fr.maxlego08.koth.api.KothStatus;
import fr.maxlego08.koth.api.KothTeam;
2024-02-22 11:44:35 +01:00
import fr.maxlego08.koth.api.KothType;
2024-02-22 19:04:09 +01:00
import fr.maxlego08.koth.api.discord.DiscordWebhookConfig;
2024-02-22 14:31:23 +01:00
import fr.maxlego08.koth.api.events.KothCapEvent;
import fr.maxlego08.koth.api.events.KothCatchEvent;
import fr.maxlego08.koth.api.events.KothLooseEvent;
import fr.maxlego08.koth.api.events.KothSpawnEvent;
import fr.maxlego08.koth.api.events.KothStartEvent;
import fr.maxlego08.koth.api.events.KothStopEvent;
import fr.maxlego08.koth.api.events.KothWinEvent;
2024-02-22 18:20:24 +01:00
import fr.maxlego08.koth.api.utils.HologramConfig;
2024-02-29 18:14:48 +01:00
import fr.maxlego08.koth.api.utils.PlayerResult;
2024-03-09 11:50:06 +01:00
import fr.maxlego08.koth.api.utils.RandomCommand;
import fr.maxlego08.koth.api.utils.ScoreboardConfiguration;
2024-02-22 14:31:23 +01:00
import fr.maxlego08.koth.hook.teams.NoneHook;
import fr.maxlego08.koth.save.Config;
2024-02-22 16:23:25 +01:00
import fr.maxlego08.koth.scoreboard.ScoreBoardManager;
2024-02-22 14:31:23 +01:00
import fr.maxlego08.koth.zcore.enums.Message;
2024-02-22 16:23:25 +01:00
import fr.maxlego08.koth.zcore.logger.Logger;
2024-02-22 11:33:50 +01:00
import fr.maxlego08.koth.zcore.utils.Cuboid;
import fr.maxlego08.koth.zcore.utils.ProgressBar;
2024-02-22 14:31:23 +01:00
import fr.maxlego08.koth.zcore.utils.ZUtils;
2024-02-22 16:23:25 +01:00
import fr.maxlego08.koth.zcore.utils.builder.TimerBuilder;
import fr.maxlego08.koth.zcore.utils.interfaces.CollectionConsumer;
import fr.mrmicky.fastboard.FastBoard;
2024-02-22 14:31:23 +01:00
import org.bukkit.Bukkit;
2024-02-22 11:33:50 +01:00
import org.bukkit.Location;
2024-02-22 14:31:23 +01:00
import org.bukkit.Material;
2024-02-29 18:14:48 +01:00
import org.bukkit.OfflinePlayer;
2024-02-22 14:31:23 +01:00
import org.bukkit.World;
import org.bukkit.block.BlockFace;
2024-02-22 19:43:52 +01:00
import org.bukkit.block.Chest;
2024-02-22 14:31:23 +01:00
import org.bukkit.command.CommandSender;
2024-02-22 19:43:52 +01:00
import org.bukkit.entity.Item;
2024-02-22 14:31:23 +01:00
import org.bukkit.entity.Player;
2024-02-22 19:43:52 +01:00
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;
2024-02-22 11:33:50 +01:00
import java.util.ArrayList;
2024-02-22 16:23:25 +01:00
import java.util.Collections;
2024-02-29 18:14:48 +01:00
import java.util.Comparator;
2024-02-22 14:31:23 +01:00
import java.util.HashMap;
2024-02-22 11:33:50 +01:00
import java.util.List;
2024-02-22 14:31:23 +01:00
import java.util.Map;
2024-02-22 19:48:33 +01:00
import java.util.Random;
2024-02-22 14:31:23 +01:00
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
2024-02-22 16:23:25 +01:00
import java.util.stream.Collectors;
2024-02-22 11:33:50 +01:00
2024-02-22 14:31:23 +01:00
public class ZKoth extends ZUtils implements Koth {
2024-02-22 11:33:50 +01:00
2024-02-22 14:31:23 +01:00
private final KothPlugin plugin;
2024-02-22 11:33:50 +01:00
private final String fileName;
2024-02-22 11:44:35 +01:00
private final KothType kothType;
private final ScoreboardConfiguration cooldownScoreboard;
private final ScoreboardConfiguration startScoreboard;
2024-02-22 14:31:23 +01:00
private final int cooldownStart;
private final int stopAfterSeconds;
2024-02-22 19:48:33 +01:00
private final int randomItemStacks;
2024-02-22 14:31:23 +01:00
private final Map<UUID, Integer> playersValues = new HashMap<>();
private final boolean enableStartCapMessage;
private final boolean enableLooseCapMessage;
private final boolean enableEverySecondsCapMessage;
2024-02-22 18:20:24 +01:00
private final HologramConfig hologramConfig;
2024-02-22 19:43:52 +01:00
private final KothLootType kothLootType;
2024-02-29 18:14:48 +01:00
private final List<String> blacklistTeamId;
private final ProgressBar progressBar;
2024-03-09 11:50:06 +01:00
private final List<RandomCommand> randomCommands;
private final int maxRandomCommands;
private final DiscordWebhookConfig discordWebhookConfig;
2024-02-22 19:43:52 +01:00
private List<ItemStack> itemStacks;
2024-02-22 11:33:50 +01:00
private String name;
private int captureSeconds;
private Location minLocation;
private Location maxLocation;
private List<String> startCommands = new ArrayList<>();
private List<String> endCommands = new ArrayList<>();
2024-02-22 14:31:23 +01:00
private KothStatus kothStatus = KothStatus.STOP;
private KothTeam kothTeam = new NoneHook();
private Player currentPlayer;
private AtomicInteger remainingSeconds;
private TimerTask timerTask;
private TimerTask timerTaskStop;
2024-02-29 18:14:48 +01:00
private List<PlayerResult> playerResults = new ArrayList<>();
2024-02-22 11:33:50 +01:00
2024-03-09 11:50:06 +01:00
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) {
2024-02-22 14:31:23 +01:00
this.plugin = plugin;
2024-02-22 11:33:50 +01:00
this.fileName = fileName;
2024-02-22 11:44:35 +01:00
this.kothType = kothType;
2024-02-22 11:33:50 +01:00
this.name = name;
this.captureSeconds = captureSeconds;
this.minLocation = minLocation;
this.maxLocation = maxLocation;
this.startCommands = startCommands;
this.endCommands = endCommands;
this.startScoreboard = startScoreboard;
this.cooldownScoreboard = cooldownScoreboard;
2024-02-22 14:31:23 +01:00
this.cooldownStart = cooldownStart;
this.stopAfterSeconds = stopAfterSeconds;
this.enableStartCapMessage = enableStartCapMessage;
this.enableLooseCapMessage = enableLooseCapMessage;
this.enableEverySecondsCapMessage = enableEverySecondsCapMessage;
2024-02-22 18:20:24 +01:00
this.hologramConfig = hologramConfig;
2024-02-22 19:43:52 +01:00
this.itemStacks = itemStacks;
this.kothLootType = kothLootType;
2024-02-22 19:04:09 +01:00
this.discordWebhookConfig = discordWebhookConfig;
2024-02-22 19:48:33 +01:00
this.randomItemStacks = randomItemStacks;
2024-02-29 18:14:48 +01:00
this.blacklistTeamId = blacklistTeamId;
this.progressBar = progressBar;
2024-03-09 11:50:06 +01:00
this.randomCommands = randomCommands;
this.maxRandomCommands = maxRandomCommands;
}
2024-02-22 14:31:23 +01:00
public ZKoth(KothPlugin plugin, String fileName, KothType kothType, String name, int captureSeconds, Location minLocation, Location maxLocation) {
this.plugin = plugin;
this.fileName = fileName;
this.kothType = kothType;
this.name = name;
this.captureSeconds = captureSeconds;
this.minLocation = minLocation;
this.maxLocation = maxLocation;
this.startScoreboard = new ScoreboardConfiguration();
this.cooldownScoreboard = new ScoreboardConfiguration();
2024-02-22 14:31:23 +01:00
this.cooldownStart = 300;
this.stopAfterSeconds = 3600;
this.enableStartCapMessage = true;
this.enableLooseCapMessage = true;
this.enableEverySecondsCapMessage = false;
2024-02-22 18:20:24 +01:00
this.hologramConfig = new HologramConfig(false, new ArrayList<>(), getCenter());
2024-02-22 19:04:09 +01:00
this.discordWebhookConfig = null;
2024-02-22 19:43:52 +01:00
this.itemStacks = new ArrayList<>();
this.kothLootType = KothLootType.NONE;
2024-02-22 19:48:33 +01:00
this.randomItemStacks = 0;
2024-02-29 18:14:48 +01:00
this.blacklistTeamId = new ArrayList<>();
this.progressBar = new ProgressBar(10, '-', "&a", "&7");
2024-03-09 11:50:06 +01:00
this.randomCommands = new ArrayList<>();
this.maxRandomCommands = 0;
2024-02-22 11:33:50 +01:00
}
@Override
public String getFileName() {
return this.fileName;
}
2024-02-22 11:44:35 +01:00
@Override
public KothType getKothType() {
return this.kothType;
}
2024-02-22 11:33:50 +01:00
@Override
public String getName() {
return this.name;
}
2024-02-22 11:44:35 +01:00
@Override
public void setName(String name) {
this.name = name;
}
2024-02-22 11:33:50 +01:00
@Override
public Location getMinLocation() {
return this.minLocation;
}
@Override
public Location getMaxLocation() {
return this.maxLocation;
}
@Override
public Cuboid getCuboid() {
return new Cuboid(this.maxLocation, this.minLocation);
}
2024-02-22 14:31:23 +01:00
@Override
public boolean isEnableStartCapMessage() {
return this.enableStartCapMessage;
}
@Override
public boolean isEnableLooseCapMessage() {
return this.enableLooseCapMessage;
}
@Override
public boolean isEnableEverySecondsCapMessage() {
return this.enableEverySecondsCapMessage;
}
@Override
public int getStopAfterSeconds() {
return this.stopAfterSeconds;
}
2024-02-22 11:33:50 +01:00
@Override
public Location getCenter() {
Cuboid cuboid = getCuboid();
return cuboid.getCenter();
}
@Override
public List<String> getStartCommands() {
return this.startCommands;
}
@Override
public List<String> getEndCommands() {
return this.endCommands;
}
@Override
public void move(Location minLocation, Location maxLocation) {
this.minLocation = minLocation;
this.maxLocation = maxLocation;
}
@Override
2024-02-22 11:44:35 +01:00
public int getCaptureSeconds() {
return captureSeconds;
2024-02-22 11:33:50 +01:00
}
@Override
public void setCaptureSeconds(int captureSeconds) {
this.captureSeconds = captureSeconds;
}
@Override
public ScoreboardConfiguration getCooldownScoreboard() {
return this.cooldownScoreboard;
}
@Override
public ScoreboardConfiguration getStartScoreboard() {
return this.startScoreboard;
}
2024-02-22 14:31:23 +01:00
@Override
public KothStatus getStatus() {
return this.kothStatus;
}
@Override
public void spawn(CommandSender sender, boolean now) {
if (this.minLocation == null || this.maxLocation == null) {
message(sender, Message.SPAWN_ERROR);
} else if (this.kothStatus == KothStatus.COOLDOWN) {
message(sender, Message.SPAWN_COOLDOWN);
} else if (this.kothStatus == KothStatus.START) {
message(sender, Message.SPAWN_ALREADY);
} else {
if (now) spawnNow();
else spawn();
}
}
@Override
public void spawn(boolean now) {
if (this.minLocation == null || this.maxLocation == null || this.kothStatus != KothStatus.STOP) return;
if (now) spawnNow();
else spawn();
}
@Override
2024-02-22 16:26:43 +01:00
public void stop() {
2024-02-22 19:04:09 +01:00
2024-02-22 19:43:52 +01:00
if (this.kothStatus == KothStatus.STOP) return;
2024-02-22 19:04:09 +01:00
2024-02-22 14:31:23 +01:00
KothStopEvent event = new KothStopEvent(this);
event.call();
if (event.isCancelled()) return;
2024-03-08 14:07:37 +01:00
if (this.discordWebhookConfig != null) this.discordWebhookConfig.send(this.plugin, this, KothEvent.STOP);
2024-02-22 14:31:23 +01:00
broadcast(Message.EVENT_STOP);
if (this.timerTask != null) {
this.timerTask.cancel();
}
this.kothStatus = KothStatus.STOP;
this.currentPlayer = null;
this.timerTask = null;
this.remainingSeconds = null;
this.playersValues.clear();
2024-02-22 16:23:25 +01:00
this.plugin.getScoreBoardManager().clearBoard();
2024-02-22 14:31:23 +01:00
// this.resetBlocks();
if (this.timerTaskStop != null) this.timerTaskStop.cancel();
2024-02-22 18:20:24 +01:00
this.plugin.getKothHologram().end(this);
2024-02-22 14:31:23 +01:00
}
2024-02-22 16:26:43 +01:00
@Override
public void stop(CommandSender sender) {
2024-03-04 16:01:05 +01:00
if (this.kothStatus == KothStatus.STOP) {
2024-02-22 16:26:43 +01:00
message(sender, Message.EVENT_DISABLE);
return;
}
this.stop();
}
2024-02-22 14:31:23 +01:00
private void spawn() {
this.resetData();
this.kothStatus = KothStatus.COOLDOWN;
this.remainingSeconds = new AtomicInteger(this.cooldownStart);
KothStartEvent event = new KothStartEvent(this);
event.call();
if (event.isCancelled()) return;
2024-03-08 14:07:37 +01:00
if (this.discordWebhookConfig != null) this.discordWebhookConfig.send(this.plugin, this, KothEvent.START);
2024-02-22 19:04:09 +01:00
2024-02-22 16:23:25 +01:00
if (this.cooldownScoreboard.isEnable()) {
ScoreBoardManager scoreBoardManager = this.plugin.getScoreBoardManager();
scoreBoardManager.setLinesAndSchedule(onScoreboard());
for (Player player : Bukkit.getOnlinePlayers()) {
scoreBoardManager.createBoard(player, color(this.cooldownScoreboard.getTitle()));
}
}
2024-02-22 14:31:23 +01:00
this.timerTask = scheduleFix(this.plugin, 0, Config.enableDebug ? 10 : 1000, (task, isCancelled) -> {
if (!isCancelled) {
task.cancel();
return;
}
if (this.kothStatus != KothStatus.COOLDOWN) {
task.cancel();
return;
}
int currentRemainingSeconds = this.remainingSeconds.get();
if (Config.displayMessageCooldown.contains(currentRemainingSeconds)) {
broadcast(Message.EVENT_COOLDOWN);
}
if (currentRemainingSeconds <= 0) {
this.timerTask.cancel();
this.spawnNow();
return;
}
this.plugin.getScoreBoardManager().update();
2024-02-22 14:31:23 +01:00
this.remainingSeconds.decrementAndGet();
});
}
private void spawnNow() {
this.resetData();
this.kothStatus = KothStatus.START;
KothSpawnEvent event = new KothSpawnEvent(this);
event.call();
if (event.isCancelled()) return;
2024-03-08 14:07:37 +01:00
if (this.discordWebhookConfig != null) this.discordWebhookConfig.send(this.plugin, this, KothEvent.SPAWN);
2024-02-22 14:31:23 +01:00
this.remainingSeconds = new AtomicInteger(this.captureSeconds);
2024-02-22 16:23:25 +01:00
broadcast(Message.EVENT_START);
ScoreBoardManager scoreBoardManager = this.plugin.getScoreBoardManager();
if (!this.cooldownScoreboard.isEnable() && this.startScoreboard.isEnable()) {
scoreBoardManager.setLinesAndSchedule(onScoreboard());
for (Player player : Bukkit.getOnlinePlayers()) {
scoreBoardManager.createBoard(player, color(this.startScoreboard.getTitle()));
}
} else if (this.cooldownScoreboard.isEnable() && this.startScoreboard.isEnable()) {
if (scoreBoardManager.getBoards().isEmpty()) {
scoreBoardManager.setLinesAndSchedule(onScoreboard());
for (Player player : Bukkit.getOnlinePlayers()) {
scoreBoardManager.createBoard(player, color(this.startScoreboard.getTitle()));
}
} else {
for (Player player : Bukkit.getOnlinePlayers()) {
FastBoard board = scoreBoardManager.getBoard(player);
if (board == null) scoreBoardManager.createBoard(player, color(this.startScoreboard.getTitle()));
else board.updateTitle(color(this.startScoreboard.getTitle()));
}
}
}
2024-02-22 14:31:23 +01:00
// this.changeBlocks(Config.noOneCapturingMaterial, true);
/*if (this.kothType == KothType.POINT_COUNT) {
this.startschedule();
}*/
Koth koth = this;
Timer timer = new Timer();
this.timerTaskStop = new TimerTask() {
@Override
public void run() {
2024-02-22 19:04:09 +01:00
plugin.getKothHologram().end(koth);
2024-02-22 16:23:25 +01:00
Bukkit.getScheduler().runTask(plugin, () -> stop(Bukkit.getConsoleSender()));
2024-02-22 14:31:23 +01:00
}
};
timer.schedule(this.timerTaskStop, this.stopAfterSeconds * 1000L);
2024-02-22 18:20:24 +01:00
this.plugin.getKothHologram().start(this);
2024-02-22 14:31:23 +01:00
/*if (Config.discordWebhookConfig != null) {
Config.discordWebhookConfig.send(this.plugin, this);
}*/
this.startCommands.forEach(command -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), replaceKothInformations(command)));
}
private void resetData() {
this.playersValues.clear();
this.currentPlayer = null;
}
@Override
public void playerMove(Player player, KothTeam kothTeam) {
if (this.kothStatus != KothStatus.START) return;
this.kothTeam = kothTeam;
2024-02-29 18:14:48 +01:00
if (this.blacklistTeamId.contains(this.kothTeam.getTeamId(player))) return;
2024-02-22 14:31:23 +01:00
Cuboid cuboid = this.getCuboid();
if (this.currentPlayer == null && cuboid.contains(player.getLocation())) {
this.currentPlayer = player;
this.startCap(player);
2024-02-24 12:11:49 +01:00
updateDisplay();
2024-02-22 14:31:23 +01:00
} else if (this.currentPlayer != null && !cuboid.contains(this.currentPlayer.getLocation())) {
KothLooseEvent event = new KothLooseEvent(this.currentPlayer, this);
event.call();
2024-03-08 14:07:37 +01:00
if (this.discordWebhookConfig != null) this.discordWebhookConfig.send(this.plugin, this, KothEvent.LOOSE);
2024-02-22 19:04:09 +01:00
2024-02-22 14:31:23 +01:00
if (event.isCancelled()) return;
broadcast(Message.EVENT_LOOSE);
if (this.timerTask != null) {
this.timerTask.cancel();
}
// this.changeBlocks(Config.noOneCapturingMaterial, true);
this.remainingSeconds = new AtomicInteger(this.captureSeconds);
this.timerTask = null;
this.currentPlayer = null;
2024-02-24 12:11:49 +01:00
updateDisplay();
2024-02-22 14:31:23 +01:00
}
}
@Override
public int getCooldownStart() {
return this.cooldownStart;
}
private void startCap(Player player) {
2024-02-22 16:23:25 +01:00
if (this.currentPlayer == null) return;
2024-02-22 14:31:23 +01:00
KothCatchEvent event = new KothCatchEvent(this, player, this.captureSeconds);
event.call();
if (event.isCancelled()) {
this.currentPlayer = null;
return;
}
2024-03-08 14:07:37 +01:00
if (this.discordWebhookConfig != null) this.discordWebhookConfig.send(this.plugin, this, KothEvent.START_CAP);
2024-02-22 19:04:09 +01:00
2024-02-22 14:31:23 +01:00
if (enableStartCapMessage) {
broadcast(Message.EVENT_CATCH);
}
int captureSeconds = event.getCaptureSeconds();
captureSeconds = captureSeconds < 0 ? 30 : captureSeconds;
this.remainingSeconds = new AtomicInteger(captureSeconds);
Cuboid cuboid = getCuboid();
// this.changeBlocks(Config.onePersonneCapturingMaterial, false);
2024-02-24 12:11:49 +01:00
updateDisplay();
2024-02-22 14:31:23 +01:00
scheduleFix(this.plugin, 0, 1000, (task, isCancelled) -> {
this.timerTask = task;
if (!isCancelled) {
task.cancel();
return;
}
if (this.kothStatus != KothStatus.START) {
task.cancel();
return;
}
int currentRemainingSeconds = this.remainingSeconds.get();
if (this.currentPlayer != null) {
2024-02-22 16:23:25 +01:00
if (!this.currentPlayer.isValid() || !this.currentPlayer.isOnline() || !cuboid.contains(this.currentPlayer.getLocation())) {
2024-02-22 14:31:23 +01:00
this.currentPlayer = null;
2024-02-24 12:11:49 +01:00
updateDisplay();
2024-02-22 14:31:23 +01:00
}
}
if (this.currentPlayer == null) {
KothLooseEvent kothLooseEvent = new KothLooseEvent(null, this);
kothLooseEvent.call();
2024-02-22 19:04:09 +01:00
if (kothLooseEvent.isCancelled()) return;
2024-03-08 14:07:37 +01:00
if (this.discordWebhookConfig != null)
this.discordWebhookConfig.send(this.plugin, this, KothEvent.LOOSE);
2024-02-22 14:31:23 +01:00
if (this.timerTask != null) {
this.timerTask.cancel();
}
// this.changeBlocks(Config.noOneCapturingMaterial, true);
this.timerTask = null;
this.currentPlayer = null;
this.remainingSeconds = new AtomicInteger(this.captureSeconds);
if (enableLooseCapMessage) {
broadcast(Message.EVENT_LOOSE);
}
2024-02-24 12:11:49 +01:00
updateDisplay();
2024-02-22 14:31:23 +01:00
return;
}
if (this.hasWin()) {
this.endKoth(task, cuboid, player);
} else {
KothCapEvent capEvent = new KothCapEvent(this, player, this.remainingSeconds.get(), this.kothTeam.getTeamName(player));
capEvent.call();
2024-02-22 14:31:23 +01:00
2024-02-22 19:58:18 +01:00
if (Config.displayMessageKothCap.contains(currentRemainingSeconds)) {
broadcast(Message.EVENT_TIMER);
} else if (enableEverySecondsCapMessage) {
broadcast(Message.EVENT_EVERYSECONDS);
}
2024-02-24 12:11:49 +01:00
updateDisplay();
2024-02-22 19:58:18 +01:00
2024-02-22 14:31:23 +01:00
switch (this.kothType) {
case CAPTURE:
default:
this.remainingSeconds.decrementAndGet();
break;
case SCORE:
// case TIMER:
this.playersValues.put(this.currentPlayer.getUniqueId(), this.getValue(this.currentPlayer) + 1);
2024-02-29 18:14:48 +01:00
this.playerResults.clear(); // Clear cache
2024-02-22 14:31:23 +01:00
break;
}
}
});
}
2024-03-09 11:50:06 +01:00
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));
}
}
2024-02-22 14:31:23 +01:00
public void endKoth(TimerTask task, Cuboid cuboid, Player player) {
KothWinEvent kothWinEvent = new KothWinEvent(this, this.currentPlayer);
kothWinEvent.call();
if (kothWinEvent.isCancelled()) return;
2024-03-08 14:07:37 +01:00
if (this.discordWebhookConfig != null) this.discordWebhookConfig.send(this.plugin, this, KothEvent.WIN);
2024-02-22 19:04:09 +01:00
2024-02-22 14:31:23 +01:00
task.cancel();
broadcast(Message.EVENT_WIN);
2024-02-24 12:11:49 +01:00
this.plugin.getKothHologram().end(this);
2024-02-22 16:23:25 +01:00
this.plugin.getScoreBoardManager().clearBoard();
2024-03-09 11:50:06 +01:00
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++;
2024-02-22 14:31:23 +01:00
}
}
2024-03-09 11:50:06 +01:00
}
2024-02-22 14:31:23 +01:00
Location center = cuboid.getCenter();
World world = center.getWorld();
while (center.getBlock().getRelative(BlockFace.DOWN).getType().equals(Material.AIR)) {
center = center.getBlock().getRelative(BlockFace.DOWN).getLocation();
}
2024-02-22 19:43:52 +01:00
Location location = center.clone();
2024-02-22 14:31:23 +01:00
2024-02-22 19:43:52 +01:00
if (this.itemStacks.size() != 0) {
switch (this.kothLootType) {
2024-02-22 14:31:23 +01:00
case CHEST:
location.getBlock().setType(Material.CHEST);
Chest chest = (Chest) location.getBlock().getState();
2024-02-22 19:48:33 +01:00
this.getRandomItemStacks().forEach(itemStack -> chest.getInventory().addItem(itemStack));
2024-02-22 14:31:23 +01:00
2024-02-22 19:43:52 +01:00
Bukkit.getScheduler().runTaskLater(this.plugin, () -> location.getBlock().setType(Material.AIR), 20L * Config.removeChestSec);
2024-02-22 14:31:23 +01:00
break;
case DROP:
location.add(0.5, 0.3, 0.5);
2024-02-22 19:48:33 +01:00
this.getRandomItemStacks().forEach(itemStack -> {
2024-02-22 14:31:23 +01:00
Item item = world.dropItem(location, itemStack);
Vector vector = item.getVelocity();
vector.setZ(0);
vector.setY(0.5);
vector.setX(0);
item.setVelocity(vector);
});
break;
case INVENTORY:
2024-02-22 19:48:33 +01:00
this.getRandomItemStacks().forEach(itemStack -> give(this.currentPlayer, itemStack));
2024-02-22 14:31:23 +01:00
break;
default:
break;
}
2024-02-22 19:43:52 +01:00
}
2024-02-22 14:31:23 +01:00
this.kothStatus = KothStatus.STOP;
this.currentPlayer = null;
this.timerTask = null;
this.remainingSeconds = null;
this.playersValues.clear();
// this.resetBlocks();
2024-02-22 16:23:25 +01:00
if (this.timerTaskStop != null) this.timerTaskStop.cancel();
2024-02-22 14:31:23 +01:00
}
public int getValue(Player player) {
return player == null ? 0 : this.playersValues.getOrDefault(player.getUniqueId(), 0);
}
public boolean hasWin() {
switch (this.kothType) {
case CAPTURE:
return this.remainingSeconds != null && this.remainingSeconds.get() <= 0;
case SCORE:
return this.currentPlayer != null && this.getValue(this.currentPlayer) >= this.captureSeconds;
/*case TIMER:
return this.currentPlayer == null ? false : this.getValue(this.currentPlayer) >= this.maxSecondsCap;*/
default:
return false;
}
}
2024-02-22 16:23:25 +01:00
private void broadcast(Message message) {
switch (message.getType()) {
case ACTION: {
if (message.getMessage() == null) {
Logger.info(message.name() + " is null, check your config plz !", Logger.LogType.ERROR);
return;
}
String realMessage = replaceMessage(message.getMessage());
this.broadcastAction(realMessage);
break;
}
case CENTER: {
if (message.getMessages().size() == 0) {
String realMessage = replaceMessage(message.getMessage());
broadcastCenterMessage(Collections.singletonList(realMessage));
} else {
broadcastCenterMessage(message.getMessages().stream().map(this::replaceMessage).collect(Collectors.toList()));
}
break;
}
case TCHAT: {
if (message.getMessages().size() == 0) this.broadcast(replaceMessage(message.getMessage()));
else message.getMessages().forEach(m -> this.broadcast(replaceMessage(m)));
break;
}
case TITLE: {
String title = replaceMessage(message.getTitle());
String subTitle = replaceMessage(message.getSubTitle());
int fadeInTime = message.getStart();
int showTime = message.getTime();
int fadeOutTime = message.getEnd();
for (Player player : Bukkit.getOnlinePlayers()) {
this.title(player, title, subTitle, fadeInTime, showTime, fadeOutTime);
}
break;
}
case NONE:
default:
break;
}
}
2024-02-22 18:20:24 +01:00
@Override
public String replaceMessage(String string) {
2024-02-22 14:31:23 +01:00
2024-02-22 19:04:09 +01:00
if (string == null) return null;
2024-02-22 14:31:23 +01:00
string = string.replace("%playerName%", this.currentPlayer != null ? this.currentPlayer.getName() : Config.noPlayer);
string = string.replace("%teamName%", this.currentPlayer != null ? this.kothTeam.getTeamName(this.currentPlayer) : Config.noFaction);
string = string.replace("%teamId%", this.currentPlayer != null ? this.kothTeam.getTeamId(this.currentPlayer) : Config.noFaction);
string = string.replace("%teamLeader%", this.currentPlayer != null ? this.kothTeam.getTeamName(this.currentPlayer) : Config.noFaction);
2024-02-22 16:23:25 +01:00
int seconds = this.remainingSeconds == null ? this.captureSeconds : this.remainingSeconds.get();
string = string.replace("%captureFormat%", TimerBuilder.getStringTime(seconds));
string = string.replace("%captureSeconds%", String.valueOf(seconds));
2024-02-22 14:31:23 +01:00
return replaceKothInformations(string);
}
2024-02-22 19:04:09 +01:00
@Override
public DiscordWebhookConfig getDiscordWebhookConfig() {
return this.discordWebhookConfig;
}
2024-02-22 19:43:52 +01:00
@Override
public List<ItemStack> getItemStacks() {
return this.itemStacks;
}
@Override
public void setItemStacks(List<ItemStack> itemStacks) {
this.itemStacks = itemStacks;
}
@Override
public KothLootType getLootType() {
return this.kothLootType;
}
2024-02-22 14:31:23 +01:00
private String replaceKothInformations(String string) {
Location centerLocation = getCenter();
2024-02-22 16:23:25 +01:00
int seconds = this.remainingSeconds == null ? this.captureSeconds : this.remainingSeconds.get();
string = string.replace("%spawnSeconds%", String.valueOf(seconds));
string = string.replace("%spawnFormat%", TimerBuilder.getStringTime(seconds));
2024-02-22 14:31:23 +01:00
string = string.replace("%name%", this.name);
string = string.replace("%world%", centerLocation.getWorld().getName());
string = string.replace("%minX%", String.valueOf(minLocation.getBlockX()));
string = string.replace("%minY%", String.valueOf(minLocation.getBlockY()));
string = string.replace("%minZ%", String.valueOf(minLocation.getBlockZ()));
string = string.replace("%maxX%", String.valueOf(maxLocation.getBlockX()));
string = string.replace("%maxY%", String.valueOf(maxLocation.getBlockY()));
string = string.replace("%maxZ%", String.valueOf(maxLocation.getBlockZ()));
string = string.replace("%centerX%", String.valueOf(centerLocation.getBlockX()));
string = string.replace("%centerY%", String.valueOf(centerLocation.getBlockY()));
string = string.replace("%centerZ%", String.valueOf(centerLocation.getBlockZ()));
return string;
}
2024-02-22 16:23:25 +01:00
@Override
public CollectionConsumer<Player> onScoreboard() {
return player -> {
ScoreboardConfiguration scoreboard = (this.kothStatus == KothStatus.COOLDOWN) ? this.cooldownScoreboard : this.startScoreboard;
return scoreboard.getLines().stream().map(e -> color(papi(replaceMessage(e), player))).collect(Collectors.toList());
};
}
2024-02-22 18:20:24 +01:00
@Override
public HologramConfig getHologramConfig() {
return this.hologramConfig;
}
2024-02-22 19:48:33 +01:00
@Override
public List<ItemStack> getRandomItemStacks() {
List<ItemStack> itemStacks = new ArrayList<>(this.itemStacks);
if (this.randomItemStacks <= 0) {
return itemStacks;
}
Collections.shuffle(itemStacks, new Random());
return itemStacks.stream().limit(this.randomItemStacks).collect(Collectors.toList());
}
2024-02-22 19:58:18 +01:00
@Override
public int getRandomItemStack() {
return this.randomItemStacks;
}
2024-02-22 21:29:34 +01:00
@Override
public int getRemainingSeconds() {
return this.remainingSeconds == null ? this.captureSeconds : this.remainingSeconds.get();
2024-02-22 21:29:34 +01:00
}
@Override
public Player getCurrentPlayer() {
return this.currentPlayer;
}
2024-02-24 12:11:49 +01:00
@Override
public void updateDisplay() {
this.plugin.getKothHologram().update(this);
this.plugin.getScoreBoardManager().update();
}
2024-02-29 18:14:48 +01:00
@Override
public int getScore(Player player) {
2024-03-31 12:37:50 +02:00
return player == null ? 0 : this.playersValues.getOrDefault(player.getUniqueId(), 0);
2024-02-29 18:14:48 +01:00
}
@Override
public PlayerResult getPlayer(int position) {
this.sortScores();
2024-04-23 12:01:40 +02:00
if (this.playerResults.isEmpty() || position > this.playerResults.size()) return Config.defaultPlayerResult;
2024-02-29 18:14:48 +01:00
return this.playerResults.get(position - 1);
}
@Override
public List<String> getBlacklistTeamId() {
return this.blacklistTeamId;
}
private void sortScores() {
// If the list is not empty, then there is already a cache.
if (!this.playerResults.isEmpty()) return;
this.playerResults = this.playersValues.entrySet().stream().map(entry -> {
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(entry.getKey());
int points = entry.getValue();
String teamName = this.kothTeam.getTeamName(offlinePlayer);
String teamLeader = this.kothTeam.getLeaderName(offlinePlayer);
String teamId = this.kothTeam.getTeamId(offlinePlayer);
return new PlayerResult(offlinePlayer.getName(), points, teamName, teamId, teamLeader);
}).sorted(Comparator.comparingInt(PlayerResult::getPoints).reversed()).collect(Collectors.toList());
}
@Override
public ProgressBar getProgressBar() {
return this.progressBar;
}
2024-03-09 11:50:06 +01:00
@Override
public List<RandomCommand> getRandomCommands() {
return this.randomCommands;
}
@Override
public int getMaxRandomCommands() {
return this.maxRandomCommands;
}
2024-02-22 11:33:50 +01:00
}