Skip to content

Commit

Permalink
Add: The hopper counter will clear the hopper when it accepts item st…
Browse files Browse the repository at this point in the history
…ack (#38)
  • Loading branch information
hhhxiao committed Oct 1, 2022
1 parent 5d6e828 commit 61a1b33
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 41 deletions.
76 changes: 38 additions & 38 deletions src/functions/HopperCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <mc/Block.hpp>
#include <mc/BlockActor.hpp>
#include <mc/BlockSource.hpp>
#include <mc/ChunkPos.hpp>
#include <mc/Dimension.hpp>
#include <mc/I18n.hpp>
#include <mc/Item.hpp>
#include <mc/ItemStackBase.hpp>
Expand All @@ -12,8 +14,22 @@
#include "HookAPI.h"
#include "Msg.h"
#include "TrapdoorMod.h"

namespace trapdoor {

namespace {

BlockSource *getProperRegion(Block *block, const BlockPos &pos) {
auto cp = fromBlockPos(pos).toChunkPos();
for (int i = 0; i < 3; i++) {
auto bs = Global<Level>->getDimension(1)->tryGetClosestPublicRegion({cp.x, cp.z});
if (bs && &bs->getBlock(pos.x, pos.y, pos.z) == block) {
return bs;
}
}
return nullptr;
}
} // namespace

const size_t HopperChannelManager::HOPPER_COUNTER_BLOCK = 236;
void HopperChannelManager::tick() {
if (this->enable) {
Expand Down Expand Up @@ -110,61 +126,45 @@ namespace trapdoor {
?setItem@HopperBlockActor@@UEAAXHAEBVItemStack@@@Z
*/


#define HOPPER_RET \
original(self, index, itemStack); \
return;

THook(void, "?setItem@HopperBlockActor@@UEAAXHAEBVItemStack@@@Z", void *self, unsigned int index,
ItemStackBase *itemStack) {
auto &hcm = trapdoor::mod().getHopperChannelManager();

if (!hcm.isEnable()) {
original(self, index, itemStack);
return;
HOPPER_RET
}

//Global<Level>->getBlockSource();

auto &ba = dAccess<BlockActor, -200>(self);
auto *block = ba.getBlock();
if (!block) {
original(self, index, itemStack);
return;
}
auto &ba = dAccess<BlockActor>(self, -200);
auto pos = ba.getPosition();
auto cp = trapdoor::fromBlockPos(pos).toChunkPos();
auto bs = Global<Level>->getDimension(0)->tryGetClosestPublicRegion({cp.x, cp.z});

// get Point Position
auto &pos = ba.getPosition();
// try to get player
Player *nearest = nullptr;

try {
Global<Level>->forEachPlayer([&](Player &player) {
auto &b = player.getRegion().getBlock(pos);
if (&b == block) {
trapdoor::logger().debug("find {}", player.getRealName());
nearest = &player;
throw std::logic_error("");
}
return true;
});
} catch (std::exception &) {
}
if (!nearest) {
original(self, index, itemStack);
return;
auto block = ba.getBlock();
if (!bs || !block) {
trapdoor::logger().debug("can not found a BlockSource");
HOPPER_RET
}

auto dir = trapdoor::facingToBlockPos(static_cast<trapdoor::TFACING>(block->getVariant()));
auto pointPos = BlockPos(pos.x + dir.x, pos.y + dir.y, pos.z + dir.z);
auto &pointBlock = nearest->getRegion().getBlock(pointPos);
if (pointBlock.getId() != 236) { // 混凝土
original(self, index, itemStack);
return;
auto &pointBlock = bs->getBlock(pointPos);

if (pointBlock.getId() != trapdoor::HopperChannelManager::HOPPER_COUNTER_BLOCK) { // 混凝土
HOPPER_RET;
}

auto ch = pointBlock.getVariant();
if (ch < 0 || ch > 15 || itemStack->getName().empty()) {
original(self, index, itemStack);
return;
HOPPER_RET;
}

trapdoor::logger().debug("{} ==> {}", itemStack->getName(), itemStack->getCount());
trapdoor::mod().getHopperChannelManager().getChannel(ch).add(itemStack->getName(),
itemStack->getCount());
itemStack->setNull();
HOPPER_RET;
}
5 changes: 4 additions & 1 deletion src/functions/InfoDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace trapdoor {
}

if (pos == BlockPos::MAX) {
return {"Get blockName failure", false};
return {"Get block failure", false};
}
auto &b = p->getRegion().getBlock(pos);
trapdoor::TextBuilder builder;
Expand All @@ -85,9 +85,12 @@ namespace trapdoor {
}

builder.sText(trapdoor::TextBuilder::AQUA, "Base:\n")
.text(" - Ptr: ")
.sTextF(trapdoor::TextBuilder::GREEN, "%p\n", &b)
.text(" - Name / Type: ")
.sTextF(trapdoor::TextBuilder::GREEN, "%s / %s\n", b.getName().c_str(),
b.getTypeName().c_str())

.text(" - ID / RTID: ")
.sTextF(trapdoor::TextBuilder::GREEN, "%d / %d\n", b.getId(), b.getRuntimeId())
.text(" - Variant: ")
Expand Down
2 changes: 1 addition & 1 deletion src/functions/SpawnHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "HookAPI.h"
#include "Msg.h"
#include "Utils.h"
enum SpawnBlockRequirements;
enum class SpawnBlockRequirements;
class SpawnConditions;
class MobSpawnRules;
namespace trapdoor {
Expand Down
2 changes: 1 addition & 1 deletion src/include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
#define Trapdoor_VERSION_PATCH 2
#define TRAPDOOR_VERSION 0.19.2
#define GAME_VERSION 1.19.30.04
#define BUILD_TIME 2022-10-01 00:26:49
#define BUILD_TIME 2022-10-01 10:38:19

0 comments on commit 61a1b33

Please sign in to comment.