[Urgent] Why doesn't this toggle work?

Discussion in 'Plugin Development' started by creepers84, Apr 22, 2013.

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

    creepers84

    I have this code:
    Code:
    package me.creepers84.projectilewither;
     
    import java.util.HashMap;
    import java.util.logging.Logger;
     
    import org.bukkit.entity.Player;
    import org.bukkit.entity.WitherSkull;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityShootBowEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class ProjectileWither extends JavaPlugin implements Listener {
    public final Logger logger = Logger.getLogger("Minecraft");
    public static ProjectileWither plugin;
     
    public static HashMap<Player, String> toggle = new HashMap<Player, String>();
     
     
    @Override
    public void onDisable() {
    this.logger.info("[ProjectileWither] Plugin Disabled");
    }
     
    @Override
    public void onEnable() {
    this.logger.info("[ProjectileWither] Plugin Enabled");
    getServer().getPluginManager().registerEvents(this, this);
    }
     
     
    @EventHandler
    public void onShoot(EntityShootBowEvent e){
    if (e.getEntity() instanceof Player) {
    Player p = (Player) e.getEntity();
    if (toggle.get(p) == "off") return;
    if(p.hasPermission("Projectile.Wither")){
    e.setCancelled(true);
    p.launchProjectile(WitherSkull.class).setVelocity(e.getProjectile().getVelocity());
    }
    }
    }
    @EventHandler
    public void onPlayerInteractEvent(PlayerInteractEvent event){
    Player p = event.getPlayer();
    if (!event.getAction().equals(Action.LEFT_CLICK_AIR)) return;
    if (toggle.get(p) == "on"){
    toggle.remove(p);
    toggle.put(p, "off");
    }else if (toggle.get(p) == "off"){
    toggle.remove(p);
    toggle.put(p, "on");
    }
     
    }
     
    }
    I want to make it so that people with permission left click to toggle firing wither skulls on and then if they click again it will toggle it back to firing arrows etc..... How come this doesn't work. Is there anything wrong?
     
  2. Don't store Player objects, use player's name.

    And you're using equals() and '==' totally wrong... you use '==' on enums (like Action.LEFT_CLICK_AIR) properly but it won't work properly on Strings (like "off") and other objects... you should look at the Java documentation the diference between '==' and equals().

    You should just use equals() always when comparing stuff unless it's a primitive (int).
     
  3. Offline

    creepers84

    Can you amend the code above?
     
  4. Offline

    Tirelessly

  5. Offline

    creepers84

    Yes but I can not correctly amend my own code with everything Digi has recommended. If he amends it. Then I will be able to learn from it =D.
     
  6. creepers84
    ...

    HashMap<Player, String> --> HashMap<String, String>

    toggle.get/put/remove/etc(player) --> toggle.*(player.getName())

    toggle.get(player.getName()) == "string" --> toggle.get(player.getName()).equals("string")


    I'm really not sure why that is so hard... expecially since you've made alot of plugins so far.
     
  7. Can't help but wonder how...
     
  8. Offline

    Hoolean

    creepers84

    For the sake of optimisation I recommend using a HashSet (like a list but doesn't allow duplicates) instead of a HashMap.

    If the player has it enabled they are put in the set and if they disable it they are taken out of the set. :)

    Also, you should use the player's name (a String) instead of the Player object when using Lists or HashMaps, as using the Player object will cause problems :)
     
    breezeyboy likes this.
  9. using the Player object will cause problems
    Fixed that for you.
     
    MrBluebear3 likes this.
  10. Offline

    Hoolean

    Changed. What I really meant was, whilst it will not to seem to cause problems (from Eclipse, etc) when run will break, and generally break silently :)
     
  11. Right :p
     
  12. Offline

    creepers84

    All of my plugins are relatively simple. Since I have only recently started java Programming. Gimme a Frikkin Break!
     
Thread Status:
Not open for further replies.

Share This Page