Skip to content

Commit

Permalink
Move loggers into classes where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
stirante committed Jun 14, 2024
1 parent 5029bbb commit 88a80a5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
5 changes: 2 additions & 3 deletions src/Timings.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Logger } from "./Logging";

const log = Logger.getLogger("Timings", 'timings');
/**
* A simple class to measure the time it takes to perform an operation.
*/
export default class Timings {
private static readonly log = Logger.getLogger("Timings", 'timings');
static lastTime = -1;
static lastOperation = "";

Expand All @@ -29,7 +28,7 @@ export default class Timings {
static end() {
const time = new Date().getTime();
if (this.lastTime > 0) {
log.debug(`Operation ${this.lastOperation} took ${time - this.lastTime}ms`);
Timings.log.debug(`Operation ${this.lastOperation} took ${time - this.lastTime}ms`);
}
this.lastTime = -1;
}
Expand Down
15 changes: 7 additions & 8 deletions src/Vec3.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Vector3, Direction } from "@minecraft/server";
import { Logger } from "./Logging";

const log = Logger.getLogger("vec3", "vec3", "bedrock-boost");

type VectorLike = Vector3 | Vec3 | Direction | number[] | number

export default class Vec3 implements Vector3 {
private static readonly log = Logger.getLogger("vec3", "vec3", "bedrock-boost");
public static readonly Zero = new Vec3(0, 0, 0);
public static readonly Down = new Vec3(Direction.Down);
public static readonly Up = new Vec3(Direction.Up);
Expand Down Expand Up @@ -61,7 +60,7 @@ export default class Vec3 implements Vector3 {
this.z = x.z;
} else {
if (!x || (!x.x && x.x !== 0) || (!x.y && x.y !== 0) || (!x.z && x.z !== 0)) {
log.error(new Error("Invalid vector"), x);
Vec3.log.error(new Error("Invalid vector"), x);
throw new Error("Invalid vector");
}
this.x = x.x;
Expand Down Expand Up @@ -93,7 +92,7 @@ export default class Vec3 implements Vector3 {
if (x === Direction.East) return Vec3.East;
if (x === Direction.West) return Vec3.West;
if (!x || (!(x as any).x && (x as any).x !== 0) || (!(x as any).y && (x as any).y !== 0) || (!(x as any).z && (x as any).z !== 0)) {
log.error(new Error('Invalid arguments'), x, y, z);
Vec3.log.error(new Error('Invalid arguments'), x, y, z);
throw new Error('Invalid arguments');
}
return new Vec3((x as any).x as number, (x as any).y as number, (x as any).z as number);
Expand All @@ -113,7 +112,7 @@ export default class Vec3 implements Vector3 {
if (x === Direction.East) return Vec3.East;
if (x === Direction.West) return Vec3.West;
if (!x || (!(x as any).x && (x as any).x !== 0) || (!(x as any).y && (x as any).y !== 0) || (!(x as any).z && (x as any).z !== 0)) {
log.error(new Error('Invalid arguments'), x, y, z);
Vec3.log.error(new Error('Invalid arguments'), x, y, z);
throw new Error('Invalid arguments');
}
return new Vec3((x as any).x as number, (x as any).y as number, (x as any).z as number);
Expand Down Expand Up @@ -221,7 +220,7 @@ export default class Vec3 implements Vector3 {
*/
normalize(): Vec3 {
if (this.isZero()) {
log.error(new Error("Cannot normalize zero-length vector"));
Vec3.log.error(new Error("Cannot normalize zero-length vector"));
throw new Error("Cannot normalize zero-length vector");
}
const len = this.length();
Expand Down Expand Up @@ -627,7 +626,7 @@ export default class Vec3 implements Vector3 {
*/
toDirection(): Direction {
if (this.isZero()) {
log.error(new Error("Cannot convert zero-length vector to direction"));
Vec3.log.error(new Error("Cannot convert zero-length vector to direction"));
throw new Error("Cannot convert zero-length vector to direction");
}
const normalized = this.normalize();
Expand All @@ -639,7 +638,7 @@ export default class Vec3 implements Vector3 {
if (maxValue === normalized.z) return Direction.North;
if (maxValue === -normalized.z) return Direction.South;
// This should never happen
log.error(new Error("Cannot convert vector to direction"), this);
Vec3.log.error(new Error("Cannot convert vector to direction"), this);
throw new Error("Cannot convert vector to direction");
}
/**
Expand Down
8 changes: 4 additions & 4 deletions src/scheduling/PlayerPulseScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { Player, system, world } from "@minecraft/server";
import PulseScheduler from "./PulseScheduler";
import { Logger } from "../Logging";

const log = Logger.getLogger("PlayerPulseScheduler", "bedrock-boost", "player-pulse-scheduler");

/**
* Represents a PulseScheduler that processes players.
*/
export default class PlayerPulseScheduler extends PulseScheduler<Player> {

private static readonly log = Logger.getLogger("PlayerPulseScheduler", "bedrock-boost", "player-pulse-scheduler");

/**
* Creates a new EntityPulseScheduler instance.
* @param period The period of the scheduler.
Expand All @@ -35,7 +35,7 @@ export default class PlayerPulseScheduler extends PulseScheduler<Player> {
const pushPlayer = () => {
attempts++;
if (attempts > 10) {
log.warn("Failed to push player to scheduler after 10 attempts.");
PlayerPulseScheduler.log.warn("Failed to push player to scheduler after 10 attempts.");
return;
}
try {
Expand All @@ -47,7 +47,7 @@ export default class PlayerPulseScheduler extends PulseScheduler<Player> {
this.push(player);
}
} catch (e) {
log.debug("Failed to push player to scheduler.", e);
PlayerPulseScheduler.log.debug("Failed to push player to scheduler.", e);
system.runTimeout(pushPlayer, 1);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/scheduling/PulseScheduler.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { system } from "@minecraft/server";
import { Logger } from "../Logging";

const log: Logger = Logger.getLogger("PulseScheduler", "bedrock-boost", "pulse-scheduler");

/**
* Represents a scheduler that executes a processor function at regular intervals for each item in the list.
* @template T The type of items in the scheduler.
*/
export default class PulseScheduler<T> {

private static readonly log: Logger = Logger.getLogger("PulseScheduler", "bedrock-boost", "pulse-scheduler");
protected items: T[] = [];
private period: number;
private currentTick: number = 0;
Expand Down Expand Up @@ -120,13 +120,13 @@ export default class PulseScheduler<T> {

private tick() {
if (this.items.length === 0) {
log.trace("No items to process.");
PulseScheduler.log.trace("No items to process.");
return;
}
// Number of items to process this tick
const scheduledExecutions = this.executionSchedule[this.currentTick];
if (scheduledExecutions === 0) {
log.trace("No items to process this tick.");
PulseScheduler.log.trace("No items to process this tick.");
// Increment the tick counter
this.currentTick = (this.currentTick + 1) % this.period;
// Reset the index if we're at the end of the period
Expand All @@ -145,7 +145,7 @@ export default class PulseScheduler<T> {
try {
this.processor(this.items[this.nextIndex]);
} catch (e) {
log.error("Error processing item", e);
PulseScheduler.log.error("Error processing item", e);
}
executed++;
}
Expand Down

0 comments on commit 88a80a5

Please sign in to comment.