This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
ScriptCraft/src/main/java/com/thebrokenrail/scriptcraft/api/CustomBlockEntity.java

78 lines
2.4 KiB
Java

package com.thebrokenrail.scriptcraft.api;
import com.thebrokenrail.scriptcraft.quickjs.QuickJSManager;
import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.Tickable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
public class CustomBlockEntity extends BlockEntity implements BlockEntityClientSerializable, Tickable {
private static int newObjID = 0;
private final Identifier id;
private final int objID;
public CustomBlockEntity(BlockEntityType<?> type, Identifier id) {
super(type);
this.id = id;
objID = newObjID++;
QuickJSManager.bridge("CustomBlockEntity.create", id.toString(), (double) objID);
}
@Override
public void fromTag(CompoundTag tag) {
super.fromTag(tag);
QuickJSManager.bridge("CustomBlockEntity.fromTag", id.toString(), (double) objID, tag);
}
@Override
public void fromClientTag(CompoundTag compoundTag) {
fromTag(compoundTag);
}
@Override
public CompoundTag toTag(CompoundTag tag) {
return (CompoundTag) QuickJSManager.bridge("CustomBlockEntity.toTag", id.toString(), (double) objID, super.toTag(tag));
}
@Override
public CompoundTag toClientTag(CompoundTag compoundTag) {
return toTag(compoundTag);
}
@Override
public void tick() {
QuickJSManager.bridge("CustomBlockEntity.tick", id.toString(), (double) objID);
}
@Override
public void setLocation(World world, BlockPos pos) {
super.setLocation(world, pos);
QuickJSManager.bridge("CustomBlockEntity.setLocation", id.toString(), (double) objID, world, (double) pos.getX(), (double) pos.getY(), (double) pos.getZ());
}
@SuppressWarnings("deprecation")
@Override
protected void finalize() throws Throwable {
try {
QuickJSManager.bridge("CustomBlockEntity.free", id.toString(), (double) objID);
} finally {
super.finalize();
}
}
@Override
public void markDirty() {
super.markDirty();
if (hasWorld() && getWorld() instanceof ServerWorld) {
sync();
}
}
}