diff --git a/winterly-common/src/main/java/ru/pinkgoosik/winterly/data/CachedFlowers.java b/winterly-common/src/main/java/ru/pinkgoosik/winterly/data/CachedFlowers.java index 2387d27..9333391 100644 --- a/winterly-common/src/main/java/ru/pinkgoosik/winterly/data/CachedFlowers.java +++ b/winterly-common/src/main/java/ru/pinkgoosik/winterly/data/CachedFlowers.java @@ -2,48 +2,27 @@ import org.jetbrains.annotations.Nullable; -import java.util.LinkedHashMap; -import java.util.Map; import net.minecraft.core.BlockPos; -import net.minecraft.resources.ResourceKey; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; public class CachedFlowers { public static CachedFlowers instance = new CachedFlowers(); - public Map, Map> data = new LinkedHashMap<>(); - public static Block getFlower(Level world, BlockPos pos) { return instance.getFlowerImpl(world, pos); } @Nullable public Block getFlowerImpl(Level world, BlockPos pos) { - var map = data.get(world.dimension()); - if(map != null) { - return map.get(pos); - } - else { - var newMap = new LinkedHashMap(); - data.put(world.dimension(), newMap); - return null; - } - } + throw new UnsupportedOperationException(); + } public static void cacheFlower(Level world, BlockPos pos, Block flower) { instance.cacheFlowerImpl(world, pos, flower); } public void cacheFlowerImpl(Level world, BlockPos pos, Block flower) { - var map = data.get(world.dimension()); - if(map != null) { - map.put(pos, flower); - } - else { - var newMap = new LinkedHashMap(); - newMap.put(pos, flower); - data.put(world.dimension(), newMap); - } + throw new UnsupportedOperationException(); } } diff --git a/winterly-fabric/build.gradle.kts b/winterly-fabric/build.gradle.kts index 8f86a62..8d50647 100644 --- a/winterly-fabric/build.gradle.kts +++ b/winterly-fabric/build.gradle.kts @@ -40,11 +40,7 @@ dependencies { // modLocalRuntime("dev.emi:trinkets:${rootProject.property("trinkets_version")}") modApi(files("trinkets-3.10.0.jar")) modImplementation("dev.onyxstudios.cardinal-components-api:cardinal-components-entity:${rootProject.property("cca_version")}") - modImplementation("dev.onyxstudios.cardinal-components-api:cardinal-components-base:${rootProject.property("cca_version")}") - include("dev.onyxstudios.cardinal-components-api:cardinal-components-base:${rootProject.property("cca_version")}") - modImplementation("dev.onyxstudios.cardinal-components-api:cardinal-components-world:${rootProject.property("cca_version")}") - include("dev.onyxstudios.cardinal-components-api:cardinal-components-world:${rootProject.property("cca_version")}") modApi("io.wispforest:owo-lib:${rootProject.property("owo_lib")}") modApi("me.shedaniel.cloth:cloth-config-fabric:${rootProject.property("cloth_config")}") { exclude("net.fabricmc.fabric-api") } diff --git a/winterly-fabric/src/main/java/ru/pinkgoosik/winterly/fabric/WinterlyFabric.java b/winterly-fabric/src/main/java/ru/pinkgoosik/winterly/fabric/WinterlyFabric.java index e33b2c5..a255f27 100644 --- a/winterly-fabric/src/main/java/ru/pinkgoosik/winterly/fabric/WinterlyFabric.java +++ b/winterly-fabric/src/main/java/ru/pinkgoosik/winterly/fabric/WinterlyFabric.java @@ -2,14 +2,12 @@ import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup; -import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents; -import net.fabricmc.loader.api.FabricLoader; import net.minecraft.core.Registry; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.network.chat.Component; import net.minecraft.world.item.CreativeModeTab; import ru.pinkgoosik.winterly.Winterly; -import ru.pinkgoosik.winterly.fabric.compat.WinterlyOwoLibIntegration; +import ru.pinkgoosik.winterly.fabric.data.WinterlyAttachedData; import ru.pinkgoosik.winterly.fabric.registry.WinterlyBlockEntities; import ru.pinkgoosik.winterly.fabric.registry.WinterlyBlocks; import ru.pinkgoosik.winterly.registry.CommonWinterlyBlocks; @@ -28,6 +26,7 @@ public void onInitialize() { WinterlyBlocks.init(); WinterlyBlockEntities.init(); WinterlyFeatures.init(); + WinterlyAttachedData.init(); // if(FabricLoader.getInstance().isModLoaded("owo")) { // WinterlyOwoLibIntegration.initItemGroup(); diff --git a/winterly-fabric/src/main/java/ru/pinkgoosik/winterly/fabric/data/WinterlyAttachedData.java b/winterly-fabric/src/main/java/ru/pinkgoosik/winterly/fabric/data/WinterlyAttachedData.java new file mode 100644 index 0000000..afcf292 --- /dev/null +++ b/winterly-fabric/src/main/java/ru/pinkgoosik/winterly/fabric/data/WinterlyAttachedData.java @@ -0,0 +1,65 @@ +package ru.pinkgoosik.winterly.fabric.data; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Maps; +import com.mojang.serialization.Codec; +import net.fabricmc.fabric.api.attachment.v1.AttachmentRegistry; +import net.fabricmc.fabric.api.attachment.v1.AttachmentType; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerChunkEvents; +import net.minecraft.core.BlockPos; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Block; +import org.jetbrains.annotations.Nullable; +import ru.pinkgoosik.winterly.Winterly; +import ru.pinkgoosik.winterly.data.CachedFlowers; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class WinterlyAttachedData { + public static final AttachmentType> CACHED_FLOWERS = AttachmentRegistry.createPersistent(Winterly.id("cached_flowers"), Codec.unboundedMap(Codec.STRING, Codec.STRING)); + + public static void init() { + ServerChunkEvents.CHUNK_LOAD.register((world, chunk) -> { + var map = chunk.getAttached(CACHED_FLOWERS); + + if(map != null) { + if(map instanceof ImmutableMap) { + chunk.setAttached(CACHED_FLOWERS, Maps.newLinkedHashMap(map)); + } + } + }); + + CachedFlowers.instance = new CachedFlowers() { + @Override + public @Nullable Block getFlowerImpl(Level world, BlockPos pos) { + var map = world.getChunkAt(pos).getAttached(CACHED_FLOWERS); + if(map != null) { + var block = map.get(pos.toShortString()); + if(block != null) return BuiltInRegistries.BLOCK.get(ResourceLocation.parse(block)); + } + else { + world.getChunkAt(pos).setAttached(CACHED_FLOWERS, new LinkedHashMap()); + } + return null; + } + + @Override + public void cacheFlowerImpl(Level world, BlockPos pos, Block flower) { + Map map = world.getChunkAt(pos).getAttached(CACHED_FLOWERS); + + if(map != null) { + map.put(pos.toShortString(), BuiltInRegistries.BLOCK.getKey(flower).toString()); + } + else { + map = new LinkedHashMap<>(); + map.put(pos.toShortString(), BuiltInRegistries.BLOCK.getKey(flower).toString()); + world.getChunkAt(pos).setAttached(CACHED_FLOWERS, map); + } + + } + }; + } +} diff --git a/winterly-fabric/src/main/java/ru/pinkgoosik/winterly/fabric/data/WinterlyComponents.java b/winterly-fabric/src/main/java/ru/pinkgoosik/winterly/fabric/data/WinterlyComponents.java deleted file mode 100644 index 10ae3ea..0000000 --- a/winterly-fabric/src/main/java/ru/pinkgoosik/winterly/fabric/data/WinterlyComponents.java +++ /dev/null @@ -1,16 +0,0 @@ -package ru.pinkgoosik.winterly.fabric.data; - -import org.ladysnake.cca.api.v3.component.ComponentKey; -import org.ladysnake.cca.api.v3.component.ComponentRegistryV3; -import org.ladysnake.cca.api.v3.world.WorldComponentFactoryRegistry; -import org.ladysnake.cca.api.v3.world.WorldComponentInitializer; -import ru.pinkgoosik.winterly.Winterly; - -public class WinterlyComponents implements WorldComponentInitializer { - public static final ComponentKey WORLD_DATA = ComponentRegistryV3.INSTANCE.getOrCreate(Winterly.id("world_data"), WorldData.class); - - @Override - public void registerWorldComponentFactories(WorldComponentFactoryRegistry registry) { - registry.register(WORLD_DATA, WorldData::new); - } -} diff --git a/winterly-fabric/src/main/java/ru/pinkgoosik/winterly/fabric/data/WorldData.java b/winterly-fabric/src/main/java/ru/pinkgoosik/winterly/fabric/data/WorldData.java deleted file mode 100644 index a35207e..0000000 --- a/winterly-fabric/src/main/java/ru/pinkgoosik/winterly/fabric/data/WorldData.java +++ /dev/null @@ -1,60 +0,0 @@ -package ru.pinkgoosik.winterly.fabric.data; - -import java.util.LinkedHashMap; -import net.minecraft.core.BlockPos; -import net.minecraft.core.HolderLookup; -import net.minecraft.core.registries.BuiltInRegistries; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.level.Level; -import org.ladysnake.cca.api.v3.component.ComponentV3; -import ru.pinkgoosik.winterly.data.CachedFlowers; - -@SuppressWarnings("NullableProblems") -public class WorldData implements ComponentV3 { - - public Level world; - - public WorldData(Level world) { - this.world = world; - } - - @Override - public void readFromNbt(CompoundTag tag, HolderLookup.Provider registryLookup) { - CachedFlowers.instance.data = new LinkedHashMap<>(); - var cachedFlowerNbt = tag.getCompound("cachedFlower"); - - if(!cachedFlowerNbt.isEmpty()) { - int size = cachedFlowerNbt.getInt("size"); - - for(int i = 0; i < size; i++) { - CompoundTag entry = cachedFlowerNbt.getCompound(String.valueOf(i)); - BlockPos pos = new BlockPos(entry.getInt("x"), entry.getInt("y"), entry.getInt("z")); - var block = BuiltInRegistries.BLOCK.getOptional(ResourceLocation.parse(entry.getString("block"))); - block.ifPresent(bl -> CachedFlowers.cacheFlower(world, pos, bl)); - } - } - } - - @Override - public void writeToNbt(CompoundTag tag, HolderLookup.Provider registryLookup) { - CompoundTag cachedFlowerNbt = new CompoundTag(); - var map = CachedFlowers.instance.data.get(world.dimension()); - if(map != null) { - cachedFlowerNbt.putInt("size", map.size()); - int index = -1; - - for(var entry : map.entrySet()) { - index++; - CompoundTag entryNbt = new CompoundTag(); - entryNbt.putInt("x", entry.getKey().getX()); - entryNbt.putInt("y", entry.getKey().getY()); - entryNbt.putInt("z", entry.getKey().getZ()); - entryNbt.putString("block", BuiltInRegistries.BLOCK.getKey(entry.getValue()).toString()); - cachedFlowerNbt.put(String.valueOf(index), entryNbt); - } - tag.put("cachedFlower", cachedFlowerNbt); - } - - } -} diff --git a/winterly-fabric/src/main/resources/fabric.mod.json b/winterly-fabric/src/main/resources/fabric.mod.json index f87243e..cc5a90e 100644 --- a/winterly-fabric/src/main/resources/fabric.mod.json +++ b/winterly-fabric/src/main/resources/fabric.mod.json @@ -23,9 +23,6 @@ "client": [ "ru.pinkgoosik.winterly.fabric.client.WinterlyFabricClient" ], - "cardinal-components": [ - "ru.pinkgoosik.winterly.fabric.data.WinterlyComponents" - ], "modmenu": [ "ru.pinkgoosik.winterly.fabric.compat.WinterlyModMenuIntegration" ] @@ -41,10 +38,7 @@ "modmenu.curseforge": "https://curseforge.com/minecraft/mc-mods/winterly", "modmenu.modrinth": "https://modrinth.com/mod/winterly" } - }, - "cardinal-components": [ - "winterly:world_data" - ] + } }, "depends": { "fabricloader": "*", diff --git a/winterly-neoforge/src/main/java/ru/pinkgoosik/winterly/neoforge/WinterlyNeoforge.java b/winterly-neoforge/src/main/java/ru/pinkgoosik/winterly/neoforge/WinterlyNeoforge.java index 66fc7e2..fbda63e 100644 --- a/winterly-neoforge/src/main/java/ru/pinkgoosik/winterly/neoforge/WinterlyNeoforge.java +++ b/winterly-neoforge/src/main/java/ru/pinkgoosik/winterly/neoforge/WinterlyNeoforge.java @@ -11,6 +11,7 @@ import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent; import net.neoforged.fml.loading.FMLEnvironment; import net.neoforged.neoforge.event.BuildCreativeModeTabContentsEvent; +import net.neoforged.neoforge.registries.NeoForgeRegistries; import net.neoforged.neoforge.registries.RegisterEvent; import ru.pinkgoosik.winterly.neoforge.client.WinterlyNeoforgeClient; import ru.pinkgoosik.winterly.neoforge.data.WinterlyDataAttachments; @@ -29,9 +30,6 @@ public class WinterlyNeoforge { public WinterlyNeoforge(IEventBus bus) { - WinterlyFeatures.init(bus); - WinterlyDataAttachments.init(bus); - bus.addListener(this::register); bus.addListener(this::buildCreativeTab); bus.addListener(this::commonSetup); @@ -49,6 +47,8 @@ public void register(RegisterEvent event) { registry.register(Winterly.id("items"), CreativeModeTab.builder().icon(BuiltInRegistries.ITEM.get(Winterly.id("snowguy"))::getDefaultInstance).title(Component.translatable("itemGroup.winterly.items")).build()); }); + event.register(Registries.FEATURE, WinterlyFeatures::init); + event.register(NeoForgeRegistries.ATTACHMENT_TYPES.key(), WinterlyDataAttachments::init); event.register(Registries.ITEM, registry -> ITEMS.forEach((id, sup) -> registry.register(id, sup.get()))); event.register(Registries.BLOCK, registry -> CommonWinterlyBlocks.BLOCKS.forEach((id, sup) -> registry.register(id, sup.get()))); event.register(Registries.ITEM, registry -> CommonWinterlyBlocks.BLOCKS.forEach((id, sup) -> registry.register(id, new BlockItem(BuiltInRegistries.BLOCK.get(id), new Item.Properties())))); diff --git a/winterly-neoforge/src/main/java/ru/pinkgoosik/winterly/neoforge/data/WorldData.java b/winterly-neoforge/src/main/java/ru/pinkgoosik/winterly/neoforge/data/ChunkData.java similarity index 88% rename from winterly-neoforge/src/main/java/ru/pinkgoosik/winterly/neoforge/data/WorldData.java rename to winterly-neoforge/src/main/java/ru/pinkgoosik/winterly/neoforge/data/ChunkData.java index c7521bf..85c237d 100644 --- a/winterly-neoforge/src/main/java/ru/pinkgoosik/winterly/neoforge/data/WorldData.java +++ b/winterly-neoforge/src/main/java/ru/pinkgoosik/winterly/neoforge/data/ChunkData.java @@ -14,14 +14,14 @@ import java.util.Map; @SuppressWarnings("NullableProblems") -public class WorldData { +public class ChunkData { public Map cachedFlowers = new LinkedHashMap<>(); - public static class WorldDataIAttachmentSerializer implements IAttachmentSerializer { + public static class ChunkDataIAttachmentSerializer implements IAttachmentSerializer { @Override - public WorldData read(IAttachmentHolder holder, Tag tag, HolderLookup.Provider lookup) { - WorldData data = new WorldData(); + public ChunkData read(IAttachmentHolder holder, Tag tag, HolderLookup.Provider lookup) { + ChunkData data = new ChunkData(); var cachedFlowersNbt = ((CompoundTag) tag).getCompound("cachedFlowers"); @@ -40,7 +40,7 @@ public WorldData read(IAttachmentHolder holder, Tag tag, HolderLookup.Provider l } @Override - public Tag write(WorldData data, HolderLookup.Provider lookup) { + public Tag write(ChunkData data, HolderLookup.Provider lookup) { CompoundTag cachedFlowersNbt = new CompoundTag(); cachedFlowersNbt.putInt("size", data.cachedFlowers.size()); int index = -1; diff --git a/winterly-neoforge/src/main/java/ru/pinkgoosik/winterly/neoforge/data/WinterlyDataAttachments.java b/winterly-neoforge/src/main/java/ru/pinkgoosik/winterly/neoforge/data/WinterlyDataAttachments.java index f92262c..bdb9a6b 100644 --- a/winterly-neoforge/src/main/java/ru/pinkgoosik/winterly/neoforge/data/WinterlyDataAttachments.java +++ b/winterly-neoforge/src/main/java/ru/pinkgoosik/winterly/neoforge/data/WinterlyDataAttachments.java @@ -3,37 +3,30 @@ import net.minecraft.core.BlockPos; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; -import net.neoforged.bus.api.IEventBus; import net.neoforged.neoforge.attachment.AttachmentType; -import net.neoforged.neoforge.registries.DeferredRegister; -import net.neoforged.neoforge.registries.NeoForgeRegistries; +import net.neoforged.neoforge.registries.RegisterEvent; import org.jetbrains.annotations.Nullable; import ru.pinkgoosik.winterly.Winterly; import ru.pinkgoosik.winterly.data.CachedFlowers; -import java.util.function.Supplier; - public class WinterlyDataAttachments { - private static final DeferredRegister> ATTACHMENT_TYPES = DeferredRegister.create(NeoForgeRegistries.ATTACHMENT_TYPES, Winterly.MOD_ID); + private static AttachmentType CHUNK_DATA; - private static final Supplier> WORLD_DATA = ATTACHMENT_TYPES.register( - "world_data", () -> AttachmentType.builder(WorldData::new).serialize(new WorldData.WorldDataIAttachmentSerializer()).build() - ); + public static void init(RegisterEvent.RegisterHelper> registry) { + CHUNK_DATA = AttachmentType.builder(ChunkData::new).serialize(new ChunkData.ChunkDataIAttachmentSerializer()).build(); - public static void init(IEventBus eventBus) { + registry.register(Winterly.id("chunk_data"), CHUNK_DATA); CachedFlowers.instance = new CachedFlowers() { @Override public @Nullable Block getFlowerImpl(Level world, BlockPos pos) { - return world.getData(WORLD_DATA).cachedFlowers.get(pos); + return world.getChunkAt(pos).getData(CHUNK_DATA).cachedFlowers.get(pos); } @Override public void cacheFlowerImpl(Level world, BlockPos pos, Block flower) { - world.getData(WORLD_DATA).cachedFlowers.put(pos, flower); + world.getChunkAt(pos).getData(CHUNK_DATA).cachedFlowers.put(pos, flower); } }; - - ATTACHMENT_TYPES.register(eventBus); } } diff --git a/winterly-neoforge/src/main/java/ru/pinkgoosik/winterly/neoforge/registry/WinterlyFeatures.java b/winterly-neoforge/src/main/java/ru/pinkgoosik/winterly/neoforge/registry/WinterlyFeatures.java index ae28233..ee5298b 100644 --- a/winterly-neoforge/src/main/java/ru/pinkgoosik/winterly/neoforge/registry/WinterlyFeatures.java +++ b/winterly-neoforge/src/main/java/ru/pinkgoosik/winterly/neoforge/registry/WinterlyFeatures.java @@ -1,20 +1,15 @@ package ru.pinkgoosik.winterly.neoforge.registry; -import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.world.level.levelgen.feature.Feature; -import net.neoforged.bus.api.IEventBus; -import net.neoforged.neoforge.registries.DeferredRegister; +import net.neoforged.neoforge.registries.RegisterEvent; +import ru.pinkgoosik.winterly.Winterly; import ru.pinkgoosik.winterly.worldgen.CryomarbleFeature; import ru.pinkgoosik.winterly.worldgen.UndergroundIcicleFeature; public class WinterlyFeatures { - public static final DeferredRegister> REGISTERER = DeferredRegister.create(BuiltInRegistries.FEATURE, "winterly"); - - public static void init(IEventBus eventBus) { - REGISTERER.register("underground_icicle", UndergroundIcicleFeature::new); - REGISTERER.register("cryomarble", CryomarbleFeature::new); - - REGISTERER.register(eventBus); + public static void init(RegisterEvent.RegisterHelper> registry) { + registry.register(Winterly.id("underground_icicle"), new UndergroundIcicleFeature()); + registry.register(Winterly.id("cryomarble"), new CryomarbleFeature()); } }