Solved how to do this ?

Discussion in 'Plugin Development' started by karolis11234, Jul 6, 2014.

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

    karolis11234

    so i want to make a /enchant command but i dont know how to do some things.
    the current code is:
    if (cmd.getName().equalsIgnoreCase("enchant")) {
    if (args.length == 2) {
    int effect =
    int enchlevel =
    ItemStack item = new ItemStack(player.getInventory().getItemInHand());
    Enchantment ench1 = new EnchantmentWrapper(effect);
    item.addUnsafeEnchantment(ench1, enchlevel);
    return false;
    }
    }
    the question is how can i get so that when the player something like /enchant sharpness 5 would get the appropriate enchantment ?
     
  2. Offline

    JasonDL13

    Look into how to use Arrays. In an array if the length if 2 it has two data stored in it.

    args[0] is sharpness in /enchant sharpness 5
    args[1] is 5 in /enchant sharpness 5

    So if you look at the enchantment class it's an enum. And you can get enums from doing valueOf.

    So for example: Enchantment.valueOf(args[0])

    Be sure to do a try-catch statement because that might throw an error if it doesn't exist.
    And to get the level. Use Integer.parseInt(String);. That will convert a string into a int. But try-catch that to because that will throw a numberformatexecption if it isn't a number.

    One more thing. Sharpness in game code isn't called sharpness. It's called DAMAGE_ALL. So your players either have to type that or you have to make a way so if someone types Sharpness it enchants as DAMAGE_ALL.
     
  3. Offline

    karolis11234

    can you show me how you would use the Integer.parseInt(string); thing . this is my new code:
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("enchant")) {
    2. if (args.length == 2) {
    3. int effect = Enchantment.valueOf(args[0]);
    4. int enchlevel =
    5. ItemStack item = new ItemStack(player.getInventory().getItemInHand());
    6. Enchantment ench1 = new EnchantmentWrapper(effect);
    7. item.addUnsafeEnchantment(ench1, enchlevel);
    8. return false;
    9. }
    10. }

    the valueOf is red underlined btw
     
  4. Offline

    andrewgies17


    "Red underlined" means you are probably using and IDE like Eclipse. If you are using Eclipse, mouse over the little red "X" on the left hand side of the code editor and on the same line as the error. That will give you details about the error. Could you tell us what it's saying?
     
  5. Offline

    Necrodoom

    karolis11234 read bukkit Java docs about the enchantment object, and what do you not understand about parsing string to int exactly?
     
  6. Offline

    JasonDL13

    Well for one don't use effect as an int. Use it as Enchantment.

    Integer.parseInt(String); will convert a String to an int. And it will return a String. So make enchlevel equal to Integer.parseInt(args[1])

    And you need to do a try catch statement to prevent errors.

    Here's an example:

    try{
    //code
    }catch(Execption e) {
    Errors will be caut (I forgot how to spell the work)
    }

    So you would put your command code in the try area and if it fails and throws an execption. Message them something like it doesn't work.
     
  7. Offline

    ZanderMan9

    Integer.parseInt(String) is used to return the integer value of said string. Use it in place of an actual integer, like so:
    int integer = Integer.parseInt("23");
    Now integer == 23.
    You might want to use isInt to make sure someone doesn't screw it up...
     
  8. Offline

    karolis11234

    i now understand about parsing string to int thanks for JasonDL13.
    i edit my code now its:
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("enchant")) {
    2. if (args.length == 2) {
    3. Enchantment effect = Enchantment.valueOf(args[0]);
    4. int enchlevel = Integer.parseInt(args[1]);
    5. ItemStack item = new ItemStack(player.getInventory().getItemInHand());
    6. Enchantment ench1 = new EnchantmentWrapper(effect);
    7. item.addUnsafeEnchantment(ench1, enchlevel);
    8. return false;
    9. }
    10. }

    but valueOf is still underlined and new EnchantmentWrapper(effect); is underlined too
     
  9. Offline

    andrewgies17

    Like I said in my previous post. Please tell us what the actual error is. "It is underlined" doesn't give us any information to help you with.
     
  10. Offline

    xTigerRebornx

    JasonDL13 Enchantment isn't an enum (It looks like one, but isn't)
    karolis11234 JasonDL13 assumed it was an enum, though it isn't. Enchantment does have a getByName() method, try that. Also, what is this "EnchantmentWrapper", and why are you trying to use it when you already have the Enchantment? Simply apply the enchantment to the ItemStack, I don't see a need for this wrapper.
     
  11. Offline

    JasonDL13

    EDIT: after looking at the JavaDoc (and the help of xTigerRebornx)
    Enchantment isn't an enum. My bad. It does look like one though. Use getByName instead of valueOf to get the Enchantment object.

    Well I don't understand why you're using an EnchantmentWrapper. Just use the ItemStack method. I can't remeber what it is but you should be able to find it out. It's addEnchantment or something. And again, you need a try-catch block! If you don't have a try-catch block and theres an error, which is probably going to happen depending on what people type. Your command will not execute completely.

    Here's another example of a try catch block-

    Code:java
    1. String toBeParsed = "5";
    2. int integer;
    3.  
    4. try {
    5. integer = Integer.parseInt(toBeParsed);
    6. }catch(Execption e) {
    7. //toBeParsed wasn't a number.
    8. System.out.println("Error! Your String wasn't a number!")
    9. return;
    10. }
    11.  
    12. System.out.println("Complete! Your int object is: " + integer);


    The will convert the String into an int, catching all the errors.
     
  12. Offline

    karolis11234

    so now when it doesnt give me errors but when i use it ingame it sends me message: /enchant
    and also it doesnt enchant the item.
    EDIT:
    code here:
    Code:
            if (cmd.getName().equalsIgnoreCase("enchant")) {
                if (args.length == 2) {
                    Enchantment effect = Enchantment.getByName(args[0]);
                    int enchlevel = Integer.parseInt(args[1]);
                    ItemStack item = new ItemStack(player.getInventory().getItemInHand());
                    item.addUnsafeEnchantment(effect, enchlevel);
                    return false;
                }
            }
     
  13. Offline

    JasonDL13

    karolis11234 Is the whole command you did just "/enchant"? because you didn't code it to do anything if the args length isn't 2. Also no one can really help you if you don't post your code
     
  14. Offline

    Necrodoom

  15. Offline

    karolis11234

    i
    you want me to paste the whole code in my Main.java ?
     
  16. Offline

    Necrodoom

  17. Offline

    karolis11234

    here it is:
    Code:java
    1. package com.karolis.Random;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5.  
    6. import org.bukkit.Material;
    7. import org.bukkit.block.Block;
    8. import org.bukkit.block.Sign;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.enchantments.Enchantment;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.entity.ThrownExpBottle;
    14. import org.bukkit.event.EventHandler;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.block.Action;
    17. import org.bukkit.event.block.BlockBreakEvent;
    18. import org.bukkit.event.block.SignChangeEvent;
    19. import org.bukkit.event.player.PlayerInteractEvent;
    20. import org.bukkit.inventory.ItemStack;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22. import org.bukkit.potion.PotionEffect;
    23. import org.bukkit.potion.PotionEffectType;
    24.  
    25. public class Main extends JavaPlugin implements Listener {
    26. public final HashMap<Player, ArrayList<Block>> hashmap = new HashMap<Player, ArrayList<Block>>();
    27. public int number = 3;
    28.  
    29. @Override
    30. public void onEnable() {
    31. getLogger().info("Pasileido!");
    32. getServer().getPluginManager().registerEvents(this, this);
    33. }
    34.  
    35. @Override
    36. public void onDisable() {
    37. getLogger().info("Issijunge!");
    38. }
    39.  
    40. @SuppressWarnings("deprecation")
    41. @Override
    42. public boolean onCommand(CommandSender sender, Command cmd, String label,
    43. String[] args) {
    44. Player player = (Player) sender;
    45. if (cmd.getName().equalsIgnoreCase("explo")) {
    46. if (args.length == 1) {
    47. Player target = sender.getServer().getPlayer(args[0]);
    48. if (target == null) {
    49. sender.sendMessage(args[0] + " is not currently online.");
    50. return true;
    51. }
    52. float explosionPower = 50F;
    53. target.getWorld().createExplosion(target.getLocation(),
    54. explosionPower);
    55. return true;
    56. }
    57. }
    58. if (cmd.getName().equalsIgnoreCase("pirmas")) {
    59. int pirmas = 100;
    60. player.sendMessage("Parasiai /pirmas");
    61. player.giveExp(pirmas);
    62. return true;
    63. }
    64. if (cmd.getName().equalsIgnoreCase("hash")) {
    65. if (hashmap.containsKey(player)) {
    66. hashmap.remove(player);
    67. return true;
    68. } else {
    69. hashmap.put(player, null);
    70. return true;
    71. }
    72. }
    73. if (cmd.getName().equalsIgnoreCase("hashtest")) {
    74. if (hashmap.containsKey(player)) {
    75. player.sendMessage("You Are In The Hashmap!");
    76. return true;
    77. } else {
    78. player.sendMessage("You Are Not In The Hashmap!");
    79. return true;
    80. }
    81. }
    82. if (cmd.getName().equalsIgnoreCase("enchant")) {
    83. if (args.length == 2) {
    84. Enchantment effect = Enchantment.getByName(args[0]);
    85. int enchlevel = Integer.parseInt(args[1]);
    86. ItemStack item = new ItemStack(player.getInventory().getItemInHand());
    87. item.addUnsafeEnchantment(effect, enchlevel);
    88. return false;
    89. }
    90. }
    91. return false;
    92. }
    93.  
    94. @EventHandler
    95. public void onBreak(BlockBreakEvent event) {
    96. if (event.getBlock().getType() == Material.GRASS) {
    97. event.setCancelled(true);
    98. }
    99. }
    100.  
    101. @SuppressWarnings("deprecation")
    102. @EventHandler
    103. public void onInte(PlayerInteractEvent event) {
    104. Player player = event.getPlayer();
    105. @SuppressWarnings("unused")
    106. Block block = (Block) event.getClickedBlock();
    107. if (!(event.getAction() == Action.RIGHT_CLICK_BLOCK))
    108. return;
    109. if (event.getClickedBlock().getState() instanceof Sign) {
    110. Sign s = (Sign) event.getClickedBlock().getState();
    111. if (s.getLine(0).equalsIgnoreCase("[Heal]")) {
    112. player.addPotionEffect(new PotionEffect(
    113. PotionEffectType.INCREASE_DAMAGE, 20 * 120, 10));
    114. player.addPotionEffect(new PotionEffect(
    115. PotionEffectType.HEALTH_BOOST, 20 * 120, 10));
    116. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED,
    117. 20 * 120, 10));
    118. player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP,
    119. 20 * 120, 5));
    120. player.addPotionEffect(new PotionEffect(
    121. PotionEffectType.FAST_DIGGING, 20 * 120, 10));
    122. player.addPotionEffect(new PotionEffect(
    123. PotionEffectType.REGENERATION, 20 * 120, 10));
    124. player.addPotionEffect(new PotionEffect(
    125. PotionEffectType.FIRE_RESISTANCE, 20 * 120, 1));
    126. player.addPotionEffect(new PotionEffect(
    127. PotionEffectType.ABSORPTION, 20 * 120, 10));
    128. player.addPotionEffect(new PotionEffect(
    129. PotionEffectType.NIGHT_VISION, 20 * 120, 1));
    130. event.getPlayer().setHealth(20);
    131. event.getPlayer().setFoodLevel(20);
    132. event.getPlayer().sendMessage("You were healed and buffed!");
    133. }
    134. }
    135. if (player.getItemInHand().getType() == Material.STICK) {
    136. if (!player.hasPermission("random.staff")) {
    137. player.sendMessage("Neturi Leidimo!!");
    138. }
    139. player.launchProjectile(ThrownExpBottle.class);
    140. player.launchProjectile(ThrownExpBottle.class);
    141. player.launchProjectile(ThrownExpBottle.class);
    142. player.launchProjectile(ThrownExpBottle.class);
    143. player.launchProjectile(ThrownExpBottle.class);
    144. player.sendMessage("Paspaudiai medi!");
    145. }
    146.  
    147. if (player.getItemInHand().getType() == Material.BLAZE_ROD) {
    148. if (!player.hasPermission("random.rod")) {
    149. player.sendMessage("Neturi Leidimo!!");
    150. } else {
    151. for (int i = 0; i <= 10; i++) {
    152. player.launchProjectile(ThrownExpBottle.class);
    153. player.launchProjectile(ThrownExpBottle.class);
    154. player.launchProjectile(ThrownExpBottle.class);
    155. player.launchProjectile(ThrownExpBottle.class);
    156. player.launchProjectile(ThrownExpBottle.class);
    157. player.sendMessage("!");
    158. }
    159. }
    160. }
    161. }
    162.  
    163. @EventHandler
    164. public void signchange(SignChangeEvent event) {
    165.  
    166. }
    167. }
    168.  

    theres a lot of random things because im only learning to code bukkit.
     
  18. Offline

    Necrodoom

    karolis11234 you are returning false on a successful run of the enchant command, hence why it returns the syntax.
     
  19. Offline

    karolis11234

    so it doesnt return the syntax now but it still doesnt enchant the item
     
  20. Offline

    JasonDL13

  21. Offline

    karolis11234

    /enchant DAMAGE_ALL 5
     
  22. Offline

    JasonDL13

    I actually just saw what your error is. You're creating a new ItemStack from the item in hand. Instead of just getting it. And you're applying an Enchantment to the Item you just created, and doing nothing with it. Use addEnchantment to p.getItemInHand(). That will enchant the item​
     
  23. Offline

    karolis11234

Thread Status:
Not open for further replies.

Share This Page