[GUI's] Sell an Item!

Discussion in 'Plugin Development' started by BechGameplay, Jan 22, 2015.

Thread Status:
Not open for further replies.
  1. Hi Guys!
    I cant figure out how to give a player an Item + Charge him for money when he clicks on an Item in the GUI!
    - Its a SHOP Gui ;)

    Hope you can help ;)

    Vault Setup:
    Code:
    public static Economy econ = null;
       
          public void onEnable()
          {
           
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
           
            if (!setupEconomy())
            {
              getLogger().severe(String.format("[%s] - M8 i har ikke noget Penge Plugin", new Object[] { getDescription().getName() }));
              getServer().getPluginManager().disablePlugin(this);
              return;
            }
          }
         
          private boolean setupEconomy()
          {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
              return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
              return false;
            }
            econ = (Economy)rsp.getProvider();
            return econ != null;
          }
    My Code where i want the Player to Pay + Receive the item:
    Code:
        @EventHandler
        public void onInventoryClickE(InventoryClickEvent e) {
            if (!e.getInventory().getName().equalsIgnoreCase(MiningMenu.getName())) return;
           
            if(e.getCurrentItem().getItemMeta() == null) return;
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Diamond = 250 ,-")){
                e.setCancelled(true);
    
                EconomyResponse r = econ.withdrawPlayer(e.getWhoClicked(), 250);
               
                  if (r.transactionSuccess()) {
                e.getWhoClicked().getInventory().addItem(new ItemStack(Material.DIAMOND, 1));
                  }
            }
        }
     
  2. Offline

    mine-care

    on click of an item, get its price (you should have them somewhere) and give the player the item and then economy.withdraw(ammount); (code writtine on ipad sorri)
     
  3. @mine-care
    It didnt work ;(

    I tried another way, but it didnt work ;( Any fixes?!?!:
    Code:
                  EconomyResponse r = econ.withdrawPlayer(e.getPlayer(), 250);
                  if (r.transactionSuccess()) {
              
                e.getWhoClicked().getInventory().addItem(new ItemStack(Material.DIAMOND, 1));
                  }
     
  4. Offline

    mine-care

    @BechGameplay what didnt work? did it take the $$ did it add the diamond?
     
  5. @mine-care
    In Eclipse it gave me an error, there is nothing called; econ.withdraw

    Sry for beeing nooby ;( Still Learning ;)
     
  6. Offline

    mine-care

    @BechGameplay i am not sure if the method is that, check the available methods for one that contains withdraw from a player's accound (read docs) And no need to apologise! =D i like to help people learn! <3
     
  7. Offline

    1Rogue

    I have an API for this if you're interested (requires Java 8): https://github.com/CodeLanx/CodelanxLib/tree/master/src/main/java/com/codelanx/codelanxlib/inventory

    An example implementation:
    [​IMG]
    Code:java
    1. /*
    2.  * Copyright (C) 2015 Codelanx, All Rights Reserved
    3.  *
    4.  * This work is licensed under a Creative Commons
    5.  * Attribution-NonCommercial-NoDerivs 3.0 Unported License.
    6.  *
    7.  * This program is protected software: You are free to distrubute your
    8.  * own use of this software under the terms of the Creative Commons BY-NC-ND
    9.  * license as published by Creative Commons in the year 2015 or as published
    10.  * by a later date. You may not provide the source files or provide a means
    11.  * of running the software outside of those licensed to use it.
    12.  *
    13.  * This program is distributed in the hope that it will be useful,
    14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    16.  *
    17.  * You should have received a copy of the Creative Commons BY-NC-ND license
    18.  * long with this program. If not, see <[url]https://creativecommons.org/licenses/>[/url].
    19.  */
    20. package com.codelanx.codelanxlib.test;
    21.  
    22. import com.codelanx.codelanxlib.inventory.InventoryInterface;
    23. import com.codelanx.codelanxlib.inventory.InventoryPanel;
    24. import org.bukkit.Material;
    25. import org.bukkit.entity.Player;
    26. import org.bukkit.inventory.ItemStack;
    27.  
    28. /**
    29.  * Class description for {@link InterfaceTest}
    30.  *
    31.  * @since 0.1.0
    32.  * @author 1Rogue
    33.  * @version 0.1.0
    34.  */
    35. public final class InterfaceTest {
    36.  
    37. private final InventoryInterface internal;
    38.  
    39. public InterfaceTest() {
    40. this.internal = this.sampleInventory();
    41. }
    42.  
    43. /**
    44.   * Opens the interface for the player
    45.   *
    46.   * @since 0.1.0
    47.   * @version 0.1.0
    48.   *
    49.   * @param p
    50.   */
    51. public void open(Player p) {
    52. this.internal.openInterface(p);
    53. }
    54.  
    55. /**
    56.   * Creates an example of an {@link InventoryInterface}
    57.   *
    58.   * @since 0.1.0
    59.   * @version 0.1.0
    60.   *
    61.   * @return The example {@link InventoryInterface}
    62.   */
    63. private InventoryInterface sampleInventory() {
    64. InventoryInterface myInterface = new InventoryInterface();
    65. //Add panels, or inventory screens
    66. //Makes an inventory screen with 1 row of 9 slots, with the title "Buy a diamond!"
    67. InventoryPanel sellDiamond = myInterface.newPanel("Buy a diamond!", 1);
    68. //creates an icon in this panel's inventory and defines behavior
    69. sellDiamond.newIcon(new ItemStack(Material.DIAMOND), (player, ii, icon) -> {
    70. /*
    71.   Types:
    72.   player -> org.bukkit.entity.Player
    73.   ii -> com.codelanx.codelanxlib.inventory.InventoryInterface
    74.   icon -> com.codelanx.codelanxlib.inventory.MenuIcon
    75.   */
    76. //take money using the "player" variable
    77. player.getInventory().addItem(new ItemStack(Material.DIAMOND));
    78. player.closeInventory();
    79. });
    80. return myInterface;
    81. }
    82.  
    83. }

    Code:java
    1. /*
    2.  * Copyright (C) 2015 Codelanx
    3.  *
    4.  * This program is free software: you can redistribute it and/or modify
    5.  * it under the terms of the GNU General Public License as published by
    6.  * the Free Software Foundation, either version 3 of the License, or
    7.  * (at your option) any later version.
    8.  *
    9.  * This program is distributed in the hope that it will be useful,
    10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12.  * GNU General Public License for more details.
    13.  *
    14.  * You should have received a copy of the GNU General Public License
    15.  * along with this program. If not, see <[url]http://www.gnu.org/licenses/>[/url].
    16.  */
    17. package com.codelanx.bukkittest;
    18.  
    19. import org.bukkit.command.Command;
    20. import org.bukkit.command.CommandSender;
    21. import org.bukkit.entity.Player;
    22. import org.bukkit.plugin.java.JavaPlugin;
    23.  
    24. /**
    25.  * Class description for {@link BukkitTest}
    26.  *
    27.  * @since 1.0.0
    28.  * @author 1Rogue
    29.  * @version 1.0.0
    30.  */
    31. public class BukkitTest extends JavaPlugin {
    32.  
    33. private final static InterfaceTest test = new InterfaceTest();
    34.  
    35. @Override
    36. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    37. if (!(sender instanceof Player)) {
    38. return true;
    39. }
    40. BukkitTest.test.open((Player) sender);
    41. return true;
    42. }
    43.  
    44. }


    It allows you to set icons and such to do whatever action you want, and you can save/load the options to and from a yaml file.
     
    Last edited: Jan 23, 2015
  8. Offline

    teej107

    Are you sure the code in your event is running? Put in debug code!
     
  9. The event is running just fine, when i put in:

    Code:
    e.getWhoClicked().setGameMode(GameMode.CREATIVE);
    It changes the gamemode, and when people click on an item the get the item but they arent charged!
    @teej107

    @1Rogue Cool Thanks! I will just make sure that it is charging the players money?!

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

    1Rogue

    Well that's a functional interface you're supplying, you can do whatever code you want there. It's the code that will be executed when the item is clicked inside the inventory.
     
  11. @1Rogue
    That is my Problem, i dont know how to charge em Money ;(

    Im Used to use this method, but it didnt work. Eclipse gives me an error when i type e.getPlayer() or if i change it to e.getWhoClicked()
    Code:
    EconomyResponse r = econ.withdrawPlayer(e.getPlayer(), 250);
                  if (r.transactionSuccess()) {
     
  12. Offline

    1Rogue

    What's your current code?
     
  13. @1Rogue This is my WithDraw Code ;)
     
  14. Offline

    1Rogue

    I meant all of your code for the entire shop aspect.
     
  15. @1Rogue
    This is My Economy Setup:
    Code:
    public static Economy econ = null;
      
          public void onEnable()
          {
          
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
          
            if (!setupEconomy())
            {
              getLogger().severe(String.format("[%s] - M8 i har ikke noget Penge Plugin", new Object[] { getDescription().getName() }));
              getServer().getPluginManager().disablePlugin(this);
              return;
            }
          }
        
          private boolean setupEconomy()
          {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
              return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
              return false;
            }
            econ = (Economy)rsp.getProvider();
            return econ != null;
          }
     
  16. Offline

    1Rogue

    And your InventoryClickEvent method?
     
  17. @1Rogue
    Here you Go:
    Code:
        @EventHandler
        public void onInventoryClickE(InventoryClickEvent e) {
            if (!e.getInventory().getName().equalsIgnoreCase(MiningMenu.getName())) return;
           
            if(e.getCurrentItem().getItemMeta() == null) return;
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Diamond = 250 ,-")){
                e.setCancelled(true);
    
                //NEED THE WITHDRAW CODEHERE!
               
                e.getWhoClicked().getInventory().addItem(new ItemStack(Material.DIAMOND, 1));
               
            }
        }
     
  18. Offline

    1Rogue

    http://docs.oracle.com/javase/tutorial/java/index.html Try going through all of the first three sections ("Object-Oriented Programming Concepts" until "Classes and Objects"). If you read it all, then you should be able to come back here and tell me your problem + solution (hint: Using a public static econ variable isn't the solution).
     
  19. @1Rogue
    I tried to read it, but i didnt get much out of it ;(
    - If you could give me some more hints i could try to figure it out myself ;)

    Problem:
    - I Cant figure out to charge people for money when they click on an Item in the GUI ;(

    Solution:
    I tried this: http://gyazo.com/9e821979db11b7acff2e05550d7e9e0e but it didnt work ;( + i changed Public Static econ to Private econ ;)

    Hope you can help me ;)

    Sry for beeing a noob ;(
     
Thread Status:
Not open for further replies.

Share This Page