Give Player Snowball

Discussion in 'Plugin Development' started by GRocksMc, Apr 19, 2017.

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

    GRocksMc

    I'm trying to give a player a snowball every second if he is in a certain area. I have other events running just fine in the same class, so that's not the problem.

    Code:
    Code:java
    1.  
    2. @EventHandler
    3. public void bombArea(PlayerMoveEvent e){
    4. Player p = e.getPlayer();
    5. if(p.getLocation().getWorld().getName().startsWith("Bomb")){
    6. for(double x = 0; x <= 10; x++){
    7. for(double y = 0; y <= 10; y++){
    8. for(double z = 0; z <= 10; z++){
    9. Location bombArea = new Location(p.getLocation().getWorld(), x, y, z);
    10. if(p.getLocation().equals(bombArea)){
    11. Bukkit.getScheduler().scheduleSyncDelayedTask(main, new Runnable(){
    12. public void run(){
    13. ItemStack snowball = new ItemStack(Material.SNOW_BALL);
    14. p.getInventory().addItem(snowball);
    15. }
    16. }, 20);
    17. }
    18. }
    19. }
    20. }
    21. }
    22. }
    23.  


    This isn't working, am I getting the area wrong? All help is appreciated!
     
  2. Offline

    jobisingh

    What specifically is not working

    Sent from my SAMSUNG-SM-G935A using Tapatalk
     
  3. Offline

    GRocksMc

    @jobisingh

    As far as I know, it's not giving me snowballs.

    I'll do some debugging right now.
     
  4. Offline

    Caderape2

    @GRocksMc Do you know how many times in one second this code can be called ? Leave this event alone and go for a scheduler every X seconds. this will be much better for the performance of the server.

    Are u sure about it ? your arena is a cuboid of 10 blocks from the location 0 ?
     
    MCMastery likes this.
  5. Offline

    GRocksMc

    @Caderape2

    I'm using a scheduler:
    Code:java
    1.  
    2. //Main Class
    3. BukkitTask Snowball = new BomberEvents(null).runTaskTimer(this, 20, 20);
    4.  
    5. //extends BukkitRunnable
    6. public void run(){
    7. for(Player p : Bukkit.getOnlinePlayers()){
    8. for(double x = 0; x <= 10; x++){
    9. for(double y = 0; y <= 10; y++){
    10. for(double z = 0; z <= 10; z++){
    11. if(p.getLocation().getWorld().getName().startsWith("Bomb")){
    12. Location bombArea = new Location(p.getLocation().getWorld(), x, y, z);
    13. if(p.getLocation().equals(bombArea)){
    14. ItemStack snowball = new ItemStack(Material.SNOW_BALL);
    15. p.getInventory().addItem(snowball);
    16. }
    17. }
    18. }
    19. }
    20. }
    21. }
    22. }
    23.  


    It's still not giving me the snowball. The 10x10x10 is just for testing purposes.
     
  6. Offline

    MCMastery

    So the bomb area is centered around (5,5,5) (coordinates)? Just making sure
     
  7. Offline

    GRocksMc

    @MCMastery

    I will change the coordinates after those coordinates work. At the moment, to test I just run around in that 10x10x10 area to see if it will give me a snowball. It hasn't work once yet.
     
  8. Offline

    Caderape2

    @GRocksMc
    I am wondering if the equals method compare a, int value of the location or the double value.
    if it's the double, that might be the problem. cuz double 10 != double 10.1

    You should try something like that for check a cuboid, might be more efficient.

     
  9. Offline

    MCMastery

    You are checking if their location exactly equals (10,10,10), (9,9,9) etc. It could be (9.5,10,10) for example.

    (Very rarely would it actually be exactly 10 x 10 x 10)

    Oh see @Caderape2's post
     
  10. Offline

    GRocksMc

    @Caderape2 @MCMastery @timtower @Zombie_Striker

    To get the locations from the config would I do this?

    Code:java
    1.  
    2. if(main.getConfig().get("BombSnowballLocation1") != null && main.getConfig().get("BombSnowballLocation") != null){
    3. String OGloc1 = main.getConfig().getString("BombSnowballLocation1");
    4. String[] locs1 = OGloc1.split(",");
    5. int X1 = Integer.parseInt(locs1[0]);
    6. int Y1 = Integer.parseInt(locs1[1]);
    7. int Z1 = Integer.parseInt(locs1[2]);
    8. String OGloc2 = main.getConfig().getString("BombSnowballLocation2");
    9. String[] locs2 = OGloc2.split(",");
    10. int X2 = Integer.parseInt(locs2[0]);
    11. int Y2 = Integer.parseInt(locs2[1]);
    12. int Z2 = Integer.parseInt(locs2[2]);
    13. if(p.getLocation().getWorld().getName().startsWith("Bomb")){
    14. if(X1 <= p.getLocation().getBlockX() && X2 >= p.getLocation().getBlockX()){
    15. if(Y1 <= p.getLocation().getBlockY() && Y2 >= p.getLocation().getBlockY()){
    16. if(Z1 <= p.getLocation().getBlockZ() && Z2 >= p.getLocation().getBlockZ()){
    17.  
    18. //config:
    19. BombSnowballLocation1: 0,0,0
    20. BombSnowballLocation2: 10,10,10
    21.  


    EDIT: Sorry, that was stupid of me, I accidentally only put in BombSnowballLocation for the first if statement.
     
    Last edited: Apr 21, 2017
  11. Offline

    Caderape2

    @GRocksMc
    You condition is wrong. You have to check if the player is between the minimum point and the maximum point, so if the player blockx is between 0 and 10.
     
Thread Status:
Not open for further replies.

Share This Page