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", .model = "cubyz:cube",
.rotation = .planar, .rotation = .planar,
.entityDataClass = .chest, .blockEntity = .chest,
} }

View File

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

View File

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

View File

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

View File

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

View File

@ -16,8 +16,8 @@ const rotation = @import("rotation.zig");
const RotationMode = rotation.RotationMode; const RotationMode = rotation.RotationMode;
const Degrees = rotation.Degrees; const Degrees = rotation.Degrees;
const Entity = main.server.Entity; const Entity = main.server.Entity;
const entity_data = @import("entity_data.zig"); const block_entity = @import("block_entity.zig");
const EntityDataClass = entity_data.EntityDataClass; const BlockEntityType = block_entity.BlockEntityType;
const sbb = main.server.terrain.structure_building_blocks; const sbb = main.server.terrain.structure_building_blocks;
const blueprint = main.blueprint; const blueprint = main.blueprint;
@ -77,7 +77,7 @@ var _friction: [maxBlockCount]f32 = undefined;
var _allowOres: [maxBlockCount]bool = undefined; var _allowOres: [maxBlockCount]bool = undefined;
var _touchFunction: [maxBlockCount]?*const TouchFunction = 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); 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); _friction[size] = zon.get(f32, "friction", 20);
_allowOres[size] = zon.get(bool, "allowOres", false); _allowOres[size] = zon.get(bool, "allowOres", false);
_touchFunction[size] = TouchFunctions.getFunctionPointer(zon.get([]const u8, "touchFunction", "")); _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"); const oreProperties = zon.getChild("ore");
if(oreProperties != .null) blk: { if(oreProperties != .null) blk: {
@ -386,8 +386,8 @@ pub const Block = packed struct { // MARK: Block
return _touchFunction[self.typ]; return _touchFunction[self.typ];
} }
pub fn entityDataClass(self: Block) ?*EntityDataClass { pub fn blockEntity(self: Block) ?*BlockEntityType {
return _entityDataClass[self.typ]; return _blockEntity[self.typ];
} }
pub fn canBeChangedInto(self: Block, newBlock: Block, item: main.items.ItemStack, shouldDropSourceBlockOnSuccess: *bool) main.rotation.RotationMode.CanBeChangedInto { 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, voxelSizeMask: i32,
widthShift: u5, widthShift: u5,
blockPosToEntityDataMap: std.AutoHashMapUnmanaged(u32, u32), blockPosToEntityDataMap: std.AutoHashMapUnmanaged(u32, main.block_entity.BlockEntityIndex),
blockPosToEntityDataMapMutex: std.Thread.Mutex, blockPosToEntityDataMapMutex: std.Thread.Mutex,
pub fn init(pos: ChunkPosition) *Chunk { 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 audio = @import("audio.zig");
pub const assets = @import("assets.zig"); pub const assets = @import("assets.zig");
pub const block_entity = @import("block_entity.zig");
pub const blocks = @import("blocks.zig"); pub const blocks = @import("blocks.zig");
pub const blueprint = @import("blueprint.zig"); pub const blueprint = @import("blueprint.zig");
pub const chunk = @import("chunk.zig"); pub const chunk = @import("chunk.zig");
pub const entity = @import("entity.zig"); pub const entity = @import("entity.zig");
pub const entity_data = @import("entity_data.zig");
pub const files = @import("files.zig"); pub const files = @import("files.zig");
pub const game = @import("game.zig"); pub const game = @import("game.zig");
pub const graphics = @import("graphics.zig"); pub const graphics = @import("graphics.zig");
@ -606,8 +606,8 @@ pub fn main() void { // MARK: main()
rotation.init(); rotation.init();
defer rotation.deinit(); defer rotation.deinit();
entity_data.init(); block_entity.init();
defer entity_data.deinit(); defer block_entity.deinit();
blocks.TouchFunctions.init(); blocks.TouchFunctions.init();
defer blocks.TouchFunctions.deinit(); defer blocks.TouchFunctions.deinit();

View File

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

View File

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

View File

@ -1029,11 +1029,11 @@ pub const ServerWorld = struct { // MARK: ServerWorld
return currentBlock; return currentBlock;
} }
if(currentBlock != _newBlock) { 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); baseChunk.updateBlockAndSetChanged(x, y, z, _newBlock);
if(currentBlock != _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(); baseChunk.mutex.unlock();