Worked on a few things

This commit is contained in:
DarkGuardsman 2013-10-18 21:07:32 -04:00
parent 31e3c83dd9
commit d7dc835aef
4 changed files with 253 additions and 46 deletions

View file

@ -10,7 +10,7 @@ import java.util.Map.Entry;
import net.minecraft.nbt.NBTTagCompound;
import dark.api.AccessLevel;
import dark.api.UserAccess;
import dark.core.prefab.helpers.NBTFileLoader;
import dark.core.prefab.helpers.NBTFileHelper;
public class GlobalAccessManager
{
@ -206,7 +206,7 @@ public class GlobalAccessManager
{
hasLoaded = true;
loading = true;
NBTFileLoader.loadData(GlobalAccessLoader.SAVE_NAME);
NBTFileHelper.loadNBTFile(GlobalAccessLoader.SAVE_NAME);
// TODO save the file
loading = false;
}

View file

@ -0,0 +1,165 @@
package dark.core.prefab.helpers;
import java.util.HashMap;
import java.util.Map.Entry;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityList;
import net.minecraft.entity.boss.EntityDragon;
import net.minecraft.entity.boss.EntityWither;
import net.minecraft.entity.item.EntityBoat;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.item.EntityMinecart;
import net.minecraft.entity.monster.EntityCreeper;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.monster.EntitySkeleton;
import net.minecraft.entity.monster.EntitySlime;
import net.minecraft.entity.monster.EntitySpider;
import net.minecraft.entity.monster.EntityZombie;
import net.minecraft.entity.passive.EntityAnimal;
import net.minecraft.entity.passive.EntityChicken;
import net.minecraft.entity.passive.EntityCow;
import net.minecraft.entity.passive.EntityPig;
import net.minecraft.entity.passive.EntitySheep;
import net.minecraft.entity.player.EntityPlayer;
/** Dictionary to track entities by several names to be used for anything. Current use is armbot task
* so the user has an easy way to ID creatures.
*
* @author DarkGuardsman */
public class EntityDictionary
{
public static HashMap<String, Class<? extends Entity>> entityMap = new HashMap();
public static HashMap<Class<? extends Entity>, Boolean> grabMap = new HashMap();
private static boolean init = false;
/** Call this very last in a mod so that all mods have a chance to load there entities */
public static void init()
{
if (!init)
{
init = true;
for (Object object : EntityList.classToStringMapping.entrySet())
{
if (object instanceof Entry)
{
Object key = ((Entry) object).getKey();
Object value = ((Entry) object).getKey();
if (key instanceof Class && value instanceof String)
{
entityMap.put((String) value, (Class) key);
}
}
}
}
}
static
{
addName("chicken", EntityChicken.class);
addName("cow", EntityCow.class);
addName("sheep", EntitySheep.class);
addName("pig", EntityPig.class);
addName("player", EntityPlayer.class);
addName("zombie", EntityZombie.class);
addName("zomb", EntityZombie.class);
addName("skeleton", EntitySkeleton.class);
addName("skel", EntitySkeleton.class);
addName("animal", EntityAnimal.class);
addName("monster", EntityMob.class);
addName("mob", EntityMob.class);
addName("creeper", EntityCreeper.class);
addName("spider", EntitySpider.class);
addName("slime", EntitySlime.class);
addName("items", EntityItem.class);
addName("item", EntityItem.class);
addName("all", Entity.class);
addName("everything", Entity.class);
addName("boat", EntityBoat.class);
addName("cart", EntityMinecart.class);
setCanNotBeGrabbed(EntityDragon.class);
setCanNotBeGrabbed(EntityWither.class);
}
public static Class<? extends Entity> get(String name)
{
return entityMap.get(name);
}
/** Can the entity be grabbed by something such as a robot, or another entity. By default most
* entities can be grabbed by another object. */
public static boolean canGrab(String name)
{
if (entityMap.containsKey(name))
{
return canGrab(entityMap.get(name));
}
return true;
}
/** Can the entity be grabbed by something such as a robot, or another entity. By default most
* entities can be grabbed by another object. */
public static boolean canGrab(Entity entity)
{
if (entity != null)
{
if (canGrab(entity.getClass()))
{
return true;
}
else
{
for (Entry<Class<? extends Entity>, Boolean> entry : grabMap.entrySet())
{
if (entry.getKey().isInstance(entity))
{
return entry.getValue();
}
}
return true;
}
}
return false;
}
/** Can the entity be grabbed by something such as a robot, or another entity. By default most
* entities can be grabbed by another object. */
public static boolean canGrab(Class<? extends Entity> clazz)
{
if (grabMap.containsKey(clazz))
{
return grabMap.get(clazz);
}
else
{
for (Entry<Class<? extends Entity>, Boolean> entry : grabMap.entrySet())
{
if (entry.getKey().isAssignableFrom(clazz))
{
return entry.getValue();
}
}
return true;
}
}
public static void setCanNotBeGrabbed(Class<? extends Entity> clazz)
{
grabMap.put(clazz, false);
}
public static void setCanBeGrabbed(Class<? extends Entity> clazz)
{
grabMap.put(clazz, true);
}
public static void addName(Class<? extends Entity> clazz, String name)
{
entityMap.put(name, clazz);
}
public static void addName(String name, Class<? extends Entity> clazz)
{
addName(clazz, name);
}
}

View file

@ -26,47 +26,68 @@ import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.FMLLog;
public class NBTFileLoader
/** Helper class used to work with minecraft's NBT file system.
*
* @author DarkGuardsman */
public class NBTFileHelper
{
/** Saves NBT data in the world folder.
*
* @return True on success. */
public static boolean saveData(File saveDirectory, String filename, NBTTagCompound data)
/** @param saveDirectory - file
* @param filename
* @param data
* @return */
public static boolean saveNBTFile(File saveDirectory, String filename, NBTTagCompound data)
{
try
{
File tempFile = new File(saveDirectory, filename + "_tmp.dat");
File file = new File(saveDirectory, filename + ".dat");
CompressedStreamTools.writeCompressed(data, new FileOutputStream(tempFile));
if (file.exists())
{
file.delete();
}
tempFile.renameTo(file);
FMLLog.fine("Saved " + filename + " NBT data file successfully.");
return true;
}
catch (Exception e)
{
System.out.println("Failed to save " + filename + ".dat!");
e.printStackTrace();
return false;
}
return saveNBTFile(new File(saveDirectory, filename), data);
}
public static boolean saveData(String filename, NBTTagCompound data)
/** Saves an NBT file
*
* @param file - exact File
* @param data - nbt data
* @return */
public static boolean saveNBTFile(File file, NBTTagCompound data)
{
return saveData(getSaveDirectory(MinecraftServer.getServer().getFolderName()), filename, data);
if (file != null && data != null)
{
try
{
File tempFile = new File(file.getAbsoluteFile(), file.getName() + ".tmp");
CompressedStreamTools.writeCompressed(data, new FileOutputStream(tempFile));
if (file.exists())
{
file.delete();
}
tempFile.renameTo(file);
FMLLog.fine("Saved " + file.getName() + " NBT data file successfully.");
return true;
}
catch (Exception e)
{
System.out.println("Failed to save " + file.getName() + ".dat!");
e.printStackTrace();
}
}
return false;
}
/** Uses the default world directory to save the data to file by the given name
*
* @param filename - file name
* @param data - nbt data
* @return true if everything goes well */
public static boolean saveNBTFile(String filename, NBTTagCompound data)
{
return saveNBTFile(getWorldSaveDirectory(MinecraftServer.getServer().getFolderName()), filename, data);
}
/** Reads NBT data from the world folder.
*
* @return The NBT data */
public static NBTTagCompound loadData(File saveDirectory, String filename)
public static NBTTagCompound loadNBTFile(File saveDirectory, String filename)
{
try
{
@ -91,12 +112,16 @@ public class NBTFileLoader
}
}
public static NBTTagCompound loadData(String filename)
/** Loads an NBT file from the current world file
*
* @param filename - name of the file
* @return NBTTagCompound that was stored in the file */
public static NBTTagCompound loadNBTFile(String filename)
{
return loadData(getSaveDirectory(MinecraftServer.getServer().getFolderName()), filename);
return loadNBTFile(getWorldSaveDirectory(MinecraftServer.getServer().getFolderName()), filename);
}
public static File getSaveDirectory(String worldName)
public static File getWorldSaveDirectory(String worldName)
{
File parent = getBaseDirectory();
@ -195,6 +220,14 @@ public class NBTFileLoader
}
/** @param key
* @param value
* @return NBTTagCompound that then can be added to save file */
public static NBTTagCompound saveObject(String key, Object value)
{
return NBTFileHelper.saveObject(new NBTTagCompound(), key, value);
}
/** Reads an unknown object with a known name from NBT
*
* @param tag - tag to read the value from
@ -284,4 +317,5 @@ public class NBTFileLoader
}
return null;
}
}

View file

@ -67,17 +67,9 @@ public abstract class TileEntityMachine extends TileEntityInv implements ISidedI
{
this.sendPowerUpdate();
}
if (this.hasGUI && this.getContainer() != null && this.ticks % 5 == 0)
if (this.ticks % 5 == 0)
{
this.playersUsingMachine = 0;
for (Object entity : this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getBoundingBox(xCoord - 10, yCoord - 10, zCoord - 10, xCoord + 10, yCoord + 10, zCoord + 10)))
{
if (entity instanceof EntityPlayer && ((EntityPlayer) entity).openContainer.getClass().equals(this.getContainer()))
{
this.playersUsingMachine += 1;
this.sendGUIPacket(((EntityPlayer) entity));
}
}
this.sendGUIPacket();
}
}
@ -225,6 +217,22 @@ public abstract class TileEntityMachine extends TileEntityInv implements ISidedI
}
public void sendGUIPacket()
{
if (this.hasGUI && this.getContainer() != null && this.ticks % 5 == 0)
{
this.playersUsingMachine = 0;
for (Object entity : this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getBoundingBox(xCoord - 10, yCoord - 10, zCoord - 10, xCoord + 10, yCoord + 10, zCoord + 10)))
{
if (entity instanceof EntityPlayer && ((EntityPlayer) entity).openContainer.getClass().equals(this.getContainer()))
{
this.playersUsingMachine += 1;
this.sendGUIPacket(((EntityPlayer) entity));
}
}
}
}
@Override
public Packet getDescriptionPacket()
{