re-did packet system for terminal

Also moved access cod into TileEntityInv as i play to allow all GUI
based machines to be locked by the player.
This commit is contained in:
DarkGuardsman 2013-09-18 08:05:19 -04:00
parent 788d4e406e
commit 8ff0262ce1
6 changed files with 150 additions and 333 deletions

View file

@ -1,84 +0,0 @@
package dark.core.prefab.helpers;
import universalelectricity.core.vector.Vector3;
public enum Direction
{
/** -Y */
DOWN(0, -1, 0),
/** +Y */
UP(0, 1, 0),
/** -Z */
NORTH(0, 0, -1),
/** +Z */
SOUTH(0, 0, 1),
/** -X */
WEST(-1, 0, 0),
/** +X */
EAST(1, 0, 0),
/** -X +Y */
NORTH_UP(0, 1, -1),
/** +X +Y */
SOUTH_UP(0, 1, 1),
/** -X +Y */
WEST_UP(-1, 1, 0),
/** +Z +Y */
EAST_UP(1, 1, 0),
/** -X -Y */
NORTH_DOWN(0, -1, -1),
/** +X -Y */
SOUTH_DOWN(0, -1, 1),
/** -X -Y */
WEST_DOWN(-1, -1, 0),
/** +Z -Y */
EAST_DOWN(1, -1, 0),
/** Used only by getOrientation, for invalid inputs */
UNKNOWN(0, 0, 0);
public static final Direction[] VALID_DIRECTIONS = { DOWN, UP, NORTH, SOUTH, WEST, EAST, NORTH_UP, SOUTH_UP, WEST_UP, EAST_UP, NORTH_DOWN, SOUTH_DOWN, WEST_DOWN, EAST_DOWN };
public final int offsetX;
public final int offsetY;
public final int offsetZ;
public static final int[] OPPOSITES = { 1, 0, 3, 2, 5, 4, 6 };
private Direction(int x, int y, int z)
{
offsetX = x;
offsetY = y;
offsetZ = z;
}
public static Direction getOrientation(int id)
{
if (id >= 0 && id < VALID_DIRECTIONS.length)
{
return VALID_DIRECTIONS[id];
}
return UNKNOWN;
}
public static Vector3 modifyPositionFromSide(Vector3 vec, Direction side, double amount)
{
double x = amount * side.offsetX;
double y = amount * side.offsetY;
double z = amount * side.offsetZ;
return new Vector3(vec.x + x, vec.y + y, vec.z + z);
}
}

View file

@ -1,10 +0,0 @@
package dark.core.prefab.helpers;
/** Used by machines that only rotate to 4 directions
*
* @author DarkGuardsman * */
public class MetaGroup
{
}

View file

@ -1,50 +0,0 @@
package dark.core.prefab.helpers;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Time
{
/** Returns the current time stamp as both the Data and string of time */
public static Pair<String, Date> getCurrentTimeStamp()
{
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
String strDate = sdfDate.format(now);
return new Pair<String, Date>(strDate, now);
}
/** Returns the current time stamp as a class of numbers */
public static TimeData getCurrentTimeData()
{
return new TimeData();
}
/** Used to stored the data in a class for easy access */
public static class TimeData
{
int hour, min, sec, day, month, year;
@SuppressWarnings("deprecation")
public TimeData()
{
Date now = new Date();
this.hour = now.getHours();
this.min = now.getMinutes();
this.sec = now.getSeconds();
this.day = now.getDay();
this.month = now.getMonth();
this.year = now.getYear();
}
public TimeData(int hour, int min, int sec, int day, int month, int year)
{
this.hour = hour;
this.min = min;
this.sec = sec;
this.day = day;
this.month = month;
this.year = year;
}
}
}

View file

@ -1,22 +1,32 @@
package dark.core.prefab.machine;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.prefab.tile.TileEntityAdvanced;
import dark.core.interfaces.IExternalInv;
import dark.core.interfaces.IInvBox;
import dark.core.prefab.access.AccessLevel;
import dark.core.prefab.access.ISpecialAccess;
import dark.core.prefab.access.UserAccess;
import dark.core.prefab.invgui.InvChest;
/** Prefab for simple object who only need basic inv support and nothing more
*
* @author Darkguardsman */
public class TileEntityInv extends TileEntityAdvanced implements IExternalInv, ISidedInventory
public class TileEntityInv extends TileEntityAdvanced implements IExternalInv, ISidedInventory, ISpecialAccess
{
protected IInvBox inventory;
protected boolean lockInv;
/** A list of user access data. */
protected final List<UserAccess> users = new ArrayList<UserAccess>();
@Override
public IInvBox getInventory()
@ -143,11 +153,98 @@ public class TileEntityInv extends TileEntityAdvanced implements IExternalInv, I
return true;
}
/*
* User access
*/
@Override
public AccessLevel getUserAccess(String username)
{
for (int i = 0; i < this.users.size(); i++)
{
if (this.users.get(i).username.equalsIgnoreCase(username))
{
return this.users.get(i).level;
}
}
return AccessLevel.NONE;
}
public boolean canUserAccess(String username)
{
return (this.getUserAccess(username).ordinal() > AccessLevel.BASIC.ordinal());
}
@Override
public List<UserAccess> getUsers()
{
return this.users;
}
@Override
public List<UserAccess> getUsersWithAcess(AccessLevel level)
{
List<UserAccess> players = new ArrayList<UserAccess>();
for (int i = 0; i < this.users.size(); i++)
{
UserAccess ref = this.users.get(i);
if (ref.level == level)
{
players.add(ref);
}
}
return players;
}
@Override
public boolean addUserAccess(String player, AccessLevel lvl, boolean save)
{
this.removeUserAccess(player);
boolean bool = this.users.add(new UserAccess(player, lvl, save));
this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
return bool;
}
@Override
public boolean removeUserAccess(String player)
{
List<UserAccess> removeList = new ArrayList<UserAccess>();
for (int i = 0; i < this.users.size(); i++)
{
UserAccess ref = this.users.get(i);
if (ref.username.equalsIgnoreCase(player))
{
removeList.add(ref);
}
}
if (removeList != null && removeList.size() > 0)
{
boolean bool = this.users.removeAll(removeList);
this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
return bool;
}
return false;
}
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
this.getInventory().loadInv(nbt);
// Read user list
this.users.clear();
NBTTagList userList = nbt.getTagList("Users");
for (int i = 0; i < userList.tagCount(); ++i)
{
NBTTagCompound var4 = (NBTTagCompound) userList.tagAt(i);
this.users.add(UserAccess.loadFromNBT(var4));
}
}
@Override
@ -155,6 +252,19 @@ public class TileEntityInv extends TileEntityAdvanced implements IExternalInv, I
{
super.writeToNBT(nbt);
this.getInventory().saveInv(nbt);
}
// Write user list
NBTTagList usersTag = new NBTTagList();
for (int player = 0; player < this.users.size(); ++player)
{
UserAccess access = this.users.get(player);
if (access != null && access.shouldSave)
{
NBTTagCompound accessData = new NBTTagCompound();
access.writeToNBT(accessData);
usersTag.appendTag(accessData);
}
}
nbt.setTag("Users", usersTag);
}
}

View file

@ -42,7 +42,10 @@ public class TileEntityMachine extends TileEntityInv implements ISidedInventory,
/** GUI display data update */
GUI("guiGeneral"),
/** Full tile read/write data from tile NBT */
NBT("nbtAll");
NBT("nbtAll"),
GUI_EVENT("clientGui"),
GUI_COMMAND("clientCommand"),
TERMINAL_OUTPUT("serverTerminal");
public String name;

View file

@ -1,5 +1,7 @@
package dark.core.prefab.terminal;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
@ -24,25 +26,15 @@ import dark.core.prefab.access.AccessLevel;
import dark.core.prefab.access.ISpecialAccess;
import dark.core.prefab.access.UserAccess;
import dark.core.prefab.machine.TileEntityEnergyMachine;
import dark.core.prefab.machine.TileEntityMachine.SimplePacketTypes;
/** @author Calclavia, DarkGuardsman */
public abstract class TileEntityTerminal extends TileEntityEnergyMachine implements ISpecialAccess, IPacketReceiver, ITerminal
public abstract class TileEntityTerminal extends TileEntityEnergyMachine implements IPacketReceiver, ITerminal
{
public enum PacketType
{
GUI_EVENT,
GUI_COMMAND,
TERMINAL_OUTPUT,
DESCRIPTION_DATA;
}
/** A list of everything typed inside the terminal */
private final List<String> terminalOutput = new ArrayList<String>();
/** A list of user access data. */
private final List<UserAccess> users = new ArrayList<UserAccess>();
/** The amount of lines the terminal can store. */
public static final int SCROLL_SIZE = 15;
@ -66,37 +58,27 @@ public abstract class TileEntityTerminal extends TileEntityEnergyMachine impleme
super(wattsPerTick, maxEnergy);
}
@Override
public void updateEntity()
public void senGUIPacket(EntityPlayer entity)
{
super.updateEntity();
if (!this.worldObj.isRemote)
if(!this.worldObj.isRemote)
{
if (this.ticks % 3 == 0)
{
for (EntityPlayer player : this.playersUsing)
{
PacketDispatcher.sendPacketToPlayer(this.getDescriptionPacket(), (Player) player);
}
}
PacketDispatcher.sendPacketToPlayer(this.getDescriptionPacket(), (Player) entity);
}
}
/** Sends all NBT data. Server -> Client */
@Override
public Packet getDescriptionPacket()
{
NBTTagCompound nbt = new NBTTagCompound();
this.writeToNBT(nbt);
return PacketHandler.instance().getPacket(this.getChannel(), this, PacketType.DESCRIPTION_DATA.ordinal(), nbt);
return PacketHandler.instance().getPacket(this.getChannel(), this, SimplePacketTypes.NBT.name, nbt);
}
/** Sends all Terminal data Server -> Client */
public void sendTerminalOutputToClients()
{
List data = new ArrayList();
data.add(PacketType.TERMINAL_OUTPUT.ordinal());
data.add(SimplePacketTypes.TERMINAL_OUTPUT.name);
data.add(this.getTerminalOuput().size());
data.addAll(this.getTerminalOuput());
@ -113,50 +95,44 @@ public abstract class TileEntityTerminal extends TileEntityEnergyMachine impleme
{
if (this.worldObj.isRemote)
{
Packet packet = PacketHandler.instance().getPacket(this.getChannel(), this, PacketType.GUI_COMMAND.ordinal(), entityPlayer.username, cmdInput);
Packet packet = PacketHandler.instance().getPacket(this.getChannel(), this, SimplePacketTypes.GUI_COMMAND.name, entityPlayer.username, cmdInput);
PacketDispatcher.sendPacketToServer(packet);
}
}
@Override
public void handlePacketData(INetworkManager network, int packetID, Packet250CustomPayload packet, EntityPlayer player, ByteArrayDataInput dataStream)
public boolean simplePacket(String id, DataInputStream dis, EntityPlayer player)
{
try
{
PacketType packetType = PacketType.values()[dataStream.readInt()];
switch (packetType)
if (!super.simplePacket(id, dis, player))
{
case DESCRIPTION_DATA:
if (this.worldObj.isRemote)
{
if (this.worldObj.isRemote)
if(id.equalsIgnoreCase(SimplePacketTypes.TERMINAL_OUTPUT.name))
{
short size = dataStream.readShort();
int size = dis.readInt();
if (size > 0)
List<String> oldTerminalOutput = new ArrayList(this.terminalOutput);
this.terminalOutput.clear();
for (int i = 0; i < size; i++)
{
byte[] byteCode = new byte[size];
dataStream.readFully(byteCode);
this.readFromNBT(CompressedStreamTools.decompress(byteCode));
this.terminalOutput.add(dis.readUTF());
}
}
break;
}
case GUI_COMMAND:
{
if (!this.worldObj.isRemote)
{
CommandRegistry.onCommand(this.worldObj.getPlayerEntityByName(dataStream.readUTF()), this, dataStream.readUTF());
this.sendTerminalOutputToClients();
if (!this.terminalOutput.equals(oldTerminalOutput) && this.terminalOutput.size() != oldTerminalOutput.size())
{
this.setScroll(this.getTerminalOuput().size() - SCROLL_SIZE);
}
return true;
}
break;
}
case GUI_EVENT:
else
{
if (!this.worldObj.isRemote)
if (id.equalsIgnoreCase("GuiClosed"))
{
if (dataStream.readBoolean())
if (dis.readBoolean())
{
this.playersUsing.add(player);
this.sendTerminalOutputToClients();
@ -165,112 +141,21 @@ public abstract class TileEntityTerminal extends TileEntityEnergyMachine impleme
{
this.playersUsing.remove(player);
}
return true;
}
break;
}
case TERMINAL_OUTPUT:
{
if (this.worldObj.isRemote)
else if (id.equalsIgnoreCase(SimplePacketTypes.GUI_COMMAND.name))
{
int size = dataStream.readInt();
List<String> oldTerminalOutput = new ArrayList(this.terminalOutput);
this.terminalOutput.clear();
for (int i = 0; i < size; i++)
{
this.terminalOutput.add(dataStream.readUTF());
}
if (!this.terminalOutput.equals(oldTerminalOutput) && this.terminalOutput.size() != oldTerminalOutput.size())
{
this.setScroll(this.getTerminalOuput().size() - SCROLL_SIZE);
}
CommandRegistry.onCommand(this.worldObj.getPlayerEntityByName(dis.readUTF()), this, dis.readUTF());
this.sendTerminalOutputToClients();
return true;
}
break;
}
default:
break;
}
}
catch (Exception e)
catch (IOException e)
{
FMLLog.severe("DarkLib>>>TerminalInstance>>>PacketReadError>>>ForTile>>>" + this.toString());
e.printStackTrace();
}
}
@Override
public AccessLevel getUserAccess(String username)
{
for (int i = 0; i < this.users.size(); i++)
{
if (this.users.get(i).username.equalsIgnoreCase(username))
{
return this.users.get(i).level;
}
}
return AccessLevel.NONE;
}
public boolean canUserAccess(String username)
{
return (this.getUserAccess(username).ordinal() > AccessLevel.BASIC.ordinal());
}
@Override
public List<UserAccess> getUsers()
{
return this.users;
}
@Override
public List<UserAccess> getUsersWithAcess(AccessLevel level)
{
List<UserAccess> players = new ArrayList<UserAccess>();
for (int i = 0; i < this.users.size(); i++)
{
UserAccess ref = this.users.get(i);
if (ref.level == level)
{
players.add(ref);
}
}
return players;
}
@Override
public boolean addUserAccess(String player, AccessLevel lvl, boolean save)
{
this.removeUserAccess(player);
boolean bool = this.users.add(new UserAccess(player, lvl, save));
this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
return bool;
}
@Override
public boolean removeUserAccess(String player)
{
List<UserAccess> removeList = new ArrayList<UserAccess>();
for (int i = 0; i < this.users.size(); i++)
{
UserAccess ref = this.users.get(i);
if (ref.username.equalsIgnoreCase(player))
{
removeList.add(ref);
}
}
if (removeList != null && removeList.size() > 0)
{
boolean bool = this.users.removeAll(removeList);
this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
return bool;
}
return false;
}
@ -319,41 +204,4 @@ public abstract class TileEntityTerminal extends TileEntityEnergyMachine impleme
return this.scroll;
}
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
// Read user list
this.users.clear();
NBTTagList userList = nbt.getTagList("Users");
for (int i = 0; i < userList.tagCount(); ++i)
{
NBTTagCompound var4 = (NBTTagCompound) userList.tagAt(i);
this.users.add(UserAccess.loadFromNBT(var4));
}
}
@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
// Write user list
NBTTagList usersTag = new NBTTagList();
for (int player = 0; player < this.users.size(); ++player)
{
UserAccess access = this.users.get(player);
if (access != null && access.shouldSave)
{
NBTTagCompound accessData = new NBTTagCompound();
access.writeToNBT(accessData);
usersTag.appendTag(accessData);
}
}
nbt.setTag("Users", usersTag);
}
}