How can I shorten this code?

Discussion in 'Plugin Development' started by sohardhun15, Mar 30, 2014.

Thread Status:
Not open for further replies.
  1. Code:java
    1. public static ItemStack createPotion(Material material,
    2. PotionEffectType potionEffectType, int duration, int amplifier,
    3. boolean boo, Player p, boolean splash) {
    4. int asd = amplifier - 1;
    5. ItemStack i = new ItemStack(material);
    6. ItemMeta im = i.getItemMeta();
    7. im.setDisplayName(ChatColor.AQUA
    8. + potionEffectType.getName().toLowerCase() + " for " + duration
    9. + " secs, with " + asd + " level");
    10. i.setItemMeta(im);
    11. PotionMeta pm = (PotionMeta) i.getItemMeta();
    12. PotionEffect pe = new PotionEffect(potionEffectType, duration * 20,
    13. amplifier - 1);
    14. pm.addCustomEffect(pe, boo);
    15. i.setItemMeta(pm);
    16. Potion po = new Potion(PotionType.getByEffect(potionEffectType));
    17. po.setSplash(splash);
    18. po.apply(i);
    19. return i;
    20. }



    Code:java
    1. if (cmd.getName().equalsIgnoreCase("potion")) {
    2.  
    3. if (!(sender instanceof Player)) {
    4. sender.sendMessage(ChatColor.RED
    5. + "Only players can use this command!");
    6. return false;
    7. }
    8. Player p = (Player) sender;
    9. if (args.length < 4 || args.length > 4) {
    10. p.sendMessage(ChatColor.RED
    11. + "Use: /potion <Effect> <Duration> <Amplifier> <Splash>");
    12. return false;
    13. }
    14. try {
    15. Integer.parseInt(args[0]);
    16. Integer.parseInt(args[1]);
    17. Integer.parseInt(args[2]);
    18. Boolean.parseBoolean(args[3]);
    19. } catch (Exception e) {
    20. p.sendMessage(ChatColor.RED + "Something went wrong");
    21. return false;
    22. }
    23.  
    24. int i = Integer.parseInt(args[0]);
    25. int dur = Integer.parseInt(args[1]);
    26. int amp = Integer.parseInt(args[2]);
    27. boolean splash = Boolean.parseBoolean(args[3]);
    28.  
    29. if (dur < 1 || amp < 0) {
    30. p.sendMessage(ChatColor.RED
    31. + "Use: /potion <Effect> <Duration> <Amplifier> <Splash>");
    32. }
    33. switch (i) {
    34. case 1:
    35. p.getInventory().addItem(
    36. createPotion(Material.POTION,
    37. PotionEffectType.getById(i), dur, amp, true, p,
    38. splash));
    39. break;
    40. case 2:
    41. p.getInventory().addItem(
    42. createPotion(Material.POTION,
    43. PotionEffectType.getById(i), dur, amp, true, p,
    44. splash));
    45. break;
    46. case 3:
    47. p.getInventory().addItem(
    48. createPotion(Material.POTION,
    49. PotionEffectType.getById(i), dur, amp, true, p,
    50. splash));
    51. break;
    52. case 4:
    53. p.getInventory().addItem(
    54. createPotion(Material.POTION,
    55. PotionEffectType.getById(i), dur, amp, true, p,
    56. splash));
    57. break;
    58. case 5:
    59. p.getInventory().addItem(
    60. createPotion(Material.POTION,
    61. PotionEffectType.getById(i), dur, amp, true, p,
    62. splash));
    63. break;
    64. case 6:
    65. p.getInventory().addItem(
    66. createPotion(Material.POTION,
    67. PotionEffectType.getById(i), dur, amp, true, p,
    68. splash));
    69. break;
    70. case 7:
    71. p.getInventory().addItem(
    72. createPotion(Material.POTION,
    73. PotionEffectType.getById(i), dur, amp, true, p,
    74. splash));
    75. break;
    76. case 8:
    77. p.getInventory().addItem(
    78. createPotion(Material.POTION,
    79. PotionEffectType.getById(i), dur, amp, true, p,
    80. splash));
    81. break;
    82. case 9:
    83. p.getInventory().addItem(
    84. createPotion(Material.POTION,
    85. PotionEffectType.getById(i), dur, amp, true, p,
    86. splash));
    87. break;
    88. case 10:
    89. p.getInventory().addItem(
    90. createPotion(Material.POTION,
    91. PotionEffectType.getById(i), dur, amp, true, p,
    92. splash));
    93. break;
    94. case 11:
    95. p.getInventory().addItem(
    96. createPotion(Material.POTION,
    97. PotionEffectType.getById(i), dur, amp, true, p,
    98. splash));
    99. break;
    100. case 12:
    101. p.getInventory().addItem(
    102. createPotion(Material.POTION,
    103. PotionEffectType.getById(i), dur, amp, true, p,
    104. splash));
    105. break;
    106. case 13:
    107. p.getInventory().addItem(
    108. createPotion(Material.POTION,
    109. PotionEffectType.getById(i), dur, amp, true, p,
    110. splash));
    111. break;
    112. case 14:
    113. p.getInventory().addItem(
    114. createPotion(Material.POTION,
    115. PotionEffectType.getById(i), dur, amp, true, p,
    116. splash));
    117. break;
    118. case 15:
    119. p.getInventory().addItem(
    120. createPotion(Material.POTION,
    121. PotionEffectType.getById(i), dur, amp, true, p,
    122. splash));
    123. break;
    124. case 16:
    125. p.getInventory().addItem(
    126. createPotion(Material.POTION,
    127. PotionEffectType.getById(i), dur, amp, true, p,
    128. splash));
    129. break;
    130. case 17:
    131. p.getInventory().addItem(
    132. createPotion(Material.POTION,
    133. PotionEffectType.getById(i), dur, amp, true, p,
    134. splash));
    135. break;
    136. case 18:
    137. p.getInventory().addItem(
    138. createPotion(Material.POTION,
    139. PotionEffectType.getById(i), dur, amp, true, p,
    140. splash));
    141. break;
    142. case 19:
    143. p.getInventory().addItem(
    144. createPotion(Material.POTION,
    145. PotionEffectType.getById(i), dur, amp, true, p,
    146. splash));
    147. break;
    148. case 20:
    149. p.getInventory().addItem(
    150. createPotion(Material.POTION,
    151. PotionEffectType.getById(i), dur, amp, true, p,
    152. splash));
    153. break;
    154. case 21:
    155. p.getInventory().addItem(
    156. createPotion(Material.POTION,
    157. PotionEffectType.getById(i), dur, amp, true, p,
    158. splash));
    159. break;
    160. case 22:
    161. p.getInventory().addItem(
    162. createPotion(Material.POTION,
    163. PotionEffectType.getById(i), dur, amp, true, p,
    164. splash));
    165. break;
    166. case 23:
    167. p.getInventory().addItem(
    168. createPotion(Material.POTION,
    169. PotionEffectType.getById(i), dur, amp, true, p,
    170. splash));
    171. break;
    172. default:
    173. p.getInventory().addItem(new ItemStack(Material.POTION, 1));
    174. break;
    175. }
    176. }


    I get out the switch, now it looks like this:
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("potion")) {
    2.  
    3. if (!(sender instanceof Player)) {
    4. sender.sendMessage(ChatColor.RED
    5. + "Csak játékosok használhatják!");
    6. return false;
    7. }
    8. Player p = (Player) sender;
    9. if (args.length < 4 || args.length > 4) {
    10. p.sendMessage(ChatColor.RED
    11. + "Helyes használat: /potion <Effect> <Duration> <Amplifier> <Splash>");
    12. return false;
    13. }
    14. try {
    15. Integer.parseInt(args[0]);
    16. Integer.parseInt(args[1]);
    17. Integer.parseInt(args[2]);
    18. Boolean.parseBoolean(args[3]);
    19. } catch (Exception e) {
    20. p.sendMessage(ChatColor.RED + "Hiba");
    21. return false;
    22. }
    23.  
    24. int i = Integer.parseInt(args[0]);
    25. int dur = Integer.parseInt(args[1]);
    26. int amp = Integer.parseInt(args[2]);
    27. boolean splash = Boolean.parseBoolean(args[3]);
    28.  
    29. if (dur < 1 || amp < 0) {
    30. p.sendMessage(ChatColor.RED
    31. + "Helyes használat: /potion <Effect> <Duration> <Amplifier> <Splash>");
    32. }
    33. p.getInventory().addItem(
    34. createPotion(Material.POTION,
    35. PotionEffectType.getById(i), dur, amp, true, p,
    36. splash));
    37. }


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

    TheLunarFrog

    I don't really know what you're asking -- there aren't many things you can do to shorten code if you're already writing modular code.

    First off, a tip:
    Code:java
    1.  
    2. if (!(sender instanceof Player)) {
    3. sender.sendMessage(ChatColor.RED
    4. + "Csak játékosok használhatják!");
    5. return false;
    6. }

    You should not return false, but rather, always true. Read the documentation -- returning false means that the command was not recognized by the server, and will cause the server to send back one of these messages:
    • Command not recognized; type /help for help (or whatever it actually says)
    • /potion
    The same holds true for other conditions of the same idea.
    Next up:
    Why bother with this:

    Code:java
    1. try{
    2. Integer.parseInt(args[0]);
    3. Integer.parseInt(args[1]);
    4. Integer.parseInt(args[2]);
    5. Boolean.parseBoolean(args[3]);
    6. }catch(Exception e){
    7. p.sendMessage(ChatColor.RED + "Hiba");
    8. return false;
    9. }
    10.  
    11. int i = Integer.parseInt(args[0]);
    12. int dur = Integer.parseInt(args[1]);
    13. int amp = Integer.parseInt(args[2]);
    14. boolean splash = Boolean.parseBoolean(args[3]);


    When you could do this:
    Code:java
    1. int i = 0, dur = 0, amp = 0;
    2. boolean splash = false;
    3. try{
    4. i = Integer.parseInt(args[0]);
    5. dur = Integer.parseInt(args[1]);
    6. amp = Integer.parseInt(args[2]);
    7. splash = Boolean.parseBoolean(args[3]);
    8. }catch(Exception e){
    9. p.sendMessage(ChatColor.RED + "Hiba");
    10. return true;

    Additionally, I don't know if this is intended:
    Code:java
    1. if (dur < 1 || amp < 0) {
    2. p.sendMessage(ChatColor.RED
    3. + "Helyes használat: /potion <Effect> <Duration> <Amplifier> <Splash>");
    4. }

    You never return in this control statement. You may have intended to, because it seems you give the user an error message, but you still perform whatever operation your plugin intends on the line which proceeds this control struct.

    Edit: awesome formatting bugs...

    Edit 2:
    I noticed this, looking back:
    Code:java
    1. try {
    2. Integer.parseInt(args[0]);
    3. Integer.parseInt(args[1]);
    4. Integer.parseInt(args[2]);
    5. Boolean.parseBoolean(args[3]);
    6. } catch (Exception e) {
    7. p.sendMessage(ChatColor.RED + "Hiba");
    8. return false;
    9. }

    In your original post, you translated the catch block to say "Something went wrong." The something that went wrong is that something in the try block (this is not always the case, just this particular one) was formatted incorrectly -- i.e., for boolean, it was something other than 0, 1, true, false, yes, or no; while for the integers, it would be something that is less than -2,147,483,648 or larger than 2,147,483,647. This isn't necessarily a programming issue, but just some insight as to what actually happens.
     
    Stupeflip likes this.
  3. Offline

    AoH_Ruthless

    sohardhun15
    You can shorten your switch statement.
    Also, if they are all Material.POTION, why not remove the material parameter and just create the itemstack as a material.Potion?
    Code:java
    1. switch (blah) { // If the cases have the same return, you can do the following
    2. case 1:
    3. case 2:
    4. case 3:
    5. case 4: // etc...
    6. createPotion(/*PARAMAETERS*/);
    7. default:
    8. break;
    9. }
     
  4. Offline

    TheLunarFrog


    i.e. it was removed
     
  5. Offline

    AoH_Ruthless

Thread Status:
Not open for further replies.

Share This Page