Advertisement
Guest User

ModEntityHelper

a guest
Sep 2nd, 2012
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.58 KB | None | 0 0
  1. package net.minecraft.src;
  2.  
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.util.Arrays;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9.  
  10. import net.minecraft.src.BaseMod;
  11. import net.minecraft.src.Entity;
  12. import net.minecraft.src.EntityList;
  13. import net.minecraft.src.ModLoader;
  14. import net.minecraft.src.Packet23VehicleSpawn;
  15. import net.minecraft.src.World;
  16.  
  17. /**
  18.  * The Class ModEntityHelper makes registering and handling easy.
  19.  *
  20.  * @author lockNload147
  21.  */
  22. public class ModEntityHelper {
  23.  
  24.     /** The start entity id. */
  25.     private static int startEntityId = -1;
  26.  
  27.     /** The start vehicle type. */
  28.     private static int startVehicleType = -1;
  29.  
  30.     /** occupied vehicle Ids */
  31.     private static final int[] occupiedVehicleType;
  32.  
  33.     /** The base mod. */
  34.     private BaseMod baseMod;
  35.  
  36.     /** The class to type map. */
  37.     public Map<Class<? extends Entity>, Integer> classToTypeMap;
  38.  
  39.     /** The type to class map. */
  40.     public Map<Integer, Class<? extends Entity>> typeToClassMap;
  41.  
  42.     static {
  43.         occupiedVehicleType = new int[] { 1, 10, 11, 12, 50, 51, 60, 61, 62,
  44.                 63, 64, 65, 70, 72, 73, 75, 90 };
  45.     }
  46.  
  47.     /**
  48.      * Instantiates a new mod entity helper.
  49.      *
  50.      * @param baseMod
  51.      *            the base mod
  52.      */
  53.     public ModEntityHelper(BaseMod baseMod) {
  54.         this.baseMod = baseMod;
  55.         classToTypeMap = new HashMap<Class<? extends Entity>, Integer>();
  56.         typeToClassMap = new HashMap<Integer, Class<? extends Entity>>();
  57.     }
  58.  
  59.     /**
  60.      * Adds the entity with a unique Entity Id.
  61.      *
  62.      * @param entityClass
  63.      *            the entity class
  64.      */
  65.     public void addEntity(Class<? extends Entity> entityClass) {
  66.         addEntity(entityClass, getUniqueEntityId());
  67.     }
  68.  
  69.     /**
  70.      * Adds the entity with entity id.
  71.      *
  72.      * @param entityClass
  73.      *            the entity class
  74.      * @param entityId
  75.      *            the entity id
  76.      */
  77.     public void addEntity(Class<? extends Entity> entityClass, int entityId) {
  78.         ModLoader.registerEntityID(entityClass, entityClass.getSimpleName(),
  79.                 entityId);
  80.     }
  81.  
  82.     /**
  83.      * Adds the entity with tracking with a unique type id.
  84.      *
  85.      * @param entityClass
  86.      *            the entity class
  87.      * @param distance
  88.      *            the distance
  89.      * @param updateFrequency
  90.      *            the update frequency
  91.      * @param tracking
  92.      *            the tracking
  93.      */
  94.     public void addEntityWithTracking(Class<? extends Entity> entityClass,
  95.             int distance, int updateFrequency, boolean tracking) {
  96.         addEntityWithTracking(entityClass, getUniqueVehicleType(), distance,
  97.                 updateFrequency, tracking);
  98.     }
  99.  
  100.     /**
  101.      * Adds the entity with tracking.
  102.      *
  103.      * @param entityClass
  104.      *            the entity class
  105.      * @param type
  106.      *            the type
  107.      * @param distance
  108.      *            the distance
  109.      * @param updateFrequency
  110.      *            the update frequency
  111.      * @param tracking
  112.      *            the tracking
  113.      */
  114.     public void addEntityWithTracking(Class<? extends Entity> entityClass,
  115.             int type, int distance, int updateFrequency, boolean tracking) {
  116.         classToTypeMap.put(entityClass, type);
  117.         typeToClassMap.put(type, entityClass);
  118.         addEntity(entityClass);
  119.         ModLoader.addEntityTracker(baseMod, entityClass, type, distance,
  120.                 updateFrequency, tracking);
  121.     }
  122.  
  123.     /**
  124.      * Spawn entity.
  125.      *
  126.      * @param type
  127.      *            the type
  128.      * @param worldClient
  129.      *            the world client
  130.      * @param x
  131.      *            the x
  132.      * @param y
  133.      *            the y
  134.      * @param z
  135.      *            the z
  136.      * @return the entity
  137.      */
  138.     public Entity spawnEntity(int type, World worldClient, double x, double y,
  139.             double z) {
  140.  
  141.         Class<? extends Entity> entityClass = typeToClassMap.get(type);
  142.         if (entityClass != null)
  143.             try {
  144.                 return entityClass.getConstructor(World.class, Double.TYPE,
  145.                         Double.TYPE, Double.TYPE).newInstance(worldClient, x,
  146.                         y, z);
  147.             } catch (InstantiationException e) {
  148.                 e.printStackTrace();
  149.             } catch (IllegalAccessException e) {
  150.                 Logger.getLogger("Minecraft")
  151.                         .log(Level.SEVERE,
  152.                                 "Make sure " + entityClass
  153.                                         + "constructor is public", e);
  154.             } catch (IllegalArgumentException e) {
  155.                 Logger.getLogger("Minecraft")
  156.                         .log(Level.SEVERE,
  157.                                 "Make sure "
  158.                                         + entityClass
  159.                                         + "constructor has the arguments World.class, double, double,double",
  160.                                 e);
  161.             } catch (InvocationTargetException e) {
  162.                 e.printStackTrace();
  163.             } catch (NoSuchMethodException e) {
  164.                 Logger.getLogger("Minecraft").log(
  165.                         Level.SEVERE,
  166.                         "Missing constructor (World.class,double,double,double) in "
  167.                                 + entityClass, e);
  168.             } catch (SecurityException e) {
  169.                 e.printStackTrace();
  170.             }
  171.  
  172.         return null;
  173.     }
  174.  
  175.     /**
  176.      * Gets the spawn packet.
  177.      *
  178.      * @param entity
  179.      *            the entity
  180.      * @param type
  181.      *            the type
  182.      * @return the spawn packet
  183.      */
  184.     public Packet23VehicleSpawn getSpawnPacket(Entity entity, int type) {
  185.         return new Packet23VehicleSpawn(entity, type);
  186.     }
  187.  
  188.     /**
  189.      * Gets the type by entity.
  190.      *
  191.      * @param entityClass
  192.      *            the entity class
  193.      * @return the type by entity
  194.      */
  195.     public int getTypeByEntity(Class<? extends Entity> entityClass) {
  196.         return classToTypeMap.get(entityClass);
  197.     }
  198.  
  199.     /**
  200.      * Gets the type by entity.
  201.      *
  202.      * @param entityName
  203.      *            the entity name
  204.      * @return the type by entity
  205.      */
  206.     public int getTypeByEntity(String entityName) {
  207.         try {
  208.             return classToTypeMap.get(Class.forName(entityName));
  209.         } catch (ClassNotFoundException e) {
  210.             e.printStackTrace();
  211.         }
  212.  
  213.         return -1;
  214.     }
  215.  
  216.     /**
  217.      * Gets the entity by name.
  218.      *
  219.      * @param type
  220.      *            the type
  221.      * @return the entity by name
  222.      */
  223.     public Class<? extends Entity> getEntityByName(int type) {
  224.         return typeToClassMap.get(type);
  225.     }
  226.  
  227.     /**
  228.      * Gets the unique entity id.
  229.      *
  230.      * @return the unique entity id
  231.      */
  232.     public static int getUniqueEntityId() {
  233.         do {
  234.             startEntityId++;
  235.             if (startEntityId > 255)
  236.                 Logger.getLogger("Minecraft").log(Level.WARNING,
  237.                         "Entity Id is greater than 255: " + startEntityId);
  238.         } while (EntityList.getStringFromID(startEntityId) != null);
  239.  
  240.         return startEntityId;
  241.     }
  242.  
  243.     /**
  244.      * Gets the unique vehicle type.
  245.      *
  246.      * @return the unique vehicle type
  247.      */
  248.     public static int getUniqueVehicleType() {
  249.         do {
  250.             startVehicleType++;
  251.             if (startVehicleType > 255)
  252.                 Logger.getLogger("Minecraft")
  253.                         .log(Level.WARNING,
  254.                                 "Vehicle type is greater than 255: "
  255.                                         + startVehicleType);
  256.         } while (Arrays.binarySearch(occupiedVehicleType, startVehicleType) > -1);
  257.  
  258.         return startVehicleType;
  259.     }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement