Player can still move while frozen?

Discussion in 'Plugin Development' started by football70500, May 14, 2015.

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

    football70500

    Code:java
    1.  
    2. public class Main extends JavaPlugin implements Listener{
    3. public ArrayList<String> freeze = new ArrayList<String>();
    4.  
    5. @Override
    6. public void onEnable(){
    7. getLogger().info("cFreeze is now enabled!");
    8. }
    9.  
    10. @Override
    11. public void onDisable(){
    12. getLogger().info("cFreeze has been disabled!");
    13. }
    14.  
    15. @EventHandler
    16. public void onPlayerMove(PlayerMoveEvent e){
    17. if(freeze.contains(e.getPlayer().getName())){
    18. e.getPlayer().teleport(e.getPlayer());
    19. e.getPlayer().sendMessage(ChatColor.BLUE + "You Are Frozen!");
    20. }else{
    21.  
    22. }
    23. }
    24.  
    25. @EventHandler
    26. public void onLogout(PlayerQuitEvent e){
    27. Player p = e.getPlayer();
    28. if(freeze.contains(e.getPlayer())){
    29. for (Player online : Bukkit.getOnlinePlayers()) {
    30. if(online.hasPermission("freeze.seelogger")){
    31. online.sendMessage(ChatColor.RED + p.getName() + " has logged while frozen!");
    32. online.sendMessage(ChatColor.RED + p.getName() + " has logged while frozen!");
    33. online.sendMessage(ChatColor.RED + p.getName() + " has logged while frozen!");
    34. online.sendMessage(ChatColor.RED + p.getName() + " has logged while frozen!");
    35. online.sendMessage(ChatColor.RED + p.getName() + " has logged while frozen!");
    36. online.sendMessage(ChatColor.RED + p.getName() + " has logged while frozen!");
    37. }
    38. }
    39.  
    40. }
    41. }
    42.  
    43. public boolean onCommand(CommandSender sender, Command cmd, String label,
    44. String[] args) {
    45. Player target;
    46. if(cmd.getName().equalsIgnoreCase("freeze")){
    47. if(sender.hasPermission("cfreeze.freeze")){
    48. if(args.length < 1){
    49. sender.sendMessage(ChatColor.RED + "/Freeze <Name>");
    50. } else {
    51. target = Bukkit.getServer().getPlayer(args[0]);
    52. if(target != null){
    53. if(!freeze.contains(target.getName())){
    54. freeze.add(target.getName());
    55. target.sendMessage(ChatColor.RED + "You have been frozen! If you log out, you will be banned!");
    56. sender.sendMessage(ChatColor.GREEN + target.getName() + " has been frozen!");
    57. } else if(freeze.contains(target.getName())){
    58. freeze.remove(target.getName());
    59. sender.sendMessage(ChatColor.GREEN + target.getName() + " has been unfrozen!");
    60. target.sendMessage(ChatColor.RED + "You have been unfrozen!");
    61. }
    62. }
    63. }
    64. }
    65. }
    66. return false;
    67. }
    68.  
    69. }
    70.  
    71.  

    I don't get why this is happening... it just makes it so that they teleport to eachother but even cancelling the move event does nothing. When a player logs out, it doesn't spam me with the messages like it should either... whats going on?
     
  2. Offline

    meguy26

    @football70500 You are not registering events!
    Code:
    this.getPlugin().getServer().getPluginManager().registerEvents(this, this); // in on on enable
     
  3. Offline

    football70500

    @meguy26 DUH! one more question, how come it isn't sending the message when the player frozen logs out to the people with the perms.
     
  4. Offline

    meguy26

    @football70500
    *shrugs*
    Anyway, you should be using UUID lists instead of player lists, if you use player lists and a player logs out, your list keeps them in the memory, and can slow the server...
    :)
    EDIT: never mind, I need to start reading things before I say something...
     
  5. Offline

    Agentleader1

    At best, you might want to make a test if they are on ground, because freezing someone in mid-air or in water can get glitchy. Especially if you have the NoCheatPlus plugin, it'll kick them. Make sure to register the listener events.

    Adding on to what others said;
    Also for the PlayerMoveEvent, it's more reliable if you do this:
    Code:
    @EventHandler
          public void onPlayerMove(PlayerMoveEvent e){
            if(freeze.contains(e.getPlayer().getName())){
                    e.setCancelled(true); //less work.
                    e.getPlayer().sendMessage(ChatColor.BLUE + "You Are Frozen!");
            }else{
                   return;
            }
     
  6. Offline

    meguy26

    @football70500
    I figured out why it is not sending message, in PlayerQuitEvent you are checking if(freeze.contains(ev.getPlayer())) But you need to check the name, if(freeze.contains(ev.getPlayer().getName()))
     
  7. Offline

    football70500

    Thanks for that! I want to make it now so that if a player is frozen they can't take pvp
    damage, but i can't figure out how to check if my entity is a player that is in the list...

    Code:java
    1.  
    2. @EventHandler
    3. public void onDamageTake(EntityDamageEvent e) {
    4. Entity p = e.getEntity();
    5. if (p instanceof Player) {
    6. if (freeze.contains(p)) {
    7. if (e.getCause() == DamageCause.CONTACT) {
    8. e.setCancelled(true);
    9. }
    10. }
    11. }
    12.  
    13. }
     
    Last edited: May 15, 2015
  8. @football70500 Please learn the basics of Java. Bukkit is written in Java, so you cannot make plugins without first learning Java. Trying to do so will just result in problems. I recommend following the Oracle tutorials or a Java book.
     
  9. Offline

    meguy26

    @football70500
    Same problem as earlier:

     
  10. Offline

    football70500

    But i can't return .getName() because its an entity, not a player
     
  11. Offline

    meguy26

    @football70500
    cast it to a player:
    Code:
    Entity someEntity = ignorethispart;
    if(someEntity instanceof Player){
        Player player = (Player) someEntity; //we know the entity is a player so we can tell java that by casting.
        if(freeze.contains(player.getName())){
            //do stuff
        }
    }
     
  12. @football70500 No need to be rude - my advice is genuine :) I'm not claiming to be perfect, claiming that you need to be perfect, or claiming that you know no Java at all. I'm simply stating that you do not know enough Java to be making plugins. It's quite clear from the fact that you didn't realise you needed a cast, and that you made the exact same mistake twice - a contains check that couldn't possibly be true.

    Trust me, I've been doing this long enough to be able to spot these telltale signs that someone needs more Java knowledge - my advice is not meant to be taken as an insult. Please follow my advice and you will be a much better at making plugins :)
     
  13. Offline

    football70500

    @meguy26 i forgot to comment, i realized i had to do that, thanks.
     
  14. Offline

    meguy26

    @AdamQpzm
    hmm.. well, I have been doing this long enough to realize that some people learn their own way, and that the best way to infuriate, discourage, and disappoint them is to tell the they do not know enough. I for one am a bit fast on the uptake, but no one, not even me or you, can learn all of Java and then ask questions about why there code does not work; only perfect developers can do that therefore everyone who creates threads in this section does not know everything about Java. I learned what static meant last week... but I am relatively adept at Java, we all learn different things, at different paces, in different ways, and often we accidentally skip over certain things such as casting.

    All that aside, @football70500 your questions were pretty simple, so I suggest you maybe look into learning more Java, it is clear you are adept at it, but maybe instead of a Bukkit Plugin, do something a bit more immersive, try to make a 2D game, so that you can practice coding, instead of asking easy-to-solve questions.

    :)
    No offense to any party involved, but honestly, calm down about this whole "must make learn java" thing!
     
  15. Offline

    mythbusterma

    @meguy26

    The point being that these are not Java forums, if they were, we wouldn't have an issue with people asking questions about Java here. They are Bukkit forums, and are for questions specific to the Bukkit API. Any questions asking about simple Java topics are:

    1. Not on topic for these forums
    2. In all likelihood extremely well documented on Google

    That's why me and Adam are so adamant about this, because it really does plug up these forums with irrelevant questions.

    You came here asking for our help, learn some humility.
     
    KingFaris11, teej107 and nverdier like this.
  16. To prevent spazms just teleport the player to themself when they move :)
     
Thread Status:
Not open for further replies.

Share This Page