Skip to content

Commit

Permalink
Split up /Model into /Model and /ModelScale
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Dec 11, 2020
1 parent dceaea4 commit ead6f6e
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 37 deletions.
46 changes: 9 additions & 37 deletions MCGalaxy/Commands/CPE/CmdModel.cs
Expand Up @@ -40,8 +40,7 @@ public class CmdModel : EntityPropertyCmd {
}

protected override void SetBotData(Player p, PlayerBot bot, string model) {
bool changedAxisScale;
model = ParseModel(p, bot, model, out changedAxisScale);
model = ParseModel(p, bot, model);
if (model == null) return;
bot.UpdateModel(model);

Expand All @@ -50,8 +49,8 @@ public class CmdModel : EntityPropertyCmd {
}

protected override void SetOnlineData(Player p, Player who, string model) {
bool changedAxisScale;
model = ParseModel(p, who, model, out changedAxisScale);
string orig = model;
model = ParseModel(p, who, model);
if (model == null) return;
who.UpdateModel(model);

Expand All @@ -68,39 +67,21 @@ public class CmdModel : EntityPropertyCmd {
}
Server.models.Save();

if (!changedAxisScale) return;
if (who.ScaleX != 0 || who.ScaleY != 0 || who.ScaleZ != 0) {
Server.modelScales.Update(who.name, who.ScaleX + " " + who.ScaleY + " " + who.ScaleZ);
} else {
Server.modelScales.Remove(who.name);
}
Server.modelScales.Save();
// Remove model scale too when resetting model
if (orig.Length == 0) CmdModelScale.UpdateSavedScale(who);
}

static string ParseModel(Player dst, Entity entity, string model, out bool changedAxisScale) {
static string ParseModel(Player dst, Entity e, string model) {
// Reset entity's model
if (model.Length == 0) {
changedAxisScale = true;
entity.ScaleX = 0; entity.ScaleY = 0; entity.ScaleZ = 0;
e.ScaleX = 0; e.ScaleY = 0; e.ScaleZ = 0;
return "humanoid";
}

model = model.ToLower();
model = model.Replace(':', '|'); // since many assume : is for scale instead of |.
changedAxisScale = false;

if (model.CaselessStarts("x ")) {
changedAxisScale = true;
return ParseModelScale(dst, entity, model, "X scale", ref entity.ScaleX);
} else if (model.CaselessStarts("y ")) {
changedAxisScale = true;
return ParseModelScale(dst, entity, model, "Y scale", ref entity.ScaleY);
} else if (model.CaselessStarts("z ")) {
changedAxisScale = true;
return ParseModelScale(dst, entity, model, "Z scale", ref entity.ScaleZ);
}
model = model.Replace(':', '|'); // since users assume : is for scale instead of |.

float max = ModelInfo.MaxScale(entity, model);
float max = ModelInfo.MaxScale(e, model);
// restrict player model scale, but bots can have unlimited model scale
if (ModelInfo.GetRawScale(model) > max) {
dst.Message("%WScale must be {0} or less for {1} model",
Expand All @@ -109,12 +90,6 @@ public class CmdModel : EntityPropertyCmd {
}
return model;
}

static string ParseModelScale(Player dst, Entity entity, string model, string argName, ref float value) {
string[] bits = model.SplitSpaces();
float max = ModelInfo.MaxScale(entity, entity.Model);
return CommandParser.GetReal(dst, bits[1], argName, ref value, 0, max) ? entity.Model : null;
}

public override void Help(Player p) {
p.Message("%T/Model [name] [model] %H- Sets the model of that player.");
Expand All @@ -131,9 +106,6 @@ public class CmdModel : EntityPropertyCmd {
} else if (message.CaselessEq("scale")) {
p.Message("%HFor a scaled model, put \"|[scale]\" after the model name.");
p.Message("%H e.g. pig|0.5, chibi|3");
p.Message("%HUse X/Y/Z [scale] for [model] to set scale on one axis.");
p.Message("%H e.g. to set twice as tall, use 'Y 2' for [model]");
p.Message("%H Use a [scale] of 0 to reset");
} else {
Help(p);
}
Expand Down
85 changes: 85 additions & 0 deletions MCGalaxy/Commands/CPE/CmdModelScale.cs
@@ -0,0 +1,85 @@
/*
Copyright 2015 MCGalaxy
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using MCGalaxy.Bots;

namespace MCGalaxy.Commands.CPE {
public class CmdModelScale : EntityPropertyCmd {
public override string name { get { return "ModelScale"; } }
public override string type { get { return CommandTypes.Other; } }
public override LevelPermission defaultRank { get { return LevelPermission.AdvBuilder; } }
public override CommandPerm[] ExtraPerms {
get { return new[] { new CommandPerm(LevelPermission.Operator, "can change the model scale of others"),
new CommandPerm(LevelPermission.Operator, "can change the model scale of bots") }; }
}

public override void Use(Player p, string message, CommandData data) {
if (message.SplitSpaces().Length < 3) { Help(p); return; }
UseBotOrOnline(p, data, message, "model scale");
}

protected override void SetBotData(Player p, PlayerBot bot, string args) {
if (!ParseArgs(p, bot, args)) return;
bot.UpdateModel(bot.Model);

BotsFile.Save(p.level);
}

protected override void SetOnlineData(Player p, Player who, string args) {
if (!ParseArgs(p, who, args)) return;
who.UpdateModel(who.Model);

UpdateSavedScale(who);
Server.modelScales.Save();
}

internal static void UpdateSavedScale(Player p) {
if (p.ScaleX != 0 || p.ScaleY != 0 || p.ScaleZ != 0) {
Server.modelScales.Update(p.name, p.ScaleX + " " + p.ScaleY + " " + p.ScaleZ);
} else {
Server.modelScales.Remove(p.name);
}
Server.modelScales.Save();
}

static bool ParseArgs(Player dst, Entity e, string args) {
if (args.CaselessStarts("x ")) {
return ParseScale(dst, e, args, "X scale", ref e.ScaleX);
} else if (args.CaselessStarts("y ")) {
return ParseScale(dst, e, args, "Y scale", ref e.ScaleY);
} else if (args.CaselessStarts("z ")) {
return ParseScale(dst, e, args, "Z scale", ref e.ScaleZ);
}
return false;
}

static bool ParseScale(Player dst, Entity e, string args, string axis, ref float value) {
string[] bits = args.SplitSpaces();
float max = ModelInfo.MaxScale(e, e.Model);
return CommandParser.GetReal(dst, bits[1], axis, ref value, 0, max);
}

public override void Help(Player p) {
p.Message("%T/ModelScale [name] X/Y/Z [scale] %H- Sets scale for a player");
p.Message("%T/ModelScale bot [name] X/Y/Z [scale] %H- Sets scale for a bot");
p.Message("%HSets the scale of the given entity's model on one axis ");
p.Message("%H e.g. %T/ModelScale -own Y 2 %Hmakes yourself twice as tall");
p.Message("%H Use a [scale] of 0 to reset scale on that axis");
}
}
}
1 change: 1 addition & 0 deletions MCGalaxy/MCGalaxy_.csproj
Expand Up @@ -195,6 +195,7 @@
<Compile Include="Commands\CPE\CmdEnvironment.cs" />
<Compile Include="Commands\CPE\CmdHold.cs" />
<Compile Include="Commands\CPE\CmdModel.cs" />
<Compile Include="Commands\CPE\CmdModelScale.cs" />
<Compile Include="Commands\CPE\CmdPing.cs" />
<Compile Include="Commands\CPE\CmdReachDistance.cs" />
<Compile Include="Commands\CPE\CmdSkin.cs" />
Expand Down

0 comments on commit ead6f6e

Please sign in to comment.