Skip to content

Commit fc20bb7

Browse files
authored
Implement BowItem (#53)
* Implement all `Block` virtuals & numerous symbols * linter * clang-format * same change 😄 * clean * Update ServerPlayer.h * Update BaseEntityBlock.h * Update FireworksRocketEntity.h * Update ExperienceOrb.h * `BowItem` implemented * linter * update patch to be without decomp's formatting
1 parent a79176c commit fc20bb7

13 files changed

Lines changed: 5980 additions & 48 deletions

File tree

data/mcswitch_functions.csv

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2588,26 +2588,26 @@ Address,Quality,Size,Name
25882588
0x000000710006f7b0,U,000012,
25892589
0x000000710006f7bc,U,000148,
25902590
0x000000710006f850,U,000152,
2591-
0x000000710006f8e8,U,000300,_ZN7BowItemC_gen
2592-
0x000000710006fa14,U,000048,_ZN33BowItem_Pull_ItemPropertyFunctionC_gen
2593-
0x000000710006fa44,U,000048,_ZN36BowItem_Pulling_ItemPropertyFunctionC_gen
2591+
0x000000710006f8e8,O,000300,_ZN7BowItemC1Ev
2592+
0x000000710006fa14,U,000048,_ZN33BowItem_Pull_ItemPropertyFunctionC1Ev
2593+
0x000000710006fa44,U,000048,_ZN36BowItem_Pulling_ItemPropertyFunctionC1Ev
25942594
0x000000710006fa74,U,000332,_ZN7BowItem14findProjectileERKNSt3__110shared_ptrI6PlayerEE
25952595
0x000000710006fbc0,U,000092,
2596-
0x000000710006fc1c,U,000264,_ZN7BowItem12releaseUsingE12not_null_ptrI12ItemInstanceEP5LevelNSt3__110shared_ptrI12LivingEntityEEi
2596+
0x000000710006fc1c,O,000264,_ZN7BowItem12releaseUsingE12not_null_ptrI12ItemInstanceEP5LevelNSt3__110shared_ptrI12LivingEntityEEi
25972597
0x000000710006fd24,U,001344,
25982598
0x0000007100070264,U,000068,
2599-
0x00000071000702a8,U,000044,
2600-
0x00000071000702d4,U,000012,_ZN7BowItem14getUseDurationE12not_null_ptrI12ItemInstanceE
2601-
0x00000071000702e0,U,000008,_ZN7BowItem15getUseAnimationE12not_null_ptrI12ItemInstanceE
2602-
0x00000071000702e8,U,000164,_ZN7BowItem3useEP5LevelNSt3__110shared_ptrI6PlayerEEN15InteractionHand16EInteractionHandE
2603-
0x000000710007038c,U,000180,
2604-
0x0000007100070440,U,000008,_ZN7BowItem19getEnchantmentValueEv
2605-
0x0000007100070448,U,000124,_ZN7BowItem13registerIconsEP12IconRegister
2606-
0x00000071000704c4,U,000012,
2607-
0x00000071000704d0,U,000008,_ZN7BowItem16GetOverrideCountEv
2608-
0x00000071000704d8,U,000004,_ZN7BowItem22GetOverrideCountColourEv
2609-
0x00000071000704dc,U,000264,_ZN7BowItem20GetOverrideCountIconE12not_null_ptrI12ItemInstanceE
2610-
0x00000071000705e4,U,000140,_ZN7BowItem13GetUseTooltipERK21ItemToolTipDataHolder
2599+
0x00000071000702a8,O,000044,_ZN7BowItem17GetMaxBowDurationEv
2600+
0x00000071000702d4,O,000012,_ZN7BowItem14getUseDurationE12not_null_ptrI12ItemInstanceE
2601+
0x00000071000702e0,O,000008,_ZN7BowItem15getUseAnimationE12not_null_ptrI12ItemInstanceE
2602+
0x00000071000702e8,O,000164,_ZN7BowItem3useEP5LevelNSt3__110shared_ptrI6PlayerEEN15InteractionHand16EInteractionHandE
2603+
0x000000710007038c,O,000180,_ZN7BowItem19fjUseWithProjectileEP5LevelNSt3__110shared_ptrI6PlayerEEN15InteractionHand16EInteractionHandE12not_null_ptrI12ItemInstanceE
2604+
0x0000007100070440,O,000008,_ZN7BowItem19getEnchantmentValueEv
2605+
0x0000007100070448,O,000124,_ZN7BowItem13registerIconsEP12IconRegister
2606+
0x00000071000704c4,O,000012,_ZN7BowItem12getDrawnIconEi
2607+
0x00000071000704d0,O,000008,_ZN7BowItem16GetOverrideCountEv
2608+
0x00000071000704d8,O,000004,_ZN7BowItem22GetOverrideCountColourEv
2609+
0x00000071000704dc,O,000264,_ZN7BowItem20GetOverrideCountIconE12not_null_ptrI12ItemInstanceE
2610+
0x00000071000705e4,O,000140,_ZN7BowItem13GetUseTooltipERK21ItemToolTipDataHolder
26112611
0x0000007100070670,U,000060,_ZN12BowlFoodItemC_gen
26122612
0x00000071000706ac,U,000212,
26132613
0x0000007100070780,U,000056,_ZN13BreakDoorGoalC_gen
@@ -3948,7 +3948,7 @@ Address,Quality,Size,Name
39483948
0x00000071000b2780,U,000056,_ZN9BossEventC_gen2
39493949
0x00000071000b27b8,U,000036,_ZN9BossEventD0Ev
39503950
0x00000071000b27dc,U,000036,_ZN10BottleItemD0Ev
3951-
0x00000071000b2800,U,000036,_ZN7BowItemD0Ev
3951+
0x00000071000b2800,O,000036,_ZN7BowItemD0Ev
39523952
0x00000071000b2824,U,000036,_ZN12BowlFoodItemD0Ev
39533953
0x00000071000b2848,U,000004,
39543954
0x00000071000b284c,U,000036,_ZN13BreakDoorGoalD0Ev
@@ -15280,7 +15280,7 @@ Address,Quality,Size,Name
1528015280
0x0000007100220e64,U,000840,_ZN12LivingEntity21spawnItemUseParticlesE12not_null_ptrI12ItemInstanceEi
1528115281
0x00000071002211ac,U,000016,
1528215282
0x00000071002211bc,U,000316,_ZN12LivingEntity17completeUsingItemEv
15283-
0x00000071002212f8,U,000012,
15283+
0x00000071002212f8,O,000012,_ZN12LivingEntity10getUseItemEv
1528415284
0x0000007100221304,U,000088,_ZN12LivingEntity17getTicksUsingItemEv
1528515285
0x000000710022135c,U,000060,_ZN12LivingEntity16releaseUsingItemEv
1528615286
0x0000007100221398,U,000180,_ZN12LivingEntity16releaseUsingItemEi
@@ -30681,7 +30681,6 @@ Address,Quality,Size,Name
3068130681
0x0000007100495584,U,000020,
3068230682
0x0000007100495598,U,000008,
3068330683
0x00000071004955a0,U,000020,
30684-
0x00000071004955b4,U,000032,
3068530684
0x00000071004955d4,U,000020,
3068630685
0x00000071004955e8,U,000036,
3068730686
0x000000710049560c,U,000080,_ZN35PodiumPlaceMostBlocksDestroyedMedalC_gen
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include <memory>
4+
5+
class Entity;
6+
7+
class Explosion {
8+
public:
9+
std::shared_ptr<Entity> getDirectSource();
10+
};

src/Minecraft.World/net/minecraft/world/entity/LivingEntity.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
#include "net/minecraft/world/effect/MobEffects.h"
1313
#include "net/minecraft/world/entity/Entity.h"
1414
#include "net/minecraft/world/entity/player/Player.h"
15+
#include "net/minecraft/world/item/ItemInstance.h"
1516
#include "net/minecraft/world/item/enchantment/EnchantmentHelper.h"
1617
#include "net/minecraft/world/level/block/Blocks.h"
1718
#include "net/minecraft/world/level/block/state/BlockState.h"
1819
#include "net/minecraft/world/level/chunk/LevelChunk.h"
1920
#include "net/minecraft/world/level/gamemode/minigames/MiniGameDef.h"
21+
#include "types.h"
2022
#include <cmath>
2123
#include <memory>
2224

@@ -274,3 +276,7 @@ void LivingEntity::setAbsorptionAmount(float amount) {
274276
void LivingEntity::onEnterCombat() {}
275277
void LivingEntity::onLeaveCombat() {}
276278
void LivingEntity::setRecordPlayingNearby(const BlockPos&, bool) {}
279+
280+
not_null_ptr<ItemInstance> LivingEntity::getUseItem() {
281+
return this->mUseItem;
282+
}

src/Minecraft.World/net/minecraft/world/entity/LivingEntity.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ class LivingEntity : public Entity {
184184
void init();
185185
void CheckThermalAreas();
186186
void fallFlyingTravel(double&, double&, double&, Vec3*, float&, float&, double&, double);
187+
not_null_ptr<ItemInstance> getUseItem();
187188

188189
Attribute* mAttributes;
189190
CombatTracker* mCombatTracker;
@@ -262,7 +263,7 @@ class LivingEntity : public Entity {
262263
float mSpeed;
263264
int mNoJumpDelay;
264265
float mAbsorptionAmount;
265-
std::shared_ptr<ItemInstance> mUseItem; // probably not_null_ptr
266+
not_null_ptr<ItemInstance> mUseItem;
266267
int mUseItemRemaining;
267268
int mFallFlyTicks;
268269
BlockPos mLastPos;

src/Minecraft.World/net/minecraft/world/inventory/Inventory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class Inventory : public Container {
88
public:
99
bool add(not_null_ptr<ItemInstance>);
10+
bool hasResource(Item*);
1011

1112
void getName() override;
1213
void getCustomName() override;
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#include "net/minecraft/world/item/BowItem.h"
2+
3+
#include "net/minecraft/client/Minecraft.h"
4+
#include "net/minecraft/client/renderer/texture/IconRegister.h"
5+
#include "net/minecraft/resources/ResourceLocation.h"
6+
#include "net/minecraft/world/eINSTANCEOF.h"
7+
#include "net/minecraft/world/entity/player/MultiplayerLocalPlayer.h"
8+
#include "net/minecraft/world/entity/player/Player.h"
9+
#include "net/minecraft/world/inventory/Inventory.h"
10+
#include "net/minecraft/world/item/InteractionResultHolder.h"
11+
#include "net/minecraft/world/item/Item.h"
12+
#include "net/minecraft/world/item/ItemInstance.h"
13+
#include "net/minecraft/world/item/Items.h"
14+
#include "types.h"
15+
#include <cstring>
16+
#include <memory>
17+
18+
BowItem::BowItem() : Item() {
19+
this->mMaxStackSize = 1;
20+
this->setMaxDamage(384);
21+
22+
memset(this->mBowIcons, 0, 3 * sizeof(TextureAtlasSprite*)); // sizeof because readability
23+
24+
this->addProperty(ResourceLocation(L"pull"),
25+
(const ItemPropertyFunction*)new BowItem_Pull_ItemPropertyFunction());
26+
27+
this->addProperty(ResourceLocation(L"pulling"),
28+
(const ItemPropertyFunction*)new BowItem_Pulling_ItemPropertyFunction());
29+
}
30+
31+
int BowItem::GetUseTooltip(const ItemToolTipDataHolder& data) {
32+
if (!data.mPlayer->mAbilities.mIsInstabuild && !data.mPlayer->mInventory->hasResource(Items::ARROW)
33+
&& !data.mPlayer->mInventory->hasResource(Items::TIPPED_ARROW)) {
34+
return 0xFFFFFFFF;
35+
}
36+
if (data.mPlayer->isUsingItem())
37+
return 0xEC754EE9;
38+
return 0x4381BCF0;
39+
}
40+
41+
InteractionResultHolder BowItem::use(Level* level, std::shared_ptr<Player> player,
42+
InteractionHand::EInteractionHand hand) {
43+
return this->fjUseWithProjectile(level, player, hand, this->findProjectile(player));
44+
}
45+
46+
int BowItem::getUseAnimation(not_null_ptr<ItemInstance>) {
47+
return 4;
48+
}
49+
50+
int BowItem::getUseDuration(not_null_ptr<ItemInstance>) {
51+
return 72000;
52+
}
53+
54+
void BowItem::releaseUsing(not_null_ptr<ItemInstance> item, Level* level, std::shared_ptr<LivingEntity> user,
55+
int remainingTicks) {
56+
if (user->isType(ePlayer)) {
57+
std::shared_ptr<Player> player = std::static_pointer_cast<Player>(user);
58+
not_null_ptr<ItemInstance> projectile = this->findProjectile(player);
59+
this->fjReleaseUsingWithProjectile(item, level, player, remainingTicks, projectile);
60+
}
61+
}
62+
63+
int BowItem::getEnchantmentValue() {
64+
return 1;
65+
}
66+
67+
std::wstring BowItem::BOW_ICON_NAMES[3] = {L"bow_pull_0", L"bow_pull_1", L"bow_pull_2"};
68+
69+
void BowItem::registerIcons(IconRegister* iconReg) {
70+
Item::registerIcons(iconReg);
71+
72+
this->mBowIcons[0] = iconReg->registerIcon(BOW_ICON_NAMES[0]);
73+
this->mBowIcons[1] = iconReg->registerIcon(BOW_ICON_NAMES[1]);
74+
this->mBowIcons[2] = iconReg->registerIcon(BOW_ICON_NAMES[2]);
75+
}
76+
77+
int BowItem::GetOverrideCount() {
78+
return 0;
79+
}
80+
81+
int BowItem::GetOverrideCountColour() {
82+
return Item::GetOverrideCountColour();
83+
}
84+
85+
TextureAtlasSprite* BowItem::GetOverrideCountIcon(not_null_ptr<ItemInstance> item) {
86+
if (Minecraft::GetInstance()->mLocalPlayer->getUseItem() == item) {
87+
int ticksUsingItem = Minecraft::GetInstance()->mLocalPlayer->getTicksUsingItem();
88+
if (ticksUsingItem >= BowItem::GetMaxBowDuration() - 2)
89+
return Items::BOW->getDrawnIcon(2);
90+
91+
if (ticksUsingItem > (2 * BowItem::GetMaxBowDuration()) / 3)
92+
return Items::BOW->getDrawnIcon(1);
93+
94+
if (ticksUsingItem > 0)
95+
return Items::BOW->getDrawnIcon(0);
96+
}
97+
98+
return Item::GetOverrideCountIcon(item);
99+
}
100+
101+
InteractionResultHolder BowItem::fjUseWithProjectile(Level* level, std::shared_ptr<Player> player,
102+
InteractionHand::EInteractionHand hand,
103+
not_null_ptr<ItemInstance> item) {
104+
not_null_ptr<ItemInstance> itemInHand = player->getItemInHand(hand);
105+
bool isEmpty = item->isEmpty();
106+
if (player->mAbilities.mIsInstabuild || !isEmpty) {
107+
player->startUsingItem(hand);
108+
return InteractionResultHolder(InteractionResult::SUCCESS, itemInHand);
109+
} else {
110+
// shame on 4J, I've spent too much time to figure out they've done this shit down below
111+
return isEmpty ? InteractionResultHolder(InteractionResult::FAIL, itemInHand) :
112+
InteractionResultHolder(InteractionResult::PASS, itemInHand);
113+
}
114+
}
115+
116+
TextureAtlasSprite* BowItem::getDrawnIcon(int id) {
117+
return this->mBowIcons[id];
118+
}
119+
120+
int BowItem::GetMaxBowDuration() {
121+
return Minecraft::GetInstance()->GetMiniGame()->HasFeatureArrowCanKnockback() ? 10 : 20;
122+
}
Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,51 @@
11
#pragma once
22

3+
#include "net/minecraft/client/renderer/texture/TextureAtlasSprite.h"
34
#include "net/minecraft/world/item/Item.h"
45
#include <memory>
6+
#include <string>
57

68
class Player;
79
class ItemInstance;
810

11+
class BowItem_Pull_ItemPropertyFunction : ItemPropertyFunction {
12+
public:
13+
void call(not_null_ptr<ItemInstance>, Level*, std::shared_ptr<LivingEntity>) override;
14+
};
15+
16+
class BowItem_Pulling_ItemPropertyFunction : ItemPropertyFunction {
17+
public:
18+
void call(not_null_ptr<ItemInstance>, Level*, std::shared_ptr<LivingEntity>) override;
19+
};
20+
921
class BowItem : public Item {
1022
public:
11-
not_null_ptr<ItemInstance> findProjectile(const std::shared_ptr<Player>&);
23+
BowItem();
24+
25+
static int GetMaxBowDuration();
26+
27+
int GetUseTooltip(const ItemToolTipDataHolder&) override;
28+
InteractionResultHolder use(Level*, std::shared_ptr<Player>, InteractionHand::EInteractionHand) override;
29+
int getUseAnimation(not_null_ptr<ItemInstance>) override;
30+
int getUseDuration(not_null_ptr<ItemInstance>) override;
31+
void releaseUsing(not_null_ptr<ItemInstance> item, Level* level, std::shared_ptr<LivingEntity> user,
32+
int remainingTicks) override;
33+
int getEnchantmentValue() override;
34+
void registerIcons(IconRegister*) override;
35+
int GetOverrideCount() override;
36+
int GetOverrideCountColour() override;
37+
TextureAtlasSprite* GetOverrideCountIcon(not_null_ptr<ItemInstance>) override;
38+
39+
not_null_ptr<ItemInstance> findProjectile(std::shared_ptr<Player>);
40+
InteractionResultHolder fjUseWithProjectile(Level*, std::shared_ptr<Player>,
41+
InteractionHand::EInteractionHand,
42+
not_null_ptr<ItemInstance>);
43+
void fjReleaseUsingWithProjectile(not_null_ptr<ItemInstance> bow, Level* level,
44+
std::shared_ptr<LivingEntity> shooter, int remainingTicks,
45+
not_null_ptr<ItemInstance> projectile);
46+
TextureAtlasSprite* getDrawnIcon(int id);
47+
48+
static std::wstring BOW_ICON_NAMES[3];
49+
50+
TextureAtlasSprite* mBowIcons[3];
1251
};

src/Minecraft.World/net/minecraft/world/item/Item.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "net/minecraft/world/entity/EquipmentSlot.h"
66
#include "net/minecraft/world/entity/ai/attributes/AttributeModifier.h"
77
#include "net/minecraft/world/item/InteractionResultHolder.h"
8+
#include "net/minecraft/world/item/ItemTooltipDataHolder.h"
89
#include "types.h"
910
#include <memory>
1011
#include <string>
@@ -18,11 +19,6 @@ class Level;
1819
class Player;
1920
class BlockState;
2021
class CompoundTag;
21-
class ItemToolTipDataHolder {
22-
public:
23-
char gap0[24];
24-
int idk;
25-
};
2622
class Entity;
2723
class IconRegister;
2824
class BlockPos;
@@ -31,7 +27,7 @@ class TextureAtlasSprite;
3127

3228
class ItemPropertyFunction {
3329
public:
34-
virtual void call(not_null_ptr<ItemInstance>, Level*, std::shared_ptr<LivingEntity>);
30+
virtual void call(not_null_ptr<ItemInstance>, Level*, std::shared_ptr<LivingEntity>) = 0;
3531
};
3632

3733
// Don't know if this exists, it exists on Java
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include <memory>
4+
5+
class Player;
6+
7+
class ItemToolTipDataHolder {
8+
public:
9+
std::shared_ptr<Player> mPlayer;
10+
void* fill;
11+
int idk;
12+
};

src/Minecraft.World/net/minecraft/world/item/Items.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void Items::staticCtor() {
1313
IRON_AXE = get(L"iron_axe");
1414
FLINT_AND_STEEL = get(L"flint_and_steel");
1515
APPLE = get(L"apple");
16-
BOW = get(L"bow");
16+
BOW = (BowItem*)get(L"bow");
1717
ARROW = get(L"arrow");
1818
SPECTRAL_ARROW = get(L"spectral_arrow");
1919
TIPPED_ARROW = get(L"tipped_arrow");

0 commit comments

Comments
 (0)