-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from voruti/config-file
- Loading branch information
Showing
6 changed files
with
167 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
javaVersion=11 |
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
25 changes: 25 additions & 0 deletions
25
src/main/java/voruti/velocityplayerlistquery/model/Config.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,25 @@ | ||
package voruti.velocityplayerlistquery.model; | ||
|
||
import lombok.Value; | ||
import lombok.experimental.Accessors; | ||
|
||
@Value | ||
@Accessors(fluent = true) | ||
public class Config { | ||
|
||
public static final String FILE_NAME = "config.json"; | ||
|
||
public static final Config DEFAULT_CONFIG = new Config( | ||
"%1$s" | ||
); | ||
|
||
|
||
/** | ||
* Configure how players are shown in the server list. | ||
* <ul> | ||
* <li>{@code %1$s}: placeholder for the player name</li> | ||
* <li>{@code %2$s}: placeholder for the server name, on which that player is currently playing on</li> | ||
* </ul> | ||
*/ | ||
String serverListEntryFormat; | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/voruti/velocityplayerlistquery/service/PersistenceService.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,65 @@ | ||
package voruti.velocityplayerlistquery.service; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import lombok.AccessLevel; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.experimental.FieldDefaults; | ||
import org.slf4j.Logger; | ||
import voruti.velocityplayerlistquery.model.Config; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
@RequiredArgsConstructor | ||
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) | ||
public class PersistenceService { | ||
|
||
@NonNull | ||
Logger logger; | ||
@NonNull | ||
Path dataDirectory; | ||
|
||
@NonNull | ||
final Gson gson = new GsonBuilder() | ||
.setPrettyPrinting() | ||
.create(); | ||
|
||
|
||
public Config loadConfig() { | ||
logger.trace("Loading config..."); | ||
|
||
try { | ||
// checks: | ||
if (!Files.exists(dataDirectory)) { | ||
Files.createDirectories(dataDirectory); | ||
} | ||
|
||
final Path configPath = dataDirectory.resolve(Config.FILE_NAME); | ||
if (!Files.exists(configPath)) { | ||
this.writeFile(configPath, Config.DEFAULT_CONFIG); | ||
return Config.DEFAULT_CONFIG; | ||
} | ||
if (!Files.isRegularFile(configPath) || !Files.isReadable(configPath)) { | ||
throw new IllegalStateException("Config file is invalid"); | ||
} | ||
|
||
// load: | ||
return gson.fromJson( | ||
Files.readString(configPath), | ||
Config.class | ||
); | ||
} catch (IOException | IllegalStateException e) { | ||
logger.error("Error while loading config", e); | ||
return Config.DEFAULT_CONFIG; | ||
} | ||
} | ||
|
||
private <T> void writeFile(Path path, T content) throws IOException { | ||
logger.trace("Writing file: {}", path); | ||
|
||
Files.write(path, gson.toJson(content).getBytes()); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/voruti/velocityplayerlistquery/util/ServerListEntryBuilder.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,31 @@ | ||
package voruti.velocityplayerlistquery.util; | ||
|
||
import com.velocitypowered.api.proxy.Player; | ||
import lombok.AccessLevel; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.experimental.FieldDefaults; | ||
import org.slf4j.Logger; | ||
import voruti.velocityplayerlistquery.model.Config; | ||
|
||
@RequiredArgsConstructor | ||
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) | ||
public class ServerListEntryBuilder { | ||
|
||
@NonNull | ||
Logger logger; | ||
@NonNull | ||
Config config; | ||
|
||
|
||
public String buildForPlayer(Player player) { | ||
logger.trace("Building server list entry for player {}...", player.getUsername()); | ||
return String.format( | ||
this.config.serverListEntryFormat(), | ||
player.getGameProfile().getName(), | ||
player.getCurrentServer() | ||
.map(serverConnection -> serverConnection.getServerInfo().getName()) | ||
.orElse("unknown") | ||
); | ||
} | ||
} |