Skip to content

Commit 97908cc

Browse files
thePalindromeparamat
authored andcommittedDec 2, 2015
Add on_secondary_use when right clicking an item in the air
1 parent 57b4295 commit 97908cc

File tree

8 files changed

+71
-0
lines changed

8 files changed

+71
-0
lines changed
 

Diff for: ‎builtin/game/item.lua

+7
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ function core.item_place(itemstack, placer, pointed_thing, param2)
347347
return itemstack
348348
end
349349

350+
function core.item_secondary_use(itemstack, placer)
351+
return itemstack
352+
end
353+
350354
function core.item_drop(itemstack, dropper, pos)
351355
if dropper and dropper:is_player() then
352356
local v = dropper:get_look_dir()
@@ -605,6 +609,7 @@ core.craftitemdef_default = {
605609
-- Interaction callbacks
606610
on_place = redef_wrapper(core, 'item_place'), -- core.item_place
607611
on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
612+
on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
608613
on_use = nil,
609614
}
610615

@@ -622,6 +627,7 @@ core.tooldef_default = {
622627

623628
-- Interaction callbacks
624629
on_place = redef_wrapper(core, 'item_place'), -- core.item_place
630+
on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
625631
on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
626632
on_use = nil,
627633
}
@@ -640,6 +646,7 @@ core.noneitemdef_default = { -- This is used for the hand and unknown items
640646

641647
-- Interaction callbacks
642648
on_place = redef_wrapper(core, 'item_place'),
649+
on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
643650
on_drop = nil,
644651
on_use = nil,
645652
}

Diff for: ‎builtin/game/register.lua

+1
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ core.register_item(":unknown", {
272272
description = "Unknown Item",
273273
inventory_image = "unknown_item.png",
274274
on_place = core.item_place,
275+
on_secondary_use = core.item_secondary_use,
275276
on_drop = core.item_drop,
276277
groups = {not_in_creative_inventory=1},
277278
diggable = true,

Diff for: ‎doc/lua_api.txt

+5
Original file line numberDiff line numberDiff line change
@@ -3303,6 +3303,11 @@ Definition tables
33033303
--[[
33043304
^ Shall place item and return the leftover itemstack
33053305
^ default: minetest.item_place ]]
3306+
on_secondary_use = func(itemstack, user, pointed_thing),
3307+
--[[
3308+
^ Same as on_place but called when pointing at nothing.
3309+
^ pointed_thing : always { type = "nothing" }
3310+
]]
33063311
on_drop = func(itemstack, dropper, pos),
33073312
--[[
33083313
^ Shall drop item and return the leftover itemstack

Diff for: ‎src/client.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,7 @@ void Client::interact(u8 action, const PointedThing& pointed)
946946
2: digging completed
947947
3: place block or item (to abovesurface)
948948
4: use item
949+
5: perform secondary action of item
949950
*/
950951

951952
NetworkPacket pkt(TOSERVER_INTERACT, 1 + 2 + 0);

Diff for: ‎src/game.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,7 @@ class Game {
15251525
void processPlayerInteraction(std::vector<aabb3f> &highlight_boxes,
15261526
GameRunData *runData, f32 dtime, bool show_hud,
15271527
bool show_debug);
1528+
void handlePointingAtNothing(GameRunData *runData, const ItemStack &playerItem);
15281529
void handlePointingAtNode(GameRunData *runData,
15291530
const PointedThing &pointed, const ItemDefinition &playeritem_def,
15301531
const ToolCapabilities &playeritem_toolcap, f32 dtime);
@@ -3603,6 +3604,8 @@ void Game::processPlayerInteraction(std::vector<aabb3f> &highlight_boxes,
36033604
} else if (input->getLeftState()) {
36043605
// When button is held down in air, show continuous animation
36053606
runData->left_punch = true;
3607+
} else if (input->getRightClicked()) {
3608+
handlePointingAtNothing(runData, playeritem);
36063609
}
36073610

36083611
runData->pointed_old = pointed;
@@ -3618,6 +3621,15 @@ void Game::processPlayerInteraction(std::vector<aabb3f> &highlight_boxes,
36183621
}
36193622

36203623

3624+
void Game::handlePointingAtNothing(GameRunData *runData, const ItemStack &playerItem)
3625+
{
3626+
infostream << "Right Clicked in Air" << std::endl;
3627+
PointedThing fauxPointed;
3628+
fauxPointed.type = POINTEDTHING_NOTHING;
3629+
client->interact(5, fauxPointed);
3630+
}
3631+
3632+
36213633
void Game::handlePointingAtNode(GameRunData *runData,
36223634
const PointedThing &pointed, const ItemDefinition &playeritem_def,
36233635
const ToolCapabilities &playeritem_toolcap, f32 dtime)

Diff for: ‎src/network/serverpackethandler.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -1653,6 +1653,23 @@ void Server::handleCommand_Interact(NetworkPacket* pkt)
16531653
}
16541654

16551655
} // action == 4
1656+
1657+
/*
1658+
5: rightclick air
1659+
*/
1660+
else if (action == 5) {
1661+
ItemStack item = playersao->getWieldedItem();
1662+
1663+
actionstream << player->getName() << " activates "
1664+
<< item.name << std::endl;
1665+
1666+
if (m_script->item_OnSecondaryUse(
1667+
item, playersao)) {
1668+
if( playersao->setWieldedItem(item)) {
1669+
SendInventory(playersao);
1670+
}
1671+
}
1672+
}
16561673

16571674

16581675
/*

Diff for: ‎src/script/cpp_api/s_item.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,32 @@ bool ScriptApiItem::item_OnUse(ItemStack &item,
110110
return true;
111111
}
112112

113+
bool ScriptApiItem::item_OnSecondaryUse(ItemStack &item, ServerActiveObject *user)
114+
{
115+
SCRIPTAPI_PRECHECKHEADER
116+
117+
int error_handler = PUSH_ERROR_HANDLER(L);
118+
119+
if (!getItemCallback(item.name.c_str(), "on_secondary_use"))
120+
return false;
121+
122+
LuaItemStack::create(L, item);
123+
objectrefGetOrCreate(L, user);
124+
PointedThing pointed;
125+
pointed.type = POINTEDTHING_NOTHING;
126+
pushPointedThing(pointed);
127+
PCALL_RES(lua_pcall(L, 3, 1, error_handler));
128+
if (!lua_isnil(L, -1)) {
129+
try {
130+
item = read_item(L, -1, getServer());
131+
} catch (LuaError &e) {
132+
throw LuaError(std::string(e.what()) + ". item=" + item.name);
133+
}
134+
}
135+
lua_pop(L, 2); // Pop item and error handler
136+
return true;
137+
}
138+
113139
bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user,
114140
const InventoryList *old_craft_grid, const InventoryLocation &craft_inv)
115141
{

Diff for: ‎src/script/cpp_api/s_item.h

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class ScriptApiItem
4242
ServerActiveObject *placer, const PointedThing &pointed);
4343
bool item_OnUse(ItemStack &item,
4444
ServerActiveObject *user, const PointedThing &pointed);
45+
bool item_OnSecondaryUse(ItemStack &item,
46+
ServerActiveObject *user);
4547
bool item_OnCraft(ItemStack &item, ServerActiveObject *user,
4648
const InventoryList *old_craft_grid, const InventoryLocation &craft_inv);
4749
bool item_CraftPredict(ItemStack &item, ServerActiveObject *user,

0 commit comments

Comments
 (0)
Please sign in to comment.