Player move event bug

Discussion in 'Plugin Development' started by Ruptur, Mar 22, 2015.

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

    Ruptur

    I have a travel plugin which is i cant quite figure out why it wouldnt work on second travel. I would appreciate any help given.

    It is a simple teleport class that throws the player up into the air before teleporting.
    Whilst teleporting the player cant move left or right etc
    It works but the problem is after travel on second attempt it wont change the players velocity

    Again i would appreciate any help

    --------------- The classes --------------
    Show Spoiler

    Travel.java
    Code:
    public class Travel {
    
        private Player player;
        private Location location;
        private Location from;
        private Sound sound;
        private String name;
        private State state;
    
        private List<Effect> effects = new ArrayList<Effect>();
    
        private static Plugin plugin;
    
        public static void setPlugin(Plugin p){
            plugin = p;
        }
    
        public Travel(){}
    
        public Travel(Player player, Location location, Sound sound){
            this(player, location, sound, null);
        }
    
        public Travel(Player player, Location location, Sound sound, List<Effect> e){
            this.state = State.NOT_STARTED;
            this.player = player;
            this.location = location;
            this.from = player.getLocation();
      
            if (e != null && !e.isEmpty()) this.effects = e;
      
            this.sound = sound;
            this.name = player.getDisplayName() + " to " + location.getWorld().getName();
      
        }
    
        public String getName() {
            return this.name;
        }
    
        public void setPlayer(Player player){
            this.player = player;
        }
    
        public void addEffect(Effect effect){
            this.effects.add(effect);
        }
    
        /*deprecated*/
        public void initialize() {
            this.state = State.STARTED;
            Vector v = new Vector(0, 1.5, 0);
            player.setVelocity(v);
            Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                @Override
                public void run() {
                    state = State.IN_PROCESS;
                    player.teleport(location);
                    player.playSound(location, sound, 6, 1);
                    for (Effect e : effects) player.playEffect(location, e, 100);
                        state = State.FINISHED;
                }
            }, 30L);
        }
    
        public List<Effect> getEffects(){
            return this.effects;
        }
    
    
        public Location getLocation(){
            return this.location;
        }
    
        public Location getFrom() {
            return this.from;
        }
    
        public State getState() {
            return this.state;
        }
    
        public Player getPlayer(){
            return this.player;
        }
    }
    
    Travels.java
    
    Code:java
    1.  
    2. public class Travels implements Listener{
    3. private static Map<Player, Travel> travelers = new HashMap<>();
    4.  
    5. public static Travel travel(Player player, Location to, Sound sound){
    6. Travel t = new Travel(player, to, sound);
    7. travelers.put(player, t);
    8. return t;
    9. }
    10.  
    11. @EventHandler
    12. public void onFallDamage(EntityDamageEvent e){
    13. if (!(e.getEntity() instanceof Player)) return;
    14.  
    15. Player p = (Player) e.getEntity();
    16. if (travelers.containsKey(p)){
    17. if (travelers.get(p).getState() == State.FINISHED) {
    18. travelers.remove(p);
    19. return;
    20. }
    21. e.setCancelled(true);
    22. }
    23. }
    24.  
    25. @EventHandler
    26. public void onMove(PlayerMoveEvent e){
    27. if (travelers.containsKey(e.getPlayer())){
    28. if (travelers.get(e.getPlayer()).getState() == State.FINISHED) {
    29. travelers.remove(e.getPlayer());
    30. return;
    31. }
    32. Location to = e.getTo();
    33. Location from = travelers.get(e.getPlayer()).getFrom();
    34. Location p = e.getPlayer().getLocation();
    35. Travel t = travelers.get(e.getPlayer());
    36.  
    37. if (to.getX() != from.getX() || to.getZ() != from.getZ()){
    38. t.getPlayer().teleport(new Location(p.getWorld(), p.getX(), p.getY() ,p.getZ()));
    39. }
    40. if (to.getY() < e.getPlayer().getLocation().getY()){
    41. e.setCancelled(true);
    42. }
    43. }
    44. }
    45.  
    46. @EventHandler
    47. public void onTeleport(PlayerTeleportEvent e){
    48. if (travelers.containsKey(e.getPlayer())){
    49. Player p = e.getPlayer();
    50. if (travelers.get(p).getState() == State.FINISHED){
    51. travelers.remove(p);
    52. return;
    53. }
    54. e.setCancelled(true);
    55. }
    56. }
    57.  
    58. }
    59.  


    State enum
    Code:
    public enum State {
    
        FINISHED,
    
        STARTED,
    
        NOT_STARTED,
    
        IN_PROCESS
    }
    
    
    Those are the three classes involved

    And i would start it like this
    Example
    Code:
    Travel travel = Travels.travel(p, t.getLocation(), Sound.FIREWORK_BLAST);travel.initialize();
    travel.initialize();
    


    Thanks for the help :)
     
  2. I don't have much time till I have to leave so I can't help with your error but I just noticed you're storing a Player in a Map. Please don't do that, I don't care whether you've handled it safely, just don't. :) Store the UUID of the Player instead, and change everything to getPlayer().getUniqueId() instead.
     
  3. Offline

    Ruptur

    @KingFaris11
    I have a reason for doing that. Thanks for the tip
     
    KingFaris11 likes this.
  4. Offline

    MajorSkillage

    Why do you have 2 public Travel()'s on your first class?
     
  5. Offline

    Ruptur

    @MajorSkillage

    I have three to be precise.
    They do different things. I can do Trade(player, tolocation, sound, <optional> effects)
     
  6. Offline

    MajorSkillage

    You could always check the arguments and depending on the arguments of the command makes it depend on what happens. Play around with the velocity instantly after the command is run such as /travel (user) (location) then save the Location as another object and set the velocity however there could be another argument to test the velocity you need/want. It could be a different velocity depending on the location, il admit I am not too good with velocity this is just a guess to me.
     
  7. Offline

    Ruptur

    @MajorSkillage
    The teleportation part works well the problem is just on the second go (lets say i just 'traveled') it doesnt properly throw the player up in the air.
    My guess is that the player isnt removed from the map fast enough, but i dont think thats it
     
Thread Status:
Not open for further replies.

Share This Page