Hashmap somehow not working?

Discussion in 'Spigot Plugin Development' started by PurrfectMistake_, Apr 17, 2017.

  1. This looks fine to me, probably something tiny that i'm overlooking, but anyway. My goal is not necessary to teleport the player to the same block on playermoveevent, i can just cancel it, but I created that method to see if the hashmap was setup properly, but it seems that the players aren't being put into the hashmap when they're frozen... Any idea why?

    Code (Text):
    package me.nikk.Fess;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.util.Vector;

    import com.connorlinfoot.bountifulapi.BountifulAPI;

    public final class Freeze
    implements Listener, CommandExecutor
    {
      public static HashMap<UUID, Boolean> map = new HashMap<UUID, Boolean>();
      Map<String, Location> locations = new HashMap<String, Location>();

      @SuppressWarnings("deprecation")
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
      {
        if (cmd.getName().equalsIgnoreCase("freeze") &&
          (sender.hasPermission("fess.freeze"))) {
          if (args.length == 0)
          {
            sender.sendMessage(Configuration.pluginprefix + ChatColor.YELLOW + " You need to define a player to freeze!");
          }
          else
          {
            Player target = Bukkit.getServer().getPlayer(args[0]);
            if (target == null)
            {
              sender.sendMessage(Configuration.pluginprefix + " " + ChatColor.DARK_RED + "Player cannot be found!");
            }
            else if(map.containsKey(target.getUniqueId()))
            {
                locations.remove(target.getName());
                map.remove(target.getUniqueId());

              sender.sendMessage(Configuration.pluginprefix + ChatColor.DARK_GREEN + target.getName() + " has been unfrozen!");
              Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "godmode " + args[0] + " off");
              target.removePotionEffect(PotionEffectType.JUMP);
              target.setWalkSpeed(0.2F);
              target.sendMessage(Configuration.pluginprefix + " " + ChatColor.GREEN + "You have been unfrozen! You are now safe to log out.");
              BountifulAPI.sendTitle(target, Configuration.fadein, Configuration.titletime, Configuration.fadeout, ChatColor.AQUA + "Unfrozen!");
              BountifulAPI.sendSubtitle(target, Configuration.fadein, Configuration.titletime, Configuration.fadeout, ChatColor.GRAY + "Thank you for cooperating.");
            }
            else
            {
                locations.put(target.getName(), target.getLocation());
                map.put(target.getUniqueId(), true);
              target.addPotionEffect(PotionEffectType.JUMP.createEffect(12000, 128));
              target.setWalkSpeed(0.0F);
              target.setFlying(false);
              target.setAllowFlight(false);
              target.getPlayer().setVelocity(new Vector().zero());
              target.sendMessage(Configuration.pluginprefix + " " + Configuration.FreezeMsg);
              Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "godmode " + args[0] + " on");
              target.sendMessage(Configuration.pluginprefix + " " + ChatColor.RED + "Do not log out, or you will be punished.");
              sender.sendMessage(Configuration.pluginprefix + " " + ChatColor.DARK_GREEN + target.getName() + " has been frozen!");
                BountifulAPI.sendTitle(target, Configuration.fadein, Configuration.titletime, Configuration.fadeout, ChatColor.RED + "Frozen!");
              BountifulAPI.sendSubtitle(target, Configuration.fadein, Configuration.titletime, Configuration.fadeout, ChatColor.GRAY + "Please check chat for instructions");
          }
        }
      }
        return false;
    }

    @EventHandler
    public void FreezeNoMove(PlayerMoveEvent e)
    {
        if(locations.containsKey(e.getPlayer().getName()))
        {

            e.getPlayer().sendMessage(ChatColor.RED + "You cannot move while frozen!");
            e.getPlayer().teleport(locations.get(e.getPlayer().getName()));
            }
    }
    }
     
     
  2. Just wanted to point out - why are you using a uuid in one map but a string in another? (Keys) also, why is map static?
     
  3. Well, I originally had an arraylist instead of the string hashmap, but it wasn't working.. nor is this one, there's something small that would stop the whole thing from working that i'm missing...... I just can't see it ;I
     
  4. Try taking final out of the class signature.
     
  5. That would do nothing. Just other classes have this class as a superclass
     
    • Agree Agree x 1
  6. ... How the hell would I do that?
     
  7. No I wasn't talking to you. Try to make both maps static
     
  8. Talking about providing solutions which don't really do anything (aside hiding possible problems)

    @PurrfectMistake_
    • Remove the static modifiers, and make the fields private (as they should be in the majority of cases)
    • Map<K, Boolean> is equivalent to Set<K> in a lot of cases (where a missing entry is equivalent to false), but it's a lot cleaner.
    • Instead of player names, use Player UUIDs or Player objects (they're faster, less likely to yield user error, etc)
    • Method names should be camelCase, not PascalCase (naming conventions)
    • Use the same instance as CommandExecutor and Listener. You're probably not doing this, which means the two instances use different Maps.
     
  9. When can somebody do this OO Implement listener and commandexecutor lawl^^