Help with player holding block and throwing it

Discussion in 'Plugin Development' started by ArthurMaker, Mar 5, 2014.

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

    ArthurMaker

    I want to allow my ops to get blocks from the ground right-clicking it, and then when they stop to hold the right-button, they will throw the block. I have this code and picking up the block is working great, but I don't know well how to detect when the player stops to hold the right-button, since Bukkit doesn't have an event like this.

    Code:java
    1. package Millenary.Craft;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.Effect;
    7. import org.bukkit.Material;
    8. import org.bukkit.block.BlockState;
    9. import org.bukkit.entity.FallingBlock;
    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.  
    16. public class TesteListener implements Listener {
    17.  
    18. private static HashMap<String, BlockState> blocks = new HashMap<String, BlockState>();
    19. private static HashMap<String, Integer> timing = new HashMap<String, Integer>();
    20.  
    21. @SuppressWarnings("deprecation")
    22. @EventHandler
    23. public void onThrowBlock(PlayerInteractEvent event){
    24. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
    25. if(event.getPlayer().getItemInHand().getType().equals(Material.IRON_SWORD)){
    26. if(event.getPlayer().isOp()){
    27. if(!event.getClickedBlock().getType().equals(Material.AIR)){
    28. if(!blocks.containsKey(event.getPlayer().getName())){
    29. final BlockState b = event.getClickedBlock().getState();
    30. blocks.put(event.getPlayer().getName(), b);
    31. startCounter(event.getPlayer(), event);
    32. //isholding.add(event.getPlayer().getName());
    33. b.getWorld().playEffect(b.getLocation().add(0.5, 0.5, 0.5), Effect.STEP_SOUND, b.getTypeId());
    34. b.getBlock().setType(Material.AIR);
    35. Bukkit.getScheduler().scheduleSyncDelayedTask(MillenaryCraft.getInstance(), new Runnable(){
    36. public void run(){
    37. b.update(true, false);
    38. b.getWorld().playEffect(b.getLocation().add(0.5, 0.5, 0.5), Effect.STEP_SOUND, b.getTypeId());
    39. }
    40. }, 50L);
    41. }
    42. }
    43. }
    44. }
    45. }
    46. }
    47.  
    48. @SuppressWarnings("deprecation")
    49. public void startCounter(final Player player, final PlayerInteractEvent event){
    50. timing.put(player.getName(), Bukkit.getScheduler().scheduleSyncRepeatingTask(MillenaryCraft.getInstance(), new Runnable(){
    51. public void run(){
    52. if(event.isCancelled()){
    53. Player p = (Player) event.getPlayer();
    54. BlockState b = blocks.get(p.getName());
    55. FallingBlock fb = p.getWorld().spawnFallingBlock(p.getEyeLocation(), b.getType(), b.getRawData());
    56. fb.setDropItem(false);
    57. fb.setVelocity(p.getEyeLocation().getDirection().multiply(1.25D));
    58. blocks.remove(p.getName());
    59. Bukkit.getScheduler().cancelTask(timing.get(player.getName()));
    60. }
    61. }
    62. }, 0L, 3L));
    63. }
    64.  
    65.  
    66. }
     
  2. Offline

    adam753

    Yup, this isn't possible via Bukkit. Sorry.
     
  3. Offline

    ArthurMaker


    Mineplex did it on SuperSmashMobs(Enderman class).
    So yeah, it is possible.
     
  4. Offline

    adam753

    ArthurMaker
    Hm, really? Definitely not possibly using PlayerInteractEvent, I can assure you. Maybe the click window packet can do what you want? I'm afraid you'll have to wait for someone else for help with that though, I've never tried to use packets before.
     
    ArthurMaker likes this.
  5. Offline

    ArthurMaker


    Seems great.
    Maybe using "Starting right mouse drag" and "Ending right mouse drag". But I didn't understand how to do it, anyway. '-'
     
Thread Status:
Not open for further replies.

Share This Page