Teams corrections
full implementation of FightDetector
This commit is contained in:
parent
b1f188dece
commit
69e971f618
@ -93,9 +93,8 @@ public abstract class HttpRepository extends Repository {
|
||||
private <TResponse> ReqResp<TResponse> execute(HttpRequest request, Class<TResponse> clazz) {
|
||||
ReqResp<String> rawResponse = this.sendHttp(request);
|
||||
Main.logger().info(String.format(
|
||||
"HTTP-Repository fired %s, sending: %s, response: %s",
|
||||
"Request: %s\nResponse: %s",
|
||||
request,
|
||||
request.bodyPublisher().orElse(HttpRequest.BodyPublishers.ofString("none")),
|
||||
rawResponse
|
||||
));
|
||||
return new ReqResp<>(rawResponse.status(), this.gson.fromJson(rawResponse.data(), clazz));
|
||||
|
@ -1,8 +1,11 @@
|
||||
package eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.fightDetector;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.core.Main;
|
||||
import eu.mhsl.craftattack.spawn.core.appliance.Appliance;
|
||||
import eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.teams.Teams;
|
||||
import eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.teams.VaroTeam;
|
||||
import net.kyori.adventure.util.Ticks;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -10,21 +13,81 @@ import org.jetbrains.annotations.NotNull;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class FightDetector extends Appliance {
|
||||
public static final Long FIGHT_TIMEOUT = 60 * 1000L;
|
||||
public static final long FIGHT_TIMEOUT = 60 * 1000;
|
||||
private static final long BLOCK_RADIUS = 30;
|
||||
|
||||
public final Map<VaroTeam, Long> fights = new HashMap<>();
|
||||
|
||||
public FightDetector() {
|
||||
Bukkit.getScheduler().runTaskTimer(
|
||||
Main.instance(),
|
||||
() -> {
|
||||
var teamFights = this.fights.keySet().stream()
|
||||
.filter(this::isInFight)
|
||||
.toList();
|
||||
if(teamFights.isEmpty()) return;
|
||||
Main.logger().info(String.format(
|
||||
"There are %d Teams in Fight: %s",
|
||||
teamFights.size(),
|
||||
teamFights.stream()
|
||||
.map(varoTeam -> String.format(
|
||||
"%s[%s]",
|
||||
varoTeam.name,
|
||||
varoTeam.members.stream()
|
||||
.map(member -> member.player.getName())
|
||||
.collect(Collectors.joining(","))))
|
||||
.collect(Collectors.joining(", "))
|
||||
));
|
||||
},
|
||||
Ticks.TICKS_PER_SECOND * 15,
|
||||
Ticks.TICKS_PER_SECOND * 15
|
||||
);
|
||||
|
||||
Bukkit.getScheduler().runTaskTimer(
|
||||
Main.instance(),
|
||||
this::detectNearbyFights,
|
||||
Ticks.TICKS_PER_SECOND,
|
||||
Ticks.TICKS_PER_SECOND
|
||||
);
|
||||
}
|
||||
|
||||
private void detectNearbyFights() {
|
||||
var players = Bukkit.getOnlinePlayers();
|
||||
Bukkit.getScheduler().runTaskAsynchronously(Main.instance(), () -> {
|
||||
for (Player player : players) {
|
||||
VaroTeam ownTeam = this.queryAppliance(Teams.class).getTeamFromPlayer(player.getUniqueId());
|
||||
if (ownTeam == null) continue;
|
||||
|
||||
for (Player otherPlayer : players) {
|
||||
if (player.equals(otherPlayer)) continue;
|
||||
|
||||
VaroTeam otherTeam = this.queryAppliance(Teams.class).getTeamFromPlayer(otherPlayer.getUniqueId());
|
||||
if (otherTeam == null || ownTeam.equals(otherTeam)) continue;
|
||||
|
||||
if (player.getLocation().distance(otherPlayer.getLocation()) <= BLOCK_RADIUS) {
|
||||
this.setInFight(ownTeam);
|
||||
this.setInFight(otherTeam);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public boolean isInFight(VaroTeam team) {
|
||||
Long lastFightTime = this.fights.get(team);
|
||||
if(lastFightTime == null) return false;
|
||||
return (System.currentTimeMillis() - lastFightTime <= FIGHT_TIMEOUT);
|
||||
}
|
||||
|
||||
public void setInFight(Player player) {
|
||||
VaroTeam team = this.queryAppliance(Teams.class).getTeamFromPlayer(player.getUniqueId());
|
||||
public void setInFight(VaroTeam team) {
|
||||
this.fights.put(team, System.currentTimeMillis());
|
||||
}
|
||||
public void setInFight(Player player) {
|
||||
this.setInFight(this.queryAppliance(Teams.class).getTeamFromPlayer(player.getUniqueId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull List<Listener> listeners() {
|
||||
|
@ -35,7 +35,6 @@ public class ShrinkingBorder extends Appliance {
|
||||
@Override
|
||||
public void run() {
|
||||
Bukkit.getScheduler().runTask(Main.instance(), ShrinkingBorder.this::shrinkBorder);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -19,7 +19,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.util.*;
|
||||
|
||||
public class Teams extends Appliance implements DisplayName.Prefixed, DisplayName.Colored {
|
||||
private List<VaroTeam> teams = List.of();
|
||||
private final List<VaroTeam> teams = new ArrayList<>();
|
||||
|
||||
public Teams() {
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(
|
||||
@ -31,12 +31,36 @@ public class Teams extends Appliance implements DisplayName.Prefixed, DisplayNam
|
||||
}
|
||||
|
||||
public void refreshTeamList() {
|
||||
this.teams = this.queryRepository(TeamRepository.class).getTeams().data().stream()
|
||||
.map(team -> new VaroTeam(
|
||||
team.users().stream().map(user -> new VaroTeam.Member(user.uuid(), user.dead())).toList(),
|
||||
team.name(),
|
||||
team.color()
|
||||
)).toList();
|
||||
var updatedTeams = this.queryRepository(TeamRepository.class).getTeams().data();
|
||||
|
||||
for (var updatedTeam : updatedTeams) {
|
||||
VaroTeam existingTeam = this.findTeamByName(updatedTeam.name());
|
||||
|
||||
if (existingTeam != null) {
|
||||
existingTeam.members = updatedTeam.users().stream()
|
||||
.map(user -> new VaroTeam.Member(user.uuid(), user.dead()))
|
||||
.toList();
|
||||
existingTeam.color = updatedTeam.color();
|
||||
existingTeam.name = updatedTeam.name();
|
||||
} else {
|
||||
VaroTeam newTeam = new VaroTeam(
|
||||
updatedTeam.users().stream()
|
||||
.map(user -> new VaroTeam.Member(user.uuid(), user.dead()))
|
||||
.toList(),
|
||||
updatedTeam.name(),
|
||||
updatedTeam.color()
|
||||
);
|
||||
this.teams.add(newTeam);
|
||||
Main.logger().info("Added missing team to Teams registry: " + newTeam);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private @Nullable VaroTeam findTeamByName(String name) {
|
||||
for (VaroTeam team : this.teams) {
|
||||
if (team.name.equals(name)) return team;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean canLogin(UUID playerId) {
|
||||
|
@ -5,6 +5,7 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
@ -20,14 +21,12 @@ public class VaroTeam {
|
||||
}
|
||||
}
|
||||
|
||||
public final List<Member> members;
|
||||
public final UUID teamUuid;
|
||||
public final String name;
|
||||
public final String color;
|
||||
public String name;
|
||||
public String color;
|
||||
public List<Member> members;
|
||||
|
||||
public VaroTeam(List<Member> members, String name, String color) {
|
||||
this.teamUuid = UUID.randomUUID();
|
||||
this.members = members;
|
||||
this.members = new ArrayList<>(members);
|
||||
this.name = name;
|
||||
this.color = color;
|
||||
}
|
||||
@ -53,7 +52,7 @@ public class VaroTeam {
|
||||
"Unbekannter Fehler",
|
||||
"Verbindung wurde aufgrund eines unbekannten Grundes getrennt",
|
||||
"Falls du denkst, dass dies ein Fehler ist, melde dich bei einem Admin!",
|
||||
this.teamUuid
|
||||
UUID.randomUUID()
|
||||
));
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user