Merge pull request #70 from robinkanters/master

Moved /time command class and cleaned up some code
This commit is contained in:
Drew DeVault 2015-05-08 13:58:15 -06:00
commit 0997e94d36
6 changed files with 104 additions and 116 deletions

View File

@ -11,7 +11,7 @@ namespace TrueCraft.API.Server
string Name { get; } string Name { get; }
string Description { get; } string Description { get; }
string[] Aliases { get; } string[] Aliases { get; }
void Handle(IRemoteClient Client, string alias, string[] arguments); void Handle(IRemoteClient client, string alias, string[] arguments);
void Help(IRemoteClient client, string alias, string[] arguments); void Help(IRemoteClient client, string alias, string[] arguments);
} }
} }

View File

@ -1,10 +1,4 @@
using System; using TrueCraft.API.Networking;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TrueCraft.Core.Windows;
using TrueCraft.API;
using TrueCraft.API.Networking;
using TrueCraft.Core.Networking.Packets; using TrueCraft.Core.Networking.Packets;
namespace TrueCraft.Commands namespace TrueCraft.Commands
@ -26,14 +20,14 @@ namespace TrueCraft.Commands
get { return new string[0]; } get { return new string[0]; }
} }
public override void Handle(IRemoteClient Client, string alias, string[] arguments) public override void Handle(IRemoteClient client, string alias, string[] arguments)
{ {
if (arguments.Length != 0) if (arguments.Length != 0)
{ {
Help(Client, alias, arguments); Help(client, alias, arguments);
return; return;
} }
Client.SendMessage(Client.Entity.Position.ToString()); client.SendMessage(client.Entity.Position.ToString());
} }
public override void Help(IRemoteClient client, string alias, string[] arguments) public override void Help(IRemoteClient client, string alias, string[] arguments)
@ -59,14 +53,14 @@ namespace TrueCraft.Commands
get { return new string[0]; } get { return new string[0]; }
} }
public override void Handle(IRemoteClient Client, string alias, string[] arguments) public override void Handle(IRemoteClient client, string alias, string[] arguments)
{ {
if (arguments.Length != 0) if (arguments.Length != 0)
{ {
Help(Client, alias, arguments); Help(client, alias, arguments);
return; return;
} }
Client.EnableLogging = !Client.EnableLogging; client.EnableLogging = !client.EnableLogging;
} }
public override void Help(IRemoteClient client, string alias, string[] arguments) public override void Help(IRemoteClient client, string alias, string[] arguments)
@ -75,59 +69,6 @@ namespace TrueCraft.Commands
} }
} }
public class TimeCommand : Command
{
public override string Name
{
get { return "time"; }
}
public override string Description
{
get { return "Shows the current time."; }
}
public override string[] Aliases
{
get { return new string[0]; }
}
public override void Handle(IRemoteClient Client, string alias, string[] arguments)
{
switch (arguments.Length)
{
case 0:
Client.SendMessage(Client.World.Time.ToString());
break;
case 2:
if (!arguments[0].Equals("set"))
Help(Client, alias, arguments);
int newTime;
if(!Int32.TryParse(arguments[1], out newTime))
Help(Client, alias, arguments);
Client.World.Time = newTime;
Client.SendMessage(string.Format("Setting time to {0}", arguments[1]));
foreach (var client in Client.Server.Clients.Where(c => c.World.Equals(Client.World)))
client.QueuePacket(new TimeUpdatePacket(newTime));
break;
default:
Help(Client, alias, arguments);
break;
}
}
public override void Help(IRemoteClient client, string alias, string[] arguments)
{
client.SendMessage("/time: Shows the current time.");
}
}
public class ResendInvCommand : Command public class ResendInvCommand : Command
{ {
public override string Name public override string Name
@ -145,14 +86,14 @@ namespace TrueCraft.Commands
get { return new string[0]; } get { return new string[0]; }
} }
public override void Handle(IRemoteClient Client, string alias, string[] arguments) public override void Handle(IRemoteClient client, string alias, string[] arguments)
{ {
if (arguments.Length != 0) if (arguments.Length != 0)
{ {
Help(Client, alias, arguments); Help(client, alias, arguments);
return; return;
} }
Client.QueuePacket(new WindowItemsPacket(0, Client.Inventory.GetSlots())); client.QueuePacket(new WindowItemsPacket(0, client.Inventory.GetSlots()));
} }
public override void Help(IRemoteClient client, string alias, string[] arguments) public override void Help(IRemoteClient client, string alias, string[] arguments)

View File

@ -1,7 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TrueCraft.API.Networking; using TrueCraft.API.Networking;
using TrueCraft.API.Server; using TrueCraft.API.Server;
@ -19,63 +16,58 @@ namespace TrueCraft.Commands
get { return "Command help menu."; } get { return "Command help menu."; }
} }
public override void Handle(IRemoteClient Client, string alias, string[] arguments) public override void Handle(IRemoteClient client, string alias, string[] arguments)
{ {
if (arguments.Length < 1) if (arguments.Length < 1)
{ {
Help(Client, alias, arguments); Help(client, alias, arguments);
return; return;
} }
string Identifier; var identifier = arguments.Length >= 1 ? arguments[0] : "0";
if (arguments.Length >= 1) ICommand found;
Identifier = arguments[0]; if ((found = Program.CommandManager.FindByName(identifier)) != null)
else
Identifier = "0";
ICommand Found = null;
if ((Found = Program.CommandManager.FindByName(Identifier)) != null)
{ {
Found.Handle(Client, Identifier, new string[0]); found.Handle(client, identifier, new string[0]);
return; return;
} }
else if ((Found = Program.CommandManager.FindByAlias(Identifier)) != null) else if ((found = Program.CommandManager.FindByAlias(identifier)) != null)
{ {
Found.Help(Client, Identifier, new string[0]); found.Help(client, identifier, new string[0]);
return; return;
} }
int PageNumber = 0; int pageNumber;
if (int.TryParse(Identifier, out PageNumber)) if (int.TryParse(identifier, out pageNumber))
{ {
HelpPage(Client, PageNumber); HelpPage(client, pageNumber);
return; return;
} }
Help(Client, alias, arguments); Help(client, alias, arguments);
} }
public void HelpPage(IRemoteClient Client, int Page) public void HelpPage(IRemoteClient client, int page)
{ {
int PerPage = 5; const int perPage = 5;
int Pages = (int)Math.Floor((double)(Program.CommandManager.Commands.Count / PerPage)); int numPages = (int)Math.Floor(((double)Program.CommandManager.Commands.Count / perPage));
if ((Program.CommandManager.Commands.Count % PerPage) > 0) if ((Program.CommandManager.Commands.Count % perPage) > 0)
Pages++; numPages++;
if (Page < 1 || Page > Pages) if (page < 1 || page > numPages)
Page = 1; page = 1;
int StartingIndex = (Page - 1) * PerPage; int startingIndex = (page - 1) * perPage;
Client.SendMessage("--Help Page " + Page + " of " + Pages + "--"); client.SendMessage("--Help page " + page + " of " + numPages + "--");
for (int i = 0; i < PerPage; i++) for (int i = 0; i < perPage; i++)
{ {
int Index = StartingIndex + i; int index = startingIndex + i;
if (Index > Program.CommandManager.Commands.Count - 1) if (index > Program.CommandManager.Commands.Count - 1)
{ {
break; break;
} }
ICommand C = Program.CommandManager.Commands[Index]; var command = Program.CommandManager.Commands[index];
Client.SendMessage("/" + C.Name + " - " + C.Description); client.SendMessage("/" + command.Name + " - " + command.Description);
} }
} }

View File

@ -1,8 +1,4 @@
using System; using TrueCraft.API.Networking;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TrueCraft.API.Networking;
namespace TrueCraft.Commands namespace TrueCraft.Commands
{ {
@ -18,9 +14,9 @@ namespace TrueCraft.Commands
get { return "Ping pong"; } get { return "Ping pong"; }
} }
public override void Handle(IRemoteClient Client, string alias, string[] arguments) public override void Handle(IRemoteClient client, string alias, string[] arguments)
{ {
Client.SendMessage("Pong!"); client.SendMessage("Pong!");
} }
public override void Help(IRemoteClient client, string alias, string[] arguments) public override void Help(IRemoteClient client, string alias, string[] arguments)

View File

@ -0,0 +1,60 @@
using System;
using System.Linq;
using TrueCraft.API.Networking;
using TrueCraft.Core.Networking.Packets;
namespace TrueCraft.Commands
{
public class TimeCommand : Command
{
public override string Name
{
get { return "time"; }
}
public override string Description
{
get { return "Shows the current time."; }
}
public override string[] Aliases
{
get { return new string[0]; }
}
public override void Handle(IRemoteClient client, string alias, string[] arguments)
{
switch (arguments.Length)
{
case 0:
client.SendMessage(client.World.Time.ToString());
break;
case 2:
if (!arguments[0].Equals("set"))
Help(client, alias, arguments);
int newTime;
if(!Int32.TryParse(arguments[1], out newTime))
Help(client, alias, arguments);
client.World.Time = newTime;
client.SendMessage(string.Format("Setting time to {0}", arguments[1]));
foreach (var remoteClient in client.Server.Clients.Where(c => c.World.Equals(client.World)))
remoteClient.QueuePacket(new TimeUpdatePacket(newTime));
break;
default:
Help(client, alias, arguments);
break;
}
}
public override void Help(IRemoteClient client, string alias, string[] arguments)
{
client.SendMessage("/time: Shows the current time.");
}
}
}

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@ -44,6 +44,7 @@
<Compile Include="Commands\GiveMeCommand.cs" /> <Compile Include="Commands\GiveMeCommand.cs" />
<Compile Include="Commands\HelpCommand.cs" /> <Compile Include="Commands\HelpCommand.cs" />
<Compile Include="Commands\PingCommand.cs" /> <Compile Include="Commands\PingCommand.cs" />
<Compile Include="Commands\TimeCommand.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="MultiplayerServer.cs" /> <Compile Include="MultiplayerServer.cs" />
@ -78,9 +79,7 @@
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup /> <ItemGroup />
<ItemGroup> <ItemGroup />
<Folder Include="Exceptions\" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>