Regenerate block by players

Discussion in 'Plugin Development' started by coco5843, Nov 9, 2014.

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

    coco5843

    Hello,

    I would like regenerate some blocks by players

    I tried it :

    Code:
    private static HashMap<Player ,ArrayList<Location>> savePlayerBlock = new HashMap<Player, ArrayList<Location>>();
        private static HashMap<Block ,Byte> saveData = new HashMap<Block, Byte>();
        private static HashMap<Block ,Integer> saveID = new HashMap<Block,Integer>();
       
        @SuppressWarnings("deprecation")
        public static void setBlockToSaveAndRestore(Player p, Location l, Material newMaterial, byte newData) {
            Block b = l.getBlock();
            savePlayerBlock.put(p, new ArrayList<Location>());
           
            saveID.put(b, b.getTypeId());
            saveData.put(b, b.getData());
            ((ArrayList<Location>)savePlayerBlock.get(p)).add(l);
            b.setType(newMaterial);
            b.setData(newData);
            UtilParticleCrack.displayBlockCrack(l, b.getType().getId(), (byte) b.getData(), 1, 1, 1, 10);
            p.playSound(l, Sound.DIG_GRASS, 1, 1);
     
        }
     
        @SuppressWarnings("deprecation")
        public static void restoreSavedBlock(Player p) {
     
            for(Location l : savePlayerBlock.get(p)) {
     
                int i = saveID.get(l.getBlock().getTypeId());
                int d = saveData.get(l.getBlock().getData());
               
                UtilParticleCrack.displayBlockCrack(l, i, (byte) d, 1, 1, 1, 10);
                p.playSound(l, Sound.DIG_GRASS, 1, 1);
           
            }
     
        }
    But my code is bad :/
    I don't want to use this library , because I can't regenerate block when I want ..
    https://forums.bukki...-blocks.209765/
    Thank's for your help
     
  2. Offline

    mine-care

    Ok, first of all.. Remove that static everywhere! Don't abuse it!
    Secondly to give you an idea, keep track of block breaks and put the block state in a arraylist then if you want to restore it youll have to loop through the array list and use one of blockstate's regeneration methods.
     
  3. Offline

    Concurrent

    coco5843 blockstate.update(false, false); updates the block state just store the blockstate in an list then iterate and update it.
     
  4. Offline

    coco5843

    Thank's ! but
    how can avoid static method ?
     
  5. Offline

    Concurrent

    coco5843 create instance of your main class and initiate via constructor.
     
  6. Offline

    coco5843

    An example please ? :)
     
  7. Offline

    Concurrent

    Code:
    MyMainClass myMainClass;
    public MyCurrentClass(MyMainClass myMainClass){
      this.myMainClass = myMainClass;
    }
    // now you can use this variable without npe
    happy coding :)
     
  8. Offline

    MajorSkillage

    Pretty much what i would do is BlockBreakEvent whoever breaks any block get the block that they broke and save that data so it is restored when a command or whatever you want regenerated and on BlockPlaceEvent do the same thing same for Explosion if you want, if you really want to get advanced learn WorldEdit's api and store data on commands
     
  9. Offline

    teej107

    Learn actual OOP.
     
    mine-care likes this.
  10. Offline

    mine-care

  11. Offline

    fireblast709

    Actually you want to use update(true).
     
    Concurrent likes this.
  12. Offline

    coco5843

    I can't check if the blocks belong to a player with your method ..


    mine-care thank's ! I will learn it .

    Concurrent thank you I tried it:
    Code:
    public class UtilInventory {
     
        SpaceBattle spaceBattle;
        private Player player;
        public UtilInventory(SpaceBattle spaceBattle, Player player){
          this.spaceBattle = spaceBattle;
          this.player = player;
        }
     
     
        public void clearArmor() {
            this.player.getInventory().setHelmet(null);
            this.player.getInventory().setChestplate(null);
            this.player.getInventory().setLeggings(null);
            this.player.getInventory().setBoots(null);
        }
     
    }
    
    but how can I tell the method in other class ?

    Okay I found:
    UtilInventory inv = new UtilInventory(this , p);
    inv.clearArmor();
     
Thread Status:
Not open for further replies.

Share This Page