Solved Boolean toggle turns on onPlayerMoveEvent

Discussion in 'Plugin Development' started by Creeperzombi3, Jun 28, 2015.

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

    Creeperzombi3

    So whenever I reload the server, it will show as false, but whenever someone moves, it turns to true.
    How do I fix it so it doesn't become true when someone moves?

    Code:
    public class EggWool extends JavaPlugin implements Listener {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static EggWool plugin;
        public final HashMap<UUID, ArrayList<Block>> hashMap = new HashMap<UUID, ArrayList<Block>>();
        public static boolean isHatching = false;
        public boolean walk = false;
        public boolean eggT = false;
    
        @Override
        public void onEnable() {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this, this);
            walk = false; // I know these two are not needed, they are here for the reason in the title
            eggT = false;
        }
    
        @EventHandler
        @SuppressWarnings("deprecation")
        public void onProjectileHit(ProjectileHitEvent event) {
            Player player = (Player) event.getEntity().getShooter();
            EntityType egg = event.getEntity().getType();
            Entity eggg = event.getEntity();
    
            BlockIterator iterator = new BlockIterator(eggg.getWorld(), eggg
                    .getLocation().toVector(), eggg.getVelocity().normalize(), 0, 4);
            Block hitBlock = null;
    
            Random random = new Random();
            byte wool = (byte) random.nextInt(16);
    
            while (iterator.hasNext()) {
                hitBlock = iterator.next();
                if (hitBlock.getTypeId() != 7) {
                    break;
                }
            }
            Block hitBlock1 = iterator.next();
            int block = hitBlock1.getTypeId();
            byte blockType = hitBlock1.getData();
            if (player.hasPermission("eggwool.throw")) {
                if (eggT = true) {
                    if (egg == EntityType.EGG) {
                        if (block != 7 && block != 0 && block != 35) {
                            hitBlock1.setTypeIdAndData(35, wool, true);
                            Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                        public void run() {
                                            hitBlock1.setTypeIdAndData(block, blockType, true);
                                        }
                                    }, 60);
                        } else {
                            player.sendMessage(ChatColor.RED + "[" + ChatColor.BLUE + "EggWool" + ChatColor.RED + "] "
                                    + "You are not allowed to change Bedrock!");
                        }
                    }
                }
            }
        }
    
        @EventHandler
        public void onCreatureSpawn(CreatureSpawnEvent event) {
            if (event.getSpawnReason() == SpawnReason.EGG) {
                event.setCancelled(true);
            }
        }
    
        @EventHandler
        @SuppressWarnings("deprecation")
        public void onPlayerMove(PlayerMoveEvent event) {
            Player player = event.getPlayer();
            Location location = player.getLocation();
            Block block = location.subtract(0, 1, 0).getBlock();
            byte blockType = block.getData();
            int blockId = block.getTypeId();
            Random random = new Random();
            byte wool = (byte) random.nextInt(16);
            if (player.hasPermission("eggwool.walk")) {
                if (walk = true) {
                    if (blockId != 7 && blockId != 0 && blockId != 35) {
                        block.setTypeIdAndData(35, wool, true);
                        Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                    public void run() {
                                        block.setTypeIdAndData(blockId, blockType, true);
                                    }
                                }, 60);
                    }
                }
            }
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                if (label.equalsIgnoreCase("walkwool")) {
                    if(args.length == 1) {
                        if (args[0].equalsIgnoreCase("on")) {
                            walk = true;
                            player.sendMessage(ChatColor.RED + "[" + ChatColor.BLUE + "EggWool" + ChatColor.RED + "] "
                                + ChatColor.AQUA + "Blocks you walk on will now turn into Wool for a couple seconds!");
                        } else if (args[0].equalsIgnoreCase("off")) {
                            walk = false;
                            player.sendMessage(ChatColor.RED + "[" + ChatColor.BLUE + "EggWool" + ChatColor.RED + "] "
                                + ChatColor.AQUA + "Blocks you walk on will no longer turn into Wool.");
                        } else if (args[0].equalsIgnoreCase("toggle")) {
                            player.sendMessage(ChatColor.RED + "[" + ChatColor.BLUE + "EggWool" + ChatColor.RED + "] "
                                + ChatColor.DARK_PURPLE + "EggWalk is currently on: " + ChatColor.GREEN + walk);
                        } else {
                            player.sendMessage(ChatColor.RED + "[" + ChatColor.BLUE + "EggWool" + ChatColor.RED + "] "
                                + ChatColor.RED + "Usage: " + ChatColor.GREEN + "/walkwool on/off");
                        }
                    }
                } else if (label.equalsIgnoreCase("eggwool")) {
                    if (args.length == 1) {
                        if (args[0].equalsIgnoreCase("on")) {
                            eggT = true;
                            player.sendMessage(ChatColor.RED + "[" + ChatColor.BLUE + "EggWool" + ChatColor.RED + "] "
                                    + ChatColor.AQUA + "Blocks you throw an egg at will now turn into Wool for a couple seconds!");
                        } else if (args[0].equalsIgnoreCase("off")) {
                            eggT = false;
                            player.sendMessage(ChatColor.RED + "[" + ChatColor.BLUE + "EggWool" + ChatColor.RED + "] "
                                    + ChatColor.AQUA + "Blocks you throw an egg at will no longer turn into Wool.");
                        } else if (args[0].equalsIgnoreCase("toggle")) {
                            player.sendMessage(ChatColor.RED + "[" + ChatColor.BLUE + "EggWool" + ChatColor.RED + "] "
                                    + ChatColor.DARK_PURPLE + "EggThrow is currently on: " + ChatColor.GREEN + eggT);
                        } else {
                            player.sendMessage(ChatColor.RED + "[" + ChatColor.BLUE + "EggWool" + ChatColor.RED + "] "
                                    + ChatColor.RED + "Usage: " + ChatColor.GREEN + "/eggthrow on/off/toggle");
                        }
                    } else {
                        player.sendMessage(ChatColor.RED + "[" + ChatColor.BLUE + "EggWool" + ChatColor.RED + "] "
                            + ChatColor.RED + "Usage: " + ChatColor.GREEN + "/eggwool on/off/toggle");
                    }
                }
            }
            return false;
        }
    }
     
  2. Offline

    meguy26

    @Creeperzombi3
    The problem is at the following statement:
    Code:
     if (walk = true)  //line 77 in your above code
    You also have this problem on line 41...
    Oddly enough, in any other if statement its just fine, allow me to explain:

    "=" assigns a variable, so "walk = true," instead of checking if walk is true, makes walk true, because you are setting walk equal to true. Same thing on line 41. So instead do:
    Code:
    if(walk == true) //kind of a bad practice, but will work
    
    if(walk) //best practice
     
  3. Offline

    Creeperzombi3

    Thanks that fixed it, but what do you mean by:
    1. if(walk) //best practice
    What is that testing?
     
  4. Really, if (walk == true) and if(walk) is basically the same thing. An if statement only runs if whats inside the parenthesis is true.

    Both if statements mean, if walk is equal to true, run the code inside me. But doing walk == true is unnecessary.
     
  5. Offline

    Creeperzombi3

    Ok thanks for explaining
     
  6. Offline

    tytwining

    That's a confusing explanation. Not sure if @Creeperzombi3 understood it or just didn't care that much.

    Also, @Creeperzombi3 please mark this as solved.
     
  7. I'm not that good at explaining things :D Oh well
     
  8. Offline

    Creeperzombi3

    If you have a better explanation I would love it, but for now, I understood it for all I need
     
  9. Offline

    tytwining

    Using ==, >=, !=, etc basically turns what you put in there for a boolean:

    Code:
    if(1 >= 0) {}
    
    //Java basically interprets it as
    
    if(true) {}
    //because it is in fact true. (and will always be true)
    
    if(1 == 2) {}
    //is the same as
    if(false) {}
    //because 1 clearly does not equal 2.
    
    //Often times you just use variables instead of constants.
    Because the variable "walk" is already a boolean, if you just put it in an if statement it will be able to tell if it's true or false because of the way java works. You can also do this if you want the opposite:

    Code:
    if(!walk) {}
    So, in conclusion, equals(), ==, >=, !=, etc basically is just something used to turn two objects (that are not booleans) into a boolean by comparing the two. Hopefully this was a better explanation, and if not then just ask what's confusing. :)
     
  10. Offline

    Creeperzombi3

    You confused me a bit at first, but I did some testing around with it and I got it, Thanks for the explanation.
    In my mind, you just forgot to add the part where if an IF statement is false, it won't continue.
     
Thread Status:
Not open for further replies.

Share This Page