Using Hashmap to record clicked blocks and then use them in the future?

Discussion in 'Plugin Development' started by NeoSilky, Oct 22, 2011.

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

    NeoSilky

    Okay, so im doing something *KINDA* like the saving of .schematics in WorldEdit, so I need it to be able to let a player click two different blocks (left and right click) and then save all the blocks inside it to a *blueprint file*. How would I do this?

    Currently i have:
    Code:
    package me.neosilky.autobuilder;
    
    
    import java.io.File;
    
    import org.bukkit.ChatColor;
    import org.bukkit.block.Block;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerListener;
    
    public class AutoBuilderPlayerListener extends PlayerListener {
    	  public static AutoBuilder plugin;
    	  public AutoBuilderPlayerListener(AutoBuilder instance){
    	    plugin = instance;
    	  }	  
    	  
    	  public void onPlayerInteract(PlayerInteractEvent event) {
    		  Player player = event.getPlayer();
    		  Action action = event.getAction();
    		  Block block = event.getClickedBlock();
    		  FileConfiguration config = YamlConfiguration.loadConfiguration(new File(AutoBuilder.plugindir, "config.yml"));
    		  
    		  if(block == null) {
    			  return;
    		  }
    		  
    	      if (action == Action.LEFT_CLICK_BLOCK && player.getItemInHand().getTypeId() == config.getInt("RegionSelect.Tool")) {
    	          AutoBuilder.point1.put(player, block.getLocation());
    	          player.sendMessage(ChatColor.LIGHT_PURPLE + "[1]" + ChatColor.BLUE + " X: " + ChatColor.LIGHT_PURPLE + block.getX() + ChatColor.BLUE + " Y: " + ChatColor.LIGHT_PURPLE + block.getY() + ChatColor.BLUE + " Z: " + ChatColor.LIGHT_PURPLE + block.getZ());
    	          return;
    	        }
    	      if (action == Action.RIGHT_CLICK_BLOCK && player.getItemInHand().getTypeId() == config.getInt("RegionSelect.Tool")) {
    	          AutoBuilder.point2.put(player, block.getLocation());
    	          player.sendMessage(ChatColor.LIGHT_PURPLE + "[2]" + ChatColor.BLUE + " X: " + ChatColor.LIGHT_PURPLE + block.getX() + ChatColor.BLUE + " Y: " + ChatColor.LIGHT_PURPLE + block.getY() + ChatColor.BLUE + " Z: " + ChatColor.LIGHT_PURPLE + block.getZ());
    	          return;
    	        }
    	  }
    }
    
    It works fine, and im guessing it saves the location to a hashmap. Now how would i use this hashmap to save to a file or something?

    Thanks!
     
  2. Offline

    Jogy34

    you could do it a hard way by making a 4d array saving the x,y,z location in the first 3 parts and the block id in the last one.
     
  3. Offline

    Windwaker

    It depends how you want to store it. If you don't mind the user not being able to manipulate the HashMap values then you can just write it to disk as is:

    Code:java
    1.  
    2. try{
    3. oos.writeObject(yourHashMap);
    4. oos.flush();
    5. oos.close();
    6. }catch(Exception e){
    7. e.printStackTrace();
    8.  


    This produces something that is unable to be manipulated by the user though, and is simply used for storing objects to disk. If you want to produce something readable and editable your best bet would to get the x, y, and z of the block and write that to a file. (Bukkit's FileConfiguration perhaps?).
     
Thread Status:
Not open for further replies.

Share This Page