Advertisement
Guest User

ModEntityHelper

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