Skip to content

Commit

Permalink
nmsl
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Feb 10, 2023
1 parent 3d98097 commit 090b0ec
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 132 deletions.
9 changes: 7 additions & 2 deletions WorldEdit/WorldEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@
// }

THook(void, "?buildSerializationId@Block@@IEAAXI@Z", Block* block, unsigned int id) {
original(block, id);
auto& blockName = worldedit::getBlockNameMap();
auto& blockId = worldedit::getBlockIdMap();
auto blockid = block->getId();
blockName[blockid] = block->getTypeName();
blockId[block->getTypeName()] = blockid;
// std::cout << const_cast<BlockLegacy*>(&block->getLegacyBlock())->buildDescriptionId(*block) << " " << blockid
// << std::endl;
// std::cout << block->getTypeName() << " " << blockid << std::endl;
// std::cout << const_cast<BlockLegacy*>(&block->getLegacyBlock())->getTypeName() << std::endl;
// auto color = block->getLegacyBlock().getMapColor().toHexString();
// if (getBlockColorssMap().find(color) != getBlockColorssMap().end()) {
Expand All @@ -66,7 +70,7 @@ THook(void, "?buildSerializationId@Block@@IEAAXI@Z", Block* block, unsigned int
// getBlockColorssMap()[color] = block->getNbt()->toSNBT(0,
// SnbtFormat::Minimize);
// }
return original(block, id);
return;
}

namespace worldedit {
Expand All @@ -78,9 +82,10 @@ namespace worldedit {

class PlayerData& getPlayersData(std::string xuid) {
auto& playerDataMap = getPlayersDataMap();
if (playerDataMap.find(xuid) == playerDataMap.end()) {
if (playerDataMap.find(xuid) == playerDataMap.end() || (!playerDataMap[xuid].init)) {
playerDataMap[xuid] = PlayerData(Global<Level>->getPlayer(xuid));
}
// std::cout << "maxHistoryLength " << playerDataMap[xuid].maxHistoryLength << std::endl;
return playerDataMap[xuid];
}

Expand Down
2 changes: 1 addition & 1 deletion core/brush/tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ namespace worldedit {
while (!q.empty()) {
auto pos1 = q.front();
q.pop();
boundingBox = boundingBox.merge(pos1);
boundingBox.merge(pos1);
if (needEdge) {
BoundingBox(pos1 - 1, pos1 + 1).forEachBlockInBox([&](const BlockPos& tmpPos) {
if (tmpPos != pos1 && pos0.distanceTo(tmpPos) <= 0.5 + size &&
Expand Down
92 changes: 54 additions & 38 deletions core/command/RegionOperationCommand.cpp

Large diffs are not rendered by default.

26 changes: 18 additions & 8 deletions core/command/allCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

#include "Nlohmann/json.hpp"

#include "LoggerAPI.h"

Logger logger("WorldEdit");

namespace worldedit {
void commandsSetup() {
brushCommandSetup();
Expand Down Expand Up @@ -60,12 +64,15 @@ namespace worldedit {

for (auto& b : blockList.items()) {
std::string key = b.key();
std::cout << key << std::endl;
if (!isBEBlock(key)) {
Block* block = nullptr;
std::string bname = b.value()["bedrock_identifier"];
stringReplace(bname, "stone_block_", "stone_");
stringReplace(bname, "sealan_tern", "sealantern");
stringReplace(bname, "concrete_powder", "concretepowder");
if (b.value().contains("bedrock_states")) {
std::string snbt = "{\"name\":\"";
snbt += b.value()["bedrock_identifier"];
snbt += bname;
snbt += "\",\"states\":";
std::string states = b.value()["bedrock_states"].dump();
stringReplace(states, ":false", ":0b");
Expand All @@ -75,15 +82,17 @@ namespace worldedit {
try {
block = Block::create(CompoundTag::fromSNBT(snbt).get());
} catch (...) {
std::cout << snbt << std::endl;
logger.error(snbt);
break;
}
// if (block == nullptr)
// std::cout << snbt << std::endl;
// logger.error(bname);
} else {
try {
block = Block::create(b.value()["bedrock_identifier"], 0);
block = Block::create(bname, 0);
} catch (...) {
std::cout << b.value()["bedrock_identifier"] << std::endl;
logger.error(bname);
break;
}
}
if (block != nullptr) {
Expand All @@ -95,9 +104,10 @@ namespace worldedit {

try {
Global<CommandRegistry>->setSoftEnumValues("blockPattern", blocksName);
std::cout << "blockPattern" << std::endl;
// std::cout << "blockPattern" << std::endl;
} catch (...) {
std::cout << "blockPattern error" << std::endl;
logger.error("blockPattern error");
// std::cout << "blockPattern error" << std::endl;
}
},
20);
Expand Down
130 changes: 84 additions & 46 deletions core/data/PlayerData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,50 @@ namespace worldedit {
}

PlayerData::PlayerData() {
historyList.resize(0);
init = false;
xuid = "";
maxHistoryLength = 20;
updateArg = 2;
updateExArg = 1;

mainPos;
mainPosTime = 0;
mainPosDim = -1;

vicePos;
vicePosTime = 0;
vicePosDim = -1;

gMask = "";
region = nullptr;
historyList.clear();
historyFlag1 = 0;
historyFlag2 = 0;
historyList.resize(maxHistoryLength);
}

PlayerData::PlayerData(::Player* player) {
historyList.clear();
historyFlag1 = 0;
historyFlag2 = 0;
historyList.resize(maxHistoryLength);
xuid = player->getXuid();
init = true;
maxHistoryLength = 20;
updateArg = 2;
updateExArg = 1;

mainPos;
mainPosTime = 0;
mainPosDim = -1;

vicePos;
vicePosTime = 0;
vicePosDim = -1;

gMask = "";
region = nullptr;
// std::cout << "maxHistoryLength " << maxHistoryLength << std::endl;
}

bool PlayerData::changeVicePos(BlockInstance blockInstance, bool output) {
Expand All @@ -52,8 +90,8 @@ namespace worldedit {
}
if (region->setVicePos(pos, player->getDimensionId())) {
if (output)
player->sendText("worldedit.selection." + region->getName() + ".explain.secondary",
pos.toString(), region->size());
player->sendText("worldedit.selection." + region->getName() + ".explain.secondary", pos.toString(),
region->size());
vicePos = pos;
vicePosTime = 0;
vicePosDim = player->getDimensionId();
Expand Down Expand Up @@ -81,8 +119,8 @@ namespace worldedit {
}
if (region->setMainPos(pos, player->getDimensionId())) {
if (output)
player->sendText("worldedit.selection." + region->getName() + ".explain.primary",
pos.toString(), region->size());
player->sendText("worldedit.selection." + region->getName() + ".explain.primary", pos.toString(),
region->size());
mainPos = pos;
mainPosTime = 0;
mainPosDim = player->getDimensionId();
Expand All @@ -97,8 +135,7 @@ namespace worldedit {
return false;
}

void PlayerData::setVarByPlayer(
std::unordered_map<::std::string, double>& variables) {
void PlayerData::setVarByPlayer(std::unordered_map<::std::string, double>& variables) {
Player* player = Global<Level>->getPlayer(xuid);
auto playerPos = player->getPosition();
auto playerRot = player->getRotation();
Expand All @@ -109,14 +146,11 @@ namespace worldedit {
variables["pt"] = playerRot.y;
}

bool PlayerData::setBlockForHistory(BlockSource* blockSource,
const BlockPos& pos,
Block* block,
Block* exblock) {
bool PlayerData::setBlockForHistory(BlockSource* blockSource, const BlockPos& pos, Block* block, Block* exblock) {
CommandUtils::clearBlockEntityContents(*blockSource, pos);
blockSource->setExtraBlock(pos, *BedrockBlocks::mAir, 16 + updateArg);
// if (updateExArg % 2 == 1) {
blockSource->setBlock(pos, *block, updateArg, nullptr, nullptr);
blockSource->setBlock(pos, *block, updateArg, nullptr, nullptr);
// }
// else {
// blockSource->setBlockNoUpdate(pos.x, pos.y, pos.z, *block);
Expand All @@ -126,25 +160,23 @@ namespace worldedit {
blockSource->setExtraBlock(pos, *exblock, 16 + updateArg);
}
} else {
blockSource->setExtraBlock(pos, *VanillaBlocks::mFlowingWater,
16 + updateArg);
blockSource->setExtraBlock(pos, *VanillaBlocks::mFlowingWater, 16 + updateArg);
// if (updateExArg % 2 == 1) {
blockSource->setBlock(pos, *block, updateArg, nullptr, nullptr);
// }
blockSource->setBlock(pos, *block, updateArg, nullptr, nullptr);
// }
// else {
// blockSource->setBlockNoUpdate(pos.x, pos.y, pos.z, *block);
// }
}
return true;
}

bool PlayerData::setBlockSimple(
BlockSource* blockSource,
class EvalFunctions& funcs,
std::unordered_map<std::string, double> const& var,
const BlockPos& pos,
Block* block,
Block* exblock) {
bool PlayerData::setBlockSimple(BlockSource* blockSource,
class EvalFunctions& funcs,
std::unordered_map<std::string, double> const& var,
const BlockPos& pos,
Block* block,
Block* exblock) {
if (gMask != "") {
if (cpp_eval::eval<double>(gMask, var, funcs) <= 0.5) {
return false;
Expand All @@ -154,7 +186,7 @@ namespace worldedit {
CommandUtils::clearBlockEntityContents(*blockSource, pos);
blockSource->setExtraBlock(pos, *BedrockBlocks::mAir, 16 + updateArg);
// if (updateExArg % 2 == 1) {
blockSource->setBlock(pos, *block, updateArg, nullptr, nullptr);
blockSource->setBlock(pos, *block, updateArg, nullptr, nullptr);
// } else {
// blockSource->setBlockNoUpdate(pos.x, pos.y, pos.z, *block);
// }
Expand All @@ -163,10 +195,9 @@ namespace worldedit {
blockSource->setExtraBlock(pos, *exblock, 16 + updateArg);
}
} else {
blockSource->setExtraBlock(pos, *VanillaBlocks::mFlowingWater,
16 + updateArg);
blockSource->setExtraBlock(pos, *VanillaBlocks::mFlowingWater, 16 + updateArg);
// if (updateExArg % 2 == 1) {
blockSource->setBlock(pos, *block, updateArg, nullptr, nullptr);
blockSource->setBlock(pos, *block, updateArg, nullptr, nullptr);
// } else {
// blockSource->setBlockNoUpdate(pos.x, pos.y, pos.z, *block);
// }
Expand All @@ -175,13 +206,15 @@ namespace worldedit {
}

Clipboard* PlayerData::getNextHistory() {
std::cout << "getNextHistory" << std::endl;
historyFlag2 += 1;
historyFlag2 %= maxHistoryLength;
historyFlag1 = historyFlag2;
return &historyList[historyFlag2];
}

std::pair<Clipboard*, int> PlayerData::getUndoHistory() {
std::cout << "getUndoHistory" << std::endl;
std::pair<Clipboard*, int> result = {nullptr, -1};
int maximum = (historyFlag1 + 1) % maxHistoryLength;
int current = historyFlag2;
Expand All @@ -204,6 +237,7 @@ namespace worldedit {
}

std::pair<Clipboard*, int> PlayerData::getRedoHistory() {
std::cout << "getRedoHistory" << std::endl;
std::pair<Clipboard*, int> result = {nullptr, -1};
int maximum = (historyFlag1 + 1) % maxHistoryLength;
int current = historyFlag2 + 1;
Expand All @@ -228,28 +262,32 @@ namespace worldedit {
float maxDistance,
bool ignoreBorderBlocks,
bool fullOnly) {
Player* player = Global<Level>->getPlayer(xuid);
try {
Player* player = Global<Level>->getPlayer(xuid);

auto* bs = &player->getRegion();
auto pos = player->getCameraPos();
auto viewVec = player->getViewVector(1.0f);
auto viewPos = pos + (viewVec * maxDistance);
int maxDisManhattan = (int)((maxDistance + 1) * 2);
HitResult result = const_cast<BlockSource*>(bs)->clip(pos, viewPos, includeLiquid, solidOnly, maxDisManhattan,
ignoreBorderBlocks, fullOnly);
if (result.isHit() || (includeLiquid && result.isHitLiquid())) {
BlockPos bpos{};
if (includeLiquid && result.isHitLiquid()) {
bpos = result.getLiquidPos();
face = result.getLiquidFacing();
} else {
bpos = result.getBlockPos();
face = result.getFacing();
auto* bs = &player->getRegion();
auto pos = player->getCameraPos();
auto viewVec = player->getViewVector(1.0f);
auto viewPos = pos + (viewVec * maxDistance);
int maxDisManhattan = (int)((maxDistance + 1) * 2);
HitResult result = const_cast<BlockSource*>(bs)->clip(pos, viewPos, includeLiquid, solidOnly,
maxDisManhattan, ignoreBorderBlocks, fullOnly);
if (result.isHit() || (includeLiquid && result.isHitLiquid())) {
BlockPos bpos{};
if (includeLiquid && result.isHitLiquid()) {
bpos = result.getLiquidPos();
face = result.getLiquidFacing();
} else {
bpos = result.getBlockPos();
face = result.getFacing();
}
if (pos.toBlockPos().distanceTo(bpos) < maxDistance)
return Level::getBlockInstance(bpos, player->getDimensionId());
}
// auto block = const_cast<Block*>(&bs.getBlock(bpos));
return Level::getBlockInstance(bpos, player->getDimensionId());
return BlockInstance::Null;
} catch (...) {
return BlockInstance::Null;
}
return BlockInstance::Null;
}

BlockInstance PlayerData::getBlockFromViewVector(bool includeLiquid,
Expand Down
Loading

0 comments on commit 090b0ec

Please sign in to comment.