Skip to content

Commit

Permalink
fix: Blocks added to inventory
Browse files Browse the repository at this point in the history
- Added new OpenAI settings
- Added server start greeting
- Fixed images printing backwards
  • Loading branch information
jasonjgardner committed Apr 24, 2023
1 parent 36a3c47 commit 93da73f
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 62 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ RP_MODULE_UUID=ad91326d-3455-4ea2-b12d-9e49591fa1f2
BP_PACK_UUID=eefdf10b-b20b-4580-8515-d6a2c35db762
BP_MODULE_UUID=a684c1d3-ad02-4ddb-9241-cfd132b79621
GITHUB_ACTION_REPOSITORY=jasonjgardner/minecraft-rtx-rainbow
OPENAI_API_KEY=
OPENAI_API_KEY=
DEBUG=false
102 changes: 84 additions & 18 deletions serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ const state: WssState = {
functionLog: join(Deno.cwd(), 'build', 'wss', 'functions')
};

/**
* Format the position for the command
* @param x - X coordinate
* @param y - Y coordinate
* @param z - Z coordinate
* @param offsetX - Offset X coordinate
* @param offsetY - Offset Y coordinate
* @param offsetZ - Offset Z coordinate
* @returns Absolute or relative position
*/
const formatPosition = (x: number, y: number, z: number, offsetX?: number, offsetY?: number, offsetZ?: number) => {
const { offset, useAbsolutePosition } = state;
let [ox, oy, oz] = offset || [0, 0, 0];
Expand All @@ -130,9 +140,8 @@ const formatPosition = (x: number, y: number, z: number, offsetX?: number, offse
return useAbsolutePosition ? `${nx} ${ny} ${nz}` : `~${nx} ~${ny} ~${nz}`;
};



let connectionUpdateInterval: number | undefined;
let keepAliveInterval: number | undefined;

async function watch(fnNameInput: string) {
const [fnName, _params] = fnNameInput.split("?", 2);
Expand Down Expand Up @@ -173,8 +182,6 @@ function logFunction(fnName: string, content: string) {

}



async function processMessage(
{ message, sender }: { message: string; sender: string },
) {
Expand Down Expand Up @@ -228,8 +235,28 @@ async function processMessage(
return;
}

if (contents.startsWith("help/")) {
queueCommandRequest(`tell ${sender} §c[§fWSS§c] §7Available commands:"`);
queueCommandRequest(`tell ${sender} §c[§fWSS§c] §7- §fhelp§7: Show this message"`);
queueCommandRequest(`tell ${sender} §c[§fWSS§c] §7- §fread§7: Read a function file"`);
queueCommandRequest(`tell ${sender} §c[§fWSS§c] §7- §flr§7: Live reload a function file"`);
queueCommandRequest(`tell ${sender} §c[§fWSS§c] §7- §fscript§7: Run a script"`);
queueCommandRequest(`tell ${sender} §c[§fWSS§c] §7- §flog§7: Log a function to a file"`);
queueCommandRequest(`tell ${sender} §c[§fWSS§c] §7- §fhistory§7: Enable/disable block history"`);
return;
}

// Check if command is a queued message sent from the server. Do nothing if it's detected in the queue.
if (requests.find((r) => r && r.content === contents)) {
return;
}

console.warn("Unknown: %s", contents);
}
/**
* Load special functions found in `./src/functions/`
* @param fnNameInput Script source
*/
async function loadFunctionScript(fnNameInput: string) {
const [scriptFile, params] = fnNameInput.split("?", 2);
try {
Expand All @@ -248,6 +275,10 @@ async function loadFunctionScript(fnNameInput: string) {
}
}

/**
* Parse a .mcfunction file and queue it for execution
* @param fnNameInput .mcfunction file name
*/
async function queueFunctionFile(fnNameInput: string) {
const [fnName, fnParams] = fnNameInput.split("?", 2);

Expand Down Expand Up @@ -275,17 +306,11 @@ async function queueFunctionFile(fnNameInput: string) {
lines.map((c) => c && queueCommandRequest(c));
}

async function updateContent(imgUrl: string) {
if (imgUrl.length < 4) {
return;
}

state.updatePending = true;

console.info("Queued %d commands", commands.length);
state.updatePending = false;
}

/**
* Subscribe to events from the server
* @param socket WebSocket connection to the server
* @param events List of events to subscribe to
*/
function subscribe(socket: WebSocket, events: Array<string | SubscribeEvents>) {
events.forEach((event) => {
socket.send(JSON.stringify({
Expand All @@ -302,7 +327,12 @@ function subscribe(socket: WebSocket, events: Array<string | SubscribeEvents>) {
});
}

function queueCommandRequest(commandLine: string) {
/**
* Queue a command to be sent to the server
* @param commandLine - Minecraft command to execute
* @param sendRate - How many commands to send per ms
*/
function queueCommandRequest(commandLine: string, sendRate = 1) {
const uuid = crypto.randomUUID();
const content = JSON.stringify({
header: {
Expand Down Expand Up @@ -330,11 +360,15 @@ function queueCommandRequest(commandLine: string) {
//sessionStorage.setItem(`request[${uuid}]`, content);

// Speed up rend rate based on number of requests
state.sendRate = requests.length > 100 ? 3 : 1;
state.sendRate = requests.length > 500 ? Math.max(3, sendRate) : Math.max(1, sendRate);
}

/**
* Function called upon WebSocket connection open
* @param socket WebSocket connection
*/
async function onOpenHandler(socket: WebSocket) {
console.log("ws:open");
console.info("ws:open");

subscribe(socket, ["PlayerMessage", "commandResponse"]);

Expand All @@ -358,8 +392,34 @@ async function onOpenHandler(socket: WebSocket) {
console.info("Queue cleared");
}
}, state.sendRate);

// Send a keep alive every 10 seconds
keepAliveInterval = setInterval(() => {
if (requests.length > 0) {
return;
}

socket.send(JSON.stringify({
header: {
version: 1,
requestId: crypto.randomUUID(),
messageType: "commandRequest",
messagePurpose: "keepAlive",
},
body: {},
}));
}, 10000);

// Send a greeting on connect
setTimeout(() => {
console.log('%cR%cA%cI%cN%cB%cO%cW server started!', 'color:red;', 'color:orange;', 'color:yellow;', 'color:green;', 'color:blue;', 'color:indigo;', 'color:violet;');
queueCommandRequest('tellraw @a {"rawtext":[{"text":"§c§o§lR"},{"text":"§6§o§lA"},{"text":"§e§o§lI"},{"text":"§a§o§lN"},{"text":"§b§o§lB"},{"text":"§9§o§lO"},{"text":"§1§o§lW"},{"text":"§r sever connected!"}]}');
}, 1000);
}

/**
* Reset blocks in block history
*/
function resetBlocks() {
// queue command to clear a block from state.blockHistory every 10 seconds

Expand All @@ -376,6 +436,11 @@ function resetBlocks() {
q(10);
}

/**
* Remove request from queue. If block history is enabled, add block to history
* @param msg - Message from server
* @returns void
*/
function processCommandResponse(msg: { body: any, header: any }) {
if (!requests) {
return;
Expand Down Expand Up @@ -446,6 +511,7 @@ export function requestHandler(req: Request) {

socket.onclose = () => {
clearInterval(connectionUpdateInterval);
clearInterval(keepAliveInterval);
connectionUpdateInterval = undefined;
console.info("ws:close");
};
Expand Down
25 changes: 13 additions & 12 deletions src/components/BlockEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,27 @@ export default class BlockEntry {
}

toString(prevBlock: BlockEntry, nextBlock: BlockEntry) {
// let group = "itemGroup.name.concrete";

// if (this._material.label?.startsWith("glass") === true) {
// group = "itemGroup.name.glass";
// } else if (this._material.label?.startsWith("metal") === true) {
// group = "itemGroup.name.copper";
// } else if (this._material.label === "emissive") {
// group = "itemGroup.name.stainedClay";
// }
let group = "itemGroup.name.concrete";

if (this._material.label?.startsWith("glass") === true) {
group = "itemGroup.name.glass";
} else if (this._material.label?.startsWith("metal") === true) {
group = "itemGroup.name.copper";
} else if (this._material.label === "emissive") {
group = "itemGroup.name.stainedClay";
}

return JSON.stringify(
{
format_version: BEHAVIOR_BLOCK_FORMAT_VERSION,
"minecraft:block": {
description: {
identifier: this.behaviorId,
is_experimental: false,
register_to_creative_menu: true,
properties: this.properties(),
category: "construction",
menu_category: {
category: "construction",
group,
},
},
components: this.behaviors(),
events: this.events(prevBlock, nextBlock),
Expand Down
4 changes: 2 additions & 2 deletions src/components/ImagePrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export function convertImage(

func.push(
writeFill(
x + offset[0],
Math.abs((x + offset[0]) - img.width), // Starts print column at left
Math.abs((y + offset[1]) - img.height), // Starts print row at top
offset[2],
nearest,
Expand Down Expand Up @@ -253,7 +253,7 @@ async function printDecoded(

func.push(
writeFill(
x + offset[0],
Math.abs((x + offset[0]) - img.width), // Starts print column at left
Math.abs((y + offset[1]) - img.height), // Starts print row at top
offset[2],
nearest,
Expand Down
Loading

0 comments on commit 93da73f

Please sign in to comment.