Skip to content

Commit 1981a3c

Browse files
authored
Fixed bug preventing interaction with no item in-hand (#164)
* Fixed bug preventing interaction with no item in-hand * Cleaned up ItemInstance null handling * Fixed Player::isUsingItem() logic --------- Co-authored-by: Brent Da Mage <BrentDaMage@users.noreply.github.com>
1 parent 670f4c7 commit 1981a3c

File tree

12 files changed

+87
-80
lines changed

12 files changed

+87
-80
lines changed

source/client/app/Minecraft.cpp

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ void Minecraft::handleBuildAction(const BuildActionIntention& action)
270270
{
271271
LocalPlayer* player = m_pLocalPlayer;
272272
bool canInteract = getTimeMs() - m_lastInteractTime >= 200;
273-
if (player->isUsingItem()) return;
273+
// This logic is present in 0.9.0, but just does not make any sense being here.
274+
//if (player->isUsingItem()) return;
274275

275276
if (action.isDestroyStart() || action.isAttack())
276277
{
@@ -350,13 +351,7 @@ void Minecraft::handleBuildAction(const BuildActionIntention& action)
350351
else if (action.isPlace() && canInteract)
351352
{
352353
ItemInstance* pItem = getSelectedItem();
353-
if (pItem &&
354-
m_pGameMode->useItemOn(
355-
player,
356-
m_pLevel,
357-
pItem->m_itemID <= 0 ? nullptr : pItem,
358-
m_hitResult.m_tilePos,
359-
m_hitResult.m_hitSide))
354+
if (m_pGameMode->useItemOn(player, m_pLevel, pItem, m_hitResult.m_tilePos, m_hitResult.m_hitSide))
360355
{
361356
bInteract = false;
362357

@@ -366,7 +361,7 @@ void Minecraft::handleBuildAction(const BuildActionIntention& action)
366361

367362
if (isOnline())
368363
{
369-
if (pItem->m_itemID > C_MAX_TILES || pItem->m_itemID < 0)
364+
if (ItemInstance::isNull(pItem) || pItem->m_itemID > C_MAX_TILES)
370365
return;
371366

372367
TilePos tp(m_hitResult.m_tilePos);
@@ -480,7 +475,7 @@ void Minecraft::tickInput()
480475
else if (getOptions()->isKey(KM_DROP, keyCode))
481476
{
482477
ItemInstance *item = m_pLocalPlayer->m_pInventory->getSelected();
483-
if (item != nullptr)
478+
if (!ItemInstance::isNull(item))
484479
{
485480
ItemInstance itemDrop = m_pLocalPlayer->isSurvival() ? item->remove(1) : ItemInstance(*item);
486481
itemDrop.m_count = 1;
@@ -1181,22 +1176,19 @@ ItemInstance* Minecraft::getSelectedItem()
11811176
{
11821177
ItemInstance* pInst = m_pLocalPlayer->getSelectedItem();
11831178

1184-
if (!pInst)
1179+
if (ItemInstance::isNull(pInst))
11851180
return nullptr;
11861181

1187-
if (m_pGameMode->isSurvivalType())
1188-
return pInst;
1189-
1190-
// Create new "unlimited" ItemInstance for Creative mode
1191-
1192-
if (pInst->isNull())
1193-
return nullptr;
1194-
1195-
m_CurrItemInstance.m_itemID = pInst->m_itemID;
1196-
m_CurrItemInstance.m_count = 999;
1197-
m_CurrItemInstance.setAuxValue(pInst->getAuxValue());
1182+
if (m_pGameMode->isCreativeType())
1183+
{
1184+
// Create new "unlimited" ItemInstance for Creative mode
1185+
m_CurrItemInstance.m_itemID = pInst->m_itemID;
1186+
m_CurrItemInstance.m_count = 999;
1187+
m_CurrItemInstance.setAuxValue(pInst->getAuxValue());
1188+
return &m_CurrItemInstance;
1189+
}
11981190

1199-
return &m_CurrItemInstance;
1191+
return pInst;
12001192
}
12011193

12021194
int Minecraft::getFpsIntlCounter()

source/client/gui/Gui.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ void Gui::renderSlot(int slot, int x, int y, float f)
417417
Inventory* pInv = m_pMinecraft->m_pLocalPlayer->m_pInventory;
418418

419419
ItemInstance* pInst = pInv->getQuickSlotItem(slot);
420-
if (pInst == nullptr || pInst->m_itemID <= 0)
420+
if (ItemInstance::isNull(pInst))
421421
return;
422422

423423
float var6 = ((float)pInst->m_popTime) - f;
@@ -442,10 +442,7 @@ void Gui::renderSlotOverlay(int slot, int x, int y, float f)
442442
Inventory* pInv = m_pMinecraft->m_pLocalPlayer->m_pInventory;
443443

444444
ItemInstance* pInst = pInv->getQuickSlotItem(slot);
445-
if (!pInst)
446-
return;
447-
448-
if (!pInst->m_itemID)
445+
if (ItemInstance::isNull(pInst))
449446
return;
450447

451448
ItemRenderer::renderGuiItemOverlay(m_pMinecraft->m_pFont, m_pMinecraft->m_pTextures, pInst, x, y);

source/client/gui/screens/IngameBlockSelectionScreen.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ void IngameBlockSelectionScreen::init()
9999
void IngameBlockSelectionScreen::renderSlot(int index, int x, int y, float f)
100100
{
101101
ItemInstance* pItem = getInventory()->getItem(index);
102-
if (!pItem)
103-
return;
104-
105-
if (!pItem->m_itemID)
102+
if (ItemInstance::isNull(pItem))
106103
return;
107104

108105
ItemRenderer::renderGuiItem(m_pMinecraft->m_pFont, m_pMinecraft->m_pTextures, pItem, x, y, true);

source/client/player/input/MouseBuildInput.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ MouseBuildInput::MouseBuildInput()
99
m_lastButtonStates[i] = false;
1010
}
1111

12+
void MouseBuildInput::_updateLastButtonStates()
13+
{
14+
m_lastButtonStates[BUTTON_LEFT] = Mouse::isButtonDown(BUTTON_LEFT);
15+
m_lastButtonStates[BUTTON_RIGHT] = Mouse::isButtonDown(BUTTON_RIGHT);
16+
m_lastButtonStates[BUTTON_MIDDLE] = Mouse::isButtonDown(BUTTON_MIDDLE);
17+
}
18+
1219
bool MouseBuildInput::tickBuild(Player* player, BuildActionIntention* buildActionIntention)
1320
{
1421
bool wroteIntention = false;
@@ -61,10 +68,7 @@ bool MouseBuildInput::tickBuild(Player* player, BuildActionIntention* buildActio
6168
*buildActionIntention = BuildActionIntention(intent);
6269
}
6370

64-
// Log last button states
65-
m_lastButtonStates[BUTTON_LEFT] = Mouse::isButtonDown(BUTTON_LEFT);
66-
m_lastButtonStates[BUTTON_RIGHT] = Mouse::isButtonDown(BUTTON_RIGHT);
67-
m_lastButtonStates[BUTTON_MIDDLE] = Mouse::isButtonDown(BUTTON_MIDDLE);
71+
_updateLastButtonStates();
6872

6973
return wroteIntention;
7074
}

source/client/player/input/MouseBuildInput.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ class MouseBuildInput : public IBuildInput
1212
public:
1313
MouseBuildInput();
1414

15+
private:
16+
void _updateLastButtonStates();
17+
18+
public:
1519
virtual bool tickBuild(Player* player, BuildActionIntention* buildActionIntention) override;
1620
};
1721

source/client/player/input/UnifiedTurnBuild.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ bool UnifiedTurnBuild::tickBuild(Player* pPlayer, BuildActionIntention* pIntenti
182182
intent = BuildActionIntention::INTERACT;
183183
wroteIntention = true;
184184
}
185-
else */if (field_24 /* && pPlayer->isUsingItem()*/) // Holds off on acknowledging interact intent until the user is absolutely sure a tick later
185+
// Holds off on acknowledging interact intent until the user is absolutely sure a tick later
186+
else */if (field_24 /*&& pPlayer->isUsingItem()*/) // Adding pPlayer->isUsingItem() makes player break blocks way too fast when not holding items
186187
{
187188
intent = BuildActionIntention::TOUCH_HOLD_CONTINUE;
188189
wroteIntention = true;

source/client/renderer/ItemInHandRenderer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void ItemInHandRenderer::itemUsed()
4141
void ItemInHandRenderer::renderItem(ItemInstance* inst)
4242
{
4343
#ifndef ORIGINAL_CODE
44-
if (inst->isNull())
44+
if (ItemInstance::isNull(inst))
4545
return;
4646
#endif
4747

@@ -197,7 +197,7 @@ void ItemInHandRenderer::render(float f)
197197
float fAnim = pLP->getAttackAnim(f);
198198
constexpr float d = 0.8f;
199199

200-
if (!pItem->isNull())
200+
if (!ItemInstance::isNull(pItem))
201201
{
202202
glTranslatef(-0.4f * Mth::sin(float(M_PI) * Mth::sqrt(fAnim)), 0.2f * Mth::sin(2.0f * float(M_PI) * Mth::sqrt(fAnim)), -0.2f * Mth::sin(float(M_PI) * fAnim));
203203
glTranslatef(0.7f * d, -0.65f * d - (1.0f - h) * 0.6f, -0.9f * d);

source/world/entity/Player.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ void Player::displayClientMessage(const std::string& msg)
250250

251251
void Player::drop(const ItemInstance* pItemInstance, bool b)
252252
{
253-
if (pItemInstance->isNull())
253+
if (ItemInstance::isNull(pItemInstance))
254254
return;
255255

256256
ItemEntity* pItemEntity = new ItemEntity(m_pLevel, Vec3(m_pos.x, m_pos.y - 0.3f + getHeadHeight(), m_pos.z), pItemInstance);

source/world/entity/Player.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class Player : public Mob
7272
bool isSurvival() const { return getPlayerGameType() == GAME_TYPE_SURVIVAL; }
7373
bool isCreative() const { return getPlayerGameType() == GAME_TYPE_CREATIVE; }
7474
ItemInstance* getSelectedItem() const;
75-
bool isUsingItem() const { return false && !getSelectedItem()->isNull(); }
75+
bool isUsingItem() const { return !ItemInstance::isNull(getSelectedItem()); }
7676

7777
// QUIRK: Yes, I did mean it like that, as did Mojang.
7878
#pragma GCC diagnostic push

source/world/item/Inventory.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ int Inventory::getQuickSlotItemId(int slotNo)
226226

227227
int idx = m_hotbar[slotNo];
228228
ItemInstance* pInst = getItem(idx);
229-
if (!pInst)
229+
if (ItemInstance::isNull(pInst))
230230
return -1;
231231

232232
return pInst->m_itemID;
@@ -238,13 +238,8 @@ ItemInstance* Inventory::getQuickSlotItem(int slotNo)
238238
return nullptr;
239239

240240
ItemInstance* pInst = getItem(m_hotbar[slotNo]);
241-
if (!pInst)
242-
return nullptr;
243-
244-
if (pInst->m_itemID == 0)
245-
return nullptr;
246241

247-
return pInst;
242+
return !ItemInstance::isNull(pInst) ? pInst : nullptr;
248243
}
249244

250245
ItemInstance* Inventory::getSelectedItem()
@@ -324,7 +319,7 @@ void Inventory::selectItemById(int itemID, int maxHotBarSlot)
324319
int Inventory::getAttackDamage(Entity* pEnt)
325320
{
326321
ItemInstance* pInst = getSelected();
327-
if (!pInst)
322+
if (ItemInstance::isNull(pInst))
328323
return 1;
329324

330325
return pInst->getAttackDamage(pEnt);

0 commit comments

Comments
 (0)