Rename EntityDataClass to BlockEntityType (#1464)

I also removed the legacy (java) blockEntity references.

fixes #1454

@Argmaster since you wrote the original code in #1224, please review and
check if the new names are fine for you.
This commit is contained in:
IntegratedQuantum 2025-05-18 19:20:51 +02:00 committed by GitHub
parent 1f4d23464a
commit a29bb3c3b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 56 additions and 57 deletions

View File

@ -6,5 +6,5 @@
},
.model = "cubyz:cube",
.rotation = .planar,
.entityDataClass = .chest,
.blockEntity = .chest,
}

View File

@ -6,6 +6,5 @@
},
.model = "cubyz:cube",
.texture = "cubyz:frost",
.blockEntity = "cubyz.modding.base.MeltableBlockEntity",
.friction = 5,
}

View File

@ -9,6 +9,5 @@
.absorbedLight = 0xe95825,
.model = "cubyz:cube",
.texture = "cubyz:ice",
.blockEntity = "cubyz.modding.base.MeltableBlockEntity",
.friction = 1,
}

View File

@ -6,5 +6,4 @@
},
.model = "cubyz:cube",
.texture = "cubyz:snow",
.blockEntity = "cubyz.modding.base.MeltableBlockEntity",
}

View File

@ -11,7 +11,9 @@ const server = main.server;
const User = server.User;
const mesh_storage = main.renderer.mesh_storage;
pub const EntityDataClass = struct {
pub const BlockEntityIndex = u32;
pub const BlockEntityType = struct {
id: []const u8,
vtable: VTable,
@ -26,46 +28,46 @@ pub const EntityDataClass = struct {
onBreakServer: *const fn(pos: Vec3i, chunk: *Chunk) void,
onInteract: *const fn(pos: Vec3i, chunk: *Chunk) EventStatus,
};
pub fn init(comptime EntityDataClassT: type) EntityDataClass {
EntityDataClassT.init();
var class = EntityDataClass{
.id = EntityDataClassT.id,
pub fn init(comptime BlockEntityTypeT: type) BlockEntityType {
BlockEntityTypeT.init();
var class = BlockEntityType{
.id = BlockEntityTypeT.id,
.vtable = undefined,
};
inline for(@typeInfo(EntityDataClass.VTable).@"struct".fields) |field| {
if(!@hasDecl(EntityDataClassT, field.name)) {
@compileError("EntityDataClass missing field");
inline for(@typeInfo(BlockEntityType.VTable).@"struct".fields) |field| {
if(!@hasDecl(BlockEntityTypeT, field.name)) {
@compileError("BlockEntityType missing field '" ++ field.name ++ "'");
}
@field(class.vtable, field.name) = &@field(EntityDataClassT, field.name);
@field(class.vtable, field.name) = &@field(BlockEntityTypeT, field.name);
}
return class;
}
pub inline fn onLoadClient(self: *EntityDataClass, pos: Vec3i, chunk: *Chunk) void {
pub inline fn onLoadClient(self: *BlockEntityType, pos: Vec3i, chunk: *Chunk) void {
return self.vtable.onLoadClient(pos, chunk);
}
pub inline fn onUnloadClient(self: *EntityDataClass, pos: Vec3i, chunk: *Chunk) void {
pub inline fn onUnloadClient(self: *BlockEntityType, pos: Vec3i, chunk: *Chunk) void {
return self.vtable.onUnloadClient(pos, chunk);
}
pub inline fn onLoadServer(self: *EntityDataClass, pos: Vec3i, chunk: *Chunk) void {
pub inline fn onLoadServer(self: *BlockEntityType, pos: Vec3i, chunk: *Chunk) void {
return self.vtable.onLoadServer(pos, chunk);
}
pub inline fn onUnloadServer(self: *EntityDataClass, pos: Vec3i, chunk: *Chunk) void {
pub inline fn onUnloadServer(self: *BlockEntityType, pos: Vec3i, chunk: *Chunk) void {
return self.vtable.onUnloadServer(pos, chunk);
}
pub inline fn onPlaceClient(self: *EntityDataClass, pos: Vec3i, chunk: *Chunk) void {
pub inline fn onPlaceClient(self: *BlockEntityType, pos: Vec3i, chunk: *Chunk) void {
return self.vtable.onPlaceClient(pos, chunk);
}
pub inline fn onBreakClient(self: *EntityDataClass, pos: Vec3i, chunk: *Chunk) void {
pub inline fn onBreakClient(self: *BlockEntityType, pos: Vec3i, chunk: *Chunk) void {
return self.vtable.onBreakClient(pos, chunk);
}
pub inline fn onPlaceServer(self: *EntityDataClass, pos: Vec3i, chunk: *Chunk) void {
pub inline fn onPlaceServer(self: *BlockEntityType, pos: Vec3i, chunk: *Chunk) void {
return self.vtable.onPlaceServer(pos, chunk);
}
pub inline fn onBreakServer(self: *EntityDataClass, pos: Vec3i, chunk: *Chunk) void {
pub inline fn onBreakServer(self: *BlockEntityType, pos: Vec3i, chunk: *Chunk) void {
return self.vtable.onBreakServer(pos, chunk);
}
pub inline fn onInteract(self: *EntityDataClass, pos: Vec3i, chunk: *Chunk) EventStatus {
pub inline fn onInteract(self: *BlockEntityType, pos: Vec3i, chunk: *Chunk) EventStatus {
return self.vtable.onInteract(pos, chunk);
}
};
@ -134,7 +136,7 @@ fn BlockEntityDataStorage(comptime side: enum {client, server}, T: type) type {
.client => propagateRemoveClient(movedEntry.absoluteBlockPosition, dataIndex),
}
}
fn propagateRemoveServer(pos: Vec3i, index: u32) void {
fn propagateRemoveServer(pos: Vec3i, index: BlockEntityIndex) void {
const severChunk = server.world.?.getChunkFromCacheAndIncreaseRefCount(ChunkPosition.initFromWorldPos(pos, 1)).?;
defer severChunk.decreaseRefCount();
@ -144,7 +146,7 @@ fn BlockEntityDataStorage(comptime side: enum {client, server}, T: type) type {
const otherDataIndex = severChunk.super.getLocalBlockIndex(pos);
severChunk.super.blockPosToEntityDataMap.put(main.globalAllocator.allocator, otherDataIndex, index) catch unreachable;
}
fn propagateRemoveClient(pos: Vec3i, index: u32) void {
fn propagateRemoveClient(pos: Vec3i, index: BlockEntityIndex) void {
const mesh = mesh_storage.getMeshAndIncreaseRefCount(ChunkPosition.initFromWorldPos(pos, 1)).?;
defer mesh.decreaseRefCount();
@ -171,7 +173,7 @@ fn BlockEntityDataStorage(comptime side: enum {client, server}, T: type) type {
};
}
pub const EntityDataClasses = struct {
pub const BlockEntityTypes = struct {
pub const Chest = struct {
const StorageServer = BlockEntityDataStorage(
.server,
@ -215,32 +217,32 @@ pub const EntityDataClasses = struct {
};
};
var entityDataClasses: std.StringHashMapUnmanaged(EntityDataClass) = .{};
var blockyEntityTypes: std.StringHashMapUnmanaged(BlockEntityType) = .{};
pub fn init() void {
inline for(@typeInfo(EntityDataClasses).@"struct".decls) |declaration| {
const class = EntityDataClass.init(@field(EntityDataClasses, declaration.name));
entityDataClasses.putNoClobber(main.globalAllocator.allocator, class.id, class) catch unreachable;
std.log.debug("Registered EntityDataClass '{s}'", .{class.id});
inline for(@typeInfo(BlockEntityTypes).@"struct".decls) |declaration| {
const class = BlockEntityType.init(@field(BlockEntityTypes, declaration.name));
blockyEntityTypes.putNoClobber(main.globalAllocator.allocator, class.id, class) catch unreachable;
std.log.debug("Registered BlockEntityType '{s}'", .{class.id});
}
}
pub fn reset() void {
inline for(@typeInfo(EntityDataClasses).@"struct".decls) |declaration| {
@field(EntityDataClasses, declaration.name).reset();
inline for(@typeInfo(BlockEntityTypes).@"struct".decls) |declaration| {
@field(BlockEntityTypes, declaration.name).reset();
}
}
pub fn deinit() void {
inline for(@typeInfo(EntityDataClasses).@"struct".decls) |declaration| {
@field(EntityDataClasses, declaration.name).deinit();
inline for(@typeInfo(BlockEntityTypes).@"struct".decls) |declaration| {
@field(BlockEntityTypes, declaration.name).deinit();
}
entityDataClasses.deinit(main.globalAllocator.allocator);
blockyEntityTypes.deinit(main.globalAllocator.allocator);
}
pub fn getByID(_id: ?[]const u8) ?*EntityDataClass {
pub fn getByID(_id: ?[]const u8) ?*BlockEntityType {
const id = _id orelse return null;
if(entityDataClasses.getPtr(id)) |cls| return cls;
std.log.err("EntityDataClass with id '{s}' not found", .{id});
if(blockyEntityTypes.getPtr(id)) |cls| return cls;
std.log.err("BlockEntityType with id '{s}' not found", .{id});
return null;
}

View File

@ -16,8 +16,8 @@ const rotation = @import("rotation.zig");
const RotationMode = rotation.RotationMode;
const Degrees = rotation.Degrees;
const Entity = main.server.Entity;
const entity_data = @import("entity_data.zig");
const EntityDataClass = entity_data.EntityDataClass;
const block_entity = @import("block_entity.zig");
const BlockEntityType = block_entity.BlockEntityType;
const sbb = main.server.terrain.structure_building_blocks;
const blueprint = main.blueprint;
@ -77,7 +77,7 @@ var _friction: [maxBlockCount]f32 = undefined;
var _allowOres: [maxBlockCount]bool = undefined;
var _touchFunction: [maxBlockCount]?*const TouchFunction = undefined;
var _entityDataClass: [maxBlockCount]?*EntityDataClass = undefined;
var _blockEntity: [maxBlockCount]?*BlockEntityType = undefined;
var reverseIndices = std.StringHashMap(u16).init(allocator.allocator);
@ -125,7 +125,7 @@ pub fn register(_: []const u8, id: []const u8, zon: ZonElement) u16 {
_friction[size] = zon.get(f32, "friction", 20);
_allowOres[size] = zon.get(bool, "allowOres", false);
_touchFunction[size] = TouchFunctions.getFunctionPointer(zon.get([]const u8, "touchFunction", ""));
_entityDataClass[size] = entity_data.getByID(zon.get(?[]const u8, "entityDataClass", null));
_blockEntity[size] = block_entity.getByID(zon.get(?[]const u8, "blockEntity", null));
const oreProperties = zon.getChild("ore");
if(oreProperties != .null) blk: {
@ -386,8 +386,8 @@ pub const Block = packed struct { // MARK: Block
return _touchFunction[self.typ];
}
pub fn entityDataClass(self: Block) ?*EntityDataClass {
return _entityDataClass[self.typ];
pub fn blockEntity(self: Block) ?*BlockEntityType {
return _blockEntity[self.typ];
}
pub fn canBeChangedInto(self: Block, newBlock: Block, item: main.items.ItemStack, shouldDropSourceBlockOnSuccess: *bool) main.rotation.RotationMode.CanBeChangedInto {

View File

@ -255,7 +255,7 @@ pub const Chunk = struct { // MARK: Chunk
voxelSizeMask: i32,
widthShift: u5,
blockPosToEntityDataMap: std.AutoHashMapUnmanaged(u32, u32),
blockPosToEntityDataMap: std.AutoHashMapUnmanaged(u32, main.block_entity.BlockEntityIndex),
blockPosToEntityDataMapMutex: std.Thread.Mutex,
pub fn init(pos: ChunkPosition) *Chunk {

View File

@ -5,11 +5,11 @@ pub const server = @import("server/server.zig");
pub const audio = @import("audio.zig");
pub const assets = @import("assets.zig");
pub const block_entity = @import("block_entity.zig");
pub const blocks = @import("blocks.zig");
pub const blueprint = @import("blueprint.zig");
pub const chunk = @import("chunk.zig");
pub const entity = @import("entity.zig");
pub const entity_data = @import("entity_data.zig");
pub const files = @import("files.zig");
pub const game = @import("game.zig");
pub const graphics = @import("graphics.zig");
@ -606,8 +606,8 @@ pub fn main() void { // MARK: main()
rotation.init();
defer rotation.deinit();
entity_data.init();
defer entity_data.deinit();
block_entity.init();
defer block_entity.deinit();
blocks.TouchFunctions.init();
defer blocks.TouchFunctions.deinit();

View File

@ -1213,8 +1213,8 @@ pub const ChunkMesh = struct { // MARK: ChunkMesh
}
self.mutex.unlock();
if(oldBlock.entityDataClass()) |class| {
class.onBreakClient(.{_x, _y, _z}, self.chunk);
if(oldBlock.blockEntity()) |blockEntity| {
blockEntity.onBreakClient(.{_x, _y, _z}, self.chunk);
}
var neighborBlocks: [6]Block = undefined;
@ -1268,8 +1268,8 @@ pub const ChunkMesh = struct { // MARK: ChunkMesh
self.chunk.data.setValue(chunk.getIndex(x, y, z), newBlock);
self.mutex.unlock();
if(newBlock.entityDataClass()) |class| {
class.onPlaceClient(.{_x, _y, _z}, self.chunk);
if(newBlock.blockEntity()) |blockEntity| {
blockEntity.onPlaceClient(.{_x, _y, _z}, self.chunk);
}
self.updateBlockLight(x, y, z, newBlock, lightRefreshList);

View File

@ -16,7 +16,7 @@ const Vec3f = vec.Vec3f;
const Vec3d = vec.Vec3d;
const Vec4f = vec.Vec4f;
const Mat4f = vec.Mat4f;
const EventStatus = main.entity_data.EventStatus;
const EventStatus = main.block_entity.EventStatus;
const chunk_meshing = @import("chunk_meshing.zig");
const ChunkMesh = chunk_meshing.ChunkMesh;
@ -188,8 +188,8 @@ pub fn triggerOnInteractBlock(x: i32, y: i32, z: i32) EventStatus {
defer node.mutex.unlock();
const mesh = node.mesh orelse return .ignored;
const block = mesh.chunk.getBlock(x & chunk.chunkMask, y & chunk.chunkMask, z & chunk.chunkMask);
if(block.entityDataClass()) |class| {
return class.onInteract(.{x, y, z}, mesh.chunk);
if(block.blockEntity()) |blockEntity| {
return blockEntity.onInteract(.{x, y, z}, mesh.chunk);
}
// Event was not handled.
return .ignored;

View File

@ -1029,11 +1029,11 @@ pub const ServerWorld = struct { // MARK: ServerWorld
return currentBlock;
}
if(currentBlock != _newBlock) {
if(currentBlock.entityDataClass()) |class| class.onBreakServer(.{wx, wy, wz}, &baseChunk.super);
if(currentBlock.blockEntity()) |blockEntity| blockEntity.onBreakServer(.{wx, wy, wz}, &baseChunk.super);
}
baseChunk.updateBlockAndSetChanged(x, y, z, _newBlock);
if(currentBlock != _newBlock) {
if(_newBlock.entityDataClass()) |class| class.onPlaceServer(.{wx, wy, wz}, &baseChunk.super);
if(_newBlock.blockEntity()) |blockEntity| blockEntity.onPlaceServer(.{wx, wy, wz}, &baseChunk.super);
}
}
baseChunk.mutex.unlock();