Skip to content

Commit

Permalink
Updated tool saving and save gradients now
Browse files Browse the repository at this point in the history
  • Loading branch information
SIsilicon committed Jan 14, 2024
1 parent 94784af commit 51d2624
Show file tree
Hide file tree
Showing 18 changed files with 105 additions and 107 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"typescript": "^4.7.4"
},
"dependencies": {
"@minecraft/server": "^1.9.0-beta.1.20.60-preview.22",
"@minecraft/server": "^1.9.0-beta.1.20.60-preview.24",
"@minecraft/server-admin": "^1.0.0-beta.1.19.80-stable",
"@minecraft/server-ui": "^1.2.0-beta.1.20.60-preview.22"
}
Expand Down
82 changes: 42 additions & 40 deletions src/library/classes/databaseBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,60 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { world } from "@minecraft/server";
import { Entity, World, world } from "@minecraft/server";
import { Server } from "./serverBuilder.js";

const objective = world.scoreboard.getObjective("GAMETEST_DB") ?? world.scoreboard.addObjective("GAMETEST_DB", "");

export class Database {
private data: {[key: string]: any} = null;
private opened = false;
export class Database<T extends {} = {[key: string]: any}> {
private data: T;
private provider: World | Entity;

constructor(private name: string) {}
constructor(private name: string, provider: World | Entity = world, reviver?: (key: string, value: any) => any) {
const table = this.getScoreboardParticipant();
this.data = table ? JSON.parse(JSON.parse(`"${table.displayName}"`), reviver)[1] : {};
this.provider = provider;
}

/**
* Save a value or update a value in the Database under a key
* @param {string} key The key you want to save the value as
* @param {any} value The value you want to save
* @param key The key you want to save the value as
* @param value The value you want to save
* @example Database.set('Test Key', 'Test Value');
*/
set(key: string | number, value: any): void {
set(key: keyof T, value: T[typeof key]): void {
this.data[key] = value;
}
/**
* Get the value of the key
* @param {string} key
* @returns {any}
* @param key
* @returns value
* @example Database.get('Test Key');
*/
get(key: string): any {
get(key: keyof T): T[typeof key] {
return this.data[key];
}
/**
* Check if the key exists in the table
* @param {string} key
* @returns {boolean}
* @param key
* @returns Whether the key exists
* @example Database.has('Test Key');
*/
has(key: string): boolean {
has(key: keyof T): boolean {
return key in this.data;
}
/**
* Delete the key from the table
* @param {string} key
* @returns {boolean}
* @param key
* @example Database.delete('Test Key');
*/
delete(key: string): void {
delete(key: keyof T): void {
delete this.data[key];
}
/**
* Clear everything in the table
* @example Database.clear()
*/
clear(): void {
this.data = {};
}
/**
* Load the table from the scoreboard data.
* @example Database.load()
*/
load(): void {
const table = this.getScoreboardParticipant();
this.data = table ? JSON.parse(JSON.parse(`"${table.displayName}"`))[1] : {};
this.opened = true;
this.data = {} as T;
}
/**
* Save all changes to the scoreboard.
Expand All @@ -71,47 +65,55 @@ export class Database {
if (table) {
Server.runCommand(`scoreboard players reset "${table.displayName}" GAMETEST_DB`);
}
Server.runCommand(`scoreboard players add ${JSON.stringify(JSON.stringify([this.name, this.data]))} GAMETEST_DB 0`);
Server.runCommand(`scoreboard players add ${JSON.stringify(JSON.stringify(["wedit:"+this.name, this.data]))} GAMETEST_DB 0`);
}
/**
* Get all the keys in the table
* @returns {Array<string>}
* @returns Array of keys
* @example Database.keys();
*/
keys(): Array<string> {
return Object.keys(this.data);
keys(): (keyof T)[] {
return Object.keys(this.data) as (keyof T)[];
}
/**
* Get all the values in the table
* @returns {Array<any>}
* @returns Array of values
* @example Database.values();
*/
values(): Array<any> {
values(): (T[keyof T])[] {
return Object.values(this.data);
}
/**
* Get all the keys and values in the table in pairs
* @returns Array of key/value pairs
* @example Database.entries();
*/
entries(): [keyof T, T[keyof T]][] {
return Object.entries(this.data) as [keyof T, T[keyof T]][];
}
/**
* Check if all the keys exists in the table
* @param {string} keys
* @returns {boolean}
* @param keys
* @returns Whether all keys exist
* @example Database.hasAll('Test Key', 'Test Key 2', 'Test Key 3');
*/
hasAll(...keys: Array<string>): boolean {
hasAll(...keys: (keyof T)[]): boolean {
return keys.every((k) => this.has(k));
}
/**
* Check if any of the keys exists in the table
* @param {string} keys
* @returns {boolean}
* @param keys
* @returns Whether any key exists
* @example Database.hasAny('Test Key', 'Test Key 2', 'Test Key 3');
*/
hasAny(...keys: Array<string>): boolean {
hasAny(...keys: (keyof T)[]): boolean {
return keys.some((k) => this.has(k));
}

private getScoreboardParticipant() {
for (const table of objective.getParticipants()) {
const name = table.displayName;
if (name.startsWith(`[\\"${this.name}\\"`)) {
if (name.startsWith(`[\\"wedit:${this.name + (this.provider instanceof Entity ? this.provider.id : "")}\\"`)) {
return table;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/brushes/cylinder_brush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class CylinderBrush extends Brush {
id: this.id,
radius: this.radius,
height: this.height,
pattern: this.pattern.getSource(),
pattern: this.pattern,
hollow: this.hollow
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/server/brushes/overlay_brush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ export class OverlayBrush extends Brush {
id: this.id,
radius: this.radius,
depth: this.depth,
pattern: this.pattern.getSource(),
surfaceMask: this.surfaceMask.getSource()
pattern: this.pattern,
surfaceMask: this.surfaceMask
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/brushes/smooth_brush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class SmoothBrush extends Brush {
id: this.id,
radius: this.size,
iterations: this.iterations,
mask: this.mask.getSource()
mask: this.mask
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/brushes/sphere_brush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class SphereBrush extends Brush {
return {
id: this.id,
radius: this.radius,
pattern: this.pattern.getSource(),
pattern: this.pattern,
hollow: this.hollow
};
}
Expand Down
3 changes: 1 addition & 2 deletions src/server/modules/biome_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ class BiomeChanges {

flush() {
for (const [chunk, data] of this.changes) {
const tableName = `wedit:biome,${this.dimension.id},${chunk}`;
const tableName = `biome,${this.dimension.id},${chunk}`;
const database = new Database(tableName);
database.load();

let biomes: number[] = [];
if (!database.has("biomes")) {
Expand Down
2 changes: 1 addition & 1 deletion src/server/modules/mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class Mask implements CustomArgType {
return text;
}

getSource() {
toJSON() {
return this.stringObj;
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/modules/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class Pattern implements CustomArgType {
return text;
}

getSource() {
toJSON() {
return this.stringObj;
}

Expand Down
16 changes: 12 additions & 4 deletions src/server/sessions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Player } from "@minecraft/server";
import { Server, Vector, setTickTimeout, contentLog } from "@notbeer-api";
import { Server, Vector, setTickTimeout, contentLog, Database } from "@notbeer-api";
import { Tools } from "./tools/tool_manager.js";
import { History } from "@modules/history.js";
import { Mask } from "@modules/mask.js";
Expand Down Expand Up @@ -31,6 +31,10 @@ interface superPickaxe {
range: number
}

interface gradients {
[id: string]: { dither: number, patterns: Pattern[] }
}

/**
* Represents a WorldEdit user's current session with the addon.
* It manages their selections, operation history, and other things related to WorldEdit per player.
Expand Down Expand Up @@ -100,7 +104,7 @@ export class PlayerSession {
private playerId: string;
private history: History;
private regions = new Map<string, RegionBuffer>();
private gradients = new Map<string, {dither: number, patterns: Pattern[]}>();
private gradients: Database<gradients>;
private placementMode: "player" | "selection" = "player";

private _drawOutlines: boolean;
Expand All @@ -111,7 +115,11 @@ export class PlayerSession {
this.history = new History(this);
this.selection = new Selection(player);
this.drawOutlines = config.drawOutlines;

this.gradients = new Database<gradients>("gradients", player, (k, v) => {
if (k === "patterns") return (<string[]>v).map(v => new Pattern(v));
return v;
});

if (!this.getTools().length) {
this.bindTool("selection_wand", config.wandItem);
this.bindTool("navigation_wand", config.navWandItem);
Expand Down Expand Up @@ -273,6 +281,7 @@ export class PlayerSession {

public createGradient(id: string, dither: number, patterns: Pattern[]) {
this.gradients.set(id, { dither, patterns });
this.gradients.save();
}

public getGradient(id: string) {
Expand All @@ -284,7 +293,6 @@ export class PlayerSession {
region.deref();
}
this.regions.clear();
Tools.deleteBindings(this.playerId);
this.history.delete();
this.history = null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/server/tools/base_tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ export abstract class Tool {
return;
}

toJSON() {
return { type: this.type };
toJSON(): { toolType: string } {
return { toolType: this.type };
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars
Expand Down
6 changes: 3 additions & 3 deletions src/server/tools/brush_tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ class BrushTool extends Tool {
if (this.brush.id === "structure_brush") return undefined;

return {
type: this.type,
toolType: this.type,
brush: this.brush,
mask: this.mask?.getSource() ?? null,
traceMask: this.traceMask?.getSource() ?? null
mask: this.mask,
traceMask: this.traceMask
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/tools/command_tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CommandTool extends Tool {

toJSON() {
return {
type: this.type,
toolType: this.type,
command: (this.isCustom ? ";" : "/") + this.command
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/server/tools/replacer_tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class BlockReplacerTool extends Tool {

toJSON() {
return {
type: this.type,
pattern: this.pattern.getSource()
toolType: this.type,
pattern: this.pattern
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/server/tools/stacker_tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ class StackerTool extends Tool {

toJSON() {
return {
type: this.type,
toolType: this.type,
range: this.range,
mask: this.mask.getSource()
mask: this.mask
};
}

Expand Down
Loading

0 comments on commit 51d2624

Please sign in to comment.