Solved PlayerCommandPreprocessEvent Executing the Command

Discussion in 'Plugin Development' started by ERROR372, Jul 16, 2013.

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

    ERROR372

    So I'm trying to make a HorseTeleport plugin, where if you are riding a horse, and you issue a command that teleports you (based on my plugins: /tp, /warp, /spawn, /home), it will:

    1) Save the Horse you are riding on.
    2) Remove you from the Horse
    3) Execute the command
    4) Teleport the saved Horse entity to the player.

    Now, I can easily do steps 1, 2, and 4... But I cannot seem to find a way in the event to execute the command the player did. It's probably rather simple and I'm just completely glossing over it. So any help would be appreciated!


    Also, the reason I'm using PlayerCommandPreprocessEvent instead of PlayerTeleportEvent is because if you are riding a vehicle (pig, horse, minecart), you won't actually be teleported, so the event never fires.
     
  2. Offline

    soulofw0lf

    don't cancel the event and it should still work fine
     
  3. Offline

    ERROR372

    soulofw0lf

    But doesn't PlayerCommandPreprocessEvent do things before the command takes place? If it does, teleporting the horse to me before the teleporting command happens is pointless...


    Just tested it, and indeed it teleports the horse before the command happens, like I suspected. Is there some sort of code that says to execute the command the user entered, and then do more?
     
  4. Offline

    Shevchik

    Schedule a task with one tick delay?
     
  5. Offline

    ERROR372

    Okay, it looks like player.performCommand(String) works and does what I need it to do...

    Now is there a way to cancel the actual command the player typed in, so they don't get teleported twice? =P
     
  6. Offline

    autoit4you

    Code:text
    1. event.setCancelled(true);
     
  7. Offline

    ERROR372

    autoit4you

    Thanks, that's just what I needed it to do. Was under the impression setting the event to be cancelled would stop anything in it from happening. Guess not.

    And for anyone looking for a similar plugin:

    Code:java
    1. import org.bukkit.entity.EntityType;
    2. import org.bukkit.entity.Horse;
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    7.  
    8. public class HorseTeleport implements Listener
    9. {
    10. @EventHandler
    11. public void onHorseTP(PlayerCommandPreprocessEvent event)
    12. {
    13. String input = event.getMessage();
    14. if(input.startsWith("/tp") || input.startsWith("/warp") || input.startsWith("/home") || input.startsWith("/spawn"))
    15. {
    16. Player p = event.getPlayer();
    17. if(p.isInsideVehicle() && p.getVehicle().getType() == EntityType.HORSE)
    18. {
    19. Horse h = (Horse) p.getVehicle();
    20. p.leaveVehicle();
    21. String message = event.getMessage().substring(1);
    22. p.performCommand(message);
    23. h.teleport(p);
    24. event.setCancelled(true);
    25. return;
    26. }
    27. }
    28. return;
    29. }
    30.  
    31. }


    Feel free to use my code (and change it however you like).
     
Thread Status:
Not open for further replies.

Share This Page