HashMaps Storing Blocks + Location | Then Retriveing

Discussion in 'Plugin Development' started by Niknea, Jun 14, 2014.

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

    Niknea

    Hey guys,

    I am trying to store a block in a hashmap, along with an location. I am then trying to retrieve that block from the hasmap by getting the location, then place that block. How can I do this? Thanks.
     
  2. Offline

    Garris0n

    Store a BlockState?
     
  3. Offline

    Jogy34

    I don't know if that would work entirely. It sounds as if other plugins can mess it up. What you could do is create your own object that just holds a Material and data value (with anything else you may need) and store that instead.

    Also, something I found out not too long ago, about storing locations is that if the world that the location is in gets unloaded then the location is no longer valid even if the world is once again loaded up. Though, this isn't something you typically have to worry about as worlds don't usually unload unless you are going through measures to really reduce lag.
     
  4. Offline

    xTigerRebornx

    Jogy34 Using the BlockState is a valid way to do what the OP wants. It grabs a state of the block, that you can manipulate and then put back into the world as you please.
    In the case of what the OP does, he can simply store the BlockState, then when he wants that Block to be replaced, call update() on the BlockState and pass in true to force the update.
     
  5. Offline

    Garris0n

    What it's saying is that the BlockState is not always the current state of the Block. It's a snapshot of sorts. The real Block can change, but the BlockState will be the same.
     
  6. Offline

    Googlelover1234

    Niknea I'm going to give you some code that might work, but I'm not 100% sure this is exactly you want.

    Code:java
    1. private HashMap<Block, Location> bl = new HashMap<Block, Location>();
    2.  
    3. @EventHandler
    4. public void onBlockBreak(BlockBreakEvent e) {
    5. Block block = e.getBlock();
    6. Location loc = block.getLocation();
    7.  
    8. this.bl.put(block, loc);
    9. }


    This is just code I just made up from my head, so it may not work. Tahg me if you need anymore help.
     
  7. Offline

    Niknea

    Garris0n Elaborate please?

    Garris0n Googlelover1234 Here's my code:
    PHP:
          HashMap<IntegerLocationblocks = new HashMap<IntegerLocation>();
    @
    EventHandler
        
    public void onChance(BlockBreakEvent e)
        {
            
    Player p e.getPlayer();
            if(
    e.isCancelled() == true){
                return;
            }
            else
                if ((
    this.plugin.getConfig().getString("World") != null) &&
                    (
    p.getWorld().getName().equals(this.plugin.getConfig().getString("World")))) {
                
    Random random = new Random();
                
    int dice random.nextInt(300);
                if (
    dice == 7) {
                    
    e.setCancelled(true);
                  final  
    Block block e.getBlock();
     
                    
    p.sendMessage(ChatColor.DARK_AQUA "Supreme " ChatColor.GRAY "> " ChatColor.GOLD "A wild chest has appeared!");
                    final 
    Location blockloc block.getLocation();
                    
    blocks.put(e.getBlock().getTypeId(), blockloc);
                    
    blockloc.getBlock().setType(Material.CHEST);
                    
    this.chest = ((Chest)blockloc.getBlock().getState());
                    
    getItems1();
     
                    
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this.plugin, new Runnable()
                    {
                        public 
    void run() {
                            
    String s  String.valueOf(blocks.get(blockloc));
                          
    blockloc.getBlock().setType(Material.getMaterial(s));
                        }
                    }
                            , 
    300L);
                }
            }
        }
    All works, except it dosen't replace the block back to what it was, it stays as a chest.
     
  8. Offline

    Googlelover1234


    So, does it have anything in the console? I'm guessing not. You could try maybe changing the hashmap from an Integer to a block and setting the type as a block in your run() method, but I'd doubt this is the problem.
     
Thread Status:
Not open for further replies.

Share This Page