Skip to content

Commit

Permalink
feat: add option in configuration to set the jar name
Browse files Browse the repository at this point in the history
  • Loading branch information
NearW committed Aug 1, 2021
1 parent eb0ab87 commit f31389f
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 26 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ _Note: Enabling flight will prevent random disconnects when traveling too fast v
"WHITELIST": ["Notch"], // List of whitelisted players
"DATA_PACK": false, // set to true, if datapacks should be copied into the world
"SEEDS": ["-9223372036854775808", "9223372036854775807"], // List of set seeds that are played one by one, set [] to disable this option
"AUTO_SAVE": false, // Turn off to prevent 5 minute lag, default false
"KEEP_WORLDS": false, // Turn on if worlds should be renamed instead of deleted, default false
"LOAD_WORLD": "world_2021-05-21_15-31-24" // Default: "". Set world folder name that should be loaded on server start instead of deleting/archiving the world.
"AUTO_SAVE": false, // Turn off to prevent 5 minute lag
"KEEP_WORLDS": false, // Turn on if worlds should be renamed instead of deleted
"LOAD_WORLD": "world_2021-05-21_15-31-24", // Set world folder name that should be loaded on server start instead of deleting/archiving the world.
"JAR_NAME": "server.jar" // Set this value in case you use a different jar, e.g. by using a fabric server.
}
```

Expand All @@ -53,7 +54,8 @@ _Note: Enabling flight will prevent random disconnects when traveling too fast v
"SEEDS": [],
"AUTO_SAVE": false,
"KEEP_WORLDS": false,
"LOAD_WORLD": ""
"LOAD_WORLD": "",
"JAR_NAME": "server.jar"
}
```

Expand Down
3 changes: 2 additions & 1 deletion example/speedrun.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"SEEDS": ["-9223372036854775808", "9223372036854775807"],
"AUTO_SAVE": false,
"KEEP_WORLDS": false,
"LOAD_WORLD": ""
"LOAD_WORLD": "",
"JAR_NAME": "server.jar"
}
12 changes: 8 additions & 4 deletions src/adapter/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Configuration } from "../model/configuration"
import { promises } from "fs"
import { validateConfiguration } from "../validation/configuration"
import { ConfigurationApi } from "../model/configurationApi"

export async function parseConfiguration(): Promise<Configuration> {
console.log("Parsing speedrun.json ...")
const json = await promises.readFile("speedrun.json", "utf-8")
const configuration: Configuration = JSON.parse(json)
return useFallbacks(configuration)
const configuration: ConfigurationApi = JSON.parse(json)
validateConfiguration(configuration)
return useFallbacks(configuration as Configuration)
}

export async function removeSeed(configuration: Configuration) {
Expand All @@ -14,7 +17,7 @@ export async function removeSeed(configuration: Configuration) {
await promises.writeFile("speedrun.json", JSON.stringify(configuration), "utf8")
}

async function useFallbacks(configuration: Configuration): Promise<Configuration> {
async function useFallbacks(configuration: ConfigurationApi): Promise<Configuration> {
return {
MIN_RAM: configuration.MIN_RAM,
MAX_RAM: configuration.MAX_RAM,
Expand All @@ -24,6 +27,7 @@ async function useFallbacks(configuration: Configuration): Promise<Configuration
SEEDS: configuration.SEEDS ?? [],
AUTO_SAVE: configuration.AUTO_SAVE ?? false,
KEEP_WORLDS: configuration.KEEP_WORLDS ?? false,
LOAD_WORLD: configuration.LOAD_WORLD ?? ""
LOAD_WORLD: configuration.LOAD_WORLD ?? "",
JAR_NAME: configuration.JAR_NAME ?? "server.jar"
}
}
15 changes: 8 additions & 7 deletions src/model/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
export interface Configuration {
MIN_RAM: number
MAX_RAM: number
OP?: string[]
WHITELIST?: string[]
DATA_PACK?: boolean
SEEDS?: string[]
AUTO_SAVE?: boolean
KEEP_WORLDS?: boolean
LOAD_WORLD?: string
OP: string[]
WHITELIST: string[]
DATA_PACK: boolean
SEEDS: string[]
AUTO_SAVE: boolean
KEEP_WORLDS: boolean
LOAD_WORLD: string
JAR_NAME: string
}
12 changes: 12 additions & 0 deletions src/model/configurationApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export interface ConfigurationApi {
MIN_RAM: number
MAX_RAM: number
OP?: string[]
WHITELIST?: string[]
DATA_PACK?: boolean
SEEDS?: string[]
AUTO_SAVE?: boolean
KEEP_WORLDS?: boolean
LOAD_WORLD?: string
JAR_NAME?: string
}
11 changes: 7 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ import shell from "shelljs"
import { parseConfiguration } from "./adapter/configuration"
import { initSeed, setWorld } from "./adapter/serverProperties"
import { deleteWorldFolder, renameWorldFolder } from "./adapter/world"
import { validateConfiguration } from "./validation/configuration"

async function startServer() {
setupErrorListeners()

const configuration = await parseConfiguration()
validateConfiguration(configuration)
const { MIN_RAM, MAX_RAM, OP, WHITELIST, DATA_PACK, SEEDS, AUTO_SAVE, KEEP_WORLDS, LOAD_WORLD } = configuration
const { MIN_RAM, MAX_RAM, OP, WHITELIST, DATA_PACK, SEEDS, AUTO_SAVE, KEEP_WORLDS, LOAD_WORLD, JAR_NAME } = configuration

if (LOAD_WORLD) {
await setWorld(LOAD_WORLD)
Expand All @@ -23,7 +21,7 @@ async function startServer() {
await initSeed(SEEDS, configuration)
}

const server = spawn("java", [`-Xms${MIN_RAM}G`, `-Xmx${MAX_RAM}G`, "-jar", "server.jar", "nogui"])
const server = spawn("java", [`-Xms${MIN_RAM}G`, `-Xmx${MAX_RAM}G`, "-jar", JAR_NAME, "nogui"])
redirectStdio(server)
setupExitListener(server)

Expand Down Expand Up @@ -51,6 +49,11 @@ async function startServer() {
}
})

server.stderr.on("data", () => {
server.kill()
process.exit(1)
})

server.on("exit", async () => {
server.kill()
await startServer()
Expand Down
8 changes: 4 additions & 4 deletions src/validation/configuration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { validateConfiguration } from "./configuration"
import { Configuration } from "../model/configuration"
import { ConfigurationApi } from "../model/configurationApi"

describe("Configuration", () => {
function testShouldFail() {
Expand All @@ -22,7 +22,7 @@ describe("Configuration", () => {

it("should throw an error if mandatory value is missing", () => {
const message = "Error during validation. Missing configuration values for: MAX_RAM."
const configuration: Configuration = { MIN_RAM: 4, MAX_RAM: undefined }
const configuration = { MIN_RAM: 4, MAX_RAM: undefined }

try {
validateConfiguration(configuration)
Expand All @@ -33,13 +33,13 @@ describe("Configuration", () => {
})

it("should not throw an error if configuration is valid", () => {
const configuration: Configuration = { MIN_RAM: 4, MAX_RAM: 8 }
const configuration: ConfigurationApi = { MIN_RAM: 4, MAX_RAM: 8 }

validateConfiguration(configuration)
})

it("should not throw an error if optional property exists without a value", () => {
const configuration: Configuration = { MIN_RAM: 4, MAX_RAM: 8, DATA_PACK: undefined }
const configuration: ConfigurationApi = { MIN_RAM: 4, MAX_RAM: 8, DATA_PACK: undefined }

validateConfiguration(configuration)
})
Expand Down
4 changes: 2 additions & 2 deletions src/validation/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Configuration } from "../model/configuration"
import { ConfigurationApi } from "../model/configurationApi"

export function validateConfiguration(configuration: Configuration) {
export function validateConfiguration(configuration: ConfigurationApi) {
const mandatoryFields = new Set(["MIN_RAM", "MAX_RAM"])
const missingValues = []

Expand Down

0 comments on commit f31389f

Please sign in to comment.