Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixes parsing booleans in commands. Adds ClearAll command #1818

Merged
merged 3 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions Projects/UOContent/Commands/ClearCommands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using Server.Gumps;
using Server.Network;
using System;
using System.Collections.Generic;

namespace Server.Commands;

public static class ClearCommands
{
public static void Configure()
{
CommandSystem.Register("ClearFacet", AccessLevel.Owner, ClearFacet_OnCommand);
CommandSystem.Register("ClearAll", AccessLevel.Owner, ClearAll_OnCommand);
}

[Usage("ClearFacet")]
[Description("Deletes all items and mobiles in your facet. Players and their inventory will not be deleted.")]
public static void ClearFacet_OnCommand(CommandEventArgs e)
{
var from = e.Mobile;
var map = from.Map;
var list = GetObjects(entity => entity.Map == map);

DeleteObjects(list, from, map.Name);
}

[Usage( "ClearAll" )]
[Description( "Deletes all items and mobiles in all facets. Players and their inventory will not be deleted." )]
public static void ClearAll_OnCommand(CommandEventArgs e)
{
var from = e.Mobile;
var list = GetObjects();

DeleteObjects(list, from, "globally");
}

private static List<IEntity> GetObjects(Predicate<IEntity> predicate = null)
{
List<IEntity> list = [];

foreach (var item in World.Items.Values)
{
if (item.Parent == null && predicate?.Invoke(item) != false)
{
list.Add(item);
}
}

foreach (var mob in World.Mobiles.Values)
{
if (!mob.Player && predicate?.Invoke(mob) != false)
{
list.Add(mob);
}
}

return list;
}

private static void DeleteObjects(List<IEntity> list, Mobile from, string facets)
{
if (list.Count <= 0)
{
from.SendMessage("There were no objects found to delete.");
return;
}

CommandLogging.WriteLine(
from,
$"{from.AccessLevel} {CommandLogging.Format(from)} started cleaning {facets} ({list.Count} object{(list.Count == 1 ? "" : "s")})"
);

from.SendGump(
new DeleteObjectsNoticeGump(
list.Count,
facets,
okay => DeleteList_Callback(from, okay, list)
)
);
}

private class DeleteObjectsNoticeGump : StaticWarningGump<DeleteObjectsNoticeGump>
{
public override int Width => 360;
public override int Height => 260;

public override string Content { get; }

public DeleteObjectsNoticeGump(int count, string facets, Action<bool> callback) : base(callback) =>
Content =
$"You are about to delete {count} object{(count == 1 ? "" : "s")} from {facets}. Do you really wish to continue?";
}

public static void DeleteList_Callback(Mobile from, bool okay, List<IEntity> list)
{
if (!okay)
{
from.SendMessage("You have chosen not to delete those objects.");
return;
}

CommandLogging.WriteLine(
from,
$"{from.AccessLevel} {CommandLogging.Format(from)} deleting {list.Count} object{(list.Count == 1 ? "" : "s")}"
);

NetState.FlushAll();

foreach (var entity in list)
{
entity.Delete();
}

from.SendMessage($"You have deleted {list.Count} object{(list.Count == 1 ? "" : "s")}.");
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ public void Acquire(TypeBuilder typeBuilder, ILGenerator il, string fieldName)
{
Value = Enum.Parse(Type, toParse, true);
}
else if (Type == typeof(bool))
{
Value = bool.Parse(toParse);
}
else
{
MethodInfo parseMethod;
Expand Down
112 changes: 0 additions & 112 deletions Projects/UOContent/Commands/Handlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,24 @@ public static void Configure()
CommandSystem.Prefix = ServerConfiguration.GetOrUpdateSetting("commandsystem.prefix", "[");

Register("Go", AccessLevel.Counselor, Go_OnCommand);

Register("DropHolding", AccessLevel.Counselor, DropHolding_OnCommand);

Register("GetFollowers", AccessLevel.GameMaster, GetFollowers_OnCommand);

Register("ClearFacet", AccessLevel.Developer, ClearFacet_OnCommand);

Register("Where", AccessLevel.Counselor, Where_OnCommand);

Register("AutoPageNotify", AccessLevel.Counselor, APN_OnCommand);

Register("Animate", AccessLevel.GameMaster, Animate_OnCommand);

Register("Cast", AccessLevel.Counselor, Cast_OnCommand);

Register("Stuck", AccessLevel.Counselor, Stuck_OnCommand);

Register("Help", AccessLevel.Player, Help_OnCommand);

Register("Move", AccessLevel.GameMaster, Move_OnCommand);
Register("Client", AccessLevel.Counselor, Client_OnCommand);

Register("SMsg", AccessLevel.Counselor, StaffMessage_OnCommand);

Register("BCast", AccessLevel.GameMaster, BroadcastMessage_OnCommand);

Register("Bank", AccessLevel.GameMaster, Bank_OnCommand);

Register("Echo", AccessLevel.Counselor, Echo_OnCommand);

Register("Sound", AccessLevel.GameMaster, Sound_OnCommand);

Register("ViewEquip", AccessLevel.GameMaster, ViewEquip_OnCommand);

Register("Light", AccessLevel.Counselor, Light_OnCommand);
Register("Stats", AccessLevel.Counselor, Stats_OnCommand);

Register("SpeedBoost", AccessLevel.Counselor, SpeedBoost_OnCommand);
}

Expand Down Expand Up @@ -181,99 +162,6 @@ public static void DropHolding_OnTarget(Mobile from, object obj)
}
}

public static void DeleteList_Callback(Mobile from, bool okay, List<IEntity> list)
{
if (okay)
{
CommandLogging.WriteLine(
from,
$"{from.AccessLevel} {CommandLogging.Format(from)} deleting {list.Count} object{(list.Count == 1 ? "" : "s")}"
);

NetState.FlushAll();

for (var i = 0; i < list.Count; ++i)
{
list[i].Delete();
}

if (list.Count == 1)
{
from.SendMessage($"You have deleted {list.Count} object.");
}
else
{
from.SendMessage($"You have deleted {list.Count} objects.");
}
}
else
{
from.SendMessage("You have chosen not to delete those objects.");
}
}

[Usage("ClearFacet"),
Description("Deletes all items and mobiles in your facet. Players and their inventory will not be deleted.")]
public static void ClearFacet_OnCommand(CommandEventArgs e)
{
var from = e.Mobile;
var map = from.Map;

if (map == null || map == Map.Internal)
{
from.SendMessage("You may not run that command here.");
return;
}

var list = new List<IEntity>();

foreach (var item in World.Items.Values)
{
if (item.Map == map && item.Parent == null)
{
list.Add(item);
}
}

foreach (var m in World.Mobiles.Values)
{
if (m.Map == map && !m.Player)
{
list.Add(m);
}
}

if (list.Count > 0)
{
CommandLogging.WriteLine(
from,
$"{from.AccessLevel} {CommandLogging.Format(from)} starting facet clear of {map} ({list.Count} object{(list.Count == 1 ? "" : "s")})"
);

from.SendGump(
new DeleteObjectsNoticeGump(
list.Count,
okay => DeleteList_Callback(from, okay, list)
)
);
}
else
{
from.SendMessage("There were no objects found to delete.");
}
}

private class DeleteObjectsNoticeGump : StaticWarningGump<DeleteObjectsNoticeGump>
{
public override int Header => 1060635; // <CENTER>WARNING</CENTER>
public override int Width => 360;
public override int Height => 260;
public override string Content { get; }

public DeleteObjectsNoticeGump(int count, Action<bool> callback) : base(callback) =>
Content = $"You are about to delete {count} object{(count == 1 ? "" : "s")} from this facet. Do you really wish to continue?";
}

[Usage("GetFollowers")]
[Description("Teleports all pets of a targeted player to your location.")]
public static void GetFollowers_OnCommand(CommandEventArgs e)
Expand Down
Binary file modified docs/commands/commands.7z
Binary file not shown.
Loading