WIP on allowing separate block place and delete permissions

This commit is contained in:
UnknownShadow200 2023-12-18 07:03:14 +11:00
parent c8b174efde
commit 830fd8f8b3
18 changed files with 101 additions and 55 deletions

View File

@ -71,7 +71,7 @@ namespace MCGalaxy
} }
public static string GetColoredName(Player p, BlockID 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); return Group.GetColor(perms.MinRank) + Block.GetName(p, block);
} }

View File

@ -222,8 +222,8 @@ namespace MCGalaxy.Commands
/// <summary> Returns whether the player is allowed to place/modify/delete the given block. </summary> /// <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> /// <remarks> Outputs information of which ranks can modify the block if not. </remarks>
public static bool IsBlockAllowed(Player p, string action, BlockID block) { public static bool IsBlockAllowed(Player p, string action, BlockID block) {
if (p.group.Blocks[block]) return true; if (p.group.CanPlace[block]) return true;
BlockPerms.Find(block).MessageCannotUse(p, action); BlockPerms.GetPlace(block).MessageCannotUse(p, action); // TODO: Delete permissions too?
return false; return false;
} }

View File

@ -52,7 +52,7 @@ namespace MCGalaxy.Commands.Info
Group grp = Group.Find(type); Group grp = Group.Find(type);
p.Message("Blocks which {0} &Scan place: ", grp.ColoredName); p.Message("Blocks which {0} &Scan place: ", grp.ColoredName);
OutputBlocks(p, type, modifier, OutputBlocks(p, type, modifier,
b => grp.Blocks[b]); b => grp.CanPlace[b]);
} else if (args.Length > 1) { } else if (args.Length > 1) {
Help(p); Help(p);
} else { } else {

View File

@ -136,7 +136,9 @@ namespace MCGalaxy.Commands.Info
p.Message("Block \"{0}\" appears as &b{1}", p.Message("Block \"{0}\" appears as &b{1}",
message, Block.GetName(p, Block.Convert(block))); 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); DescribePhysics(p, message, block);
return true; return true;

View File

@ -29,15 +29,19 @@ namespace MCGalaxy.Commands.Moderation {
BlockID block; BlockID block;
if (!CommandParser.GetBlockIfAllowed(p, args[0], "change permissions of", out block)) return; if (!CommandParser.GetBlockIfAllowed(p, args[0], "change permissions of", out block)) return;
BlockPerms perms = BlockPerms.Find(block); // TODO rethink messaging
BlockPerms perms = BlockPerms.GetPlace(block);
SetPerms(p, args, data, perms, "block"); SetPerms(p, args, data, perms, "block");
} }
protected override void UpdatePerms(ItemPerms perms, Player p, string msg) { protected override void UpdatePerms(ItemPerms perms, Player p, string msg) {
BlockID block = ((BlockPerms)perms).ID;
// TODO better solution
perms.CopyPermissionsTo(BlockPerms.GetDelete(block));
BlockPerms.Save(); BlockPerms.Save();
BlockPerms.ApplyChanges(); BlockPerms.ApplyChanges();
BlockID block = ((BlockPerms)perms).ID;
if (!Block.IsPhysicsType(block)) { if (!Block.IsPhysicsType(block)) {
BlockPerms.ResendAllBlockPermissions(); BlockPerms.ResendAllBlockPermissions();
} }

View File

@ -17,8 +17,10 @@
*/ */
using System.Collections.Generic; using System.Collections.Generic;
namespace MCGalaxy.Commands.Moderation { namespace MCGalaxy.Commands.Moderation
public abstract class ItemPermsCmd : Command2 { {
public abstract class ItemPermsCmd : Command2
{
public override string type { get { return CommandTypes.Moderation; } } public override string type { get { return CommandTypes.Moderation; } }
public override LevelPermission defaultRank { get { return LevelPermission.Operator; } } public override LevelPermission defaultRank { get { return LevelPermission.Operator; } }

View File

@ -139,7 +139,7 @@ namespace MCGalaxy.Commands.Building
for (ushort x = minX; x <= maxX; ++x) for (ushort x = minX; x <= maxX; ++x)
{ {
block = p.level.GetBlock(x, y, z); 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++; if (block != Block.Air || cState.PasteAir) cState.UsedBlocks++;
cState.Set(block, index); cState.Set(block, index);

View File

@ -28,7 +28,8 @@ namespace MCGalaxy.Blocks
public BlockID ID; public BlockID ID;
public override string ItemName { get { return ID.ToString(); } } public override string ItemName { get { return ID.ToString(); } }
static BlockPerms[] List = new BlockPerms[Block.SUPPORTED_COUNT]; static BlockPerms[] PlaceList = new BlockPerms[Block.SUPPORTED_COUNT];
static BlockPerms[] DeleteList = new BlockPerms[Block.SUPPORTED_COUNT];
public BlockPerms(BlockID id, LevelPermission min) : base(min) { public BlockPerms(BlockID id, LevelPermission min) : base(min) {
@ -41,8 +42,8 @@ namespace MCGalaxy.Blocks
} }
/// <summary> Find the permissions for the given block. </summary> public static BlockPerms GetPlace(BlockID b) { return PlaceList[b]; }
public static BlockPerms Find(BlockID b) { return List[b]; } public static BlockPerms GetDelete(BlockID b) { return DeleteList[b]; }
public static void ResendAllBlockPermissions() { public static void ResendAllBlockPermissions() {
@ -67,10 +68,15 @@ namespace MCGalaxy.Blocks
} }
static void SaveCore() { static void SaveCore() {
using (StreamWriter w = new StreamWriter(Paths.BlockPermsFile)) { SaveList(Paths.PlacePermsFile, PlaceList, "use");
WriteHeader(w, "block", "each block", "Block ID", "lava"); SaveList(Paths.DeletePermsFile, DeleteList, "delete");
}
foreach (BlockPerms perms in List) static void SaveList(string path, BlockPerms[] list, string action) {
using (StreamWriter w = new StreamWriter(path)) {
WriteHeader(w, "block", "each block", "Block ID", "lava", action);
foreach (BlockPerms perms in list)
{ {
if (Block.Undefined(perms.ID)) continue; if (Block.Undefined(perms.ID)) continue;
w.WriteLine(perms.Serialise()); w.WriteLine(perms.Serialise());
@ -88,9 +94,14 @@ namespace MCGalaxy.Blocks
} }
public static void SetUsable(Group grp) { public static void SetUsable(Group grp) {
foreach (BlockPerms perms in List) SetUsableList(PlaceList, grp.CanPlace, grp);
SetUsableList(DeleteList, grp.CanDelete, grp);
}
static void SetUsableList(BlockPerms[] list, bool[] permsList, Group grp) {
foreach (BlockPerms perms in list)
{ {
grp.Blocks[perms.ID] = perms.UsableBy(grp.Permission); permsList[perms.ID] = perms.UsableBy(grp.Permission);
} }
} }
@ -103,14 +114,33 @@ namespace MCGalaxy.Blocks
static void LoadCore() { static void LoadCore() {
SetDefaultPerms(); SetDefaultPerms();
if (!File.Exists(Paths.BlockPermsFile)) { Save(); return; } bool placeExists = File.Exists(Paths.PlacePermsFile);
bool deleteExists = File.Exists(Paths.DeletePermsFile);
using (StreamReader r = new StreamReader(Paths.BlockPermsFile)) { if (placeExists) LoadFile(Paths.PlacePermsFile, PlaceList);
ProcessLines(r); if (deleteExists) LoadFile(Paths.DeletePermsFile, DeleteList);
if (placeExists || deleteExists) return;
if (File.Exists(Paths.BlockPermsFile)) {
LoadFile(Paths.BlockPermsFile, PlaceList);
// TODO proper migration
for (int i = 0; i < Block.SUPPORTED_COUNT; i++)
PlaceList[i].CopyPermissionsTo(DeleteList[i]);
SetDefaultSpecialDeletePerms();
File.Move(Paths.BlockPermsFile, Paths.BlockPermsFile + ".bak");
}
Save();
}
static void LoadFile(string path, BlockPerms[] list) {
using (StreamReader r = new StreamReader(path)) {
ProcessLines(r, list);
} }
} }
static void ProcessLines(StreamReader r) { static void ProcessLines(StreamReader r, BlockPerms[] list) {
string[] args = new string[4]; string[] args = new string[4];
string line; string line;
@ -131,7 +161,7 @@ namespace MCGalaxy.Blocks
List<LevelPermission> allowed, disallowed; List<LevelPermission> allowed, disallowed;
Deserialise(args, 1, out min, out allowed, out disallowed); Deserialise(args, 1, out min, out allowed, out disallowed);
Set(block, min, allowed, disallowed); Set(block, min, list, allowed, disallowed);
} catch { } catch {
Logger.Log(LogType.Warning, "Hit an error on the block " + line); Logger.Log(LogType.Warning, "Hit an error on the block " + line);
continue; continue;
@ -139,19 +169,20 @@ namespace MCGalaxy.Blocks
} }
} }
static void Set(BlockID b, LevelPermission min, static void Set(BlockID b, LevelPermission min, BlockPerms[] list,
List<LevelPermission> allowed, List<LevelPermission> disallowed) { List<LevelPermission> allowed, List<LevelPermission> disallowed) {
BlockPerms perms = List[b]; BlockPerms perms = list[b];
if (perms == null) { if (perms == null) {
perms = new BlockPerms(b, min); perms = new BlockPerms(b, min);
List[b] = perms; list[b] = perms;
} }
perms.Init(min, allowed, disallowed); perms.Init(min, allowed, disallowed);
} }
static void SetDefaultPerms() { static void SetDefaultPerms() {
for (BlockID block = 0; block < Block.SUPPORTED_COUNT; block++) { for (BlockID block = 0; block < Block.SUPPORTED_COUNT; block++)
{
BlockProps props = Block.Props[block]; BlockProps props = Block.Props[block];
LevelPermission min; LevelPermission min;
@ -167,8 +198,15 @@ namespace MCGalaxy.Blocks
min = DefaultPerm(block); min = DefaultPerm(block);
} }
Set(block, min, null, null); Set(block, min, PlaceList, null, null);
Set(block, min, DeleteList, null, null);
} }
SetDefaultSpecialDeletePerms();
}
static void SetDefaultSpecialDeletePerms() {
for (BlockID b = Block.Water; b <= Block.StillLava; b++)
DeleteList[b].MinRank = LevelPermission.Guest;
} }
static LevelPermission DefaultPerm(BlockID block) { static LevelPermission DefaultPerm(BlockID block) {

View File

@ -88,7 +88,7 @@ namespace MCGalaxy.Commands
static void SaveCore() { static void SaveCore() {
using (StreamWriter w = new StreamWriter(Paths.CmdExtraPermsFile)) { using (StreamWriter w = new StreamWriter(Paths.CmdExtraPermsFile)) {
WriteHeader(w, "extra command permissions", "extra permissions in some commands", WriteHeader(w, "extra command permissions", "extra permissions in some commands",
"CommandName:ExtraPermissionNumber", "countdown:1"); "CommandName:ExtraPermissionNumber", "countdown:1", "use");
foreach (CommandExtraPerms perms in list) { foreach (CommandExtraPerms perms in list) {
w.WriteLine(perms.Serialise()); w.WriteLine(perms.Serialise());

View File

@ -77,7 +77,7 @@ namespace MCGalaxy.Commands
static void SaveCore() { static void SaveCore() {
using (StreamWriter w = new StreamWriter(Paths.CmdPermsFile)) { using (StreamWriter w = new StreamWriter(Paths.CmdPermsFile)) {
WriteHeader(w, "command", "each command", "CommandName", "gun"); WriteHeader(w, "command", "each command", "CommandName", "gun", "use");
foreach (CommandPerms perms in List) foreach (CommandPerms perms in List)
{ {

View File

@ -99,13 +99,13 @@ namespace MCGalaxy
protected static void WriteHeader(StreamWriter w, string itemName, string itemDesc, protected static void WriteHeader(StreamWriter w, string itemName, string itemDesc,
string headerName, string headerExample) { string headerName, string headerExample, string action) {
w.WriteLine("#Version 2"); w.WriteLine("#Version 2");
w.WriteLine("# This file contains the permissions to use {0}", itemDesc); w.WriteLine("# This file contains the permissions to {1} {0}", itemDesc, action);
w.WriteLine("# How permissions work:"); w.WriteLine("# How permissions work:");
w.WriteLine("# - If the player's rank is in Disallowed, they cannot use the {0}", itemName); w.WriteLine("# - If the player's rank is in Disallowed, they cannot {1} the {0}", itemName, action);
w.WriteLine("# - Otherwise if the player's rank is in Allowed, they can use the {0}", itemName); w.WriteLine("# - Otherwise if the player's rank is in Allowed, they can {1} the {0}", itemName, action);
w.WriteLine("# - Otherwise if the player's rank is >= Lowest Rank, they can use the {0}", itemName); w.WriteLine("# - Otherwise if the player's rank is >= Lowest Rank, they can {1} the {0}", itemName, action);
w.WriteLine("#"); w.WriteLine("#");
w.WriteLine("# Layout: {0} : LowestRank : Disallowed : Allowed", headerName); w.WriteLine("# Layout: {0} : LowestRank : Disallowed : Allowed", headerName);
w.WriteLine("# e.g. {0} : 60 : 80,67 : 40,41,55", headerExample); w.WriteLine("# e.g. {0} : 60 : 80,67 : 40,41,55", headerExample);

View File

@ -160,7 +160,7 @@ namespace MCGalaxy.Drawing.Ops
#endif #endif
// Check to make sure the block is actually different and that can be used // Check to make sure the block is actually different and that can be used
if (old == b.Block || !p.group.Blocks[old] || !p.group.Blocks[b.Block]) return; if (old == b.Block || !p.group.CanDelete[old] || !p.group.CanPlace[b.Block]) return;
// Check if player can affect block at coords in world // Check if player can affect block at coords in world
AccessController denier = lvl.CanAffect(p, b.X, b.Y, b.Z); AccessController denier = lvl.CanAffect(p, b.X, b.Y, b.Z);

View File

@ -222,12 +222,6 @@ namespace MCGalaxy {
} }
internal bool BuildIn(BlockID block) {
if (block == Block.Op_Water || block == Block.Op_Lava || Props[block].IsPortal || Props[block].IsMessageBlock) return false;
block = Block.Convert(block);
return block >= Block.Water && block <= Block.StillLava;
}
/// <summary> Returns the AccessController denying the player from changing blocks at the given coordinates. </summary> /// <summary> Returns the AccessController denying the player from changing blocks at the given coordinates. </summary>
/// <remarks> If no AccessController denies the player, returns null. </remarks> /// <remarks> If no AccessController denies the player, returns null. </remarks>
public AccessController CanAffect(Player p, ushort x, ushort y, ushort z) { public AccessController CanAffect(Player p, ushort x, ushort y, ushort z) {
@ -265,7 +259,7 @@ namespace MCGalaxy {
} }
public bool CheckAffect(Player p, ushort x, ushort y, ushort z, BlockID old, BlockID block) { public bool CheckAffect(Player p, ushort x, ushort y, ushort z, BlockID old, BlockID block) {
if (!p.group.Blocks[old] || !p.group.Blocks[block]) return false; if (!p.group.CanDelete[old] || !p.group.CanPlace[block]) return false;
AccessController denier = CanAffect(p, x, y, z); AccessController denier = CanAffect(p, x, y, z);
if (denier == null) return true; if (denier == null) return true;

View File

@ -205,13 +205,14 @@ namespace MCGalaxy
int size = extBlocks ? 5 : 4; int size = extBlocks ? 5 : 4;
byte[] bulk = new byte[count * size]; byte[] bulk = new byte[count * size];
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++)
{
BlockID block = Block.FromRaw((BlockID)i); BlockID block = Block.FromRaw((BlockID)i);
bool place = group.Blocks[block] && level.CanPlace; bool place = group.CanPlace[block] && level.CanPlace;
// NOTE: If you can't delete air, then you're no longer able to place blocks // NOTE: If you can't delete air, then you're no longer able to place blocks
// (see ClassiCube client #815) // (see ClassiCube client #815)
// TODO: Maybe better solution than this? // TODO: Maybe better solution than this?
bool delete = group.Blocks[block] && (level.CanDelete || i == Block.Air); bool delete = group.CanDelete[block] && (level.CanDelete || i == Block.Air);
// Placing air is the same as deleting existing block at that position in the world // Placing air is the same as deleting existing block at that position in the world
if (block == Block.Air) place &= delete; if (block == Block.Air) place &= delete;

View File

@ -69,7 +69,8 @@ namespace MCGalaxy
internal string filename; internal string filename;
public PlayerList Players; public PlayerList Players;
public bool[] Blocks = new bool[Block.SUPPORTED_COUNT]; public bool[] CanPlace = new bool[Block.SUPPORTED_COUNT];
public bool[] CanDelete = new bool[Block.SUPPORTED_COUNT];
public Group() { } public Group() { }
private Group(LevelPermission perm, int drawLimit, int undoMins, string name, string color, int volume, int realms) { private Group(LevelPermission perm, int drawLimit, int undoMins, string name, string color, int volume, int realms) {
@ -100,7 +101,8 @@ namespace MCGalaxy
public static Group Find(string name) { public static Group Find(string name) {
MapName(ref name); MapName(ref name);
foreach (Group grp in GroupList) { foreach (Group grp in GroupList)
{
if (grp.Name.CaselessEq(name)) return grp; if (grp.Name.CaselessEq(name)) return grp;
} }
return null; return null;

View File

@ -132,9 +132,9 @@ namespace MCGalaxy
} }
internal bool CheckManualChange(BlockID old, bool deleteMode) { internal bool CheckManualChange(BlockID old, bool deleteMode) {
if (!group.Blocks[old] && !level.BuildIn(old) && !Block.AllowBreak(old)) { if (!group.CanDelete[old] && !Block.AllowBreak(old)) {
string action = deleteMode ? "delete" : "replace"; string action = deleteMode ? "delete" : "replace";
BlockPerms.Find(old).MessageCannotUse(this, action); BlockPerms.GetDelete(old).MessageCannotUse(this, action);
return false; return false;
} }
return true; return true;

View File

@ -331,7 +331,7 @@ namespace MCGalaxy {
public const string USERNAME_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890._"; public const string USERNAME_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890._";
internal byte UserType() { return group.Blocks[Block.Bedrock] ? (byte)100 : (byte)0; } internal byte UserType() { return group.CanPlace[Block.Bedrock] ? (byte)100 : (byte)0; }
#endregion #endregion

View File

@ -39,6 +39,9 @@ namespace MCGalaxy
public const string EightBallFile = "text/8ball.txt"; public const string EightBallFile = "text/8ball.txt";
public const string BlockPermsFile = "properties/block.properties"; public const string BlockPermsFile = "properties/block.properties";
public const string PlacePermsFile = "properties/place.properties";
public const string DeletePermsFile = "properties/delete.properties";
public const string CmdPermsFile = "properties/command.properties"; public const string CmdPermsFile = "properties/command.properties";
public const string CmdExtraPermsFile = "properties/ExtraCommandPermissions.properties"; public const string CmdExtraPermsFile = "properties/ExtraCommandPermissions.properties";
public const string EconomyPropsFile = "properties/economy.properties"; public const string EconomyPropsFile = "properties/economy.properties";