Skip to content

Commit

Permalink
feat: adds animation kubejs functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jasperalani authored Jan 25, 2024
2 parents ee5fb13 + 4e5722f commit e9a032b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
91 changes: 91 additions & 0 deletions src/main/java/com/kikis/ptdyeplus/kubejs/Utils.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -47,4 +65,77 @@ private static ArrayList<KeyMapping> 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<BlockPos> 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<BlockPos> blockPositions, BlockPos anchor) {
HashMapPalette<BlockState> 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);
}
}

}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public net.minecraft.world.level.chunk.PaletteResize

0 comments on commit e9a032b

Please sign in to comment.