Skip to content

Commit

Permalink
feat: add option to persist old worlds after restarting the server
Browse files Browse the repository at this point in the history
  • Loading branch information
NearW committed May 9, 2021
1 parent 05a5d60 commit 91e08b5
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ _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
"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
}
```

Expand Down
3 changes: 2 additions & 1 deletion example/speedrun.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"WHITELIST": ["Notch"],
"DATA_PACK": false,
"SEEDS": ["-9223372036854775808", "9223372036854775807"],
"AUTO_SAVE": false
"AUTO_SAVE": false,
"KEEP_WORLDS": false
}
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions src/adapter/world.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { renameWorldFolder } from "./world"
const shell = require("shelljs")

describe("World", () => {
beforeEach(() => {
Object.assign(shell, {
mv: jest.fn()
})
Date.now = jest.fn().mockReturnValue(new Date(Date.UTC(2021, 4, 21, 15, 31, 24)).valueOf())
})

describe("renameWorldFolder", () => {
it("should rename a world and postfix it with a time", () => {
renameWorldFolder()

expect(shell.mv).toHaveBeenCalledWith("world", "world_2021-05-21_15-31-24")
})
})
})
10 changes: 10 additions & 0 deletions src/adapter/world.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import rimraf from "rimraf"
import shell from "shelljs"

export function deleteWorldFolder() {
console.log("Deleting world folder ...")
rimraf.sync("world")
}

export function renameWorldFolder() {
const isoString = new Date(Date.now()).toISOString()
const end = isoString.lastIndexOf(".")
const timeString = isoString.replace(/:/g, "-").replace("T", "_").slice(0, end)
const renamedWorldFolder = `world_${timeString}`
console.log(`Renaming world folder to ${renamedWorldFolder}`)
shell.mv("world", renamedWorldFolder)
}
1 change: 1 addition & 0 deletions src/model/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export interface Configuration {
DATA_PACK: boolean
SEEDS: string[]
AUTO_SAVE: boolean
KEEP_WORLDS: boolean
}
10 changes: 7 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ import { ChildProcess, spawn } from "child_process"
import shell from "shelljs"
import { parseConfiguration } from "./adapter/configuration"
import { initSeed } from "./adapter/serverProperties"
import { deleteWorldFolder } from "./adapter/world"
import { deleteWorldFolder, renameWorldFolder } from "./adapter/world"

async function startServer() {
setupErrorListeners()

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

deleteWorldFolder()
if (KEEP_WORLDS) {
renameWorldFolder()
} else {
deleteWorldFolder()
}
await initSeed(SEEDS, configuration)

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

0 comments on commit 91e08b5

Please sign in to comment.