-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement hidden observers for series
- Loading branch information
Showing
8 changed files
with
214 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package rip.bolt.ingame.api.definitions; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class Series { | ||
|
||
private String name; | ||
private Boolean hideObservers = false; | ||
|
||
public Series() {} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public boolean getHideObservers() { | ||
return hideObservers; | ||
} | ||
|
||
public void setHideObservers(boolean hideObservers) { | ||
this.hideObservers = hideObservers; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Series{" + "name='" + name + '\'' + ", hideObservers=" + hideObservers + '}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package rip.bolt.ingame.events; | ||
|
||
import org.bukkit.event.Event; | ||
import rip.bolt.ingame.api.definitions.BoltMatch; | ||
|
||
abstract class BoltMatchEvent extends Event { | ||
|
||
private final BoltMatch boltMatch; | ||
|
||
public BoltMatchEvent(BoltMatch boltMatch) { | ||
this.boltMatch = boltMatch; | ||
} | ||
|
||
public BoltMatch getBoltMatch() { | ||
return boltMatch; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/rip/bolt/ingame/events/BoltMatchStatusChangeEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package rip.bolt.ingame.events; | ||
|
||
import javax.annotation.Nullable; | ||
import org.bukkit.event.HandlerList; | ||
import rip.bolt.ingame.api.definitions.BoltMatch; | ||
import rip.bolt.ingame.ranked.MatchStatus; | ||
|
||
public class BoltMatchStatusChangeEvent extends BoltMatchEvent { | ||
|
||
private static final HandlerList handlers = new HandlerList(); | ||
|
||
@Nullable private final MatchStatus oldStatus; | ||
private final MatchStatus newStatus; | ||
|
||
public BoltMatchStatusChangeEvent( | ||
BoltMatch boltMatch, @Nullable MatchStatus oldStatus, MatchStatus newStatus) { | ||
super(boltMatch); | ||
this.oldStatus = oldStatus; | ||
this.newStatus = newStatus; | ||
} | ||
|
||
public @Nullable MatchStatus getOldStatus() { | ||
return oldStatus; | ||
} | ||
|
||
public MatchStatus getNewStatus() { | ||
return newStatus; | ||
} | ||
|
||
@Override | ||
public HandlerList getHandlers() { | ||
return handlers; | ||
} | ||
|
||
public static HandlerList getHandlerList() { | ||
return handlers; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/java/rip/bolt/ingame/ranked/SpectatorManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package rip.bolt.ingame.ranked; | ||
|
||
import java.util.HashMap; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.EventPriority; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.player.PlayerQuitEvent; | ||
import org.bukkit.permissions.PermissionAttachment; | ||
import rip.bolt.ingame.Ingame; | ||
import rip.bolt.ingame.events.BoltMatchStatusChangeEvent; | ||
import tc.oc.pgm.api.PGM; | ||
import tc.oc.pgm.api.player.MatchPlayer; | ||
import tc.oc.pgm.api.player.VanishManager; | ||
import tc.oc.pgm.events.PlayerJoinMatchEvent; | ||
|
||
public class SpectatorManager implements Listener { | ||
|
||
private static final String SPECTATE_VANISH = "events.spectate.vanish"; | ||
private static final String PGM_VANISH = "pgm.vanish"; | ||
|
||
private final VanishManager vanishManager; | ||
private final PlayerWatcher watcher; | ||
|
||
private final HashMap<UUID, PermissionAttachment> permissions = new HashMap<>(); | ||
|
||
public SpectatorManager(PlayerWatcher playerWatcher) { | ||
this.watcher = playerWatcher; | ||
vanishManager = PGM.get().getVanishManager(); | ||
} | ||
|
||
@EventHandler(priority = EventPriority.MONITOR) | ||
public void onBoltMatchStateChange(BoltMatchStatusChangeEvent event) { | ||
if (Objects.equals(event.getNewStatus(), MatchStatus.CREATED)) { | ||
Bukkit.getOnlinePlayers().forEach(this::updatePlayer); | ||
} | ||
} | ||
|
||
@EventHandler(priority = EventPriority.NORMAL) | ||
public void onPlayerJoinEvent(PlayerJoinMatchEvent event) { | ||
updatePlayer(event.getPlayer().getBukkit()); | ||
} | ||
|
||
@EventHandler(priority = EventPriority.MONITOR) | ||
public void onPlayerQuit(PlayerQuitEvent event) { | ||
PermissionAttachment attachment = permissions.remove(event.getPlayer().getUniqueId()); | ||
if (attachment != null) attachment.remove(); | ||
} | ||
|
||
private void updatePlayer(Player player) { | ||
if (Ingame.get().getRankedManager().getMatch() == null) return; | ||
|
||
boolean hidden = Ingame.get().getRankedManager().getMatch().getSeries().getHideObservers(); | ||
boolean playing = watcher.isPlaying(player.getUniqueId()); | ||
|
||
// Allow people who can vanish to remain in current state | ||
if (!playing && player.hasPermission(PGM_VANISH)) return; | ||
|
||
if (!hidden || playing) { | ||
unvanish(player); | ||
} else { | ||
vanish(player); | ||
} | ||
} | ||
|
||
private void vanish(Player player) { | ||
permissions.computeIfAbsent( | ||
player.getUniqueId(), uuid -> player.addAttachment(Ingame.get(), SPECTATE_VANISH, true)); | ||
|
||
// Set vanish if not already | ||
if (vanishManager.isVanished(player.getUniqueId())) return; | ||
|
||
MatchPlayer matchPlayer = PGM.get().getMatchManager().getPlayer(player); | ||
vanishManager.setVanished(matchPlayer, true, true); | ||
} | ||
|
||
private void unvanish(Player player) { | ||
PermissionAttachment attachment = permissions.remove(player.getUniqueId()); | ||
if (attachment != null) attachment.remove(); | ||
|
||
// Remove vanish if not already | ||
if (!vanishManager.isVanished(player.getUniqueId())) return; | ||
|
||
MatchPlayer matchPlayer = PGM.get().getMatchManager().getPlayer(player); | ||
vanishManager.setVanished(matchPlayer, false, true); | ||
} | ||
} |