Toying with terminal and access stuff
This commit is contained in:
parent
d7dc835aef
commit
68b67bfc92
17 changed files with 160 additions and 197 deletions
|
@ -2,12 +2,15 @@ package dark.api;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
|
||||
import dark.api.access.ISpecialAccess;
|
||||
import dark.core.interfaces.IScroll;
|
||||
|
||||
/** Basic methods to make it easier to construct or interact with a terminal based tile. Recommend to
|
||||
* be used by tiles that want to mimic computer command line like interfaces. As well to restrict
|
||||
* access to the tile in the same way a computer would
|
||||
*
|
||||
*
|
||||
* @author DarkGuardsmsan */
|
||||
public interface ITerminal extends ISpecialAccess, IScroll
|
||||
{
|
||||
|
@ -16,4 +19,6 @@ public interface ITerminal extends ISpecialAccess, IScroll
|
|||
|
||||
/** Adds a string to the console. Server side only. */
|
||||
public boolean addToConsole(String msg);
|
||||
|
||||
public boolean canUse(String node, EntityPlayer player);
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package dark.api;
|
||||
package dark.api.access;
|
||||
|
||||
public enum AccessLevel
|
||||
{
|
|
@ -1,4 +1,4 @@
|
|||
package dark.api;
|
||||
package dark.api.access;
|
||||
|
||||
import java.util.List;
|
||||
|
31
src/dark/api/access/ITerminalCommand.java
Normal file
31
src/dark/api/access/ITerminalCommand.java
Normal file
|
@ -0,0 +1,31 @@
|
|||
package dark.api.access;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import dark.api.ITerminal;
|
||||
|
||||
/** Prefab for creating commands that most terminal entities can use
|
||||
*
|
||||
* @author DarkGuardsman */
|
||||
public interface ITerminalCommand
|
||||
{
|
||||
/** The command has been called by a player in a terminal.
|
||||
*
|
||||
* @return false if the call was not supported rather than failed. Used too allow several
|
||||
* commands with the same name to exist but each has its own sub calls */
|
||||
public boolean called(EntityPlayer player, ITerminal terminal, String[] args);
|
||||
|
||||
/** Can the machine use the command. Used to prevent commands from being called on machines that
|
||||
* can't support it */
|
||||
public boolean canSupport(ITerminal terminal);
|
||||
|
||||
/** What the command starts with like /time */
|
||||
public String getCommandName();
|
||||
|
||||
/** Used to restrict sub commands like /time day, or /time night. Will be added to the name of
|
||||
* the command so a command called time will have a sub comamnd day its node will equal time.day */
|
||||
public Set<String> getPermissionNodes();
|
||||
|
||||
public String getNode(String[] args);
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package dark.api;
|
||||
package dark.api.access;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
33
src/dark/api/access/TerminalPermission.java
Normal file
33
src/dark/api/access/TerminalPermission.java
Normal file
|
@ -0,0 +1,33 @@
|
|||
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;
|
||||
}
|
||||
}
|
23
src/dark/api/access/TerminalPermissionRegistry.java
Normal file
23
src/dark/api/access/TerminalPermissionRegistry.java
Normal file
|
@ -0,0 +1,23 @@
|
|||
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,4 +1,4 @@
|
|||
package dark.api;
|
||||
package dark.api.access;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
|
@ -1,4 +1,4 @@
|
|||
package dark.api;
|
||||
package dark.api.access;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
|
@ -8,8 +8,8 @@ import java.util.Map;
|
|||
import java.util.Map.Entry;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import dark.api.AccessLevel;
|
||||
import dark.api.UserAccess;
|
||||
import dark.api.access.AccessLevel;
|
||||
import dark.api.access.UserAccess;
|
||||
import dark.core.prefab.helpers.NBTFileHelper;
|
||||
|
||||
public class GlobalAccessManager
|
||||
|
|
|
@ -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.UserAccess;
|
||||
import dark.api.access.UserAccess;
|
||||
import dark.core.interfaces.IScroll;
|
||||
import dark.core.prefab.ModPrefab;
|
||||
|
||||
|
|
|
@ -11,9 +11,9 @@ import net.minecraft.nbt.NBTTagCompound;
|
|||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraftforge.common.ForgeDirection;
|
||||
import universalelectricity.prefab.tile.TileEntityAdvanced;
|
||||
import dark.api.AccessLevel;
|
||||
import dark.api.ISpecialAccess;
|
||||
import dark.api.UserAccess;
|
||||
import dark.api.access.AccessLevel;
|
||||
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;
|
||||
|
|
|
@ -1,94 +0,0 @@
|
|||
package dark.core.prefab.terminal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import dark.api.ISpecialAccess;
|
||||
import dark.api.ITerminal;
|
||||
|
||||
public class CommandHelp extends TerminalCommand
|
||||
{
|
||||
@Override
|
||||
public String getCommandPrefix()
|
||||
{
|
||||
return "help";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean processCommand(EntityPlayer player, ITerminal TE, String[] args)
|
||||
{
|
||||
if (args.length > 1)
|
||||
{
|
||||
List<String> displayed = new ArrayList<String>();
|
||||
|
||||
for (TerminalCommand cc : CommandRegistry.COMMANDS)
|
||||
{
|
||||
if (cc.getCommandPrefix().equalsIgnoreCase(args[1]) && cc.showOnHelp(player, TE) && cc.canMachineUse(TE))
|
||||
{
|
||||
TE.addToConsole("----------------------");
|
||||
TE.addToConsole(args[1] + " commands");
|
||||
TE.addToConsole("----------------------");
|
||||
List<String> ccList = cc.getCmdUses(player, TE);
|
||||
|
||||
for (String cm : ccList)
|
||||
{
|
||||
if (!displayed.contains(cm.toLowerCase()))
|
||||
{
|
||||
TE.addToConsole(cm);
|
||||
displayed.add(cm.toLowerCase());
|
||||
}
|
||||
}
|
||||
TE.addToConsole("----------------------");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
TE.addToConsole("----------------------");
|
||||
TE.addToConsole("Listing commands");
|
||||
TE.addToConsole("----------------------");
|
||||
TE.addToConsole("Help command");
|
||||
|
||||
for (TerminalCommand cc : CommandRegistry.COMMANDS)
|
||||
{
|
||||
if (cc.showOnHelp(player, TE) && cc.canMachineUse(TE))
|
||||
{
|
||||
List<String> ccList = cc.getCmdUses(player, TE);
|
||||
for (String cm : ccList)
|
||||
{
|
||||
TE.addToConsole(cm);
|
||||
}
|
||||
}
|
||||
}
|
||||
TE.addToConsole("-----------------------");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerUse(EntityPlayer var1, ISpecialAccess mm)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean showOnHelp(EntityPlayer player, ISpecialAccess mm)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getCmdUses(EntityPlayer player, ISpecialAccess mm)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canMachineUse(ISpecialAccess mm)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,23 +1,24 @@
|
|||
package dark.core.prefab.terminal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import dark.api.AccessLevel;
|
||||
import dark.api.ISpecialAccess;
|
||||
import dark.api.ITerminal;
|
||||
import dark.api.access.AccessLevel;
|
||||
import dark.api.access.ISpecialAccess;
|
||||
import dark.api.access.ITerminalCommand;
|
||||
|
||||
public class CommandUser extends TerminalCommand
|
||||
public class CommandUser implements ITerminalCommand
|
||||
{
|
||||
@Override
|
||||
public String getCommandPrefix()
|
||||
public String getCommandName()
|
||||
{
|
||||
return "users";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean processCommand(EntityPlayer player, ITerminal terminal, String[] args)
|
||||
public boolean called(EntityPlayer player, ITerminal terminal, String[] args)
|
||||
{
|
||||
if (args[0].equalsIgnoreCase("users") && args.length > 1 && args[1] != null && terminal instanceof ISpecialAccess)
|
||||
{
|
||||
|
@ -82,31 +83,46 @@ public class CommandUser extends TerminalCommand
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerUse(EntityPlayer var1, ISpecialAccess mm)
|
||||
public boolean canSupport(ITerminal mm)
|
||||
{
|
||||
return mm.getUsers().size() <= 0 || mm.getUserAccess(var1.username).ordinal() >= AccessLevel.ADMIN.ordinal();
|
||||
return mm != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean showOnHelp(EntityPlayer player, ISpecialAccess mm)
|
||||
public Set<String> getPermissionNodes()
|
||||
{
|
||||
return this.canPlayerUse(player, mm);
|
||||
Set<String> nodes = new HashSet<String>();
|
||||
nodes.add("add");
|
||||
nodes.add("remove");
|
||||
nodes.add("list");
|
||||
return nodes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getCmdUses(EntityPlayer player, ISpecialAccess mm)
|
||||
public String getNode(String[] args)
|
||||
{
|
||||
List<String> cmds = new ArrayList<String>();
|
||||
cmds.add("users list");
|
||||
cmds.add("users add [player]");
|
||||
cmds.add("users remove [player]");
|
||||
return cmds;
|
||||
if (args != null && args.length >= 1)
|
||||
{
|
||||
if (args[0] != null && args[0].equalsIgnoreCase(this.getCommandName()))
|
||||
{
|
||||
if (args.length >= 2)
|
||||
{
|
||||
if (args[1] != null && args[1].equalsIgnoreCase("add"))
|
||||
{
|
||||
return "users.add";
|
||||
}
|
||||
if (args[1] != null && args[1].equalsIgnoreCase("remove"))
|
||||
{
|
||||
return "users.remove";
|
||||
}
|
||||
if (args[1] != null && args[1].equalsIgnoreCase("list"))
|
||||
{
|
||||
return "users.list";
|
||||
}
|
||||
}
|
||||
return "users";
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canMachineUse(ISpecialAccess mm)
|
||||
{
|
||||
return mm instanceof ISpecialAccess;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,45 +0,0 @@
|
|||
package dark.core.prefab.terminal;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import dark.api.ISpecialAccess;
|
||||
import dark.api.ITerminal;
|
||||
|
||||
/** @author Calclavia, DarkGuardsman */
|
||||
public abstract class TerminalCommand
|
||||
{
|
||||
/** what the commands starts with /help /time /day
|
||||
*
|
||||
* @return */
|
||||
public abstract String getCommandPrefix();
|
||||
|
||||
/** Executes the command
|
||||
*
|
||||
* @param var1
|
||||
* @param args */
|
||||
public abstract boolean processCommand(EntityPlayer player, ITerminal terminal, String[] args);
|
||||
|
||||
/** Returns true if the given command sender is allowed to use this command. */
|
||||
public abstract boolean canPlayerUse(EntityPlayer player, ISpecialAccess specialAccess);
|
||||
|
||||
/** should this command show on /help
|
||||
*
|
||||
* @param player - used to find if it should show
|
||||
* @return true/false */
|
||||
public abstract boolean showOnHelp(EntityPlayer player, ISpecialAccess specialAccess);
|
||||
|
||||
/** returns the list of commands that the player can view on /help keep it shorter than 22 chars
|
||||
* to fit on Cmd Gui
|
||||
*
|
||||
* @param player
|
||||
* @return */
|
||||
public abstract List<String> getCmdUses(EntityPlayer player, ISpecialAccess specialAccess);
|
||||
|
||||
/** some cmds can only be use on some machines but will be access by all machines. to prevent the
|
||||
* cmd from activating on the machine return false
|
||||
*
|
||||
* @param mm
|
||||
* @return */
|
||||
public abstract boolean canMachineUse(ISpecialAccess specialAccess);
|
||||
}
|
|
@ -5,15 +5,16 @@ import java.util.List;
|
|||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import dark.api.ITerminal;
|
||||
import dark.api.access.ITerminalCommand;
|
||||
|
||||
/** @author Calclavia, DarkGuardsman */
|
||||
public class CommandRegistry
|
||||
/** @author DarkGuardsman */
|
||||
public class TerminalCommandRegistry
|
||||
{
|
||||
public static final List<TerminalCommand> COMMANDS = new ArrayList<TerminalCommand>();
|
||||
public static final List<ITerminalCommand> COMMANDS = new ArrayList<ITerminalCommand>();
|
||||
|
||||
/** @param prefix - what the command starts with for example /time
|
||||
* @param cmd - Cmd instance that will execute the command */
|
||||
public static void register(TerminalCommand cmd)
|
||||
public static void register(ITerminalCommand cmd)
|
||||
{
|
||||
if (!COMMANDS.contains(cmd))
|
||||
{
|
||||
|
@ -22,40 +23,32 @@ public class CommandRegistry
|
|||
}
|
||||
|
||||
/** When a player uses a command in any CMD machine it pass threw here first
|
||||
*
|
||||
*
|
||||
* @param terminal - The terminal, can be cast to TileEntity. */
|
||||
public static void onCommand(EntityPlayer player, ITerminal terminal, String cmd)
|
||||
public static boolean onCommand(EntityPlayer player, ITerminal terminal, String cmd)
|
||||
{
|
||||
if (cmd != null && cmd != "")
|
||||
{
|
||||
TerminalCommand currentCommand = null;
|
||||
String[] args = cmd.split(" ");
|
||||
terminal.addToConsole("\u00a7A" + player.username + ": " + cmd);
|
||||
|
||||
if (args[0] != null)
|
||||
{
|
||||
for (TerminalCommand command : COMMANDS)
|
||||
for (ITerminalCommand command : COMMANDS)
|
||||
{
|
||||
if (command.getCommandPrefix().equalsIgnoreCase(args[0]))
|
||||
if (command.getCommandName().equalsIgnoreCase(args[0]))
|
||||
{
|
||||
if (!command.canMachineUse(terminal))
|
||||
if (command.canSupport(terminal) && command.getNode(args) != null)
|
||||
{
|
||||
terminal.addToConsole("N/A");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!command.canPlayerUse(player, terminal))
|
||||
if (!terminal.canUse(command.getNode(args), player))
|
||||
{
|
||||
terminal.addToConsole("Access Denied.");
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (command.processCommand(player, terminal, args))
|
||||
if (command.called(player, terminal, args))
|
||||
{
|
||||
currentCommand = command;
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -65,6 +58,7 @@ public class CommandRegistry
|
|||
|
||||
terminal.addToConsole("Unknown Command.");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
|
@ -122,7 +122,7 @@ public abstract class TileEntityTerminal extends TileEntityEnergyMachine impleme
|
|||
{
|
||||
if (id.equalsIgnoreCase(SimplePacketTypes.GUI_COMMAND.name))
|
||||
{
|
||||
CommandRegistry.onCommand(this.worldObj.getPlayerEntityByName(dis.readUTF()), this, dis.readUTF());
|
||||
TerminalCommandRegistry.onCommand(this.worldObj.getPlayerEntityByName(dis.readUTF()), this, dis.readUTF());
|
||||
this.sendTerminalOutputToClients();
|
||||
return true;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue