setGameMode(GameMode)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
package de.pewpewproject.lasertag.lasertaggame.state.management.server.synced.implementation;
2
3
import de.pewpewproject.lasertag.LasertagMod;
4
import de.pewpewproject.lasertag.lasertaggame.gamemode.GameMode;
5
import de.pewpewproject.lasertag.lasertaggame.gamemode.GameModes;
6
import de.pewpewproject.lasertag.lasertaggame.state.management.server.synced.IGameModeManager;
7
import de.pewpewproject.lasertag.lasertaggame.state.management.server.synced.ISettingsManager;
8
import de.pewpewproject.lasertag.lasertaggame.state.synced.implementation.GameModeState;
9
import de.pewpewproject.lasertag.networking.NetworkingConstants;
10
import de.pewpewproject.lasertag.networking.server.ServerEventSending;
11
import io.netty.buffer.Unpooled;
12
import net.minecraft.network.PacketByteBuf;
13
import net.minecraft.server.MinecraftServer;
14
15
import java.io.IOException;
16
import java.nio.file.Files;
17
import java.nio.file.Path;
18
import java.nio.file.StandardOpenOption;
19
20
/**
21
 * Implementation of the IGameModeManager for the lasertag game
22
 *
23
 * @author Étienne Muser
24
 */
25
public class GameModeManager implements IGameModeManager {
26
27
    private final GameModeState gameModeState;
28
29
    private ISettingsManager settingsManager;
30
31
    private final MinecraftServer server;
32
33
    // Get path to lasertag game mode file
34
    private static final Path lasertagGameModeFilePath = LasertagMod.configFolderPath.resolve("lasertagGameMode.ltg");
35
36
    public GameModeManager(GameModeState gameModeState, MinecraftServer server) {
37
38
        this.gameModeState = gameModeState;
39
        this.server = server;
40
41
        if (Files.exists(lasertagGameModeFilePath)) {
42
43
            try {
44
                // Read the game mode file
45
                gameModeState.currentGameModeTranslatableName = Files.readString(lasertagGameModeFilePath);
46
            } catch (IOException e) {
47
                LasertagMod.LOGGER.warn("Reading of lasertag game mode file failed: " + e.getMessage());
48
            }
49
        } else {
50
51
            try {
52
                // Write to the file
53
                Files.writeString(lasertagGameModeFilePath, gameModeState.currentGameModeTranslatableName, StandardOpenOption.CREATE);
54
            } catch (IOException ex) {
55
                LasertagMod.LOGGER.warn("Creation of new lasertag game mode file in '" + lasertagGameModeFilePath + "' failed: " + ex.getMessage());
56
            }
57
        }
58
    }
59
60
    public void setSettingsManager(ISettingsManager settingsManager) {
61
        this.settingsManager = settingsManager;
62
    }
63
64
    @Override
65
    public void setGameMode(GameMode newGameMode) {
66
        gameModeState.currentGameModeTranslatableName = newGameMode.getTranslatableName();
67
        persist(newGameMode);
68
        sync(newGameMode);
69
        settingsManager.reset();
70
    }
71
72
    @Override
73
    public GameMode getGameMode() {
74
        return GameModes.GAME_MODES.get(gameModeState.currentGameModeTranslatableName);
75
    }
76
77
    private void persist(GameMode newGameMode) {
78
79
        try {
80
            Files.createDirectories(lasertagGameModeFilePath.getParent());
81
            Files.writeString(lasertagGameModeFilePath, newGameMode.getTranslatableName(), StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
82
        } catch (IOException ex) {
83
            LasertagMod.LOGGER.warn("Failed to persist lasertag game mode: " + ex.getMessage());
84
        }
85
86
87
    }
88
89
    private void sync(GameMode newGameMode) {
90
91
        // Create packet
92
        var buf = new PacketByteBuf(Unpooled.buffer());
93
94
        buf.writeString(newGameMode.getTranslatableName());
95
96
        ServerEventSending.sendToEveryone(server, NetworkingConstants.GAME_MODE_SYNC, buf);
97
    }
98
}
99