mirror of
https://github.com/SunNetservers/MiniGames.git
synced 2025-09-17 11:27:55 +02:00
Makes levers clicked in parkour arenas turn off
When a lever is clicked while inside a parkour arena, that lever is saved. Once the last player leaves, or the last player restarts, the saved levers are turned off.
This commit is contained in:
@@ -19,7 +19,8 @@ import java.util.logging.Level;
|
||||
*/
|
||||
public abstract class AbstractArenaPlayerRegistry<K extends Arena> implements ArenaPlayerRegistry<K> {
|
||||
|
||||
private final Map<UUID, ArenaSession> arenaPlayers = new HashMap<>();
|
||||
private final Map<Arena, Set<UUID>> arenaPlayers = new HashMap<>();
|
||||
private final Map<UUID, ArenaSession> arenaSessions = new HashMap<>();
|
||||
private final Map<UUID, PlayerEntryState> entryStates = new HashMap<>();
|
||||
|
||||
/**
|
||||
@@ -30,19 +31,25 @@ public abstract class AbstractArenaPlayerRegistry<K extends Arena> implements Ar
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Set<UUID> getPlayingPlayers() {
|
||||
return arenaPlayers.keySet();
|
||||
@NotNull
|
||||
public Set<UUID> getPlayingPlayers() {
|
||||
return arenaSessions.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable PlayerEntryState getEntryState(@NotNull UUID playerId) {
|
||||
@Nullable
|
||||
public PlayerEntryState getEntryState(@NotNull UUID playerId) {
|
||||
return this.entryStates.get(playerId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerPlayer(@NotNull UUID playerId, @NotNull ArenaSession arenaSession) {
|
||||
this.arenaPlayers.put(playerId, arenaSession);
|
||||
this.arenaSessions.put(playerId, arenaSession);
|
||||
this.entryStates.put(playerId, arenaSession.getEntryState());
|
||||
|
||||
this.arenaPlayers.putIfAbsent(arenaSession.getArena(), new HashSet<>());
|
||||
this.arenaPlayers.get(arenaSession.getArena()).add(playerId);
|
||||
|
||||
this.saveEntryStates();
|
||||
}
|
||||
|
||||
@@ -60,25 +67,38 @@ public abstract class AbstractArenaPlayerRegistry<K extends Arena> implements Ar
|
||||
this.saveEntryStates();
|
||||
}
|
||||
|
||||
return this.arenaPlayers.remove(playerId) != null;
|
||||
if (this.arenaSessions.containsKey(playerId)) {
|
||||
this.arenaPlayers.get(this.arenaSessions.get(playerId).getArena()).remove(playerId);
|
||||
}
|
||||
return this.arenaSessions.remove(playerId) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable ArenaSession getArenaSession(@NotNull UUID playerId) {
|
||||
return this.arenaPlayers.getOrDefault(playerId, null);
|
||||
return this.arenaSessions.getOrDefault(playerId, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeForArena(K arena, boolean immediately) {
|
||||
public void removeForArena(@NotNull K arena, boolean immediately) {
|
||||
Set<UUID> removed = new HashSet<>();
|
||||
for (Map.Entry<UUID, ArenaSession> entry : this.arenaPlayers.entrySet()) {
|
||||
for (Map.Entry<UUID, ArenaSession> entry : this.arenaSessions.entrySet()) {
|
||||
if (entry.getValue().getArena() == arena) {
|
||||
// Kick the player gracefully
|
||||
entry.getValue().triggerQuit(immediately, false);
|
||||
removed.add(entry.getKey());
|
||||
}
|
||||
}
|
||||
removed.forEach(this.arenaPlayers::remove);
|
||||
removed.forEach(this.arenaSessions::remove);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Set<UUID> getPlayingPlayers(@NotNull K arena) {
|
||||
if (arenaPlayers.containsKey(arena)) {
|
||||
return arenaPlayers.get(arena);
|
||||
} else {
|
||||
return new HashSet<>();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -20,28 +20,32 @@ public interface Arena {
|
||||
*
|
||||
* @return <p>The name of this arena</p>
|
||||
*/
|
||||
@NotNull String getArenaName();
|
||||
@NotNull
|
||||
String getArenaName();
|
||||
|
||||
/**
|
||||
* Gets the data stored for this arena
|
||||
*
|
||||
* @return <p>The stored data</p>
|
||||
*/
|
||||
@NotNull ArenaData getData();
|
||||
@NotNull
|
||||
ArenaData getData();
|
||||
|
||||
/**
|
||||
* Gets the id of this arena
|
||||
*
|
||||
* @return <p>This arena's identifier</p>
|
||||
*/
|
||||
@NotNull UUID getArenaId();
|
||||
@NotNull
|
||||
UUID getArenaId();
|
||||
|
||||
/**
|
||||
* Gets this arena's sanitized name
|
||||
*
|
||||
* @return <p>This arena's sanitized name</p>
|
||||
*/
|
||||
@NotNull String getArenaNameSanitized();
|
||||
@NotNull
|
||||
String getArenaNameSanitized();
|
||||
|
||||
/**
|
||||
* Removes the data file belonging to this arena
|
||||
@@ -63,7 +67,7 @@ public interface Arena {
|
||||
* @param block <p>The block to check</p>
|
||||
* @return <p>True if standing on the block will cause a win</p>
|
||||
*/
|
||||
boolean willCauseWin(Block block);
|
||||
boolean willCauseWin(@NotNull Block block);
|
||||
|
||||
/**
|
||||
* Gets whether standing on the given block should cause a loss
|
||||
@@ -71,7 +75,7 @@ public interface Arena {
|
||||
* @param block <p>The block to check</p>
|
||||
* @return <p>True if standing on the block will cause a loss</p>
|
||||
*/
|
||||
boolean willCauseLoss(Block block);
|
||||
boolean willCauseLoss(@NotNull Block block);
|
||||
|
||||
/**
|
||||
* Gets whether the win location is a solid block
|
||||
@@ -85,14 +89,16 @@ public interface Arena {
|
||||
*
|
||||
* @return <p>This arena's spawn location</p>
|
||||
*/
|
||||
@NotNull Location getSpawnLocation();
|
||||
@NotNull
|
||||
Location getSpawnLocation();
|
||||
|
||||
/**
|
||||
* Gets this arena's exit location
|
||||
*
|
||||
* @return <p>This arena's exit location, or null if no such location is set.</p>
|
||||
*/
|
||||
@Nullable Location getExitLocation();
|
||||
@Nullable
|
||||
Location getExitLocation();
|
||||
|
||||
/**
|
||||
* Adds a reward to this arena
|
||||
@@ -115,6 +121,7 @@ public interface Arena {
|
||||
* @param rewardCondition <p>The condition to get the rewards for</p>
|
||||
* @return <p>All rewards</p>
|
||||
*/
|
||||
@NotNull Set<Reward> getRewards(RewardCondition rewardCondition);
|
||||
@NotNull
|
||||
Set<Reward> getRewards(RewardCondition rewardCondition);
|
||||
|
||||
}
|
||||
|
@@ -18,7 +18,17 @@ public interface ArenaPlayerRegistry<K extends Arena> {
|
||||
*
|
||||
* @return <p>The ids of the playing players</p>
|
||||
*/
|
||||
@NotNull Set<UUID> getPlayingPlayers();
|
||||
@NotNull
|
||||
Set<UUID> getPlayingPlayers();
|
||||
|
||||
/**
|
||||
* Gets all players currently playing in the given arena
|
||||
*
|
||||
* @param arena <p>The arena to check</p>
|
||||
* @return <p>All players currently in the arena</p>
|
||||
*/
|
||||
@NotNull
|
||||
Set<UUID> getPlayingPlayers(@NotNull K arena);
|
||||
|
||||
/**
|
||||
* Gets the current entry state for the given player
|
||||
@@ -26,7 +36,8 @@ public interface ArenaPlayerRegistry<K extends Arena> {
|
||||
* @param playerId <p>The id of the player to get an entry state for</p>
|
||||
* @return <p>The entry state of the player, or null if not found</p>
|
||||
*/
|
||||
@Nullable PlayerEntryState getEntryState(@NotNull UUID playerId);
|
||||
@Nullable
|
||||
PlayerEntryState getEntryState(@NotNull UUID playerId);
|
||||
|
||||
/**
|
||||
* Registers that the given player has started playing the given dropper arena session
|
||||
@@ -50,7 +61,8 @@ public interface ArenaPlayerRegistry<K extends Arena> {
|
||||
* @param playerId <p>The id of the player to get arena for</p>
|
||||
* @return <p>The player's active arena session, or null if not currently playing</p>
|
||||
*/
|
||||
@Nullable ArenaSession getArenaSession(@NotNull UUID playerId);
|
||||
@Nullable
|
||||
ArenaSession getArenaSession(@NotNull UUID playerId);
|
||||
|
||||
/**
|
||||
* Removes all active sessions for the given arena
|
||||
@@ -58,6 +70,6 @@ public interface ArenaPlayerRegistry<K extends Arena> {
|
||||
* @param arena <p>The arena to remove sessions for</p>
|
||||
* @param immediately <p>Whether to immediately teleport the player</p>
|
||||
*/
|
||||
void removeForArena(K arena, boolean immediately);
|
||||
void removeForArena(@NotNull K arena, boolean immediately);
|
||||
|
||||
}
|
||||
|
@@ -22,7 +22,7 @@ public class PlayerVisibilityManager {
|
||||
*
|
||||
* @param player <p>The the player to update</p>
|
||||
*/
|
||||
public void toggleHidePlayers(@NotNull ArenaPlayerRegistry<?> playerRegistry, @NotNull Player player) {
|
||||
public void toggleHidePlayers(@Nullable ArenaPlayerRegistry<?> playerRegistry, @NotNull Player player) {
|
||||
if (displayingEnabledFor.contains(player.getUniqueId())) {
|
||||
displayingEnabledFor.remove(player.getUniqueId());
|
||||
// Make all other players hidden
|
||||
|
@@ -243,12 +243,12 @@ public class DropperArena implements Arena {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean willCauseWin(Block block) {
|
||||
public boolean willCauseWin(@NotNull Block block) {
|
||||
return block.getType() == winBlockType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean willCauseLoss(Block block) {
|
||||
public boolean willCauseLoss(@NotNull Block block) {
|
||||
return !dropperConfiguration.getBlockWhitelist().contains(block.getType());
|
||||
}
|
||||
|
||||
|
@@ -340,7 +340,7 @@ public class ParkourArena implements Arena {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean willCauseLoss(Block block) {
|
||||
public boolean willCauseLoss(@NotNull Block block) {
|
||||
return this.getKillPlaneBlocks().contains(block.getType()) || this.getObstacleBlocks().contains(block.getType());
|
||||
}
|
||||
|
||||
|
@@ -11,5 +11,5 @@ public class ParkourArenaPlayerRegistry extends AbstractArenaPlayerRegistry<Park
|
||||
protected String getEntryStateStorageKey() {
|
||||
return "parkour";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -3,6 +3,7 @@ package net.knarcraft.minigames.arena.parkour;
|
||||
import net.knarcraft.knarlib.formatting.StringFormatter;
|
||||
import net.knarcraft.minigames.MiniGames;
|
||||
import net.knarcraft.minigames.arena.AbstractArenaSession;
|
||||
import net.knarcraft.minigames.arena.Arena;
|
||||
import net.knarcraft.minigames.arena.PlayerEntryState;
|
||||
import net.knarcraft.minigames.arena.reward.RewardCondition;
|
||||
import net.knarcraft.minigames.config.MiniGameMessage;
|
||||
@@ -13,10 +14,17 @@ import net.knarcraft.minigames.util.GeyserHelper;
|
||||
import net.knarcraft.minigames.util.PlayerTeleporter;
|
||||
import net.knarcraft.minigames.util.RewardHelper;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.block.data.Powerable;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
@@ -24,10 +32,11 @@ import java.util.logging.Level;
|
||||
*/
|
||||
public class ParkourArenaSession extends AbstractArenaSession {
|
||||
|
||||
private static final @NotNull Map<Arena, Set<Block>> changedLevers = new HashMap<>();
|
||||
private final @NotNull ParkourArena arena;
|
||||
private final @NotNull Player player;
|
||||
private final @NotNull ParkourArenaGameMode gameMode;
|
||||
private Location reachedCheckpoint = null;
|
||||
private @Nullable Location reachedCheckpoint = null;
|
||||
|
||||
/**
|
||||
* Instantiates a new parkour arena session
|
||||
@@ -65,6 +74,16 @@ public class ParkourArenaSession extends AbstractArenaSession {
|
||||
this.reachedCheckpoint = location;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a lever change
|
||||
*
|
||||
* @param block <p>The block of the lever</p>
|
||||
*/
|
||||
public void registerChangedLever(@NotNull Block block) {
|
||||
changedLevers.putIfAbsent(this.arena, new HashSet<>());
|
||||
changedLevers.get(this.arena).add(block);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the checkpoint currently registered as the player's spawn location
|
||||
*
|
||||
@@ -132,6 +151,9 @@ public class ParkourArenaSession extends AbstractArenaSession {
|
||||
@Override
|
||||
public void reset() {
|
||||
this.reachedCheckpoint = null;
|
||||
if (MiniGames.getInstance().getParkourArenaPlayerRegistry().getPlayingPlayers(this.arena).size() == 1) {
|
||||
resetLevers();
|
||||
}
|
||||
super.reset();
|
||||
}
|
||||
|
||||
@@ -154,4 +176,33 @@ public class ParkourArenaSession extends AbstractArenaSession {
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void triggerQuit(boolean immediately, boolean removeSession) {
|
||||
super.triggerQuit(immediately, removeSession);
|
||||
|
||||
if (MiniGames.getInstance().getParkourArenaPlayerRegistry().getPlayingPlayers(this.arena).isEmpty()) {
|
||||
resetLevers();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets all levers if the arena is empty
|
||||
*/
|
||||
private void resetLevers() {
|
||||
// Make a copy in case new player join while the levers are resetting
|
||||
Set<Block> changed = changedLevers.remove(this.arena);
|
||||
if (changed == null || changed.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Reset all levers toggled by players in the arena
|
||||
for (Block block : changed) {
|
||||
BlockData blockData = block.getBlockData();
|
||||
if (blockData instanceof Powerable powerable) {
|
||||
powerable.setPowered(false);
|
||||
block.setBlockData(blockData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user