Gravity Gun

Discussion in 'Plugin Development' started by BlueMustache, Feb 25, 2014.

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

    BlueMustache

    Hello,
    If I spawn a falling sand entity, from a interact event, how can I do the following?
    - Keep the sand from turning into a block
    - Set the velocity of the falling sand to always follow the players cursor. (within a 4 block reach)

    I am trying to recreate the effect of a gravity gun, or the effect of the USE key. (For those who have played valve games) I am making a portal recreate (yes, another one!), and I need a USE key, pick up, falling sand block, cursor trick?
    Any ideas?
    Yes, you in the back!
     
  2. Offline

    Brendyn Todd

    BlueMustache

    I'm not entirely positive that recreating a gravity gun, as it is used in valve games, is entirely possible. If you mean to pick it up and flail it around like in the real deal.
     
  3. Offline

    Garris0n

    It is, but it's not necessarily easy.
     
  4. Offline

    Brendyn Todd

    Garris0n
    Really 0.0... good to know.
     
  5. Offline

    BlueMustache


    I want a USE style block pick up.
    I press say E, which cancels my inventory open event, and will toggle my pick up value to true. The block I'm looking at will become falling sand, and follow my cursor like the real deal.
     
  6. Offline

    Desle

    BlueMustache
    The E key is not possible. You can make them right/leftclick with the Gravity Gun though.
     
  7. Desle the "E" key fires an InventoryOpenEvent unless I am mistaken, this can be used to toggle the block pickup just as BlueMustache said. I would have no clue how to go about the rest of the code though
     
  8. Offline

    nitrousspark

    thats is, if the player has e set up to open their inventory in their controls
     
    Jake6177 likes this.
  9. Offline

    Desle

    mike546378
    Forgot that. I have mine set to c. But still, not able to detect when they open their inventory.
     
  10. Offline

    BlueMustache


    Yes you can? I will worry about that. Any ideas for the rest of the code?
     
  11. Offline

    CrazyGuy3000

    Add them to a Arraylist when they open there inventory and then remove them a few ticks later perhaps?
     
  12. Offline

    BlueMustache

    I know that, Thank You.
    My guestion is the falling sand following the cursor part.
     
  13. Offline

    CrazyGuy3000

    Pseudo time
    > Get there direction
    > Spawn Falling Sand Entity every 20 ticks at the direction there looking
    > Remove old entity
    > ???
    > Profit
     
  14. Offline

    BlueMustache


    Any ideas of getting where the player is looking, and setting the sands velocity to move to it?
     
  15. Offline

    CrazyGuy3000

    BlockIterator?
     
  16. Offline

    desht

    InventoryOpenEvent is not fired for the player's own inventory (try it and see) - that's done entirely client-side. The event is only fired for opening container blocks with a right-click.

    BlueMustache as for getting a FallingBlock entity to remain properly static... good luck :) Even if you continually reset its velocity to 0, the client still tries to make it fall, and you'll get a horribly twitchy block in front of the player. You need to find some way of tricking the client into thinking the block isn't falling.

    Getting it to follow the player's movement isn't too hard - catch the PlayerMoveEvent, and calculate where the block should be held by extending a short Vector in the direction the player is looking, e.g.:
    PHP:
    Location l player.getEyeLocation().add(player.getLocation().getDirection().multiply(1.5));
    Now if you can get the client to think your FallingBlock is static at that location, you're in business. See this thread for a couple of ideas. Garris0n had a neat idea about creating an invisible bat which you could set the FallingBlock as a passenger for. Then use the code above to move the bat around, and the block should hopefully ride with it. Worth investigating...
     
    NathanWolf and BlueMustache like this.
  17. Offline

    BlueMustache


    Thank You, just what I was looking for.
    The bat thing is a good idea, and I did not know that about the InventoryEvent.
    Also, the location snippit was very helpful.
    Thanks Guys!

    Also, I already knew about block 36, and the fact that FallingSand is glitched client side.
    I was going to use that.
    The last thing I need help with, is how would I calculate what the velocity is I need set an Entity to, to make it move to that location.
    Say I made a function calcVelocity(Location from, Location to); Returns Vector.
    You input the entity's position into "from", and the destination "to", and It will calculate the Vector required, and return it.
    What do I need to put in the function, to turn a location into a calculated velocity trajectory?
    Thanks, I promise this is the last thing.
    - Blue

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  18. Offline

    Desle

    BlueMustache desht CrazyGuy3000

    I tried the following.. although it doesn't really work, since it doesn't move your cursor smoothly.. and it gets removed after a while. Anyone knows why?

    Code:java
    1. public class GravityBlock {
    2.  
    3. public static Map<String, GravityBlock> players = new HashMap<String, GravityBlock>();
    4.  
    5. private Material type;
    6. private byte data;
    7. private EntityWitherSkull skull;
    8. private Entity block;
    9. private String player;
    10.  
    11. public GravityBlock(Material type, byte data, Location loc, Player p) {
    12. this.type = type;
    13. this.data = data;
    14. this.player = p.getName();
    15. spawn(loc);
    16. }
    17.  
    18. public void setLocation(Location loc) {
    19. this.skull.setPosition(loc.getX(), loc.getY(), loc.getZ());
    20. }
    21.  
    22. public void remove() {
    23. this.block.remove();
    24. this.skull.getBukkitEntity().remove();
    25. players.remove(this.player);
    26. }
    27.  
    28. private void spawn(Location loc) {
    29. WorldServer world = ((CraftWorld) loc.getWorld()).getHandle();
    30. EntityWitherSkull skull = new EntityWitherSkull(world);
    31. skull.setLocation(loc.getX(), loc.getY(), loc.getZ(), 0, 0);
    32. world.addEntity(skull);
    33. FallingBlock block = loc.getWorld().spawnFallingBlock(loc, type, data);
    34. block.setDropItem(false);
    35. skull.getBukkitEntity().setPassenger(block);
    36. this.block = block;
    37. this.skull = skull;
    38. players.put(this.player, this);
    39. }
    40. }
    41.  


    And this is how I moved it;
    Code:java
    1. @EventHandler
    2. public void onMove(PlayerMoveEvent e) {
    3. if (e.getFrom().getPitch() != e.getTo().getPitch() || e.getFrom().getYaw() != e.getTo().getYaw()) {
    4. if (GravityBlock.players.get(e.getPlayer().getName()) != null) {
    5. GravityBlock block = GravityBlock.players.get(e.getPlayer().getName());
    6. @SuppressWarnings("deprecation")
    7. Location loc = e.getPlayer().getTargetBlock(null, 20).getLocation();
    8. block.setLocation(loc);
    9. }
    10. return;
    11. }
    12. }
     
    MrShnig likes this.
  19. Offline

    BlueMustache


    Thank You, very helpful. (very interesting)
    I will try integrating this.
    I will revise this to the best of my abilities.
    For now, this is solved.
    Thanks.
    - Blue
     
Thread Status:
Not open for further replies.

Share This Page