-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
348 additions
and
105 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
Obsidian.API/Commands/ArgumentParsers/MinecraftTimeArgumentParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
namespace Obsidian.API.Commands.ArgumentParsers; | ||
|
||
public sealed class MinecraftTimeArgumentParser : BaseArgumentParser<MinecraftTime> | ||
{ | ||
public MinecraftTimeArgumentParser() : base(42, "minecraft:time") | ||
{ | ||
} | ||
|
||
public override bool TryParseArgument(string input, CommandContext ctx, out MinecraftTime result) | ||
{ | ||
var lastChar = input.LastOrDefault(); | ||
var isSuccess = false; | ||
|
||
result = default; | ||
|
||
if (lastChar == 'd' && int.TryParse(input.TrimEnd('d'), out var dayTime)) | ||
{ | ||
result = MinecraftTime.FromDay(dayTime); | ||
|
||
isSuccess = true; | ||
} | ||
else if (lastChar == 't' && int.TryParse(input.TrimEnd('t'), out var tickTime)) | ||
{ | ||
result = MinecraftTime.FromTick(tickTime); | ||
|
||
isSuccess = true; | ||
} | ||
else if (lastChar == 's' && int.TryParse(input.TrimEnd('s'), out var secondsTime)) | ||
{ | ||
result = MinecraftTime.FromSecond(secondsTime); | ||
|
||
isSuccess = true; | ||
} | ||
else if (int.TryParse(input, out var intValue)) | ||
{ | ||
result = MinecraftTime.FromDay(intValue); | ||
|
||
isSuccess = true; | ||
} | ||
|
||
return isSuccess; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Obsidian.API; | ||
public readonly struct MinecraftTime | ||
{ | ||
public int? Day { get; private init; } | ||
|
||
public int? Second { get; private init; } | ||
|
||
public int? Tick { get; private init; } | ||
|
||
public static MinecraftTime FromDay(int day) => new() { Day = day }; | ||
|
||
public static MinecraftTime FromSecond(int second) => new() { Second = second }; | ||
|
||
public static MinecraftTime FromTick(int tick) => new() { Tick = tick }; | ||
|
||
public bool ConvertServerTime(IServer server) | ||
{ | ||
var success = false; | ||
|
||
if (this.Day.HasValue) | ||
{ | ||
server.DefaultWorld.Time = this.Day.Value * 24000; | ||
success = true; | ||
} | ||
else if (this.Second.HasValue) | ||
{ | ||
server.DefaultWorld.Time = this.Second.Value * 20; | ||
success = true; | ||
} | ||
else if (this.Tick.HasValue) | ||
{ | ||
server.DefaultWorld.Time = this.Tick.Value; | ||
success = true; | ||
} | ||
|
||
return success; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using Obsidian.API.Commands; | ||
using Obsidian.Entities; | ||
using Obsidian.Net.Packets.Play.Clientbound; | ||
using System.Collections.Frozen; | ||
|
||
namespace Obsidian.Commands.Modules; | ||
|
||
[CommandGroup("time")] | ||
[CommandInfo("Sets declared time", "/time <set|add> (value)")] | ||
[RequirePermission(permissions: "time")] | ||
public sealed class TimeCommandModule : CommandModuleBase | ||
{ | ||
private const int Mod = 24_000; | ||
private static readonly FrozenDictionary<string, int> TimeDictionary = new Dictionary<string, int>() | ||
{ | ||
{ "day", 1000 }, | ||
{ "night", 13_000 }, | ||
{ "noon", 6000 }, | ||
{ "midnight", 18_000 } | ||
}.ToFrozenDictionary(); | ||
|
||
[Command("query")] | ||
[CommandInfo("Queries the time", "/time query <day|daytime|gametime>")] | ||
public async Task Query(string value) | ||
{ | ||
switch (value) | ||
{ | ||
case "daytime": | ||
await this.Sender.SendMessageAsync($"The time is {this.Server.DefaultWorld.DayTime}"); | ||
break; | ||
case "day": | ||
await this.Sender.SendMessageAsync($"The time is {(int)(this.Server.DefaultWorld.Time / Mod)}"); | ||
break; | ||
case "gametime": | ||
await this.Sender.SendMessageAsync($"The time is {this.Server.DefaultWorld.Time}"); | ||
break; | ||
default: | ||
await this.Sender.SendMessageAsync("Invalid value."); | ||
break; | ||
} | ||
} | ||
|
||
[Command("set")] | ||
[CommandInfo("Sets declared time", "/time set <(d|t|s)>")] | ||
public async Task SetTime(MinecraftTime time) | ||
{ | ||
if (time.ConvertServerTime(this.Server)) | ||
await this.Sender.SendMessageAsync($"Set the time to {this.Server.DefaultWorld.Time}"); | ||
else | ||
await this.Sender.SendMessageAsync("Failed to set the time."); | ||
} | ||
|
||
//TODO: Command Suggestions | ||
[Command("set")] | ||
[CommandOverload] | ||
[CommandInfo("Sets declared time", "/time set <day|night|noon|midnight>")] | ||
public async Task SetTime(string value) | ||
{ | ||
if (TimeDictionary.TryGetValue(value, out int time)) | ||
{ | ||
this.Server.DefaultWorld.DayTime = time; | ||
|
||
await this.Sender.SendMessageAsync($"Set the time to {value}"); | ||
|
||
return; | ||
} | ||
|
||
await this.Sender.SendMessageAsync($"{value} is an invalid argument value."); | ||
} | ||
|
||
[Command("add")] | ||
public async Task AddTime(int timeToAdd) | ||
{ | ||
this.Server.DefaultWorld.DayTime += timeToAdd; | ||
|
||
await this.Sender.SendMessageAsync($"Set the time to {this.Server.DefaultWorld.Time}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Obsidian.Net; | ||
|
||
namespace Obsidian.Commands.Parsers; | ||
public sealed class MinecraftTimeParser : CommandParser | ||
{ | ||
public int Min { get; set; } = 0; | ||
|
||
public MinecraftTimeParser() : base(42, "minecraft:time") | ||
{ | ||
} | ||
|
||
public override void Write(MinecraftStream stream) | ||
{ | ||
base.Write(stream); | ||
|
||
stream.WriteInt(Min); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.