Files
LegacyRenouveau/Minecraft.World/MinecartFurnace.cpp
T
Anaël d8d65bcd49 game loop: Refactor the tooltips loop
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
2026-10-04 20:54:24 +02:00

210 lines
4.5 KiB
C++

#include "Minecart.h"
#include "stdafx.h"
#include "net.minecraft.world.damagesource.h"
#include "net.minecraft.world.entity.h"
#include "net.minecraft.world.entity.player.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.item.h"
#include "net.minecraft.network.packet.h"
#include "MinecartFurnace.h"
MinecartFurnace::MinecartFurnace(Level *level) : Minecart(level)
{
defineSynchedData();
fuel = 0;
xPush = zPush = 0.0f;
}
MinecartFurnace::MinecartFurnace(Level *level, double x, double y, double z) : Minecart(level, x, y, z)
{
defineSynchedData();
fuel = 0;
xPush = zPush = 0.0f;
}
// 4J Added
int MinecartFurnace::getContainerType()
{
return ContainerOpenPacket::MINECART_HOPPER;
}
int MinecartFurnace::getType()
{
return TYPE_FURNACE;
}
void MinecartFurnace::defineSynchedData()
{
Minecart::defineSynchedData();
entityData->define(DATA_ID_FUEL, static_cast<byte>(0));
}
void MinecartFurnace::tick()
{
Minecart::tick();
if (fuel > 0)
{
fuel--;
}
if (fuel <= 0)
{
xPush = zPush = 0;
}
setHasFuel(fuel > 0);
if (hasFuel() && random->nextInt(4) == 0)
{
level->addParticle(eParticleType_largesmoke, x, y + 0.8, z, 0, 0, 0);
}
}
void MinecartFurnace::destroy(DamageSource *source)
{
Minecart::destroy(source);
if (!source->isExplosion())
{
spawnAtLocation(std::make_shared<ItemInstance>(Tile::furnace, 1), 0);
}
}
void MinecartFurnace::moveAlongTrack(int xt, int yt, int zt, double maxSpeed, double slideSpeed, int tile, int data)
{
//2 times max speed to get 8blocks
double lceMaxSpeed = maxSpeed * 2.0;
Minecart::moveAlongTrack(xt, yt, zt, lceMaxSpeed, slideSpeed, tile, data);
double sd = xPush * xPush + zPush * zPush;
if (sd > 0.01 * 0.01 && xd * xd + zd * zd > 0.001)
{
sd = Mth::sqrt(sd);
xPush /= sd;
zPush /= sd;
if (xPush * xd + zPush * zd < 0)
{
xPush = 0;
zPush = 0;
}
else
{
xPush = xd;
zPush = zd;
}
}
}
void MinecartFurnace::applyNaturalSlowdown()
{
double sd = xPush * xPush + zPush * zPush;
if (sd > 0.01 * 0.01)
{
sd = Mth::sqrt(sd);
xPush /= sd;
zPush /= sd;
//from 0.05 to 0.1
double speed = 0.1;
xd *= 0.8f;
yd *= 0;
zd *= 0.8f;
xd += xPush * speed;
zd += zPush * speed;
}
else
{
xd *= 0.98f;
yd *= 0;
zd *= 0.98f;
}
Minecart::applyNaturalSlowdown();
}
bool MinecartFurnace::interact(shared_ptr<Player> player)
{
shared_ptr<ItemInstance> selected = player->inventory->getSelected();
if (selected != nullptr && selected->id == Item::coal_Id)
{
if (!player->abilities.instabuild && --selected->count == 0) player->inventory->setItem(player->inventory->selected, nullptr);
fuel += SharedConstants::TICKS_PER_SECOND * 180;
}
xPush = x - player->x;
zPush = z - player->z;
return true;
}
void MinecartFurnace::addAdditonalSaveData(CompoundTag *base)
{
Minecart::addAdditonalSaveData(base);
base->putDouble(L"PushX", xPush);
base->putDouble(L"PushZ", zPush);
base->putShort(L"Fuel", static_cast<short>(fuel));
}
void MinecartFurnace::readAdditionalSaveData(CompoundTag *base)
{
Minecart::readAdditionalSaveData(base);
xPush = base->getDouble(L"PushX");
zPush = base->getDouble(L"PushZ");
fuel = base->getShort(L"Fuel");
}
bool MinecartFurnace::hasFuel()
{
return (entityData->getByte(DATA_ID_FUEL) & 1) != 0;
}
void MinecartFurnace::setHasFuel(bool fuel)
{
if (fuel)
{
entityData->set(DATA_ID_FUEL, static_cast<byte>(entityData->getByte(DATA_ID_FUEL) | 1));
}
else
{
entityData->set(DATA_ID_FUEL, static_cast<byte>(entityData->getByte(DATA_ID_FUEL) & ~1));
}
}
Tile *MinecartFurnace::getDefaultDisplayTile()
{
return Tile::furnace_lit;
}
int MinecartFurnace::getDefaultDisplayData()
{
return 2;
}
int MinecartFurnace::getAttackTooltipId(std::shared_ptr<Player> player)
{
return IDS_TOOLTIPS_MINE;
}
int MinecartFurnace::getInteractTooltipId(Level *level,
std::shared_ptr<Player> player,
std::shared_ptr<ItemInstance> heldItem)
{
int heldItemId = -1;
if (heldItem != nullptr && heldItem->getItem() != nullptr)
{
heldItemId = heldItem->getItem()->id;
}
if (heldItemId == Item::coal_Id)
{
return IDS_TOOLTIPS_USE;
}
return 0;
}