Regenerating land

Discussion in 'Plugin Development' started by Lolmewn, Nov 29, 2011.

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

    Lolmewn

    I want to regenerate a piece of land. It first has to save the blocks, and when I tell it to, regenerate.
    Right now, this is my code:
    Code:
    private World w;
    	private Location spawn1, spawn2, spec;
    	private HashMap<Location, Block> blocks = new HashMap<Location, Block>();
    
    	public Arena(Location first, Location two){
    		if(first.getBlockX() > two.getBlockX()){
    			lowestX = two.getBlockX();
    			highestX = first.getBlockX();
    		}else{
    			lowestX = first.getBlockX();
    			highestX = two.getBlockX();
    		}
    		if(first.getBlockY() > two.getBlockY()){
    			lowestY = two.getBlockY();
    			highestY = first.getBlockY();
    		}else{
    			lowestY = first.getBlockY();
    			highestY = two.getBlockY();
    		}
    		if(first.getBlockZ() > two.getBlockZ()){
    			lowestZ = two.getBlockZ();
    			highestZ = first.getBlockZ();
    		}else{
    			lowestZ = first.getBlockZ();
    			highestZ = two.getBlockZ();
    		}
    		w = first.getWorld();
    		addBlocksToArray();
    	}
    
    	private void addBlocksToArray() {
    		for(int a = lowestX; a < highestX; a++){
    			for(int b = lowestY; b < highestY; b++){
    				for(int c = lowestZ; c < highestZ; c++){
    					Location loc = new Location(w, a, b, c);
    					blocks.put(loc, Bukkit.getWorld(w.getName()).getBlockAt(loc));
    					System.out.println(a + "," + b + "," + c + " added.");
    				}
    			}
    		}
    	}
    	public void regenArena(){
    		for(Location loc: blocks.keySet()){
    			loc.getBlock().setTypeId(blocks.get(loc).getTypeId());
    		}
    	}
    It doesn't add any blocks in the addBlockToArray()
    Did I do something wrong?

    Or does anyone know how WorldEdit does this?
    I searched their code but it's so much :O
    Can't find it..

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
  2. Offline

    IDragonfire

  3. Offline

    Lolmewn

    I know the code is not complete, but I think the variables are obvious. The lowestX and highestX (and the others) are gotten from 2 locations, here:
    Code:
    public class Arena {
    	private int lowestX, lowestY, lowestZ, highestX, highestY, highestZ;
    	private World w;
    	private Location spawn1, spawn2, spec;
    	private HashMap<Location, Block> blocks = new HashMap<Location, Block>();
    
    	public Arena(Location first, Location two){
    		if(first.getBlockX() > two.getBlockX()){
    			lowestX = two.getBlockX();
    			highestX = first.getBlockX();
    		}else{
    			lowestX = first.getBlockX();
    			highestX = two.getBlockX();
    		}
    		if(first.getBlockY() > two.getBlockY()){
    			lowestY = two.getBlockY();
    			highestY = first.getBlockY();
    		}else{
    			lowestY = first.getBlockY();
    			highestY = two.getBlockY();
    		}
    		if(first.getBlockZ() > two.getBlockZ()){
    			lowestZ = two.getBlockZ();
    			highestZ = first.getBlockZ();
    		}else{
    			lowestZ = first.getBlockZ();
    			highestZ = two.getBlockZ();
    		}
    		w = first.getWorld();
    		addBlocksToArray();
    	}
     
  4. Offline

    ItsHarry

  5. Offline

    Lolmewn

  6. From what I have seen, you don't want to "regenerate" the blocks (with the world generator), but restore them from a set of saved blocks.
    Without actually scanning for mistakes in your code, I can tell you: Your concept won't work out.
    Block objects are real representations of the block they are assigned to. If you store them and try to get their id/type later, it will return the current value and not (as you probably intended) the one at a specific time.

    That's why there is the concept with BlockStates. Those are actually snapshots of the current state of the block as the BlockState was created (you can do that with block.getState()). It won't ever change, and you can set the block back to what it was using blockState.update(true).

    An example of the usage of BlockStates is applied in dinnerbone's small youtube tutorial series:
    http://www.youtube.com/user/dinnerbone
     
Thread Status:
Not open for further replies.

Share This Page