From c4d59a027b22b9ff4f1f35aed36e384ae95849b1 Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 28 Nov 2013 12:12:25 -0500 Subject: [PATCH] Starting on the basic of Global Access Lists --- src/dark/api/access/AccessProfile.java | 270 ++++++++++++++++++ src/dark/api/access/IProfileContainer.java | 15 + .../client/renders/ItemRenderFluidCan.java | 4 + .../core/prefab/machine/BlockMachine.java | 16 +- .../core/prefab/machine/TileEntityInv.java | 9 +- 5 files changed, 308 insertions(+), 6 deletions(-) create mode 100644 src/dark/api/access/AccessProfile.java create mode 100644 src/dark/api/access/IProfileContainer.java diff --git a/src/dark/api/access/AccessProfile.java b/src/dark/api/access/AccessProfile.java new file mode 100644 index 00000000..1fb32cd8 --- /dev/null +++ b/src/dark/api/access/AccessProfile.java @@ -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 groups = new ArrayList(); + /** 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 getUsers() + { + List users = new ArrayList(); + 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 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; + + } +} diff --git a/src/dark/api/access/IProfileContainer.java b/src/dark/api/access/IProfileContainer.java new file mode 100644 index 00000000..46bb578d --- /dev/null +++ b/src/dark/api/access/IProfileContainer.java @@ -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); +} diff --git a/src/dark/core/client/renders/ItemRenderFluidCan.java b/src/dark/core/client/renders/ItemRenderFluidCan.java index 16cd352d..b65f45cf 100644 --- a/src/dark/core/client/renders/ItemRenderFluidCan.java +++ b/src/dark/core/client/renders/ItemRenderFluidCan.java @@ -77,6 +77,10 @@ public class ItemRenderFluidCan implements IItemRenderer FMLClientHandler.instance().getClient().renderEngine.bindTexture((RenderBlockFluid.getFluidSheet(liquid))); 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.glPopAttrib(); diff --git a/src/dark/core/prefab/machine/BlockMachine.java b/src/dark/core/prefab/machine/BlockMachine.java index 2feb5063..ef161817 100644 --- a/src/dark/core/prefab/machine/BlockMachine.java +++ b/src/dark/core/prefab/machine/BlockMachine.java @@ -6,9 +6,11 @@ import java.util.Set; import net.minecraft.block.material.Material; import net.minecraft.client.renderer.texture.IconRegister; import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer; +import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.Icon; 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.SideOnly; +import dark.api.access.AccessUser; +import dark.api.access.ISpecialAccess; import dark.api.parts.INetworkPart; import dark.core.common.DarkMain; import dark.core.interfaces.IBlockActivated; @@ -30,7 +34,7 @@ import dark.core.registration.ModObjectRegistry.BlockBuildData; /** Basic TileEntity Container class designed to be used by generic machines. It is suggested that * each mod using this create there own basic block extending this to reduce need to use build data * per block. - * + * * @author Darkguardsman */ public abstract class BlockMachine extends BlockTile implements IExtraBlockInfo { @@ -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 public void onNeighborBlockChange(World world, int x, int y, int z, int blockID) { diff --git a/src/dark/core/prefab/machine/TileEntityInv.java b/src/dark/core/prefab/machine/TileEntityInv.java index 11ced9d2..a55fd1a9 100644 --- a/src/dark/core/prefab/machine/TileEntityInv.java +++ b/src/dark/core/prefab/machine/TileEntityInv.java @@ -184,6 +184,10 @@ public class TileEntityInv extends TileEntityAdvanced implements IExternalInv, I /* * 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 public AccessUser getUserAccess(String username) @@ -199,11 +203,6 @@ public class TileEntityInv extends TileEntityAdvanced implements IExternalInv, I 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 public List getUsers() {