Error in my code

Discussion in 'Plugin Development' started by Stackore, Apr 21, 2014.

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

    Stackore

    Hi, i got error in eclipse with my code and don't now what to do.
    Insert the code to see the errors.
    Code:java
    1. public void onEnable() {
    2. this.getServer().getPluginManager().registerEvents(this, this);
    3. }
    4.  
    5.  
    6. public Location[] circleFrom(Location l) {
    7. List<Location> returnList = new ArrayList<Location>();
    8. for(int x = 2; x>=-2; x--){
    9. for(int z = 2; z>=-2; z--){
    10. for(int y = 0; y>=1; y++){
    11. if(!(Math.abs(z)==1&&x==0)||!(Math.abs(x)==1&&z==0)||x!=z){
    12. returnList.add(new Location(l.getWorld(), l.getX()-x, l.getY()+y, l.getZ()-z));
    13. }
    14. }
    15. }
    16. }
    17. return returnList.toArray(new Location[returnList.size()]);
    18. }
    19.  
    20. @EventHandler
    21. public void gandalf(PlayerInteractEvent e) {
    22. Player p = e.getPlayer();
    23. Action a = e.getAction();
    24. if (a == Action.RIGHT_CLICK_AIR || a == Action.RIGHT_CLICK_BLOCK) {
    25. if (p.getItemInHand().getType() == Material.STICK) {
    26.  
    27. }
    28.  
    29. Location location=p.getLocation();
    30. for(int i = 0; i<circleFrom(location).length; i++){
    31. Location l = circleFrom(location);
    32. Material changeTo = Material.SPONGE;//Change this to whatever you want, IDC. If you want to get the
    33. //Block under the location, create a loop that will go locations //if the locations block is air continue, if not break and use that one.
    34. new BukkitRunnable(){
    35. public void run(){
    36. l.getBlock().setType(changeTo);
    37. }
    38. }.runTaskLater(this, i*2);//i*20 = (i) seconds
    39. }
    40. new BukkitRunnable(){
    41. public void run(){
    42. for(Location l : circleFrom(location)){
    43. l.getBlock().setType(Material.AIR);
    44. }
    45. }.runTaskLater(this, 3.8);//2 seconds + 18 blocks that each takes 0.1
    46. }
    47. }
    48.  
    49. }
    50. }
     
  2. Offline

    CraftedShack

    1) Which line are you getting the error on? and what if you hover over the red line in eclipse. It will say what is the error is, What does it say?

    2) You should post the whole class with all your imports, importing from the wrong location can cause errors as well.
     
  3. Offline

    Stackore


    Code:java
    1. //Whole code
    2. package me.gandalf;
    3.  
    4. import java.util.ArrayList;
    5. import java.util.List;
    6.  
    7. import org.bukkit.Location;
    8. import org.bukkit.Material;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.Action;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15. import org.bukkit.scheduler.BukkitRunnable;
    16.  
    17. public class Main extends JavaPlugin implements Listener {
    18.  
    19. public void onEnable() {
    20. this.getServer().getPluginManager().registerEvents(this, this);
    21. }
    22.  
    23.  
    24. public Location[] circleFrom(Location l) {
    25. List<Location> returnList = new ArrayList<Location>();
    26. for(int x = 2; x>=-2; x--){
    27. for(int z = 2; z>=-2; z--){
    28. for(int y = 0; y>=1; y++){
    29. if(!(Math.abs(z)==1&&x==0)||!(Math.abs(x)==1&&z==0)||x!=z){
    30. returnList.add(new Location(l.getWorld(), l.getX()-x, l.getY()+y, l.getZ()-z));
    31. }
    32. }
    33. }
    34. }
    35. return returnList.toArray(new Location[returnList.size()]);
    36. }
    37.  
    38. @EventHandler
    39. public void gandalf(PlayerInteractEvent e) {
    40. Player p = e.getPlayer();
    41. Action a = e.getAction();
    42. if (a == Action.RIGHT_CLICK_AIR || a == Action.RIGHT_CLICK_BLOCK) {
    43. if (p.getItemInHand().getType() == Material.STICK) {
    44.  
    45. }
    46.  
    47. Location location=p.getLocation();
    48. for(int i = 0; i<circleFrom(location).length; i++){
    49. Location l = circleFrom(location);
    50. Material changeTo = Material.SPONGE;//Change this to whatever you want, IDC. If you want to get the
    51. //Block under the location, create a loop that will go locations //if the locations block is air continue, if not break and use that one.
    52. new BukkitRunnable(){
    53. public void run(){
    54. l.getBlock().setType(changeTo);
    55. }
    56. }.runTaskLater(this, i*2);//i*20 = (i) seconds
    57. }
    58. new BukkitRunnable(){
    59. public void run(){
    60. for(Location l : circleFrom(location)){
    61. l.getBlock().setType(Material.AIR);
    62. }
    63. }.runTaskLater(this, 3.8);//2 seconds + 18 blocks that each takes 0.1
    64. }
    65. }
    66.  
    67. }
    68. }


    Error: Location l = circleFrom(location); - cannot convert from Location[] to Location
    Error: }.runTaskLater(this, 3.8); - syntax error on token ".", { expected'
    Error: l.getBlock().setType(changeTo); - Cannot refer to a non-final variable l inside an inner class defined in a different method
    Error: } - Syntax error, insert "}" to complete Statement

    When i insert more "}" it just give me more errors
     
  4. Offline

    Bobit

    Error #1: Line 49 say l is a location, but your method returns an array of locations.

    Error #2: In line 63, you can't have a delay of 3.8. In minecraft, all time is recorded in ticks, or 20ths of a second. You can't have a delay of 3.8 ticks. If you wanted a delay of 3.8 seconds, that number should be (3.8*20=) 76.

    Error #3: Because a runnable is actually a different class (or something like that), you can't modify variables from the class in it. Java wants you to promise that you won't by adding final to the beginning of line 50.

    Error #4: I really can't tell. I think you have too many }. Can't you tell us what line this was on?

    Error #5: You're not supposed to make a new thread on bukkit for the same problem.

    If you're planning on actually coding more plugins, you should be able to fix these errors on your own, at least the first one. That one was waaay too simple.
     
  5. Offline

    Stackore

    I'm nooby at this so i can't fix the first error i'm still new at this, 4th error is on line 64
     
  6. Offline

    Bobit

    Try inserting a } on line 62.


    Honestly though, you should take classes, or you'll run into stuff like the first error every time. I'm a noob too, but you have to understand the difference between location[] and location:

    location may be just one location.

    location[] contains some locations:
    location[1] may be a location. location[2] may be another location.

    same as the difference between int and int[], or string and string[], or nearly anything!
     
Thread Status:
Not open for further replies.

Share This Page