switching through spells

Discussion in 'Spigot Plugin Development' started by rhunter, May 13, 2015.

  1. im making a plugin where you have a blaze rod as your wand and you can right click to scroll through the spells then left click to use them. I only have 2 spells right now, lightning and fire, so I just have wandFire either equal true or false, depending on if the fire spell is selected or not. Anyone know why my code won't work?
    Code (Text):

    ////////////////////////////////FIRE WAND/////////////////////////

    booleanwandFire = true;

    @EventHandler

    public void blazerodUse(PlayerInteractEvent event){

    Player player = event.getPlayer();

    if(player.hasPermission("wands.mage")){

    event.getPlayer().setMaxHealth(20.0);

    if(player.getItemInHand().getType() == Material.BLAZE_ROD){

    if(event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK){

    if(wandFire = true){

    player.launchProjectile(SmallFireball.class);

    }else{

    List<Entity> near = player.getNearbyEntities(50.0D, 50.0D, 50.0D);

    for(Entity entity : near) {

        if(entity instanceof Player) {

            Player nearPlayer = (Player) entity;

            World world = player.getWorld();

            world.strikeLightning((Location) nearPlayer);

        }

    }


    }

    }

    if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){

    if(wandFire = true){

    wandFire = false;

    player.sendMessage(ChatColor.GOLD+"Lightning Wand");

    }

    if(wandFire = false){

    wandFire = true;

    player.sendMessage(ChatColor.GOLD+"Fire Wand");

    }

    }

    }

    }

    }
     
     
  2. Reformatted to be readable ..

    Code (Java):

    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.SmallFireball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;

    public class SpigotHelperClass implements Listener
    {
        boolean wandFire = false;
       
        @EventHandler
        public void blazerodUse(PlayerInteractEvent event)
        {
            Player player = event.getPlayer();
            if(player.hasPermission("wands.mage"))
            {
                event.getPlayer().setMaxHealth(20.0);
       
                if(player.getItemInHand().getType() == Material.BLAZE_ROD)
                {
                    if(event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK)
                    {
                        if(wandFire = true)
                        {
                            player.launchProjectile(SmallFireball.class);
                        }
                        else
                        {
                            List<Entity> near = player.getNearbyEntities(50.0D, 50.0D, 50.0D);
                            for(Entity entity : near)
                            {
                                if(entity instanceof Player)
                                {
                                    Player nearPlayer = (Player) entity;
                                    World world = player.getWorld();
                                    world.strikeLightning((Location) nearPlayer);
                                }
                            }
                        }
                    }
                    if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
                    {
                        if(wandFire = true)
                        {
                            wandFire = false;
                            player.sendMessage(ChatColor.GOLD+"Lightning Wand");
                        }
                        if(wandFire = false)
                        {
                            wandFire = true;
                            player.sendMessage(ChatColor.GOLD+"Fire Wand");
                        }
                    }
                }
            }
        }
    }
     
     
  3. what specifically about the code isn't working? is there a specific case where it's not working? or does it just not happen at all?
     
  4. Code (Text):
    if(wandFire = true)
    This does not do what you think it does. This assigns the value true to the variable wand fire, and then it checks this new value, which now obviously is true. Long story short, this if-test will never, under any circumstances, fail.

    You probably wanted to put two equality signs there, and by mistake ended up putting one. This is why we say you shouldn't test boolean values using the == operator. If-statements expect a boolean value, so just use the value itself.
     
    • Informative Informative x 1
  5. DUH! Can't believe I missed those conditionals ..

    To piggyback on his explanation, here's an example of what he means ..

    Code (Java):

    boolean myBool = false;

    if(myBool)
    {
    //This will not run
    }
    else if(!myBool)
    {
    //This will run
    }
    else
    {
    //This will not run
    }