How To Stop A Variable From Resetting ?

Discussion in 'Plugin Development' started by RLS0812, Feb 20, 2012.

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

    RLS0812

    Every time there is a player move event LastC is reset, and if (block!=LastC){} always evaluates as true.

    Code:
    package me.RLS0812.TNT;
     
    import org.bukkit.ChatColor;
    import org.bukkit.event.*;
    import org.bukkit.event.player.PlayerMoveEvent;
     
    public class Player_Move implements Listener {
        String LastC;
        @EventHandler(priority = EventPriority.NORMAL)
        public void onPlayerMove(PlayerMoveEvent event){
            String block = event.getPlayer().getLocation().getChunk().getX() + " " + event.getPlayer().getLocation().getChunk().getZ();
            if (block != LastC){
                LastC = block;
                event.getPlayer().sendMessage(ChatColor.GOLD + LastC);
            }}}
    
     
  2. RLS0812 What is the point of doing this check: if (block != LastC)
     
  3. Offline

    RLS0812

    To stop the spam to the player if they are still in the same chunk.
     
  4. Think about it this way: you need to get where the player is before the move (or his latest move event) and compare it to his new location after the move. You're comparing the new location to the new location, which always results as true.
     
  5. Offline

    RLS0812

    New to game dev ?
    That code compares new location to old location, and if different, updates old location.

    The issue is LastC is reset to null every time the player moves, I want to retain the value stored there from the if statement

    Here is a dummy version of the code
    Code:
    public class Compare_Old_To_New {
        String Old_Data;
        public void  Am_I_Different(String New_Data){
                // is the new data the same as the old data ?
            if ( Old_Data != New_Data){
                System.out.println("I am different");
                // the new data becomes the old data !
                Old_Data = New_Data;           
                }
            else{
                System.out.println("I am the same");
            }}}
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  6. new String("a") == new String("a") will return false
    new String("a").equals(new String("a")) will return true

    You know your problem now, if it helped, press like button, :D
     
Thread Status:
Not open for further replies.

Share This Page