Replacing blocks from a list

Discussion in 'Spigot Plugin Development' started by SirNickParks, Jun 8, 2015.

  1. Hey guys, so i've been working on this bug for quite a while now. And it's starting to get annoying so I came for help.

    So what i'm trying to do is take my HashMap of <Location, BlockState> and loop it through to replace the blocks that were broken.

    Here's the code:
    Code (Text):
    public HashMap<Location, BlockState> blocksToReoll;

    int repaired = 0;
    for(Location location : blocksToReoll.keySet()){
           Bukkit.broadcastMessage("Repaired: " + repaired);
    Bukkit.broadcastMessage("In list: " + blocksToReoll.size() + " - Fixed: " + repaired);
    Yes everything is registered and all. And I even get an output like so:
    Code (Text):
    [15:24:15] [Server thread/INFO]: Repaired: 1[m
    [15:24:15] [Server thread/INFO]: Repaired: 2[m
    [15:24:15] [Server thread/INFO]: Repaired: 3[m
    [15:24:15] [Server thread/INFO]: Repaired: 4[m
    [15:24:15] [Server thread/INFO]: Repaired: 5[m
    [15:24:15] [Server thread/INFO]: Repaired: 6[m
    [15:24:15] [Server thread/INFO]: Repaired: 7[m
    [15:24:15] [Server thread/INFO]: Repaired: 8[m
    [15:24:15] [Server thread/INFO]: Repaired: 9[m
    [15:24:15] [Server thread/INFO]: Repaired: 10[m
    [15:24:15] [Server thread/INFO]: Repaired: 11[m
    [15:24:15] [Server thread/INFO]: Repaired: 12[m
    [15:24:15] [Server thread/INFO]: Repaired: 13[m
    [15:24:15] [Server thread/INFO]: Repaired: 14[m
    [15:24:15] [Server thread/INFO]: In list: 14 - Fixed: 14[m
    But there's still some gaps and blocks that aren't replaced and I don't know why. Any ideas/help?
  2. Oops, didn't even realize this was in the wrong section. Sorry :(
  3. Save the blockstates instead of the material. To rollback just call BlockState#update
  4. I tried that, unfortunately had the same result. I'm thinking about using this:

    Seems to be fancy and works well.
    • If you need the key & value of the Map in a loop, use the entry set rather than the key set. It saves you a Map#get.
    • Change your field type to Map rather than HashMap. It's considered good practice to use the most generic type available (in this case HashMap does not give you any special methods over Map)
    • Use BlockState#update(true) instead of setType. It will also set the data type, and any tile entity data (though the latter is a bit broken in the current version of Spigot).
    • In this case, you don't even need the keys, hence you can just loop over Map#values().
    • A HashMap is not ordered, and could cause issues with physics and such. To avoid this, use a LinkedHashMap - it behaves exactly like a HashMap, but will keep it's order (insertion order by default, afaik)
  5. It ended it up being a lot simpler than I thought,
    Code (Text):
    public static void regen(List<BlockState> blocks) {
            for(final BlockState blockState : blocks){
                Bukkit.getScheduler().scheduleSyncDelayedTask(PlotGames.getInstance(), new Runnable() {
                    public void run() {
                        blockState.update(true, false);
                }, 2L);
    Well...i'm a smart guy...

    And thanks for the detailed reply @DarkSeraphim <3
    • Friendly Friendly x 1