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 net.minecraft.nbt.NBTTagCompound;
import dark.api.AccessLevel; import dark.api.AccessLevel;
import dark.api.UserAccess; import dark.api.UserAccess;
import dark.core.prefab.helpers.NBTFileLoader; import dark.core.prefab.helpers.NBTFileHelper;
public class GlobalAccessManager public class GlobalAccessManager
{ {
@ -206,7 +206,7 @@ public class GlobalAccessManager
{ {
hasLoaded = true; hasLoaded = true;
loading = true; loading = true;
NBTFileLoader.loadData(GlobalAccessLoader.SAVE_NAME); NBTFileHelper.loadNBTFile(GlobalAccessLoader.SAVE_NAME);
// TODO save the file // TODO save the file
loading = false; 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,17 +26,32 @@ import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.FMLLog; import cpw.mods.fml.common.FMLLog;
public class NBTFileLoader /** Helper class used to work with minecraft's NBT file system.
{
/** Saves NBT data in the world folder.
* *
* @return True on success. */ * @author DarkGuardsman */
public static boolean saveData(File saveDirectory, String filename, NBTTagCompound data) public class NBTFileHelper
{
/** @param saveDirectory - file
* @param filename
* @param data
* @return */
public static boolean saveNBTFile(File saveDirectory, String filename, NBTTagCompound data)
{
return saveNBTFile(new File(saveDirectory, filename), data);
}
/** Saves an NBT file
*
* @param file - exact File
* @param data - nbt data
* @return */
public static boolean saveNBTFile(File file, NBTTagCompound data)
{
if (file != null && data != null)
{ {
try try
{ {
File tempFile = new File(saveDirectory, filename + "_tmp.dat"); File tempFile = new File(file.getAbsoluteFile(), file.getName() + ".tmp");
File file = new File(saveDirectory, filename + ".dat");
CompressedStreamTools.writeCompressed(data, new FileOutputStream(tempFile)); CompressedStreamTools.writeCompressed(data, new FileOutputStream(tempFile));
@ -47,26 +62,32 @@ public class NBTFileLoader
tempFile.renameTo(file); tempFile.renameTo(file);
FMLLog.fine("Saved " + filename + " NBT data file successfully."); FMLLog.fine("Saved " + file.getName() + " NBT data file successfully.");
return true; return true;
} }
catch (Exception e) catch (Exception e)
{ {
System.out.println("Failed to save " + filename + ".dat!"); System.out.println("Failed to save " + file.getName() + ".dat!");
e.printStackTrace(); e.printStackTrace();
}
}
return false; return false;
} }
}
public static boolean saveData(String filename, NBTTagCompound data) /** 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 saveData(getSaveDirectory(MinecraftServer.getServer().getFolderName()), filename, data); return saveNBTFile(getWorldSaveDirectory(MinecraftServer.getServer().getFolderName()), filename, data);
} }
/** Reads NBT data from the world folder. /** Reads NBT data from the world folder.
* *
* @return The NBT data */ * @return The NBT data */
public static NBTTagCompound loadData(File saveDirectory, String filename) public static NBTTagCompound loadNBTFile(File saveDirectory, String filename)
{ {
try 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(); 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 /** Reads an unknown object with a known name from NBT
* *
* @param tag - tag to read the value from * @param tag - tag to read the value from
@ -284,4 +317,5 @@ public class NBTFileLoader
} }
return null; return null;
} }
} }

View file

@ -67,17 +67,9 @@ public abstract class TileEntityMachine extends TileEntityInv implements ISidedI
{ {
this.sendPowerUpdate(); this.sendPowerUpdate();
} }
if (this.hasGUI && this.getContainer() != null && this.ticks % 5 == 0) if (this.ticks % 5 == 0)
{ {
this.playersUsingMachine = 0; this.sendGUIPacket();
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));
}
}
} }
} }
@ -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 @Override
public Packet getDescriptionPacket() public Packet getDescriptionPacket()
{ {