From a29bb3c3b95edc1f1b7da36e01a9a259e88edb31 Mon Sep 17 00:00:00 2001 From: IntegratedQuantum <43880493+IntegratedQuantum@users.noreply.github.com> Date: Sun, 18 May 2025 19:20:51 +0200 Subject: [PATCH] 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. --- assets/cubyz/blocks/chest/_defaults.zig.zon | 2 +- assets/cubyz/blocks/frost.zig.zon | 1 - assets/cubyz/blocks/ice.zig.zon | 1 - assets/cubyz/blocks/snow.zig.zon | 1 - src/{entity_data.zig => block_entity.zig} | 70 +++++++++++---------- src/blocks.zig | 12 ++-- src/chunk.zig | 2 +- src/main.zig | 6 +- src/renderer/chunk_meshing.zig | 8 +-- src/renderer/mesh_storage.zig | 6 +- src/server/world.zig | 4 +- 11 files changed, 56 insertions(+), 57 deletions(-) rename src/{entity_data.zig => block_entity.zig} (75%) diff --git a/assets/cubyz/blocks/chest/_defaults.zig.zon b/assets/cubyz/blocks/chest/_defaults.zig.zon index 695fe5c2..6ac0b5a0 100644 --- a/assets/cubyz/blocks/chest/_defaults.zig.zon +++ b/assets/cubyz/blocks/chest/_defaults.zig.zon @@ -6,5 +6,5 @@ }, .model = "cubyz:cube", .rotation = .planar, - .entityDataClass = .chest, + .blockEntity = .chest, } diff --git a/assets/cubyz/blocks/frost.zig.zon b/assets/cubyz/blocks/frost.zig.zon index a2f86692..ba79b25e 100644 --- a/assets/cubyz/blocks/frost.zig.zon +++ b/assets/cubyz/blocks/frost.zig.zon @@ -6,6 +6,5 @@ }, .model = "cubyz:cube", .texture = "cubyz:frost", - .blockEntity = "cubyz.modding.base.MeltableBlockEntity", .friction = 5, } diff --git a/assets/cubyz/blocks/ice.zig.zon b/assets/cubyz/blocks/ice.zig.zon index e3e82f68..7e0d2931 100644 --- a/assets/cubyz/blocks/ice.zig.zon +++ b/assets/cubyz/blocks/ice.zig.zon @@ -9,6 +9,5 @@ .absorbedLight = 0xe95825, .model = "cubyz:cube", .texture = "cubyz:ice", - .blockEntity = "cubyz.modding.base.MeltableBlockEntity", .friction = 1, } diff --git a/assets/cubyz/blocks/snow.zig.zon b/assets/cubyz/blocks/snow.zig.zon index 8cf4bca7..2c8e3de5 100644 --- a/assets/cubyz/blocks/snow.zig.zon +++ b/assets/cubyz/blocks/snow.zig.zon @@ -6,5 +6,4 @@ }, .model = "cubyz:cube", .texture = "cubyz:snow", - .blockEntity = "cubyz.modding.base.MeltableBlockEntity", } diff --git a/src/entity_data.zig b/src/block_entity.zig similarity index 75% rename from src/entity_data.zig rename to src/block_entity.zig index d3bdb695..961a55e4 100644 --- a/src/entity_data.zig +++ b/src/block_entity.zig @@ -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; } diff --git a/src/blocks.zig b/src/blocks.zig index d4e3a98a..4c1247d7 100644 --- a/src/blocks.zig +++ b/src/blocks.zig @@ -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 { diff --git a/src/chunk.zig b/src/chunk.zig index f16b769c..aa86b689 100644 --- a/src/chunk.zig +++ b/src/chunk.zig @@ -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 { diff --git a/src/main.zig b/src/main.zig index e8bfe351..950f9405 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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(); diff --git a/src/renderer/chunk_meshing.zig b/src/renderer/chunk_meshing.zig index 0212666f..c65b02bc 100644 --- a/src/renderer/chunk_meshing.zig +++ b/src/renderer/chunk_meshing.zig @@ -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); diff --git a/src/renderer/mesh_storage.zig b/src/renderer/mesh_storage.zig index 6693b88b..4a0c38b1 100644 --- a/src/renderer/mesh_storage.zig +++ b/src/renderer/mesh_storage.zig @@ -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; diff --git a/src/server/world.zig b/src/server/world.zig index 42724c04..6eda8055 100644 --- a/src/server/world.zig +++ b/src/server/world.zig @@ -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();