Make player sit on a stair

Discussion in 'Plugin Development' started by sistem21, Oct 4, 2015.

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

    sistem21

    Hi guys today i wanted to know how can i make that a player sits a chair,right clicking on a stair,and if he press shift,he will exit from the chair.
    I would not use pakets or NMS,i'm not able with them,simply using events
    I hope that anyone will help me,thanks ;)
     
  2. Offline

    DoggyCode™

    Listen for the click event and check if the block clicked's state is Stair (or something). Get the location of the stair and put player in sneak mode, and add him to a ArrayList where you listen for playermoveevent and if the List contain's the player's name then you will cancel, and if the player sneaks (or something/ a command could also work) and the list contains the player you will remove him from the list which will make him able to move again.
     
  3. Offline

    Ruptur

    @sistem21 @DoggyCode™
    For more realistic sitting instead of putting them in sneak mode you can spawn an arrow entity at the location of the stairs and mount the player on the arrow with arrow.setPassenger(player).
     
  4. Offline

    sistem21

    Why an arrow?And how he can exit from the chair(pressing shift)?
     
  5. Offline

    Xerox262

    Arrows are small and not easy to notice from afar. If you press shift it exits the vehicle that you're on, which in this case is an Arrow. You could use a snowball if you wanted or any entity. It's better to use a small entity like an arrow, snowball or ender pearl. Because if you use something like a horse or a boat it could look a little strange :p
     
  6. Offline

    sistem21

    I made it but how can i remove that entity after that a player dismounts it?
    Here is my code:
    Show Spoiler

    Code:JAVA
    1.  
    2. package me.sistem21.PotatoChairs;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.Location;
    6. import org.bukkit.Material;
    7. import org.bukkit.World;
    8. import org.bukkit.entity.Entity;
    9. import org.bukkit.entity.EntityType;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.block.Action;
    14. import org.bukkit.event.player.PlayerInteractEvent;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class PotatoChairs extends JavaPlugin implements Listener{
    18.  
    19. protected PotatoChairs plugin;
    20.  
    21. public void onEnable(){
    22. plugin = this;
    23. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    24. }
    25.  
    26. @EventHandler
    27. public void on(PlayerInteractEvent e){
    28. Player p = e.getPlayer();
    29. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    30. if(p.getItemInHand().getType() == Material.AIR){
    31. if(e.getClickedBlock().getType() == Material.WOOD_STAIRS || e.getClickedBlock().getType() == Material.COBBLESTONE_STAIRS){
    32. if(p.hasPermission("potatochairs.use") || p.isOp()){
    33. Location l = e.getClickedBlock().getLocation();
    34. World w = p.getWorld();
    35. Entity chair = w.spawnEntity(l.add(0.5D, 0.2D, 0.5D), EntityType.ARROW);
    36. chair.setPassenger(p);
    37. }
    38. }
    39. }
    40.  
    41. }
    42. }
    43.  
    44.  
    45.  
    46. }
    47.  

    Here is results:
    Show Spoiler

    [​IMG]

    i don't want that when a player sit on the chair,goes in the middle of the block,and i want also that when he dismount the entity,the entity despawn
     
  7. Offline

    RoboticPlayer

    For despawning the entity, try making an entity field & when the player right clicks the block, assigning the field. Then listen for the PlayerToggleSneakEvent & if they are on the arrow, arrow.remove();
     
  8. Offline

    sistem21

    Last edited: Oct 4, 2015
  9. Offline

    Xerox262

    Don't do that
    Listen for a player exiting a vehicle, check if the vehicle is instanceof an Arrow and then if it is call Entity#remove()
     
  10. Offline

    sistem21

    Already tried,but the arrow is left there..another problem is that when a player right click on the stair,he goes in the middle of the block,as you can see in the spoiler
    Show Spoiler

    [​IMG]
     
  11. Offline

    Xerox262

    The reason it didn't remove the arrow for you was you need an EntityDismountEvent, not a VechicleLeaveEvent and the reason that you fall into the chair is because the arrow is falling, you need to stop the arrow from falling
     
  12. Offline

    sistem21

    Arrow always left there,how can i stop arrow from falling?
     
  13. Offline

    DoggyCode™

    Continuously setting its location to a bit above the block? Maybe use Scheduled repeating task?
     
  14. Offline

    sistem21

    i didnt understand what i should do @DoggyCode™
    Here is the code:
    Code:java
    1.  
    2. package me.sistem21.PotatoChairs;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.Location;
    6. import org.bukkit.Material;
    7. import org.bukkit.World;
    8. import org.bukkit.entity.Arrow;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.Action;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class PotatoChairs extends JavaPlugin implements Listener{
    17.  
    18. protected PotatoChairs plugin;
    19. private Arrow arrow;
    20.  
    21. public void onEnable(){
    22. plugin = this;
    23. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    24. }
    25.  
    26. @EventHandler
    27. public void on(PlayerInteractEvent e){
    28. Player p = e.getPlayer();
    29. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    30. if(p.getItemInHand().getType() == Material.AIR){
    31. if(e.getClickedBlock().getType() == Material.WOOD_STAIRS || e.getClickedBlock().getType() == Material.COBBLESTONE_STAIRS){
    32. if(p.hasPermission("potatochairs.use") || p.isOp()){
    33. Location l = e.getClickedBlock().getLocation();
    34. World w = p.getWorld();
    35. arrow = w.spawn(l.add(0.5D, 0.2D, 0.5D), Arrow.class);
    36. arrow.setPassenger(p);
    37. }
    38. }
    39. }
    40.  
    41. }
    42. }
    43.  
    44.  
    45.  
    46.  
    47. }
    48.  
    49.  
     
    Last edited: Oct 4, 2015
  15. Offline

    teej107

    @sistem21 Perhaps you can look into sending packets. You won't need to mess with Entities to get the sitting effect.
     
  16. Offline

    RoboticPlayer

    May I ask why not?
     
  17. Offline

    Xerox262

    No need to waste memory to store all activated chairs, plus they'll be removed if the server reloads or restarts, while an arrow and entity dismount event wouldn't
     
Thread Status:
Not open for further replies.

Share This Page