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/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/block/CustomBlockEntity.java

82 lines
2.4 KiB
Java
Raw Normal View History

2020-04-28 00:15:24 +00:00
package com.thebrokenrail.scriptcraft.api.block;
2020-04-26 17:23:16 +00:00
2020-05-05 00:36:32 +00:00
import com.thebrokenrail.scriptcraft.core.ScriptCraftCore;
2020-04-28 00:15:24 +00:00
import com.thebrokenrail.scriptcraft.core.quickjs.QuickJSManager;
2020-04-26 17:23:16 +00:00
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 {
2020-04-28 02:30:12 +00:00
private static double newObjID = 0;
2020-04-26 17:23:16 +00:00
2020-04-28 02:30:12 +00:00
private final double objID;
2020-04-26 17:23:16 +00:00
public CustomBlockEntity(BlockEntityType<?> type, Identifier id) {
super(type);
2020-04-27 14:31:58 +00:00
2020-04-28 00:15:24 +00:00
objID = newObjID++;
2020-04-26 17:23:16 +00:00
2020-05-05 00:36:32 +00:00
ScriptCraftCore.useBridge("CustomBlockEntity.create", id.toString(), objID);
2020-04-26 17:23:16 +00:00
}
@Override
public void fromTag(CompoundTag tag) {
super.fromTag(tag);
2020-05-05 00:36:32 +00:00
ScriptCraftCore.useBridge("CustomBlockEntity.fromTag", objID, tag);
2020-04-26 17:23:16 +00:00
}
@Override
public void fromClientTag(CompoundTag compoundTag) {
fromTag(compoundTag);
}
@Override
public CompoundTag toTag(CompoundTag tag) {
2020-05-05 00:36:32 +00:00
return (CompoundTag) ScriptCraftCore.useBridge("CustomBlockEntity.toTag", objID, super.toTag(tag));
2020-04-26 17:23:16 +00:00
}
@Override
public CompoundTag toClientTag(CompoundTag compoundTag) {
return toTag(compoundTag);
}
@Override
public void tick() {
2020-05-05 00:36:32 +00:00
ScriptCraftCore.useBridge("CustomBlockEntity.tick", objID);
2020-04-26 17:23:16 +00:00
}
@Override
public void setLocation(World world, BlockPos pos) {
super.setLocation(world, pos);
2020-05-05 00:36:32 +00:00
ScriptCraftCore.useBridge("CustomBlockEntity.setLocation", objID, world, (double) pos.getX(), (double) pos.getY(), (double) pos.getZ());
2020-04-26 17:23:16 +00:00
}
@SuppressWarnings("deprecation")
@Override
protected void finalize() throws Throwable {
try {
2020-05-05 00:36:32 +00:00
ScriptCraftCore.useBridge("CustomBlockEntity.free", objID);
2020-04-26 17:23:16 +00:00
} finally {
super.finalize();
}
}
@Override
public void markDirty() {
super.markDirty();
if (hasWorld() && getWorld() instanceof ServerWorld) {
sync();
}
}
2020-04-28 00:15:24 +00:00
2020-04-28 02:30:12 +00:00
public double getObjID() {
2020-04-28 00:15:24 +00:00
return objID;
}
2020-04-26 17:23:16 +00:00
}