Work on security framework

This commit is contained in:
aidancbrady 2016-03-19 22:03:17 -04:00
parent e53c00d2b8
commit 1b2aee283e
21 changed files with 667 additions and 19 deletions

View file

@ -199,6 +199,7 @@ public class GuiFormulaicAssemblicator extends GuiMekanism
GL11.glPushMatrix();
GL11.glEnable(GL11.GL_LIGHTING);
MekanismRenderer.blendOn();
GL11.glColor4f(1.0F, 1.0F, 1.0F, 0.4F);
itemRender.renderItemAndEffectIntoGUI(fontRendererObj, mc.getTextureManager(), stack, guiWidth + slot.xDisplayPosition, guiHeight + slot.yDisplayPosition);
MekanismRenderer.blendOff();
GL11.glDisable(GL11.GL_LIGHTING);

View file

@ -12,7 +12,10 @@ import mekanism.common.util.MekanismUtils.ResourceType;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import codechicken.lib.vec.Rectangle4i;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class GuiRedstoneControl extends GuiElement
{
TileEntity tileEntity;

View file

@ -0,0 +1,107 @@
package mekanism.client.gui.element;
import mekanism.api.Coord4D;
import mekanism.api.EnumColor;
import mekanism.api.util.ListUtils;
import mekanism.client.gui.IGuiWrapper;
import mekanism.client.sound.SoundHandler;
import mekanism.common.Mekanism;
import mekanism.common.network.PacketSecurityMode.SecurityModeMessage;
import mekanism.common.security.ISecurity;
import mekanism.common.security.ISecurity.SecurityMode;
import mekanism.common.util.LangUtils;
import mekanism.common.util.MekanismUtils;
import mekanism.common.util.MekanismUtils.ResourceType;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import codechicken.lib.vec.Rectangle4i;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class GuiSecurityTab extends GuiElement
{
TileEntity tileEntity;
public GuiSecurityTab(IGuiWrapper gui, TileEntity tile, ResourceLocation def)
{
super(MekanismUtils.getResource(ResourceType.GUI_ELEMENT, "GuiSecurityTab.png"), gui, def);
tileEntity = tile;
}
@Override
public Rectangle4i getBounds(int guiWidth, int guiHeight)
{
return new Rectangle4i(guiWidth + 176, guiHeight + 32, 26, 26);
}
@Override
public void renderBackground(int xAxis, int yAxis, int guiWidth, int guiHeight)
{
mc.renderEngine.bindTexture(RESOURCE);
guiObj.drawTexturedRect(guiWidth + 176, guiHeight + 32, 0, 0, 26, 26);
ISecurity tile = (ISecurity)tileEntity;
int renderX = 26 + (18*tile.getSecurity().getMode().ordinal());
if(tile.getSecurity().getOwner() != null && tile.getSecurity().getOwner().equals(mc.thePlayer.getCommandSenderName()))
{
if(xAxis >= 179 && xAxis <= 197 && yAxis >= 36 && yAxis <= 54)
{
guiObj.drawTexturedRect(guiWidth + 179, guiHeight + 36, renderX, 0, 18, 18);
}
else {
guiObj.drawTexturedRect(guiWidth + 179, guiHeight + 36, renderX, 18, 18, 18);
}
}
else {
guiObj.drawTexturedRect(guiWidth + 179, guiHeight + 36, renderX, 36, 18, 18);
}
mc.renderEngine.bindTexture(defaultLocation);
}
@Override
public void renderForeground(int xAxis, int yAxis)
{
mc.renderEngine.bindTexture(RESOURCE);
ISecurity control = (ISecurity)tileEntity;
if(xAxis >= 179 && xAxis <= 197 && yAxis >= 36 && yAxis <= 54)
{
String securityText = EnumColor.GREY + LangUtils.localize("gui.security") + ": " + control.getSecurity().getMode().getDisplay();
String ownerText = ISecurity.getOwnerDisplay(mc.thePlayer.getCommandSenderName(), control.getSecurity().getOwner());
displayTooltips(ListUtils.asList(securityText, ownerText), xAxis, yAxis);
}
mc.renderEngine.bindTexture(defaultLocation);
}
@Override
public void preMouseClicked(int xAxis, int yAxis, int button) {}
@Override
public void mouseClicked(int xAxis, int yAxis, int button)
{
ISecurity control = (ISecurity)tileEntity;
if(button == 0)
{
if(control.getSecurity().getOwner() != null && mc.thePlayer.getCommandSenderName().equals(control.getSecurity().getOwner()))
{
if(xAxis >= 179 && xAxis <= 197 && yAxis >= 36 && yAxis <= 54)
{
SecurityMode current = control.getSecurity().getMode();
int ordinalToSet = current.ordinal() < (SecurityMode.values().length-1) ? current.ordinal()+1 : 0;
SoundHandler.playSound("gui.button.press");
Mekanism.packetHandler.sendToServer(new SecurityModeMessage(Coord4D.get(tileEntity), SecurityMode.values()[ordinalToSet]));
}
}
}
}
}

View file

@ -91,17 +91,17 @@ public class HashList<T> implements Iterable<T>
return list.size();
}
public void swap( int source, int target )
public void swap(int source, int target)
{
// Make sure both source and target ar legal values
if( source == target ) return;
if( source < 0 || target < 0 ) return;
if( source >= list.size() || target >= list.size() ) return;
// Make sure both source and target are legal values
if(source == target) return;
if(source < 0 || target < 0) return;
if(source >= list.size() || target >= list.size()) return;
// Perform swap
T temp = list.get( source );
list.set( source, list.get( target ) );
list.set( target, temp );
T temp = list.get(source);
list.set(source, list.get( target));
list.set(target, temp);
}
@Override

View file

@ -73,6 +73,7 @@ import mekanism.common.recipe.ShapelessMekanismRecipe;
import mekanism.common.recipe.inputs.ItemStackInput;
import mekanism.common.recipe.machines.SmeltingRecipe;
import mekanism.common.recipe.outputs.ItemStackOutput;
import mekanism.common.security.SecurityFrequency;
import mekanism.common.tile.TileEntityAdvancedBoundingBlock;
import mekanism.common.tile.TileEntityBoundingBlock;
import mekanism.common.tile.TileEntityCardboardBox;
@ -183,6 +184,8 @@ public class Mekanism
public static FrequencyManager publicEntangloporters = new FrequencyManager(InventoryFrequency.class, "Entangloporter");
public static Map<String, FrequencyManager> privateEntangloporters = new HashMap<String, FrequencyManager>();
public static FrequencyManager securityFrequencies = new FrequencyManager(SecurityFrequency.class, "Security");
/** Mekanism creative tab */
public static CreativeTabMekanism tabMekanism = new CreativeTabMekanism();

View file

@ -55,6 +55,8 @@ import mekanism.common.network.PacketRobit;
import mekanism.common.network.PacketRobit.RobitMessage;
import mekanism.common.network.PacketScubaTankData;
import mekanism.common.network.PacketScubaTankData.ScubaTankDataMessage;
import mekanism.common.network.PacketSecurityMode;
import mekanism.common.network.PacketSecurityMode.SecurityModeMessage;
import mekanism.common.network.PacketSimpleGui;
import mekanism.common.network.PacketSimpleGui.SimpleGuiMessage;
import mekanism.common.network.PacketTileEntity;
@ -102,7 +104,7 @@ public class PacketHandler
netHandler.registerMessage(PacketDataRequest.class, DataRequestMessage.class, 7, Side.SERVER);
netHandler.registerMessage(PacketOredictionificatorGui.class, OredictionificatorGuiMessage.class, 8, Side.CLIENT);
netHandler.registerMessage(PacketOredictionificatorGui.class, OredictionificatorGuiMessage.class, 8, Side.SERVER);
//EMPTY SLOT 9
netHandler.registerMessage(PacketSecurityMode.class, SecurityModeMessage.class, 9, Side.SERVER);
netHandler.registerMessage(PacketPortableTeleporter.class, PortableTeleporterMessage.class, 10, Side.CLIENT);
netHandler.registerMessage(PacketPortableTeleporter.class, PortableTeleporterMessage.class, 10, Side.SERVER);
netHandler.registerMessage(PacketRemoveUpgrade.class, RemoveUpgradeMessage.class, 11, Side.SERVER);

View file

@ -17,4 +17,6 @@ public interface ITileComponent
public void write(NBTTagCompound nbtTags);
public void write(ArrayList data);
public void invalidate();
}

View file

@ -0,0 +1,67 @@
package mekanism.common.network;
import io.netty.buffer.ByteBuf;
import mekanism.api.Coord4D;
import mekanism.common.PacketHandler;
import mekanism.common.network.PacketSecurityMode.SecurityModeMessage;
import mekanism.common.security.ISecurity;
import mekanism.common.security.ISecurity.SecurityMode;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
public class PacketSecurityMode implements IMessageHandler<SecurityModeMessage, IMessage>
{
@Override
public IMessage onMessage(SecurityModeMessage message, MessageContext context)
{
EntityPlayer player = PacketHandler.getPlayer(context);
TileEntity tileEntity = message.coord4D.getTileEntity(player.worldObj);
if(tileEntity instanceof ISecurity)
{
String owner = ((ISecurity)tileEntity).getSecurity().getOwner();
if(owner != null && player.getCommandSenderName().equals(owner))
{
((ISecurity)tileEntity).getSecurity().setMode(message.value);
}
}
return null;
}
public static class SecurityModeMessage implements IMessage
{
public Coord4D coord4D;
public SecurityMode value;
public SecurityModeMessage() {}
public SecurityModeMessage(Coord4D coord, SecurityMode control)
{
coord4D = coord;
value = control;
}
@Override
public void toBytes(ByteBuf dataStream)
{
dataStream.writeInt(coord4D.xCoord);
dataStream.writeInt(coord4D.yCoord);
dataStream.writeInt(coord4D.zCoord);
dataStream.writeInt(coord4D.dimensionId);
dataStream.writeInt(value.ordinal());
}
@Override
public void fromBytes(ByteBuf dataStream)
{
coord4D = Coord4D.read(dataStream);
value = SecurityMode.values()[dataStream.readInt()];
}
}
}

View file

@ -0,0 +1,106 @@
package mekanism.common.security;
import mekanism.api.EnumColor;
import mekanism.common.Mekanism;
import mekanism.common.frequency.Frequency;
import mekanism.common.tile.component.TileComponentSecurity;
import mekanism.common.util.LangUtils;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
public interface ISecurity
{
public TileComponentSecurity getSecurity();
public enum SecurityMode
{
PUBLIC(EnumColor.BRIGHT_GREEN + "security.public"),
PRIVATE(EnumColor.RED + "security.private"),
TRUSTED(EnumColor.ORANGE + "security.trusted");
private String display;
private EnumColor color;
public String getDisplay()
{
return color + LangUtils.localize(display);
}
private SecurityMode(String s)
{
display = s;
}
}
public static boolean canAccess(EntityPlayer player, ItemStack stack)
{
if(stack == null || !(stack.getItem() instanceof ISecurityItem))
{
return true;
}
ISecurityItem security = (ISecurityItem)stack.getItem();
return canAccess(security.getSecurity(stack), player.getCommandSenderName(), security.getOwner(stack));
}
public static boolean canAccess(EntityPlayer player, TileEntity tile)
{
if(tile == null || !(tile instanceof ISecurity))
{
return true;
}
ISecurity security = (ISecurity)tile;
return canAccess(security.getSecurity().getMode(), player.getCommandSenderName(), security.getSecurity().getOwner());
}
public static boolean canAccess(SecurityMode mode, String username, String owner)
{
if(owner == null || username.equals(owner))
{
return true;
}
if(mode == SecurityMode.PUBLIC)
{
return true;
}
else if(mode == SecurityMode.TRUSTED)
{
SecurityFrequency freq = getFrequency(owner);
if(freq == null || freq.trusted.contains(username))
{
return true;
}
}
return false;
}
public static SecurityFrequency getFrequency(String owner)
{
for(Frequency f : Mekanism.securityFrequencies.getFrequencies())
{
if(f instanceof SecurityFrequency && f.owner.equals(owner))
{
return (SecurityFrequency)f;
}
}
return null;
}
public static String getOwnerDisplay(String user, String owner)
{
if(owner == null)
{
return EnumColor.RED + LangUtils.localize("gui.noOwner");
}
return EnumColor.GREY + LangUtils.localize("gui.owner") + ": " + (user.equals(owner) ? EnumColor.BRIGHT_GREEN : EnumColor.RED) + owner;
}
}

View file

@ -0,0 +1,15 @@
package mekanism.common.security;
import mekanism.common.security.ISecurity.SecurityMode;
import net.minecraft.item.ItemStack;
public interface ISecurityItem
{
public String getOwner(ItemStack stack);
public void setOwner(ItemStack stack, String owner);
public SecurityMode getSecurity(ItemStack stack);
public void setSecurity(ItemStack stack, SecurityMode mode);
}

View file

@ -0,0 +1,111 @@
package mekanism.common.security;
import io.netty.buffer.ByteBuf;
import java.util.ArrayList;
import mekanism.common.HashList;
import mekanism.common.PacketHandler;
import mekanism.common.frequency.Frequency;
import mekanism.common.security.ISecurity.SecurityMode;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraftforge.common.util.Constants.NBT;
public class SecurityFrequency extends Frequency
{
public boolean override;
public HashList<String> trusted = new HashList<String>();
public SecurityMode securityMode = SecurityMode.PUBLIC;
public SecurityFrequency(String o)
{
super("Security", o);
}
public SecurityFrequency(NBTTagCompound nbtTags)
{
super(nbtTags);
}
public SecurityFrequency(ByteBuf dataStream)
{
super(dataStream);
}
@Override
public void write(NBTTagCompound nbtTags)
{
super.write(nbtTags);
nbtTags.setBoolean("override", override);
nbtTags.setInteger("securityMode", securityMode.ordinal());
if(!trusted.isEmpty())
{
NBTTagList trustedList = new NBTTagList();
for(String s : trusted)
{
trustedList.appendTag(new NBTTagString(s));
}
nbtTags.setTag("trusted", trustedList);
}
}
@Override
protected void read(NBTTagCompound nbtTags)
{
super.read(nbtTags);
override = nbtTags.getBoolean("override");
securityMode = SecurityMode.values()[nbtTags.getInteger("securityMode")];
if(nbtTags.hasKey("trusted"))
{
NBTTagList trustedList = nbtTags.getTagList("trusted", NBT.TAG_STRING);
for(int i = 0; i < trustedList.tagCount(); i++)
{
trusted.add(trustedList.getStringTagAt(i));
}
}
}
@Override
public void write(ArrayList data)
{
super.write(data);
data.add(override);
data.add(securityMode.ordinal());
data.add(trusted.size());
for(String s : trusted)
{
data.add(trusted);
}
}
@Override
protected void read(ByteBuf dataStream)
{
super.read(dataStream);
override = dataStream.readBoolean();
securityMode = SecurityMode.values()[dataStream.readInt()];
trusted.clear();
int size = dataStream.readInt();
for(int i = 0; i < size; i++)
{
trusted.add(PacketHandler.readString(dataStream));
}
}
}

View file

@ -137,6 +137,17 @@ public abstract class TileEntityBasicBlock extends TileEntity implements IWrench
return data;
}
@Override
public void invalidate()
{
super.invalidate();
for(ITileComponent component : components)
{
component.invalidate();
}
}
@Override
public void validate()

View file

@ -192,11 +192,6 @@ public class TileEntityQuantumEntangloporter extends TileEntityElectricBlock imp
public void setFrequency(String name, boolean publicFreq)
{
if(name.equals(frequency))
{
return;
}
FrequencyManager manager = getManager(new InventoryFrequency(name, null).setPublic(publicFreq));
manager.deactivate(Coord4D.get(this));

View file

@ -165,11 +165,6 @@ public class TileEntityTeleporter extends TileEntityElectricBlock implements ICo
public void setFrequency(String name, boolean publicFreq)
{
if(name.equals(frequency))
{
return;
}
FrequencyManager manager = getManager(new Frequency(name, null).setPublic(publicFreq));
manager.deactivate(Coord4D.get(this));

View file

@ -216,6 +216,9 @@ public class TileComponentConfig implements ITileComponent
}
}
@Override
public void invalidate() {}
public boolean isEjecting(TransmissionType type)
{
return ejecting.get(type.ordinal());

View file

@ -374,6 +374,9 @@ public class TileComponentEjector implements ITileComponent
}
}
@Override
public void invalidate() {}
private boolean getEjecting(TransmissionType type)
{
return ((ISideConfiguration)tileEntity).getConfig().isEjecting(type);

View file

@ -0,0 +1,220 @@
package mekanism.common.tile.component;
import io.netty.buffer.ByteBuf;
import java.util.ArrayList;
import mekanism.api.Coord4D;
import mekanism.common.Mekanism;
import mekanism.common.PacketHandler;
import mekanism.common.base.ITileComponent;
import mekanism.common.frequency.Frequency;
import mekanism.common.frequency.FrequencyManager;
import mekanism.common.security.ISecurity.SecurityMode;
import mekanism.common.security.SecurityFrequency;
import mekanism.common.tile.TileEntityContainerBlock;
import mekanism.common.util.MekanismUtils;
import net.minecraft.nbt.NBTTagCompound;
public class TileComponentSecurity implements ITileComponent
{
/** TileEntity implementing this component. */
public TileEntityContainerBlock tileEntity;
private String owner;
private SecurityMode securityMode = SecurityMode.PUBLIC;
private SecurityFrequency frequency;
public TileComponentSecurity(TileEntityContainerBlock tile)
{
tileEntity = tile;
tile.components.add(this);
}
public String getOwner()
{
return owner;
}
public void setOwner(String o)
{
frequency = null;
owner = o;
}
public SecurityMode getMode()
{
return securityMode;
}
public void setMode(SecurityMode mode)
{
securityMode = mode;
}
public FrequencyManager getManager(Frequency freq)
{
if(owner == null || freq == null)
{
return null;
}
return Mekanism.securityFrequencies;
}
public void setFrequency(String owner)
{
FrequencyManager manager = Mekanism.securityFrequencies;
manager.deactivate(Coord4D.get(tileEntity));
for(Frequency freq : manager.getFrequencies())
{
if(freq.owner.equals(owner))
{
frequency = (SecurityFrequency)freq;
frequency.activeCoords.add(Coord4D.get(tileEntity));
return;
}
}
Frequency freq = new SecurityFrequency(owner).setPublic(true);
freq.activeCoords.add(Coord4D.get(tileEntity));
manager.addFrequency(freq);
frequency = (SecurityFrequency)freq;
MekanismUtils.saveChunk(tileEntity);
tileEntity.markDirty();
}
@Override
public void tick()
{
if(!tileEntity.getWorldObj().isRemote)
{
if(frequency == null && owner != null)
{
setFrequency(owner);
}
FrequencyManager manager = getManager(frequency);
if(manager != null)
{
if(frequency != null && !frequency.valid)
{
frequency = (SecurityFrequency)manager.validateFrequency(owner, Coord4D.get(tileEntity), frequency);
}
if(frequency != null)
{
frequency = (SecurityFrequency)manager.update(owner, Coord4D.get(tileEntity), frequency);
}
}
else {
frequency = null;
}
}
}
@Override
public void read(NBTTagCompound nbtTags)
{
securityMode = SecurityMode.values()[nbtTags.getInteger("securityMode")];
if(nbtTags.hasKey("owner"))
{
owner = nbtTags.getString("owner");
}
if(nbtTags.hasKey("frequency"))
{
frequency = new SecurityFrequency(nbtTags.getCompoundTag("frequency"));
frequency.valid = false;
}
}
@Override
public void read(ByteBuf dataStream)
{
securityMode = SecurityMode.values()[dataStream.readInt()];
if(dataStream.readBoolean())
{
owner = PacketHandler.readString(dataStream);
}
else {
owner = null;
}
if(dataStream.readBoolean())
{
frequency = new SecurityFrequency(dataStream);
}
else {
frequency = null;
}
}
@Override
public void write(NBTTagCompound nbtTags)
{
nbtTags.setInteger("securityMode", securityMode.ordinal());
if(owner != null)
{
nbtTags.setString("owner", owner);
}
if(frequency != null)
{
NBTTagCompound frequencyTag = new NBTTagCompound();
frequency.write(frequencyTag);
nbtTags.setTag("frequency", frequencyTag);
}
}
@Override
public void write(ArrayList data)
{
data.add(securityMode.ordinal());
if(owner != null)
{
data.add(true);
data.add(owner);
}
else {
data.add(false);
}
if(frequency != null)
{
data.add(true);
frequency.write(data);
}
else {
data.add(false);
}
}
@Override
public void invalidate()
{
if(!tileEntity.getWorldObj().isRemote)
{
if(frequency != null)
{
FrequencyManager manager = getManager(frequency);
if(manager != null)
{
manager.deactivate(Coord4D.get(tileEntity));
}
}
}
}
}

View file

@ -226,4 +226,7 @@ public class TileComponentUpgrade implements ITileComponent
{
Upgrade.saveMap(upgrades, nbtTags);
}
@Override
public void invalidate() {}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

View file

@ -540,6 +540,7 @@ gui.autoModeToggle=Turn Auto-Mode
gui.craftSingle=Craft Single Item
gui.craftAvailable=Craft Available Items
gui.fillEmpty=Fill/Empty Grid
gui.noOwner=No Owner
gui.reactor.injectionRate=Injection Rate