Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fill Command #253

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ tigerw (Tiger Wang)
tonibm19
tonitch (Debucquoy Anthony)
xoft (Mattes Dolak/madmaxoft)
rmilooo

14 changes: 14 additions & 0 deletions Info.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ g_PluginInfo =
},
},
},

["/fill"] =
{
Permission = "core.fill",
Handler = HandleFillCommand,
HelpString = "Fills a specified area with a given block type.",
ParameterCombinations =
{
{
Params = "<x1> <y1> <z1> <x2> <y2> <z2> <blockID>",
Help = "Fills the area between the coordinates (x1, y1, z1) and (x2, y2, z2) with the specified block type. Coordinates can use relative notation (e.g., ~1, ~-2).",
},
},
},

["/give"] =
{
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Implements some of the basic commands needed to run a simple server.
|/do | core.do | Runs a command as a player.|
|/effect | core.effect | Adds an effect to a player.|
|/enchant | core.enchant | Adds an enchantment to a specified player's held item.|
|/fill | core.fill | Fills a defined region with the specified block type.|
|/gamemode | core.changegm | Changes a player's gamemode.|
|/give | core.give | Gives an item to a player.|
|/help | core.help | Shows available commands.|
Expand Down Expand Up @@ -80,6 +81,7 @@ Implements some of the basic commands needed to run a simple server.
| core.effect | | `/effect` | |
| core.enchant | Allows players to add an enchantment to a player's held item. | `/enchant` | admins |
| core.enchant.self | Allows players to add an enchantment to their own held item. | `/ienchant` | admins |
| core.fill | Allows players to fill an area with a specified block type. | `/fill` | admins |
| core.give | Allows players to give items to other players. | `/give` | admins |
| core.give.unsafe | Allows players to give items to other players, even if the item is blacklisted. | `/unsafegive` | none |
| core.help | | `/help` | |
Expand Down
74 changes: 74 additions & 0 deletions cmd_fill.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
function HandleFillCommand(Split, Player)
local Response

-- Ensure proper arguments: /fill <x1> <y1> <z1> <x2> <y2> <z2> <blockID>
if (#Split ~= 8) then
Player:SendMessage("Usage: /fill <x1> <y1> <z1> <x2> <y2> <z2> <blockID>")
return true
end

-- Parse coordinates with support for relative notation
local pos = Player:GetPosition() -- Get the player's current position
local x1 = ParseCoordinate(Split[2], pos.x)
local y1 = ParseCoordinate(Split[3], pos.y)
local z1 = ParseCoordinate(Split[4], pos.z)
local x2 = ParseCoordinate(Split[5], pos.x)
local y2 = ParseCoordinate(Split[6], pos.y)
local z2 = ParseCoordinate(Split[7], pos.z)
local blockTypeName = Split[8]:lower()

-- Validate coordinates
if not (x1 and y1 and z1 and x2 and y2 and z2) then
Player:SendMessageFailure("Invalid coordinates! Use numbers or valid relative syntax (~).")
return true
end

-- Verify block ID using BlockStringToType
local blockID = BlockStringToType(blockTypeName)
if blockID == -1 then
Player:SendMessageFailure("Invalid block ID: " .. blockTypeName)
return true
end

-- Sort coordinates to ensure correct boundaries
if x1 > x2 then x1, x2 = x2, x1 end
if y1 > y2 then y1, y2 = y2, y1 end
if z1 > z2 then z1, z2 = z2, z1 end

-- Get the player's world
local world = Player:GetWorld()

-- Compute chunk range
local chunkX1, chunkZ1 = math.floor(x1 / 16), math.floor(z1 / 16)
local chunkX2, chunkZ2 = math.floor(x2 / 16), math.floor(z2 / 16)

local chunks = {}
for cx = chunkX1, chunkX2 do
for cz = chunkZ1, chunkZ2 do
table.insert(chunks, {cx, cz})
end
end

-- Load necessary chunks and perform fill operation
world:ChunkStay(chunks, nil, function()
local i = 0
for x = x1, x2 do
for y = y1, y2 do
for z = z1, z2 do
local pos = Vector3i(x, y, z)
world:SetBlock(pos, blockID, 0)
i = i + 1
end
end
end

Player:SendMessageSuccess("Filled " .. i .. " blocks with " .. blockTypeName .. ".")
LOGINFO("[" .. Player:GetName() .. "]: " .. StripColorCodes("filled " .. i .. " blocks with " .. blockTypeName .. "."))
end)

return true, Response
end

function HandleConsoleFill(Split)
return HandleFillCommand(Split)
end
11 changes: 11 additions & 0 deletions core_functions.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
-- Helper function to parse a coordinate with support for relative notation
function ParseCoordinate(coord, relativeTo)
if coord:sub(1, 1) == "~" then
local offset = tonumber(coord:sub(2)) or 0
return math.floor(relativeTo + offset)
else
local num = tonumber(coord)
return num or 0 -- Return 0 if the coordinate is invalid
end
end

-- Returns the online mode UUID for a player name, if it exists
-- Otherwise returns an offline UUID
function GetPlayerUUID(PlayerName)
Expand Down