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; }
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
Naw, line 20 has no issue I think, its just that line 25 does nothing. have tried a couple of times but no luck.
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
It's no big deal, but the way to get better at programming is figuring things out on your own 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.
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.
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.
@WPM loc.setY(loc.getY() + 3); that doesn't surround the player? That only sets a block over their head?