init; changed chat command and minor things
This commit is contained in:
commit
8175b529b3
12 changed files with 1084 additions and 0 deletions
1
src/.gitignore
vendored
Normal file
1
src/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
prompt.txt
|
||||
62
src/command.lua
Normal file
62
src/command.lua
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
-- In your mod's global scope, register the detached inventories table
|
||||
inventory_admin.detached_inventories = {}
|
||||
|
||||
function handle_player_inventory(admin_name, target_player_name)
|
||||
inventory_admin.setup_detached_inventory(target_player_name)
|
||||
inventory_admin.sync_player_to_detached_inventory(target_player_name)
|
||||
minetest.show_formspec(admin_name, "inventory_admin:player_inventory",
|
||||
inventory_admin.get_player_inventory_formspec(target_player_name, admin_name))
|
||||
return true, "Showing inventory of " .. target_player_name
|
||||
end
|
||||
|
||||
function handle_enderchest_inventory(admin_name, target_player_name)
|
||||
if inventory_admin.utils.is_mineclone2() then
|
||||
minetest.show_formspec(admin_name, "inventory_admin:enderchest_inventory",
|
||||
inventory_admin.get_enderchest_inventory_formspec(target_player_name))
|
||||
return true, "Showing enderchest inventory of " .. target_player_name
|
||||
else
|
||||
return false, "Enderchest inventory is not available in this game."
|
||||
end
|
||||
end
|
||||
|
||||
-- Modify the command_inventory function to handle the new parameters
|
||||
function inventory_admin.command_inventory(name, param)
|
||||
local player = minetest.get_player_by_name(name)
|
||||
if not player then
|
||||
return false, "You need to be online to use this command."
|
||||
end
|
||||
local args = param:split(" ")
|
||||
|
||||
if inventory_admin.utils.is_mineclone2() then
|
||||
-- Handle command logic for MineClone2
|
||||
if #args < 2 then
|
||||
return false, "Usage: /inv <type> <player_name> in MineClone2"
|
||||
end
|
||||
|
||||
local type, target_player_name = unpack(args)
|
||||
|
||||
if type ~= "player" and type ~= "ender" then
|
||||
return false, "Invalid type. Use 'player' or 'ender'."
|
||||
end
|
||||
|
||||
local target_player = minetest.get_player_by_name(target_player_name)
|
||||
if not target_player then
|
||||
return false, "Target player not found."
|
||||
end
|
||||
|
||||
if type == "player" then
|
||||
-- Handle player inventory
|
||||
return handle_player_inventory(name, target_player_name)
|
||||
elseif type == "ender" then
|
||||
-- Handle enderchest inventory
|
||||
return handle_enderchest_inventory(name, target_player_name)
|
||||
end
|
||||
else
|
||||
-- Handle command logic for non-MineClone2 (e.g., minetest_game)
|
||||
if param:trim() == "" or #args > 1 then
|
||||
return false, "Usage: /inv <player_name>"
|
||||
end
|
||||
|
||||
return handle_player_inventory(name, param:trim())
|
||||
end
|
||||
end
|
||||
95
src/formspecs.lua
Normal file
95
src/formspecs.lua
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
local S = minetest.get_translator("mcl_chests")
|
||||
local F = minetest.formspec_escape
|
||||
local C = minetest.colorize
|
||||
|
||||
|
||||
-- Get the formspec for the inventory based on the game
|
||||
function inventory_admin.get_player_inventory_formspec(target_player_name, admin_name)
|
||||
if inventory_admin.utils.is_mineclone2() then
|
||||
-- MineClone2 formspec
|
||||
local formspec = {
|
||||
"formspec_version[4]",
|
||||
"size[11.75,13]", -- Adjust the height to accommodate the spacing
|
||||
|
||||
-- Title for the target player's inventory
|
||||
"label[0.375,0.375;", minetest.formspec_escape(target_player_name .. "'s Inventory"), "]",
|
||||
|
||||
-- Slot backgrounds for the target player's main inventory excluding the hotbar
|
||||
mcl_formspec.get_itemslot_bg_v4(0.375, 1, 9, 3),
|
||||
|
||||
-- Slot list for the target player's main inventory excluding the hotbar
|
||||
"list[detached:" .. target_player_name .. "_inventory;main;0.375,1;9,3;9]",
|
||||
|
||||
-- Slot background for the target player's hotbar, placed at the bottom
|
||||
mcl_formspec.get_itemslot_bg_v4(0.375, 5, 9, 1),
|
||||
|
||||
-- Slot list for the target player's hotbar
|
||||
"list[detached:" .. target_player_name .. "_inventory;main;0.375,5;9,1;0]",
|
||||
|
||||
-- Title for the admin's inventory, moved further down to create space
|
||||
"label[0.375,6.5;Your Inventory]",
|
||||
|
||||
-- Slot backgrounds for the admin player's main inventory excluding the hotbar
|
||||
mcl_formspec.get_itemslot_bg_v4(0.375, 7, 9, 3),
|
||||
|
||||
-- Slot list for the admin player's main inventory excluding the hotbar
|
||||
"list[current_player;main;0.375,7;9,3;9]",
|
||||
|
||||
-- Slot background for the admin player's hotbar, placed further down with spacing similar to the singleplayer's hotbar
|
||||
mcl_formspec.get_itemslot_bg_v4(0.375, 11, 9, 1),
|
||||
|
||||
-- Slot list for the admin player's hotbar, with adjusted Y-coordinate for correct spacing
|
||||
"list[current_player;main;0.375,11;9,1;0]",
|
||||
|
||||
-- Listrings to allow moving items between the target's and admin's inventories
|
||||
"listring[detached:" .. target_player_name .. "_inventory;main]",
|
||||
"listring[current_player;main]",
|
||||
}
|
||||
|
||||
return table.concat(formspec)
|
||||
else
|
||||
-- minetest_game formspec
|
||||
local formspec = {
|
||||
"size[9,11]", -- Width of 8 slots, and enough height to accommodate all slots and labels
|
||||
|
||||
-- Title for the target player's inventory
|
||||
"label[0.5,0;", minetest.formspec_escape(target_player_name .. "s Inventar"), "]",
|
||||
|
||||
-- Singleplayer's complete inventory, including the hotbar in one block
|
||||
"list[detached:" .. target_player_name .. "_inventory;main;0,0.5;9,4;]", -- hotbar, 9 slots per row, 1 rows
|
||||
|
||||
-- Title for the admin's inventory
|
||||
"label[0.5,5.5;Dein Inventar]",
|
||||
|
||||
-- Admin's hotbar visually separated
|
||||
"list[current_player;main;0,6;9,1;0]", -- The hotbar with 9 slots
|
||||
|
||||
-- Admin's main inventory excluding the hotbar
|
||||
"list[current_player;main;0,7;9,3;9]", -- 3 rows of 9 slots each, starting after the hotbar
|
||||
|
||||
-- Listrings for item movement between the inventories
|
||||
"listring[detached:" .. target_player_name .. "_inventory;main]",
|
||||
"listring[current_player;main]",
|
||||
}
|
||||
|
||||
return table.concat(formspec)
|
||||
end
|
||||
end
|
||||
|
||||
function inventory_admin.get_enderchest_inventory_formspec(target_player_name)
|
||||
local formspec_ender_chest = {
|
||||
"formspec_version[4]",
|
||||
"size[11.75,10.425]",
|
||||
"label[0.375,0.375;", F(C(mcl_formspec.label_color, target_player_name.."'s "..S("Ender Chest"))) .. "]",
|
||||
mcl_formspec.get_itemslot_bg_v4(0.375, 0.75, 9, 3),
|
||||
"list[player:" .. target_player_name .. ";enderchest;0.375,0.75;9,3;]", -- Access the target player's enderchest
|
||||
"label[0.375,4.7;", F(C(mcl_formspec.label_color, S("Inventar"))) .. "]",
|
||||
mcl_formspec.get_itemslot_bg_v4(0.375, 5.1, 9, 3),
|
||||
"list[current_player;main;0.375,5.1;9,3;9]",
|
||||
mcl_formspec.get_itemslot_bg_v4(0.375, 9.05, 9, 1),
|
||||
"list[current_player;main;0.375,9.05;9,1;]",
|
||||
"listring[player:" .. target_player_name .. ";enderchest]",
|
||||
"listring[current_player;main]",
|
||||
}
|
||||
return table.concat(formspec_ender_chest)
|
||||
end
|
||||
87
src/sync.lua
Normal file
87
src/sync.lua
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
function inventory_admin.setup_detached_inventory(target_player_name)
|
||||
-- Create a detached inventory if it does not exist
|
||||
if not inventory_admin.detached_inventories[target_player_name] then
|
||||
inventory_admin.detached_inventories[target_player_name] = minetest.create_detached_inventory(target_player_name .. "_inventory", {
|
||||
-- Define the callback functions for inventory actions
|
||||
on_put = function(inv, listname, index, stack, player)
|
||||
-- Sync the changes from the detached inventory to the player's inventory when items are put
|
||||
inventory_admin.sync_inventory_to_player(target_player_name, listname, index, stack)
|
||||
end,
|
||||
on_take = function(inv, listname, index, stack, player)
|
||||
-- Sync the changes from the detached inventory to the player's inventory when items are taken
|
||||
inventory_admin.sync_inventory_to_player(target_player_name, listname, index, nil)
|
||||
end,
|
||||
on_move = function(inv, from_list, from_index, to_list, to_index, count, player)
|
||||
-- Sync the entire inventory when items are moved within the detached inventory
|
||||
inventory_admin.sync_inventory_to_player(target_player_name)
|
||||
end,
|
||||
})
|
||||
|
||||
-- Set the size of the inventory (e.g., main and hotbar are typically 9 slots each)
|
||||
if inventory_admin.utils.is_mineclone2() then
|
||||
inventory_admin.detached_inventories[target_player_name]:set_size("main", 36) -- Adjust size accordingly
|
||||
inventory_admin.detached_inventories[target_player_name]:set_size("hotbar", 9) -- Adjust size accordingly
|
||||
else
|
||||
inventory_admin.detached_inventories[target_player_name]:set_size("main", 36) -- Adjust size accordingly
|
||||
inventory_admin.detached_inventories[target_player_name]:set_size("hotbar", 9) -- Adjust size accordingly
|
||||
end
|
||||
end
|
||||
|
||||
-- Fill the detached inventory with the player's inventory items
|
||||
inventory_admin.sync_player_to_detached_inventory(target_player_name)
|
||||
end
|
||||
|
||||
function inventory_admin.sync_player_to_detached_inventory(target_player_name)
|
||||
local player = minetest.get_player_by_name(target_player_name)
|
||||
if not player then
|
||||
minetest.log("error", "Player not found: " .. target_player_name)
|
||||
return
|
||||
end
|
||||
|
||||
local player_inv = player:get_inventory()
|
||||
local detached_inv = inventory_admin.detached_inventories[target_player_name]
|
||||
|
||||
-- Check if the detached inventory has been set up
|
||||
if not detached_inv then
|
||||
minetest.log("error", "Detached inventory not found for player: " .. target_player_name)
|
||||
return
|
||||
end
|
||||
|
||||
-- Copy the player's inventory into the detached inventory, including the hotbar
|
||||
for i = 1, player_inv:get_size("main") do
|
||||
detached_inv:set_stack("main", i, player_inv:get_stack("main", i))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
function inventory_admin.sync_inventory_to_player(target_player_name, listname, index, stack)
|
||||
local player = minetest.get_player_by_name(target_player_name)
|
||||
if not player then
|
||||
minetest.log("error", "Player not found: " .. target_player_name)
|
||||
return
|
||||
end
|
||||
|
||||
local player_inv = player:get_inventory()
|
||||
local detached_inv = inventory_admin.detached_inventories[target_player_name]
|
||||
|
||||
-- If specific listname and index are provided, only sync that particular slot
|
||||
if listname and index then
|
||||
if stack then
|
||||
-- The stack is provided, so we update the slot with the new stack
|
||||
player_inv:set_stack(listname, index, stack)
|
||||
else
|
||||
-- If stack is nil, it means an item was taken out, so we set the slot to be empty
|
||||
player_inv:set_stack(listname, index, ItemStack(nil))
|
||||
end
|
||||
else
|
||||
-- Sync the entire inventory, which includes the hotbar since it's part of 'main'
|
||||
for i = 1, detached_inv:get_size("main") do
|
||||
player_inv:set_stack("main", i, detached_inv:get_stack("main", i))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
5
src/utils.lua
Normal file
5
src/utils.lua
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
inventory_admin.utils = {}
|
||||
|
||||
function inventory_admin.utils.is_mineclone2()
|
||||
return minetest.get_modpath("mcl_core") ~= nil
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue