Starting on the basic of Global Access Lists

This commit is contained in:
Robert 2013-11-28 12:12:25 -05:00
parent e54ceedcec
commit c4d59a027b
5 changed files with 308 additions and 6 deletions

View file

@ -0,0 +1,270 @@
package dark.api.access;
import java.io.File;
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 net.minecraft.server.MinecraftServer;
import net.minecraft.tileentity.TileEntity;
import dark.api.IVirtualObject;
import dark.core.prefab.terminal.TerminalCommandRegistry;
import dark.core.save.NBTFileHelper;
import dark.core.save.SaveManager;
public class AccessProfile implements ISpecialAccess, IVirtualObject
{
/** A list of user access data. */
protected List<AccessGroup> groups = new ArrayList<AccessGroup>();
/** Display name */
protected String profileName = "";
/** Only used by global profiles that have no defined container. Also LocalHost means it was
* created by a tileEntity */
protected String profileID = "LocalHost";
protected boolean global = false;
protected File saveFile;
public AccessProfile()
{
if (global)
{
SaveManager.register(this);
}
}
public AccessProfile(NBTTagCompound nbt)
{
this(nbt, false);
}
public AccessProfile(NBTTagCompound nbt, boolean global)
{
this();
this.load(nbt);
if (this.profileName == null || this.profileID == null)
{
if (!global)
{
this.generateNew("Default", null);
}
else
{
this.generateNew("New Group", "global");
}
}
}
public AccessProfile generateNew(String name, Object object)
{
TerminalCommandRegistry.loadNewGroupSet(this);
this.profileName = name;
name.replaceAll(" ", "");
String id = null;
// Created by player for personal use
if (object instanceof EntityPlayer)
{
id = ((EntityPlayer) object).username + "_" + System.currentTimeMillis();
}//Created by a tile
else if (object instanceof TileEntity || object == null)
{
id = "LocalHost:" + name;
}//created by the game or player for global use
else if (object instanceof String && ((String) object).equalsIgnoreCase("global"))
{
id = "P_" + name + "_" + System.currentTimeMillis();
this.global = true;
}
this.profileID = id;
return this;
}
public String getName()
{
return this.profileName;
}
public String getID()
{
return this.profileID;
}
public boolean isGlobal()
{
return this.global;
}
@Override
public AccessUser getUserAccess(String username)
{
for (AccessGroup group : this.groups)
{
AccessUser user = group.getMember(username);
if (user != null)
{
return user;
}
}
return new AccessUser(username);
}
@Override
public List<AccessUser> getUsers()
{
List<AccessUser> users = new ArrayList<AccessUser>();
for (AccessGroup group : this.groups)
{
users.addAll(group.getMembers());
}
return users;
}
@Override
public boolean setUserAccess(String player, AccessGroup g, boolean save)
{
return setUserAccess(new AccessUser(player).setTempary(save), g);
}
@Override
public boolean setUserAccess(AccessUser user, AccessGroup group)
{
boolean bool = false;
if (user != null && user.getName() != null)
{
bool = this.removeUserAccess(user.getName()) && group == null;
if (group != null)
{
bool = group.addMemeber(user);
}
if (bool)
{
this.onProfileUpdate();
}
}
return bool;
}
public boolean removeUserAccess(String player)
{
boolean re = false;
for (AccessGroup group : this.groups)
{
AccessUser user = group.getMember(player);
if (user != null && group.removeMemeber(user))
{
re = true;
}
}
if (re)
{
this.onProfileUpdate();
}
return re;
}
public void onProfileUpdate()
{
}
@Override
public AccessGroup getGroup(String name)
{
for (AccessGroup group : this.getGroups())
{
if (group.getName().equalsIgnoreCase(name))
{
return group;
}
}
return null;
}
@Override
public boolean addGroup(AccessGroup group)
{
if (!this.groups.contains(group))
{
for (AccessGroup g : this.groups)
{
if (group.getName().equalsIgnoreCase(g.getName()))
{
return false;
}
}
if (this.groups.add(group))
{
this.onProfileUpdate();
return true;
}
}
return false;
}
@Override
public AccessGroup getOwnerGroup()
{
return this.getGroup("owner");
}
@Override
public List<AccessGroup> getGroups()
{
if (this.groups == null || this.groups.isEmpty())
{
TerminalCommandRegistry.loadNewGroupSet(this);
}
return this.groups;
}
@Override
public void save(NBTTagCompound nbt)
{
this.profileName = nbt.getString("name");
this.global = nbt.getBoolean("global");
NBTTagList userList = nbt.getTagList("groups");
if (userList != null && userList.tagCount() > 0)
{
this.groups.clear();
for (int i = 0; i < userList.tagCount(); i++)
{
AccessGroup group = new AccessGroup("");
group.load((NBTTagCompound) userList.tagAt(i));
this.groups.add(group);
}
}
}
@Override
public void load(NBTTagCompound nbt)
{
nbt.setString("name", this.profileName);
nbt.setBoolean("global", this.global);
NBTTagList usersTag = new NBTTagList();
for (AccessGroup group : this.getGroups())
{
usersTag.appendTag(group.save(new NBTTagCompound()));
}
nbt.setTag("groups", usersTag);
}
@Override
public File getSaveFile()
{
if (this.saveFile == null)
{
this.saveFile = new File(NBTFileHelper.getWorldSaveDirectory(MinecraftServer.getServer().getFolderName()), "CoreMachine/Access/" + this.getID() + ".dat");
}
return this.saveFile;
}
@Override
public void setSaveFile(File file)
{
this.saveFile = file;
}
}

View file

@ -0,0 +1,15 @@
package dark.api.access;
/** Applied to tileEntities that contain an access profile that describes how the tile interacts
* with users
*
* @author DarkGuardsman */
public interface IProfileContainer
{
/** Return the active profile of the machine. When calling this avoid editing the profile */
public AccessProfile getAccessProfile();
/** Strait up yes or no can this user access the tile. Any future checks should be done after the
* user has accessed the machine */
public boolean canAccess(String username);
}

View file

@ -77,6 +77,10 @@ public class ItemRenderFluidCan implements IItemRenderer
FMLClientHandler.instance().getClient().renderEngine.bindTexture((RenderBlockFluid.getFluidSheet(liquid))); FMLClientHandler.instance().getClient().renderEngine.bindTexture((RenderBlockFluid.getFluidSheet(liquid)));
int cap = ((ItemFluidCan) CoreRecipeLoader.itemFluidCan).getCapacity(item); int cap = ((ItemFluidCan) CoreRecipeLoader.itemFluidCan).getCapacity(item);
if(liquid.getFluid().isGaseous())
{
cap = liquid.amount;
}
GL11.glCallList(displayList[(int) ((float) liquid.amount / (float) (cap) * (RenderBlockFluid.DISPLAY_STAGES - 1))]); GL11.glCallList(displayList[(int) ((float) liquid.amount / (float) (cap) * (RenderBlockFluid.DISPLAY_STAGES - 1))]);
GL11.glPopAttrib(); GL11.glPopAttrib();

View file

@ -6,9 +6,11 @@ import java.util.Set;
import net.minecraft.block.material.Material; import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister; import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer; import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container; import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory; import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon; import net.minecraft.util.Icon;
import net.minecraft.world.World; import net.minecraft.world.World;
@ -20,6 +22,8 @@ import com.builtbroken.common.Pair;
import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly; import cpw.mods.fml.relauncher.SideOnly;
import dark.api.access.AccessUser;
import dark.api.access.ISpecialAccess;
import dark.api.parts.INetworkPart; import dark.api.parts.INetworkPart;
import dark.core.common.DarkMain; import dark.core.common.DarkMain;
import dark.core.interfaces.IBlockActivated; import dark.core.interfaces.IBlockActivated;
@ -99,6 +103,16 @@ public abstract class BlockMachine extends BlockTile implements IExtraBlockInfo
} }
} }
@Override
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entity, ItemStack itemStack)
{
TileEntity tile = world.getBlockTileEntity(x, y, z);
if (tile instanceof ISpecialAccess && entity instanceof EntityPlayer)
{
((ISpecialAccess) tile).setUserAccess(new AccessUser((EntityPlayer) entity), ((ISpecialAccess) tile).getOwnerGroup());
}
}
@Override @Override
public void onNeighborBlockChange(World world, int x, int y, int z, int blockID) public void onNeighborBlockChange(World world, int x, int y, int z, int blockID)
{ {

View file

@ -184,6 +184,10 @@ public class TileEntityInv extends TileEntityAdvanced implements IExternalInv, I
/* /*
* User access * User access
*/ */
public boolean canOpen(String username)
{
return this.getUserAccess(username) != null && this.getUserAccess(username).hasNode(Nodes.INV_OPEN_NODE) || this.getOwnerGroup().getMembers().size() <= 0;
}
@Override @Override
public AccessUser getUserAccess(String username) public AccessUser getUserAccess(String username)
@ -199,11 +203,6 @@ public class TileEntityInv extends TileEntityAdvanced implements IExternalInv, I
return new AccessUser(username); return new AccessUser(username);
} }
public boolean canOpen(String username)
{
return this.getUserAccess(username) != null && this.getUserAccess(username).hasNode(Nodes.INV_OPEN_NODE) || this.getOwnerGroup().getMembers().size() <= 0;
}
@Override @Override
public List<AccessUser> getUsers() public List<AccessUser> getUsers()
{ {