diff --git a/src/main/java/com/pqvel1l/structorycraft/AltarStructureChecker.java b/src/main/java/com/pqvel1l/structorycraft/AltarStructureChecker.java new file mode 100644 index 0000000..1e78c2d --- /dev/null +++ b/src/main/java/com/pqvel1l/structorycraft/AltarStructureChecker.java @@ -0,0 +1,62 @@ +package com.pqvel1l.structorycraft; + +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtIo; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.state.property.Property; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Map; + +public class AltarStructureChecker { + + public boolean checkStructure(World world, BlockPos pos) { + // Загрузить NBT файл + NbtCompound nbtData = loadNBTFile("data/structorycraft/structures/tier1.nbt"); + + // Пройтись по всем блокам в структуре и проверить их + for (String key : nbtData.getKeys()) { + NbtCompound blockNBT = nbtData.getCompound(key); + BlockPos blockPos = BlockPos.fromLong(blockNBT.getLong("pos")); + BlockState expectedBlockState = createBlockStateFromNbt(blockNBT.getCompound("state")); + + BlockState actualBlockState = world.getBlockState(pos.add(blockPos)); + if (!actualBlockState.equals(expectedBlockState)) { + return false; + } + } + return true; + } + + private NbtCompound loadNBTFile(String filePath) { + try (FileInputStream fileInputStream = new FileInputStream(new File(filePath))) { + return NbtIo.readCompressed(fileInputStream); + } catch (IOException e) { + e.printStackTrace(); + return new NbtCompound(); + } + } + + private BlockState createBlockStateFromNbt(NbtCompound nbt) { + Block block = Block.getBlockFromItem(net.minecraft.item.Item.byRawId(nbt.getInt("id"))); + BlockState state = block.getDefaultState(); + + for (String key : nbt.getKeys()) { + Property property = block.getStateManager().getProperty(key); + if (property != null) { + state = applyProperty(state, property, nbt.getString(key)); + } + } + + return state; + } + + private > BlockState applyProperty(BlockState state, Property property, String value) { + return state.with(property, property.parse(value).orElseThrow(IllegalArgumentException::new)); + } +} diff --git a/src/main/java/com/pqvel1l/structorycraft/StructoryCraftMod.java b/src/main/java/com/pqvel1l/structorycraft/StructoryCraftMod.java index aa18950..50112ce 100644 --- a/src/main/java/com/pqvel1l/structorycraft/StructoryCraftMod.java +++ b/src/main/java/com/pqvel1l/structorycraft/StructoryCraftMod.java @@ -1,6 +1,8 @@ package com.pqvel1l.structorycraft; import net.fabricmc.api.ModInitializer; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -9,7 +11,13 @@ public class StructoryCraftMod implements ModInitializer { public static final String MODID = "structorycraft"; public static final Logger LOGGER = LoggerFactory.getLogger(MODID); + + @Override public void onInitialize() { } + public boolean checkAltarStructure(World world, BlockPos pos) { + AltarStructureChecker checker = new AltarStructureChecker(); + return checker.checkStructure(world, pos); + } } \ No newline at end of file diff --git a/src/main/resources/data/structorycraft/structures/tier1.nbt b/src/main/resources/data/structorycraft/structures/tier1.nbt new file mode 100644 index 0000000..0d4fd89 Binary files /dev/null and b/src/main/resources/data/structorycraft/structures/tier1.nbt differ