Skip to content

Commit

Permalink
Fix: hopper counter can not work in non-overworld
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhxiao committed Apr 22, 2022
1 parent 110751a commit 32143e1
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 76 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ version/*
venv/
log/
/vsbuild
/sym
/sym
cmake-build-relwithdebinfo/
tools/sym
91 changes: 47 additions & 44 deletions api/block/Block.cpp
Original file line number Diff line number Diff line change
@@ -1,55 +1,58 @@
//
// Created by xhy on 2020/8/26.
//
#include <vector>
#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 <vector>

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<char *>(this, off::BLOCK_GET_VARIANT);
}

//获取方块实体的位置
BlockPos *BlockActor::getPosition() {
return offset_cast<BlockPos *>(this, off::BLOCKSOURCE_GET_POSITION);
//return reinterpret_cast<BlockPos *>(reinterpret_cast<VA>(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<char *>(this, off::BLOCK_GET_VARIANT);
}

Block *BlockActor::getBlock() {
//*(this + 2)
// return nullptr;
return *reinterpret_cast<Block **>(reinterpret_cast<VA>(this) + 16);
// return (Block *)*((char *)this + 2);
}

//获取方块实体的位置
BlockPos *BlockActor::getPosition() {
return offset_cast<BlockPos *>(this, off::BLOCKSOURCE_GET_POSITION);
// return reinterpret_cast<BlockPos *>(reinterpret_cast<VA>(this) + 44);
}

} // namespace trapdoor
49 changes: 24 additions & 25 deletions api/block/Block.h
Original file line number Diff line number Diff line change
@@ -1,50 +1,49 @@
#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 <vector>

struct Biome;

struct Biome;

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;
22 changes: 16 additions & 6 deletions mod/function/HopperCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,23 @@ THook(
//附近没玩家,直接返回
auto real_this = reinterpret_cast<trapdoor::BlockActor *>(
reinterpret_cast<VA>(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();
Expand Down

0 comments on commit 32143e1

Please sign in to comment.