From d7dc835aef849a5caa609f4991f3356a9a480eda Mon Sep 17 00:00:00 2001 From: DarkGuardsman Date: Fri, 18 Oct 2013 21:07:32 -0400 Subject: [PATCH] Worked on a few things --- .../prefab/access/GlobalAccessManager.java | 4 +- .../core/prefab/helpers/EntityDictionary.java | 165 ++++++++++++++++++ ...{NBTFileLoader.java => NBTFileHelper.java} | 102 +++++++---- .../prefab/machine/TileEntityMachine.java | 28 +-- 4 files changed, 253 insertions(+), 46 deletions(-) create mode 100644 src/dark/core/prefab/helpers/EntityDictionary.java rename src/dark/core/prefab/helpers/{NBTFileLoader.java => NBTFileHelper.java} (76%) diff --git a/src/dark/core/prefab/access/GlobalAccessManager.java b/src/dark/core/prefab/access/GlobalAccessManager.java index a7ebd79a..3190e2e3 100644 --- a/src/dark/core/prefab/access/GlobalAccessManager.java +++ b/src/dark/core/prefab/access/GlobalAccessManager.java @@ -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; } diff --git a/src/dark/core/prefab/helpers/EntityDictionary.java b/src/dark/core/prefab/helpers/EntityDictionary.java new file mode 100644 index 00000000..96f555bc --- /dev/null +++ b/src/dark/core/prefab/helpers/EntityDictionary.java @@ -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> entityMap = new HashMap(); + public static HashMap, 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 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, 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 clazz) + { + if (grabMap.containsKey(clazz)) + { + return grabMap.get(clazz); + } + else + { + for (Entry, Boolean> entry : grabMap.entrySet()) + { + if (entry.getKey().isAssignableFrom(clazz)) + { + return entry.getValue(); + } + } + return true; + } + } + + public static void setCanNotBeGrabbed(Class clazz) + { + grabMap.put(clazz, false); + } + public static void setCanBeGrabbed(Class clazz) + { + grabMap.put(clazz, true); + } + + public static void addName(Class clazz, String name) + { + entityMap.put(name, clazz); + } + + public static void addName(String name, Class clazz) + { + addName(clazz, name); + } +} diff --git a/src/dark/core/prefab/helpers/NBTFileLoader.java b/src/dark/core/prefab/helpers/NBTFileHelper.java similarity index 76% rename from src/dark/core/prefab/helpers/NBTFileLoader.java rename to src/dark/core/prefab/helpers/NBTFileHelper.java index f9930b8a..81ca822f 100644 --- a/src/dark/core/prefab/helpers/NBTFileLoader.java +++ b/src/dark/core/prefab/helpers/NBTFileHelper.java @@ -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; } + } diff --git a/src/dark/core/prefab/machine/TileEntityMachine.java b/src/dark/core/prefab/machine/TileEntityMachine.java index d6b14420..4d9cdd26 100644 --- a/src/dark/core/prefab/machine/TileEntityMachine.java +++ b/src/dark/core/prefab/machine/TileEntityMachine.java @@ -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() {