Skip to content

Commit

Permalink
feat(diagnostics): Add a reload command and support command keybinds (#…
Browse files Browse the repository at this point in the history
…245)

## Summary
- Adds a vscode command to reload in the command pallete `>Minecraft
Diagnostics: Reload MC`
- Added a keyboard shortcut for this `ctrl+shift+r` this can be edited
by the user in keyboard shortcuts
- Added a vscode command which allows users to add custom keyboard
shortcuts to run commands from `keybindings.json`
- Example `{ "key": "ctrl+shift+t", "command":
"minecraft-debugger.runMinecraftCommand", "args": "say hello" },`
- Added an error message if you have an active debug session but try and
run an empty command from within the home view

## To test:
- Launch a debug session of the extension
**Reload**
- Try and run the reload command without a session active an observe the
warning message
- Start a debugging session
- Run the reload command from the command pallete, confirm it reloads in
game
- Run the reload command from hot keys `ctrl+shift+r`, confirm it
reloads in game
**Command Keybinds**
- Open the `keybindings.json` (stored in
Roaming/Code/User/keybindings.json) or command pallete `>Preferences:
Open Keyboard Shortcuts (JSON)`
- Add a keybinding such as:
- ```[{ "key": "ctrl+shift+t", "command":
"minecraft-debugger.runMinecraftCommand", "args": "say hello" }]```
- Save and use the keybind
- Confirm Scripting says hello
  • Loading branch information
ThomasOrs authored Oct 24, 2024
1 parent af23871 commit d860411
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@
{
"command": "minecraft-debugger.showMinecraftDiagnostics",
"title": "Minecraft Diagnostics: Show"
},
{
"command": "minecraft-debugger.minecraftReload",
"title": "Minecraft Reload"
}
],
"keybindings":[
{
"command": "minecraft-debugger.minecraftReload",
"key": "ctrl+shift+r",
"when": "inDebugMode && editorLangId == typescript || inDebugMode && editorLangId == javascript"
}
],
"breakpoints": [
Expand Down
29 changes: 28 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,35 @@ export function activate(context: vscode.ExtensionContext) {
}
);

const minecraftReloadCommand = vscode.commands.registerCommand('minecraft-debugger.minecraftReload', () => {
if (!vscode.debug.activeDebugSession) {
vscode.window.showErrorMessage('Error running command reload: No active Minecraft Debugger session.');
}
eventEmitter.emit('run-minecraft-command', 'reload');
});

// Create a command to allow keyboard shortcuts to run Minecraft commands
const runMinecraftCommand = vscode.commands.registerCommand(
'minecraft-debugger.runMinecraftCommand',
(...args: any[]) => {
if (args.length === 0) {
vscode.window.showErrorMessage('No command provided.');
return;
}
const command = args[0]; // Use only the first argument
if (typeof command !== 'string') {
vscode.window.showErrorMessage('Command must be a string.');
return;
}
if (!vscode.debug.activeDebugSession) {
vscode.window.showErrorMessage('Error running command: No active Minecraft Debugger session.');
}
eventEmitter.emit('run-minecraft-command', command);
}
);

// Add command to the extension context
context.subscriptions.push(showDiagnosticsCommand);
context.subscriptions.push(showDiagnosticsCommand, minecraftReloadCommand, runMinecraftCommand);
}

// called when extension is deactivated
Expand Down
4 changes: 4 additions & 0 deletions src/panels/HomeViewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export class HomeViewProvider implements vscode.WebviewViewProvider {
break;
}
case 'run-minecraft-command': {
if (!message.command || message.command.trim() === '') {
vscode.window.showErrorMessage('Minecraft Command Shortcut can not be empty.');
return;
}
this._eventEmitter.emit('run-minecraft-command', message.command);
break;
}
Expand Down

0 comments on commit d860411

Please sign in to comment.