Previous block delete help.

Discussion in 'Plugin Development' started by arcticmunkii, Mar 12, 2014.

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

    arcticmunkii

    Hi again guys so far I have created a tiny plugin which basically spawns a block underneath you where you go (so you can clib heights etc), however the only problem is I'm at bit stuck when it comes to removing the previous block ( the x y and z seems to confuse me).

    This is my code so far
    Code:
        @EventHandler
        public void carpet(PlayerMoveEvent event){
            Player player = event.getPlayer();
            Location loc = player.getLocation();
     
            player.setWalkSpeed(0.4F);
            loc.setY(loc.getY() - 1);
            Block block = player.getWorld().getBlockAt((int) loc.getX(), (int) loc.getY(), (int) loc.getZ());
            Block blockDel = player.getWorld().getBlockAt((int) loc.getX() + 1, (int) loc.getY(), (int) loc.getZ() + 1);
     
     
            if (block.getType() == Material.AIR){
            player.getWorld().getBlockAt(loc).setType(Material.GLASS);
            }
     
            /*if (blockDel.getType() == Material.GLASS){
                player.getWorld().getBlockAt((int) loc.getX(), (int) loc.getY(), (int) loc.getZ()).setType(Material.AIR);
       
                }*/
     
     
     
     
     
     
        }
    The comment out paragraph is where I need pointers, I want to do it myself :p I just need a poin in the right direction.

    Thanks

    - Arctic
     
  2. Offline

    tamajpm

    arcticmunkii

    For the location substraction to get the block underneath the player you can just do:
    Block block = player.getLocation().substract(0, 1, 0).getBlock();
     
  3. Offline

    arcticmunkii

    @tamajpmThanks for you reply, I don't want to delete the block underneath the player's Current location, I want to delete the block under the location he just came from.

    Any clues?

    Thanks again

    - Arctic
     
  4. arcticmunkii You could've just asked for help on your last thread. :) I, again, recommend that you don't use PlayerMoveEvent for this, but if you insist:

    Make a HashMap which stores a String and a Location. In the event, first check if this hashmapcontains the player's name. If it does, get the location from the hashmap and set it to air. Then, after you set the air to grass, add the player's name and the location of the block that you changed to the hashmap.
     
  5. Offline

    arcticmunkii

    AdamQpzm Thanks for your reply, I'm still not sure how else to do it other than using the PlayerMoveEvent, I know you said by using Scheduled programming however I want the block to be there EVERY time he moves to create a platform effect.

    Do you mind giving me an example?

    Thanks

    - Arctic
     
  6. Offline

    Borlea

    To get location the guy was last at. do e.getFrom()
    Block blockDel = player.getWorld().getBlockAt(e.getFrom().add(0, -1, 0);
    This should work, untested. it gets last location, then minus one to get block under him.
     
  7. Offline

    Amgis

    AdamQpzm

    Although PlayerMoveEvent can be CPU-intensive with a large amount of players, I believe it to be the most reliable method that would guarantee that the old carpet would be deleted, as a PlayerMoveEvent provides both a fromLocation and toLocation, thus allowing one to simultaneously know where the old carpet was and where the new carpet should be all in one method.

    Code:
    static int carpetLength;
     
    private void fill(Location location) {
     
        Block block;
       
        for(int x = -(carpetLength/2); x<carpetLength/2 + 1;x++) {
           
            for(int z = -(carpetLength/2); z<carpetLength/2 + 1;z++) {
               
                block = world.getBlockAt((int) (location.getX() + x),
                        (int) location.getY() - 1,
                        (int) (location.getZ() + z));
               
                if(block.getType().equals(Material.AIR)) {
                   
                    block.setType(Material.GLASS);   
                }
            }
        }
    }
     
    private void delete(Location location) {
       
        Block block;
       
        for(int x = -(carpetLength/2); x<carpetLength/2 + 1;x++) {
           
            for(int z = -(carpetLength/2); z<carpetLength/2 + 1;z++) {
               
                block = world.getBlockAt((int) (location.getX() + x),
                        (int) location.getY() - 1,
                        (int) (location.getZ() + z));
               
                if(block.getType() == Material.GLASS) {
                   
                    block.setType(Material.AIR);   
                }
            }
        }
    }
     
    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
     
        if(event.getPlayer().equals(this.getPlayer())) {
     
            this.delete(event.getFrom());
                    // would delete old carpet.
           
            this.fill(event.getTo());
                    // would draw new carpet.
        }
    }
     
  8. arcticmunkii Eh, sorry, I don't really have time for an example of a task to do it right now. I'll just use PlayerMoveEvent and warn you that it gets fired a lot! (turning counts as moving)

    PHP:
    private static HashMap<StringLocationblocks = new HashMap<StringLocation>;
     
    @
    EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
        
    Player p event.getPlayer();
        
    Location loc p.getLocation().subtract(0,1,0);
        if(
    blocks.containsKey(p.getName()) {
            
    block.get(p.getName()).getBlock().setType(Material.AIR);
        }
        
    loc.getBlock().setType(Material.GRASS);
        
    blocks.put(p.getName(), loc);
    }
     
  9. Offline

    Amgis

    arcticmunkii

    I think I see what you are saying. You want to "slow down" the PlayerMoveEvent such that it doesn't fire as much?

    In which case, the code would be a bit messier, and I'm not sure how reliable it will be-- but it will definitely involve less method calls.

    I'm not sure where to begin on this. I have no idea how you can 'catch' the old carpet and delete it using Bukkit's Scheduler.
    Code:java
    1. public void scheduleRepeatingTask() {
    2.  
    3. Plugin yourPlugin;
    4.  
    5. Bukkit.getScheduler().scheduleSyncRepeatingTask(yourPlugin, new Runnable() {
    6.  
    7. @Override
    8. public void run() {
    9.  
    10. // don't know how to get old carpet
    11. }
    12.  
    13.  
    14. }, 2, 0);
    15. }
     
  10. Offline

    arcticmunkii

    Amgis AdamQpzm Thanks a lot for your help guys I'm going to go and have a play around with everything now, one more thing before I go I don't suppose you could tell me how it would work if you used scheduled programming instead?

    - Arctic
     
  11. Offline

    tamajpm

    arcticmunkii

    The delayed schedular is just a timer. So after 20 ticks (20 ticks = 1 second) you can do something. Or how many thinks you want. If you want 5 seconds you could just to 20 * 5. The code you want to run after that 5 seconds or something you can put into the run method in the schedular itself.
     
  12. Offline

    Amgis

    arcticmunkii

    To get started on scheduled programming, see my prototype scheduler function above. Hypothetically, you would insert code inside run() to both delete the old carpet and place a new carpet.

    If you wish to use the scheduler over PlayerMoveEvent, I would highly suggest using a Carpet class that stores each individual block of the carpet. That way, you have a reference to the old carpet, where you could simply call some sort of delete() function that would delete the old glass and redraw() a new carpet. Either way, I would definitely delete the entire old carpet, and redraw the entire new carpet. Don't try to guess which blocks to delete.

    I would store the blocks in the carpet as a 2D array of blocks, where the center block is the block that is directly under the player. Every time redraw() is called, this 2D array of blocks is updated such that the center block is the block directly under the player.
    Code:java
    1. Block[][] blocks;
     
Thread Status:
Not open for further replies.

Share This Page