Inventory Shop (Hats)

Discussion in 'Plugin Development' started by Stackore, May 2, 2014.

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

    Stackore

    Hello im coding an inventory shop. but i don't now how to do when the player don't have bought an item the prefix is red with lore cost: <price> and when the player has bought the item the prefix is green and the lore is click to equip (for example) so the player don't bought the item again you know
    (I'm working with hats right now)
    Hope you guys understand me!
    Code:
    Code:java
    1. public void Hats() {
    2. Hats = Bukkit.createInventory(null, 36, "Hats");
    3. ItemStack dirt = new ItemStack(Material.DIRT, 1);
    4. ItemMeta dirtmeta = dirt.getItemMeta();
    5. List<String> dirtlore = new ArrayList<String>();
    6. dirtlore.add(ChatColor.GRAY + "The basic dirt hat");
    7. dirtlore.add(ChatColor.GOLD + "Cost: 500");
    8. dirtmeta.setLore(dirtlore);
    9. dirtmeta.setDisplayName(ChatColor.RED + "" + ChatColor.ITALIC + "Dirty");
    10. dirt.setItemMeta(dirtmeta);
    11.  
    12. Hats.setItem(10, dirt);


    Code: click event
    Code:java
    1. @EventHandler
    2. public void hats(InventoryClickEvent event) {
    3. Player p = (Player) event.getWhoClicked();
    4. if (event.getInventory().getTitle().equals("Hats")) {
    5. if (event.getCurrentItem() != null && event.getCurrentItem().getType() == Material.DIRT) {
    6. EconomyResponse r = econ.withdrawPlayer(p.getName(), 500);
    7. if(r.transactionSuccess()) {
    8. ItemStack dirt = new ItemStack(Material.DIRT);
    9. p.getInventory().setHelmet(dirt);
    10. p.sendMessage(ChatColor.GREEN + "" + ChatColor.ITALIC + "You bought the Dirty hat!");
    11. event.setCancelled(true);
    12. p.closeInventory();
    13. }
    14. else {
    15. p.sendMessage(ChatColor.RED + "" + ChatColor.ITALIC + "You don't have enough money!");
    16. }
    17.  
    18.  
    19. }


    Anyone? [diamond]

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

    hintss

    And what's your question?
     
  3. Offline

    Tehmaker

    So the menu has a red name when the player hasn't bought the item, otherwise it is green?

    You would save the player's unique id, and list all the hats he has, then when you bring up the menu of items, you check if the player owns that hat, if he does, change the color to green.
     
  4. Offline

    Stackore

    Yes, could you give me an example please?

    Haven't you read it?

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

    mibac138

    You need to store some where data, did the player bought the hat, and if he did then you do the text green, else red, this is what you mean ?

    Code:java
    1. // When player buys a hat, then (getConfig is a example)
    2. getConfig().set(player.getName() + "." + hatname, true)
    3.  
    4. //When creating a inventory
    5. if (getConfig().getBoolean(player.getName() + "." + hatname) == true) {
    6. Item.getItemMeta().setDisplayName(ChatColor.GREEN + hatname);
    7. } else {
    8. Item.getItemMeta().setDispalyName(ChatColor.RED + hatname);
    9. }
     
  6. Offline

    Tehmaker

    It would probably best to use the player's uuid as a string.... Considering that otherwise players could change their name and steal other's hats haha.
     
  7. Offline

    mibac138

    Konkz likes this.
  8. Offline

    Stackore

    Could you insert that do my code? I got some errors

    I did this:
    Code:java
    1. ublic void Hats() {
    2. Hats = Bukkit.createInventory(null, 36, "Hats");
    3.  
    4. Player player = (Player);
    5.  
    6. ItemStack dirt = new ItemStack(Material.DIRT, 1);
    7. ItemMeta dirtmeta = dirt.getItemMeta();
    8. List<String> dirtlore = new ArrayList<String>();
    9. dirtlore.add(ChatColor.GRAY + "The basic dirt hat");
    10. dirtlore.add(ChatColor.GOLD + "Cost: 500");
    11. dirtmeta.setLore(dirtlore);
    12. dirtmeta.setDisplayName(ChatColor.RED + "" + ChatColor.ITALIC + "Dirty");
    13. dirt.setItemMeta(dirtmeta);
    14.  
    15. Hats.setItem(10, dirt);
    16.  
    17. if (getConfig().getBoolean(player.getName() + "." + "Dirty") == true) {
    18. dirt.getItemMeta().setDisplayName(ChatColor.GREEN + "Dirty");
    19. } else {
    20. dirt.getItemMeta().setDisplayName(ChatColor.RED + "Dirty");
    21. }
    22.  
    23.  
    24.  
    25. }
    26.  
    27. @EventHandler
    28. public void hats(InventoryClickEvent event) {
    29. Player p = (Player) event.getWhoClicked();
    30. if (event.getInventory().getTitle().equals("Hats")) {
    31. if (event.getCurrentItem() != null && event.getCurrentItem().getType() == Material.DIRT) {
    32. EconomyResponse r = econ.withdrawPlayer(p.getName(), 500);
    33. if(r.transactionSuccess()) {
    34. ItemStack dirt = new ItemStack(Material.DIRT);
    35. p.getInventory().setHelmet(dirt);
    36. p.sendMessage(ChatColor.GREEN + "" + ChatColor.ITALIC + "You bought the Dirty hat! " + ChatColor.RED + "" + ChatColor.ITALIC + "NOTE: Your hats will not be saved!");
    37. p.getWorld().playSound(p.getLocation(), Sound.NOTE_PLING , 1, 1);
    38. getConfig().set(p.getName() + "." + "Dirty", true);
    39. event.setCancelled(true);
    40. p.closeInventory();
    41. }
    42. else {
    43. p.sendMessage(ChatColor.RED + "" + ChatColor.ITALIC + "You don't have enough money!");
    44. }
    45. }
    46. }
    47. }

    But i get consol errors :( What i did wrong?

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

    mibac138

    This should work:
    Code:java
    1. private ItemStack dirt;
    2.  
    3. public void Hats ( Player player ) {
    4. Inventory Hats = Bukkit.createInventory( null, 36, "Hats" );
    5.  
    6. dirt = new ItemStack( Material.DIRT, 1 );
    7. ItemMeta dirtmeta = dirt.getItemMeta( );
    8. List< String > dirtlore = new ArrayList< String >( );
    9. dirtlore.add( ChatColor.GRAY + "The basic dirt hat" );
    10. dirtlore.add( ChatColor.GOLD + "Cost: 500" );
    11. dirtmeta.setLore( dirtlore );
    12.  
    13. if ( getConfig( ).getBoolean( player.getName( ) + ".Dirty" ) == true ) {
    14. dirt.getItemMeta( ).setDisplayName(
    15. ChatColor.GREEN + "" + ChatColor.ITALIC + "Dirty" );
    16. } else {
    17. dirt.getItemMeta( ).setDisplayName(
    18. ChatColor.RED + "" + ChatColor.ITALIC + "Dirty" );
    19. }
    20.  
    21. dirt.setItemMeta( dirtmeta );
    22.  
    23. Hats.setItem( 10, dirt );
    24.  
    25. }
    26.  
    27. @EventHandler
    28. public void hats ( InventoryClickEvent event ) {
    29. Player p = (Player) event.getWhoClicked( );
    30. if ( event.getInventory( ).getTitle( ).equals( "Hats" ) ) {
    31. if ( event.getCurrentItem( ) != null
    32. && event.getCurrentItem( ).getType( ) == Material.DIRT
    33. && dirt.getItemMeta( ).getDisplayName( ) != ChatColor.GREEN
    34. + "" + ChatColor.ITALIC + "Dirty" ) {
    35. EconomyResponse r = econ.withdrawPlayer( p.getName( ), 500 );
    36. if ( r.transactionSuccess( ) ) {
    37. ItemStack dirt = new ItemStack( Material.DIRT );
    38. p.getInventory( ).setHelmet( dirt );
    39. p.sendMessage( ChatColor.GREEN + "" + ChatColor.ITALIC
    40. + "You bought the Dirty hat! " + ChatColor.RED + ""
    41. + ChatColor.ITALIC
    42. + "NOTE: Your hats will not be saved!" );
    43. p.getWorld( ).playSound( p.getLocation( ),
    44. Sound.NOTE_PLING, 1, 1 );
    45. getConfig( ).set( p.getName( ) + ".Dirty", true );
    46. event.setCancelled( true );
    47. p.closeInventory( );
    48. } else {
    49. p.sendMessage( ChatColor.RED + "" + ChatColor.ITALIC
    50. + "You don't have enough money!" );
    51. }
    52. }
    53. }
    54. }


    BTW. Paste errors
     
  10. Offline

    Stackore

    I got consol error on line: if ( getConfig( ).getBoolean( player.getName( ) + ".Dirty" ) == true ) {
    And line: Hats(null);
    The player.getName is the error
     
  11. Offline

    TGRHavoc

    Stackore
    You're not calling the hats method like this "Hats(null)" are you? If you are then changed the "null" into a player.
     
  12. Offline

    Stackore

    I error if Hats(player);
     
Thread Status:
Not open for further replies.

Share This Page