Sign Shop (Buy and Sell)

Discussion in 'Plugin Development' started by GRocksMc, Mar 15, 2017.

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

    GRocksMc

    At the moment, I'm making a plugin that if you click (right or left(just left right now)) it will buy/sell an item.
    Sign would say:
    [Sky]
    cost of buying
    item:data
    how much you get for selling

    I can't, for the life of me, figure out how to get the data off of the sign and apply it to an ItemStack
    Code:java
    1.  
    2. if(event.getAction() == Action.LEFT_CLICK_BLOCK){
    3. if((block.getType() == Material.SIGN_POST) || (block.getType() == Material.WALL_SIGN)){
    4. Sign sign = (Sign) block.getState();
    5. if(sign.getLine(0).equals("§7[§3Sky§7]")){
    6. String line3 = sign.getLine(3).replaceAll(\\d", "");
    7. String line2 = sign.getLine(2).replaceAll(\\d", "");
    8. int points = main.getConfig().getInt("SkyBlockScore." + p);
    9. if(isInt(line3)){
    10. if(isInt(line2)){
    11. int itemID = Integer.parseInt(line2);
    12. int income = Integer.parseInt(line3);
    13. String[] split2 = line2.split("\\:");
    14. Material itemString = Material.getMaterial(itemID);
    15. int data = Integer.parseInt(split2[1]);
    16. ItemStack item = new ItemStack(itemString, 1, (short) data);
    17. if(player.getInventory().contains(item)){
    18. player.getInventory().removeItem(item);
    19. int newpoints = points + income;
    20. main.getConfig().set("SkyBlockScore." + p, newpoints);
    21. main.saveConfig();
    22. player.sendMessage(ChatColor.GRAY + "You just sold " + ChatColor.DARK_AQUA + item.toString().replaceAll("ItemStack\\{(\\w+)\\s+x\\s+\\d+\\}", "$1").toLowerCase());
    23. player.sendMessage(ChatColor.GRAY + "You now have " + ChatColor.DARK_AQUA + newpoints + ChatColor.GRAY + " points");
    24. }
    25. else{
    26. player.sendMessage(ChatColor.GRAY + "You don't have " + ChatColor.DARK_AQUA + line2);
    27. }
    28. }
    29. else{
    30. int income = Integer.parseInt(line3);
    31. String[] split2 = line2.split("\\:");
    32. Material itemString = Material.getMaterial(split2[0].toUpperCase());
    33. int data = Integer.parseInt(split2[1]);
    34. ItemStack item = new ItemStack(itemString, 1, (short) data);
    35. //player.getInventory().addItem(new ItemStack(Material.getMaterial("stone".toUpperCase()), 1, (short) 0));
    36. if(player.getInventory().contains(item)){
    37. player.getInventory().removeItem(item);
    38. int newpoints = points + income;
    39. main.getConfig().set("SkyBlockScore." + p, newpoints);
    40. main.saveConfig();
    41. player.sendMessage(ChatColor.GRAY + "You just sold " + ChatColor.DARK_AQUA + split2[0]);
    42. player.sendMessage(ChatColor.GRAY + "You now have " + ChatColor.DARK_AQUA + newpoints + ChatColor.GRAY + " points");
    43. }
    44. else{
    45. player.sendMessage(ChatColor.GRAY + "You don't have " + ChatColor.DARK_AQUA + split2[0]);
    46. }
    47. }
    48. }
    49. }
    50. }
    51. }
    52.  

    It probably look like a lot... just move you eyes towards the ItemStack part.

    My problem is that when I sell (left click), it it takes a block if I have two different stacks of that block.

    Thanks for your help!
     
  2. Offline

    Zombie_Striker

    ItemID includes both the material and the durability. What you want to do is split linte to at the ':'. The first bit will be the item id, and the second bit will be the id. You already did the data, now what you need to do is make sure itemID only parses the first bit.
     
  3. Offline

    GRocksMc

    @Zombie_Striker

    Code:
    Code:java
    1.  
    2. if(event.getAction() == Action.LEFT_CLICK_BLOCK){
    3. if((block.getType() == Material.SIGN_POST) || (block.getType() == Material.WALL_SIGN)){
    4. Sign sign = (Sign) block.getState();
    5. if(sign.getLine(0).equals("§7[§3Sky§7]")){
    6. String line3 = sign.getLine(3).replaceAll(\\d", "");
    7. String line2 = sign.getLine(2).replaceAll(\\d", "");
    8. int points = main.getConfig().getInt("SkyBlockScore." + p);
    9. String[] split2 = line2.split(":");
    10. int income = Integer.parseInt(line3);
    11. if(isInt(line3)){
    12. if(isInt(split2[0])){
    13. int itemID = Integer.parseInt(split2[0]);
    14. int data = Integer.parseInt(split2[1]);
    15. Material itemString = Material.getMaterial(itemID);
    16. ItemStack item = new ItemStack(itemString, 1, (short) data);
    17. if(player.getInventory().contains(item)){
    18. player.getInventory().removeItem(item);
    19. int newpoints = points + income;
    20. main.getConfig().set("SkyBlockScore." + p, newpoints);
    21. main.saveConfig();
    22. player.sendMessage(ChatColor.GRAY + "You just sold " + ChatColor.DARK_AQUA + item.toString().replaceAll("ItemStack\\{(\\w+)\\s+x\\s+\\d+\\}", "$1").toLowerCase());
    23. player.sendMessage(ChatColor.GRAY + "You now have " + ChatColor.DARK_AQUA + newpoints + ChatColor.GRAY + " points");
    24. }
    25. else{
    26. player.sendMessage(ChatColor.GRAY + "You don't have minecraft:" + ChatColor.DARK_AQUA + split2[0]);
    27. }
    28. }
    29. else{
    30. Material itemString = Material.getMaterial(split2[0].toUpperCase());
    31. int data = Integer.parseInt(split2[1]);
    32. ItemStack item = new ItemStack(itemString, 1, (short) data);
    33. //player.getInventory().addItem(new ItemStack(Material.getMaterial("stone".toUpperCase()), 1, (short) 0));
    34. if(player.getInventory().contains(item)){
    35. player.getInventory().removeItem(item);
    36. int newpoints = points + income;
    37. main.getConfig().set("SkyBlockScore." + p, newpoints);
    38. main.saveConfig();
    39. player.sendMessage(ChatColor.GRAY + "You just sold " + ChatColor.DARK_AQUA + split2[0]);
    40. player.sendMessage(ChatColor.GRAY + "You now have " + ChatColor.DARK_AQUA + newpoints + ChatColor.GRAY + " points");
    41. }
    42. else{
    43. player.sendMessage(ChatColor.GRAY + "You don't have " + ChatColor.DARK_AQUA + split2[0]);
    44. }
    45. }
    46. }
    47. }
    48. }
    49. }
    50.  

    It tells me that I don't have that block. It's not seeing that block in my inventory.

    It works for "grass:0" but not "stone:0"

    EDIT: the problem above is because I had a single block of grass while I had an entire stack of stone. This is because I have the ItemStack amount set to 1. How do I have it remove 1 from a stack?

    I think I can look for that item in the player's inventory with a for loop. If it's there, then get the amount. Subtract 1 from that amount. So... I know what to do, I just don't know how to do it.
     
    Last edited: Mar 15, 2017
  4. Offline

    timtower Administrator Administrator Moderator

    @GRocksMc You can lower the amount of the itemstack by 1, just remember to remove the entire stack when the amount gets to 0.
     
  5. Offline

    GRocksMc

    @timtower

    I don't know how to get the ItemStack and it's amount in order to lower it by 1.
     
  6. Offline

    timtower Administrator Administrator Moderator

    @GRocksMc Loop through the inventory.
    Find an itemstack with the same type and data.
    Lower the amount.
     
  7. Offline

    GRocksMc

    This is what I've gotten so far...
    Code:java
    1.  
    2. String line3 = sign.getLine(3).replaceAll(\\d", "");
    3. String line2 = sign.getLine(2).replaceAll(\\d", "");
    4. int points = main.getConfig().getInt("SkyBlockScore." + p);
    5. String[] split2 = line2.split(":");
    6. int data = Integer.parseInt(split2[1]);
    7. int income = Integer.parseInt(line3);
    8. if(isInt(line3)){
    9. if(isInt(split2[0])){
    10. int itemID = Integer.parseInt(split2[0]);
    11. Material itemString = Material.getMaterial(itemID);
    12. ItemStack item = new ItemStack(itemString, 1, (short) data);
    13. int invamount = 0;
    14. if(player.getInventory().contains(item)){
    15. int invlength = player.getInventory().getContents().length;
    16. for(int i = 0; i < invlength; i++){
    17. if(player.getInventory().getItem(i).equals(itemString)){
    18. invamount = player.getInventory().getItem(i).getAmount();
    19. break;
    20. }
    21. i++;
    22. }
    23. int newamount = invamount - 1;
    24. player.getInventory().removeItem(item);
    25. int newpoints = points + income;
    26. main.getConfig().set("SkyBlockScore." + p, newpoints);
    27. main.saveConfig();
    28. player.sendMessage(ChatColor.GRAY + "You just sold " + ChatColor.DARK_AQUA + item.toString().replaceAll("ItemStack\\{(\\w+)\\s+x\\s+\\d+\\}", "$1").toLowerCase());
    29. player.sendMessage(ChatColor.GRAY + "You now have " + ChatColor.DARK_AQUA + newpoints + ChatColor.GRAY + " points");
    30. }
    31.  


    I don't know what to do with this, I'm not so good with loops.
     
  8. Offline

    Caderape2

    @GRocksMc


    You just have to loops player.getInventoty().GetContents().
    Check for each itemstack if the material and the data is good, then set the amount with itemstack.setAmount(itemstack.getamount()-1)
     
  9. Offline

    GRocksMc

    Caderape2 timtower

    I'm sorry to say this, but I don't know how to loop player.getInventory().getContents().
     
  10. Offline

    Caderape2

    getContents() return a list of itemstack.
    You can do a for loops for get each itemstack.
    @GRocksMc
     
  11. Offline

    GRocksMc

    @Caderape2

    Would this do it?
    Code:java
    1.  
    2. int itemID = Integer.parseInt(split2[0]);
    3. Material itemString = Material.getMaterial(itemID);
    4. ItemStack item = new ItemStack(itemString, 1, (short) data);
    5. int invamount = 0;
    6. int itemNumber = 0;
    7. if(player.getInventory().contains(item)){
    8. int invlength = player.getInventory().getContents().length;
    9. for(int i = 0; i < invlength; i++){
    10. if(player.getInventory().getItem(i).equals(itemString)){
    11. invamount = player.getInventory().getItem(i).getAmount();
    12. itemNumber = i;
    13. break;
    14. }
    15. i++;
    16. }
    17. int newamount = invamount - 1;
    18. player.getInventory().getItem(itemNumber).setAmount(newamount);;
    19. int newpoints = points + income;
    20. main.getConfig().set("SkyBlockScore." + p, newpoints);
    21. main.saveConfig();
    22. player.sendMessage(ChatColor.GRAY + "You just sold " + ChatColor.DARK_AQUA + item.toString().replaceAll("ItemStack\\{(\\w+)\\s+x\\s+\\d+\\}", "$1").toLowerCase());
    23. player.sendMessage(ChatColor.GRAY + "You now have " + ChatColor.DARK_AQUA + newpoints + ChatColor.GRAY + " points");
    24. }
    25. else{
    26. player.sendMessage(ChatColor.GRAY + "You don't have minecraft:" + ChatColor.DARK_AQUA + split2[0]);
    27. }
    28.  
     
    Last edited: Mar 16, 2017
  12. Offline

    Caderape2

    @GRocksMc
    the loops is correct, even if you could do a 'for (ItemStack item : inventory.getcontents())'
    Don't use equal, that will check the material, the data, the amount, and the itemMeta.
    just check if the material and the durability are the same.

    You may check the amount of the itemstack. I'm don't think it will remove the item if u set the amount to 0
     
  13. Offline

    GRocksMc

    @Caderape2

    I want it to check the data of the item in case it's andesite or something like it.

    Can you give me an example please.
     
    Last edited: Mar 16, 2017
  14. Offline

    SeniorCluckers

    @GRocksMc

    I'm not sure if this could help you but take a look. I did this whilst creating a gui shop, I would set the price of 16 Wool to 4 Iron and get the players Iron amount in his inventory and if the player at least had 4 Iron then I would remove 4 Iron and it worked perfectly. You could loop it easily if you want like a sell all type of thing. Of course you would have to change it to fit your needs.

    Code:
            ItemStack playerIron = new ItemStack(Material.IRON_INGOT);
            ItemStack priceIron = new ItemStack(Material.IRON_INGOT, 4);
            ItemStack wool = new ItemStack(Material.WOOL, 16);
    
            if (clicked == null || clicked.hasItemMeta() == false || clicked.getItemMeta().hasDisplayName() == false) {
                return;
            } else {
                if (inventory.getName().equals(ItemStacks.blocksshopInventory.getName())) {
                    if (clicked.getType() == Material.WOOL && clicked.getAmount() == 16) {
                        if (player.getInventory().containsAtLeast(playerIron, 4)) {
                            player.getInventory().removeItem(priceIron);
                            player.getInventory().addItem(wool);
                            String name = clicked.getItemMeta().getDisplayName().toString();
                            player.sendMessage(purchase + ChatColor.GOLD + ChatColor.stripColor(name));
                        }
                    }
                }
            
     
    Last edited: Mar 16, 2017
  15. Offline

    GRocksMc

    @SeniorCluckers

    Thank you so much! I believe that this will solve my problems. I will test this ASAP when I get back to my computer. (I did not know there was a containsAtLeast()!!!)

    Final Code (YAY!):
    Code:java
    1.  
    2. package com.GRocks.ApplicationPlugin;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.Material;
    6. import org.bukkit.block.Block;
    7. import org.bukkit.block.Sign;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. import org.bukkit.inventory.ItemStack;
    14. import org.bukkit.plugin.Plugin;
    15.  
    16. import net.md_5.bungee.api.ChatColor;
    17.  
    18. public class skyShop implements Listener{
    19.  
    20. Plugin main = Bukkit.getPluginManager().getPlugin("GamerVerse");
    21. Main plugin;
    22. public skyShop(Main instance) {
    23. plugin = instance;
    24. }
    25.  
    26. public static boolean isInt(String s) {
    27. try {
    28. Integer.parseInt(s);
    29. }
    30. catch (NumberFormatException nfe) {
    31. return false;
    32. }
    33. return true;
    34. }
    35.  
    36. @SuppressWarnings("deprecation")
    37. @EventHandler
    38. public void onPlayerInteractShopSign(PlayerInteractEvent event){
    39. if(event.getPlayer().getWorld().getName().equalsIgnoreCase("skyblock") || event.getPlayer().getWorld().getName().equalsIgnoreCase("Misc")){
    40. Player player = event.getPlayer();
    41. String p = player.getName();
    42. Block block = event.getClickedBlock();
    43. if(event.getAction() == Action.LEFT_CLICK_BLOCK){
    44. if((block.getType() == Material.SIGN_POST) || (block.getType() == Material.WALL_SIGN)){
    45. Sign sign = (Sign) block.getState();
    46. if(sign.getLine(0).equals("§7[§3Sky§7]")){
    47. String line3 = sign.getLine(3).replaceAll(\\d", "");
    48. String line2 = sign.getLine(2).replaceAll(\\d", "");
    49. int points = main.getConfig().getInt("SkyBlockScore." + p);
    50. String[] split2 = line2.split(":");
    51. int data = Integer.parseInt(split2[1]);
    52. int income = Integer.parseInt(line3);
    53. if(isInt(split2[0])){
    54. int itemID = Integer.parseInt(split2[0]);
    55. Material itemString = Material.getMaterial(itemID);
    56. ItemStack item = new ItemStack(itemString, 1);
    57. item.setDurability((short) data);
    58. if(player.getInventory().containsAtLeast(item, 1)){
    59. player.getInventory().removeItem(item);
    60. int newpoints = points + income;
    61. main.getConfig().set("SkyBlockScore." + p, newpoints);
    62. main.saveConfig();
    63. player.sendMessage(ChatColor.GRAY + "You just sold " + ChatColor.DARK_AQUA + item.toString().replaceAll("ItemStack\\{(\\w+)\\s+x\\s+\\d+\\}", "$1").toLowerCase());
    64. player.sendMessage(ChatColor.GRAY + "You now have " + ChatColor.DARK_AQUA + newpoints + ChatColor.GRAY + " points");
    65. }
    66. else{
    67. player.sendMessage(ChatColor.GRAY + "You don't have minecraft:" + ChatColor.DARK_AQUA + split2[0]);
    68. }
    69. }
    70. else{
    71. Material itemString = Material.getMaterial(split2[0].toUpperCase());
    72. ItemStack item = new ItemStack(itemString, 1);
    73. item.setDurability((short) data);
    74. if(player.getInventory().containsAtLeast(item, 1)){
    75. player.getInventory().removeItem(item);
    76. int newpoints = points + income;
    77. main.getConfig().set("SkyBlockScore." + p, newpoints);
    78. main.saveConfig();
    79. player.sendMessage(ChatColor.GRAY + "You just sold " + ChatColor.DARK_AQUA + split2[0]);
    80. player.sendMessage(ChatColor.GRAY + "You now have " + ChatColor.DARK_AQUA + newpoints + ChatColor.GRAY + " points");
    81. }
    82. else{
    83. player.sendMessage(ChatColor.GRAY + "You don't have " + ChatColor.DARK_AQUA + split2[0]);
    84. }
    85. }
    86. }
    87. }
    88. }
    89. else if(event.getAction() == Action.RIGHT_CLICK_BLOCK){
    90. if((block.getType() == Material.SIGN_POST) || (block.getType() == Material.WALL_SIGN)){
    91. Sign sign = (Sign) block.getState();
    92. if(sign.getLine(0).equals("§7[§3Sky§7]")){
    93. String line2 = sign.getLine(2).replaceAll(\\d", "");
    94. String line1 = sign.getLine(1).replaceAll(\\d", "");
    95. int points = main.getConfig().getInt("SkyBlockScore." + p);
    96. String[] split2 = line2.split(":");
    97. int data = Integer.parseInt(split2[1]);
    98. int price = Integer.parseInt(line1);
    99. if(isInt(split2[0])){
    100. int itemID = Integer.parseInt(split2[0]);
    101. Material itemString = Material.getMaterial(itemID);
    102. ItemStack item = new ItemStack(itemString, 1);
    103. item.setDurability((short) data);
    104. if(points >= price){
    105. player.getInventory().addItem(item);
    106. int newpoints = points - price;
    107. main.getConfig().set("SkyBlockScore." + p, newpoints);
    108. main.saveConfig();
    109. player.sendMessage(ChatColor.GRAY + "You just bought " + ChatColor.DARK_AQUA + item.toString().replaceAll("ItemStack\\{(\\w+)\\s+x\\s+\\d+\\}", "$1").toLowerCase());
    110. player.sendMessage(ChatColor.GRAY + "You now have " + ChatColor.DARK_AQUA + newpoints + ChatColor.GRAY + " points");
    111. }
    112. else{
    113. player.sendMessage(ChatColor.GRAY + "You need " + ChatColor.DARK_AQUA + price + ChatColor.GRAY + " points to buy that! You only have " + ChatColor.DARK_AQUA + points);
    114. }
    115. }
    116. else{
    117. Material itemString = Material.getMaterial(split2[0].toUpperCase());
    118. ItemStack item = new ItemStack(itemString, 1);
    119. item.setDurability((short) data);
    120. if(points >= price){
    121. player.getInventory().addItem(item);
    122. int newpoints = points - price;
    123. main.getConfig().set("SkyBlockScore." + p, newpoints);
    124. main.saveConfig();
    125. player.sendMessage(ChatColor.GRAY + "You just bought " + ChatColor.DARK_AQUA + itemString);
    126. player.sendMessage(ChatColor.GRAY + "You now have " + ChatColor.DARK_AQUA + newpoints + ChatColor.GRAY + " points");
    127. }
    128. else{
    129. player.sendMessage(ChatColor.GRAY + "You need " + ChatColor.DARK_AQUA + price + ChatColor.GRAY + " points to buy that! You only have " + ChatColor.DARK_AQUA + points);
    130. }
    131. }
    132. }
    133. }
    134. }
    135. }
    136. }
    137. }
    138.  
     
    Last edited: Mar 23, 2017
Thread Status:
Not open for further replies.

Share This Page