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/block/CustomBlockEntity.java

81 lines
2.3 KiB
Java

package com.thebrokenrail.scriptcraft.api.block;
import com.thebrokenrail.scriptcraft.core.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 long newObjID = 0;
private final long objID;
public CustomBlockEntity(BlockEntityType<?> type, Identifier id) {
super(type);
objID = newObjID++;
QuickJSManager.bridge("CustomBlockEntity.create", id.toString(), (double) objID);
}
@Override
public void fromTag(CompoundTag tag) {
super.fromTag(tag);
QuickJSManager.bridge("CustomBlockEntity.fromTag", (double) objID, tag);
}
@Override
public void fromClientTag(CompoundTag compoundTag) {
fromTag(compoundTag);
}
@Override
public CompoundTag toTag(CompoundTag tag) {
return (CompoundTag) QuickJSManager.bridge("CustomBlockEntity.toTag", (double) objID, super.toTag(tag));
}
@Override
public CompoundTag toClientTag(CompoundTag compoundTag) {
return toTag(compoundTag);
}
@Override
public void tick() {
QuickJSManager.bridge("CustomBlockEntity.tick", (double) objID);
}
@Override
public void setLocation(World world, BlockPos pos) {
super.setLocation(world, pos);
QuickJSManager.bridge("CustomBlockEntity.setLocation", (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", (double) objID);
} finally {
super.finalize();
}
}
@Override
public void markDirty() {
super.markDirty();
if (hasWorld() && getWorld() instanceof ServerWorld) {
sync();
}
}
public long getObjID() {
return objID;
}
}