From 32143e1ce37559731530593d92a3961909b450ef Mon Sep 17 00:00:00 2001 From: hhhxiao <1417599491@qq.com> Date: Sat, 23 Apr 2022 01:25:47 +0800 Subject: [PATCH] Fix: hopper counter can not work in non-overworld --- .gitignore | 4 +- api/block/Block.cpp | 91 ++++++++++++++++++---------------- api/block/Block.h | 49 +++++++++--------- mod/function/HopperCounter.cpp | 22 +++++--- 4 files changed, 90 insertions(+), 76 deletions(-) diff --git a/.gitignore b/.gitignore index 5466d66..0b4c56f 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,6 @@ version/* venv/ log/ /vsbuild -/sym \ No newline at end of file +/sym +cmake-build-relwithdebinfo/ +tools/sym \ No newline at end of file diff --git a/api/block/Block.cpp b/api/block/Block.cpp index 198d487..0d8675a 100644 --- a/api/block/Block.cpp +++ b/api/block/Block.cpp @@ -1,55 +1,58 @@ // // Created by xhy on 2020/8/26. // -#include #include "Block.h" -#include "tools/Message.h" -#include "lib/mod.h" -#include "lib/SymHook.h" +#include "Offset.h" #include "block/BlockLegacy.h" -#include "tools/DirtyLogger.h" +#include "lib/SymHook.h" +#include "lib/mod.h" #include "tools/CastHelper.h" -#include "Offset.h" +#include "tools/DirtyLogger.h" +#include "tools/Message.h" +#include namespace trapdoor { - using namespace SymHook; - - //获取方块legacy - BlockLegacy *Block::getLegacy() { - return SYM_CALL( - BlockLegacy * (*)(Block * block), - MSSYM_B1QE14getLegacyBlockB1AA5BlockB2AAE19QEBAAEBVBlockLegacyB2AAA2XZ, - this - ); - } - - //获取方块名字 - std::string Block::getName() { - std::string debugStr; - SYM_CALL( - void(*)(void * block, std::string &), - MSSYM_B1QE13toDebugStringB1AA5BlockB2AAA4QEBAB1QA2AVB2QDA5basicB1UA6stringB1AA2DUB2QDA4charB1UA6traitsB1AA1DB1AA3stdB2AAA1VB2QDA9allocatorB1AA1DB1AA12B2AAA3stdB2AAA2XZ, - this, debugStr); - //remove "name: " - return debugStr.erase(0, 6); - } - - //是否是空气 - bool Block::isAir() { - return this->getLegacy()->getBlockID() == AIR; - } - - //获取特殊值 - int Block::getVariant() { - - return *offset_cast(this, off::BLOCK_GET_VARIANT); - } - - //获取方块实体的位置 - BlockPos *BlockActor::getPosition() { - return offset_cast(this, off::BLOCKSOURCE_GET_POSITION); - //return reinterpret_cast(reinterpret_cast(this) + 44); - } +using namespace SymHook; + +//获取方块legacy +BlockLegacy *Block::getLegacy() { + return SYM_CALL( + BlockLegacy * (*)(Block * block), + MSSYM_B1QE14getLegacyBlockB1AA5BlockB2AAE19QEBAAEBVBlockLegacyB2AAA2XZ, + this); +} + +//获取方块名字 +std::string Block::getName() { + std::string debugStr; + SYM_CALL( + void (*)(void *block, std::string &), + MSSYM_B1QE13toDebugStringB1AA5BlockB2AAA4QEBAB1QA2AVB2QDA5basicB1UA6stringB1AA2DUB2QDA4charB1UA6traitsB1AA1DB1AA3stdB2AAA1VB2QDA9allocatorB1AA1DB1AA12B2AAA3stdB2AAA2XZ, + this, debugStr); + // remove "name: " + return debugStr.erase(0, 6); +} + +//是否是空气 +bool Block::isAir() { return this->getLegacy()->getBlockID() == AIR; } + +//获取特殊值 +int Block::getVariant() { + + return *offset_cast(this, off::BLOCK_GET_VARIANT); +} + +Block *BlockActor::getBlock() { + //*(this + 2) + // return nullptr; + return *reinterpret_cast(reinterpret_cast(this) + 16); + // return (Block *)*((char *)this + 2); +} +//获取方块实体的位置 +BlockPos *BlockActor::getPosition() { + return offset_cast(this, off::BLOCKSOURCE_GET_POSITION); + // return reinterpret_cast(reinterpret_cast(this) + 44); } +} // namespace trapdoor diff --git a/api/block/Block.h b/api/block/Block.h index e3a1448..85b19ff 100644 --- a/api/block/Block.h +++ b/api/block/Block.h @@ -1,13 +1,13 @@ #pragma once -#include "lib/mod.h" -#include "lib/SymHook.h" -#include "graphics/BlockPos.h" #include "BlockLegacy.h" +#include "graphics/BlockPos.h" +#include "lib/SymHook.h" +#include "lib/mod.h" #include -struct Biome; +struct Biome; namespace trapdoor { @@ -15,36 +15,35 @@ namespace trapdoor { * 方块接口 * */ - class BlockLegacy; - - class Block { - public: - // 获取方块legacy - BlockLegacy *getLegacy(); +class BlockLegacy; - //获取方块名称(和item返回的名称不一样,不知道Mojang怎么想的) - std::string getName(); +class Block { +public: + // 获取方块legacy + BlockLegacy *getLegacy(); - //获取特殊值 - int getVariant(); + //获取方块名称(和item返回的名称不一样,不知道Mojang怎么想的) + std::string getName(); - //是否是空气 - bool isAir(); - }; + //获取特殊值 + int getVariant(); + //是否是空气 + bool isAir(); +}; /* * 方块实体接口 */ - class BlockActor { - public: - //获取位置 - BlockPos *getPosition(); +class BlockActor { +public: + //获取位置 + BlockPos *getPosition(); - //获取方块对象 - // Block *getBlock(); - }; + //获取方块对象 + Block *getBlock(); +}; -} +} // namespace trapdoor typedef trapdoor::Block Block; typedef trapdoor::BlockActor BlockActor; diff --git a/mod/function/HopperCounter.cpp b/mod/function/HopperCounter.cpp index 1f25206..fdfa0b8 100644 --- a/mod/function/HopperCounter.cpp +++ b/mod/function/HopperCounter.cpp @@ -118,13 +118,23 @@ THook( //附近没玩家,直接返回 auto real_this = reinterpret_cast( reinterpret_cast(hopperActor) - 208); + auto position = real_this->getPosition(); - auto nearestPlayer = - trapdoor::bdsMod->getLevel()->getNearestPlayer(*position); - if (!nearestPlayer) { - original(hopperActor, index, itemStack); - return; - } + trapdoor::Actor *nearestPlayer = nullptr; + + auto *hopper_block = real_this->getBlock(); + trapdoor::bdsMod->getLevel()->forEachPlayer([&](trapdoor::Actor *player) { + if (!player) + return; + auto bs = player->getBlockSource(); + if (!bs) + return; + auto *block = bs->getBlock(*position); + if (hopper_block && block && block == hopper_block) { + nearestPlayer = player; + } + }); + auto bs = nearestPlayer->getBlockSource(); auto hopperBlockVariant = (trapdoor::FACING)bs->getBlock(*position)->getVariant();