Solved Block Velocity

Discussion in 'Plugin Development' started by TheHadDad, Feb 17, 2018.

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

    TheHadDad

    Hi there! Around a year ago, I stopped coding bukkit plugins. I've started again, but i've forgotten many things and need to practice making random things. I've been having trouble with multiplying block velocity. I don't know if i need to set pitch and yaw, x y z etc. I know it might sound simple, but i'm super stuck and have been searching for a while. Btw, i cannot setVelocity.

    I would highly appreciate if someone could explain the steps involved too :).

    Code (atm) :
    Code:
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            Action a = e.getAction();
            if (a == Action.RIGHT_CLICK_BLOCK) {
                if (p.getItemInHand().getType() == Material.STICK) {
                    if (e.getClickedBlock().getType() == Material.DIRT) {
                        Block b = e.getClickedBlock();
                        Location bl = b.getLocation();
                        Vector v = bl.toVector(); //what does this mean?
                    }
                }
            }
        }

    Don't hesitate to reply if you are unsure, I will try absolutely everything.
     
  2. Offline

    Nostos

    Explanation: You are listening to PlayerInteractEvent then getting the player then what the player does (action) you check if they right click the block, then you check what they right clicked with then you check if the block is dirt then you get the block, get its location then you get the location to a vector, never done this personally but it sounds like you need something after toVector() like toVector().multiply(5) or something and then do something like b.setVelocity(v) (I think thats why setVelocity isn't working for you, you have to setVelocity to a vector).

    I assume you wanted explanation for all of the code? sorry if I'm being dumb lol
     
  3. Offline

    TheHadDad

    Haha thank you for explaining, I will try doing what you said about setting Velocity to a vector.


    EDIT :
    I have given it a go, I tried multiplying the vector, but there is no setVelocity after that. I did multiply the vector by a double though, could that be part of the problem?

    Code:
    @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            Action a = e.getAction();
            if (a == Action.RIGHT_CLICK_BLOCK) {
                if (p.getItemInHand().getType() == Material.STICK) {
                    if (e.getClickedBlock().getType() == Material.DIRT) {
                        Block b = e.getClickedBlock();
                        Location bl = b.getLocation();
                        Vector v = bl.toVector();
                        v.multiply(5); // nope xD
    
                    }
                }
            }
        }
     
  4. Offline

    Lorinthio

    Take this a step back and ask first, what you are trying to achieve? What is your desired result?

    Multiplying a vector does nothing if you don't assign it to something. You can't set the velocity of a block because it doesn't move. You may need something like make it a FallingBlock, and then set the velocity of that since it acts as an entity.

    I'm heading offline but feel free to reply back and I will check back in the morning!
     
  5. Offline

    TheHadDad

    Sorry for the late reply, I figured it out. I needed to save the block clicked on aswell as save it's location and the players velocity:
    Code:
    MaterialData data = new MaterialData(b.getType(), b.getData());
    Then set the clicked block to air, b.setType(Material.AIR);
    I can't fully explain it atm since i'm in a rush, but here is the code for those who are stuck, try to understand it while reading. Don't ctrl c + ctrl v, for your sake (unless you want to test it out)

    Code:
        public void shootBlock(Player p, Block b) {
    
            MaterialData data = new MaterialData(b.getType(), b.getData());
            b.setType(Material.AIR);
            FallingBlock block = (FallingBlock) p.getWorld().spawnFallingBlock(b.getLocation(), data);
            Vector vec = p.getLocation().getDirection();
    
            if (vec.getY() < 0) {
                vec.setY(Math.abs(vec.getY()));
            }
            vec.multiply(new Vector(1, 2.5, 1));
            block.setVelocity(vec);
        }
    
    if (a == Action.RIGHT_CLICK_BLOCK) {
                if (p.getItemInHand().isSimilar(lvl1)) {
                    shootBlock(p, e.getClickedBlock());
                }
    
    //I turned it into a function so that I could make different levels, lvl1 is an itemstack, so you could make something like ItemStack lvl1 = new ItemStack(Material.STICK); which I did.
    
    Going to set this post as solved :D Ty for your replies.
     
Thread Status:
Not open for further replies.

Share This Page