Create and Spawn entity, and control it?

Discussion in 'Plugin Development' started by cvcs1, May 16, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Shamebot

    Think of a hashmap as a dictionary, you have a key and a value,
    let's say your key is CreatureType.Sheep so you can get the value with HashMap.get(CreatureType.Sheep)
    The type of the key and the value is set when you define your hasmap,
    for example
    Code:
    HashMap<Integer,String> someHashMap = new HashMap<Integer,String>();
    someHashMap.put(4,"hi");
    someHashMap.put(12,"--helloo--");
    System.out.println(someHashMap.get(4));
    
    HashMap<Player,Boolean> someHashMap2 = new HashMap<Player,Boolean>();
    someHashMap2.put(getServer().getPlayer("Shamebot"),true);
    someHashMap2.put(getServer().getPlayer("asdf"),false);
    System.out.println(someHashMap2.get(getServer().getPlayer("Shamebot")));
     
  2. Oh ok, so I could do like:

    Code:
    HashMap<Chunk,ArrayList<Creature>> someHashMap = new HashMap<Chunk,ArrayList<Creature>>;
    someHashMap.put(chunkThatHasSavedEntity, arrayWithTheSavedEntities);
    ArrayList entities = someHashMap.get(chunkThatIsLoading);
    if (entities != null) {// or !entities.isEmpty()?
    for (int x = 0; x < entities.length; x ++){
    spawnCreature(entities[x]);
    }
    
    Or I could use an Array instead of an ArrayList, doesnt matter.
     
  3. Offline

    Shamebot

    I would add a HashMap<CreatureType,Integer> which stores the amount of entities which should be in the area, to the Region class I posted.
    When somebody creates a region for his animals you create a new instance of the Region class and add the animals to the HashMap. Then onChunkLoad check all of you Region instances if the chunk is part one, load all of it and spawn the missing entities.
     
  4. Hmm... I'm beginning to understand. How exactly do you define a region?
     
  5. Offline

    Shamebot

    Either by a command, something like /plugin pos1 and /plugin pos2 or by rightclicking.
    The you do Region region = new Region(...) and store it in an ArrayList.
    I'd add:
    1. A method like chunkIntersects(Chunk chunk) to check whether a chunk is part of the Region in onChunkLoad
    2. A mthod like getChunks() which returns all chunks part of the Region, you just need to copy some code
    3. the mentioned HashMap, question is how to now the entities to add, maybe the player rightclicking them
     
  6. Ok thanks for all this help. For one:

    If I have to locations, is min and max the two cornerS?
     
  7. Offline

    Shamebot

    You mean these? Yes, so you can check whether a location is within them easily.
     
  8. Is there a way to get the surrounding blocks (edge) of the two points? So I can put a barrier of fences?
     
  9. Offline

    Shamebot

    Sure, have a look at Block.getFace (BlockFace face, int distance) or rather Block.getRelative (int modX, int modY, int modZ).
     
  10. Ok you lost me... Sorry about all of this trouble. I want, after the user has selected two points, two create a fence around the square that the two points are corners of.
     
  11. Offline

    Shamebot

    Maybe it's easier to use World.GetBlockAt(...)
    1. Count from min.getX() to max.getX()
    2. set the block at xcount, min.getZ() to fence
    3. set the block at xcount, max.getZ() to fence
    1. Count from min.getZ() to max.getZ()
    2. set the block at min.getX(), zcount to fence
    3. set the block at max.getX(), zcount to fence
    Question is, what to do, if the high of the corners aren't equal.
     
  12. Hm. Ok so right now I have it so you can select the points, it saves them, and spawns a [Insert Animal]
    The problems I'm having now are:
    1. I do not understand your math that you just posted. (as in do I have to make a class region? what does it do? etc)
    2. I don't know how to make an animal spawn in the middle. tried some complex math thing that failed. (pig 10 block higher??? ahhaha)

    Thanks again!
     
  13. Offline

    Shamebot

    So you managed to create the fence?
    x >> y shifts the bits of x y places, or said in another way divides x by 2^y and cuts the decimal places, so
    x >> 4 divides x by 16 to get the chunk coordinates.

    You don't neccesarily have to make the region class, but I recommend it, so you have everything related to your region in one class and don't clutter up your plugin class, simply said it's easier to read/keep track of what your code does.

    Try
    Code:
    Location middle = min.clone().setX(min.getX()+(max.getX()-min.getX())/2);
    middle.setZ(min.getZ()+(max.getZ()-min.getZ())/2);
     
  14. Yes, I managed to create a fence, however (not a big deal) it leaves a corner empty, just one, when it creates the fences. rather annoying.
     
  15. Offline

    Shamebot

    How does your code look like? Maybe you used a < instead of a <= ?
     
  16. Here it is:
    Code:
         
     int chunkMinX = min.getBlockX();  //Edit: fixed mistake here
             int chunkMaxX = max.getBlockX();
             int chunkMinZ = min.getBlockZ();
             int chunkMaxZ = max.getBlockZ();
     
             player.sendMessage(ChatColor.DARK_PURPLE + "Making Fence!");
    
             for (int x = chunkMaxZ; x > chunkMinZ; x --){
            	 Location location = new Location(min.getWorld(), chunkMaxX, y, x);
            	 Block block = location.getBlock();
            	 block.setType(Material.FENCE);
             }
             for (int x = chunkMaxX; x > chunkMinX; x --){
            	 Location location = new Location(min.getWorld(), x, y, chunkMaxZ);
            	 Block block = location.getBlock();
            	 block.setType(Material.FENCE);
    
             }
             for (int x = chunkMaxZ; x > chunkMinZ; x --){
            	 Location location = new Location(min.getWorld(), chunkMinX, y, x);
            	 Block block = location.getBlock();
            	 block.setType(Material.FENCE);
    
             }
             for (int x = chunkMaxX; x > chunkMinX; x --){
            	 Location location = new Location(min.getWorld(), x, y, chunkMinZ);
            	 Block block = location.getBlock();
            	 block.setType(Material.FENCE);
    
             }
    
    I think your right though. Im going to change them to >=/<=
     
  17. Offline

    Shamebot

    Little improvements:
    merge the 4 loops into 2
    you can use min.getWorld.getBlockAt(x,y,z)
    rename your variables a little, confused me a bit
     
Thread Status:
Not open for further replies.

Share This Page