diff --git a/build.gradle b/build.gradle index 85332e6..65469a2 100644 --- a/build.gradle +++ b/build.gradle @@ -15,6 +15,7 @@ println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty ' minecraft { mappings channel: 'official', version: '1.19.2' + accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg') runs { client { diff --git a/src/main/java/com/kikis/ptdyeplus/kubejs/Utils.java b/src/main/java/com/kikis/ptdyeplus/kubejs/Utils.java index 269b706..1db9043 100644 --- a/src/main/java/com/kikis/ptdyeplus/kubejs/Utils.java +++ b/src/main/java/com/kikis/ptdyeplus/kubejs/Utils.java @@ -1,10 +1,28 @@ package com.kikis.ptdyeplus.kubejs; +import com.kikis.ptdyeplus.PtdyePlus; +import com.simibubi.create.content.contraptions.mounted.CartAssemblerBlockEntity; +import com.simibubi.create.foundation.utility.NBTHelper; import net.minecraft.client.KeyMapping; import net.minecraft.client.Minecraft; +import net.minecraft.core.BlockPos; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.NbtUtils; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.chunk.HashMapPalette; +import net.minecraft.world.phys.AABB; +import net.minecraftforge.registries.GameData; import org.lwjgl.glfw.GLFW; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Objects; +import java.util.Set; public class Utils { @@ -47,4 +65,77 @@ private static ArrayList getKeyMappings() { return mappings; } + /** + * Creates a contraption nbt from given block positions + * @param level The level of the given block positions are in + * @param blockPositions The list of block positions to add to the contraption. In world space; i.e. not locale space + * @param anchor The position the contraption should move around. In world space; i.e. not locale space + * @return Tag for the created contraption + */ + public CompoundTag createContraptionNBTFrom(Level level, Set blockPositions, BlockPos anchor, AABB bounds) { + CompoundTag contraptionNbt = new CompoundTag(); + contraptionNbt.putString("Type", "mounted"); + contraptionNbt.put("Blocks", createBlockCompound(level, blockPositions, anchor)); + contraptionNbt.put("Anchor", NbtUtils.writeBlockPos(new BlockPos(0, 0, 0))); + contraptionNbt.put("BoundsFront", NBTHelper.writeAABB(bounds)); + NBTHelper.writeEnum(contraptionNbt, "RotationMode", CartAssemblerBlockEntity.CartMovementMode.ROTATION_LOCKED); + + // defaults + contraptionNbt.put("Actors", new ListTag()); + contraptionNbt.putBoolean("BottomlessSupply", false); + contraptionNbt.put("DisabledActors", new ListTag()); + contraptionNbt.put("FluidStorage", new ListTag()); + contraptionNbt.put("Interactors", new ListTag()); + contraptionNbt.put("Passengers", new ListTag()); + contraptionNbt.put("Seats", new ListTag()); + contraptionNbt.putBoolean("Stalled", false); + contraptionNbt.put("Storage", new ListTag()); + contraptionNbt.put("SubContraptions", new ListTag()); + contraptionNbt.put("Superglue", new ListTag()); + return contraptionNbt; + } + + @SuppressWarnings("UnstableApiUsage") + private static CompoundTag createBlockCompound(Level level, Set blockPositions, BlockPos anchor) { + HashMapPalette palette = new HashMapPalette<>(GameData.getBlockStateIDMap(), 16,(i, s) -> { + throw new IllegalStateException("Palette Map index exceeded maximum"); + }); + ListTag blockList = new ListTag(); + + for (BlockPos blockPos : blockPositions) { + BlockState state = level.getBlockState(blockPos); + int id = palette.idFor(state); + CompoundTag c = new CompoundTag(); + c.putLong("Pos", (blockPos.subtract(anchor)).asLong()); + c.putInt("State", id); + BlockEntity blockEntity = level.getBlockEntity(blockPos); + if (blockEntity != null) { + c.put("Data", blockEntity.saveWithoutMetadata()); + } + blockList.add(c); + } + + ListTag paletteNBT = new ListTag(); + for (BlockState blockState : palette.getEntries()) + paletteNBT.add(NbtUtils.writeBlockState(blockState)); + + CompoundTag compound = new CompoundTag(); + compound.put("Palette", paletteNBT); + compound.put("BlockList", blockList); + + return compound; + } + + /** + * Creates directory if it doesn't already exist + * @param directoryPath Target directory to create + */ + public void createDirectory(String directoryPath) { + try { + Files.createDirectory(Paths.get(directoryPath)); + } catch (IOException e) { + PtdyePlus.LOGGER.warn("Could not create directory. Could it already exist? Directory: {}", directoryPath); + } + } + } diff --git a/src/main/resources/META-INF/accesstransformer.cfg b/src/main/resources/META-INF/accesstransformer.cfg new file mode 100644 index 0000000..953d282 --- /dev/null +++ b/src/main/resources/META-INF/accesstransformer.cfg @@ -0,0 +1 @@ +public net.minecraft.world.level.chunk.PaletteResize \ No newline at end of file