Did a bit of work on new user access system
This commit is contained in:
parent
68b67bfc92
commit
cbe12e64db
17 changed files with 454 additions and 420 deletions
|
@ -4,7 +4,7 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
|
||||
/** Generic class for link objects of the same class type to each other.
|
||||
*
|
||||
*
|
||||
* @author Robert Seifert */
|
||||
public class Group<J>
|
||||
{
|
||||
|
@ -23,6 +23,11 @@ public class Group<J>
|
|||
}
|
||||
}
|
||||
|
||||
public List<J> getMembers()
|
||||
{
|
||||
return this.memebers;
|
||||
}
|
||||
|
||||
protected boolean isValid(J obj)
|
||||
{
|
||||
return obj != null && memebers.contains(obj);
|
||||
|
|
|
@ -1,19 +1,21 @@
|
|||
package com.builtbroken.common;
|
||||
|
||||
/** Class to track a user. Mainly used to create groups of users
|
||||
*
|
||||
/** Class to track a user. Mainly used to create groups of users, and is mostly a prefab since it
|
||||
* only stores a string name. People who use this class should extend it to make better use out of
|
||||
* the class.
|
||||
*
|
||||
* @author Robert Seifert */
|
||||
public class User
|
||||
{
|
||||
protected String username;
|
||||
|
||||
protected User()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public User(String username)
|
||||
{
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return this.username;
|
||||
}
|
||||
}
|
||||
|
|
122
src/dark/api/access/AccessGroup.java
Normal file
122
src/dark/api/access/AccessGroup.java
Normal file
|
@ -0,0 +1,122 @@
|
|||
package dark.api.access;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
|
||||
import com.builtbroken.common.Group;
|
||||
|
||||
/** Used by a terminal to track what users are part of each group. As well used to setup access
|
||||
* points to the terminal.
|
||||
*
|
||||
* @author DarkGuardsman */
|
||||
public class AccessGroup extends Group<AccessUser>
|
||||
{
|
||||
protected List<String> nodes = new ArrayList<String>();
|
||||
protected AccessGroup extendGroup;
|
||||
|
||||
public AccessGroup(String name, AccessUser... js)
|
||||
{
|
||||
super(name, js);
|
||||
}
|
||||
|
||||
public void setToExtend(AccessGroup group)
|
||||
{
|
||||
this.extendGroup = group;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addMemeber(AccessUser obj)
|
||||
{
|
||||
if (super.addMemeber(obj))
|
||||
{
|
||||
obj.setGroup(this);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public AccessUser getMember(String name)
|
||||
{
|
||||
for (AccessUser user : this.memebers)
|
||||
{
|
||||
if (user.getName().equalsIgnoreCase(name))
|
||||
{
|
||||
return user;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public NBTTagCompound save(NBTTagCompound nbt)
|
||||
{
|
||||
nbt.setString("groupName", this.name());
|
||||
NBTTagList usersTag = new NBTTagList();
|
||||
for (AccessUser user : this.memebers)
|
||||
{
|
||||
if (!user.isTempary)
|
||||
{
|
||||
NBTTagCompound accessData = new NBTTagCompound();
|
||||
user.save(accessData);
|
||||
usersTag.appendTag(accessData);
|
||||
}
|
||||
}
|
||||
nbt.setTag("Users", usersTag);
|
||||
usersTag = new NBTTagList();
|
||||
for (String str : this.nodes)
|
||||
{
|
||||
NBTTagCompound accessData = new NBTTagCompound();
|
||||
accessData.setString("name", str);
|
||||
usersTag.appendTag(accessData);
|
||||
}
|
||||
nbt.setTag("nodes", usersTag);
|
||||
return nbt;
|
||||
}
|
||||
|
||||
public void load(NBTTagCompound nbt)
|
||||
{
|
||||
this.setName(nbt.getString("groupName"));
|
||||
NBTTagList userList = nbt.getTagList("users");
|
||||
for (int i = 0; i < userList.tagCount(); ++i)
|
||||
{
|
||||
memebers.add(AccessUser.loadFromNBT((NBTTagCompound) userList.tagAt(i)));
|
||||
}
|
||||
userList = nbt.getTagList("nodes");
|
||||
for (int i = 0; i < userList.tagCount(); ++i)
|
||||
{
|
||||
this.nodes.add(((NBTTagCompound) userList.tagAt(i)).getString("name"));
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasNode(String node)
|
||||
{
|
||||
return this.nodes.contains(node);
|
||||
}
|
||||
|
||||
public void addNode(String node)
|
||||
{
|
||||
this.nodes.add(node);
|
||||
}
|
||||
|
||||
public void removeNode(String node)
|
||||
{
|
||||
if (this.nodes.contains(node))
|
||||
{
|
||||
this.nodes.remove(node);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isMemeber(String string)
|
||||
{
|
||||
for (AccessUser user : this.memebers)
|
||||
{
|
||||
if (user.getName().equalsIgnoreCase(string))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
package dark.api.access;
|
||||
|
||||
public enum AccessLevel
|
||||
{
|
||||
NONE("None"),
|
||||
BASIC("Basic"),
|
||||
USER("Standard"),
|
||||
ADMIN("Admin"),
|
||||
OWNER("Owner");
|
||||
|
||||
public String displayName;
|
||||
|
||||
private AccessLevel(String name)
|
||||
{
|
||||
displayName = name;
|
||||
}
|
||||
|
||||
/** Gets the access level in varies ways
|
||||
*
|
||||
* @return AccessLevel NONE instead of null if correct level can't be found. */
|
||||
public static AccessLevel get(Object ob)
|
||||
{
|
||||
if (ob instanceof String)
|
||||
{
|
||||
for (AccessLevel access : AccessLevel.values())
|
||||
{
|
||||
if (access.displayName.equalsIgnoreCase((String) ob) || access.name().equalsIgnoreCase((String) ob))
|
||||
{
|
||||
return access;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ob instanceof Integer)
|
||||
{
|
||||
int i = (Integer) ob;
|
||||
|
||||
if (i >= 0 && i < AccessLevel.values().length)
|
||||
{
|
||||
return AccessLevel.values()[i];
|
||||
}
|
||||
}
|
||||
|
||||
return NONE;
|
||||
}
|
||||
}
|
99
src/dark/api/access/AccessUser.java
Normal file
99
src/dark/api/access/AccessUser.java
Normal file
|
@ -0,0 +1,99 @@
|
|||
package dark.api.access;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
|
||||
import com.builtbroken.common.User;
|
||||
|
||||
/** Used to define a users access to a terminal based object.
|
||||
*
|
||||
* @author DarkGuardsman */
|
||||
public class AccessUser extends User
|
||||
{
|
||||
protected boolean isTempary = false;
|
||||
protected NBTTagCompound extraData;
|
||||
protected AccessGroup group;
|
||||
protected List<String> nodes = new ArrayList<String>();
|
||||
|
||||
public AccessUser(String username)
|
||||
{
|
||||
super(username);
|
||||
}
|
||||
|
||||
public AccessUser(EntityPlayer player)
|
||||
{
|
||||
super(player.username);
|
||||
}
|
||||
|
||||
public AccessGroup getGroup()
|
||||
{
|
||||
return this.group;
|
||||
}
|
||||
|
||||
public void setGroup(AccessGroup group)
|
||||
{
|
||||
this.group = group;
|
||||
}
|
||||
|
||||
public boolean hasNode(String node)
|
||||
{
|
||||
if (group != null && group.hasNode(node))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return nodes.contains(node);
|
||||
}
|
||||
|
||||
public NBTTagCompound save(NBTTagCompound nbt)
|
||||
{
|
||||
nbt.setString("username", this.username);
|
||||
nbt.setCompoundTag("extraData", this.userData());
|
||||
NBTTagList usersTag = new NBTTagList();
|
||||
for (String str : this.nodes)
|
||||
{
|
||||
NBTTagCompound accessData = new NBTTagCompound();
|
||||
accessData.setString("name", str);
|
||||
usersTag.appendTag(accessData);
|
||||
}
|
||||
nbt.setTag("nodes", usersTag);
|
||||
return nbt;
|
||||
}
|
||||
|
||||
public AccessUser load(NBTTagCompound nbt)
|
||||
{
|
||||
this.username = nbt.getString("username");
|
||||
this.extraData = nbt.getCompoundTag("extraData");
|
||||
NBTTagList userList = nbt.getTagList("nodes");
|
||||
for (int i = 0; i < userList.tagCount(); ++i)
|
||||
{
|
||||
this.nodes.add(((NBTTagCompound) userList.tagAt(i)).getString("name"));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public static AccessUser loadFromNBT(NBTTagCompound nbt)
|
||||
{
|
||||
return new AccessUser("").load(nbt);
|
||||
}
|
||||
|
||||
public AccessUser setTempary(boolean si)
|
||||
{
|
||||
this.isTempary = si;
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Used to add other data to the user */
|
||||
public NBTTagCompound userData()
|
||||
{
|
||||
if (this.extraData == null)
|
||||
{
|
||||
this.extraData = new NBTTagCompound();
|
||||
}
|
||||
return this.extraData;
|
||||
}
|
||||
|
||||
}
|
|
@ -3,37 +3,35 @@ package dark.api.access;
|
|||
import java.util.List;
|
||||
|
||||
/** Used by any object that needs to restrict access to it by a set of usernames
|
||||
*
|
||||
*
|
||||
* @author DarkGuardsman */
|
||||
public interface ISpecialAccess
|
||||
{
|
||||
/** Gets the player's access level on the machine he is using
|
||||
*
|
||||
*
|
||||
* @return access level of the player, make sure to return no access if the player doesn't have
|
||||
* any */
|
||||
public AccessLevel getUserAccess(String username);
|
||||
public AccessUser getUserAccess(String username);
|
||||
|
||||
/** gets the access list for the machine
|
||||
*
|
||||
*
|
||||
* @return hasMap of players and there access levels */
|
||||
public List<UserAccess> getUsers();
|
||||
public List<AccessUser> getUsers();
|
||||
|
||||
/** sets the players access level in the access map
|
||||
*
|
||||
*
|
||||
* @param player
|
||||
* @return true if the level was set false if something went wrong */
|
||||
public boolean addUserAccess(String username, AccessLevel level, boolean save);
|
||||
public boolean setUserAccess(String username, AccessGroup group, boolean save);
|
||||
|
||||
/** Removes the user from the access list
|
||||
*
|
||||
*
|
||||
* @param username
|
||||
* @return */
|
||||
public boolean removeUserAccess(String username);
|
||||
|
||||
/** Gets a list of users with this specified access level.
|
||||
*
|
||||
* @param level
|
||||
* @return */
|
||||
List<UserAccess> getUsersWithAcess(AccessLevel level);
|
||||
public AccessGroup getGroup(String name);
|
||||
|
||||
public void addGroup(AccessGroup group);
|
||||
|
||||
}
|
||||
|
|
|
@ -1,47 +0,0 @@
|
|||
package dark.api.access;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
|
||||
import com.builtbroken.common.Group;
|
||||
|
||||
/** Used by a terminal to track what users are part of each group. As well used to setup access
|
||||
* points to the terminal.
|
||||
*
|
||||
* @author DarkGuardsman */
|
||||
public class TerminalGroup extends Group<TerminalUser>
|
||||
{
|
||||
public TerminalGroup(String name, TerminalUser... js)
|
||||
{
|
||||
super(name, js);
|
||||
}
|
||||
|
||||
public NBTTagCompound save(NBTTagCompound nbt)
|
||||
{
|
||||
nbt.setString("groupName", this.name());
|
||||
NBTTagList usersTag = new NBTTagList();
|
||||
for (TerminalUser user : this.memebers)
|
||||
{
|
||||
if (!user.isTempary)
|
||||
{
|
||||
NBTTagCompound accessData = new NBTTagCompound();
|
||||
user.save(accessData);
|
||||
usersTag.appendTag(accessData);
|
||||
}
|
||||
}
|
||||
nbt.setTag("Users", usersTag);
|
||||
|
||||
return nbt;
|
||||
}
|
||||
|
||||
public void load(NBTTagCompound nbt)
|
||||
{
|
||||
this.setName(nbt.getString("groupName"));
|
||||
NBTTagList userList = nbt.getTagList("users");
|
||||
for (short i = 0; i < userList.tagCount(); ++i)
|
||||
{
|
||||
NBTTagCompound var4 = (NBTTagCompound) userList.tagAt(i);
|
||||
memebers.add(TerminalUser.loadFromNBT(var4));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
package dark.api.access;
|
||||
|
||||
/** Used too store information about a permission node with in a terminal like device. Such as ICBM
|
||||
* sentry guns, or GS locked chests.
|
||||
*
|
||||
* @author DarkGuardsman */
|
||||
public class TerminalPermission
|
||||
{
|
||||
protected String permissionName = "p";
|
||||
protected String saveName = "access.general.p";
|
||||
|
||||
public TerminalPermission(String name)
|
||||
{
|
||||
this.permissionName = name;
|
||||
this.saveName = "access.general." + name;
|
||||
}
|
||||
|
||||
public TerminalPermission(String name, String saveName)
|
||||
{
|
||||
this.permissionName = name;
|
||||
this.saveName = saveName;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return this.permissionName;
|
||||
}
|
||||
|
||||
public String getSaveName()
|
||||
{
|
||||
return this.saveName;
|
||||
}
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
package dark.api.access;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class TerminalPermissionRegistry
|
||||
{
|
||||
public static HashMap<String, TerminalPermission> permission = new HashMap();
|
||||
|
||||
static
|
||||
{
|
||||
registerPermission(new TerminalPermission("help", "general.help"));
|
||||
registerPermission(new TerminalPermission("access", "security.core.access"));
|
||||
registerPermission(new TerminalPermission("access.set", "security.core.access.set"));
|
||||
}
|
||||
|
||||
public static void registerPermission(TerminalPermission p)
|
||||
{
|
||||
if (!permission.containsKey(p.getName()))
|
||||
{
|
||||
permission.put(p.getSaveName(), p);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
package dark.api.access;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
import com.builtbroken.common.User;
|
||||
|
||||
/** Used to define a users access to a terminal based object.
|
||||
*
|
||||
* @author DarkGuardsman */
|
||||
public class TerminalUser extends User
|
||||
{
|
||||
protected boolean isTempary = false;
|
||||
protected NBTTagCompound extraData;
|
||||
|
||||
public TerminalUser(String username)
|
||||
{
|
||||
super(username);
|
||||
}
|
||||
|
||||
public TerminalUser(EntityPlayer player)
|
||||
{
|
||||
super(player.username);
|
||||
}
|
||||
|
||||
private TerminalUser()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public NBTTagCompound save(NBTTagCompound nbt)
|
||||
{
|
||||
nbt.setString("username", this.username);
|
||||
nbt.setCompoundTag("extraData", this.userData());
|
||||
return nbt;
|
||||
}
|
||||
|
||||
public TerminalUser load(NBTTagCompound nbt)
|
||||
{
|
||||
this.username = nbt.getString("username");
|
||||
this.extraData = nbt.getCompoundTag("extraData");
|
||||
return this;
|
||||
}
|
||||
|
||||
public static TerminalUser loadFromNBT(NBTTagCompound nbt)
|
||||
{
|
||||
return new TerminalUser().load(nbt);
|
||||
}
|
||||
|
||||
public TerminalUser setTempary(boolean si)
|
||||
{
|
||||
this.isTempary = si;
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Used to add other data to the user */
|
||||
public NBTTagCompound userData()
|
||||
{
|
||||
if (this.extraData == null)
|
||||
{
|
||||
this.extraData = new NBTTagCompound();
|
||||
}
|
||||
return this.extraData;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,101 +0,0 @@
|
|||
package dark.api.access;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
|
||||
public class UserAccess
|
||||
{
|
||||
public String username;
|
||||
public AccessLevel level;
|
||||
public boolean shouldSave;
|
||||
|
||||
public UserAccess(String user, AccessLevel level, boolean save)
|
||||
{
|
||||
this.username = user;
|
||||
this.level = level;
|
||||
this.shouldSave = save;
|
||||
}
|
||||
|
||||
/** Write to nbt */
|
||||
public NBTTagCompound writeToNBT(NBTTagCompound nbt)
|
||||
{
|
||||
nbt.setString("username", this.username);
|
||||
nbt.setInteger("ID", this.level.ordinal());
|
||||
return nbt;
|
||||
}
|
||||
|
||||
/** Read from nbt */
|
||||
public void readFromNBT(NBTTagCompound nbt)
|
||||
{
|
||||
this.username = nbt.getString("username");
|
||||
this.level = AccessLevel.get(nbt.getInteger("ID"));
|
||||
}
|
||||
|
||||
public static UserAccess loadFromNBT(NBTTagCompound nbt)
|
||||
{
|
||||
UserAccess access = new UserAccess("", AccessLevel.NONE, true);
|
||||
access.readFromNBT(nbt);
|
||||
return access;
|
||||
}
|
||||
|
||||
/** Reads an entire UserAccess list from an nbt file
|
||||
*
|
||||
* @param nbt - nbt being read
|
||||
* @return - the list */
|
||||
public static List<UserAccess> readListFromNBT(NBTTagCompound nbt, String tagName)
|
||||
{
|
||||
|
||||
NBTTagList userList = nbt.getTagList(tagName);
|
||||
List<UserAccess> users = new ArrayList<UserAccess>();
|
||||
for (int i = 0; i < userList.tagCount(); ++i)
|
||||
{
|
||||
NBTTagCompound var4 = (NBTTagCompound) userList.tagAt(i);
|
||||
users.add(UserAccess.loadFromNBT(var4));
|
||||
}
|
||||
return users;
|
||||
}
|
||||
|
||||
/** writes an entire UserAccess list to nbt at one time
|
||||
*
|
||||
* @param save - nbt to save to
|
||||
* @param users - list to save */
|
||||
public static void writeListToNBT(NBTTagCompound save, List<UserAccess> users)
|
||||
{
|
||||
NBTTagList usersTag = new NBTTagList();
|
||||
for (int player = 0; player < users.size(); ++player)
|
||||
{
|
||||
UserAccess access = users.get(player);
|
||||
if (access != null && access.shouldSave)
|
||||
{
|
||||
NBTTagCompound accessData = new NBTTagCompound();
|
||||
access.writeToNBT(accessData);
|
||||
usersTag.appendTag(accessData);
|
||||
}
|
||||
}
|
||||
|
||||
save.setTag("Users", usersTag);
|
||||
}
|
||||
|
||||
/** Removes a user from a list of UserAccess then returns that list */
|
||||
public static List<UserAccess> removeUserAccess(String player, List<UserAccess> users)
|
||||
{
|
||||
List<UserAccess> removeList = new ArrayList<UserAccess>();
|
||||
List<UserAccess> returnList = users;
|
||||
for (int i = 0; i < users.size(); i++)
|
||||
{
|
||||
UserAccess ref = users.get(i);
|
||||
if (ref.username.equalsIgnoreCase(player))
|
||||
{
|
||||
removeList.add(ref);
|
||||
}
|
||||
}
|
||||
if (removeList != null && removeList.size() > 0)
|
||||
{
|
||||
returnList.removeAll(removeList);
|
||||
}
|
||||
return returnList;
|
||||
}
|
||||
}
|
|
@ -8,15 +8,14 @@ import java.util.Map;
|
|||
import java.util.Map.Entry;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import dark.api.access.AccessLevel;
|
||||
import dark.api.access.UserAccess;
|
||||
import dark.api.access.AccessUser;
|
||||
import dark.core.prefab.helpers.NBTFileHelper;
|
||||
|
||||
public class GlobalAccessManager
|
||||
{
|
||||
|
||||
/** Hash map of loaded lists **/
|
||||
private static Map<String, List<UserAccess>> globalUserLists = new HashMap<String, List<UserAccess>>();
|
||||
private static Map<String, List<AccessUser>> globalUserLists = new HashMap<String, List<AccessUser>>();
|
||||
/** Master save NBT that gets saved **/
|
||||
private static NBTTagCompound masterSaveNbt = new NBTTagCompound();
|
||||
/** Used to check to see if file is in the process of being loaded **/
|
||||
|
@ -27,7 +26,7 @@ public class GlobalAccessManager
|
|||
public static boolean needsSaving = false;
|
||||
|
||||
/** Gets or creates a userAccess list to be used for any reason
|
||||
*
|
||||
*
|
||||
* @param name - name of the access list being created or loaded
|
||||
* @param owner - the player's name to be used to create a new list
|
||||
* @return - UserAccess list */
|
||||
|
@ -69,7 +68,7 @@ public class GlobalAccessManager
|
|||
}
|
||||
|
||||
/** creates a new user access list
|
||||
*
|
||||
*
|
||||
* @param name
|
||||
* @param owner
|
||||
* @return */
|
||||
|
@ -86,7 +85,7 @@ public class GlobalAccessManager
|
|||
}
|
||||
|
||||
/** Loads up a UserAccess List
|
||||
*
|
||||
*
|
||||
* @param name - name of the list
|
||||
* @return - the list */
|
||||
public static List<UserAccess> getList(String name)
|
||||
|
@ -109,7 +108,7 @@ public class GlobalAccessManager
|
|||
}
|
||||
|
||||
/** adds a user to the global list
|
||||
*
|
||||
*
|
||||
* @param listName - name of the list
|
||||
* @param user - user being added as a UserAccess instance
|
||||
* @return true if added */
|
||||
|
@ -140,7 +139,7 @@ public class GlobalAccessManager
|
|||
}
|
||||
|
||||
/** Removes a user from the global list
|
||||
*
|
||||
*
|
||||
* @param listName - name of the list
|
||||
* @param user - user being removed
|
||||
* @return true if removed */
|
||||
|
@ -168,7 +167,7 @@ public class GlobalAccessManager
|
|||
}
|
||||
|
||||
/** Loads a given Global user list from the master save
|
||||
*
|
||||
*
|
||||
* @param name - name given to the list for reference
|
||||
* @return - the list of user access levels to be used */
|
||||
private static List<UserAccess> loadList(String name)
|
||||
|
@ -183,7 +182,7 @@ public class GlobalAccessManager
|
|||
}
|
||||
|
||||
/** Saves a given Global user list into the master save
|
||||
*
|
||||
*
|
||||
* @param name - name to save the list as
|
||||
* @param list - list to be saved */
|
||||
private static void saveList(String name, List<UserAccess> list)
|
||||
|
|
|
@ -19,7 +19,7 @@ import universalelectricity.core.vector.Vector2;
|
|||
import universalelectricity.prefab.vector.Region2;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import dark.api.access.UserAccess;
|
||||
import dark.api.access.AccessUser;
|
||||
import dark.core.interfaces.IScroll;
|
||||
import dark.core.prefab.ModPrefab;
|
||||
|
||||
|
@ -156,10 +156,10 @@ public class GuiGlobalList extends GuiContainer implements IScroll
|
|||
{
|
||||
Object object = getDisplayList().get(currentLine);
|
||||
String line = "-----";
|
||||
if (object instanceof UserAccess)
|
||||
if (object instanceof AccessUser)
|
||||
{
|
||||
UserAccess accesInfo = (UserAccess) object;
|
||||
line = accesInfo.username + " (" + accesInfo.level.displayName + ")";
|
||||
AccessUser accesInfo = (AccessUser) object;
|
||||
line = accesInfo.getName() + " (" + accesInfo.getGroup().name() + ")";
|
||||
}
|
||||
else if (object instanceof String)
|
||||
{
|
||||
|
|
|
@ -11,15 +11,15 @@ import net.minecraft.nbt.NBTTagCompound;
|
|||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraftforge.common.ForgeDirection;
|
||||
import universalelectricity.prefab.tile.TileEntityAdvanced;
|
||||
import dark.api.access.AccessLevel;
|
||||
import dark.api.access.AccessGroup;
|
||||
import dark.api.access.AccessUser;
|
||||
import dark.api.access.ISpecialAccess;
|
||||
import dark.api.access.UserAccess;
|
||||
import dark.core.interfaces.IExternalInv;
|
||||
import dark.core.interfaces.IInvBox;
|
||||
import dark.core.prefab.invgui.InvChest;
|
||||
|
||||
/** Prefab for simple object who only need basic inv support and nothing more
|
||||
*
|
||||
*
|
||||
* @author Darkguardsman */
|
||||
public class TileEntityInv extends TileEntityAdvanced implements IExternalInv, ISidedInventory, ISpecialAccess
|
||||
{
|
||||
|
@ -27,7 +27,7 @@ public class TileEntityInv extends TileEntityAdvanced implements IExternalInv, I
|
|||
protected boolean lockInv;
|
||||
protected int invSlots = 1;
|
||||
/** A list of user access data. */
|
||||
protected final List<UserAccess> users = new ArrayList<UserAccess>();
|
||||
protected List<AccessGroup> groups = new ArrayList<AccessGroup>();
|
||||
|
||||
@Override
|
||||
public IInvBox getInventory()
|
||||
|
@ -159,52 +159,40 @@ public class TileEntityInv extends TileEntityAdvanced implements IExternalInv, I
|
|||
*/
|
||||
|
||||
@Override
|
||||
public AccessLevel getUserAccess(String username)
|
||||
public AccessUser getUserAccess(String username)
|
||||
{
|
||||
for (int i = 0; i < this.users.size(); i++)
|
||||
for (AccessGroup group : this.groups)
|
||||
{
|
||||
if (this.users.get(i).username.equalsIgnoreCase(username))
|
||||
AccessUser user = group.getMember(username);
|
||||
if (user != null)
|
||||
{
|
||||
return this.users.get(i).level;
|
||||
return user;
|
||||
}
|
||||
}
|
||||
return AccessLevel.NONE;
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean canUserAccess(String username)
|
||||
{
|
||||
return (this.getUserAccess(username).ordinal() > AccessLevel.BASIC.ordinal());
|
||||
return this.getUserAccess(username) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserAccess> getUsers()
|
||||
public List<AccessUser> getUsers()
|
||||
{
|
||||
return this.users;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserAccess> getUsersWithAcess(AccessLevel level)
|
||||
{
|
||||
List<UserAccess> players = new ArrayList<UserAccess>();
|
||||
|
||||
for (int i = 0; i < this.users.size(); i++)
|
||||
List<AccessUser> users = new ArrayList<AccessUser>();
|
||||
for (AccessGroup group : this.groups)
|
||||
{
|
||||
UserAccess ref = this.users.get(i);
|
||||
|
||||
if (ref.level == level)
|
||||
{
|
||||
players.add(ref);
|
||||
}
|
||||
users.addAll(group.getMembers());
|
||||
}
|
||||
return players;
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addUserAccess(String player, AccessLevel lvl, boolean save)
|
||||
public boolean setUserAccess(String player, AccessGroup g, boolean save)
|
||||
{
|
||||
this.removeUserAccess(player);
|
||||
boolean bool = this.users.add(new UserAccess(player, lvl, save));
|
||||
boolean bool = g.addMemeber(new AccessUser(player).setTempary(save));
|
||||
this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
|
||||
return bool;
|
||||
}
|
||||
|
@ -212,23 +200,42 @@ public class TileEntityInv extends TileEntityAdvanced implements IExternalInv, I
|
|||
@Override
|
||||
public boolean removeUserAccess(String player)
|
||||
{
|
||||
List<UserAccess> removeList = new ArrayList<UserAccess>();
|
||||
for (int i = 0; i < this.users.size(); i++)
|
||||
boolean re = false;
|
||||
for (AccessGroup group : this.groups)
|
||||
{
|
||||
UserAccess ref = this.users.get(i);
|
||||
if (ref.username.equalsIgnoreCase(player))
|
||||
AccessUser user = group.getMember(player);
|
||||
if (user != null && group.removeMemeber(user))
|
||||
{
|
||||
removeList.add(ref);
|
||||
re = true;
|
||||
}
|
||||
}
|
||||
if (removeList != null && removeList.size() > 0)
|
||||
if (re)
|
||||
{
|
||||
|
||||
boolean bool = this.users.removeAll(removeList);
|
||||
this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
|
||||
return bool;
|
||||
}
|
||||
return false;
|
||||
return re;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccessGroup getGroup(String name)
|
||||
{
|
||||
for (AccessGroup group : this.groups)
|
||||
{
|
||||
if (group.name().equalsIgnoreCase(name))
|
||||
{
|
||||
return group;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addGroup(AccessGroup group)
|
||||
{
|
||||
if (!this.groups.contains(group))
|
||||
{
|
||||
this.groups.add(group);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -237,14 +244,42 @@ public class TileEntityInv extends TileEntityAdvanced implements IExternalInv, I
|
|||
super.readFromNBT(nbt);
|
||||
this.getInventory().loadInv(nbt);
|
||||
// Read user list
|
||||
this.users.clear();
|
||||
|
||||
NBTTagList userList = nbt.getTagList("Users");
|
||||
|
||||
for (int i = 0; i < userList.tagCount(); ++i)
|
||||
if (!nbt.hasKey("group"))
|
||||
{
|
||||
NBTTagCompound var4 = (NBTTagCompound) userList.tagAt(i);
|
||||
this.users.add(UserAccess.loadFromNBT(var4));
|
||||
NBTTagList userList = nbt.getTagList("Users");
|
||||
AccessGroup usr = new AccessGroup("user");
|
||||
AccessGroup admin = new AccessGroup("admin");
|
||||
AccessGroup owner = new AccessGroup("owner");
|
||||
this.groups.add(usr);
|
||||
this.groups.add(admin);
|
||||
this.groups.add(owner);
|
||||
for (int i = 0; i < userList.tagCount(); ++i)
|
||||
{
|
||||
AccessUser user = new AccessUser(((NBTTagCompound) userList.tagAt(i)).getString("username"));
|
||||
switch (nbt.getInteger("ID"))
|
||||
{
|
||||
case 1:
|
||||
case 2:
|
||||
usr.addMemeber(user);
|
||||
break;
|
||||
case 3:
|
||||
admin.addMemeber(user);
|
||||
break;
|
||||
case 4:
|
||||
owner.addMemeber(user);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
NBTTagList userList = nbt.getTagList("groups");
|
||||
for (int i = 0; i < userList.tagCount(); i++)
|
||||
{
|
||||
AccessGroup group = new AccessGroup("");
|
||||
group.load((NBTTagCompound) userList.tagAt(i));
|
||||
this.groups.add(group);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -255,17 +290,10 @@ public class TileEntityInv extends TileEntityAdvanced implements IExternalInv, I
|
|||
this.getInventory().saveInv(nbt);
|
||||
// Write user list
|
||||
NBTTagList usersTag = new NBTTagList();
|
||||
for (int player = 0; player < this.users.size(); ++player)
|
||||
for (AccessGroup group : this.groups)
|
||||
{
|
||||
UserAccess access = this.users.get(player);
|
||||
if (access != null && access.shouldSave)
|
||||
{
|
||||
NBTTagCompound accessData = new NBTTagCompound();
|
||||
access.writeToNBT(accessData);
|
||||
usersTag.appendTag(accessData);
|
||||
}
|
||||
usersTag.appendTag(group.save(new NBTTagCompound()));
|
||||
}
|
||||
|
||||
nbt.setTag("Users", usersTag);
|
||||
nbt.setTag("groups", usersTag);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ import java.util.Set;
|
|||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import dark.api.ITerminal;
|
||||
import dark.api.access.AccessLevel;
|
||||
import dark.api.access.ISpecialAccess;
|
||||
import dark.api.access.ITerminalCommand;
|
||||
|
||||
|
@ -31,7 +30,7 @@ public class CommandUser implements ITerminalCommand
|
|||
terminal.addToConsole("Listing Users");
|
||||
for (int i = 0; i < turret.getUsers().size(); i++)
|
||||
{
|
||||
terminal.addToConsole(" " + i + ") " + turret.getUsers().get(i).username);
|
||||
terminal.addToConsole(" " + i + ") " + turret.getUsers().get(i).getName());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -58,22 +57,30 @@ public class CommandUser implements ITerminalCommand
|
|||
}
|
||||
if (args[1].equalsIgnoreCase("add") && args.length > 2)
|
||||
{
|
||||
if (args[2] != null)
|
||||
if (args[2] != null && terminal.getGroup(args[2]) != null)
|
||||
{
|
||||
if (turret.addUserAccess(args[2], AccessLevel.USER, true))
|
||||
if (args.length > 3)
|
||||
{
|
||||
terminal.addToConsole("Added: " + args[2]);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
terminal.addToConsole("User already exists.");
|
||||
return true;
|
||||
if (terminal.getGroup(args[2]).isMemeber(args[3]))
|
||||
{
|
||||
terminal.addToConsole("User already exists.");
|
||||
return true;
|
||||
}
|
||||
else if (turret.setUserAccess(args[3], terminal.getGroup(args[2]), true))
|
||||
{
|
||||
terminal.addToConsole("Added: " + args[3] + " to group " + args[2]);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
terminal.addToConsole("Invalid username.");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
terminal.addToConsole("Invalid username.");
|
||||
terminal.addToConsole("Invalid group.");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,24 +1,106 @@
|
|||
package dark.core.prefab.terminal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import dark.api.ITerminal;
|
||||
import dark.api.access.AccessGroup;
|
||||
import dark.api.access.ITerminalCommand;
|
||||
|
||||
/** @author DarkGuardsman */
|
||||
public class TerminalCommandRegistry
|
||||
{
|
||||
public static final List<ITerminalCommand> COMMANDS = new ArrayList<ITerminalCommand>();
|
||||
public static final List<String> nodes = new ArrayList<String>();
|
||||
public static final HashMap<String, List<String>> groupDefaultNodes = new HashMap();
|
||||
|
||||
static
|
||||
{
|
||||
List<String> list = new ArrayList<String>();
|
||||
//Owner group defaults
|
||||
list.add("group.owner");
|
||||
list.add("inv.disable");
|
||||
list.add("inv.enable");
|
||||
groupDefaultNodes.put("owner", list);
|
||||
//Admin group defaults
|
||||
list.clear();
|
||||
list.add("group.admin");
|
||||
list.add("inv.edit");
|
||||
list.add("inv.lock");
|
||||
list.add("inv.unlock");
|
||||
list.add("inv.change");
|
||||
groupDefaultNodes.put("admin", list);
|
||||
//User group defaults
|
||||
list.clear();
|
||||
list.add("group.user");
|
||||
list.add("inv.open");
|
||||
list.add("inv.take");
|
||||
list.add("inv.give");
|
||||
groupDefaultNodes.put("user", list);
|
||||
}
|
||||
|
||||
/** Builds a new default group list for a basic machine */
|
||||
public static List<AccessGroup> createDefaultGroups()
|
||||
{
|
||||
List<AccessGroup> groups = new ArrayList<AccessGroup>();
|
||||
AccessGroup ownerPrefab = new AccessGroup("owner");
|
||||
AccessGroup adminPrefab = new AccessGroup("admin");
|
||||
AccessGroup userPrefab = new AccessGroup("user");
|
||||
ownerPrefab.setToExtend(adminPrefab);
|
||||
adminPrefab.setToExtend(userPrefab);
|
||||
List<String> groupNodes = groupDefaultNodes.get("owner");
|
||||
if (groupNodes != null)
|
||||
{
|
||||
for (String stra : groupNodes)
|
||||
{
|
||||
ownerPrefab.addNode(stra);
|
||||
}
|
||||
}
|
||||
groupNodes = groupDefaultNodes.get("admin");
|
||||
if (groupNodes != null)
|
||||
{
|
||||
for (String stra : groupNodes)
|
||||
{
|
||||
adminPrefab.addNode(stra);
|
||||
}
|
||||
}
|
||||
groupNodes = groupDefaultNodes.get("user");
|
||||
if (groupNodes != null)
|
||||
{
|
||||
for (String stra : groupNodes)
|
||||
{
|
||||
userPrefab.addNode(stra);
|
||||
}
|
||||
}
|
||||
|
||||
return groups;
|
||||
}
|
||||
|
||||
/** @param prefix - what the command starts with for example /time
|
||||
* @param cmd - Cmd instance that will execute the command */
|
||||
public static void register(ITerminalCommand cmd)
|
||||
public static void register(ITerminalCommand cmd, String group)
|
||||
{
|
||||
if (!COMMANDS.contains(cmd))
|
||||
{
|
||||
COMMANDS.add(cmd);
|
||||
if (group != null)
|
||||
{
|
||||
if (groupDefaultNodes.containsKey(group))
|
||||
{
|
||||
List<String> stra = new ArrayList<String>();
|
||||
stra.add(cmd.getCommandName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void register(String node)
|
||||
{
|
||||
if (!nodes.contains(node))
|
||||
{
|
||||
nodes.add(node);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -181,4 +181,11 @@ public abstract class TileEntityTerminal extends TileEntityEnergyMachine impleme
|
|||
return this.scroll;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canUse(String node, EntityPlayer player)
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue