Skip to content

Commit 21ff173

Browse files
committed
Fully working shulker hover
1 parent a07677d commit 21ff173

4 files changed

Lines changed: 55 additions & 14 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
cmake_minimum_required(VERSION 3.12)
2-
project(ItemInformation)
2+
project(BetterInventory)
33
set(MOD_VERSION "1.4.0")
44

5-
65
# Define only RelWithDebInfo as the available build configuration
76
set(CMAKE_CONFIGURATION_TYPES "RelWithDebInfo" CACHE STRING "Build configurations" FORCE)
87
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo" FORCE)

include/ShulkerRenderer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#include <minecraft/src-client/common/client/renderer/NinesliceInfo.h>
1111
#include <amethyst/ui/NinesliceHelper.h>
1212

13+
#define SHULKER_CACHE_SIZE 16
14+
1315
class ShulkerRenderer {
1416
public:
15-
void Render(UIRenderContext* ctx, HoverRenderer* hoverRenderer);
17+
void Render(UIRenderContext* ctx, HoverRenderer* hoverRenderer, int index);
1618
};

src/ShulkerRenderer.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "ShulkerRenderer.h"
22

3-
extern ItemStack shulkerInventory[27];
3+
extern ItemStack shulkerInventory[SHULKER_CACHE_SIZE][27];
44

55
// Texture loading
66
static HashedString flushString(0xA99285D21E94FC80, "ui_flush");
@@ -22,23 +22,34 @@ glm::tvec2<float> itemSlotUvPos(188.0f / 256.0f, 184.0f / 256.0f);
2222
glm::tvec2<float> itemSlotUvSize(22.0f / 256.0f, 22.0f / 256.0f);
2323
Amethyst::NinesliceHelper backgroundNineslice(16, 16, 4, 4);
2424

25-
void ShulkerRenderer::Render(UIRenderContext* ctx, HoverRenderer* hoverRenderer) {
25+
int countNewlines(const std::string& str) {
26+
int newlineCount = 0;
27+
28+
for (char c : str) {
29+
if (c == '\n') {
30+
newlineCount++;
31+
}
32+
}
33+
34+
return newlineCount;
35+
}
36+
37+
void ShulkerRenderer::Render(UIRenderContext* ctx, HoverRenderer* hoverRenderer, int index) {
2638
// Only load inventory resources once
2739
if (!hasLoadedTexture) {
2840
ctx->getTexture(itemSlotTexture.get(), &itemSlotLocation, true);
2941
ctx->getTexture(backgroundTexture.get(), &backgroundLocation, true);
3042
hasLoadedTexture = true;
3143
}
3244

33-
float textHeight = 30.0f;
45+
float textHeight = (countNewlines(hoverRenderer->mFilteredContent) + 1) * 10.0f;
3446
float panelWidth = slotSize * 9;
3547
float panelHeight = slotSize * 3 + textHeight;
3648

3749
float panelX = hoverRenderer->mCursorPosition.x + hoverRenderer->mOffset.x;
3850
float panelY = hoverRenderer->mCursorPosition.y + hoverRenderer->mOffset.y;
3951

4052
// Draw the background panel
41-
//RectangleArea background = {panelX, panelX + panelWidth, panelY, panelY + panelHeight};
4253
RectangleArea background = {panelX - 4, panelX + panelWidth + 4, panelY - 4, panelY + panelHeight + 4};
4354
backgroundNineslice.Draw(background, backgroundTexture.get(), ctx);
4455
ctx->flushImages(mce::Color::WHITE, 1.0f, flushString);
@@ -61,7 +72,7 @@ void ShulkerRenderer::Render(UIRenderContext* ctx, HoverRenderer* hoverRenderer)
6172

6273
for (int x = 0; x < 9; x++) {
6374
for (int y = 0; y < 3; y++) {
64-
const ItemStack* itemStack = &shulkerInventory[y * 9 + x];
75+
const ItemStack* itemStack = &shulkerInventory[index][y * 9 + x];
6576
if (itemStack->mItem == nullptr) continue;
6677

6778
float xPos = (x * slotSize) + borderSize + panelX;
@@ -84,7 +95,7 @@ void ShulkerRenderer::Render(UIRenderContext* ctx, HoverRenderer* hoverRenderer)
8495
// Draw the item counts
8596
for (int x = 0; x < 9; x++) {
8697
for (int y = 0; y < 3; y++) {
87-
ItemStack* itemStack = &shulkerInventory[y * 9 + x];
98+
ItemStack* itemStack = &shulkerInventory[index][y * 9 + x];
8899
if (itemStack->mItem == nullptr) continue;
89100
if (itemStack->count == 1) continue;
90101

src/dllmain.cpp

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Item::_appendFormattedHovertext _Item_appendFormattedHovertext;
88
HoverRenderer::__renderHoverBox __renderHoverBox;
99

1010
ShulkerRenderer shulkerRenderer;
11-
ItemStack shulkerInventory[27];
11+
ItemStack shulkerInventory[SHULKER_CACHE_SIZE][27];
1212

1313
static void Item_appendFormattedHovertext(Item* self, const ItemStackBase& itemStack, Level& level, std::string& text, uint8_t a5) {
1414
_Item_appendFormattedHovertext(self, itemStack, level, text, a5);
@@ -20,18 +20,36 @@ static void Item_appendFormattedHovertext(Item* self, const ItemStackBase& itemS
2020
uint64_t current = max - item->getDamageValue(itemStack.mUserData);
2121
text.append(fmt::format("\n{}7Durability: {} / {}{}r", "\xc2\xa7", current, max, "\xc2\xa7"));
2222
}
23-
23+
2424
std::string rawNameId;
2525
itemStack.getRawNameId(rawNameId);
26+
27+
if (rawNameId.find("shulker_box") != std::string::npos) {
28+
// Don't append the id for shulker boxes (makes it too long)
29+
text.append(fmt::format("\n{}8{}:{}{}r", "\xc2\xa7", item->mNamespace, rawNameId, "\xc2\xa7"));
30+
return;
31+
}
32+
2633
text.append(fmt::format("\n{}8{}:{} ({}){}r", "\xc2\xa7", item->mNamespace, rawNameId, item->mId, "\xc2\xa7"));
2734
}
2835

36+
int index = 0;
37+
2938
static void Shulker_appendFormattedHovertext(ShulkerBoxBlockItem* self, const ItemStackBase& itemStack, Level& level, std::string& text, uint8_t someBool) {
3039
// Use the appendFormattedHovertext for regular items, we don't want the list of items
3140
Item_appendFormattedHovertext(self, itemStack, level, text, someBool);
41+
42+
index++;
43+
if (index >= SHULKER_CACHE_SIZE) index = 0;
44+
45+
// Hide a secret index in the shulker name
46+
// We do this because appendFormattedHovertext gets called for the neightboring items so if there is a shulker
47+
// to the right of this one then its preview will get overriden, so we keep track of multiple at once using a rolling identifier
48+
text += fmt::format("{}{:x}", "\xc2\xa7", index);
49+
int thisIndex = index;
3250

3351
// Reset all the currrent item stacks
34-
for (auto& itemStack : shulkerInventory) {
52+
for (auto& itemStack : shulkerInventory[index]) {
3553
itemStack = ItemStack();
3654
}
3755

@@ -45,15 +63,26 @@ static void Shulker_appendFormattedHovertext(ShulkerBoxBlockItem* self, const It
4563
for (int i = 0; i < items->size(); i++) {
4664
const CompoundTag* itemCompound = items->getCompound(i);
4765
byte slot = itemCompound->getByte("Slot");
48-
shulkerInventory[slot]._loadItem(itemCompound);
66+
shulkerInventory[thisIndex][slot]._loadItem(itemCompound);
4967
}
5068
}
5169

5270
static void _renderHoverBox(HoverRenderer* self, MinecraftUIRenderContext* ctx, IClientInstance* client, RectangleArea* aabb, float someFloat) {
5371
// This is really bad code, it is relying on the fact that I have also hooked appendFormattedHovertext for items to append the item identifier
5472
// I have no idea where the currently hovered item is stored in the game! I can't find any references to it, so it might be set in some weird place?
73+
5574
if (self->mFilteredContent.find("shulker_box") != std::string::npos) {
56-
shulkerRenderer.Render(ctx, self);
75+
std::string cachedIndex = self->mFilteredContent.substr(self->mFilteredContent.size() - 7, 1);
76+
77+
try {
78+
int index = std::stoi(cachedIndex, nullptr, 16);
79+
shulkerRenderer.Render(ctx, self, index);
80+
}
81+
catch (...) {
82+
Log::Warning("There was an issue reading the shulker box! No id found, instead got: {}", cachedIndex);
83+
return;
84+
}
85+
5786
return;
5887
}
5988

0 commit comments

Comments
 (0)