TP and receive item?

Discussion in 'Plugin Development' started by Famous Guy, Nov 2, 2013.

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

    Famous Guy

    I'm trying to make a plugin where some one does /Spawn there invenotry gets cleared and gets TPed to spawn and receives a compass. And so the item can have a custom name and lore
    What I have:
    Code:java
    1. package me.Famous_Guy2.Spawn;
    2.  
    3.  
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.inventory.ItemStack;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Spawn extends JavaPlugin {
    14.  
    15. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] arsg) {
    16. if (!(sender instanceof Player)) {
    17. sender.sendMessage(ChatColor.RED + "Wat");
    18. return true;
    19. }
    20.  
    21. Player p = (Player) sender;
    22.  
    23. if (commandLabel.equalsIgnoreCase("spawn")) {
    24. org.bukkit.inventory.PlayerInventory pi = p.getInventory();
    25. pi.addItem(new ItemStack(Material.COMPASS, 1));
    26.  
    27.  
    28. return false;
    29. }
    30.  
    31. if (commandLabel.equalsIgnoreCase("clearme")) {
    32.  
    33. }
    34. return true;
    35. }
    36.  
    37. }
    38.  
     
  2. Online

    timtower Administrator Administrator Moderator

    Famous Guy I use this class to rename stuff and add lores:
    Code:java
    1.  
    2. import java.util.ArrayList;
    3. import java.util.Arrays;
    4. import java.util.List;
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.inventory.ItemStack;
    7. import org.bukkit.inventory.meta.ItemMeta;
    8.  
    9. public class LoreAPI
    10. {
    11. /**
    12. * Adds lore to the ItemStack
    13. *
    14. * @param itemStack
    15. * ItemStack to add lore to
    16. * @param lore
    17. * Lore to add to ItemStack
    18. * @return lored ItemStack
    19. */
    20. public ItemStack addLore(final ItemStack itemStack, final String lore)
    21. {
    22. ItemStack ret = itemStack;
    23. List<String> loreList = getLore(ret);
    24. loreList.add(lore);
    25. return setLore(ret, loreList);
    26. }
    27.  
    28. /**
    29. * @return all items that the plugin can use
    30. */
    31. /**
    32. * Gets the lore of an ItemStack
    33. *
    34. * @param itemStack
    35. * ItemStack to get the lore of
    36. * @return the lore of an ItemStack
    37. */
    38. public List<String> getLore(final ItemStack itemStack)
    39. {
    40. ItemStack ret = itemStack;
    41. ItemMeta itemMeta;
    42. if (ret.hasItemMeta())
    43. {
    44. itemMeta = ret.getItemMeta();
    45. }
    46. else
    47. {
    48. itemMeta = Bukkit.getItemFactory().getItemMeta(ret.getType());
    49. }
    50. if (itemMeta.hasLore())
    51. return itemMeta.getLore();
    52. return new ArrayList<String>();
    53. }
    54.  
    55. /**
    56. * Gets the name of an ItemStack
    57. *
    58. * @param itemStack
    59. * ItemStack to get the lore of
    60. * @return the lore of an ItemStack
    61. */
    62. public String getName(final ItemStack itemStack)
    63. {
    64. ItemStack ret = itemStack;
    65. ItemMeta itemMeta;
    66. if (ret.hasItemMeta())
    67. {
    68. itemMeta = ret.getItemMeta();
    69. }
    70. else
    71. {
    72. itemMeta = Bukkit.getItemFactory().getItemMeta(ret.getType());
    73. }
    74. if (itemMeta.hasDisplayName())
    75. return itemMeta.getDisplayName();
    76. String unfName = ret.getType().name();
    77. String[] split = unfName.split("_");
    78. String fName = new String();
    79. for (String s : split)
    80. {
    81. String firstLetter = s.substring(0, 1);
    82. String restOfWord = s.substring(1, s.length());
    83. String newName = firstLetter.toUpperCase()
    84. + restOfWord.toLowerCase();
    85. fName = fName + newName + " ";
    86. }
    87. return fName;
    88. }
    89.  
    90. /**
    91. * Replace a line of lore with another line
    92. *
    93. * @param tool
    94. * Tool to replace lore on
    95. * @param toReplace
    96. * Line of lore to be replaced
    97. * @param replaceWith
    98. * Line replacing toReplace
    99. * @return Tool with new lore
    100. */
    101. public ItemStack replaceLore(final ItemStack tool, final String toReplace,
    102. final String replaceWith)
    103. {
    104. ItemMeta meta = tool.getItemMeta();
    105. List<String> loreList = meta.getLore();
    106. if ((loreList == null) || loreList.isEmpty())
    107. return tool;
    108. for (String s : meta.getLore())
    109. if (s.equals(toReplace))
    110. {
    111. loreList.remove(s);
    112. loreList.add(replaceWith);
    113. }
    114. meta.setLore(loreList);
    115. tool.setItemMeta(meta);
    116. return tool;
    117. }
    118.  
    119. /**
    120. * Sets the lore of an ItemStack
    121. *
    122. * @param itemStack
    123. * ItemStack to set lore for
    124. * @param lore
    125. * Lore to give to the ItemStack
    126. * @return lored ItemStack
    127. */
    128. public ItemStack setLore(final ItemStack itemStack, final List<String> lore)
    129. {
    130. ItemStack ret = itemStack;
    131. ItemMeta itemMeta;
    132. if (ret.hasItemMeta())
    133. {
    134. itemMeta = ret.getItemMeta();
    135. }
    136. else
    137. {
    138. itemMeta = Bukkit.getItemFactory().getItemMeta(ret.getType());
    139. }
    140. itemMeta.setLore(lore);
    141. ret.setItemMeta(itemMeta);
    142. return ret;
    143. }
    144.  
    145. /**
    146. * Sets the lore of an ItemStack
    147. *
    148. * @param itemStack
    149. * ItemStack to set lore for
    150. * @param lore
    151. * Lore to give to the ItemStack
    152. * @return lored ItemStack
    153. */
    154. public ItemStack setLore(final ItemStack itemStack, final String... lore)
    155. {
    156. ItemStack ret = itemStack;
    157. ItemMeta itemMeta;
    158. if (ret.hasItemMeta())
    159. {
    160. itemMeta = ret.getItemMeta();
    161. }
    162. else
    163. {
    164. itemMeta = Bukkit.getItemFactory().getItemMeta(ret.getType());
    165. }
    166. itemMeta.setLore(Arrays.asList(lore));
    167. ret.setItemMeta(itemMeta);
    168. return ret;
    169. }
    170.  
    171. /**
    172. * Sets the name of an ItemStack
    173. *
    174. * @param itemStack
    175. * ItemStack to set name for
    176. * @param name
    177. * Name to give to the ItemStack
    178. * @return named ItemStack
    179. */
    180. public ItemStack setName(final ItemStack itemStack, final String name)
    181. {
    182. ItemStack ret = itemStack;
    183. ItemMeta itemMeta;
    184. if (ret.hasItemMeta())
    185. {
    186. itemMeta = ret.getItemMeta();
    187. }
    188. else
    189. {
    190. itemMeta = Bukkit.getItemFactory().getItemMeta(ret.getType());
    191. }
    192. itemMeta.setDisplayName(name);
    193. ret.setItemMeta(itemMeta);
    194. return ret;
    195. }
    196. }
    197.  
     
  3. Offline

    Famous Guy

    What about to clear your inv and tp to spawn?
     
  4. Online

    timtower Administrator Administrator Moderator

    Code:java
    1. public void teleportToSpawn(Player player){
    2. Location spawn = player.getLocation().getWorld().getSpawnLocation();
    3. player.teleport(spawn);
    4. }
    5.  
    6. public void clearInventory(Player player){
    7. player.getInventory().clear();
    8. }
     
  5. Offline

    Famous Guy

    Is this right?
    Code:java
    1. package me.Famous_Guy2.Spawn;
    2.  
    3. import javax.tools.JavaFileManager.Location;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.inventory.ItemStack;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Spawn extends JavaPlugin {
    13.  
    14. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] arsg) {
    15. if (!(sender instanceof Player)) {
    16. sender.sendMessage(ChatColor.RED + "Wat");
    17. return true;
    18. }
    19.  
    20. public void teleportToSpawn(Player player){
    21. Location spawn = player.getLocation().getWorld().getSpawnLocation();
    22. player.teleport(spawn);
    23. }
    24.  
    25. public void clearInventory(Player player){
    26. player.getInventory().clear();
    27. }
    28.  
    29. Player p = (Player) sender;
    30.  
    31. if (commandLabel.equalsIgnoreCase("spawn")){
    32. org.bukkit.inventory.PlayerInventory pi = p.getInventory();
    33. pi.addItem(new ItemStack(Material.COMPASS, 1));
    34.  
    35.  
    36. return false;
    37. }
    38.  
    39. if (commandLabel.equalsIgnoreCase("clearme")) {
    40.  
    41. }
    42. return true;
    43. }
    44.  
    45. }
    46.  
     
  6. Offline

    Blah1

    Well you need to call the methods withing the command check and you haven't added the lore or display name. Also, why are you making a method for one line of code? Just put it within if cmd == spawn
     
  7. Offline

    iBCoLLiN

    Why not just use an ArrayList for the lore's?
     
Thread Status:
Not open for further replies.

Share This Page