Skip to content

Commit

Permalink
refactor data attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
PinkGoosik committed Jul 13, 2024
1 parent 07cf685 commit 1c33dd3
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResourceKey<Level>, Map<BlockPos, Block>> 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<BlockPos, Block>();
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<BlockPos, Block>();
newMap.put(pos, flower);
data.put(world.dimension(), newMap);
}
throw new UnsupportedOperationException();
}
}
4 changes: 0 additions & 4 deletions winterly-fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +26,7 @@ public void onInitialize() {
WinterlyBlocks.init();
WinterlyBlockEntities.init();
WinterlyFeatures.init();
WinterlyAttachedData.init();

// if(FabricLoader.getInstance().isModLoaded("owo")) {
// WinterlyOwoLibIntegration.initItemGroup();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Map<String, String>> 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<String, String>) {
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<String, String>());
}
return null;
}

@Override
public void cacheFlowerImpl(Level world, BlockPos pos, Block flower) {
Map<String, String> 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);
}

}
};
}
}

This file was deleted.

This file was deleted.

8 changes: 1 addition & 7 deletions winterly-fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand All @@ -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": "*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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()))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
import java.util.Map;

@SuppressWarnings("NullableProblems")
public class WorldData {
public class ChunkData {
public Map<BlockPos, Block> cachedFlowers = new LinkedHashMap<>();

public static class WorldDataIAttachmentSerializer implements IAttachmentSerializer<Tag, WorldData> {
public static class ChunkDataIAttachmentSerializer implements IAttachmentSerializer<Tag, ChunkData> {

@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");

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AttachmentType<?>> ATTACHMENT_TYPES = DeferredRegister.create(NeoForgeRegistries.ATTACHMENT_TYPES, Winterly.MOD_ID);
private static AttachmentType<ChunkData> CHUNK_DATA;

private static final Supplier<AttachmentType<WorldData>> WORLD_DATA = ATTACHMENT_TYPES.register(
"world_data", () -> AttachmentType.builder(WorldData::new).serialize(new WorldData.WorldDataIAttachmentSerializer()).build()
);
public static void init(RegisterEvent.RegisterHelper<AttachmentType<?>> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<Feature<?>> 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<Feature<?>> registry) {
registry.register(Winterly.id("underground_icicle"), new UndergroundIcicleFeature());
registry.register(Winterly.id("cryomarble"), new CryomarbleFeature());
}
}

0 comments on commit 1c33dd3

Please sign in to comment.