Skip to content

Commit

Permalink
feat: add option to load a specific world for aa runs
Browse files Browse the repository at this point in the history
  • Loading branch information
NearW committed May 9, 2021
1 parent 91e08b5 commit 9460ffe
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 25 deletions.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Minecraft Speedrun Server

This project provides a better startup for a speedrun server as it automatically:
This project provides a better startup for a speedrun server as it can automatically:

- Deletes the world folder after stopping the server
- Restarts the server
- Turns off auto-save to prevent the lag every 5 minutes
- Moves datapacks to the world folder **(optional)**
- Sets the seed from a list of seeds sequentially **(optional)**
- Delete the world folder after stopping the server
- Rename the world folder after stopping the server to persist previously played worlds
- Restart the server
- Turn off auto-save to prevent the lag every 5 minutes
- Move datapacks to the world folder
- Set the seed from a list of seeds sequentially
- Load a specific world for All Advancement runs

## Prerequisites

Expand Down Expand Up @@ -36,7 +38,8 @@ _Note: Enabling flight will prevent random disconnects when traveling too fast v
"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
"KEEP_WORLDS": false, // Turn on if worlds should be renamed instead of deleted, default false
"LOAD_WORLD": "world_2021-05-21_15-31-24" // Set world folder name that should be loaded on server start, leave empty ("") to disable
}
```

Expand All @@ -49,3 +52,8 @@ minecraft server and put the unzipped datapacks inside and enable it in the `spe

Add seeds to the `SEEDS` list, if you want to restart the world with set seeds. On each restart, the first seed of the list will be loaded and removed from the list.
This is really helpful, if you want to do some speedrun battles with a pool of set seeds.

### All Advancements

Set `AUTO_SAVE` and `KEEP_WORLDS` to `true` while resetting for a good seed. Once a playable seed is found,
set `LOAD_WORLD` to the world folder name of that world to prevent future resets.
3 changes: 2 additions & 1 deletion example/speedrun.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"DATA_PACK": false,
"SEEDS": ["-9223372036854775808", "9223372036854775807"],
"AUTO_SAVE": false,
"KEEP_WORLDS": false
"KEEP_WORLDS": false,
"LOAD_WORLD": "world_2021-05-21_15-31-24"
}
23 changes: 12 additions & 11 deletions src/adapter/serverProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@ export async function initSeed(seeds: string[], configuration: Configuration) {
}
}

export async function setWorld(world: string) {
console.log(`Setting world ${world} ...`)
const serverProperties = await promises.readFile(FILE_NAME, "utf-8")
await setProperty(serverProperties, "level-name", world)
}

async function setSeed(seed) {
console.log(`Setting seed ${seed} ...`)
const serverProperties = await promises.readFile(FILE_NAME, "utf-8")
const updated = getUpdatedServerProperties(serverProperties, seed)

await promises.writeFile(FILE_NAME, updated, "utf-8")
await setProperty(serverProperties, "level-seed", seed)
}

async function resetSeed() {
console.log("Resetting seed ...")
const serverProperties = await promises.readFile(FILE_NAME, "utf-8")
const updated = getUpdatedServerProperties(serverProperties)

await promises.writeFile(FILE_NAME, updated, "utf-8")
await setProperty(serverProperties, "level-seed")
}

async function seedExists() {
Expand All @@ -41,10 +43,9 @@ async function seedExists() {
return end - start > 1
}

function getUpdatedServerProperties(serverProperties: string, seed: string = "") {
const key = "level-seed="
const start = serverProperties.indexOf(key) + key.length
async function setProperty(serverProperties: string, key: string, value: string = "") {
const start = serverProperties.indexOf(key) + key.length + 1
const end = serverProperties.indexOf("\n", start)

return serverProperties.substring(0, start) + seed + serverProperties.substring(end)
const updated = serverProperties.substring(0, start) + value + serverProperties.substring(end)
await promises.writeFile(FILE_NAME, updated, "utf-8")
}
1 change: 1 addition & 0 deletions src/model/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export interface Configuration {
SEEDS: string[]
AUTO_SAVE: boolean
KEEP_WORLDS: boolean
LOAD_WORLD: string
}
16 changes: 10 additions & 6 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { ChildProcess, spawn } from "child_process"
import shell from "shelljs"
import { parseConfiguration } from "./adapter/configuration"
import { initSeed } from "./adapter/serverProperties"
import { initSeed, setWorld } from "./adapter/serverProperties"
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, KEEP_WORLDS } = configuration
const { MIN_RAM, MAX_RAM, OP, WHITELIST, DATA_PACK, SEEDS, AUTO_SAVE, KEEP_WORLDS, LOAD_WORLD } = configuration

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

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

0 comments on commit 9460ffe

Please sign in to comment.