Skip to content

Commit

Permalink
use a validation time interval to improve perf
Browse files Browse the repository at this point in the history
  • Loading branch information
vytdev committed Jun 17, 2024
1 parent 97382e0 commit 3310d81
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/server/client.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { GameMode, ItemStack, Player, system, world } from "@minecraft/server";
import { GameMode, ItemStack, Player, world } from "@minecraft/server";
import {
Database,
events,
message,
msToString,
formatNumber,
getPlayerByName,
setTickInterval,
config
} from "../catalyst/index.js";
import { smpName, combatTime, ranks } from "./index.js";
import { smpName, validationInterval, combatTime, ranks } from "./index.js";
import { isPlayerAdmin, setPlayerAdmin } from "./utils.js";

/**
Expand Down Expand Up @@ -201,8 +202,10 @@ events.on("afterEntityHurt", ev => {
getClientById(ev.damageSource.damagingEntity.id)?.setCombatTag();
});

let validationTimeCounter = 0;

// loop checks and sidebar update
system.runInterval(() => {
setTickInterval(() => {
// get the date
const d = new Date();
const date =
Expand All @@ -211,15 +214,20 @@ system.runInterval(() => {
d.getFullYear().toString();

clients.forEach(v => {
// non-admin players should not have op permissions
// and should always be in survival mode
if (!v.isAdmin) {
v.player.setOp(false);
v.player.setGameMode(GameMode.survival);
}
// admin players
else {
v.player.setOp(true);

// a validation time interval to improve server performance
if (++validationTimeCounter >= validationInterval) {
validationTimeCounter = 0;
// non-admin players should not have op permissions
// and should always be in survival mode
if (!v.isAdmin) {
v.player.setOp(false);
v.player.setGameMode(GameMode.survival);
}
// admin players
else {
v.player.setOp(true);
}
}

// update playtime
Expand Down
5 changes: 5 additions & 0 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export const logger = new Logger();
*/
export const smpName = "Infinity";

/**
* time interval per permission validation (in ticks)
*/
export const validationInterval = 20 * 5; // 5 seconds

/**
* the name of the owner
*/
Expand Down

0 comments on commit 3310d81

Please sign in to comment.