Skip to content

Commit

Permalink
Merge pull request #787 from UnknownShadow200/PlaceDeletePermissions
Browse files Browse the repository at this point in the history
Split up Block permissions into Place and Delete permissions
  • Loading branch information
UnknownShadow200 committed Dec 25, 2023
2 parents 293df1e + dc3b48b commit ffab090
Show file tree
Hide file tree
Showing 20 changed files with 217 additions and 111 deletions.
33 changes: 19 additions & 14 deletions GUI/PropertyWindow/PropertyWindow.Blocks.cs
Expand Up @@ -30,13 +30,14 @@ public partial class PropertyWindow : Form {

// need to keep a list of changed block perms, because we don't want
// to modify the server's live permissions if user clicks 'discard'
BlockPerms blockPermsOrig, blockPermsCopy;
List<BlockPerms> blockPermsChanged = new List<BlockPerms>();
BlockPerms placePermsOrig, placePermsCopy;
List<BlockPerms> placePermsChanged = new List<BlockPerms>();
BlockProps[] blockPropsChanged = new BlockProps[Block.Props.Length];
// TODO delete permissions too

void LoadBlocks() {
blk_list.Items.Clear();
blockPermsChanged.Clear();
placePermsChanged.Clear();
blockIDMap = new List<BlockID>();

for (int b = 0; b < blockPropsChanged.Length; b++)
Expand All @@ -59,7 +60,7 @@ public partial class PropertyWindow : Form {
}

void SaveBlocks() {
if (blockPermsChanged.Count > 0)
if (placePermsChanged.Count > 0)
SaveBlockPermissions();
if (AnyBlockPropsChanged())
SaveBlockProps();
Expand All @@ -68,10 +69,13 @@ public partial class PropertyWindow : Form {
}

void SaveBlockPermissions() {
foreach (BlockPerms changed in blockPermsChanged)
foreach (BlockPerms changed in placePermsChanged)
{
BlockPerms orig = BlockPerms.Find(changed.ID);
changed.CopyPermissionsTo(orig);
BlockPerms pOrig = BlockPerms.GetPlace(changed.ID);
changed.CopyPermissionsTo(pOrig);

BlockPerms dOrig = BlockPerms.GetDelete(changed.ID);
changed.CopyPermissionsTo(dOrig);
}

BlockPerms.Save();
Expand Down Expand Up @@ -101,8 +105,9 @@ public partial class PropertyWindow : Form {

void blk_list_SelectedIndexChanged(object sender, EventArgs e) {
curBlock = blockIDMap[blk_list.SelectedIndex];
blockPermsOrig = BlockPerms.Find(curBlock);
blockPermsCopy = blockPermsChanged.Find(p => p.ID == curBlock);
placePermsOrig = BlockPerms.GetPlace(curBlock);
placePermsCopy = placePermsChanged.Find(p => p.ID == curBlock);

BlockInitSpecificArrays();
blockItems.SupressEvents = true;

Expand All @@ -119,7 +124,7 @@ public partial class PropertyWindow : Form {
blk_cbLava.Checked = props.LavaKills;
blk_cbWater.Checked = props.WaterKills;

BlockPerms perms = blockPermsCopy != null ? blockPermsCopy : blockPermsOrig;
BlockPerms perms = placePermsCopy != null ? placePermsCopy : placePermsOrig;
blockItems.Update(perms);
}

Expand All @@ -132,10 +137,10 @@ public partial class PropertyWindow : Form {
}

ItemPerms BlockGetOrAddPermsChanged() {
if (blockPermsCopy != null) return blockPermsCopy;
blockPermsCopy = blockPermsOrig.Copy();
blockPermsChanged.Add(blockPermsCopy);
return blockPermsCopy;
if (placePermsCopy != null) return placePermsCopy;
placePermsCopy = placePermsOrig.Copy();
placePermsChanged.Add(placePermsCopy);
return placePermsCopy;
}


Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Blocks/Block.Convert.cs
Expand Up @@ -71,7 +71,7 @@ public static partial class Block
}

public static string GetColoredName(Player p, BlockID block) {
BlockPerms perms = BlockPerms.Find(block);
BlockPerms perms = BlockPerms.GetPlace(block); // TODO check Delete perms too?
return Group.GetColor(perms.MinRank) + Block.GetName(p, block);
}

Expand Down
4 changes: 2 additions & 2 deletions MCGalaxy/Commands/CommandParser.cs
Expand Up @@ -222,8 +222,8 @@ public static class CommandParser
/// <summary> Returns whether the player is allowed to place/modify/delete the given block. </summary>
/// <remarks> Outputs information of which ranks can modify the block if not. </remarks>
public static bool IsBlockAllowed(Player p, string action, BlockID block) {
if (p.group.Blocks[block]) return true;
BlockPerms.Find(block).MessageCannotUse(p, action);
if (p.group.CanPlace[block]) return true;
BlockPerms.GetPlace(block).MessageCannotUse(p, action); // TODO: Delete permissions too?
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Commands/Information/CmdBlocks.cs
Expand Up @@ -52,7 +52,7 @@ public sealed class CmdBlocks : Command2
Group grp = Group.Find(type);
p.Message("Blocks which {0} &Scan place: ", grp.ColoredName);
OutputBlocks(p, type, modifier,
b => grp.Blocks[b]);
b => grp.CanPlace[b]);
} else if (args.Length > 1) {
Help(p);
} else {
Expand Down
4 changes: 3 additions & 1 deletion MCGalaxy/Commands/Information/CmdHelp.cs
Expand Up @@ -136,7 +136,9 @@ public sealed class CmdHelp : Command2

p.Message("Block \"{0}\" appears as &b{1}",
message, Block.GetName(p, Block.Convert(block)));
BlockPerms.Find(block).MessageCannotUse(p, "use");

BlockPerms.GetPlace(block) .MessageCannotUse(p, "use");
BlockPerms.GetDelete(block).MessageCannotUse(p, "delete");

DescribePhysics(p, message, block);
return true;
Expand Down
68 changes: 54 additions & 14 deletions MCGalaxy/Commands/Moderation/CmdBlockSet.cs
Expand Up @@ -14,46 +14,86 @@
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 MCGalaxy.Blocks;
using BlockID = System.UInt16;

namespace MCGalaxy.Commands.Moderation {
public sealed class CmdBlockSet : ItemPermsCmd {
namespace MCGalaxy.Commands.Moderation
{
public sealed class CmdBlockSet : ItemPermsCmd
{
public override string name { get { return "BlockSet"; } }

public override void Use(Player p, string message, CommandData data) {
bool canPlace = true; const string PLACE_PREFIX = "place ";
bool canDelete = true; const string DELETE_PREFIX = "delete ";
string placeMsg = null, deleteMsg = null;

if (message.CaselessStarts(PLACE_PREFIX)) {
canDelete = false;
message = message.Substring(PLACE_PREFIX.Length);
} else if (message.CaselessStarts(DELETE_PREFIX)) {
canPlace = false;
message = message.Substring(DELETE_PREFIX.Length);
}

string[] args = message.SplitSpaces(2);
if (args.Length < 2) { Help(p); return; }

BlockID block;
if (!CommandParser.GetBlockIfAllowed(p, args[0], "change permissions of", out block)) return;

BlockPerms perms = BlockPerms.Find(block);
SetPerms(p, args, data, perms, "block");
// TODO avoid showing message twice
if (canPlace) {
BlockPerms perms = BlockPerms.GetPlace(block);
placeMsg = SetPerms(p, args, data, perms, "block", "use", "usable");
}
if (canDelete) {
BlockPerms perms = BlockPerms.GetDelete(block);
deleteMsg = SetPerms(p, args, data, perms, "block", "delete", "deletable");
}

if (placeMsg == null && deleteMsg == null) return;
UpdatePerms(block, p, placeMsg, deleteMsg);
}

protected override void UpdatePerms(ItemPerms perms, Player p, string msg) {
void UpdatePerms(BlockID block, Player p, string placeMsg, string deleteMsg) {
BlockPerms.Save();
BlockPerms.ApplyChanges();

BlockID block = ((BlockPerms)perms).ID;
if (!Block.IsPhysicsType(block)) {
BlockPerms.ResendAllBlockPermissions();
}

}
string name = Block.GetName(p, block);
Announce(p, name + msg);

if (placeMsg != null && deleteMsg != null) {
Announce(p, name + placeMsg.Replace("usable", "usable and deletable"));
} else if (placeMsg != null) {
Announce(p, name + placeMsg);
} else {
Announce(p, name + deleteMsg);
}
}

public override void Help(Player p) {
p.Message("&T/BlockSet [block] [rank]");
p.Message("&HSets lowest rank that can modify/use [block] to [rank]");
p.Message("&HSets lowest rank that can use and delete [block] to [rank]");
p.Message("&T/BlockSet place [block] [rank]");
p.Message("&HSets lowest rank that can use/modify [block] to [rank]");
p.Message("&T/BlockSet delete [block] [rank]");
p.Message("&HSets lowest rank that can delete [block] to [rank]");
p.Message("&H- For more advanced permissions, see &T/Help blockset advanced");
p.Message("&H- To see available ranks, type &T/ViewRanks");
}

public override void Help(Player p, string message) {
if (!message.CaselessEq("advanced")) { base.Help(p, message); return; }

p.Message("&T/BlockSet [block] +[rank]");
p.Message("&HAllows a specific rank to modify/use [block]");
p.Message("&HAllows a specific rank to use and delete [block]");
p.Message("&T/BlockSet [block] -[rank]");
p.Message("&HPrevents a specific rank from modifying/using [block]");
p.Message("&HTo see available ranks, type &T/ViewRanks");
p.Message("&HPrevents a specific rank from using or deleting [block]");
// TODO place and delete messages
}
}
}
51 changes: 32 additions & 19 deletions MCGalaxy/Commands/Moderation/CmdCmdSet.cs
Expand Up @@ -15,15 +15,17 @@
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
namespace MCGalaxy.Commands.Moderation {
public sealed class CmdCmdSet : ItemPermsCmd {
namespace MCGalaxy.Commands.Moderation
{
public sealed class CmdCmdSet : ItemPermsCmd
{
public override string name { get { return "CmdSet"; } }

public override void Use(Player p, string message, CommandData data) {
string[] args = message.SplitSpaces(3);
if (args.Length < 2) { Help(p); return; }

string cmdName = args[0], cmdArgs = "";
string cmdName = args[0], cmdArgs = "", msg;
Command.Search(ref cmdName, ref cmdArgs);
Command cmd = Command.Find(cmdName);

Expand All @@ -35,42 +37,53 @@ public sealed class CmdCmdSet : ItemPermsCmd {
}

if (args.Length == 2) {
SetPerms(p, args, data, cmd.Permissions, "command");
msg = SetPerms(p, args, data, cmd.Permissions, "command", "use", "usable");
if (msg != null)
UpdateCommandPerms(cmd.Permissions, p, msg);
} else {

int num = 0;
if (!CommandParser.GetInt(p, args[2], "Extra permission number", ref num)) return;

CommandExtraPerms perms = CommandExtraPerms.Find(cmd.name, num);
if (perms == null) {
p.Message("This command has no extra permission by that number."); return;
}
SetPerms(p, args, data, perms, "extra permission");

msg = SetPerms(p, args, data, perms, "extra permission", "use", "usable");
if (msg != null)
UpdateExtraPerms(perms, p, msg);
}
}

protected override void UpdatePerms(ItemPerms perms, Player p, string msg) {
if (perms is CommandPerms) {
CommandPerms.Save();
CommandPerms.ApplyChanges();
Announce(p, perms.ItemName + msg);
} else {
CommandExtraPerms.Save();
CommandExtraPerms ex = (CommandExtraPerms)perms;
//Announce(p, cmd.name + "&S's extra permission " + idx + " was set to " + grp.ColoredName);
Announce(p, ex.CmdName + " extra permission #" + ex.Num + msg);
}
void UpdateCommandPerms(ItemPerms perms, Player p, string msg) {
CommandPerms.Save();
CommandPerms.ApplyChanges();
Announce(p, perms.ItemName + msg);
}

void UpdateExtraPerms(CommandExtraPerms perms, Player p, string msg) {
CommandExtraPerms.Save();
//Announce(p, cmd.name + "&S's extra permission " + idx + " was set to " + grp.ColoredName);
Announce(p, perms.CmdName + " extra permission #" + perms.Num + msg);
}

public override void Help(Player p) {
p.Message("&T/CmdSet [cmd] [rank]");
p.Message("&HSets lowest rank that can use [cmd] to [rank]");
p.Message("&T/CmdSet [cmd] [rank] [extra permission number]");
p.Message("&HSet the lowest rank that has that extra permission for [cmd]");
p.Message("&H- For more advanced permissions, see &T/Help cmdset advanced");
p.Message("&H- To see available ranks, type &T/ViewRanks");
}

public override void Help(Player p, string message) {
if (!message.CaselessEq("advanced")) { base.Help(p, message); return; }

p.Message("&T/CmdSet [cmd] +[rank]");
p.Message("&HAllows a specific rank to use [cmd]");
p.Message("&T/CmdSet [cmd] -[rank]");
p.Message("&HPrevents a specific rank from using [cmd]");
p.Message("&T/CmdSet [cmd] [rank] [extra permission number]");
p.Message("&HSet the lowest rank that has that extra permission for [cmd]");
p.Message("&HTo see available ranks, type &T/ViewRanks");
}
}
}
27 changes: 14 additions & 13 deletions MCGalaxy/Commands/Moderation/ItemPermsCmd.cs
Expand Up @@ -17,44 +17,45 @@
*/
using System.Collections.Generic;

namespace MCGalaxy.Commands.Moderation {
public abstract class ItemPermsCmd : Command2 {
namespace MCGalaxy.Commands.Moderation
{
public abstract class ItemPermsCmd : Command2
{
public override string type { get { return CommandTypes.Moderation; } }
public override LevelPermission defaultRank { get { return LevelPermission.Operator; } }

protected void SetPerms(Player p, string[] args, CommandData data, ItemPerms perms, string type) {
protected string SetPerms(Player p, string[] args, CommandData data, ItemPerms perms, string type,
string actionNoun, string actionAdjective) {
string grpName = args[1];
if (!perms.UsableBy(data.Rank)) {
p.Message("You rank cannot use this {0}.", type); return;
p.Message("You rank cannot {1} this {0}.", type, actionNoun); return null;
}

if (grpName[0] == '+') {
Group grp = GetGroup(p, data, grpName.Substring(1));
if (grp == null) return;
if (grp == null) return null;

perms.Allow(grp.Permission);
UpdatePerms(perms, p, " &Scan now be used by " + grp.ColoredName);
return " &Sis now " + actionAdjective + " by " + grp.ColoredName;
} else if (grpName[0] == '-') {
Group grp = GetGroup(p, data, grpName.Substring(1));
if (grp == null) return;
if (grp == null) return null;

if (data.Rank == grp.Permission) {
p.Message("You cannot disallow your own rank from using a {0}.", type); return;
p.Message("&WCannot deny permissions for your own rank"); return null;
}

perms.Disallow(grp.Permission);
UpdatePerms(perms, p, " &Sis no longer usable by " + grp.ColoredName);
return " &Sis no longer " + actionAdjective + " by " + grp.ColoredName;
} else {
Group grp = GetGroup(p, data, grpName);
if (grp == null) return;
if (grp == null) return null;

perms.MinRank = grp.Permission;
UpdatePerms(perms, p, " &Sis now usable by " + grp.ColoredName + "&S+");
return " &Sis now " + actionAdjective + " by " + grp.ColoredName + "&S+";
}
}

protected abstract void UpdatePerms(ItemPerms perms, Player p, string msg);

protected static Group GetGroup(Player p, CommandData data, string grpName) {
Group grp = Matcher.FindRanks(p, grpName);
if (grp == null) return null;
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Commands/building/CmdCopy.cs
Expand Up @@ -139,7 +139,7 @@ public sealed class CmdCopy : Command2
for (ushort x = minX; x <= maxX; ++x)
{
block = p.level.GetBlock(x, y, z);
if (!p.group.Blocks[block]) { index++; continue; }
if (!p.group.CanPlace[block]) { index++; continue; }

if (block != Block.Air || cState.PasteAir) cState.UsedBlocks++;
cState.Set(block, index);
Expand Down

0 comments on commit ffab090

Please sign in to comment.