Skip to content

Commit

Permalink
Merge pull request #93 from bedrock-dev/fix-issue-91
Browse files Browse the repository at this point in the history
Fix: hud hopper counter info displayer error
  • Loading branch information
hhhxiao authored Apr 10, 2023
2 parents f1f6f31 + 6406c5b commit 5411927
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 56 deletions.
37 changes: 16 additions & 21 deletions src/functions/HUDHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace trapdoor {

std::string buildRedstoneInfo(Player *player) {
auto pointBlock = reinterpret_cast<Actor *>(player)->getBlockFromViewVector();
if (pointBlock.isNull()) return "Signal: -\n";
if (pointBlock.isNull()) return "Signal: -";

auto &cs = player->getDimension().getCircuitSystem();
auto &graph = getCircuitSceneGraph(&cs);
Expand All @@ -51,17 +51,10 @@ namespace trapdoor {
static_cast<unsigned char>(FaceID::East)));
}

return fmt::format("Signal: {} / {}\n", redstone_signal, container_signal);
return fmt::format("Signal: {} / {}", redstone_signal, container_signal);
}

std::string buildHopperCounter(Player *player) {
// auto &hcm = trapdoor::mod().getHopperChannelManager();
// if (!hcm.isEnable()) return "";
// TextBuilder b;
// auto pointBlock = reinterpret_cast<Actor
// *>(player)->getBlockFromViewVector(); if (pointBlock.isNull()) return "";
// auto *block = pointBlock.getBlock();
// if (block->getId() == HopperChannelManager::HOPPER_COUNTER_BLOCK) {
return trapdoor::mod().getHopperChannelManager().getHUDData(
trapdoor::mod().getUserConfig().getActiveHopperChannel(player->getRealName()));
}
Expand Down Expand Up @@ -103,7 +96,7 @@ namespace trapdoor {
delta.y * 20, delta.z * 20);
auto &biome = bs.getBiome(pos);
b.textF("Biome: %s (%d)\n", biome.getName().c_str(), biome.getId());
return b.get();
return b.removeEndl().get();
}

std::string buildMsptHud() {
Expand All @@ -117,14 +110,14 @@ namespace trapdoor {
.text(" TPS: ")
.sTextF(color, "%.1f", tps)
.text("\n");
return builder.get();
return builder.removeEndl().get();
}
std::string buildSpawnCap() {
auto cap = dAccess<int, 64 * 4>(&Global<Level>->getSpawner());
auto color = cap >= 200 ? trapdoor::TextBuilder::RED : trapdoor::TextBuilder::GREEN;
TextBuilder builder;
builder.text("Global Cap: ").sTextF(color, "%d\n", cap);
return builder.get();
builder.text("Global Cap: ").sTextF(color, "%d", cap);
return builder.removeEndl().get();
}
} // namespace

Expand Down Expand Up @@ -200,6 +193,7 @@ namespace trapdoor {
refresh_time =
(refresh_time + 1) % trapdoor::mod().getConfig().getBasicConfig().hudRefreshFreq;
if (refresh_time != 1) return;
auto ps = [](const std::string &s) { return s.empty() ? s : s + "\n"; };
// 遍历所有的有配置文件的信息(想有显示就必须add,add就会创建表项,因此不会出现玩家不在列表的选项)
auto playerInfos = trapdoor::mod().getUserConfig().getPlayerData();
for (auto &info : playerInfos) {
Expand All @@ -208,30 +202,31 @@ namespace trapdoor {
std::string s;
auto &cfg = info.second.hud_config;
if (cfg[HUDItemType::Base]) {
s += buildBaseHud(p);
s += ps(buildBaseHud(p));
}

if (cfg[HUDItemType::Mspt]) {
s += buildMsptHud();
s += ps(buildMsptHud());
}
if (cfg[HUDItemType::Redstone]) {
s += buildRedstoneInfo(p);
s += ps(buildRedstoneInfo(p));
}
if (cfg[HUDItemType::Vill]) {
s += buildVillagerInfo(p);
s += ps(buildVillagerInfo(p));
}
if (cfg[HUDItemType::Counter]) {
s += buildHopperCounter(p);
s += ps(buildHopperCounter(p));
}
if (cfg[HUDItemType::Cont]) {
s += buildContainerInfo(p);
s += ps(buildContainerInfo(p));
}
if (cfg[HUDItemType::GlobalCap]) {
s += buildSpawnCap();
s += ps(buildSpawnCap());
}
if (s.back() == '\n') {
while (!s.empty() && s.back() == '\n') {
s.pop_back();
}
// trapdoor::logger().debug("Send HUD: [{}]", s);
p->sendText(s, TextType::TIP);
}
}
Expand Down
75 changes: 41 additions & 34 deletions src/functions/HopperCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace trapdoor {

void HopperChannelManager::tick() {
if (this->isEnable()) {
for (auto &channel: channels) {
for (auto &channel : channels) {
channel.tick();
}
}
Expand All @@ -37,8 +37,9 @@ namespace trapdoor {
return ErrorMsg("hopper.error.invalid-channel");
} else {
if (player) {
//添加到用户缓存中
trapdoor::mod().getUserConfig().setActiveHopperChannel(player->getRealName(), channel);
// 添加到用户缓存中
trapdoor::mod().getUserConfig().setActiveHopperChannel(player->getRealName(),
channel);
}

auto &ch = this->getChannel(channel);
Expand Down Expand Up @@ -67,13 +68,15 @@ namespace trapdoor {
}

std::string HopperChannelManager::getHUDData(int channel) {
if (!this->isEnable()) return "";
if (channel < 0 || channel > 15) return "";
auto &ch = this->getChannel(channel);
return ch.info();
return ch.info(true);
}

bool
HopperChannelManager::isEnable() const { return trapdoor::mod().getConfig().getGlobalFunctionConfig().hopperCounter; }
bool HopperChannelManager::isEnable() const {
return trapdoor::mod().getConfig().getGlobalFunctionConfig().hopperCounter;
}

void CounterChannel::add(const std::string &itemName, size_t num) {
counterList[itemName] += num;
Expand All @@ -87,7 +90,7 @@ namespace trapdoor {

std::string CounterChannel::info(bool simple) {
size_t n = 0;
for (const auto &i: this->counterList) {
for (const auto &i : this->counterList) {
n += i.second;
}

Expand All @@ -98,37 +101,41 @@ namespace trapdoor {
std::string stringBuilder;
trapdoor::TextBuilder builder;

if (!simple) {
builder.text("Channel: ").sTextF(TB::BOLD | TB::WHITE, "%d \n", channel);
builder
.text("Total ")
// total items number
.num(n)
// total speed
.text(" items (")
.num(static_cast<float>(n) * 1.0f / static_cast<float>(gameTick) * 72000)
.text("/h)")
// time
.text(" in ")
.num(gameTick)
// time in hour
.text(" gt (")
.num(static_cast<float>(gameTick) / 72000.0f)
.text(" h)\n");
} else {
builder.textF("%d,(%.1f h))\n", n, static_cast<float>(gameTick) / 72000.0f);
}

for (const auto &i: counterList) {
builder.text("Channel: ").sTextF(TB::BOLD | TB::WHITE, "%d \n", channel);

// if (!simple) {
// builder.text("Total ");
// }
//
// total items number
builder
.num(n)
// total speed
.text(" items (")
.num(static_cast<float>(n) * 1.0f / static_cast<float>(gameTick) * 72000)
.text("/h)")
// time
.text(" in ")
.num(gameTick)
// time in hour
.text(" gt (")
.num(static_cast<float>(gameTick) / 72000.0f)
.text(" h)\n");

// } else {
// builder.textF("%d,(%.1f h))\n", n, static_cast<float>(gameTick) / 72000.0f);
// }

for (const auto &i : counterList) {
builder.sText(TB::GRAY, " - ");
builder.textF("%s: ", i.first.c_str())
.num(i.second)
.text(" (")
.num(static_cast<float>(i.second) * 1.0f / static_cast<float>(gameTick) * 72000)
.text("/h)\n");
.num(i.second)
.text(" (")
.num(static_cast<float>(i.second) * 1.0f / static_cast<float>(gameTick) * 72000)
.text("/h)\n");
}

return builder.get();
return builder.removeEndl().get();
}

} // namespace trapdoor
Expand Down
2 changes: 1 addition & 1 deletion src/functions/VillageHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ namespace trapdoor {
}
}
}
return builder.get();
return builder.removeEndl().get();
}

ActionResult VillageHelper::showVillageInfo(Player *p) {
Expand Down

0 comments on commit 5411927

Please sign in to comment.