Skip to content

Commit

Permalink
Add time and tp command.
Browse files Browse the repository at this point in the history
More progress for #327
  • Loading branch information
IntegratedQuantum committed May 28, 2024
1 parent e084709 commit e696ce1
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/server/command/_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ pub fn execute(msg: []const u8, source: *User) void {
const end = std.mem.indexOfScalar(u8, msg, ' ') orelse msg.len;
const command = msg[0..end];
if(commands.get(command)) |cmd| {
const result = std.fmt.allocPrint(main.stackAllocator.allocator, "#00ff00Executing Command /{s}", .{msg}) catch unreachable;
defer main.stackAllocator.free(result);
source.sendMessage(result);
cmd.exec(msg[@min(end + 1, msg.len)..], source);
} else {
const result = std.fmt.allocPrint(main.stackAllocator.allocator, "#ff0000Unrecognized Command \"{s}\"", .{command}) catch unreachable;
Expand Down
4 changes: 3 additions & 1 deletion src/server/command/_list.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


pub const help = @import("help.zig");
pub const help = @import("help.zig");
pub const time = @import("time.zig");
pub const tp = @import("tp.zig");
2 changes: 2 additions & 0 deletions src/server/command/help.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn execute(args: []const u8, source: *User) void {
if(args.len == 0) {
var iterator = command.commands.valueIterator();
while(iterator.next()) |cmd| {
msg.append('/');
msg.appendSlice(cmd.name);
msg.appendSlice(": ");
msg.appendSlice(cmd.description);
Expand All @@ -24,6 +25,7 @@ pub fn execute(args: []const u8, source: *User) void {
var split = std.mem.splitScalar(u8, args, ' ');
while(split.next()) |arg| {
if(command.commands.get(arg)) |cmd| {
msg.append('/');
msg.appendSlice(cmd.name);
msg.appendSlice(": ");
msg.appendSlice(cmd.description);
Expand Down
36 changes: 36 additions & 0 deletions src/server/command/time.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const std = @import("std");

const main = @import("root");
const User = main.server.User;

pub const description = "Get or set the server time.";
pub const usage = "/time\n/time <day/night>\n/time <time>";

pub fn execute(args: []const u8, source: *User) void {
var split = std.mem.splitScalar(u8, args, ' ');
if(split.next()) |arg| blk: {
if(arg.len == 0) break :blk;
var gameTime: i64 = undefined;
if(std.ascii.eqlIgnoreCase(arg, "day")) {
gameTime = 0;
} else if(std.ascii.eqlIgnoreCase(arg, "night")) {
gameTime = main.server.ServerWorld.dayCycle/2;
} else {
gameTime = std.fmt.parseInt(i64, arg, 0) catch {
const msg = std.fmt.allocPrint(main.stackAllocator.allocator, "#ff0000Expected i64 number, found \"{s}\"", .{arg}) catch unreachable;
defer main.stackAllocator.free(msg);
source.sendMessage(msg);
return;
};
}
if(split.next() != null) {
source.sendMessage("#ff0000Too many arguments for command /time");
return;
}
main.server.world.?.gameTime = gameTime;
return;
}
const msg = std.fmt.allocPrint(main.stackAllocator.allocator, "#ffff00{}", .{main.server.world.?.gameTime}) catch unreachable;
defer main.stackAllocator.free(msg);
source.sendMessage(msg);
}
40 changes: 40 additions & 0 deletions src/server/command/tp.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const std = @import("std");

const main = @import("root");
const User = main.server.User;

pub const description = "Teleport to location.";
pub const usage = "/tp <x> <y>\n/tp <x> <y> <z>";

pub fn execute(args: []const u8, source: *User) void {
var x: ?f64 = null;
var y: ?f64 = null;
var z: ?f64 = null;
var split = std.mem.splitScalar(u8, args, ' ');
while(split.next()) |arg| {
const num: f64 = std.fmt.parseFloat(f64, arg) catch {
const msg = std.fmt.allocPrint(main.stackAllocator.allocator, "#ff0000Expected number, found \"{s}\"", .{arg}) catch unreachable;
defer main.stackAllocator.free(msg);
source.sendMessage(msg);
return;
};
if(x == null) {
x = num;
} else if(y == null) {
y = num;
} else if(z == null) {
z = num;
} else {
source.sendMessage("#ff0000Too many arguments for command /tp");
return;
}
}
if(x == null or y == null) {
source.sendMessage("#ff0000Too few arguments for command /tp");
return;
}
if(z == null) {
z = source.player.pos[2];
}
main.network.Protocols.genericUpdate.sendTPCoordinates(source.conn, .{x.?, y.?, z.?});
}

0 comments on commit e696ce1

Please sign in to comment.