Skip to content

Commit ceb88d1

Browse files
Block entities use proper cubyz: ids (#2833)
Also adds markers extracted from #2564
1 parent 5f70b8f commit ceb88d1

6 files changed

Lines changed: 20 additions & 22 deletions

File tree

assets/cubyz/blocks/chest/_defaults.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
.model = "cubyz:cube",
88
.rotation = "cubyz:planar",
9-
.blockEntity = .chest,
9+
.blockEntity = "cubyz:chest",
1010
.onInteract = .{
1111
.type = .openChest,
1212
},

assets/cubyz/blocks/sign/_defaults.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
.ceiling = "cubyz:sign/ceiling",
1414
.floor = "cubyz:sign/floor",
1515
},
16-
.blockEntity = .sign,
16+
.blockEntity = "cubyz:sign",
1717
.onInteract = .{
1818
.type = .editSign,
1919
},

src/block_entity.zig

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const UpdateEvent = union(enum) {
2727

2828
pub const ErrorSet = BinaryReader.AllErrors || error{Invalid};
2929

30-
pub const BlockEntityType = struct {
30+
pub const BlockEntityType = struct { // MARK: BlockEntityType
3131
id: []const u8,
3232
vtable: VTable,
3333

@@ -43,10 +43,10 @@ pub const BlockEntityType = struct {
4343
getServerToClientData: *const fn (pos: Vec3i, chunk: *Chunk, writer: *BinaryWriter) void,
4444
getClientToServerData: *const fn (pos: Vec3i, chunk: *Chunk, writer: *BinaryWriter) void,
4545
};
46-
pub fn init(comptime BlockEntityTypeT: type) BlockEntityType {
46+
pub fn init(comptime BlockEntityTypeT: type, comptime id: []const u8) BlockEntityType {
4747
BlockEntityTypeT.init();
4848
var class = BlockEntityType{
49-
.id = BlockEntityTypeT.id,
49+
.id = id,
5050
.vtable = undefined,
5151
};
5252

@@ -90,7 +90,7 @@ pub const BlockEntityType = struct {
9090
}
9191
};
9292

93-
fn BlockEntityDataStorage(T: type) type {
93+
fn BlockEntityDataStorage(T: type) type { // MARK: BlockEntityDataStorage
9494
return struct {
9595
pub const DataT = T;
9696
var freeIndexList: main.ListUnmanaged(BlockEntityIndex) = .{};
@@ -181,14 +181,13 @@ fn BlockEntityDataStorage(T: type) type {
181181
};
182182
}
183183

184-
pub const BlockEntityTypes = struct {
185-
pub const Chest = struct {
184+
pub const BlockEntityTypes = struct { // MARK: BlockEntityTypes
185+
pub const @"cubyz:chest" = struct { // MARK: cubyz:chest
186186
pub const inventorySize = 20;
187187
const StorageServer = BlockEntityDataStorage(struct {
188188
invId: main.items.Inventory.InventoryId,
189189
});
190190

191-
pub const id = "chest";
192191
pub fn init() void {
193192
StorageServer.init();
194193
}
@@ -249,8 +248,8 @@ pub const BlockEntityTypes = struct {
249248
pub fn updateServerData(pos: Vec3i, chunk: *Chunk, event: UpdateEvent) ErrorSet!void {
250249
switch (event) {
251250
.remove => {
252-
const chest = StorageServer.remove(pos, chunk) orelse return;
253-
main.items.Inventory.ServerSide.destroyAndDropExternallyManagedInventory(chest.invId, pos);
251+
const chestComponent = StorageServer.remove(pos, chunk) orelse return;
252+
main.items.Inventory.ServerSide.destroyAndDropExternallyManagedInventory(chestComponent.invId, pos);
254253
},
255254
.update => |_| {
256255
StorageServer.mutex.lock();
@@ -268,7 +267,7 @@ pub const BlockEntityTypes = struct {
268267
pub fn renderAll(_: Mat4f, _: Vec3f, _: Vec3d) void {}
269268
};
270269

271-
pub const Sign = struct {
270+
pub const @"cubyz:sign" = struct { // MARK: cubyz:sign
272271
const StorageServer = BlockEntityDataStorage(struct {
273272
text: []const u8,
274273
});
@@ -307,7 +306,6 @@ pub const BlockEntityTypes = struct {
307306
const textureHeight = 72;
308307
const textureMargin = 4;
309308

310-
pub const id = "sign";
311309
pub fn init() void {
312310
StorageServer.init();
313311
StorageClient.init();
@@ -433,7 +431,7 @@ pub const BlockEntityTypes = struct {
433431
const localPos = mesh.chunk.getLocalBlockPos(pos);
434432
const block = mesh.chunk.data.getValue(localPos.toIndex());
435433
const blockEntity = block.blockEntity() orelse return;
436-
if (!std.mem.eql(u8, blockEntity.id, id)) return;
434+
if (!std.mem.eql(u8, blockEntity.id, "cubyz:sign")) return;
437435

438436
StorageClient.mutex.lock();
439437
defer StorageClient.mutex.unlock();
@@ -521,7 +519,7 @@ var blockyEntityTypes: std.StringHashMapUnmanaged(BlockEntityType) = .{};
521519

522520
pub fn init() void {
523521
inline for (@typeInfo(BlockEntityTypes).@"struct".decls) |declaration| {
524-
const class = BlockEntityType.init(@field(BlockEntityTypes, declaration.name));
522+
const class = BlockEntityType.init(@field(BlockEntityTypes, declaration.name), declaration.name);
525523
blockyEntityTypes.putNoClobber(main.globalAllocator.allocator, class.id, class) catch unreachable;
526524
std.log.debug("Registered BlockEntityType '{s}'", .{class.id});
527525
}

src/callbacks/block/client/editSign.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ pub fn init(_: ZonElement) ?*anyopaque {
1111
}
1212

1313
pub fn run(_: *anyopaque, params: main.callbacks.ClientBlockCallback.Params) main.callbacks.Result {
14-
if (params.block.blockEntity() == null or !std.mem.eql(u8, params.block.blockEntity().?.id, "sign")) {
14+
if (params.block.blockEntity() == null or !std.mem.eql(u8, params.block.blockEntity().?.id, "cubyz:sign")) {
1515
std.log.err("Can only edit sign if block entity of the block is a sign.", .{});
1616
return .ignored;
1717
}
18-
main.block_entity.BlockEntityTypes.Sign.StorageClient.mutex.lock();
19-
defer main.block_entity.BlockEntityTypes.Sign.StorageClient.mutex.unlock();
20-
const data = main.block_entity.BlockEntityTypes.Sign.StorageClient.get(params.blockPos, params.chunk);
18+
main.block_entity.BlockEntityTypes.@"cubyz:sign".StorageClient.mutex.lock();
19+
defer main.block_entity.BlockEntityTypes.@"cubyz:sign".StorageClient.mutex.unlock();
20+
const data = main.block_entity.BlockEntityTypes.@"cubyz:sign".StorageClient.get(params.blockPos, params.chunk);
2121
main.gui.windowlist.sign_editor.openFromSignData(params.blockPos, if (data) |_data| _data.text else "");
2222

2323
return .handled;

src/callbacks/block/client/openChest.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ pub fn init(_: ZonElement) ?*anyopaque {
1111
}
1212

1313
pub fn run(_: *anyopaque, params: main.callbacks.ClientBlockCallback.Params) main.callbacks.Result {
14-
if (params.block.blockEntity() == null or !std.mem.eql(u8, params.block.blockEntity().?.id, "chest")) {
14+
if (params.block.blockEntity() == null or !std.mem.eql(u8, params.block.blockEntity().?.id, "cubyz:chest")) {
1515
std.log.err("Can only open chest if block entity of the block is a chest.", .{});
1616
return .ignored;
1717
}
1818
main.network.protocols.blockEntityUpdate.sendClientDataUpdateToServer(main.game.world.?.conn, params.blockPos);
1919

20-
const inventory = main.items.Inventory.ClientInventory.init(main.globalAllocator, main.block_entity.BlockEntityTypes.Chest.inventorySize, .serverShared, .{.blockInventory = params.blockPos}, .{});
20+
const inventory = main.items.Inventory.ClientInventory.init(main.globalAllocator, main.block_entity.BlockEntityTypes.@"cubyz:chest".inventorySize, .serverShared, .{.blockInventory = params.blockPos}, .{});
2121

2222
main.gui.windowlist.chest.setInventory(inventory);
2323
main.gui.openWindow("chest");

src/gui/windows/sign_editor.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn apply() void {
4444
return;
4545
}
4646

47-
main.block_entity.BlockEntityTypes.Sign.updateTextFromClient(pos, textComponent.currentString.items);
47+
main.block_entity.BlockEntityTypes.@"cubyz:sign".updateTextFromClient(pos, textComponent.currentString.items);
4848

4949
gui.toggleGameMenu();
5050
}

0 commit comments

Comments
 (0)