Check if player has amount of item multiplied by X

Discussion in 'Plugin Development' started by khave, Feb 9, 2014.

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

    khave

    Hello again.

    So how do I check if the player has like:
    16 cobblestone, it then takes that from him and drops 1 iron ore, BUT if the player has 32 it will drop 2 iron ores (16 * 2 = 32).
    So it'll check if the player has 16 * X amount of items and then drop the X amount of cobble corresponding to that.

    Thanks for the help in advance.
     
  2. Offline

    felixfritz

    So create an integer that is the amount of cobblestone divided by 16?
    Code:java
    1. int amount = item.getAmount();
    2.  
    3. int ironAmount = amount / 16;
    4. //if amount < 16, it will be 0
    5. //if amount >= 16 and amount < 32, it will be 1, etc.
     
  3. Offline

    khave

    Code:java
    1.  
    2.  



    What I said was wrong. It was supposed to be in the 16 table of multiplication.
    So if it's:
    16 > 1 iron
    32 > 2 iron
    48 > 3 iron
    64 > 4 iron

    And so on.

    Why the hell is this in code?

    Aha!
     
  4. Offline

    Harmings

    khave
    This what you mean?
    ItemStack[] inv = p.getInventory().getContents();
    for (ItemStack contents : inv) {
    if (contents.getType() == Material.COBBLESTONE) {
    p.getInventory().addItem(new ItemStack(Material.IRON_INGOT, contents.getAmount() / 16));
    }
    }
     
  5. Offline

    khave

    No, that gives me an error in the console D:
     
  6. Offline

    Harmings

    khave
    Can you post the stacktrace?
     
  7. Offline

    khave


    If that means the error in the console then here you go:

    I think it's because there are some numbers you can't divide by 16. But don't quote me on that. I'm not the best at maths.

    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:481) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:466) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:191) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.PlayerInteractManager.interact(PlayerInteractManager.java:374) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:628) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.a(SourceFile:60) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.handle(SourceFile:9) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    Caused by: java.lang.NullPointerException
    at me.khave.listeners.Transmuting.onPlayerInteract(Transmuting.java:49) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0-ea]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0-ea]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0-ea]
    at java.lang.reflect.Method.invoke(Method.java:601) ~[?:1.7.0-ea]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    ... 15 more
     
  8. Offline

    Harmings

    khave
    Correct, can you post line 49 in the Transmuting class?
     
  9. Offline

    khave


    Code:java
    1. ItemStack[] inv = p.getInventory().getContents();
    2. for (ItemStack contents : inv) { //Line 49
    3. if (contents.getType() == Material.COBBLESTONE) {
    4. p.getInventory().removeItem(new ItemStack(Material.COBBLESTONE, contents.getAmount() / 16));
    5. p.getWorld().dropItem(clicked.getLocation().add(0, 2, 0), new ItemStack(Material.IRON_ORE, contents.getAmount() / 16));
    6. }
    7. }
     
  10. Offline

    Harmings

    khave
    Try changing line 3 in your last post to
    Code:
    if (contents != null && contents.getType() == Material.COBBLESTONE) {
     
  11. Offline

    khave


    It didn't give me an error, but I had 16 + 16 cobble, so it should've given me 2 iron ores, but it only gave me one?
     
  12. Offline

    Harmings

    khave
    32 divided by 16 is 2 so it should drop two... I'm not sure why it would only drop one, maybe someone else knows?
     
  13. Offline

    khave


    Thank you for getting this far with me ^-^
     
  14. Offline

    Harmings

    khave
    No problem, glad to help as much as I could:)
     
  15. Offline

    khave

    Can't I make a while or for-loop? Like while the player has 16 cobblestone, remove 16 cobblestone, spew out 1 iron ore?
     
Thread Status:
Not open for further replies.

Share This Page