mirror of
https://github.com/eaglerforge/EaglerForgeInjector
synced 2025-07-23 06:01:38 -09:00
partially complete timescale tutorial
This commit is contained in:
parent
a8cd453a68
commit
4c7fdf5f86
@ -35,4 +35,4 @@ Prerequisites:
|
||||
Tutorials:
|
||||
- [Custom Blocks](custom_block.md)
|
||||
- [Custom Items](custom_item.md)
|
||||
- [Timescale Command](comingsoon.md)
|
||||
- [Timescale Command](timescale.md)
|
25
docs/tutorials/timescale.md
Normal file
25
docs/tutorials/timescale.md
Normal file
@ -0,0 +1,25 @@
|
||||
## Timescale Mod with ModAPI
|
||||
This mod will cover adding a new command that controls the speed that Eaglercraft runs at. This tutorial assumes that you have some knowledge on how to use [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) in JavaScript.
|
||||
|
||||
Let's get our basic template code down:
|
||||
|
||||
```javascript
|
||||
(function TimescaleCommand() {
|
||||
ModAPI.meta.title("Timescale Command");
|
||||
ModAPI.meta.description("use /timescale to control time");
|
||||
ModAPI.meta.credits("By <author_name>");
|
||||
})()
|
||||
```
|
||||
|
||||
Our mod is going to be split into 2 distinct parts: client-side and server-side. The client will modify `ModAPI.mc.timer.timerSpeed` when a `/timescale` command is sent, while the server will set the timescale to a global variable, and then modify `net.minecraft.server.MinecraftServer`'s `getCurrentTimeMillis()` to change the rate calculations happen at on the server.\
|
||||
\
|
||||
However, there is a slight logistical problem: the server gets the time in milliseconds as a JavaScript [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt), which means we can't just multiply it by a number, we have to either multiply or divide it by another BigInt.
|
||||
```javascript
|
||||
var x = 1n * 1.0; //TypeError
|
||||
|
||||
var x = 1n * 1n; //Success
|
||||
```
|
||||
To allow us to both speed up and slow down time, we'll need to check if the speed inputted is greater or equal to 1. If it is, we round it and then convert it to a `BigInt`, which we'll store on the `globalThis` object. If it's less we'll need to find the speed to the power of -1, or `1 / speed`. We can then round this value and convert that to a `BigInt` to store on `globalThis`. We'll also set `globalThis.timeScaleDividing` to `true`, to signal to replaced `getCurrentTimeMillis()` to divide by the `BigInt` speed factor instead of multiply.\
|
||||
\
|
||||
Finally, due to our `BigInt` rounding shenanigans on the server, we have to replicate the rounding inaccuracy on the client.
|
||||
|
@ -6,14 +6,14 @@
|
||||
if (event.message.toLowerCase().startsWith("/timescale")) {
|
||||
var speed = parseFloat(event.message.split(" ")[1]);
|
||||
if (!speed) {
|
||||
PluginAPI.javaClient.$timer.$timerSpeed = 1;
|
||||
PluginAPI.mc.timer.timerSpeed = 1;
|
||||
} else {
|
||||
if (speed < 1) {
|
||||
speed = 1 / Math.round(1 / speed);
|
||||
} else {
|
||||
speed = Math.round(speed);
|
||||
}
|
||||
PluginAPI.javaClient.$timer.$timerSpeed = speed;
|
||||
PluginAPI.mc.timer.timerSpeed = speed;
|
||||
}
|
||||
PluginAPI.displayToChat("[Timescale] Set world timescale to " + speed.toFixed(2) + ".");
|
||||
}
|
||||
@ -37,13 +37,13 @@
|
||||
}
|
||||
}
|
||||
if (ModAPI.server) {
|
||||
ModAPI.server.currentTime = PluginAPI.hooks.methods.nms_MinecraftServer_getCurrentTimeMillis();
|
||||
ModAPI.server.currentTime = PluginAPI.hooks.methods[ModAPI.util.getMethodFromPackage("net.minecraft.server.MinecraftServer", "getCurrentTimeMillis")]();
|
||||
}
|
||||
event.preventDefault = true;
|
||||
}
|
||||
});
|
||||
const original_getCurrentTime = ModAPI.hooks.methods.nms_MinecraftServer_getCurrentTimeMillis;
|
||||
PluginAPI.hooks.methods.nms_MinecraftServer_getCurrentTimeMillis = function () {
|
||||
const original_getCurrentTime = ModAPI.hooks.methods[ModAPI.util.getMethodFromPackage("net.minecraft.server.MinecraftServer", "getCurrentTimeMillis")];
|
||||
PluginAPI.hooks.methods[ModAPI.util.getMethodFromPackage("net.minecraft.server.MinecraftServer", "getCurrentTimeMillis")] = function () {
|
||||
if (globalThis.timeScaleDividing) {
|
||||
return original_getCurrentTime() / globalThis.timeScale;
|
||||
} else {
|
||||
|
Loading…
x
Reference in New Issue
Block a user