diff --git a/assets/opensb/binds/opensb.binds b/assets/opensb/binds/opensb.binds index 8a4fdc8..d06b6f4 100644 --- a/assets/opensb/binds/opensb.binds +++ b/assets/opensb/binds/opensb.binds @@ -4,7 +4,8 @@ "camera": { "name": "Camera" }, "voice": { "name": "Voice" }, "building": { "name": "Building" }, - "inventory": { "name": "Inventory" } + "inventory": { "name": "Inventory" }, + "editing" : { "name" : "Editing" } }, "name": "Open^#ebd74a;Starbound", "binds": { @@ -48,6 +49,18 @@ "default": [], "group": "building", "name": "Shrink Building Radius" + }, + "editingCopyItemJson": { + "default": [], // [{"type": "key", "value": "C","mods": ["LShift"]}], + "name": "Copy Item in Cursor", + "group": "editing", + "tags" : ["clipboard"] + }, + "editingPasteItemJson": { + "default": [], // [{"type": "key", "value": "V", "mods": ["LShift"]}], + "name": "Paste Item in Cursor", + "group": "editing", + "tags" : ["clipboard"] } } } 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