Turn Blocks into the corresponding ore

Discussion in 'Plugin Development' started by DefaultSyntax, Jun 2, 2014.

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

    DefaultSyntax

    Hi, I'm making a plugin that allows players to do /gem and it will turn the block in their hand to the corresponding ore. So let's say they're holding 24 Diamond blocks, doing /gem would give them 216 Diamonds.

    The part i need help with is checking how many blocks the player has and when they do /gem, it will give them the right amount.
     
  2. Offline

    bobthefish

    Try this:
    Code:java
    1. Player p = e.getPlayer();
    2. int amount = p.getItemInHand().getAmount() *9;//9 diamonds per block
    3. // now you can remove the blocks from the player
    4. p.getInventory().addItem(Blocktype, amount);//gives them the item
    5. p.updateInventory();//makes it appear in the inventory
     
  3. Offline

    PreFiXAUT

    Not that good. You should first check if the Player has enoght Space for the new Items.
    You should also give him a full example, and not some pieces.
    Show Spoiler

    Code:
    if (!(cs instanceof Player)) {
        cs.sendMessage("You have to be a Player to do this!");
        return true;
    }
    Player p = (Player) sender;
    if (p.getItemInHand() == null || p.getItemInHand().getType() == Material.AIR) {
        cs.sendMessage("You cannot convert this!");
        return true;
    }
    ItemStack itm = p.getItemInHand();
    if (itm.getType() != Material.DIAMOND_BLOCK || ADD_OTHERTHINGS_IN_HERE) {
        cs.sendMessage("You cannot convert this!");
        return true;
    }
    if (p.getInventory().firstEmpty == -1) {
        cs.sendMessage("You have no space!");
        return true;
    }
    int slots = 0;
    for (int i=0; i < p.getInventory().getSize(); i++) {
        ItemStack tmp = p.getInventory().getItem(i);
        if (tmp == null) slots++;
        else if (tmp.getType() == Material.AIR)slots++;
    }
    int amt = itm.getAmount() *9;
    if (slots >= (int) (amt/64)) {
        p.getInventory().remove(itm);
        p.getInventory().addItem(new ItemStack(Material.DIAMOND, amt));
        p.updateInventory();
        p.sendMessage("Success!");
        return true;
    } else {
        cs.sendMessage("You have no space!");
        return true;
    }

    Kinda much, but it's easy to understand.
     
  4. Offline

    Rocoty

    PreFiXAUT Really? Give OP a full example of code without explanation whatsoever, and you are okay with that? This is not how people learn to code. This is how people learn to copy code from the internet and use it in their own code until it does not work. And then what do they do? They ask for more help because they did not know what they were doing.

    Giving out free code, i.e spoonfeeding, is not the way it should be done, sorry.
     
    bobthefish and ZodiacTheories like this.
  5. Offline

    PreFiXAUT

    Sorry, but the thing is, I'm not a tutorial guy. When People would inform themself anyway, then they would probably won't have any problems.
    Also you can't say they are not gonna learn from it. Some people (like me for example) mostly need the code to understand and learn from it. It depends on the People who are doing it.
    Also the People who are just copying the Code without checking what it does won't get anywhere, and I think they know it themself. You can't be a Coder when you're just a Copycat, so when someone want's to get better, he will. He will learn maybe from the Code or from Tutorials or by own experience, but when they aren't doing anything they won't get it then.
    I don't give a ... if they are learning it. When they want it fine, then do it. I'm just here to solving the error and thats it.
     
  6. Offline

    Rocoty

    I don't want to be rude, but I would really appreciate it if you cared more about teaching rather than fixing, or simply left these forums. It is nothing more than destructive for both parts when neither wants progress.

    These forums are made so that new developers can get help with their problems. It is not a code mill. You should have a look over at http://www.coderanch.com/forums/f-33/java. Here people don't only fix, but they teach as well. This is simply because everyone there knows that the more you teach the more you help.

    If this were a forum where developers can just come post their code and get it back fixed I would have agreed with you. But this is simply not the case, and as such I get really upset when someone is being spoonfed code, because I know that in most cases the recipient of such code will gladly take it without second thought.

    Now, I know you are saying that some people, including yourself (and me), learn by looking at code and analyzing it. But I will tell you that these people will learn a lot more if they sit down and try to figure things out themselves. Try for hours, anything they can think of. This puts the brain in motion. Now, if they figure it out, good for them. They have solved their problem and learned how to fix it. The greatest lesson is, however, learned when they don't succeed. When they have tried everything they can think of and it feels like no matter what they do, there is no progress. But with just a little more information, with just a tiny push, they may be able to figure out the rest. It is in such cases where most people learn the most. Learning from their own mistakes. Learning by doing.

    I would appreciate it if you took this new knowledge into serious consideration before spoonfeeding someone code again. I also hope OP DefaultSyntax finds this post helpful. Thank you.
     
  7. Offline

    DefaultSyntax

    Rocoty PreFiXAUT bobthefish
    Thanks so much. Rocoty I'll definitely take your advice with the code that was given to me. I'll try to figure it out first and use the code PreFiXAUT gave to me. Thank you guys so much! :)

    PreFiXAUT
    Why are you returning the first 4 methods?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
Thread Status:
Not open for further replies.

Share This Page