Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/assets/opensb/scripts
diff options
context:
space:
mode:
authorKae <80987908+Novaenia@users.noreply.github.com>2024-12-10 18:50:03 +1100
committerKae <80987908+Novaenia@users.noreply.github.com>2024-12-10 18:50:03 +1100
commitad508c5322382a1ff36b18ab00bab5790ffd74ed (patch)
tree1f1d7531ab098a2440e67b66eb8b5431639e5ae5 /assets/opensb/scripts
parentd95eac316405fb100963d47dcd95ccced3462383 (diff)
add item copy-paste binds
decided against making them bound by default
Diffstat (limited to 'assets/opensb/scripts')
-rw-r--r--assets/opensb/scripts/opensb/player/commands.lua9
-rw-r--r--assets/opensb/scripts/opensb/player/copy_paste.lua81
-rw-r--r--assets/opensb/scripts/opensb/player/player.lua2
3 files changed, 87 insertions, 5 deletions
diff --git a/assets/opensb/scripts/opensb/player/commands.lua b/assets/opensb/scripts/opensb/player/commands.lua
index 2b42f48..9696e0d 100644
--- a/assets/opensb/scripts/opensb/player/commands.lua
+++ b/assets/opensb/scripts/opensb/player/commands.lua
@@ -2,7 +2,7 @@ local module = {}
modules.commands = module
local commands = {}
-local function command(name, func)
+local function register(name, func)
commands[name] = func
end
@@ -12,8 +12,7 @@ function module.init()
end
end
-
-command("run", function(src)
+register("run", function(src)
local success, result = pcall(loadstring, src, "/run")
if not success then
return "^#f00;compile error: " .. result
@@ -35,4 +34,6 @@ command("run", function(src)
end
end
end
-end) \ No newline at end of file
+end)
+
+module.register = register \ No newline at end of file
diff --git a/assets/opensb/scripts/opensb/player/copy_paste.lua b/assets/opensb/scripts/opensb/player/copy_paste.lua
new file mode 100644
index 0000000..2a15af4
--- /dev/null
+++ b/assets/opensb/scripts/opensb/player/copy_paste.lua
@@ -0,0 +1,81 @@
+local module = {}
+modules.copy_paste = module
+
+local commands = modules.commands
+local function getItemName(item)
+ return item.parameters.shortdescription
+ or root.itemConfig(item.name).config.shortdescription
+ or item.name
+end
+
+local function popupError(prefix, msg)
+ sb.logError("%s: %s", prefix, msg)
+ msg = #msg > 80 and msg:sub(1, 80) .. "..." or msg
+ local findNewLine = msg:find("\n", 1, true)
+ interface.queueMessage("^#f00;error:^reset; " .. (findNewLine and msg:sub(1, findNewLine - 1) or msg), 7)
+end
+
+local function getClipboardText()
+ local text = clipboard.hasText() and clipboard.getText()
+ return text and text:sub(1, 1) == "{" and text or nil
+end
+
+local function copyItem()
+ local item = player.swapSlotItem() or player.primaryHandItem() or player.altHandItem()
+ if not item then return end
+
+ clipboard.setText(sb.printJson(item, 2))
+ local message = string.format("Copied ^cyan;%s^reset; to clipboard", getItemName(item))
+ interface.queueMessage(message, 4, 0.5)
+end
+
+local function pasteItem()
+ if player.swapSlotItem() then return end
+ local data = getClipboardText()
+ if not data then return end
+
+ local success, result = pcall(sb.parseJson, data)
+ if not success then
+ popupError("Error parsing clipboard item", result)
+ else
+ local success, err = pcall(player.setSwapSlotItem, result)
+ if not success then popupError("Error loading clipboard item", err)
+ else
+ local message = string.format("Pasted ^cyan;%s^reset; from clipboard", getItemName(result))
+ interface.queueMessage(message, 4, 0.5)
+ end
+ end
+end
+
+function module.update()
+ if input.bindDown("opensb", "editingCopyItemJson") then
+ copyItem()
+ end
+
+ if input.bindDown("opensb", "editingPasteItemJson") then
+ pasteItem()
+ end
+end
+
+commands.register("exportplayer", function()
+ if not clipboard.available() then
+ return "Clipboard unavailable"
+ end
+ local success, text = pcall(sb.printJson, player.save(), 2)
+ if not success then return text end
+ clipboard.setText(text)
+ return "Exported player to clipboard"
+end)
+
+commands.register("importplayer", function()
+ local data = getClipboardText()
+ if not data then return "Clipboard does not contain JSON" end
+
+ local success, result = pcall(sb.parseJson, data)
+ if not success then
+ return "Error parsing player: " .. result
+ else
+ success, result = pcall(player.load, result)
+ return success and "Loaded player from clipboard" or "Error loading player: " .. result
+ end
+end) \ No newline at end of file
diff --git a/assets/opensb/scripts/opensb/player/player.lua b/assets/opensb/scripts/opensb/player/player.lua
index 05cbfb9..7670595 100644
--- a/assets/opensb/scripts/opensb/player/player.lua
+++ b/assets/opensb/scripts/opensb/player/player.lua
@@ -1,2 +1,2 @@
require "/scripts/opensb/util/modules.lua"
-modules("/scripts/opensb/player/", {"commands"}) \ No newline at end of file
+modules("/scripts/opensb/player/", {"commands", "copy_paste"}) \ No newline at end of file