name and enchant a stick (help)

Discussion in 'Plugin Development' started by RebzO1, Jan 24, 2014.

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

    RebzO1

    OK i had a little help with an error i had doing this (only my second plugin)

    here is what i have so far:
    Code:java
    1. package me.rebz.snowball;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.EventPriority;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12. import org.bukkit.entity.Snowball;
    13.  
    14. public class Main extends JavaPlugin implements Listener {
    15. public void onEnable(){
    16. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    17. }
    18.  
    19. public Main Main;
    20.  
    21. @EventHandler(ignoreCancelled = false, priority = EventPriority.MONITOR)
    22. public void onStick(PlayerInteractEvent event){
    23. Player player = event.getPlayer();
    24.  
    25. if(player.getInventory().getItemInHand().getType() == Material.STICK){
    26. if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
    27. player.launchProjectile(Snowball.class);
    28. }
    29. }
    30.  
    31. }
    32.  
    33. }


    do i had an idea and i know how to do some of it:

    What i want to do is, when a player types in /snowgun it will give them an enchanted (knockback II stick named idk.... lets go with 'snow gun'

    This way a normal stick wouldn't launch snowballs i can do the command part as far as.. command gives them the stick.

    How would i go about doing this the naming and enchanting and normal sticks not being 'snow gun'
     
  2. Offline

    CraftBang

    @Rebz01 you can give the ItemStack a custom name, and you can do ItemStack.addUnsafeEnchantments
    (Since adding it so a stick is not legit)

    Than make a check on the interact part, the name and enchants.
     
  3. Offline

    RebzO1

    added the part where i need to type in a command to get the stick (i did google assistance with the name & enchant before posting here)

    Code:java
    1. package me.rebz.snowball;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.EventPriority;
    12. import org.bukkit.event.block.Action;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.entity.Snowball;
    15.  
    16. public class Main extends JavaPlugin implements Listener {
    17. public void onEnable(){
    18. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    19. }
    20.  
    21. public Main Main;
    22. public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args){
    23. if(CommandLabel.equalsIgnoreCase("snowgun")){
    24. if (!(sender instanceof Player)){
    25. sender.sendMessage("Don't be a noob you're not a player!");
    26. return true;
    27. }
    28. else{
    29.  
    30. @EventHandler(ignoreCancelled = false, priority = EventPriority.MONITOR)
    31. public void onStick(PlayerInteractEvent event){
    32. Player player = event.getPlayer();
    33.  
    34. if(player.getInventory().getItemInHand().getType() == Material.STICK){
    35. if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
    36. player.launchProjectile(Snowball.class);
    37. }
    38. }
    39.  
    40. }
    41.  
    42. }


    Although on the else 'else{' the '{' is flagged:
    Syntax error, insert "}" to complete Block
    Syntax error, insert "}" to complete MethodBody
    Syntax error, insert "}" to complete Statement
    Syntax error, insert "else Statement" to complete BlockStatements

    So lol clearly im missing something there too.
     
  4. Offline

    Mrawesomecookie

    @Rebz01
    Tips:
    1. Why would you use the MONITOR priority?
    2. No need to have the "ignorecancelled = false".
    3. Wheres your code for giving the snowgun?
    4. Please learn proper grammer, kthx.
     
  5. Offline

    RebzO1

    I'm not sure how to do this the knockback isn't important just yet though, a stick that isn't a gun shouldn't throw snowballs is my first hurdle


    1. one of my friends told me to add that line, i know it works without
    2. removed the whole line. ty
    3. code is added in the second paste
    4. off topic and unnecessary (grammar*)

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

    Mrawesomecookie

    RebzO1
    1. So what are you doing to give the player the snowgun?
    2. Do you even know what that line does?
     
  7. Offline

    RebzO1

    Mrawesomecookie
    1. player will type /snowgun to get it
    2. no clue i assumed he knew lol

    As i have no prior plugin/coding experience i threw what i have together by watching and reading bukkit tuts
     
  8. Offline

    Mrawesomecookie

    Then you probably should not be making Bukkit plugins right now. Try watching a good tutorial series on Bukkit.
     
  9. Offline

    RebzO1

    Mrawesomecookie
    Yes maybe so
    But who are you to tell anyone they shouldn't do something...
    I learn by creating smaller projects and work up.

    If i need help i am not afraid to ask.
    This is the second time you posted something pointless on my thread, if you have no intention of offering advice pls get your post count up elsewhere.

    I still have the issue of not knowing how to name a stick 'snowgun' and making it so that a normal stick does not throw snowballs

    this is what i have so far:
    Code:java
    1. package me.rebz.snowball;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.EventPriority;
    12. import org.bukkit.event.block.Action;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.entity.Snowball;
    15.  
    16. public class Main extends JavaPlugin implements Listener {
    17. public void onEnable(){
    18. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    19. }
    20.  
    21. public Main Main;
    22. public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args){
    23. if(CommandLabel.equalsIgnoreCase("snowgun")){
    24. if (!(sender instanceof Player)){
    25. sender.sendMessage("Don't be a noob you're not a player!");
    26. return true;
    27. }
    28. else{
    29.  
    30. public void onStick(PlayerInteractEvent event){
    31. Player player = event.getPlayer();
    32.  
    33. if(player.getInventory().getItemInHand().getType() == Material.STICK){
    34. if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
    35. player.launchProjectile(Snowball.class);
    36. }
    37. }
    38. }
    39. }


    still have this issue too:
    the 'else{' the '{' is flagged:
    Syntax error, insert "}" to complete Block
    Syntax error, insert "}" to complete MethodBody
    Syntax error, insert "}" to complete Statement
    Syntax error, insert "else Statement" to complete BlockStatements

    any help or positive criticism would be appreciated
    regards
    rebz

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

    Mrawesomecookie

    RebzO1
    If you want to change the item's name use something like this:
    Code:java
    1. is.getItemMeta().setDisplayName(ChatColor.RED + "SnowGun");

    Also, I have not looked at my post count since I signed up to this forum.
     
  11. Offline

    RebzO1

    Mrawesomecookie ok i think i added that in the correct place

    Mrawesomecookie
    i still don't know where i went wrong the syntax error is stopping me compiling the plugin to test
    the 'else{' the '{' is flagged:
    Syntax error, insert "}" to complete Block
    Syntax error, insert "}" to complete MethodBody
    Syntax error, insert "}" to complete Statement
    Syntax error, insert "else Statement" to complete BlockStatements


    i have tried putting } in various places without any luck

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

    MOMOTHEREAL

    You are putting an event in a command method, without putting @EventHandler.
    So you should add a } on line 28 (last code), and also @EventHandler before the onStick method.
     
  13. Offline

    RebzO1

    ok ill try that now

    MOMOTHEREAL
    current code:
    Code:java
    1. package me.rebz.snowball;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Material;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.EventPriority;
    13. import org.bukkit.event.block.Action;
    14. import org.bukkit.event.player.PlayerInteractEvent;
    15. import org.bukkit.entity.Snowball;
    16. import org.bukkit.inventory.ItemStack;
    17.  
    18. public class Main extends JavaPlugin implements Listener {
    19. public void onEnable(){
    20. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    21. }
    22.  
    23. public Main Main;
    24. public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args){
    25. if(CommandLabel.equalsIgnoreCase("snowgun")){
    26. if (!(sender instanceof Player)){
    27. sender.sendMessage("Don't be a noob you're not a player!");
    28. return true;
    29. }
    30. }
    31. else{
    32.  
    33. @EventHandler public void onStick(PlayerInteractEvent event){
    34. Player player = event.getPlayer();
    35.  
    36. if(player.getInventory().getItemInHand().getType() == Material.STICK){
    37. if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
    38. ItemStack is;
    39. is.getItemMeta().setDisplayName(ChatColor.RED + "SnowGun");
    40. player.launchProjectile(Snowball.class);
    41. }
    42. }
    43. }
    44. }


    still getting errors on that { after else though
    - Syntax error, insert "}" to complete Block
    - Syntax error, insert "}" to complete MethodBody


    *facedesk*

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

    HyrulesLegend

    Mrawesomecookie RebzO1 Stop. One, don't watch TheBCBroz, they teach bad and outdated methods, also, learn Java first, before you learn Bukkit.
     
  15. Offline

    xGamingDudex

    Replace:
    Code:
     public Main Main;
            public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args){
            if(CommandLabel.equalsIgnoreCase("snowgun")){
                if (!(sender instanceof Player)){
                    sender.sendMessage("Don't be a noob you're not a player!");
                    return true;
                }
                else{
    With:
    Code:
     public Main Main;
            public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args){
            if(CommandLabel.equalsIgnoreCase("snowgun")){
                if (!(sender instanceof Player)){
                    sender.sendMessage("Don't be a noob you're not a player!");
                    return true;
                } else{
                    Player p = (Player) sender;
                    ItemStack stick = new ItemStack(Material.STICK);
                    ItemMeta im = stick.getItemMeta();
                    im.setDisplayName("Snow Gun");
                    stick.setItemMeta(im);
                    stick.addEnchantment(Enchantment.KNOCKBACK, 2);
                    p.setItemInHand(stick);
    }
     
  16. Offline

    RebzO1

    i can see what your code is doing however the addUnsafeEnchantment is flagged and when i click quickfix it says remove unsafe then its flagged again saying put unsafe back.

    however i really appreciate your help this is the biggest step forward all day lol

    xGamingDudex
    forgot tahg for post above

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

    xGamingDudex

    Sorry, my bad, just use the unsafe enchantment as this will ignore checking if the item is actually fit the enchantment. I'm using 1.74 R0.2 and doesn't get any flags, but if it tells you that it is deprecated or something like that then just ignore it, as long as it runs fine you shouldn't have to worry
     
  18. Offline

    RebzO1

    xGamingDudex
    No need for sorry i just learned somethin from you so even though im still stuck i can actually understand your changes.

    stick.UnsafeEnchantment(Enchantment.KNOCKBACK, 2);

    The method UnsafeEnchantment(Enchantment, int) is undefined for the type ItemStack

    thats the error i get xGamingDudex
     
  19. Offline

    xGamingDudex

    are you importing org.bukkit.inventory.ItemStack?
     
  20. Offline

    RebzO1

    xGamingDudex

    yes, when i comment out that line about enchantments i can type /snowgun and i get a stick that fires snowballs
    unfortunately lol a normal stick does the same.

    but yes org.bukkit.inventory.ItemStack is there

    Code:java
    1. package me.rebz.snowball;
    2.  
    3. //import net.minecraft.server.v1_7_R1.Enchantment;
    4. import org.bukkit.plugin.java.JavaPlugin;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.Bukkit;
    8. //import org.bukkit.ChatColor;
    9. import org.bukkit.Material;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.EventHandler;
    13. //import org.bukkit.event.EventPriority;
    14. import org.bukkit.event.block.Action;
    15. import org.bukkit.event.player.PlayerInteractEvent;
    16. import org.bukkit.entity.Snowball;
    17. import org.bukkit.inventory.ItemStack;
    18. import org.bukkit.inventory.meta.ItemMeta;
    19.  
    20. public class Main extends JavaPlugin implements Listener {
    21. public void onEnable(){
    22. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    23. }
    24.  
    25. public Main Main;
    26. public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args){
    27. if(CommandLabel.equalsIgnoreCase("snowgun")){
    28. if (!(sender instanceof Player)){
    29. sender.sendMessage("Don't be a noob you're not a player!");
    30. return true;
    31. } else{
    32. Player player = (Player) sender;
    33. ItemStack stick = new ItemStack(Material.STICK);
    34. ItemMeta im = stick.getItemMeta();
    35. im.setDisplayName("SnowGun");
    36. stick.setItemMeta(im);
    37. // snowgun.addUnsafeEnchantment(Enchantment.KNOCKBACK, 2);
    38. player.setItemInHand(stick);
    39. }
    40. }
    41. return false;
    42. }
    43. @EventHandler public void onStick(PlayerInteractEvent event){
    44. Player player = event.getPlayer();
    45.  
    46. if(player.getInventory().getItemInHand().getType() == Material.STICK){
    47. if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
    48. player.launchProjectile(Snowball.class);
    49. }
    50. }
    51. }
    52. }


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

    MOMOTHEREAL

    stick.UnsafeEnchantment(Enchantment.KNOCKBACK, 2);
    has to be:
    stick.addUnsafeEnchantment(Enchantment.KNOCKBACK, 2);

    Also, I strongly suggest to learn more Java before doing all of this.
     
  22. Offline

    Wizehh

    I reccomend practicing by starting off small, making plugins for the basic commands "/teleport", "/heal", etc, and learning how to use arraylists and hashmaps; once you've mastered all that, you can start making more complex plugins.
    Well, you don't have to do that, that's just my reccomendation :)
     
  23. Offline

    RebzO1

    ok we have it working
    we have an enchanted stick called snowgun that fires snowballzzzz :D
    thank you guys
    error was fixed by adding @eventhandler

    Wizehh

    i am in complete agreement i think i bit off more than i can chew here, making a stick fire snowballs was kinda easy but all the stuff i wanted to add was a bit much,

    ill be taking a step backwards now and make sure i understand what im doing as im doing it..
    the good thing abouth this thread is i understand why it now works and not what i was doing wrong but what i had missed.

    thanks again guys
    RebzO1

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

    Wizehh

    Awesome, really glad I could help!
     
  25. Offline

    RebzO1

    Wizehh
    the only problem im having is a stick still fires snowballs whereas the snowgun has knockback2 and fires snowballs
    the normal sticks shouldn't do anything
     
  26. Offline

    Wizehh

    Use an if statement to check the display name/item meta of the item.
     
  27. Offline

    RebzO1

    Wizehh i will look into that tommoz you have been a great help and myself and the players waiting for this appreciate it!

    i have had too much to drink to see which line i type on at the moment :D
     
  28. Offline

    Wizehh

    Sure, no problem.
     
  29. Offline

    RebzO1

    Wizehh

    plugin works as needed now :)

    next projext starts now :D
     
  30. Offline

    Wizehh

Thread Status:
Not open for further replies.

Share This Page