Solved Freezing help

Discussion in 'Plugin Development' started by WPM, Jul 25, 2015.

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

    WPM

    I am trying to make a freeze test plugin without using any events, just by spawning blocks around the the player so they cant move, the thing that doesnt work is that when the player is not in the arraylist, the blocks spawned do not get removed. I know there is something wrong in the code but I cant get to it. Any help without spoonfeeding will be great :)

    Code:
        ArrayList<Player> freeze = new ArrayList<Player>();
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(cmd.getName().equalsIgnoreCase("freeze")){
                if(args.length == 0){
                    sender.sendMessage(ChatColor.RED + "Please specify a player");
                    return true;
                }
                Player target = Bukkit.getPlayer(args[0]);
                if(target == null){
                    sender.sendMessage(ChatColor.RED + args[0] + " is not online!");
                    return true;
                }
                if(!freeze.contains(target)){
                    freeze.add(target);
                }else{
                    freeze.remove(target);
                    return false;
                }
                if(freeze.contains(target)){
                    Location loc = target.getLocation();
                    loc.setY(loc.getY() + 3);
                    Block block = loc.getBlock();
                    block.setType(Material.GLASS);
                    if(!freeze.contains(target)){
                        block.setType(Material.AIR);
                    }
                }
            }
            return false;
        }
     
  2. Offline

    Asc_Nicholas

    Look at the logic of your last if statement (line 20) I'm pretty certain that is where the issue lies. If you look at the code and cannot figure it out post back here and I'll try to explain to you why I THINK that if statement block is causing the problem
     
  3. Offline

    WPM

    Naw, line 20 has no issue I think, its just that line 25 does nothing. have tried a couple of times but no luck.
     
  4. Offline

    Asc_Nicholas

    That's the thing, you have line 25 inside of the if statement that only gets executed if the player exists in the array list. In the logic of that code the target will never not exist in the array list, so line 25 never gets executed. Think about what you have to do to change that from happening
     
  5. Offline

    WPM

    I feel retarded because I cant squash it :(
     
  6. Offline

    Asc_Nicholas

    It's no big deal, but the way to get better at programming is figuring things out on your own :p Here try this:

    Code:
        ArrayList<Player> freeze = new ArrayList<Player>();
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(cmd.getName().equalsIgnoreCase("freeze")){
                if(args.length == 0){
                    sender.sendMessage(ChatColor.RED + "Please specify a player");
                    return true;
                }
                Player target = Bukkit.getPlayer(args[0]);
                if(target == null){
                    sender.sendMessage(ChatColor.RED + args[0] + " is not online!");
                    return true;
                }
                if(!freeze.contains(target)){
                    freeze.add(target);
                }else{
                    freeze.remove(target);
                    return false;
                }
                if(freeze.contains(target)){
                    Location loc = target.getLocation();
                    loc.setY(loc.getY() + 3);
                    Block block = loc.getBlock();
                    block.setType(Material.GLASS);
                    } else {
                        //Do code that removes glass / sets air around the player 
                    }
                }
            }
            return false;
        }
    You needed to take the if statement out of the if statement and make an else statement so that the code would be executed if the player does not exist inside the array list.

    If that doesn't work let me know.
     
  7. Offline

    WPM

    @Asc_Nicholas
    That wont work because 'block' wont be resolved.
     
  8. Offline

    Asc_Nicholas

    @WPM

    Can you show me exactly what you typed?
     
  9. Offline

    WPM

    'block' in the else statement shows up red because its not resolved.
     
  10. Offline

    Asc_Nicholas

    That's because block was defined locally inside the if statement either you'll have define block to air type in the else statement or you'll have to make it so both the if statement AND the else statement can access it.
     
  11. Offline

    WPM

    Yes I know I realized that. I am a bit sleepy and cant focus well sometimes.

    Here is what I ended up with

    Still doesnt work
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(cmd.getName().equalsIgnoreCase("freeze")){
                if(args.length == 0){
                    sender.sendMessage(ChatColor.RED + "Please specify a player");
                    return true;
                }
                Player target = Bukkit.getPlayer(args[0]);
                if(target == null){
                    sender.sendMessage(ChatColor.RED + args[0] + " is not online!");
                    return true;
                }
                if(!freeze.contains(target)){
                    freeze.add(target);
                }else{
                    freeze.remove(target);
                    return false;
                }
                if(freeze.contains(target)){
                    Location loc = target.getLocation();
                    loc.setY(loc.getY() + 3);
                    Block block = loc.getBlock();
                    block.setType(Material.GLASS);
                }else{   
                    if(!freeze.contains(target)){
                    Location loc = target.getLocation();
                    loc.setY(loc.getY() + 3);
                    Block block = loc.getBlock();
                    block.setType(Material.AIR);
                    }
                }
            }
            return false;
        }
    I got it to work after experimenting and of course with your help :) Thank you kind sir :>

    EDIT: I decided to put why it did not work. I removed the 'return false;' after 'freeze.remove(target);'

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  12. Offline

    Monollyth

    Why spawn blocks around the player when you can just prevent the player from moving?
     
  13. Offline

    WPM

    Because I dont want to cancel the players move event. Duh
     
  14. Offline

    DoggyCode™

    @WPM
    loc.setY(loc.getY() + 3);

    that doesn't surround the player? That only sets a block over their head?
     
  15. Offline

    WPM

    Yes I know. I was just using that as a test.
     
Thread Status:
Not open for further replies.

Share This Page