clean and addition of access code

this code came from ICBM_Sentries but was part of dark/library before
being merged into sentry code.
This commit is contained in:
Robert Seifert 2013-04-24 11:23:20 -04:00
parent a3c2bc8299
commit 893a94d142
19 changed files with 1032 additions and 609 deletions

View file

@ -1,5 +1,12 @@
package dark.library;
import java.awt.Color;
import net.minecraft.world.World;
import universalelectricity.core.vector.Vector3;
import cpw.mods.fml.client.FMLClientHandler;
import dark.library.effects.FXBeam;
public class DarkMain
{
/* RESOURCE FILE PATHS */
@ -12,4 +19,8 @@ public class DarkMain
public static final String TEXTURE_NAME_PREFIX = "dark:";
public static void renderBeam(World world, Vector3 position, Vector3 target, Color color, int age)
{
FMLClientHandler.instance().getClient().effectRenderer.addEffect(new FXBeam(world, position, target, color.getRed(), color.getGreen(), color.getBlue(), age));
}
}

View file

@ -1,4 +1,4 @@
package dark.library.locking;
package dark.library.access;
public enum AccessLevel
{

View file

@ -1,4 +1,4 @@
package dark.library.locking;
package dark.library.access;
import java.util.ArrayList;
import java.util.HashMap;

View file

@ -1,4 +1,4 @@
package dark.library.locking;
package dark.library.access;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.MinecraftForge;

View file

@ -1,4 +1,4 @@
package dark.library.locking;
package dark.library.access;
import java.util.ArrayList;
import java.util.List;
@ -30,7 +30,7 @@ public class UserAccess
}
/**
* Reads a userAccess instance from nbt
* Read from nbt
*/
public void readFromNBT(NBTTagCompound nbt)
{
@ -38,9 +38,6 @@ public class UserAccess
this.level = AccessLevel.get(nbt.getInteger("ID"));
}
/**
* writes a userAccess instance to nbt
*/
public static UserAccess loadFromNBT(NBTTagCompound nbt)
{
UserAccess access = new UserAccess("", AccessLevel.NONE, true);
@ -48,6 +45,7 @@ public class UserAccess
return access;
}
/**
* Reads an entire UserAccess list from an nbt file
*

View file

@ -0,0 +1,14 @@
package dark.library.access.interfaces;
public interface IScroll
{
/**
* Scrolls the text field up or down. Client side only. Positive value will scroll the text down
* while a negative value will scroll it up.
*/
public void scroll(int amount);
public void setScroll(int length);
public int getScroll();
}

View file

@ -0,0 +1,50 @@
package dark.library.access.interfaces;
import java.util.List;
import dark.library.access.AccessLevel;
import dark.library.access.UserAccess;
public interface ISpecialAccess
{
/**
* Gets the player's access level on the machine he is using
*
* @return access level of the player, make sure to return no access if the player doesn't have
* any
*/
public AccessLevel getUserAccess(String username);
/**
* gets the access list for the machine
*
* @return hasMap of players and there access levels
*/
public List<UserAccess> getUsers();
/**
* sets the players access level in the access map
*
* @param player
* @return true if the level was set false if something went wrong
*/
public boolean addUserAccess(String username, AccessLevel level, boolean save);
/**
* Removes the user from the access list
*
* @param username
* @return
*/
public boolean removeUserAccess(String username);
/**
* Gets a list of users with this specified access level.
*
* @param level
* @return
*/
List<UserAccess> getUsersWithAcess(AccessLevel level);
}

View file

@ -0,0 +1,17 @@
package dark.library.access.interfaces;
import java.util.List;
public interface ITerminal extends ISpecialAccess, IScroll
{
/**
* Gets an output of the string stored in the console.
*/
public List<String> getTerminalOuput();
/**
* Adds a string to the console. Server side only.
*/
public boolean addToConsole(String msg);
}

View file

@ -0,0 +1,201 @@
package dark.library.effects;
import net.minecraft.client.Minecraft;
import net.minecraft.client.particle.EntityFX;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.entity.EntityLiving;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import org.lwjgl.opengl.GL11;
import universalelectricity.core.vector.Vector3;
import dark.library.DarkMain;
/**
* Based off Thaumcraft's Beam Renderer.
*
* @author Calclavia, Azanor
*
*/
public class FXBeam extends EntityFX
{
double movX = 0.0D;
double movY = 0.0D;
double movZ = 0.0D;
private float length = 0.0F;
private float rotYaw = 0.0F;
private float rotPitch = 0.0F;
private float prevYaw = 0.0F;
private float prevPitch = 0.0F;
private Vector3 target = new Vector3();
private float endModifier = 1.0F;
private boolean reverse = false;
private boolean pulse = false;
private int rotationSpeed = 20;
private float prevSize = 0.0F;
private float beamD = 0.08f;
public FXBeam(World par1World, Vector3 position, Vector3 target, float red, float green, float blue, int age)
{
super(par1World, position.x, position.y, position.z, 0.0D, 0.0D, 0.0D);
this.setRGB(red, green, blue);
this.setSize(0.02F, 0.02F);
this.noClip = true;
this.motionX = 0.0D;
this.motionY = 0.0D;
this.motionZ = 0.0D;
this.target = target;
float xd = (float) (this.posX - this.target.x);
float yd = (float) (this.posY - this.target.y);
float zd = (float) (this.posZ - this.target.z);
this.length = (float) new Vector3(this).distanceTo(this.target);
double var7 = MathHelper.sqrt_double(xd * xd + zd * zd);
this.rotYaw = ((float) (Math.atan2(xd, zd) * 180.0D / 3.141592653589793D));
this.rotPitch = ((float) (Math.atan2(yd, var7) * 180.0D / 3.141592653589793D));
this.prevYaw = this.rotYaw;
this.prevPitch = this.rotPitch;
this.particleMaxAge = age;
/**
* Sets the particle age based on distance.
*/
EntityLiving renderentity = Minecraft.getMinecraft().renderViewEntity;
int visibleDistance = 50;
if (!Minecraft.getMinecraft().gameSettings.fancyGraphics)
{
visibleDistance = 25;
}
if (renderentity.getDistance(this.posX, this.posY, this.posZ) > visibleDistance)
{
this.particleMaxAge = 0;
}
}
@Override
public void onUpdate()
{
this.prevPosX = this.posX;
this.prevPosY = this.posY;
this.prevPosZ = this.posZ;
this.prevYaw = this.rotYaw;
this.prevPitch = this.rotPitch;
float xd = (float) (this.posX - this.target.x);
float yd = (float) (this.posY - this.target.y);
float zd = (float) (this.posZ - this.target.z);
this.length = MathHelper.sqrt_float(xd * xd + yd * yd + zd * zd);
double var7 = MathHelper.sqrt_double(xd * xd + zd * zd);
this.rotYaw = ((float) (Math.atan2(xd, zd) * 180.0D / 3.141592653589793D));
this.rotPitch = ((float) (Math.atan2(yd, var7) * 180.0D / 3.141592653589793D));
if (this.particleAge++ >= this.particleMaxAge)
{
setDead();
}
}
public void setRGB(float r, float g, float b)
{
this.particleRed = r;
this.particleGreen = g;
this.particleBlue = b;
}
@Override
public void renderParticle(Tessellator tessellator, float f, float f1, float f2, float f3, float f4, float f5)
{
tessellator.draw();
GL11.glPushMatrix();
float var9 = 1.0F;
float slide = this.worldObj.getTotalWorldTime();
float rot = this.worldObj.provider.getWorldTime() % (360 / this.rotationSpeed) * this.rotationSpeed + this.rotationSpeed * f;
float size = 1.0F;
if (this.pulse)
{
size = Math.min(this.particleAge / 4.0F, 1.0F);
size = this.prevSize + (size - this.prevSize) * f;
}
float op = 0.5F;
if ((this.pulse) && (this.particleMaxAge - this.particleAge <= 4))
{
op = 0.5F - (4 - (this.particleMaxAge - this.particleAge)) * 0.1F;
}
Minecraft.getMinecraft().renderEngine.bindTexture(DarkMain.BLOCK_TEXTURE_DIRECTORY + "");
GL11.glTexParameterf(3553, 10242, 10497.0F);
GL11.glTexParameterf(3553, 10243, 10497.0F);
GL11.glDisable(2884);
float var11 = slide + f;
if (this.reverse)
var11 *= -1.0F;
float var12 = -var11 * 0.2F - MathHelper.floor_float(-var11 * 0.1F);
GL11.glEnable(3042);
GL11.glBlendFunc(770, 1);
GL11.glDepthMask(false);
float xx = (float) (this.prevPosX + (this.posX - this.prevPosX) * f - interpPosX);
float yy = (float) (this.prevPosY + (this.posY - this.prevPosY) * f - interpPosY);
float zz = (float) (this.prevPosZ + (this.posZ - this.prevPosZ) * f - interpPosZ);
GL11.glTranslated(xx, yy, zz);
float ry = this.prevYaw + (this.rotYaw - this.prevYaw) * f;
float rp = this.prevPitch + (this.rotPitch - this.prevPitch) * f;
GL11.glRotatef(90.0F, 1.0F, 0.0F, 0.0F);
GL11.glRotatef(180.0F + ry, 0.0F, 0.0F, -1.0F);
GL11.glRotatef(rp, 1.0F, 0.0F, 0.0F);
double var44 = -beamD * size;
double var17 = beamD * size;
double var44b = -beamD * size * this.endModifier;
double var17b = beamD * size * this.endModifier;
GL11.glRotatef(rot, 0.0F, 1.0F, 0.0F);
for (int t = 0; t < 3; t++)
{
double var29 = this.length * size * var9;
double var31 = 0.0D;
double var33 = 1.0D;
double var35 = -1.0F + var12 + t / 3.0F;
double var37 = this.length * size * var9 + var35;
GL11.glRotatef(60.0F, 0.0F, 1.0F, 0.0F);
tessellator.startDrawingQuads();
tessellator.setBrightness(200);
tessellator.setColorRGBA_F(this.particleRed, this.particleGreen, this.particleBlue, op);
tessellator.addVertexWithUV(var44b, var29, 0.0D, var33, var37);
tessellator.addVertexWithUV(var44, 0.0D, 0.0D, var33, var35);
tessellator.addVertexWithUV(var17, 0.0D, 0.0D, var31, var35);
tessellator.addVertexWithUV(var17b, var29, 0.0D, var31, var37);
tessellator.draw();
}
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
GL11.glDepthMask(true);
GL11.glDisable(3042);
GL11.glEnable(2884);
GL11.glPopMatrix();
tessellator.startDrawingQuads();
this.prevSize = size;
Minecraft.getMinecraft().renderEngine.bindTexture("/particles.png");
}
}

View file

@ -0,0 +1,17 @@
package dark.library.gui;
import org.lwjgl.input.Keyboard;
import net.minecraft.client.gui.GuiScreen;
public class GuiGlobalList extends GuiScreen
{
@Override
protected void keyTyped(char keycode, int par2)
{
if (keycode == Keyboard.KEY_ESCAPE)
{
this.mc.thePlayer.closeScreen();
}
}
}

View file

@ -18,7 +18,7 @@ public class ItemFindingHelper
*/
public static List<EntityItem> findAllItemIn(World world, Vector3 start, Vector3 end)
{
return world.getEntitiesWithinAABB(EntityItem.class, AxisAlignedBB.getBoundingBox(start.x, start.y, start.z, end.x, end.x, end.x));
return world.getEntitiesWithinAABB(EntityItem.class, AxisAlignedBB.getBoundingBox(start.x, start.y, start.z, end.x, end.y, end.z));
}
/**

View file

@ -1,39 +0,0 @@
package dark.library.locking;
import java.util.List;
public interface ISpecialAccess
{
/**
* Gets the player's access level on the machine he is using
*
* @return access level of the player, make sure to return no access if the player doesn't have
* any
*/
public AccessLevel getUserAccess(String username);
/**
* gets the access list for the machine
*/
public List<UserAccess> getUsers();
/**
* Set the user's access in the list
*
* @param user - userAccess instance
* @param isServer - true if added server side
* @return true if added to the list
*/
public boolean addUserAccess(UserAccess user, boolean isServer);
/**
* Removes the user from the access list
*/
public boolean removeUserAccess(String username, boolean isServer);
/**
* Gets a list of users with the specified access level.
*/
public List<UserAccess> getUsersWithAcess(AccessLevel level);
}

View file

@ -1,280 +0,0 @@
package dark.library.locking.prefab;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet250CustomPayload;
import universalelectricity.core.vector.Vector3;
import universalelectricity.prefab.network.IPacketReceiver;
import universalelectricity.prefab.network.PacketManager;
import universalelectricity.prefab.tile.TileEntityElectricityRunnable;
import com.google.common.io.ByteArrayDataInput;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.network.PacketDispatcher;
import dark.library.locking.AccessLevel;
import dark.library.locking.ISpecialAccess;
import dark.library.locking.UserAccess;
public abstract class TileEntityElectricLockable extends TileEntityElectricityRunnable implements ISpecialAccess, IPacketReceiver
{
public enum PacketType
{
DESCR_DATA, LIST_EDIT, SETTING_EDIT, OTHER;
}
/**
* A list of user access data.
*/
private final List<UserAccess> users = new ArrayList<UserAccess>();
/**
* The amount of players using the console.
*/
public int playersUsing = 0;
/**
* was the access list changed, used to trigger a packet update early
*/
public boolean listUpdate = false;
@Override
public void updateEntity()
{
super.updateEntity();
if (!this.worldObj.isRemote)
{
// // update lit when changes are made or every 2 seconds if a player is near
if (listUpdate || (this.worldObj.getClosestPlayer(xCoord, yCoord, zCoord, 20) != null && this.ticks % 40 == 0))
{
listUpdate = false;
PacketManager.sendPacketToClients(this.getDescriptionPacket(), this.worldObj, new Vector3(this), 20);
}
}
}
/**
* Channel to be used to send packets on
*/
public abstract String getChannel();
/**
* Sends all NBT data. Server -> Client
*/
@Override
public Packet getDescriptionPacket()
{
NBTTagCompound nbt = new NBTTagCompound();
this.writeToNBT(nbt);
return PacketManager.getPacket(this.getChannel(), this, PacketType.DESCR_DATA.ordinal(), nbt);
}
/**
* send a packet the server with info on an access list change
*
* @param player - player's access
* @param remove - is the change a remove order
*/
public void sendEditToServer(UserAccess player, boolean remove)
{
if (this.worldObj.isRemote && player != null)
{
Packet packet = PacketManager.getPacket(this.getChannel(), this, PacketType.LIST_EDIT.ordinal(), player.username, player.level.ordinal(), player.shouldSave, remove);
PacketDispatcher.sendPacketToServer(packet);
}
}
@Override
public void handlePacketData(INetworkManager network, int packetID, Packet250CustomPayload packet, EntityPlayer player, ByteArrayDataInput dataStream)
{
try
{
PacketType packetType = PacketType.values()[dataStream.readInt()];
switch (packetType)
{
case DESCR_DATA:
{
if (this.worldObj.isRemote)
{
short size = dataStream.readShort();
if (size > 0)
{
byte[] byteCode = new byte[size];
dataStream.readFully(byteCode);
this.readFromNBT(CompressedStreamTools.decompress(byteCode));
}
}
break;
}
case LIST_EDIT:
{
if (!this.worldObj.isRemote)
{
String name = dataStream.readUTF();
AccessLevel level = AccessLevel.get(dataStream.readInt());
Boolean shouldSave = dataStream.readBoolean();
Boolean remove = dataStream.readBoolean();
if (remove)
{
this.removeUserAccess(name, true);
}
else
{
this.addUserAccess(new UserAccess(name, level, shouldSave), true);
}
}
break;
}
case SETTING_EDIT:
{
// TODO add settings packet handler when settings are added
break;
}
// // PacketType.Other is treated as a default call //
default:
break;
}
}
catch (Exception e)
{
FMLLog.severe("GS: Failed to handle packet for locked door.");
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;
}
@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;
}
/**
* checks to see if a user is on the access list regardless of access
*/
public boolean isOnList(String username)
{
for (UserAccess user : this.getUsers())
{
if (user.username.equalsIgnoreCase(username))
{
return true;
}
}
return false;
}
@Override
public boolean addUserAccess(UserAccess user, boolean isServer)
{
if (!isServer)
{
this.sendEditToServer(user, false);
}
else
{
this.removeUserAccess(user.username, isServer);
this.listUpdate = true;
return this.users.add(user);
}
return false;
}
@Override
public boolean removeUserAccess(String player, boolean isServer)
{
if (!isServer)
{
UserAccess access = new UserAccess(player, AccessLevel.BASIC, false);
this.sendEditToServer(access, true);
}
else
{
List<UserAccess> list = UserAccess.removeUserAccess(player, this.users);
if (list.size() < this.users.size())
{
this.users.clear();
this.users.addAll(list);
this.listUpdate = true;
return true;
}
}
return false;
}
/**
* can the player access this tileEntity in any way
*/
public boolean canAccess(EntityPlayer player)
{
if (this.users.size() <= 0)
{
return true;
}
return this.getUserAccess(player.username).ordinal() >= AccessLevel.USER.ordinal();
}
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
// Read user list
this.users.clear();
this.users.addAll(UserAccess.readListFromNBT(nbt, "Users"));
this.listUpdate = true;
}
@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
// Write user list
UserAccess.writeListToNBT(nbt, this.users);
}
}

View file

@ -1,280 +0,0 @@
package dark.library.locking.prefab;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet250CustomPayload;
import universalelectricity.core.vector.Vector3;
import universalelectricity.prefab.network.IPacketReceiver;
import universalelectricity.prefab.network.PacketManager;
import universalelectricity.prefab.tile.TileEntityAdvanced;
import com.google.common.io.ByteArrayDataInput;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.network.PacketDispatcher;
import dark.library.locking.AccessLevel;
import dark.library.locking.ISpecialAccess;
import dark.library.locking.UserAccess;
public abstract class TileEntityLockable extends TileEntityAdvanced implements ISpecialAccess, IPacketReceiver
{
public enum PacketType
{
DESCR_DATA, LIST_EDIT, SETTING_EDIT, OTHER;
}
/**
* A list of user access data.
*/
private final List<UserAccess> users = new ArrayList<UserAccess>();
/**
* The amount of players using the console.
*/
public int playersUsing = 0;
/**
* was the access list changed, used to trigger a packet update early
*/
public boolean listUpdate = false;
@Override
public void updateEntity()
{
super.updateEntity();
if (!this.worldObj.isRemote)
{
// // update lit when changes are made or every 2 seconds if a player is near
if (listUpdate || (this.worldObj.getClosestPlayer(xCoord, yCoord, zCoord, 20) != null && this.ticks % 40 == 0))
{
listUpdate = false;
PacketManager.sendPacketToClients(this.getDescriptionPacket(), this.worldObj, new Vector3(this), 20);
}
}
}
/**
* Channel to be used to send packets on
*/
public abstract String getChannel();
/**
* Sends all NBT data. Server -> Client
*/
@Override
public Packet getDescriptionPacket()
{
NBTTagCompound nbt = new NBTTagCompound();
this.writeToNBT(nbt);
return PacketManager.getPacket(this.getChannel(), this, PacketType.DESCR_DATA.ordinal(), nbt);
}
/**
* send a packet the server with info on an access list change
*
* @param player - player's access
* @param remove - is the change a remove order
*/
public void sendEditToServer(UserAccess player, boolean remove)
{
if (this.worldObj.isRemote && player != null)
{
Packet packet = PacketManager.getPacket(this.getChannel(), this, PacketType.LIST_EDIT.ordinal(), player.username, player.level.ordinal(), player.shouldSave, remove);
PacketDispatcher.sendPacketToServer(packet);
}
}
@Override
public void handlePacketData(INetworkManager network, int packetID, Packet250CustomPayload packet, EntityPlayer player, ByteArrayDataInput dataStream)
{
try
{
PacketType packetType = PacketType.values()[dataStream.readInt()];
switch (packetType)
{
case DESCR_DATA:
{
if (this.worldObj.isRemote)
{
short size = dataStream.readShort();
if (size > 0)
{
byte[] byteCode = new byte[size];
dataStream.readFully(byteCode);
this.readFromNBT(CompressedStreamTools.decompress(byteCode));
}
}
break;
}
case LIST_EDIT:
{
if (!this.worldObj.isRemote)
{
String name = dataStream.readUTF();
AccessLevel level = AccessLevel.get(dataStream.readInt());
Boolean shouldSave = dataStream.readBoolean();
Boolean remove = dataStream.readBoolean();
if (remove)
{
this.removeUserAccess(name, true);
}
else
{
this.addUserAccess(new UserAccess(name, level, shouldSave), true);
}
}
break;
}
case SETTING_EDIT:
{
// TODO add settings packet handler when settings are added
break;
}
// // PacketType.Other is treated as a default call //
default:
break;
}
}
catch (Exception e)
{
FMLLog.severe("GS: Failed to handle packet for locked door.");
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;
}
@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;
}
/**
* checks to see if a user is on the access list regardless of access
*/
public boolean isOnList(String username)
{
for (UserAccess user : this.getUsers())
{
if (user.username.equalsIgnoreCase(username))
{
return true;
}
}
return false;
}
@Override
public boolean addUserAccess(UserAccess user, boolean isServer)
{
if (!isServer)
{
this.sendEditToServer(user, false);
}
else
{
this.removeUserAccess(user.username, isServer);
this.listUpdate = true;
return this.users.add(user);
}
return false;
}
@Override
public boolean removeUserAccess(String player, boolean isServer)
{
if (!isServer)
{
UserAccess access = new UserAccess(player, AccessLevel.BASIC, false);
this.sendEditToServer(access, true);
}
else
{
List<UserAccess> list = UserAccess.removeUserAccess(player, this.users);
if (list.size() < this.users.size())
{
this.users.clear();
this.users.addAll(list);
this.listUpdate = true;
return true;
}
}
return false;
}
/**
* can the player access this tileEntity in any way
*/
public boolean canAccess(EntityPlayer player)
{
if (this.users.size() <= 0)
{
return true;
}
return this.getUserAccess(player.username).ordinal() >= AccessLevel.USER.ordinal();
}
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
// Read user list
this.users.clear();
this.users.addAll(UserAccess.readListFromNBT(nbt, "Users"));
this.listUpdate = true;
}
@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
// Write user list
UserAccess.writeListToNBT(nbt, this.users);
}
}

View file

@ -0,0 +1,364 @@
package dark.library.terminal;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet250CustomPayload;
import universalelectricity.core.vector.Vector3;
import universalelectricity.prefab.network.IPacketReceiver;
import universalelectricity.prefab.network.PacketManager;
import universalelectricity.prefab.tile.TileEntityElectricityRunnable;
import com.google.common.io.ByteArrayDataInput;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.network.PacketDispatcher;
import dark.library.access.AccessLevel;
import dark.library.access.UserAccess;
import dark.library.access.interfaces.ISpecialAccess;
import dark.library.access.interfaces.ITerminal;
import dark.library.terminal.commands.CommandRegistry;
/**
*
* @author Calclavia, DarkGuardsman
*
*/
public abstract class TileEntityTerminal extends TileEntityElectricityRunnable implements ISpecialAccess, 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 players using the console.
*/
public int playersUsing = 0;
/**
* The amount of lines the terminal can store.
*/
public static final int SCROLL_SIZE = 15;
/**
* Used on client side to determine the scroll of the terminal.
*/
private int scroll = 0;
@Override
public void initiate()
{
super.initiate();
}
@Override
public void updateEntity()
{
super.updateEntity();
if (!this.worldObj.isRemote)
{
if (this.playersUsing > 0)
{
if (this.ticks % 5 == 0)
{
PacketManager.sendPacketToClients(this.getDescriptionPacket(), this.worldObj, new Vector3(this), 12);
}
}
}
}
/**
* Packet Methods
*/
public abstract String getChannel();
/**
* Sends all NBT data. Server -> Client
*/
@Override
public Packet getDescriptionPacket()
{
NBTTagCompound nbt = new NBTTagCompound();
this.writeToNBT(nbt);
return PacketManager.getPacket(this.getChannel(), this, PacketType.DESCRIPTION_DATA.ordinal(), nbt);
}
/**
* Sends all Terminal data Server -> Client
*/
public void sendTerminalOutputToClients()
{
List data = new ArrayList();
data.add(PacketType.TERMINAL_OUTPUT.ordinal());
data.add(this.getTerminalOuput().size());
data.addAll(this.getTerminalOuput());
Packet packet = PacketManager.getPacket(this.getChannel(), this, data.toArray());
PacketManager.sendPacketToClients(packet, worldObj, new Vector3(this), 10);
}
/**
* Client -> Server
*
* @param entityPlayer
* @param cmdInput
*/
public void sendCommandToServer(EntityPlayer entityPlayer, String cmdInput)
{
if (this.worldObj.isRemote)
{
Packet packet = PacketManager.getPacket(this.getChannel(), this, PacketType.GUI_COMMAND.ordinal(), entityPlayer.username, cmdInput);
PacketDispatcher.sendPacketToServer(packet);
}
}
@Override
public void handlePacketData(INetworkManager network, int packetID, Packet250CustomPayload packet, EntityPlayer player, ByteArrayDataInput dataStream)
{
try
{
PacketType packetType = PacketType.values()[dataStream.readInt()];
switch (packetType)
{
case DESCRIPTION_DATA:
{
if (this.worldObj.isRemote)
{
short size = dataStream.readShort();
if (size > 0)
{
byte[] byteCode = new byte[size];
dataStream.readFully(byteCode);
this.readFromNBT(CompressedStreamTools.decompress(byteCode));
}
}
break;
}
case GUI_COMMAND:
{
if (!this.worldObj.isRemote)
{
CommandRegistry.onCommand(this.worldObj.getPlayerEntityByName(dataStream.readUTF()), this, dataStream.readUTF());
this.sendTerminalOutputToClients();
}
break;
}
case GUI_EVENT:
{
if (!this.worldObj.isRemote)
{
if (dataStream.readBoolean())
{
this.playersUsing++;
this.sendTerminalOutputToClients();
}
else
{
this.playersUsing--;
}
}
break;
}
case TERMINAL_OUTPUT:
{
if (this.worldObj.isRemote)
{
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);
}
}
break;
}
default:
break;
}
}
catch (Exception e)
{
FMLLog.severe("ICBM: Failed to receive packet for terminal.");
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;
}
@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);
return this.users.add(new UserAccess(player, lvl, save));
}
@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)
{
return this.users.removeAll(removeList);
}
return false;
}
@Override
public List<String> getTerminalOuput()
{
return this.terminalOutput;
}
@Override
public boolean addToConsole(String msg)
{
if (!this.worldObj.isRemote)
{
int usedLines = 0;
msg.trim();
if (msg.length() > 23)
{
msg = msg.substring(0, 22);
}
this.getTerminalOuput().add(msg);
this.sendTerminalOutputToClients();
return true;
}
return false;
}
@Override
public void scroll(int amount)
{
this.setScroll(this.scroll + amount);
}
@Override
public void setScroll(int length)
{
this.scroll = Math.max(Math.min(length, this.getTerminalOuput().size()), 0);
}
@Override
public int getScroll()
{
return this.scroll;
}
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
this.wattsReceived = nbt.getDouble("wattsReceived");
// 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);
nbt.setDouble("wattsReceived", this.wattsReceived);
// 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

@ -0,0 +1,96 @@
package dark.library.terminal.commands;
import java.util.ArrayList;
import java.util.List;
import dark.library.access.interfaces.ISpecialAccess;
import dark.library.access.interfaces.ITerminal;
import net.minecraft.entity.player.EntityPlayer;
public class CommandHelp extends TerminalCommand
{
@Override
public String getCommandPrefix()
{
return "help";
}
@Override
public boolean processCommand(EntityPlayer player, ITerminal TE, String[] args)
{
if (args.length > 1)
{
List<String> displayed = new ArrayList<String>();
for (TerminalCommand cc : CommandRegistry.COMMANDS)
{
if (cc.getCommandPrefix().equalsIgnoreCase(args[1]) && cc.showOnHelp(player, TE) && cc.canMachineUse(TE))
{
TE.addToConsole("----------------------");
TE.addToConsole(args[1] + " commands");
TE.addToConsole("----------------------");
List<String> ccList = cc.getCmdUses(player, TE);
for (String cm : ccList)
{
if (!displayed.contains(cm.toLowerCase()))
{
TE.addToConsole(cm);
displayed.add(cm.toLowerCase());
}
}
TE.addToConsole("----------------------");
}
}
return true;
}
else
{
TE.addToConsole("----------------------");
TE.addToConsole("Listing commands");
TE.addToConsole("----------------------");
TE.addToConsole("Help command");
for (TerminalCommand cc : CommandRegistry.COMMANDS)
{
if (cc.showOnHelp(player, TE) && cc.canMachineUse(TE))
{
List<String> ccList = cc.getCmdUses(player, TE);
for (String cm : ccList)
{
TE.addToConsole(cm);
}
}
}
TE.addToConsole("-----------------------");
return true;
}
}
@Override
public boolean canPlayerUse(EntityPlayer var1, ISpecialAccess mm)
{
return true;
}
@Override
public boolean showOnHelp(EntityPlayer player, ISpecialAccess mm)
{
return false;
}
@Override
public List<String> getCmdUses(EntityPlayer player, ISpecialAccess mm)
{
return null;
}
@Override
public boolean canMachineUse(ISpecialAccess mm)
{
return true;
}
}

View file

@ -0,0 +1,80 @@
package dark.library.terminal.commands;
import java.util.ArrayList;
import java.util.List;
import dark.library.access.interfaces.ITerminal;
import net.minecraft.entity.player.EntityPlayer;
/**
*
* @author Calclavia, DarkGuardsman
*
*/
public class CommandRegistry
{
public static final List<TerminalCommand> COMMANDS = new ArrayList<TerminalCommand>();
/**
*
* @param prefix - what the command starts with for example /time
* @param cmd - Cmd instance that will execute the command
*/
public static void register(TerminalCommand cmd)
{
if (!COMMANDS.contains(cmd))
{
COMMANDS.add(cmd);
}
}
/**
* When a player uses a command in any CMD machine it pass threw here first
*
* @param terminal - The terminal, can be cast to TileEntity.
*/
public static void onCommand(EntityPlayer player, ITerminal terminal, String cmd)
{
if (cmd != null && cmd != "")
{
TerminalCommand currentCommand = null;
String[] args = cmd.split(" ");
terminal.addToConsole("\u00a7A" + player.username + ": " + cmd);
if (args[0] != null)
{
for (TerminalCommand command : COMMANDS)
{
if (command.getCommandPrefix().equalsIgnoreCase(args[0]))
{
if (!command.canMachineUse(terminal))
{
terminal.addToConsole("N/A");
return;
}
else
{
if (!command.canPlayerUse(player, terminal))
{
terminal.addToConsole("Access Denied.");
return;
}
else
{
if (command.processCommand(player, terminal, args))
{
currentCommand = command;
return;
}
}
}
}
}
}
terminal.addToConsole("Unkown Command.");
}
}
}

View file

@ -0,0 +1,112 @@
package dark.library.terminal.commands;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import dark.library.access.AccessLevel;
import dark.library.access.interfaces.ISpecialAccess;
import dark.library.access.interfaces.ITerminal;
public class CommandUser extends TerminalCommand
{
@Override
public String getCommandPrefix()
{
return "users";
}
@Override
public boolean processCommand(EntityPlayer player, ITerminal terminal, String[] args)
{
if (args[0].equalsIgnoreCase("users") && args.length > 1 && args[1] != null && terminal instanceof ISpecialAccess)
{
ISpecialAccess turret = (ISpecialAccess) terminal;
// ILockable
if (args[1].equalsIgnoreCase("List"))
{
terminal.addToConsole("");
terminal.addToConsole("Listing Users");
for (int i = 0; i < turret.getUsers().size(); i++)
{
terminal.addToConsole(" " + i + ") " + turret.getUsers().get(i).username);
}
return true;
}
if (args[1].equalsIgnoreCase("remove") && args.length > 2)
{
if (args[2] != null)
{
if (turret.removeUserAccess(args[2]))
{
terminal.addToConsole("Removed: " + args[2]);
return true;
}
else
{
terminal.addToConsole(" User not found.");
return true;
}
}
else
{
terminal.addToConsole("Invalid username.");
return true;
}
}
if (args[1].equalsIgnoreCase("add") && args.length > 2)
{
if (args[2] != null)
{
if (turret.addUserAccess(args[2], AccessLevel.USER, true))
{
terminal.addToConsole("Added: " + args[2]);
return true;
}
else
{
terminal.addToConsole("User already exists.");
return true;
}
}
else
{
terminal.addToConsole("Invalid username.");
return true;
}
}
return false;
}
return false;
}
@Override
public boolean canPlayerUse(EntityPlayer var1, ISpecialAccess mm)
{
return mm.getUserAccess(var1.username).ordinal() >= AccessLevel.ADMIN.ordinal();
}
@Override
public boolean showOnHelp(EntityPlayer player, ISpecialAccess mm)
{
return this.canPlayerUse(player, mm);
}
@Override
public List<String> getCmdUses(EntityPlayer player, ISpecialAccess mm)
{
List<String> cmds = new ArrayList<String>();
cmds.add("users list");
cmds.add("users add [player]");
cmds.add("users remove [player]");
return cmds;
}
@Override
public boolean canMachineUse(ISpecialAccess mm)
{
return mm instanceof ISpecialAccess;
}
}

View file

@ -0,0 +1,62 @@
package dark.library.terminal.commands;
import java.util.List;
import dark.library.access.interfaces.ISpecialAccess;
import dark.library.access.interfaces.ITerminal;
import net.minecraft.entity.player.EntityPlayer;
/**
*
* @author Calclavia, DarkGuardsman
*
*/
public abstract class TerminalCommand
{
/**
* what the commands starts with /help /time /day
*
* @return
*/
public abstract String getCommandPrefix();
/**
* Executes the command
*
* @param var1
* @param args
*/
public abstract boolean processCommand(EntityPlayer player, ITerminal terminal, String[] args);
/**
* Returns true if the given command sender is allowed to use this command.
*/
public abstract boolean canPlayerUse(EntityPlayer player, ISpecialAccess specialAccess);
/**
* should this command show on /help
*
* @param player - used to find if it should show
* @return true/false
*/
public abstract boolean showOnHelp(EntityPlayer player, ISpecialAccess specialAccess);
/**
* returns the list of commands that the player can view on /help keep it shorter than 22 chars
* to fit on Cmd Gui
*
* @param player
* @return
*/
public abstract List<String> getCmdUses(EntityPlayer player, ISpecialAccess specialAccess);
/**
* some cmds can only be use on some machines but will be access by all machines. to prevent the
* cmd from activating on the machine return false
*
* @param mm
* @return
*/
public abstract boolean canMachineUse(ISpecialAccess specialAccess);
}