-= Can't seem to get this code to work =-

Discussion in 'Plugin Development' started by ThePluginMaster, Oct 16, 2013.

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

    ThePluginMaster

    I'm almost complete with this plugin, however, when I go to type in one of the commands my console warns me that I have a "Type mismatch : cannot convert object to string" error at lines 130 and 160. I've tried to play around a little and managed to fix the errors, however, the gui opens up, but the player is able to drag the item of the slot which isn't supposed to happen. How do I fix this?
    Code:java
    1.  
    2. import java.util.ArrayList;
    3. import java.util.HashMap;
    4. import java.util.Iterator;
    5. import java.util.List;
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Material;
    9. import org.bukkit.Server;
    10. import org.bukkit.configuration.file.FileConfiguration;
    11. import org.bukkit.enchantments.Enchantment;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.inventory.Inventory;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.PlayerInventory;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17.  
    18. public class Handler
    19. {
    20. private FrostyTokens plugin;
    21. private HashMap<String, Enchantment> enchantments = new HashMap();
    22.  
    23. public Handler(FrostyTokens main)
    24. {
    25. this.plugin = main;
    26.  
    27. this.enchantments.put("PROTECTION_ENVIRONMENTAL", Enchantment.getById(0));
    28. this.enchantments.put("PROTECTION_FIRE", Enchantment.getById(1));
    29. this.enchantments.put("PROTECTION_FALL", Enchantment.getById(2));
    30. this.enchantments.put("PROTECTION_EXPLOSIONS", Enchantment.getById(3));
    31. this.enchantments.put("PROTECTION_PROJECTILE", Enchantment.getById(4));
    32. this.enchantments.put("OXYGEN", Enchantment.getById(5));
    33. this.enchantments.put("WATER_WORKER", Enchantment.getById(6));
    34. this.enchantments.put("THORNS", Enchantment.getById(7));
    35. this.enchantments.put("DAMAGE_ALL", Enchantment.getById(16));
    36. this.enchantments.put("DAMAGE_UNDEAD", Enchantment.getById(17));
    37. this.enchantments.put("DAMAGE_ARTHROPODS", Enchantment.getById(18));
    38. this.enchantments.put("KNOCKBACK", Enchantment.getById(19));
    39. this.enchantments.put("FIRE_ASPECT", Enchantment.getById(20));
    40. this.enchantments.put("LOOT_BONUS_MOBS", Enchantment.getById(21));
    41. this.enchantments.put("DIG_SPEED", Enchantment.getById(32));
    42. this.enchantments.put("SILK_TOUCH", Enchantment.getById(33));
    43. this.enchantments.put("DURABILITY", Enchantment.getById(34));
    44. this.enchantments.put("LOOT_BONUS_BLOCKS", Enchantment.getById(35));
    45. this.enchantments.put("ARROW_DAMAGE", Enchantment.getById(48));
    46. this.enchantments.put("ARROW_KNOCKBACK", Enchantment.getById(49));
    47. this.enchantments.put("ARROW_FIRE", Enchantment.getById(50));
    48. this.enchantments.put("ARROW_INFINITE", Enchantment.getById(51));
    49. }
    50.  
    51. public Boolean tokenExist(String p)
    52. {
    53. if (Settings.hasAccount(p)) {
    54. return Boolean.valueOf(true);
    55. }
    56. return Boolean.valueOf(false);
    57. }
    58.  
    59. public void saveDefaultTokens(String player)
    60. {
    61. if (!Settings.hasAccount(player))
    62. Settings.setPlayerBalance(player, this.plugin.getConfig().getInt("settings.startingTokens", 0));
    63. }
    64.  
    65. public void takeTokens(String player, int t)
    66. {
    67. Settings.setPlayerBalance(player, Settings.getPlayerBalance(player) - t);
    68.  
    69. FrostyTokens.tsLog.write("Taken " + t + " token(s) from " + player);
    70. }
    71.  
    72. public void giveTokens(String player, int t)
    73. {
    74. Settings.setPlayerBalance(player, Settings.getPlayerBalance(player) + t);
    75. FrostyTokens.tsLog.write("Gave " + t + " token(s) to " + player);
    76. if ((this.plugin.getServer().getPlayer(player) != null) &&
    77. (this.plugin.getServer().getPlayer(player).isOnline()))
    78. this.plugin.getServer().getPlayer(player).sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + "FF" + ChatColor.GRAY + "Tokens" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + "You received " + ChatColor.GREEN + t + ChatColor.AQUA + " FF" + ChatColor.GRAY + "token(s)");
    79. }
    80.  
    81. public Boolean hasTokens(String player, int t)
    82. {
    83. if (Settings.getPlayerBalance(player) >= t) {
    84. return Boolean.valueOf(true);
    85. }
    86. return Boolean.valueOf(false);
    87. }
    88.  
    89. public int tokenBalance(String player)
    90. {
    91. return Settings.getPlayerBalance(player);
    92. }
    93.  
    94. public void giveItem(Player player, String item, int amount)
    95. {
    96. ItemStack output = getItem(item, amount);
    97.  
    98. player.getInventory().addItem(new ItemStack[] { output });
    99. }
    100.  
    101. public ItemStack getItem(String item, int amount)
    102. {
    103. String base = "items." + item + ".";
    104. int id = this.plugin.getConfig().getInt(base + "id");
    105. short meta = (short)this.plugin.getConfig().getInt(base + "meta", 0);
    106.  
    107. String name = this.plugin.getConfig().getString(base + "name", "");
    108.  
    109. List lore = this.plugin.getConfig().getList(base + "lore");
    110.  
    111. List enchants = this.plugin.getConfig().getList(base + "enchants");
    112.  
    113. ItemStack output = new ItemStack(id, amount, meta);
    114. ItemMeta itemMeta = output.getItemMeta();
    115. if (name != "")
    116. itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
    117. String color;
    118. if (lore != null) {
    119. List colorLore = new ArrayList();
    120.  
    121. for (Iterator localIterator = lore.iterator(); localIterator.hasNext(); ) { color = (String)localIterator.next();
    122. colorLore.add(ChatColor.translateAlternateColorCodes('&', color));
    123. }
    124.  
    125. itemMeta.setLore(colorLore);
    126. }
    127.  
    128. if (enchants != null)
    129. {
    130. for (String enchant : enchants <-THIS ) {
    131. String[] breakdown = enchant.split(":");
    132.  
    133. Enchantment ench = getEnchantmentByName(breakdown[0]);
    134. int level = Integer.valueOf(breakdown[1]).intValue();
    135. if (level > ench.getMaxLevel()) {
    136. level = ench.getMaxLevel();
    137. }
    138. itemMeta.addEnchant(ench, level, false);
    139. }
    140.  
    141. }
    142.  
    143. output.setItemMeta(itemMeta);
    144. return output;
    145. }
    146.  
    147. public void slotClick(int slot, Player player)
    148. {
    149. if (slot <= this.plugin.getConfig().getInt("settings.size")) {
    150. if (this.plugin.getConfig().getInt("shop.slot" + slot + ".price", -1) != -1) {
    151. int price = this.plugin.getConfig().getInt("shop.slot" + slot + ".price");
    152. if (hasTokens(player.getName(), price).booleanValue())
    153. {
    154. String base = "shop.slot" + slot + ".";
    155. ItemStack item = getItem(this.plugin.getConfig().getString(base + "displayItem"), 1);
    156.  
    157. List cmdlist = this.plugin.getConfig().getList(base + "commands");
    158. if (cmdlist != null)
    159. {
    160. for (String command : cmdlist <- THIS) {
    161. if (command.contains("{PLAYER}")) {
    162. command = command.replace("{PLAYER}", player.getName());
    163. }
    164.  
    165. this.plugin.getServer().dispatchCommand(Bukkit.getConsoleSender(), command);
    166. }
    167. }
    168. takeTokens(player.getName(), price);
    169.  
    170. if (item.getItemMeta().getDisplayName() == null) {
    171. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + "FF" + ChatColor.GRAY + "Tokens" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + "Succesfully bought " + item.getType().name().toLowerCase());
    172. FrostyTokens.tsLog.write(player.getName() + " bought " + item.getType().name().toLowerCase());
    173. return;
    174. }
    175. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + "FF" + ChatColor.GRAY + "Tokens" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + "Succesfully bought " + item.getItemMeta().getDisplayName());
    176. FrostyTokens.tsLog.write(player.getName() + " bought " + item.getItemMeta().getDisplayName());
    177. return;
    178. }
    179. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + "FF" + ChatColor.GRAY + "Tokens" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + "You don't have enough" + ChatColor.AQUA + " FF" + ChatColor.GRAY + "Tokens!");
    180. return;
    181. }
    182. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + "FF" + ChatColor.GRAY + "Tokens" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + "You can't purchase something that doesn't exist");
    183. return;
    184. }
    185. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + "FF" + ChatColor.GRAY + "Tokens" + ChatColor.DARK_GRAY + "]" + ChatColor.GOLD + "Slot was incorrect");
    186. }
    187.  
    188. public void playerGUI(Player player)
    189. {
    190. int t = 0;
    191. if (Settings.hasAccount(player.getName())) {
    192. t = Settings.getPlayerBalance(player.getName());
    193. }
    194. player.openInventory(guiShop(t));
    195. }
    196.  
    197. public Inventory guiShop(int tokens) {
    198. int size = this.plugin.getConfig().getInt("settings.size");
    199. Inventory i = Bukkit.createInventory(null, size, ChatColor.translateAlternateColorCodes('&', "&7You have " + ChatColor.GREEN + tokens + " &bFF&7Tokens"));
    200. for (int in = 0; in < size; in++) {
    201. int price = this.plugin.getConfig().getInt("shop.slot" + (in + 1) + ".price", 1);
    202.  
    203. String base = "shop.slot" + (in + 1) + ".";
    204.  
    205. ItemStack slot2 = getItem(this.plugin.getConfig().getString(base + "displayItem"), 1);
    206.  
    207. if ((slot2 != null) && (slot2.getType() != Material.AIR)) {
    208. ItemMeta im = slot2.getItemMeta();
    209. if (im.getLore() == null) {
    210. List l = new ArrayList();
    211. l.add(ChatColor.translateAlternateColorCodes('&', "&aPrice: " + price));
    212. im.setLore(l);
    213. } else {
    214. List l = im.getLore();
    215. l.add(ChatColor.translateAlternateColorCodes('&', "&aPrice: " + price));
    216. im.setLore(l);
    217. }
    218. slot2.setItemMeta(im);
    219.  
    220. i.setItem(in, slot2);
    221. }
    222. }
    223. return i;
    224. }
    225.  
    226. public Enchantment getEnchantmentByName(String name)
    227. {
    228. return (Enchantment)this.enchantments.get(name);
    229. }
    230. }
    231.  
     
  2. Offline

    TryB4

    ThePluginMaster
    use a small b in boolean
    Other than that, I don't see anything else.
     
  3. Offline

    amhokies

    It shouldn't matter if the boolean is capitalized or not.

    ThePluginMaster
    EDIT: When you're getting the list from the config, it's stored as a List of Objects, not Strings. You can either use plugin.getConfig().getStringList(path) or cast the Objects in the List to Strings.
     
  4. Offline

    FireBreath14

    can you do enchants.toString()? Cause you may be casting something and bukkit is throwing a fit about it...
     
  5. Offline

    Chinwe

    Instead of simply using List name = new ArrayList(), supply the type it will contain (in this case, Strings):
    Code:
    List<String> list = new ArrayList<String>();
     
Thread Status:
Not open for further replies.

Share This Page