Add player movement freezing during countdown and winner declaration logic
This commit is contained in:
parent
d7160b86cd
commit
e97a6ade73
|
|
@ -34,7 +34,7 @@ public final class Main extends JavaPlugin {
|
|||
|
||||
private void registerServices() {
|
||||
round = Round.createSingletonInstance();
|
||||
roundService = RoundService.createSingletonInstance(round);
|
||||
roundService = RoundService.createSingletonInstance(round, this);
|
||||
playerTeleporterService = PlayerTeleporterService.createSingletonInstance();
|
||||
playerService = PlayerService.createSingletonInstance(round, roundService, playerTeleporterService);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
package com.alttd.hunger_games.event_listeners;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerMoveEvent;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class PlayerMovementListener implements Listener {
|
||||
private final Set<UUID> uuidSet;
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerMove(PlayerMoveEvent event) {
|
||||
if (!uuidSet.contains(event.getPlayer().getUniqueId())) {
|
||||
return;
|
||||
}
|
||||
Location from = event.getFrom();
|
||||
Location to = event.getTo();
|
||||
//Only freeze movement not rotation or jumping
|
||||
if (from.getBlockX() != to.getBlockX()
|
||||
|| from.getBlockZ() != to.getBlockZ()) {
|
||||
event.setTo(from);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,13 @@
|
|||
package com.alttd.hunger_games.services;
|
||||
|
||||
import com.alttd.hunger_games.Main;
|
||||
import com.alttd.hunger_games.data_objects.PLAYER_STATE;
|
||||
import com.alttd.hunger_games.data_objects.ROUND_STATE;
|
||||
import com.alttd.hunger_games.event_listeners.PlayerMovementListener;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
|
@ -10,21 +15,24 @@ import java.util.stream.Collectors;
|
|||
public class RoundService implements RoundListener {
|
||||
|
||||
private static RoundService instance = null;
|
||||
private PlayerMovementListener playerMovementListener;
|
||||
private final Main main;
|
||||
|
||||
@Getter
|
||||
private ROUND_STATE roundState;
|
||||
private final HashMap<UUID, PLAYER_STATE> players = new HashMap<>();
|
||||
|
||||
public static RoundService createSingletonInstance(Round round) {
|
||||
public static RoundService createSingletonInstance(Round round, Main main) {
|
||||
if (instance != null) {
|
||||
throw new IllegalStateException("RoundService is already initialized.");
|
||||
}
|
||||
instance = new RoundService(round);
|
||||
instance = new RoundService(round, main);
|
||||
return instance;
|
||||
}
|
||||
|
||||
private RoundService(Round round) {
|
||||
private RoundService(Round round, Main main) {
|
||||
this.roundState = round.register(this);
|
||||
this.main = main;
|
||||
}
|
||||
|
||||
public Set<UUID> getPlayers(PLAYER_STATE playerState) {
|
||||
|
|
@ -36,6 +44,20 @@ public class RoundService implements RoundListener {
|
|||
|
||||
protected void setPlayerState(UUID uuid, PLAYER_STATE playerState) {
|
||||
players.put(uuid, playerState);
|
||||
if (!roundState.equals(ROUND_STATE.KILL_PHASE) && !roundState.equals(ROUND_STATE.FINALE)) {
|
||||
return;
|
||||
}
|
||||
Set<UUID> uuidSet = getPlayers(PLAYER_STATE.REGISTERED);
|
||||
if (uuidSet.size() != 1) {
|
||||
return;
|
||||
}
|
||||
declareWinner(uuidSet.iterator().next());
|
||||
}
|
||||
|
||||
private void declareWinner(UUID winnerUUID) {
|
||||
Player player = Bukkit.getPlayer(winnerUUID);
|
||||
//TODO message
|
||||
//TODO reset round
|
||||
}
|
||||
|
||||
protected void clear() {
|
||||
|
|
@ -49,8 +71,27 @@ public class RoundService implements RoundListener {
|
|||
@Override
|
||||
public void stateChange(ROUND_STATE roundState) {
|
||||
this.roundState = roundState;
|
||||
stopFreeze();
|
||||
if (roundState.equals(ROUND_STATE.PLAYER_REGISTRATION)) {
|
||||
clear();
|
||||
}
|
||||
if (roundState.equals(ROUND_STATE.COUNTDOWN)) {
|
||||
startFreeze();
|
||||
}
|
||||
}
|
||||
|
||||
private void stopFreeze() {
|
||||
if (playerMovementListener == null) {
|
||||
return;
|
||||
}
|
||||
HandlerList.unregisterAll(playerMovementListener);
|
||||
playerMovementListener = null;
|
||||
}
|
||||
|
||||
private void startFreeze() {
|
||||
stopFreeze();
|
||||
Set<UUID> uuidSet = getPlayers(PLAYER_STATE.REGISTERED);
|
||||
playerMovementListener = new PlayerMovementListener(uuidSet);
|
||||
main.getServer().getPluginManager().registerEvents(playerMovementListener, main);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user