Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix proximity alarms #1121

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.Nullable;
import tc.oc.pgm.api.filter.Filter;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.player.MatchPlayer;
import tc.oc.pgm.api.region.Region;
Expand All @@ -20,11 +22,19 @@ public class RegionPlayerTracker implements Listener {
private final Match match;
private final Set<MatchPlayer> players = Sets.newHashSet();

// The region to check against
private Region region;
// A static filter players must match when entering
private @Nullable Filter staticFilter;

public RegionPlayerTracker(Match match, Region region) {
this(match, region, null);
}

public RegionPlayerTracker(Match match, Region region, @Nullable Filter staticFilter) {
this.match = match;
this.region = region;
this.staticFilter = staticFilter;
}

public Set<MatchPlayer> getPlayers() {
Expand Down Expand Up @@ -52,7 +62,9 @@ public void handlePlayerMove(Player bukkit, Vector to) {
MatchPlayer player = this.match.getPlayer(bukkit);
if (!MatchPlayers.canInteract(player)) return;

if (!player.getBukkit().isDead() && this.region.contains(to.toBlockVector())) {
if (!player.getBukkit().isDead()
&& this.region.contains(to.toBlockVector())
&& (this.staticFilter == null || this.staticFilter.query(player).isAllowed())) {
this.players.add(player);
} else {
this.players.remove(player);
Expand Down
83 changes: 33 additions & 50 deletions core/src/main/java/tc/oc/pgm/proximity/ProximityAlarm.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@
import static net.kyori.adventure.sound.Sound.sound;
import static net.kyori.adventure.text.Component.text;

import com.google.common.collect.Sets;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.util.Vector;
import org.bukkit.entity.Firework;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.player.MatchPlayer;
import tc.oc.pgm.spawns.events.ParticipantDespawnEvent;
import tc.oc.pgm.spawns.events.ParticipantSpawnEvent;
import tc.oc.pgm.util.event.PlayerCoarseMoveEvent;
import tc.oc.pgm.controlpoint.RegionPlayerTracker;
import tc.oc.pgm.fireworks.FireworkMatchModule;
import tc.oc.pgm.util.nms.NMSHacks;

public class ProximityAlarm implements Listener {
public class ProximityAlarm {
private static final long MESSAGE_INTERVAL = 5000;
private static final float FLARE_CHANCE = 0.25f;

Expand All @@ -32,66 +29,52 @@ public class ProximityAlarm implements Listener {
protected final Random random;
protected final Match match;
protected final ProximityAlarmDefinition definition;
protected final Set<MatchPlayer> playersInside = Sets.newHashSet();
protected long lastMessageTime = 0;

protected final RegionPlayerTracker playerTracker;

public ProximityAlarm(Match match, ProximityAlarmDefinition definition, Random random) {
this.random = random;
this.match = match;
this.definition = definition;
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerMove(final PlayerCoarseMoveEvent event) {
updatePlayer(this.match.getPlayer(event.getPlayer()), event.getTo());
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerSpawn(final ParticipantSpawnEvent event) {
updatePlayer(event.getPlayer(), event.getPlayer().getBukkit().getLocation());
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerDespawn(final ParticipantDespawnEvent event) {
this.playersInside.remove(event.getPlayer());
}

private void updatePlayer(MatchPlayer player, Location location) {
if (player != null
&& player.canInteract()
&& this.definition.detectFilter.query(player).isAllowed()) {
if (!player.isDead() && this.definition.detectRegion.contains(location.toVector())) {
this.playersInside.add(player);
} else {
this.playersInside.remove(player);
}
}
this.playerTracker =
new RegionPlayerTracker(match, definition.detectRegion, definition.detectFilter);
}

public void showAlarm() {
if (this.random.nextFloat() < FLARE_CHANCE) {
if (!this.playersInside.isEmpty()) {
this.showFlare();
this.showMessage();
}
if (this.random.nextFloat() < FLARE_CHANCE && !this.playerTracker.getPlayers().isEmpty()) {
this.showFlare();
this.showMessage();
}
}

private void showFlare() {
Vector pos = this.definition.detectRegion.getBounds().getCenterPoint();
float angle = (float) (this.random.nextFloat() * Math.PI * 2);
if (!definition.flares) return;

pos.add(
new Vector(
Math.sin(angle) * this.definition.flareRadius,
0,
Math.cos(angle) * this.definition.flareRadius));
float angle = (float) (this.random.nextFloat() * Math.PI * 2);
Location location =
this.definition
.detectRegion
.getBounds()
.getCenterPoint()
.toLocation(match.getWorld())
.add(
Math.sin(angle) * this.definition.flareRadius,
0,
Math.cos(angle) * this.definition.flareRadius);

Set<Color> colors = new HashSet<>();

for (MatchPlayer player : this.playersInside) {
for (MatchPlayer player : this.playerTracker.getPlayers()) {
colors.add(player.getParty().getFullColor());
}

Firework firework =
FireworkMatchModule.spawnFirework(
location,
FireworkEffect.builder().with(FireworkEffect.Type.BALL).withColor(colors).build(),
0);
NMSHacks.skipFireworksLaunch(firework);
}

private void showMessage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ProximityAlarmMatchModule(Match match, Set<ProximityAlarmDefinition> defi
@Override
public void enable() {
for (ProximityAlarm proximityAlarm : this.proximityAlarms) {
this.match.addListener(proximityAlarm, MatchScope.RUNNING);
this.match.addListener(proximityAlarm.playerTracker, MatchScope.RUNNING);
}

match
Expand Down