[Urgent] Drag & Drop Shop Plugin!

Discussion in 'Plugin Development' started by EliteLX, Jun 28, 2014.

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

    EliteLX

    Hey fellas, I'm in a bit need of help. I'm making a drag & drop shop plugin that gives players a certain amount based on my config.yml and it's working ok, but still extremely buggy and I've even set up debug messages.

    Basically, it functions by when the menu opens a book item with instructions as its lore on how to use the automatic shop is set at the first slot of the inventory. When they close the shop, I'm trying to get it to remove the book from slot 1, make sure the inventory isn't empty, and then check EACH SLOT of the inventory and add to the "Cost" int which is how much the user will be paid, then go through a check to make sure that there aren't any broken items in the inventory and make sure the price of the items sold are all above $0 in the config.yml.

    I'm currently using the ints "broke", and "soldnothing" as 0/1 ghetto booleans you could say lol. Basically, if the item isn't at full durability in a certain slot "broke" is set at 1 and gives back that broken item so they can't sell it, and once it goes to pay the user if broke = 1 it tells them some of the items they sold we're repaired fully so they have been returned to them.

    I'm in need of help. Please Bukkit, why isn't this working?

    Problem #1: Open the inventory & close it and I get the message "you can't sell those items"
    Expectation for #1: A message that says "You did not put in any items to sell!"
    Problem #2: If I put an item in that has a cost above $0 in the config.yml, it sends me a debug message saying "there was nothing in the inventory" and that I didn't put any items to sell.
    Expectation for #2: It should sell the item.

    Will somebody please check my code?

    Code:java
    1. package me.EliteLX.ExternoxShop;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import net.milkbowl.vault.economy.Economy;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.Material;
    11. import org.bukkit.command.Command;
    12. import org.bukkit.command.CommandSender;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.EventHandler;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.inventory.InventoryCloseEvent;
    17. import org.bukkit.inventory.Inventory;
    18. import org.bukkit.inventory.ItemStack;
    19. import org.bukkit.inventory.meta.ItemMeta;
    20. import org.bukkit.plugin.RegisteredServiceProvider;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22.  
    23. public class Main
    24. extends JavaPlugin
    25. implements Listener
    26. {
    27. private ItemStack ist;
    28. Economy vault = null;
    29. boolean vaultLoaded = false;
    30. String shopName = "";
    31.  
    32. String prefix = (ChatColor.AQUA + "[" + ChatColor.GOLD + "Externox 2.0" + ChatColor.AQUA + "] " + ChatColor.YELLOW);
    33.  
    34.  
    35. public void onDisable()
    36. {
    37. getLogger().info("ExternoxShop Disabled!");
    38. }
    39.  
    40. public void onEnable()
    41. {
    42. if (!setupEconomy())
    43. {
    44. getLogger().severe("Vault not found with loaded economy plugin, disabling!");
    45. setEnabled(false);
    46. return;
    47. }
    48. getLogger().info("Enabling...");
    49. getServer().getPluginManager().registerEvents(this, this);
    50. getLogger().info("Registered Events!");
    51. getConfig().options().copyDefaults(true);
    52. saveConfig();
    53. this.shopName = ChatColor.translateAlternateColorCodes('&', getConfig().getString("name"));
    54. getLogger().info("Enabled!");
    55. }
    56.  
    57. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    58. {
    59. if ((cmd.getName().equalsIgnoreCase("shop")) && (sender.hasPermission("guiseller.use"))) {
    60. if ((sender instanceof Player))
    61. {
    62. Player p = (Player)sender;
    63. createSellerInv(p);
    64. }
    65. else
    66. {
    67. sender.sendMessage(ChatColor.BLUE + "You must be in-game to use this plugin!");
    68. }
    69. }
    70. if ((cmd.getName().equalsIgnoreCase("reloadshop")) && (sender.hasPermission("guiseller.admin")))
    71. {
    72. reloadConfig();
    73. this.shopName = ChatColor.translateAlternateColorCodes('&', getConfig().getString("name"));
    74. Player p = (Player) sender;
    75. p.sendMessage(prefix + "ExternoxShop has been reloaded!");
    76. }
    77. return false;
    78. }
    79.  
    80. private void createSellerInv(Player p)
    81. {
    82. Inventory inv = Bukkit.createInventory(p, 27, this.shopName);
    83. this.ist = new ItemStack(Material.BOOK, 1);
    84. ItemMeta meta = this.ist.getItemMeta();
    85. meta.setDisplayName(ChatColor.RED + "Need Help?");
    86. List<String> loreList = new ArrayList<String>();
    87. loreList.add(" ");
    88. loreList.add(ChatColor.LIGHT_PURPLE + "Drag items you want to sell into this menu!");
    89. loreList.add(ChatColor.RED + "Warning: ALL items will be sold!");
    90. meta.setLore(loreList);
    91. this.ist.setItemMeta(meta);
    92. this.ist.setAmount(0);
    93. inv.addItem(new ItemStack[] { this.ist });
    94. p.openInventory(inv);
    95. }
    96.  
    97.  
    98.  
    99. @SuppressWarnings("deprecation")
    100. @EventHandler
    101. public void onGUIClose(InventoryCloseEvent event)
    102. {
    103. if (event.getInventory().getName().equalsIgnoreCase(this.shopName))
    104. {
    105.  
    106. event.getInventory().removeItem(new ItemStack[] { this.ist });
    107. Player p = (Player)event.getPlayer();
    108. int sold = 0;
    109. int broke = 0;
    110. int soldnothing = 0;
    111.  
    112. for(ItemStack item : event.getInventory().getContents())
    113. {
    114. if(item != null){
    115. p.sendMessage("there was nothing in the inventory");
    116. soldnothing = 1;
    117. }
    118. }
    119.  
    120. for (int i = 0; i < 27; i++){
    121. ItemStack is = event.getInventory().getItem(i);
    122. if ((is != null) && (getConfig().getInt("prices." + is.getType().name()) != 0) && (is.getAmount() != 0)) {
    123. if (is.getDurability() == is.getType().getMaxDurability()) {
    124. sold += getConfig().getInt("prices." + is.getType().name()) * is.getAmount();
    125. } else {
    126. broke = 1;
    127. p.sendMessage(is.getType().name() + " item is not at full durability");
    128. p.getInventory().addItem(is);
    129. p.updateInventory();
    130. }
    131. } else {
    132. if(is != null){
    133. if(getConfig().getInt("prices." + is.getType().name()) == 0){
    134. p.sendMessage(is.getType().name() + " item has no price");
    135. p.getInventory().addItem(is);
    136. p.updateInventory();
    137. }
    138. }
    139. }
    140.  
    141.  
    142. }
    143.  
    144.  
    145. if(soldnothing == 0){
    146. if(broke == 0) {
    147. if (sold != 0){
    148. p.sendMessage(prefix + ChatColor.GOLD + "Thank you! Your items sold for " + ChatColor.GREEN + "$" + sold);
    149.  
    150. sold = 0;
    151. broke = 0;
    152. soldnothing = 0;
    153.  
    154. if (this.vault.hasAccount(p.getName()))
    155. {
    156. this.vault.depositPlayer(p.getName(), sold);
    157. }
    158. else
    159. {
    160. this.vault.createPlayerAccount(p.getName());
    161. this.vault.depositPlayer(p.getName(), sold);
    162. }
    163. } else {
    164. p.sendMessage(prefix + ChatColor.RED + "You can not sell those items!");
    165. }
    166. } else {
    167. p.sendMessage(prefix + "You put in some items that weren't fully repaired, so they have been returned.");
    168. }
    169. } else {
    170. p.sendMessage(prefix + "You did not put in any items to sell!");
    171. }
    172.  
    173.  
    174. }
    175. }
    176.  
    177.  
    178. public boolean setupEconomy()
    179. {
    180. if (!this.vaultLoaded)
    181. {
    182. this.vaultLoaded = true;
    183. if (getServer().getPluginManager().getPlugin("Vault") != null)
    184. {
    185. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    186. if (rsp != null) {
    187. this.vault = ((Economy)rsp.getProvider());
    188. }
    189. }
    190. }
    191. return this.vault != null;
    192. }
    193. }
     
  2. I must say your code is very unreadable, because there are no spaces or tabs.

    I see one weird thing in your code, from line 112 to 118

    1. for(ItemStack item : event.getInventory().getContents())
    2. {
    3. if(item != null){
    4. p.sendMessage("there was nothing in the inventory");
    5. soldnothing = 1;
    6. }
    7. }
    Are you trying to check if the inventory is empty?
    Then it should be something like this:

    Code:java
    1. boolean isEmpty = true;
    2. for (ItemStack item : event.getInventory().getContents()) {
    3. if (item != null) {
    4. isEmpty = false;
    5. break;// break out of this for-loop, we see that it's not empty
    6. }
    7. }
    8. if (isEmpty) {
    9. p.sendMessage("there was nothing in the inventory");
    10. soldnothing = 1;
    11. }
     
  3. Offline

    RingOfStorms

    That has to do with the syntax box, it does not support tabs and so it removes all of the spacing. It isn't his fault :)
     
  4. Offline

    incriptec

    Can you possibly release this plugin to the public? I'd love to have this...
     
Thread Status:
Not open for further replies.

Share This Page