EaglerForgeInjector/docs/apidoc/dedicatedserver.md
2024-12-09 18:08:52 +08:00

39 lines
1.5 KiB
Markdown

## ModAPI.dedicatedServer
This is the dedicated server module, used for modding singleplayer in more powerful ways.
ModAPI.dedicatedServer has the following methods:
- `appendCode(code: Function | String) : void`
- Injects provided code into the dedicated server.
### Modding the dedicated server
Eaglercraft runs the dedicated server in a service worker. This means that in order to run code to mod the dedicated server, you have to change context into the service worker.
`ModAPI.dedicatedServer.appendCode()` allows you to inject code into the dedicates server before startup. The code injected will throw errors if it attempts to access resources outside the functions context.
For example, take a look at this mod, which will inject some code that does nothing into the dedicated server:
```javascript
var myVariable = 4;
function myServerSideModCode() {
function subfunction() {
console.log("serverside!!!");
}
subfunction();
//Successfully running serverside
//However, we cannot access `myVariable`
}
ModAPI.dedicatedServer.appendCode(myServerSideModCode);
```
You can check if a mod if running serverside using this:
```javascript
function myServerSideModCode(isServerSide) {
console.log(isServerSide);
}
myServerSideModCode(); //logs undefined
ModAPI.dedicatedServer.appendCode(myServerSideModCode); //when server starts, will log `true`
```
Once serverside, you can only access server side events, like `serverstart` or `tick`. [See serverside events](events.md#server-side-events)