Skip to content

Commit

Permalink
fix: Fix vanilla dimension can't create when the number of dimensions…
Browse files Browse the repository at this point in the history
… exceeds nine
  • Loading branch information
quizhizhe committed Jun 2, 2024
1 parent 1fdc069 commit 88d1f4c
Show file tree
Hide file tree
Showing 9 changed files with 440 additions and 13 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.3.1] - 2024-06-02

### Fix

- Fix vanilla dimension can't create when the number of dimensions exceeds nine.

## [0.3.0] - 2024-06-01

### Added
Expand Down Expand Up @@ -53,7 +59,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Release the first version

[Unreleased]: https://github.com/LiteLDev/MoreDimensions/compare/v0.3.0...HEAD
[Unreleased]: https://github.com/LiteLDev/MoreDimensions/compare/v0.3.1...HEAD
[0.3.1]: https://github.com/LiteLDev/MoreDimensions/compare/v0.3.0...v0.3.1
[0.3.0]: https://github.com/LiteLDev/MoreDimensions/compare/v0.2.1...v0.3.0
[0.2.1]: https://github.com/LiteLDev/MoreDimensions/compare/v0.2.0...v0.2.1
[0.2.0]: https://github.com/LiteLDev/MoreDimensions/compare/v0.1.2...v0.2.0
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"name": "${pluginName}",
"entry": "${pluginFile}",
"type": "native",
"version": "0.3.0",
"version": "0.3.1",
"author": "LiteLDev"
}
12 changes: 3 additions & 9 deletions src/more_dimensions/api/dimension/CustomDimensionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ LL_TYPE_STATIC_HOOK(
Bedrock::Result<DimensionType>,
Bedrock::Result<int>&& dim
) {
if (!dim || *dim <= 2) {
return origin(std::move(dim));
}
if (!VanillaDimensions::DimensionMap.mLeft.contains(*dim)) {
return VanillaDimensions::Undefined;
}
Expand All @@ -66,7 +63,6 @@ LL_TYPE_STATIC_HOOK(
DimensionType,
int dimId
) {
if (dimId <= 2) return origin(dimId);
if (!VanillaDimensions::DimensionMap.mLeft.contains(dimId)) {
return VanillaDimensions::Undefined;
}
Expand All @@ -93,7 +89,6 @@ LL_TYPE_STATIC_HOOK(
std::string const,
DimensionType const& dim
) {
if (dim <= 2) return origin(dim);
return VanillaDimensions::DimensionMap.mLeft.at(dim);
}

Expand Down Expand Up @@ -168,8 +163,7 @@ CustomDimensionManager::CustomDimensionManager() : impl(std::make_unique<Impl>()
name,
Impl::DimensionInfo{
info.dimId,
*CompoundTag::fromBinaryNbt(ll::string_utils::decompress(ll::base64_utils::decode(info.base64Nbt)))
}
*CompoundTag::fromBinaryNbt(ll::string_utils::decompress(ll::base64_utils::decode(info.base64Nbt)))}
);
}
impl->mNewDimensionId += static_cast<int>(impl->customDimensionMap.size());
Expand Down Expand Up @@ -238,6 +232,7 @@ DimensionType CustomDimensionManager::addDimension(
ll::memory::modify(VanillaDimensions::Undefined, [&](auto& uid) {
uid.id = impl->mNewDimensionId;
loggerMoreDimMag.debug("Set VanillaDimensions::Undefined to {}", uid.id);
loggerMoreDimMag.debug("Now VanillaDimensions::Undefined is {}", VanillaDimensions::Undefined.id);
});

// config
Expand All @@ -248,8 +243,7 @@ DimensionType CustomDimensionManager::addDimension(
dimName,
CustomDimensionConfig::Config::Info{
info.id,
ll::base64_utils::encode(ll::string_utils::compress(info.nbt.toBinaryNbt()))
}
ll::base64_utils::encode(ll::string_utils::compress(info.nbt.toBinaryNbt()))}
);
CustomDimensionConfig::saveConfigFile();
}
Expand Down
82 changes: 81 additions & 1 deletion src/test/TestCustomDimension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ll/api/event/server/ServerStartedEvent.h"
#include "more_dimensions/api/dimension/CustomDimensionManager.h"
#include "more_dimensions/api/dimension/SimpleCustomDimension.h"
#include "test/generator/flat-gen-village/FlatVillageDimension.h"

static bool reg = [] {
using namespace ll::event;
Expand All @@ -17,6 +18,85 @@ static bool reg = [] {
.addDimension<more_dimensions::SimpleCustomDimension>("testNewTheEndDimension", 345, GeneratorType::TheEnd);
more_dimensions::CustomDimensionManager::getInstance()
.addDimension<more_dimensions::SimpleCustomDimension>("testNewVoidDimension", 345, GeneratorType::Void);
more_dimensions::CustomDimensionManager::getInstance()
.addDimension<flat_village_dimension::FlatVillageDimension>("testFlatVillage");
});
return true;
}();
}();


// #include "ll/api/memory/Hook.h"
// #include "mc/world/level/dimension/DimensionManager.h"
// #include "mc/world/level/dimension/DimensionFactory.h"
//
// LL_AUTO_TYPE_INSTANCE_HOOK(
// GetOrCreateDimension,
// HookPriority::Normal,
// DimensionManager,
// "?getOrCreateDimension@DimensionManager@@QEAA?AV?$WeakRef@VDimension@@@@V?$AutomaticID@VDimension@@H@@@Z",
// WeakRef<Dimension>,
// DimensionType dim) {
// std::cout<<"getOrCreateDimension->"<< dim.id <<std::endl;;
// return origin(dim);
// };
//
// LL_AUTO_TYPE_INSTANCE_HOOK(
// DimensonFactory123,
// HookPriority::Normal,
// DimensionFactory,
// "?create@DimensionFactory@@UEBA?AV?$OwnerPtr@VDimension@@@@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z",
// OwnerPtr<Dimension>,
// std::string const &name) {
// std::cout<<"Dimension creative->"<<name<<std::endl;;
// return origin(name);
// };
//
// #include "ll/api/command/CommandHandle.h"
// #include "ll/api/command/CommandRegistrar.h"
// #include "mc/server/commands/CommandOutput.h"
// #include "mc/server/commands/ServerCommands.h"
// #include "ll/api/service/Bedrock.h"
// #include "mc/world/level/Level.h"
// #include "mc/world/level/dimension/DimensionFactory.h"
// #include "mc/world/level/dimension/VanillaDimensions.h"
//
// #include "mc/world/events/ServerInstanceEventCoordinator.h"
//
// struct ParamTest {
// int p1;
// };
//
// LL_AUTO_TYPE_INSTANCE_HOOK(
// registerBuiltinCommands,
// ll::memory::HookPriority::Normal,
// ServerInstanceEventCoordinator,
// &ServerInstanceEventCoordinator::sendServerThreadStarted,
// void,
// ::ServerInstance& ins
//) {
// origin(ins);
//
// auto& cmd = ll::command::CommandRegistrar::getInstance().getOrCreateCommand("t", "test tttttt");
// static auto lambda = [](CommandOrigin const&, CommandOutput& output, ParamTest const& param) {
// output.success("p1: {}", param.p1);
// auto dim = ll::service::getLevel()->getOrCreateDimension(param.p1);
// if (dim.expired()) {
// std::cout<<"他宝贝的,销毁了"<<std::endl;
// } else {
// auto dim_ptr = dim.lock();
// std::cout<<"这是正常的:"<<dim_ptr->mName<<std::endl;
// }
// auto& dimM = ll::service::getLevel()->getDimensionManager();
// for (auto& item: dimM.mDimensions) {
// std::cout<<"Dimension have->" <<item.first.id<<std::endl;
// };
// for (auto item: VanillaDimensions::DimensionMap.mLeft) {
// std::cout<< "Dimension Factory->"<<item.first<<",name:"<<item.second<<std::endl;
// std::cout<< "Dimension Factory->"<<item.first<<",get
// name:"<<VanillaDimensions::toString(item.first)<<std::endl;
// }
// };
// cmd.overload<ParamTest>()
// .required("p1")
// .execute(lambda);
// }
112 changes: 112 additions & 0 deletions src/test/generator/flat-gen-village/FlatVillageDimension.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include "FlatVillageDimension.h"

#include "test/generator/flat-gen-village/FlatVillageGenerator.h"
#include "mc/world/level/BlockSource.h"
#include "mc/world/level/DimensionConversionData.h"
#include "mc/world/level/Level.h"
#include "mc/world/level/LevelSeed64.h"
#include "mc/world/level/chunk/ChunkGeneratorStructureState.h"
#include "mc/world/level/chunk/VanillaLevelChunkUpgrade.h"
#include "mc/world/level/dimension/DimensionBrightnessRamp.h"
#include "mc/world/level/dimension/OverworldBrightnessRamp.h"
#include "mc/world/level/dimension/VanillaDimensions.h"
#include "mc/world/level/levelgen/structure/StructureFeatureRegistry.h"
#include "mc/world/level/levelgen/structure/StructureSetRegistry.h"
#include "mc/world/level/levelgen/structure/VillageFeature.h"


namespace flat_village_dimension {

FlatVillageDimension::FlatVillageDimension(std::string const& name, more_dimensions::DimensionFactoryInfo const& info)
: Dimension(info.level, info.dimId, {-64, 320}, info.scheduler, name) {
// 这里说明下,在DimensionFactoryInfo里面more-dimensions会提供维度id,请不要使用固定维度id,避免id冲突导致维度注册出现异常
mDefaultBrightness.sky = Brightness::MAX;
mSeaLevel = -61;
mHasWeather = true;
mDimensionBrightnessRamp = std::make_unique<OverworldBrightnessRamp>();
mDimensionBrightnessRamp->buildBrightnessRamp();
}

CompoundTag FlatVillageDimension::generateNewData() { return {}; }

std::unique_ptr<WorldGenerator>
FlatVillageDimension::createGenerator(br::worldgen::StructureSetRegistry const& structureSetRegistry) {
std::unique_ptr<WorldGenerator> worldGenerator;
uint seed = 2024;
auto& levelData = getLevel().getLevelData();

// 实例化我们写的Generator类
worldGenerator = std::make_unique<flat_village_generator::FlatVillageGenerator>(
*this,
seed,
levelData.getFlatWorldGeneratorOptions()
);
// structureSetRegistry里面仅有的土径结构村庄生成需要用到,所以我们拿一下
std::vector<std::shared_ptr<const br::worldgen::StructureSet>> structureMap;
for (auto iter = structureSetRegistry.begin(); iter != structureSetRegistry.end(); iter++) {
structureMap.emplace_back(iter->second);
}
worldGenerator->getStructureFeatureRegistry().mChunkGeneratorStructureState.mSeed = seed;
worldGenerator->getStructureFeatureRegistry().mChunkGeneratorStructureState.mSeed64 =
LevelSeed64::fromUnsigned32(seed);

// 这个就相当于在这个生成器里注册结构了
// VillageFeature的第二第三个参数是村庄之间的最大间隔与最小间隔
worldGenerator->getStructureFeatureRegistry().mStructureFeatures.emplace_back(
std::make_unique<VillageFeature>(seed, 34, 8)
);
// 此为必须,一些结构生成相关
worldGenerator->getStructureFeatureRegistry().mChunkGeneratorStructureState =
br::worldgen::ChunkGeneratorStructureState::createFlat(seed, worldGenerator->getBiomeSource(), structureMap);

// 必须调用,初始化生成器
worldGenerator->init();
return std::move(worldGenerator);
}

void FlatVillageDimension::upgradeLevelChunk(ChunkSource& cs, LevelChunk& lc, LevelChunk& generatedChunk) {
auto blockSource = BlockSource(getLevel(), *this, cs, false, true, false);
VanillaLevelChunkUpgrade::_upgradeLevelChunkViaMetaData(lc, generatedChunk, blockSource);
VanillaLevelChunkUpgrade::_upgradeLevelChunkLegacy(lc, blockSource);
}

void FlatVillageDimension::fixWallChunk(ChunkSource& cs, LevelChunk& lc) {
auto blockSource = BlockSource(getLevel(), *this, cs, false, true, false);
VanillaLevelChunkUpgrade::fixWallChunk(lc, blockSource);
}

bool FlatVillageDimension::levelChunkNeedsUpgrade(LevelChunk const& lc) const {
return VanillaLevelChunkUpgrade::levelChunkNeedsUpgrade(lc);
}
void FlatVillageDimension::_upgradeOldLimboEntity(CompoundTag& tag, ::LimboEntitiesVersion vers) {
auto isTemplate = getLevel().getLevelData().isFromWorldTemplate();
return VanillaLevelChunkUpgrade::upgradeOldLimboEntity(tag, vers, isTemplate);
}

std::unique_ptr<ChunkSource>
FlatVillageDimension::_wrapStorageForVersionCompatibility(std::unique_ptr<ChunkSource> cs, ::StorageVersion /*ver*/) {
return cs;
}

Vec3 FlatVillageDimension::translatePosAcrossDimension(Vec3 const& fromPos, DimensionType fromId) const {
Vec3 topos;
VanillaDimensions::convertPointBetweenDimensions(
fromPos,
topos,
fromId,
mId,
getLevel().getDimensionConversionData()
);
constexpr auto clampVal = 32000000.0f - 128.0f;

topos.x = std::clamp(topos.x, -clampVal, clampVal);
topos.z = std::clamp(topos.z, -clampVal, clampVal);

return topos;
}

short FlatVillageDimension::getCloudHeight() const { return 192; }

bool FlatVillageDimension::hasPrecipitationFog() const { return true; }

} // namespace flat_village_dimension
47 changes: 47 additions & 0 deletions src/test/generator/flat-gen-village/FlatVillageDimension.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#pragma once

#include "mc/world/level/dimension/Dimension.h" // 新维度类需要继承的类
#include "more_dimensions/api/dimension/CustomDimensionManager.h" // 引入DimensionFactoryInfo的声明

// 建议是加一个命名空间,避免与其他插件同类名的情况
namespace flat_village_dimension {

class FlatVillageDimension : public Dimension {
public:
// 建议固定这样写,DimensionFactoryInfo类里面提供了Dimension实例化的基本数据,name就是维度名,多维度是维度名区分不同维度
FlatVillageDimension(std::string const& name, more_dimensions::DimensionFactoryInfo const& info);

// 多维度需要的一个方法,参数是你需要处理的数据,比如种子,这里不没有这样的需要,后面说原因
static CompoundTag generateNewData();

// 以下六个是必须重写的函数
// 维度地形的生成器,是本教程主要更改的地方
std::unique_ptr<WorldGenerator> createGenerator(br::worldgen::StructureSetRegistry const&) override;

// 与本教程无关,按照本教程写的就行,无需留意
void upgradeLevelChunk(ChunkSource& chunkSource, LevelChunk& oldLc, LevelChunk& newLc) override;

// 与本教程无关,按照本教程写的就行,无需留意
void fixWallChunk(ChunkSource& cs, LevelChunk& lc) override;

// 与本教程无关,按照本教程写的就行,无需留意
bool levelChunkNeedsUpgrade(LevelChunk const& lc) const override;

// 与本教程无关,按照本教程写的就行,无需留意
void _upgradeOldLimboEntity(CompoundTag& tag, ::LimboEntitiesVersion vers) override;

// 与本教程无关,按照本教程写的就行,无需留意
std::unique_ptr<ChunkSource>
_wrapStorageForVersionCompatibility(std::unique_ptr<ChunkSource> cs, ::StorageVersion ver) override;

// 当你转到这个维度时,坐标怎么转换,比如主世界与地狱的
Vec3 translatePosAcrossDimension(Vec3 const& pos, DimensionType did) const override;

// 云高度,默认是y128,但多维度高度范围是在y-64~320,与主世界相同,重写它,放高些
short getCloudHeight() const override;

// 非必要。下雨时,可视范围的更改
bool hasPrecipitationFog() const override;
};

} // namespace flat_village_dimension
Loading

0 comments on commit 88d1f4c

Please sign in to comment.