Add network layer to make security preferences persistent

This commit is contained in:
aidancbrady 2016-04-30 11:26:12 -04:00
parent c4520a072b
commit fe99d25305
13 changed files with 284 additions and 41 deletions

View file

@ -1,5 +1,8 @@
package mekanism.client;
import java.util.HashMap;
import java.util.Map;
import mekanism.api.MekanismAPI;
import mekanism.api.MekanismAPI.BoxBlacklistEvent;
import mekanism.api.MekanismConfig.general;
@ -9,12 +12,15 @@ import mekanism.common.Mekanism;
import mekanism.common.base.IModule;
import mekanism.common.content.boiler.SynchronizedBoilerData;
import mekanism.common.network.PacketKey.KeyMessage;
import mekanism.common.security.SecurityData;
import net.minecraft.client.Minecraft;
import net.minecraft.client.settings.KeyBinding;
import net.minecraftforge.common.MinecraftForge;
public class MekanismClient extends Mekanism
{
public static Map<String, SecurityData> clientSecurityMap = new HashMap<String, SecurityData>();
public static VoiceClient voiceClient;
public static long ticksPassed = 0;
@ -32,6 +38,8 @@ public class MekanismClient extends Mekanism
public static void reset()
{
clientSecurityMap.clear();
if(general.voiceServerEnabled)
{
if(MekanismClient.voiceClient != null)

View file

@ -3,6 +3,7 @@ package mekanism.client.gui.element;
import mekanism.api.Coord4D;
import mekanism.api.EnumColor;
import mekanism.api.util.ListUtils;
import mekanism.client.MekanismClient;
import mekanism.client.gui.IGuiWrapper;
import mekanism.client.sound.SoundHandler;
import mekanism.common.Mekanism;
@ -10,6 +11,7 @@ import mekanism.common.network.PacketSecurityMode.SecurityModeMessage;
import mekanism.common.security.ISecurityItem;
import mekanism.common.security.ISecurityTile;
import mekanism.common.security.ISecurityTile.SecurityMode;
import mekanism.common.security.SecurityData;
import mekanism.common.security.SecurityFrequency;
import mekanism.common.util.LangUtils;
import mekanism.common.util.MekanismUtils;
@ -57,17 +59,17 @@ public class GuiSecurityTab extends GuiElement
guiObj.drawTexturedRect(guiWidth + 176, guiHeight + 32, 0, 0, 26, 26);
SecurityMode mode = getSecurity();
SecurityFrequency freq = getFrequency();
SecurityData data = MekanismClient.clientSecurityMap.get(getOwner());
if(freq != null && freq.override)
if(data != null && data.override)
{
mode = freq.securityMode;
mode = data.mode;
}
int renderX = 26 + (18*mode.ordinal());
if(getOwner() != null && getOwner().equals(mc.thePlayer.getCommandSenderName()) &&
(getFrequency() == null || !getFrequency().override))
(data == null || !data.override))
{
if(xAxis >= 179 && xAxis <= 197 && yAxis >= 36 && yAxis <= 54)
{
@ -91,12 +93,12 @@ public class GuiSecurityTab extends GuiElement
if(xAxis >= 179 && xAxis <= 197 && yAxis >= 36 && yAxis <= 54)
{
String securityDisplay = isItem ? SecurityUtils.getSecurityDisplay(getItem()) : SecurityUtils.getSecurityDisplay(tileEntity);
String securityDisplay = isItem ? SecurityUtils.getSecurityDisplay(getItem(), Side.CLIENT) : SecurityUtils.getSecurityDisplay(tileEntity, Side.CLIENT);
String securityText = EnumColor.GREY + LangUtils.localize("gui.security") + ": " + securityDisplay;
String ownerText = SecurityUtils.getOwnerDisplay(mc.thePlayer.getCommandSenderName(), getOwner());
String overrideText = EnumColor.RED + "(" + LangUtils.localize("gui.overridden") + ")";
if(isItem ? SecurityUtils.isOverridden(getItem()) : SecurityUtils.isOverridden(tileEntity))
if(isItem ? SecurityUtils.isOverridden(getItem(), Side.CLIENT) : SecurityUtils.isOverridden(tileEntity, Side.CLIENT))
{
displayTooltips(ListUtils.asList(securityText, ownerText, overrideText), xAxis, yAxis);
}

View file

@ -6,6 +6,8 @@ import mekanism.common.network.PacketJetpackData.JetpackDataMessage;
import mekanism.common.network.PacketJetpackData.JetpackPacket;
import mekanism.common.network.PacketScubaTankData.ScubaTankDataMessage;
import mekanism.common.network.PacketScubaTankData.ScubaTankPacket;
import mekanism.common.network.PacketSecurityUpdate.SecurityPacket;
import mekanism.common.network.PacketSecurityUpdate.SecurityUpdateMessage;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraftforge.common.MinecraftForge;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
@ -29,6 +31,7 @@ public class CommonPlayerTracker
Mekanism.packetHandler.sendTo(new BoxBlacklistMessage(), (EntityPlayerMP)event.player);
Mekanism.packetHandler.sendTo(new JetpackDataMessage(JetpackPacket.FULL, null, false), (EntityPlayerMP)event.player);
Mekanism.packetHandler.sendTo(new ScubaTankDataMessage(ScubaTankPacket.FULL, null, false), (EntityPlayerMP)event.player);
Mekanism.packetHandler.sendTo(new SecurityUpdateMessage(SecurityPacket.FULL, null, null), (EntityPlayerMP)event.player);
Mekanism.logger.info("Sent config to '" + event.player.getDisplayName() + ".'");
}

View file

@ -59,6 +59,8 @@ 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.PacketSecurityUpdate;
import mekanism.common.network.PacketSecurityUpdate.SecurityUpdateMessage;
import mekanism.common.network.PacketSimpleGui;
import mekanism.common.network.PacketSimpleGui.SimpleGuiMessage;
import mekanism.common.network.PacketTileEntity;
@ -134,6 +136,7 @@ public class PacketHandler
netHandler.registerMessage(PacketFlamethrowerData.class, FlamethrowerDataMessage.class, 27, Side.SERVER);
netHandler.registerMessage(PacketDropperUse.class, DropperUseMessage.class, 28, Side.SERVER);
netHandler.registerMessage(PacketEntityMove.class, EntityMoveMessage.class, 29, Side.CLIENT);
netHandler.registerMessage(PacketSecurityUpdate.class, SecurityUpdateMessage.class, 30, Side.CLIENT);
}
/**
@ -302,6 +305,20 @@ public class PacketHandler
netHandler.sendTo(message, player);
}
/**
* Send this message to everyone connected to the server.
* @param message - message to send
*/
public void sendToAll(IMessage message)
{
MinecraftServer server = MinecraftServer.getServer();
for(EntityPlayer player : (List<EntityPlayer>)server.getConfigurationManager().playerEntityList)
{
sendTo(message, (EntityPlayerMP)player);
}
}
/**
* Send this message to everyone within a certain range of a point.
*

View file

@ -74,9 +74,9 @@ public class ItemBlockEnergyCube extends ItemBlock implements IEnergizedItem, IE
if(hasSecurity(itemstack))
{
list.add(SecurityUtils.getOwnerDisplay(entityplayer.getCommandSenderName(), getOwner(itemstack)));
list.add(EnumColor.GREY + LangUtils.localize("gui.security") + ": " + SecurityUtils.getSecurityDisplay(itemstack));
list.add(EnumColor.GREY + LangUtils.localize("gui.security") + ": " + SecurityUtils.getSecurityDisplay(itemstack, Side.CLIENT));
if(SecurityUtils.isOverridden(itemstack))
if(SecurityUtils.isOverridden(itemstack, Side.CLIENT))
{
list.add(EnumColor.RED + "(" + LangUtils.localize("gui.overridden") + ")");
}

View file

@ -37,6 +37,7 @@ import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants.NBT;
import cpw.mods.fml.relauncher.Side;
public class ItemBlockGasTank extends ItemBlock implements IGasItem, ISustainedInventory, ITierItem, ISecurityItem
{
@ -135,9 +136,9 @@ public class ItemBlockGasTank extends ItemBlock implements IGasItem, ISustainedI
if(hasSecurity(itemstack))
{
list.add(SecurityUtils.getOwnerDisplay(entityplayer.getCommandSenderName(), getOwner(itemstack)));
list.add(EnumColor.GREY + LangUtils.localize("gui.security") + ": " + SecurityUtils.getSecurityDisplay(itemstack));
list.add(EnumColor.GREY + LangUtils.localize("gui.security") + ": " + SecurityUtils.getSecurityDisplay(itemstack, Side.CLIENT));
if(SecurityUtils.isOverridden(itemstack))
if(SecurityUtils.isOverridden(itemstack, Side.CLIENT))
{
list.add(EnumColor.RED + "(" + LangUtils.localize("gui.overridden") + ")");
}

View file

@ -190,9 +190,9 @@ public class ItemBlockMachine extends ItemBlock implements IEnergizedItem, ISpec
if(hasSecurity(itemstack))
{
list.add(SecurityUtils.getOwnerDisplay(entityplayer.getCommandSenderName(), getOwner(itemstack)));
list.add(EnumColor.GREY + LangUtils.localize("gui.security") + ": " + SecurityUtils.getSecurityDisplay(itemstack));
list.add(EnumColor.GREY + LangUtils.localize("gui.security") + ": " + SecurityUtils.getSecurityDisplay(itemstack, Side.CLIENT));
if(SecurityUtils.isOverridden(itemstack))
if(SecurityUtils.isOverridden(itemstack, Side.CLIENT))
{
list.add(EnumColor.RED + "(" + LangUtils.localize("gui.overridden") + ")");
}

View file

@ -0,0 +1,116 @@
package mekanism.common.network;
import io.netty.buffer.ByteBuf;
import java.util.ArrayList;
import java.util.List;
import mekanism.client.MekanismClient;
import mekanism.common.Mekanism;
import mekanism.common.PacketHandler;
import mekanism.common.frequency.Frequency;
import mekanism.common.network.PacketSecurityUpdate.SecurityUpdateMessage;
import mekanism.common.security.SecurityData;
import mekanism.common.security.SecurityFrequency;
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 PacketSecurityUpdate implements IMessageHandler<SecurityUpdateMessage, IMessage>
{
@Override
public IMessage onMessage(SecurityUpdateMessage message, MessageContext context)
{
if(message.packetType == SecurityPacket.UPDATE)
{
MekanismClient.clientSecurityMap.put(message.playerUsername, message.securityData);
}
return null;
}
public static class SecurityUpdateMessage implements IMessage
{
public SecurityPacket packetType;
public String playerUsername;
public SecurityData securityData;
public SecurityUpdateMessage() {}
public SecurityUpdateMessage(SecurityPacket type, String username, SecurityData data)
{
packetType = type;
if(packetType == SecurityPacket.UPDATE)
{
playerUsername = username;
securityData = data;
}
}
@Override
public void toBytes(ByteBuf dataStream)
{
dataStream.writeInt(packetType.ordinal());
if(packetType == SecurityPacket.UPDATE)
{
PacketHandler.writeString(dataStream, playerUsername);
securityData.write(dataStream);
}
else if(packetType == SecurityPacket.FULL)
{
List<SecurityFrequency> frequencies = new ArrayList<SecurityFrequency>();
for(Frequency frequency : Mekanism.securityFrequencies.getFrequencies())
{
if(frequency instanceof SecurityFrequency)
{
frequencies.add((SecurityFrequency)frequency);
}
}
dataStream.writeInt(frequencies.size());
for(SecurityFrequency frequency : frequencies)
{
PacketHandler.writeString(dataStream, frequency.owner);
new SecurityData(frequency).write(dataStream);
}
}
}
@Override
public void fromBytes(ByteBuf dataStream)
{
packetType = SecurityPacket.values()[dataStream.readInt()];
if(packetType == SecurityPacket.UPDATE)
{
playerUsername = PacketHandler.readString(dataStream);
securityData = SecurityData.read(dataStream);
}
else if(packetType == SecurityPacket.FULL)
{
MekanismClient.clientSecurityMap.clear();
int amount = dataStream.readInt();
for(int i = 0; i < amount; i++)
{
String owner = PacketHandler.readString(dataStream);
SecurityData data = SecurityData.read(dataStream);
MekanismClient.clientSecurityMap.put(owner, data);
}
}
}
}
public static enum SecurityPacket
{
UPDATE,
FULL;
}
}

View file

@ -0,0 +1,34 @@
package mekanism.common.security;
import io.netty.buffer.ByteBuf;
import mekanism.common.security.ISecurityTile.SecurityMode;
public class SecurityData
{
public SecurityMode mode = SecurityMode.PUBLIC;
public boolean override;
public SecurityData() {}
public SecurityData(SecurityFrequency frequency)
{
mode = frequency.securityMode;
override = frequency.override;
}
public void write(ByteBuf dataStream)
{
dataStream.writeInt(mode.ordinal());
dataStream.writeBoolean(override);
}
public static SecurityData read(ByteBuf dataStream)
{
SecurityData data = new SecurityData();
data.mode = SecurityMode.values()[dataStream.readInt()];
data.override = dataStream.readBoolean();
return data;
}
}

View file

@ -6,6 +6,7 @@ import mekanism.common.util.InventoryUtils;
import mekanism.common.util.SecurityUtils;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import cpw.mods.fml.relauncher.Side;
public class TileEntityPersonalChest extends TileEntityContainerBlock implements ISecurityTile
{
@ -76,7 +77,7 @@ public class TileEntityPersonalChest extends TileEntityContainerBlock implements
@Override
public int[] getAccessibleSlotsFromSide(int side)
{
if(side == 0 || SecurityUtils.getSecurity(this) != SecurityMode.PUBLIC)
if(side == 0 || SecurityUtils.getSecurity(this, Side.SERVER) != SecurityMode.PUBLIC)
{
return InventoryUtils.EMPTY;
}

View file

@ -10,9 +10,12 @@ import mekanism.common.PacketHandler;
import mekanism.common.base.IBoundingBlock;
import mekanism.common.frequency.Frequency;
import mekanism.common.frequency.FrequencyManager;
import mekanism.common.network.PacketSecurityUpdate.SecurityPacket;
import mekanism.common.network.PacketSecurityUpdate.SecurityUpdateMessage;
import mekanism.common.security.IOwnerItem;
import mekanism.common.security.ISecurityItem;
import mekanism.common.security.ISecurityTile.SecurityMode;
import mekanism.common.security.SecurityData;
import mekanism.common.security.SecurityFrequency;
import mekanism.common.util.MekanismUtils;
import net.minecraft.item.ItemStack;
@ -168,6 +171,8 @@ public class TileEntitySecurityDesk extends TileEntityContainerBlock implements
if(frequency != null)
{
frequency.override = !frequency.override;
Mekanism.packetHandler.sendToAll(new SecurityUpdateMessage(SecurityPacket.UPDATE, owner, new SecurityData(frequency)));
}
}
else if(type == 3)
@ -175,6 +180,8 @@ public class TileEntitySecurityDesk extends TileEntityContainerBlock implements
if(frequency != null)
{
frequency.securityMode = SecurityMode.values()[dataStream.readInt()];
Mekanism.packetHandler.sendToAll(new SecurityUpdateMessage(SecurityPacket.UPDATE, owner, new SecurityData(frequency)));
}
}

View file

@ -1,18 +1,20 @@
package mekanism.common.util;
import mekanism.api.EnumColor;
import mekanism.client.MekanismClient;
import mekanism.common.Mekanism;
import mekanism.common.frequency.Frequency;
import mekanism.common.security.IOwnerItem;
import mekanism.common.security.ISecurityItem;
import mekanism.common.security.ISecurityTile;
import mekanism.common.security.ISecurityTile.SecurityMode;
import mekanism.common.security.SecurityData;
import mekanism.common.security.SecurityFrequency;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChatComponentText;
import cpw.mods.fml.relauncher.Side;
public final class SecurityUtils
{
@ -25,7 +27,7 @@ public final class SecurityUtils
return owner == null || owner.equals(player.getCommandSenderName());
}
if((!player.worldObj.isRemote && MekanismUtils.isOp((EntityPlayerMP)player)) || stack == null || !(stack.getItem() instanceof ISecurityItem))
if(stack == null || !(stack.getItem() instanceof ISecurityItem))
{
return true;
}
@ -37,7 +39,7 @@ public final class SecurityUtils
public static boolean canAccess(EntityPlayer player, TileEntity tile)
{
if((!player.worldObj.isRemote && MekanismUtils.isOp((EntityPlayerMP)player)) || tile == null || !(tile instanceof ISecurityTile))
if(tile == null || !(tile instanceof ISecurityTile))
{
return true;
}
@ -112,55 +114,91 @@ public final class SecurityUtils
player.addChatMessage(new ChatComponentText(EnumColor.DARK_BLUE + "[Mekanism] " + EnumColor.RED + LangUtils.localize("gui.noAccessDesc")));
}
public static SecurityMode getSecurity(ISecurityTile security)
public static SecurityMode getSecurity(ISecurityTile security, Side side)
{
SecurityFrequency freq = security.getSecurity().getFrequency();
if(freq != null && freq.override)
if(side == Side.SERVER)
{
return freq.securityMode;
SecurityFrequency freq = security.getSecurity().getFrequency();
if(freq != null && freq.override)
{
return freq.securityMode;
}
}
else if(side == Side.CLIENT)
{
SecurityData data = MekanismClient.clientSecurityMap.get(security.getSecurity().getOwner());
if(data != null && data.override)
{
return data.mode;
}
}
return security.getSecurity().getMode();
}
public static String getSecurityDisplay(ItemStack stack)
public static String getSecurityDisplay(ItemStack stack, Side side)
{
ISecurityItem security = (ISecurityItem)stack.getItem();
SecurityMode mode = security.getSecurity(stack);
if(security.getOwner(stack) != null)
{
SecurityFrequency freq = getFrequency(security.getOwner(stack));
if(freq != null && freq.override)
if(side == Side.SERVER)
{
mode = freq.securityMode;
SecurityFrequency freq = getFrequency(security.getOwner(stack));
if(freq != null && freq.override)
{
mode = freq.securityMode;
}
}
else if(side == Side.CLIENT)
{
SecurityData data = MekanismClient.clientSecurityMap.get(security.getOwner(stack));
if(data != null && data.override)
{
mode = data.mode;
}
}
}
return mode.getDisplay();
}
public static String getSecurityDisplay(TileEntity tile)
public static String getSecurityDisplay(TileEntity tile, Side side)
{
ISecurityTile security = (ISecurityTile)tile;
SecurityMode mode = security.getSecurity().getMode();
if(security.getSecurity().getOwner() != null)
{
SecurityFrequency freq = getFrequency(security.getSecurity().getOwner());
if(freq != null && freq.override)
if(side == Side.SERVER)
{
mode = freq.securityMode;
SecurityFrequency freq = getFrequency(security.getSecurity().getOwner());
if(freq != null && freq.override)
{
mode = freq.securityMode;
}
}
else if(side == Side.CLIENT)
{
SecurityData data = MekanismClient.clientSecurityMap.get(security.getSecurity().getOwner());
if(data != null && data.override)
{
mode = data.mode;
}
}
}
return mode.getDisplay();
}
public static boolean isOverridden(ItemStack stack)
public static boolean isOverridden(ItemStack stack, Side side)
{
ISecurityItem security = (ISecurityItem)stack.getItem();
@ -169,12 +207,20 @@ public final class SecurityUtils
return false;
}
SecurityFrequency freq = getFrequency(security.getOwner(stack));
if(side == Side.SERVER)
{
SecurityFrequency freq = getFrequency(security.getOwner(stack));
return freq != null && freq.override;
return freq != null && freq.override;
}
else {
SecurityData data = MekanismClient.clientSecurityMap.get(security.getOwner(stack));
return data != null && data.override;
}
}
public static boolean isOverridden(TileEntity tile)
public static boolean isOverridden(TileEntity tile, Side side)
{
ISecurityTile security = (ISecurityTile)tile;
@ -183,8 +229,16 @@ public final class SecurityUtils
return false;
}
SecurityFrequency freq = getFrequency(security.getSecurity().getOwner());
if(side == Side.SERVER)
{
SecurityFrequency freq = getFrequency(security.getSecurity().getOwner());
return freq != null && freq.override;
return freq != null && freq.override;
}
else {
SecurityData data = MekanismClient.clientSecurityMap.get(security.getSecurity().getOwner());
return data != null && data.override;
}
}
}

View file

@ -129,9 +129,9 @@ public class ItemBlockGenerator extends ItemBlock implements IEnergizedItem, ISp
if(hasSecurity(itemstack))
{
list.add(SecurityUtils.getOwnerDisplay(entityplayer.getCommandSenderName(), getOwner(itemstack)));
list.add(EnumColor.GREY + LangUtils.localize("gui.security") + ": " + SecurityUtils.getSecurityDisplay(itemstack));
list.add(EnumColor.GREY + LangUtils.localize("gui.security") + ": " + SecurityUtils.getSecurityDisplay(itemstack, Side.CLIENT));
if(SecurityUtils.isOverridden(itemstack))
if(SecurityUtils.isOverridden(itemstack, Side.CLIENT))
{
list.add(EnumColor.RED + "(" + LangUtils.localize("gui.overridden") + ")");
}