|
| 1 | +package com.vltno.timeloop; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.function.Consumer; |
| 8 | + |
| 9 | +public class LoopSceneManager { |
| 10 | + private TimeLoopConfig config; |
| 11 | + private String scenePrefix; |
| 12 | + private Map<String, PlayerData> recordingPlayers; |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + // Constructor to initialize recordingPlayers map |
| 17 | + public LoopSceneManager(TimeLoopConfig config) { |
| 18 | + this.config = config; |
| 19 | + this.scenePrefix = config.scenePrefix; |
| 20 | + this.recordingPlayers = new HashMap<>(); |
| 21 | + } |
| 22 | + |
| 23 | + // Method to add a player to the recordingPlayers map |
| 24 | + public void addPlayer(List<String> args) { |
| 25 | + String playerName = args.get(0); |
| 26 | + List<String> nickname = args.size() > 1 ? args.subList(1, args.size()) : null; |
| 27 | + List<String> skin = args.size() > 2 ? args.subList(2, args.size()) : null; |
| 28 | + |
| 29 | + if (playerName != null && !playerName.isEmpty()) { |
| 30 | + String tempNickname = (nickname == null || nickname.isEmpty()) ? playerName : nickname.getFirst(); |
| 31 | + String tempSkin = (skin == null || skin.isEmpty()) ? playerName : skin.getFirst(); |
| 32 | + |
| 33 | + // Use player name as the key and store a PlayerData object |
| 34 | + recordingPlayers.put(playerName, new PlayerData(playerName, tempNickname, tempSkin)); |
| 35 | + } else { |
| 36 | + System.out.println("Invalid player data. Player not added."); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + // Method to remove a specific player from the recordingPlayers map |
| 42 | + public void removePlayer(String playerName) { |
| 43 | + if (playerName != null && !playerName.isEmpty()) { |
| 44 | + PlayerData removed = recordingPlayers.remove(playerName); |
| 45 | + if (removed != null) { |
| 46 | + System.out.println("Player '" + playerName + "' removed successfully."); |
| 47 | + } else { |
| 48 | + System.out.println("Player '" + playerName + "' not found in the list."); |
| 49 | + } |
| 50 | + } else { |
| 51 | + System.out.println("Invalid player name. Player not removed."); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + // Method to generate playerSceneName for a given player |
| 57 | + public String getPlayerSceneName(String playerName) { |
| 58 | + return (playerName.startsWith(scenePrefix)) ? playerName : (scenePrefix + "_" + playerName).toLowerCase(); |
| 59 | + } |
| 60 | + |
| 61 | + // Method to get all playerSceneNames for recordingPlayers |
| 62 | + public List<String> getAllPlayerSceneNames() { |
| 63 | + List<String> playerSceneNames = new ArrayList<>(); |
| 64 | + for (PlayerData player : recordingPlayers.values()) { |
| 65 | + playerSceneNames.add(getPlayerSceneName(player.getName())); |
| 66 | + } |
| 67 | + return playerSceneNames; |
| 68 | + } |
| 69 | + |
| 70 | + public void forEachPlayerSceneName(Consumer<String> action) { |
| 71 | + getAllPlayerSceneNames().forEach(action); |
| 72 | + } |
| 73 | + |
| 74 | + // Method to perform an action for each recording player |
| 75 | + public void forEachRecordingPlayer(Consumer<PlayerData> action) { |
| 76 | + recordingPlayers.values().forEach(action); |
| 77 | + } |
| 78 | + |
| 79 | + // Method to set recording players with a new map |
| 80 | + public void setRecordingPlayers(Map<String, PlayerData> recordingPlayers) { |
| 81 | + this.recordingPlayers = recordingPlayers; |
| 82 | + } |
| 83 | + |
| 84 | + public void saveRecordingPlayers() { |
| 85 | + config.recordingPlayers = new HashMap<>(recordingPlayers); |
| 86 | + } |
| 87 | +} |
0 commit comments