Skip to content

Commit 4020fa6

Browse files
committed
Fixed bug preventing interaction with no item in-hand
* Cleaned up ItemInstance null handling
1 parent 670f4c7 commit 4020fa6

File tree

11 files changed

+83
-78
lines changed

11 files changed

+83
-78
lines changed

source/client/app/Minecraft.cpp

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,7 @@ void Minecraft::handleBuildAction(const BuildActionIntention& action)
350350
else if (action.isPlace() && canInteract)
351351
{
352352
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))
353+
if (m_pGameMode->useItemOn(player, m_pLevel, pItem, m_hitResult.m_tilePos, m_hitResult.m_hitSide))
360354
{
361355
bInteract = false;
362356

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

367361
if (isOnline())
368362
{
369-
if (pItem->m_itemID > C_MAX_TILES || pItem->m_itemID < 0)
363+
if (ItemInstance::isNull(pItem) || pItem->m_itemID > C_MAX_TILES)
370364
return;
371365

372366
TilePos tp(m_hitResult.m_tilePos);
@@ -480,7 +474,7 @@ void Minecraft::tickInput()
480474
else if (getOptions()->isKey(KM_DROP, keyCode))
481475
{
482476
ItemInstance *item = m_pLocalPlayer->m_pInventory->getSelected();
483-
if (item != nullptr)
477+
if (!ItemInstance::isNull(item))
484478
{
485479
ItemInstance itemDrop = m_pLocalPlayer->isSurvival() ? item->remove(1) : ItemInstance(*item);
486480
itemDrop.m_count = 1;
@@ -1181,22 +1175,19 @@ ItemInstance* Minecraft::getSelectedItem()
11811175
{
11821176
ItemInstance* pInst = m_pLocalPlayer->getSelectedItem();
11831177

1184-
if (!pInst)
1178+
if (ItemInstance::isNull(pInst))
11851179
return nullptr;
11861180

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());
1181+
if (m_pGameMode->isCreativeType())
1182+
{
1183+
// Create new "unlimited" ItemInstance for Creative mode
1184+
m_CurrItemInstance.m_itemID = pInst->m_itemID;
1185+
m_CurrItemInstance.m_count = 999;
1186+
m_CurrItemInstance.setAuxValue(pInst->getAuxValue());
1187+
return &m_CurrItemInstance;
1188+
}
11981189

1199-
return &m_CurrItemInstance;
1190+
return pInst;
12001191
}
12011192

12021193
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/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 false /* && 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);

source/world/item/ItemInstance.cpp

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -151,29 +151,6 @@ bool ItemInstance::isStackedByData()
151151
return getItem()->isStackedByData();
152152
}
153153

154-
bool ItemInstance::matches(ItemInstance* other) const
155-
{
156-
return this->getAuxValue() == other->getAuxValue() &&
157-
this->m_count == other->m_count &&
158-
this->m_itemID == other->m_itemID;
159-
}
160-
161-
bool ItemInstance::matches(ItemInstance* a1, ItemInstance* a2)
162-
{
163-
if (a1 == a2 && a1 == nullptr)
164-
return true;
165-
166-
if (a1 == nullptr || a2 == nullptr)
167-
return false;
168-
169-
return a1->matches(a2);
170-
}
171-
172-
int ItemInstance::getAttackDamage(Entity *pEnt)
173-
{
174-
return getItem()->getAttackDamage(pEnt);
175-
}
176-
177154
void ItemInstance::mineBlock(const TilePos& pos, Facing::Name face)
178155
{
179156
return getItem()->mineBlock(this, pos, face);
@@ -212,18 +189,54 @@ bool ItemInstance::useOn(Player* player, Level* level, const TilePos& pos, Facin
212189
return getItem()->useOn(this, player, level, pos, face);
213190
}
214191

192+
int ItemInstance::getAttackDamage(Entity* pEnt)
193+
{
194+
return getItem()->getAttackDamage(pEnt);
195+
}
196+
215197
bool ItemInstance::isNull() const
216198
{
217199
// 0.9.2
218200
if (m_itemID <= 0) // m_field_10, assuming this is m_itemID
219201
return true;
220202

221-
if (m_auxValue != 0)
222-
return false;
223-
if (m_count != 0)
224-
return false;
225-
if (m_popTime != 0)
203+
if (m_auxValue != 0 ||
204+
m_count != 0 ||
205+
m_popTime != 0)
206+
{
226207
return false;
208+
}
227209

228210
return true; // isNull
229211
}
212+
213+
bool ItemInstance::isNull(const ItemInstance* item)
214+
{
215+
return item == nullptr || item->isNull();
216+
}
217+
218+
bool ItemInstance::matches(const ItemInstance* a1, const ItemInstance* a2)
219+
{
220+
if (a1 == a2 && a1 == nullptr)
221+
return true;
222+
223+
if (a1 == nullptr || a2 == nullptr)
224+
return false;
225+
226+
return a1 == a2;
227+
}
228+
229+
bool ItemInstance::operator==(const ItemInstance& other) const
230+
{
231+
return this->getAuxValue() == other.getAuxValue() &&
232+
this->m_count == other.m_count &&
233+
this->m_itemID == other.m_itemID;
234+
}
235+
236+
bool ItemInstance::operator!=(const ItemInstance& other) const
237+
{
238+
// doing this is likely more efficient than inverting the result of == after the fact
239+
return this->getAuxValue() != other.getAuxValue() ||
240+
this->m_count != other.m_count ||
241+
this->m_itemID != other.m_itemID;
242+
}

0 commit comments

Comments
 (0)