Buy and Sell Signs

Discussion in 'Plugin Development' started by Creeper674, Sep 25, 2014.

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

    Creeper674

    Hello all!

    I am creating a store like that of essentials, but instead it uses a points system I created as the economy.

    However, I am stuck on how to go about doing this. My intention is to have a sign which looks like this:
    <Buy>
    23:1 (Item Id)
    64 (Amount)
    35 (Cost in points)

    Here is a bit of the code, and well it's not the best I've ever written.

    Code:java
    1. @EventHandler
    2. public void onSign(PlayerInteractEvent event) {
    3. Player p = event.getPlayer();
    4. Block clicked = event.getClickedBlock();
    5. if (!(event.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    6. if (clicked.getState() instanceof Sign) {
    7. Sign s = (Sign) event.getClickedBlock().getState();
    8. int itemid = Integer.parseInt(s.getLine(1));
    9. if (s.getLine(0).equalsIgnoreCase("<Buy>")) {
    10. if (!(s.getLine(1).equals(null))) {
    11. p.getInventory().setItem(0, new ItemStack());
    12. }
    13. } else if (s.getLine(0).equalsIgnoreCase("<Sell>")) {
    14.  
    15. }
    16. }
    17. }


    This is incomplete and messy, I know, but if you could answer these questions that'd be great.

    1) How do I get the setItem() to get the ID on line1 of the sign to give that item to the player?

    2) How would I add things to a players inventory without overriding a full slot?

    Any help appreciated, thanks!
     
  2. Offline

    CoolGuy2001

    1) item.getTypeId() returns item`s id.
    2) p.getInventory().addItem(ItemStack is); will add a new item only if the inventory has room. :)
     
  3. Offline

    Creeper674

    CoolGuy2001 or anyone else,

    Thanks for that, however I need a bit of help with how I structure the code. My process so far is:

    1) Parse the second line as an integer called 'itemid'
    2) Make a new object of an item 'ItemStack item;'

    I want to give the player the item, and I want item to be the material which has been specified in itemid on the sign. I've literally confused myself with this, and need some new thinking. Thanks for all the help!
     
  4. Offline

    d3v1n302418

    Creeper674 Material.getById(itemid); Its deprecated but you'll live.
     
  5. Offline

    CoolGuy2001

    Also when you are adding the item in the original code, define the itemstack like so:

    Code:
    p.getInventory().addItem(new ItemStack(Material.STONE, 1));
    That would add 1 stone!

    Also use:
    Code:
    Integer.parseInt(s.getLine(1));
    This will get the ID from the sign and use it as an integer!

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

    Creeper674

    Thank you very much CoolGuy2001 and d3v1n302418 for the help, however I have one more issue.

    When I set up a sign which is an item, not a block it returns a null pointer exception because it wants a itemstack and gets a material. Anyway I can fix this?
     
  7. Offline

    Creeper674

    Any help would be appreciated this the problem above! ^
     
  8. Offline

    xTrollxDudex

    If I understand what you are trying to say correctly, you should be creating a new ItemStack from the ID.
     
  9. Offline

    Creeper674

    xTrollxDudex

    To get the item on the signs second line, I use
    Code:java
    1. int id = Integer.parseInt(s.getLine(1));
    2. Material item = Material.getMaterial(id);


    and then to give them the item I use
    Code:java
    1. p.getInventory().addItem(new ItemStack(item, amount));


    So I think I am creatinga new ItemStack, yet any item I try to buy which is not a block returns a null pointer exception.
     
Thread Status:
Not open for further replies.

Share This Page