Moved the whole logic to a new file (TooltipManager.cpp), and rewrote most of the logic to profit from object oriented programming. The side-effect is that the changes are scattered across entities, tiles and items because moving everything to OOP requires to add the tooltip to every .cpp file for every tile/item/entities (or their class, on plants for example which all use the tooltip IDS_TOOLTIPS_PLANT)
The issue with the default tooltips loop written by 4J is that:
Its running in the main Minecraft::tick loop and is more than a thousand lines long, polluting the function.
Not using C++'s full potential with object oriented programming, 4J decided to use a big switch case (probably because it could run better on hardware without branch prediction like the WiiU or Xbox 360 ?)
And seems to be better off in its own file than in the main Minecraft.cpp file
105 lines
3.1 KiB
C++
105 lines
3.1 KiB
C++
#include "stdafx.h"
|
|
#include "net.minecraft.world.level.tile.h"
|
|
#include "net.minecraft.world.level.h"
|
|
#include "net.minecraft.world.entity.h"
|
|
#include "net.minecraft.world.phys.h"
|
|
#include "LeashItem.h"
|
|
|
|
LeashItem::LeashItem(int id) : Item(id)
|
|
{
|
|
}
|
|
|
|
bool LeashItem::useOn(shared_ptr<ItemInstance> itemInstance, shared_ptr<Player> player, Level *level, int x, int y, int z, int face, float clickX, float clickY, float clickZ, bool bTestUseOnOnly)
|
|
{
|
|
int tile = level->getTile(x, y, z);
|
|
if (Tile::tiles[tile] != nullptr && Tile::tiles[tile]->getRenderShape() == Tile::SHAPE_FENCE)
|
|
{
|
|
if (bTestUseOnOnly) return bindPlayerMobsTest(player, level, x,y,z);
|
|
|
|
if (level->isClientSide)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
bindPlayerMobs(player, level, x, y, z);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool LeashItem::bindPlayerMobs(shared_ptr<Player> player, Level *level, int x, int y, int z)
|
|
{
|
|
// check if there is a knot at the given coordinate
|
|
shared_ptr<LeashFenceKnotEntity> activeKnot = LeashFenceKnotEntity::findKnotAt(level, x, y, z);
|
|
|
|
// look for entities that can be attached to the fence
|
|
bool foundMobs = false;
|
|
bool knotCreated = false;
|
|
double range = 7;
|
|
vector<shared_ptr<Entity> > *mobs = level->getEntitiesOfClass(typeid(Mob), AABB::newTemp(x - range, y - range, z - range, x + range, y + range, z + range));
|
|
if (mobs != nullptr)
|
|
{
|
|
for(auto& it : *mobs)
|
|
{
|
|
shared_ptr<Mob> mob = dynamic_pointer_cast<Mob>(it);
|
|
if (mob->isLeashed() && mob->getLeashHolder() == player)
|
|
{
|
|
if (activeKnot == nullptr)
|
|
{
|
|
activeKnot = LeashFenceKnotEntity::createAndAddKnot(level, x, y, z);
|
|
knotCreated = true;
|
|
}
|
|
mob->setLeashedTo(activeKnot, true);
|
|
foundMobs = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (foundMobs && knotCreated)
|
|
{
|
|
activeKnot->playSound(eSoundType_ENTITY_LEASHKNOT_PLACE, 1.0f, 1.0f);
|
|
}
|
|
|
|
return foundMobs;
|
|
}
|
|
|
|
// 4J-JEV: Similar to bindPlayerMobs, but doesn't actually bind mobs,
|
|
bool LeashItem::bindPlayerMobsTest(shared_ptr<Player> player, Level *level, int x, int y, int z)
|
|
{
|
|
// look for entities that can be attached to the fence
|
|
double range = 7;
|
|
vector<shared_ptr<Entity>> *mobs = level->getEntitiesOfClass(typeid(Mob), AABB::newTemp(x - range, y - range, z - range, x + range, y + range, z + range));
|
|
|
|
if (mobs != nullptr)
|
|
{
|
|
for(auto& it : *mobs)
|
|
{
|
|
shared_ptr<Mob> mob = dynamic_pointer_cast<Mob>(it);
|
|
if (mob->isLeashed() && mob->getLeashHolder() == player) return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
int LeashItem::getUseTooltipId(std::shared_ptr<ItemInstance> itemInstance,
|
|
std::shared_ptr<Player> player,
|
|
Level* level,
|
|
int x, int y, int z,
|
|
bool bUseItem) const
|
|
{
|
|
if (bUseItem && level != nullptr)
|
|
{
|
|
int targetTile = level->getTile(x, y, z);
|
|
// Not4J Anaël: We need to check if the target block is a fence because leashes can only attach to those
|
|
if (targetTile > 0 && Tile::tiles[targetTile] != nullptr)
|
|
{
|
|
if (targetTile == Tile::fence_Id)
|
|
{
|
|
return IDS_TOOLTIPS_ATTACH;
|
|
}
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|