Solved setItem() syntax error in Eclipse?

Discussion in 'Plugin Development' started by ccrama, Oct 20, 2013.

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

    ccrama

    Hello Bukkit,

    I keep getting this syntax error in eclipse from setItem. I set the two strings in set item (location and item) to variables, but replacing the variables with said content doesn't get a better result.

    Any help is appreciated!

    Code:java
    1. World world = Bukkit.getWorld("world");
    2. Location loc = new Location(world, -79.559, 78, -36.67);
    3. ItemStack item = new ItemStack(Material.WOOL, 1, (byte)11);
    4. world.setItem(loc, item);
    5. Location loc2 = new Location(world, -79.87, 78, -72.54);
    6. ItemStack item2 = new ItemStack(Material.WOOL, 1, (byte)14);
    7. world.setItem(loc2, item2);


    The error is on the () of world.setItem()
     
  2. Offline

    Cr4zy[Box]

    What are you trying to do? I m new in this plugin things but i think that if u are trying to replace or add a block to the world u might be doing it wrong.
     
  3. Offline

    ccrama

    Yes, I am trying to add a fallen block to the world (Wool)
     
  4. Offline

    Cr4zy[Box]

    Why dont u just do like that:

    Code:java
    1. event.getPlayer().getWorld().spawnFallingBlock(Block.getLocation(), new ItemStack(Material.WOOL), (byte) 0);
     
  5. Offline

    ccrama

    Because it is not tied to a player. It will be an arena handler, when the game begins, wool blocks will spawn at the two team targets. But I think
    world.spawnFallingBlock(loc, item); would work, unless my variables are messing it up...
     
  6. Offline

    RealDope

    Are you trying to spawn a Falling Block? Or an ItemStack laying on the ground?
     
  7. Offline

    ccrama

    Item stack lying on the ground RealDope
     
  8. Offline

    xTrollxDudex

    ccrama
    PHP:
    loc.getWorld().dropItem(locitem)
     
  9. Offline

    ccrama

    Thanks xTrollxDudex


    (and Haha)


    EDIT: I get a new syntax error when I use xTrollxDudex 's method. I am thinking it's a variable problem. This is what I now have:

    Code:java
    1. Location loc = new Location(Bukkit.getWorld("world"), -79.559, 78, -36.67);
    2. ItemStack item = new ItemStack(Material.WOOL, 1, (byte)11);
    3. loc.getWorld().dropItem(loc, item);
    4. Location loc2 = new Location(Bukkit.getWorld("world"), -79.87, 78, -72.54);
    5. ItemStack item2 = new ItemStack(Material.WOOL, 1, (byte)14);
    6. loc2.getWorld().dropItem(loc2, item2);
     
  10. Offline

    xTrollxDudex

    ccrama
    What's the syntax error?
     
  11. Offline

    ccrama

    xTrollxDudex
    Identifier expected after this token on getWorld(). . Also says { expected after the period... I can't figure this one out!
     
  12. Offline

    xTrollxDudex

    ccrama
    Can I see everything in the class?
     
  13. Offline

    ccrama

    This is the class so far. I haven't done much to it since I can't get items to spawn

    Code:java
    1. package me.ccrama.MineFortress2;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Location;
    5. import org.bukkit.Material;
    6. import org.bukkit.World;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.world.WorldEvent;
    9. import org.bukkit.inventory.ItemStack;
    10.  
    11. public class Arena {
    12. Location loc = new Location(Bukkit.getWorld("world"), -79.559, 78, -36.67);
    13. ItemStack item = new ItemStack(Material.WOOL, 1, (byte)11);
    14. loc.getWorld().dropItem(loc, item);
    15. Location loc2 = new Location(Bukkit.getWorld("world"), -79.87, 78, -72.54);
    16. ItemStack item2 = new ItemStack(Material.WOOL, 1, (byte)14);
    17. loc2.getWorld().dropItem(loc2, item2);
    18.  
    19.  
    20. }
    21. }
     
  14. Offline

    xTrollxDudex

    ccrama
    Well you do have too many ending braces...
     
  15. Offline

    ccrama

    xTrollxDudex Eclipse told me to... IDK what's up with it.

    xTrollxDudex It says there is a { expected after the period after getWorld(), and there is still an error with the getWorld method... [​IMG]

    I hate to do this, but bump. I can't really continue my plugin until I get this item drop problem figured out. I edited the code to create a variable for world, because the new error was on getWorld, but now I get an error on the () saying Syntax error on "(", delete this token.

    Code:java
    1. package me.ccrama.MineFortress2;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Location;
    5. import org.bukkit.Material;
    6. import org.bukkit.World;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.world.WorldEvent;
    9. import org.bukkit.inventory.ItemStack;
    10.  
    11. public class Arena {
    12. Location loc = new Location( Bukkit.getWorld("world"), -79.559, 78, -36.67);
    13. ItemStack item = new ItemStack(Material.WOOL, 1, (byte)11);
    14. World w = Bukkit.getWorld("world");
    15. w.dropItem(loc, item);
    16. Location loc2 = new Location(Bukkit.getWorld("world"), -79.87, 78, -72.54);
    17. ItemStack item2 = new ItemStack(Material.WOOL, 1, (byte)14);
    18. w.dropItem(loc2, item2);
    19.  
    20.  
    21. }


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

    Minecrell

    ccrama
    You need to put that into a method... Like
    Code:java
    1. public void doSomething() {
    2. // your code
    3. }

    Then just call the method if you need it.
     
  17. Offline

    Pew446

    I'm sorry, this has nothing to do with the topic, but your post says it was edited 6,969 times, and I'm dying of laughter. Thanks for inadvertently making my day :)


    Your code should be in a method! You're simply placing it within the class, with no method wrapper around it. Try this:

    Code:java
    1.  
    2. package me.ccrama.MineFortress2;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.Location;
    6. import org.bukkit.Material;
    7. import org.bukkit.World;
    8. import org.bukkit.inventory.ItemStack;
    9.  
    10. public class Arena {
    11.  
    12. public Arena()
    13. {
    14. Location loc = new Location( Bukkit.getWorld("world"), -79.559, 78, -36.67);
    15. ItemStack item = new ItemStack(Material.WOOL, 1, (byte)11);
    16. World w = Bukkit.getWorld("world");
    17. w.dropItem(loc, item);
    18. Location loc2 = new Location(Bukkit.getWorld("world"), -79.87, 78, -72.54);
    19. ItemStack item2 = new ItemStack(Material.WOOL, 1, (byte)14);
    20. w.dropItem(loc2, item2);
    21. }
    22.  
    23. }



    This type of method is called a constructor, by the way. It would be called when you say something like:

    Arena newArena = new Arena();

    If you wanted it to be an actual method you can call, like Arena.doStuff(); You would do something like this:

    Code:java
    1.  
    2. package me.ccrama.MineFortress2;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.Location;
    6. import org.bukkit.Material;
    7. import org.bukkit.World;
    8. import org.bukkit.inventory.ItemStack;
    9.  
    10. public class Arena {
    11.  
    12. public void doStuff()
    13. {
    14. Location loc = new Location( Bukkit.getWorld("world"), -79.559, 78, -36.67);
    15. ItemStack item = new ItemStack(Material.WOOL, 1, (byte)11);
    16. World w = Bukkit.getWorld("world");
    17. w.dropItem(loc, item);
    18. Location loc2 = new Location(Bukkit.getWorld("world"), -79.87, 78, -72.54);
    19. ItemStack item2 = new ItemStack(Material.WOOL, 1, (byte)14);
    20. w.dropItem(loc2, item2);
    21. }
    22.  
    23. }


    and you would use it like this:

    Arena newArena = new Arena();
    newArena.doStuff();

    If you were to add "static" before "void" you could also do:

    Arena.doStuff();

    Good Luck! :)
     
  18. Offline

    ccrama

    Minecrell and Pew446 thanks so much! I totally overlooked that :3. Works great now!
     
    Pew446 likes this.
  19. Offline

    Minecrell

    He didn't edit his post.. :eek:
     
  20. Offline

    ccrama

    Trolldudex did haha
     
  21. Offline

    Pew446

    Oh god it's his signature.. That is the absolute best signature on earth. Guess I got trolled ;) Made my day tho!
     
    xTrollxDudex, Minecrell and ccrama like this.
  22. Offline

    Minecrell

    Lol, and I have signatures turned off, so I didn't see it! :p
     
Thread Status:
Not open for further replies.

Share This Page