add item copy-paste binds

decided against making them bound by default
This commit is contained in:
Kae 2024-12-10 18:50:03 +11:00
parent d95eac3164
commit ad508c5322
4 changed files with 101 additions and 6 deletions

View File

@ -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"]
}
}
}

View File

@ -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
@ -36,3 +35,5 @@ command("run", function(src)
end
end
end)
module.register = register

View File

@ -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)

View File

@ -1,2 +1,2 @@
require "/scripts/opensb/util/modules.lua"
modules("/scripts/opensb/player/", {"commands"})
modules("/scripts/opensb/player/", {"commands", "copy_paste"})