Add selection wand (#1298)

* Add selection wand

* Update zon file extension

* Apply review change requests

* Simplify receive logic for worldEditPos

* Convert isServerSide to method
This commit is contained in:
Krzysztof Wiśniewski 2025-04-25 20:49:37 +02:00 committed by GitHub
parent f41504f684
commit fde5a69cdc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 43 additions and 14 deletions

View File

@ -0,0 +1,4 @@
.{
.texture = "selection_wand.png",
.stackSize = 1,
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

View File

@ -973,19 +973,28 @@ pub const Protocols = struct {
},
.worldEditPos => {
const typ = try reader.readEnum(WorldEditPosition);
switch(typ) {
.selectedPos1, .selectedPos2 => {
const pos = try reader.readVec(Vec3i);
switch(typ) {
.selectedPos1 => game.Player.selectionPosition1 = pos,
.selectedPos2 => game.Player.selectionPosition2 = pos,
else => unreachable,
}
},
.clear => {
game.Player.selectionPosition1 = null;
game.Player.selectionPosition2 = null;
},
const pos: ?Vec3i = switch(typ) {
.selectedPos1, .selectedPos2 => try reader.readVec(Vec3i),
.clear => null,
};
if(conn.isServerSide()) {
switch(typ) {
.selectedPos1 => conn.user.?.worldEditData.selectionPosition1 = pos.?,
.selectedPos2 => conn.user.?.worldEditData.selectionPosition2 = pos.?,
.clear => {
conn.user.?.worldEditData.selectionPosition1 = null;
conn.user.?.worldEditData.selectionPosition2 = null;
},
}
} else {
switch(typ) {
.selectedPos1 => game.Player.selectionPosition1 = pos,
.selectedPos2 => game.Player.selectionPosition2 = pos,
.clear => {
game.Player.selectionPosition1 = null;
game.Player.selectionPosition2 = null;
},
}
}
},
.timeAndBiome => {
@ -1789,6 +1798,10 @@ pub const Connection = struct { // MARK: Connection
return self.connectionState.load(.unordered) == .connected;
}
fn isServerSide(conn: *Connection) bool {
return conn.user != null;
}
fn handlePacketLoss(self: *Connection, loss: LossStatus) void {
if(loss == .noLoss) return;
self.slowStart = false;

View File

@ -840,6 +840,11 @@ pub const MeshSelection = struct { // MARK: MeshSelection
}
}
}
if(std.mem.eql(u8, baseItem.id, "cubyz:selection_wand")) {
game.Player.selectionPosition2 = selectedPos;
main.network.Protocols.genericUpdate.sendWorldEditPos(main.game.world.?.conn, .selectedPos2, selectedPos);
return;
}
},
.tool => |tool| {
_ = tool; // TODO: Tools might change existing blocks.
@ -851,6 +856,14 @@ pub const MeshSelection = struct { // MARK: MeshSelection
pub fn breakBlock(inventory: main.items.Inventory, slot: u32, deltaTime: f64) void {
if(selectedBlockPos) |selectedPos| {
const stack = inventory.getStack(slot);
const isSelectionWand = stack.item != null and stack.item.? == .baseItem and std.mem.eql(u8, stack.item.?.baseItem.id, "cubyz:selection_wand");
if(isSelectionWand) {
game.Player.selectionPosition1 = selectedPos;
main.network.Protocols.genericUpdate.sendWorldEditPos(main.game.world.?.conn, .selectedPos1, selectedPos);
return;
}
if(@reduce(.Or, lastSelectedBlockPos != selectedPos)) {
mesh_storage.removeBreakingAnimation(lastSelectedBlockPos);
lastSelectedBlockPos = selectedPos;
@ -863,7 +876,6 @@ pub const MeshSelection = struct { // MARK: MeshSelection
main.items.Inventory.Sync.ClientSide.mutex.lock();
if(!game.Player.isCreative()) {
const stack = inventory.getStack(slot);
var damage: f32 = 1;
const isTool = stack.item != null and stack.item.? == .tool;
if(isTool) {