ClassiCube/ClassicalSharp/Commands/SinglePlayerCommands.cs
2017-01-25 21:16:11 +11:00

124 lines
3.8 KiB
C#

// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
using System;
using System.Collections.Generic;
using System.Text;
using ClassicalSharp.Events;
using ClassicalSharp.Renderers;
using OpenTK.Input;
namespace ClassicalSharp.Commands {
public sealed class ModelCommand : Command {
public ModelCommand() {
Name = "Model";
Help = new string[] {
"&a/client model [name]",
"&bnames: &echibi, chicken, creeper, human, pig, sheep",
"&e skeleton, spider, zombie, <numerical block id>",
};
}
public override void Execute(string[] args) {
if (args.Length == 1) {
game.Chat.Add("&e/client model: &cYou didn't specify a model name.");
} else {
game.LocalPlayer.SetModel(Utils.ToLower(args[1]));
if (args.Length >= 4) {
ClassicalSharp.Entities.LocationUpdate update = ClassicalSharp.Entities.LocationUpdate.Empty();
if (args[2] == "x") update.RotX = float.Parse(args[3]);
if (args[2] == "z") update.RotZ = float.Parse(args[3]);
game.LocalPlayer.SetLocation(update, true);
}
}
}
}
public sealed class CuboidCommand : Command {
public CuboidCommand() {
Name = "Cuboid";
Help = new string[] {
"&a/client cuboid [block] [persist]",
"&eFills the 3D rectangle between two points with [block].",
"&eIf no block is given, uses your currently held block.",
"&e If persist is given and is \"yes\", then the command",
"&e will repeatedly cuboid, without needing to be typed in again.",
};
}
byte block = 0xFF;
Vector3I mark1, mark2;
bool persist = false;
public override void Execute(string[] args) {
game.UserEvents.BlockChanged -= BlockChanged;
block = 0xFF;
mark1 = new Vector3I(int.MaxValue);
mark2 = new Vector3I(int.MaxValue);
persist = false;
if (!ParseBlock(args)) return;
if (args.Length > 2 && Utils.CaselessEquals(args[2], "yes"))
persist = true;
game.Chat.Add("&eCuboid: &fPlace or delete a block.", MessageType.ClientStatus3);
game.UserEvents.BlockChanged += BlockChanged;
}
bool ParseBlock(string[] args) {
if (args.Length == 1) return true;
if (Utils.CaselessEquals(args[1], "yes")) { persist = true; return true; }
int temp = -1;
byte block = 0;
if ((temp = game.BlockInfo.FindID(args[1])) != -1) {
block = (byte)temp;
} else if (!byte.TryParse(args[1], out block)) {
game.Chat.Add("&eCuboid: &c\"" + args[1] + "\" is not a valid block name or id."); return false;
}
if (block >= Block.CpeCount && game.BlockInfo.Name[block] == "Invalid") {
game.Chat.Add("&eCuboid: &cThere is no block with id \"" + args[1] + "\"."); return false;
}
this.block = block;
return true;
}
void BlockChanged(object sender, BlockChangedEventArgs e) {
if (mark1.X == int.MaxValue) {
mark1 = e.Coords;
game.UpdateBlock(mark1.X, mark1.Y, mark1.Z, e.OldBlock);
game.Chat.Add("&eCuboid: &fMark 1 placed at (" + e.Coords + "), place mark 2.",
MessageType.ClientStatus3);
} else {
mark2 = e.Coords;
DoCuboid();
game.Chat.Add(null, MessageType.ClientStatus3);
if (!persist) {
game.UserEvents.BlockChanged -= BlockChanged;
} else {
mark1 = new Vector3I(int.MaxValue);
game.Chat.Add("&eCuboid: &fPlace or delete a block.", MessageType.ClientStatus3);
}
}
}
void DoCuboid() {
Vector3I min = Vector3I.Min(mark1, mark2);
Vector3I max = Vector3I.Max(mark1, mark2);
if (!game.World.IsValidPos(min) || !game.World.IsValidPos(max)) return;
byte toPlace = block;
if (toPlace == Block.Invalid) toPlace = game.Inventory.HeldBlock;
for (int y = min.Y; y <= max.Y; y++)
for (int z = min.Z; z <= max.Z; z++)
for (int x = min.X; x <= max.X; x++)
{
game.UpdateBlock(x, y, z, toPlace);
}
}
}
}