Anyone think they can code this?

Discussion in 'Programming' started by Auzzi, May 1, 2015.

  1. Hey! I am planning on expanding my plugin and I need someone to code something for me since I am better at editing. I need it to be where if you right click with a slimeball in your hand, it shoots out and you spawn a web at where you were looking. Thanks a million! <3
     
    • Funny Funny x 2
    • Optimistic Optimistic x 2

  2. This will only take like 5 minutes. Why don't you do it yourself if you have experience in making plugins whether you are better at editing which I don't really get...
     
    • Agree Agree x 1
    • Optimistic Optimistic x 1
  3. I know how to edit but I dont know how to make.. I have only been learning for like a week
     
    • Funny Funny x 3
  4. You should consider learning how to make in addition to editing, it's a lot more fun!

    Anyways, to get you started:
    Code (Text):
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
              if (event.getPlayer().getItemInHand().getType() == Material.SLIME_BALL) {
                        BlockIterator iterator = new BlockIterator(event.getPlayer().getEyeLocation(), Y_OFFSET, MAX_DISTANCE);
                        Block block;
                        while (iterator.hasNext()) {
                                  block = iterator.next();
                                  if (!(block.isEmpty())) {
                                            // block is now the block they clicked on, this doesn't however find the face that was clicked on
                                            break;
                                  }
                        }
              }
    }
     
    Where Y_OFFSET is the offset in the Y-axis for the cast (you probably want to keep this 0) and MAX_DISTANCE is the max amount of blocks from your current position you want to scan (the operation can be rather calculation heavy if someone is pointing at a mountain range 500 blocks away), I suggest putting this to a value around 100.
     
  5. Aw! Thank you so much! :D
     
  6. So after that, it should end up looking something lke this? *Im not very good at this xD Sorry

    Code (Text):
           @EventHandler
           public void onPlayerInteract(PlayerInteractEvent event) {
                     if (event.getPlayer().getItemInHand().getType() == Material.SLIME_BALL) {
                               BlockIterator iterator = new BlockIterator(event.getPlayer().getEyeLocation(), Y_OFFSET(0), MAX_DISTANCE(100));
                               Block block;
                               while (iterator.hasNext()) {
                                         block = iterator.next();
                                         if (!(block.isEmpty())) {
                                                   // block is now the block they clicked on, this doesn't however find the face that was clicked on
                                                   break;
                                         }
                               }
                     }
           }

        private double Y_OFFSET(int i) {
            // TODO Auto-generated method stub
            return 0;
        }

        private int MAX_DISTANCE(int i) {
            // TODO Auto-generated method stub
            return 0;
        }
    }
     
    • Like Like x 1
  7. Haha that works yes, additionally you can just store them as variables on the class (complete uppercase names are usually intended for final variables). So for example:
    Code (Text):
    public class Test {
            private final int Y_OFFSET = 0;
            private final int MAX_DISTANCE = 100;

            // onEnable code goes here

            // empty main method goes here

            // onPlayerInteractEvent code goes here
    }
    What the BlockIterator in the previous code does is fire an imaginary line from the Players current EyeLocation to the point where their mouse is pointed (pretty much exactly where your crosshair is on the screen). It then calculates across this line from the starting point (player eye location) to the end point (MAX_DISTANCE in the direction of where the player clicked, regardless of whether there's a block thats not air in the way it will keep going!) and checks every block along the way. With "if (!(block.isEmpty()))" we look for the first block that is not air along this line (air blocks are considered empty by the spigot api). The first block our iterator reports as not empty will be the block the player clicked on (snow on top of blocks actually counts as a whole block as well, keep that in mind if you're running into weird behaviour). Thinking about it, the block you want to replace with a web block is the one just before the first block that is not air.

    Try modifying my code in such a way that it stores the current block we are looking at, so that we can still access it when we are looking at the next one (iterators can't go backwards). Then when you do run into the first block that is not air, you can set the type on the previous block (the one that we stored locally in the method) to Material.WEB.

    Hope this gives you an idea of how to finish it :)
     
    • Like Like x 1
    • Friendly Friendly x 1
    • Useful Useful x 1
  8. Thanks soooo much ! I really appreciate it :)
     
  9. That shouldn't have anything to do with the code we just wrote, but show me the part of the code where you're doing things with guns and listeners (the exception is thrown because a 'Guns' object cannot be cast to a 'Listener' object, so look for a Guns reference preceded by (Listener) in brackets).
     
  10. Alright I fixed the error by deleting where it said (Listener) and now nothing from inside the plugin is working, but at least there is no error! xD
     
  11. Hahaha, don't delete it d: show the part with the (Listener) syntax. Chances are it can be fixed. If you want to make a class a Listener you can do so via:
    Code (Text):
    public class MyClass implements Listener {

    }
    rather than casting it to a Listener via (Listener)
     
    #12 fishb6nes, May 4, 2015
    Last edited: May 4, 2015
  12. Ahh well there isnt a listener so idk why i put it there to begin with xDD
     
    • Creative Creative x 1
  13. Also, if you only want it to execute on a right-click, you can check that the action is of type RIGHT_CLICK_BLOCK or RIGHT_CLICK_AIR - so it doesn't fire on left clicks, or when you initiate a physical action (walk on pressure plates ect)
    Code (Text):
    if(e.getAction() == Action.RIGHT_CLICK_BLOCK ||
                    e.getAction() == Action.RIGHT_CLICK_AIR) {}
     
    • Like Like x 2
  14. On a scale of 1 to 10 how correct is this? xD Again sorry I'm really new to this. I've only been learning for like 2 weeks.
    Code (Text):
    @EventHandler
      public void onPlayerInteract(PlayerInteractEvent event) {
                if (event.getPlayer().getItemInHand().getType() == Material.SLIME_BALL) {
                          BlockIterator iterator = new BlockIterator(event.getPlayer().getEyeLocation(), Y_OFFSET(0), MAX_DISTANCE(100));
                          Block block;
                          while (iterator.hasNext()) {
                                    block = iterator.next();
                                    if (!(block.isEmpty())) {
                                        block.setType(Material.WEB);
                                    }
                          }
                }
      }

    private double Y_OFFSET(int i) {
       return 0;
    }

    private int MAX_DISTANCE(int i) {
       return 0;
    }
    }
     
  15. Almost, 3 points of note;

    - Change the Y_OFFSET and MAX_DISTANCE methods to members (variables) in stead (check above, I already spoiled how to do it).
    - Set the type of the previous block when you are inside the isEmpty if statement to web.
    - Add Wood's suggestion and check if the player is actually right clicking as PlayerInteractEvent is fired for both left and right clicks.
     
  16. *deep breath* Ok... I am pretty close, but I am not sure what you mean when you say "Set the type of the previous block when you are inside the isEmpty if statement to web. Am I supposed to put it here:
    if (!(block.isEmpty(here)));
    Because thats not working :/
     
  17. Code (Text):
    if (!(block.isEmpty())) {
              // right here, but instead of the block we are at right now, we want to set the previous
              // block (thus 'block' the previous iteration in the loop before we called next() on it again) to web
    }
    The while loop goes over a series of blocks, when we are at the block where the if statement is true, we want to set the previous block in the series to web.
     
  18. I'm still confused on what to put, now that I know where to put it. do I put block.Something or ??