added team task management
This commit is contained in:
@ -0,0 +1,52 @@
|
|||||||
|
package eu.mhsl.craftattack.spawn.varo.appliances.internal.teamTasks;
|
||||||
|
|
||||||
|
import eu.mhsl.craftattack.spawn.core.Main;
|
||||||
|
import eu.mhsl.craftattack.spawn.core.appliance.Appliance;
|
||||||
|
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceCommand;
|
||||||
|
import eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.teams.VaroTeam;
|
||||||
|
import org.bukkit.scheduler.BukkitTask;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class TeamTasks extends Appliance {
|
||||||
|
public enum Type {
|
||||||
|
/**
|
||||||
|
* Task for kicking Team after the desired Playtime
|
||||||
|
*/
|
||||||
|
TIME_KICK
|
||||||
|
}
|
||||||
|
|
||||||
|
private final Map<VaroTeam, Map<Type, BukkitTask>> tasks = new HashMap<>();
|
||||||
|
|
||||||
|
private Map<Type, BukkitTask> getTeamTasks(VaroTeam team) {
|
||||||
|
return this.tasks.computeIfAbsent(team, varoTeam -> new HashMap<>());
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<Type, BukkitTask> getRunningTeamTasks(VaroTeam team) {
|
||||||
|
return this.getTeamTasks(team).entrySet().stream()
|
||||||
|
.filter(entry -> !entry.getValue().isCancelled())
|
||||||
|
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void cancelTeamTasks(VaroTeam team) {
|
||||||
|
Main.logger().info(String.format("All TeamTasks for Team %s were cancelled: %s", team.name, this.getRunningTeamTasks(team)));
|
||||||
|
this.getTeamTasks(team).forEach((type, bukkitTask) -> bukkitTask.cancel());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addTask(VaroTeam team, Type type, BukkitTask runnable) {
|
||||||
|
if(this.getTeamTasks(team).containsKey(type) && !this.getTeamTasks(team).get(type).isCancelled()) {
|
||||||
|
throw new IllegalStateException(String.format("Task %s for Team %s was already running!", type.name(), team.name));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.getTeamTasks(team).put(type, runnable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected @NotNull List<ApplianceCommand<?>> commands() {
|
||||||
|
return List.of(new TeamTasksCommand());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
package eu.mhsl.craftattack.spawn.varo.appliances.internal.teamTasks;
|
||||||
|
|
||||||
|
import eu.mhsl.craftattack.spawn.core.Main;
|
||||||
|
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceCommand;
|
||||||
|
import eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.teams.Teams;
|
||||||
|
import org.bukkit.command.Command;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class TeamTasksCommand extends ApplianceCommand<TeamTasks> {
|
||||||
|
public TeamTasksCommand() {
|
||||||
|
super("teamTasks");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void execute(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||||
|
if(args.length < 1) throw new Error("Please specify Teamname");
|
||||||
|
var team = Main.instance().getAppliance(Teams.class).findTeamByName(args[0]);
|
||||||
|
if(team == null) throw new Error("Team not found!");
|
||||||
|
var tasks = this.getAppliance().getRunningTeamTasks(team);
|
||||||
|
if(tasks.isEmpty()) {
|
||||||
|
sender.sendMessage("No Tasks found!");
|
||||||
|
} else {
|
||||||
|
sender.sendMessage(
|
||||||
|
tasks.entrySet()
|
||||||
|
.stream()
|
||||||
|
.map(entry -> String.format("%s: %d", entry.getKey().name(), entry.getValue().getTaskId()))
|
||||||
|
.collect(Collectors.joining("\n"))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||||
|
var teams = Main.instance().getAppliance(Teams.class).getAllTeams();
|
||||||
|
return teams.stream().map(team -> team.name).toList();
|
||||||
|
}
|
||||||
|
}
|
@ -36,7 +36,7 @@ public class JoinProtection extends Appliance {
|
|||||||
|
|
||||||
Bukkit.getScheduler().runTaskTimer(
|
Bukkit.getScheduler().runTaskTimer(
|
||||||
Main.instance(),
|
Main.instance(),
|
||||||
this::notifyPlayers,
|
this::updateStatus,
|
||||||
Ticks.TICKS_PER_SECOND,
|
Ticks.TICKS_PER_SECOND,
|
||||||
Ticks.TICKS_PER_SECOND
|
Ticks.TICKS_PER_SECOND
|
||||||
);
|
);
|
||||||
@ -57,7 +57,7 @@ public class JoinProtection extends Appliance {
|
|||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void notifyPlayers() {
|
public void updateStatus() {
|
||||||
IteratorUtil.onlinePlayers(player -> {
|
IteratorUtil.onlinePlayers(player -> {
|
||||||
Options options = this.protectedPlayers.get(player.getUniqueId());
|
Options options = this.protectedPlayers.get(player.getUniqueId());
|
||||||
if(options == null) return;
|
if(options == null) return;
|
||||||
|
@ -5,6 +5,7 @@ import eu.mhsl.craftattack.spawn.core.appliance.Appliance;
|
|||||||
import eu.mhsl.craftattack.spawn.core.util.text.DisconnectInfo;
|
import eu.mhsl.craftattack.spawn.core.util.text.DisconnectInfo;
|
||||||
import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.displayName.DisplayName;
|
import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.displayName.DisplayName;
|
||||||
import eu.mhsl.craftattack.spawn.varo.api.repositories.TeamRepository;
|
import eu.mhsl.craftattack.spawn.varo.api.repositories.TeamRepository;
|
||||||
|
import eu.mhsl.craftattack.spawn.varo.appliances.internal.teamTasks.TeamTasks;
|
||||||
import eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.joinProtection.JoinProtection;
|
import eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.joinProtection.JoinProtection;
|
||||||
import eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.playTimer.PlayTimer;
|
import eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.playTimer.PlayTimer;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
@ -15,6 +16,7 @@ import org.bukkit.Bukkit;
|
|||||||
import org.bukkit.OfflinePlayer;
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.scheduler.BukkitTask;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@ -139,11 +141,14 @@ public class Teams extends Appliance implements DisplayName.Prefixed, DisplayNam
|
|||||||
.findAny()
|
.findAny()
|
||||||
.ifPresentOrElse(
|
.ifPresentOrElse(
|
||||||
member -> team.kickTeam(teamNotCompleteInfo),
|
member -> team.kickTeam(teamNotCompleteInfo),
|
||||||
() -> Bukkit.getScheduler().runTaskLater(
|
() -> {
|
||||||
|
BukkitTask kickTask = Bukkit.getScheduler().runTaskLater(
|
||||||
Main.instance(),
|
Main.instance(),
|
||||||
team::timeOverKick,
|
team::timeOverKick,
|
||||||
Ticks.TICKS_PER_SECOND * 60 * PlayTimer.PLAYTIME_MINUTES
|
Ticks.TICKS_PER_SECOND * 60 * PlayTimer.PLAYTIME_MINUTES
|
||||||
)
|
);
|
||||||
|
this.queryAppliance(TeamTasks.class).addTask(team, TeamTasks.Type.TIME_KICK, kickTask);
|
||||||
|
}
|
||||||
),
|
),
|
||||||
Ticks.TICKS_PER_SECOND * (JoinProtection.resistanceDuration / 2)
|
Ticks.TICKS_PER_SECOND * (JoinProtection.resistanceDuration / 2)
|
||||||
);
|
);
|
||||||
|
@ -2,10 +2,12 @@ package eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.teams;
|
|||||||
|
|
||||||
import eu.mhsl.craftattack.spawn.core.Main;
|
import eu.mhsl.craftattack.spawn.core.Main;
|
||||||
import eu.mhsl.craftattack.spawn.core.util.text.DisconnectInfo;
|
import eu.mhsl.craftattack.spawn.core.util.text.DisconnectInfo;
|
||||||
|
import eu.mhsl.craftattack.spawn.varo.appliances.internal.teamTasks.TeamTasks;
|
||||||
import eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.fightDetector.FightDetector;
|
import eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.fightDetector.FightDetector;
|
||||||
import net.kyori.adventure.util.Ticks;
|
import net.kyori.adventure.util.Ticks;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.OfflinePlayer;
|
import org.bukkit.OfflinePlayer;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -59,17 +61,25 @@ public class VaroTeam {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void kickTeam(DisconnectInfo disconnectInfo) {
|
public List<Player> getOnlinePlayers() {
|
||||||
this.members.stream()
|
return this.members.stream()
|
||||||
.map(member -> Bukkit.getPlayer(member.player.getUniqueId()))
|
.map(member -> Bukkit.getPlayer(member.player.getUniqueId()))
|
||||||
.filter(Objects::nonNull)
|
.filter(Objects::nonNull)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void kickTeam(DisconnectInfo disconnectInfo) {
|
||||||
|
this.getOnlinePlayers()
|
||||||
.forEach(player -> player.kick(disconnectInfo.getComponent()));
|
.forEach(player -> player.kick(disconnectInfo.getComponent()));
|
||||||
|
Main.instance().getAppliance(TeamTasks.class).cancelTeamTasks(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void timeOverKick() {
|
public void timeOverKick() {
|
||||||
boolean isInFight = Main.instance().getAppliance(FightDetector.class).isInFight(this);
|
boolean isInFight = Main.instance().getAppliance(FightDetector.class).isInFight(this);
|
||||||
if(isInFight) {
|
if(isInFight) {
|
||||||
Main.logger().info(String.format("Cannot kick Team %s because it is in a fight!", this.name));
|
Main.logger().info(String.format("Cannot kick Team %s because it is in a fight!", this.name));
|
||||||
|
this.getOnlinePlayers()
|
||||||
|
.forEach(player -> player.sendMessage("Du befindest dich in einer Kampfhandlung oder unmittelbar"));
|
||||||
Bukkit.getScheduler().runTaskLater(
|
Bukkit.getScheduler().runTaskLater(
|
||||||
Main.instance(),
|
Main.instance(),
|
||||||
this::timeOverKick,
|
this::timeOverKick,
|
||||||
|
Reference in New Issue
Block a user