pulled in more of my code collection

This commit is contained in:
Robert Seifert 2013-04-20 08:05:28 -04:00
parent 5e6098313e
commit a1d51ae1ef
17 changed files with 897 additions and 4 deletions

View file

@ -0,0 +1,26 @@
package dark.library.gui;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.tileentity.TileEntity;
public class ContainerFake extends Container
{
TileEntity entity = null;
public ContainerFake(TileEntity entity)
{
this.entity = entity;
}
public boolean canInteractWith(EntityPlayer par1EntityPlayer)
{
if (entity instanceof IInventory)
{
return ((IInventory) this.entity).isUseableByPlayer(par1EntityPlayer);
}
return true;
}
}

View file

@ -0,0 +1,42 @@
package dark.library.gui;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import org.lwjgl.opengl.GL11;
public class GuiButtonArrow extends GuiButton
{
boolean isLeft = false;
public GuiButtonArrow(int par1, int par2, int par3, boolean left)
{
super(par1, par2, par3, 8, 8, "");
isLeft = left;
}
/**
* Draws this button to the screen.
*/
public void drawButton(Minecraft par1Minecraft, int width, int hight)
{
if (this.drawButton)
{
GL11.glBindTexture(GL11.GL_TEXTURE_2D, par1Minecraft.renderEngine.getTexture("/dark/library/resources/textures/gui/gui@.png"));
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
boolean var4 = width >= this.xPosition && hight >= this.yPosition && width < this.xPosition + this.width && hight < this.yPosition + this.height;
int var5 = 106;
int varWid = 28;
if (isLeft)
{
varWid -= 8;
}
if (var4)
{
var5 += this.height;
}
this.drawTexturedModalRect(this.xPosition, this.yPosition, varWid, var5, this.width, this.height);
}
}
}

View file

@ -1,4 +1,4 @@
package dark.helpers;
package dark.library.helpers;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

View file

@ -1,4 +1,4 @@
package dark.helpers;
package dark.library.helpers;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;

View file

@ -1,4 +1,4 @@
package dark.helpers;
package dark.library.helpers;
import java.util.ArrayList;
import java.util.List;

View file

@ -1,4 +1,4 @@
package dark.helpers;
package dark.library.helpers;
public class MetaGroup

View file

@ -0,0 +1,43 @@
package dark.library.locking;
public enum AccessLevel
{
NONE("None"), BASIC("Basic"), USER("Standard"), ADMIN("Admin"), OWNER("Owner");
public String displayName;
private AccessLevel(String name)
{
displayName = name;
}
/**
* Gets the access level in varies ways
*
* @return AccessLevel NONE instead of null if correct level can't be found.
*/
public static AccessLevel get(Object ob)
{
if (ob instanceof String)
{
for (AccessLevel access : AccessLevel.values())
{
if (access.displayName.equalsIgnoreCase((String) ob) || access.name().equalsIgnoreCase((String) ob))
{
return access;
}
}
}
if (ob instanceof Integer)
{
int i = (Integer) ob;
if (i >= 0 && i < AccessLevel.values().length)
{
return AccessLevel.values()[i];
}
}
return NONE;
}
}

View file

@ -0,0 +1,205 @@
package dark.library.locking;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.nbt.NBTTagCompound;
import universalelectricity.prefab.flag.NBTFileLoader;
public class GlobalAccessList
{
/** Hash map of loaded lists **/
private static Map<String, List<UserAccess>> globalUserLists = new HashMap<String, List<UserAccess>>();
/** Master save NBT that gets saved **/
private static NBTTagCompound masterSaveNbt = new NBTTagCompound();
/** Used to check to see if file is in the process of being loaded **/
public static boolean loading = false;
/** Used to check to see if file was loaded at least once **/
public static boolean hasLoaded = false;
/** Used to check to see if file was changed and needs saved **/
public static boolean needsSaving = false;
/**
* Gets or creates a userAccess list to be used for any reason
*
* @param name - name of the access list being created or loaded
* @param owner - the player's name to be used to create a new list
* @return - UserAccess list
*/
public List<UserAccess> getOrCreateList(String name, String owner)
{
if (name.toCharArray().length < 5 || owner.isEmpty() || name.startsWith("Default#"))
{
return null;
}
List<UserAccess> list = getList(name);
if (list == null)
{
list = this.createList(name, owner);
}
return list;
}
/**
* creates a new user access list
*
* @param name
* @param owner
* @return
*/
public List<UserAccess> createList(String name, String owner)
{
/*** Creates a new List if one doesn't exist ***/
List<UserAccess> list = new ArrayList<UserAccess>();
list.add(new UserAccess(owner, AccessLevel.OWNER, true));
globalUserLists.put(name, list);
saveList(name, list);
this.needsSaving = true;
return list;
}
/**
* Loads up a UserAccess List
*
* @param name - name of the list
* @return - the list
*/
public List<UserAccess> getList(String name)
{
if (globalUserLists.containsKey(name))
{
/*** Get the list if its already loaded up ***/
return globalUserLists.get(name);
}
else
{
/*** Loads the saved list if it exists ***/
List<UserAccess> list = loadList(name);
if (list != null)
{
globalUserLists.put(name, list);
}
return list;
}
}
/**
* adds a user to the global list
*
* @param listName - name of the list
* @param user - user being added as a UserAccess instance
* @return true if added
*/
public boolean addUser(String listName, UserAccess user)
{
if (user == null)
{
return false;
}
List<UserAccess> userList = this.getList(listName);
if (userList != null)
{
if (userList.contains(user))
{
userList = UserAccess.removeUserAccess(user.username, userList);
}
if (userList.add(user))
{
globalUserLists.put(listName, userList);
this.saveList(listName, userList);
this.needsSaving = true;
return true;
}
}
return false;
}
/**
* Removes a user from the global list
*
* @param listName - name of the list
* @param user - user being removed
* @return true if removed
*/
public boolean removeUser(String listName, UserAccess user)
{
if (user == null)
{
return false;
}
List<UserAccess> userList = this.getList(listName);
if (userList != null)
{
if (userList.contains(user))
{
userList = UserAccess.removeUserAccess(user.username, userList);
globalUserLists.put(listName, userList);
this.saveList(listName, userList);
this.needsSaving = true;
return true;
}
}
return false;
}
/**
* Loads a given Global user list from the master save
*
* @param name - name given to the list for reference
* @return - the list of user access levels to be used
*/
private List<UserAccess> loadList(String name)
{
NBTTagCompound masterSave = getMasterSaveFile();
if (masterSave != null && masterSave.hasKey(name))
{
NBTTagCompound accessSave = masterSave.getCompoundTag(name);
return UserAccess.readListFromNBT(accessSave, "Users");
}
return null;
}
/**
* Saves a given Global user list into the master save
*
* @param name - name to save the list as
* @param list - list to be saved
*/
private void saveList(String name, List<UserAccess> list)
{
NBTTagCompound masterSave = getMasterSaveFile();
if (masterSave != null)
{
NBTTagCompound accessSave = masterSave.getCompoundTag(name);
UserAccess.writeListToNBT(accessSave, list);
this.getMasterSaveFile().setCompoundTag(name, accessSave);
}
}
/**
* Loads the master save from the world folder
*/
public static NBTTagCompound getMasterSaveFile()
{
if (masterSaveNbt.hasNoTags())
{
if (!loading)
{
hasLoaded = true;
loading = true;
NBTFileLoader.loadData(GlobalAccessLoader.SAVE_NAME);
// TODO save the file
loading = false;
}
}
return masterSaveNbt;
}
}

View file

@ -0,0 +1,55 @@
package dark.library.locking;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.MinecraftForge;
import cpw.mods.fml.common.Mod.ServerStarting;
import cpw.mods.fml.common.event.FMLServerStartingEvent;
import dark.library.saving.INbtSave;
import dark.library.saving.SaveManager;
public class GlobalAccessLoader implements INbtSave
{
public static boolean isInitialized = false;
public static GlobalAccessLoader intance = new GlobalAccessLoader();
/** Name of the save file **/
public static final String SAVE_NAME = "Global_Access_List";
public void initiate()
{
if (!isInitialized)
{
MinecraftForge.EVENT_BUS.register(this);
SaveManager.intance.registerNbtSave(this);
isInitialized = true;
}
}
@ServerStarting
public void serverStarting(FMLServerStartingEvent event)
{
if (!GlobalAccessList.hasLoaded)
{
GlobalAccessList.getMasterSaveFile();
}
}
@Override
public String saveFileName()
{
return this.SAVE_NAME;
}
@Override
public NBTTagCompound getSaveData()
{
return GlobalAccessList.getMasterSaveFile();
}
@Override
public boolean shouldSave(boolean isServer)
{
return isServer && GlobalAccessList.hasLoaded && !GlobalAccessList.loading;
}
}

View file

@ -0,0 +1,39 @@
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

@ -0,0 +1,114 @@
package dark.library.locking;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
public class UserAccess
{
public String username;
public AccessLevel level;
public boolean shouldSave;
public UserAccess(String user, AccessLevel level, boolean save)
{
this.username = user;
this.level = level;
this.shouldSave = save;
}
/**
* Write to nbt
*/
public NBTTagCompound writeToNBT(NBTTagCompound nbt)
{
nbt.setString("username", this.username);
nbt.setInteger("ID", this.level.ordinal());
return nbt;
}
/**
* Reads a userAccess instance from nbt
*/
public void readFromNBT(NBTTagCompound nbt)
{
this.username = nbt.getString("username");
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);
access.readFromNBT(nbt);
return access;
}
/**
* Reads an entire UserAccess list from an nbt file
*
* @param nbt - nbt being read
* @return - the list
*/
public static List<UserAccess> readListFromNBT(NBTTagCompound nbt, String tagName)
{
NBTTagList userList = nbt.getTagList(tagName);
List<UserAccess> users = new ArrayList<UserAccess>();
for (int i = 0; i < userList.tagCount(); ++i)
{
NBTTagCompound var4 = (NBTTagCompound) userList.tagAt(i);
users.add(UserAccess.loadFromNBT(var4));
}
return users;
}
/**
* writes an entire UserAccess list to nbt at one time
*
* @param save - nbt to save to
* @param users - list to save
*/
public static void writeListToNBT(NBTTagCompound save, List<UserAccess> users)
{
NBTTagList usersTag = new NBTTagList();
for (int player = 0; player < users.size(); ++player)
{
UserAccess access = users.get(player);
if (access != null && access.shouldSave)
{
NBTTagCompound accessData = new NBTTagCompound();
access.writeToNBT(accessData);
usersTag.appendTag(accessData);
}
}
save.setTag("Users", usersTag);
}
/**
* Removes a user from a list of UserAccess then returns that list
*/
public static List<UserAccess> removeUserAccess(String player, List<UserAccess> users)
{
List<UserAccess> removeList = new ArrayList<UserAccess>();
List<UserAccess> returnList = users;
for (int i = 0; i < users.size(); i++)
{
UserAccess ref = users.get(i);
if (ref.username.equalsIgnoreCase(player))
{
removeList.add(ref);
}
}
if (removeList != null && removeList.size() > 0)
{
returnList.removeAll(removeList);
}
return returnList;
}
}

View file

@ -0,0 +1,280 @@
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);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

View file

@ -0,0 +1,27 @@
package dark.library.saving;
import net.minecraft.nbt.NBTTagCompound;
/**
* Classes the register to need saving on world save use this
*
* @author DarkGuardsman
*
*/
public interface INbtSave
{
/**
* gets the file name to save as
*/
public String saveFileName();
/**
* the data to save when saving to the file
*/
public NBTTagCompound getSaveData();
/**
* can the file be saved at this moment
*/
public boolean shouldSave(boolean isServer);
}

View file

@ -0,0 +1,62 @@
package dark.library.saving;
import java.util.ArrayList;
import java.util.List;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.world.WorldEvent;
import universalelectricity.prefab.flag.NBTFileLoader;
import cpw.mods.fml.common.Mod.ServerStopping;
import cpw.mods.fml.common.event.FMLServerStoppingEvent;
public class SaveManager
{
public static List<INbtSave> nbtSaveList = new ArrayList<INbtSave>();
public static boolean isInitialized = false;
public static SaveManager intance = new SaveManager();
/**
* registers a class that uses INbtSave to save data to a file in the worldSave file
*
* @param saveClass
*/
public void registerNbtSave(INbtSave saveClass)
{
if (!isInitialized)
{
MinecraftForge.EVENT_BUS.register(this);
isInitialized = true;
}
if (saveClass != null && !nbtSaveList.contains(saveClass))
{
nbtSaveList.add(saveClass);
}
}
@ForgeSubscribe
public void onWorldSave(WorldEvent.Save event)
{
save(!event.world.isRemote);
}
@ServerStopping
public void serverStopping(FMLServerStoppingEvent event)
{
save(true);
}
public void save(boolean isServer)
{
for (INbtSave save : nbtSaveList)
{
if (save.shouldSave(isServer))
{
NBTFileLoader.saveData(save.saveFileName(), save.getSaveData());
}
}
}
}