Seed Replant Question

Discussion in 'Plugin Development' started by X_angelz_X, Jan 7, 2014.

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

    X_angelz_X

    Hello guys, I've been testing around trying to get stuff to work with a plugin I am deving which deals with if seeds are dropped or, are on the ground, then they will auto-replant themselves. Unfortunately, my code doesn't seem to be working correctly, and I am quiet unsure what I've done wrong. I am not getting any errors whatsoever in console or inside the plugin itself, here is the event handler...



    Code:java
    1. public class AutoSeedingListener implements Listener {
    2. public static AutoSeeding plugin;
    3.  
    4.  
    5. public void onItemSpawn(ItemSpawnEvent event){
    6. Item item = (Item)event.getEntity();
    7.  
    8.  
    9.  
    10. if (item.getItemStack().equals(Material.SEEDS)){
    11.  
    12. Block middletop = item.getLocation().getBlock();
    13.  
    14. Vector downvec=item.getVelocity();
    15. downvec.setX(0);
    16. downvec.setY(-1);
    17. downvec.setZ(0);
    18.  
    19. Vector upvec=item.getVelocity();
    20. upvec.setX(0);
    21. upvec.setY(1);
    22. upvec.setZ(0);
    23.  
    24. Vector tmpvec=item.getVelocity();
    25.  
    26. Location loc = middletop.getLocation().add(downvec);
    27.  
    28. Boolean abort = false;
    29.  
    30. for (int i=0; i<3; i++) {
    31. for (int j=0; j<3;j++) {
    32.  
    33. tmpvec.setX(i-1);
    34. tmpvec.setY(0);
    35. tmpvec.setZ(j-1);
    36. Location tmploc = loc.clone();
    37. tmploc.add(tmpvec);
    38.  
    39. if (tmploc.getBlock().equals(Material.SOIL)){
    40. tmploc.add(upvec);
    41. if (tmploc.getBlock().equals(Material.AIR)){
    42. tmploc.getBlock().setType(Material.CROPS);
    43. item.remove();
    44. abort=true;
    45. break;
    46. }
    47. }
    48.  
    49.  
    50. }
    51. if(abort) break;
    52.  
    53. }
    54. }
    55. }
    56.  
    57.  
    58.  
    59.  
    60.  
    61.  
    62.  
    63.  
    64.  
    65.  
    66. }


    If anyone can maybe help me out, that would be great.

    Anyone?

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

    Domi381

    X_angelz_X

    I think i know what you have done wrong.

    Code:java
    1. if (item.getItemStack().equals(Material.SEEDS))


    This is your mistake. You compared a ItemStack with a material, and it will never equal. You first have to get the material of the ItemStack and then compare it to "Material.SEEDS".

    This should be how it is right:

    Code:java
    1. if (item.getItemStack().getType().equals(Material.SEEDS))


    I hope I helped you out and it is working now.

    Gz Domi381
     
  3. Offline

    Splated

    http://dev.bukkit.org/bukkit-plugins/smart-items/

    I cycle through all the Item stacks on the ground every 10 ticks.

    If the block below is dirt and the one its in is air.
    It plants nearby soil also and removes one from the stack until the stack is gone.


    Code:java
    1. Collection<Item> itemlist = world.getEntitiesByClass(Item.class);
    2. for(Item item: itemlist){
    3. Block blockAbove = item.getLocation().getBlock().getRelative(0, 1, 0);
    4. Block blockin = item.getLocation().getBlock();
    5. if (blockBelow.getType() == Material.SOUL_SAND || blockBelow.getType() == Material.SOIL){
    6.  
    7. if (tools.canIbuild(item.getLocation()) ){
    8. int size=3;
    9.  
    10.  
    11. Loop:{
    12.  
    13. int x=0, z=0, dx = 0, dz = -1;
    14. int t = size;
    15. int maxI = t*t;
    16.  
    17. for (int i=0; i < maxI; i++){
    18.  
    19. if ((-size < x && x <= size) && (-size < z && z <= size)) {
    20.  
    21.  
    22. Block TmpblockBelow = blockBelow.getRelative(x, 0, z);
    23. Block Tmpblockin = blockin.getRelative(x, 0, z);
    24.  
    25. // Netherwarts
    26. if ((TmpblockBelow.getType() == Material.SOUL_SAND) &&
    27. (Tmpblockin.getType() == Material.AIR) &&
    28. (item.getItemStack().getType().getId() == 372)) { //netherwart id
    29. Tmpblockin.setType(Material.NETHER_WARTS);
    30. tools.remove1(item);
    31. break Loop;
    32. }
    33.  
    34.  
    35. // Crops
    36. if (TmpblockBelow.getType() == Material.SOIL &&
    37. Tmpblockin.getType() == Material.AIR){
    38.  
    39.  
    40.  
    41. if (item.getItemStack().getType() == Material.SEEDS ) {
    42. Tmpblockin.setType(Material.CROPS);
    43. tools.remove1(item);
    44. break Loop;
    45. }
    46.  
    47. if (item.getItemStack().getType() == Material.MELON_SEEDS) {
    48. Tmpblockin.setType(Material.MELON_STEM);
    49. tools.remove1(item);
    50. break Loop;
    51. }
    52.  
    53. if (item.getItemStack().getType() == Material.PUMPKIN_SEEDS ) {
    54. Tmpblockin.setType(Material.PUMPKIN_STEM);
    55. tools.remove1(item);
    56. break Loop;
    57. }
    58.  
    59.  
    60.  
    61. if (item.getItemStack().getType() == Material.CARROT_ITEM ) {
    62. Tmpblockin.setType(Material.CARROT);
    63. tools.remove1(item);
    64. break Loop;
    65. }
    66.  
    67. if (item.getItemStack().getType() == Material.POTATO_ITEM ) {
    68. Tmpblockin.setType(Material.POTATO);
    69. tools.remove1(item);
    70. break Loop;
    71. }
    72.  
    73.  
    74.  
    75. }
    76.  
    77.  
    78.  
    79.  
    80.  
    81.  
    82. }
    83.  
    84. if( (x == z) || ((x < 0) && (x == -z)) || ((x > 0) && (x == 1-z))){
    85. t=dx; dx=-dz; dz=t;
    86. }
    87. x+=dx; z+=dz;
    88. }
    89. }//loop
    90.  
    91.  
    92.  
    93. }
     
  4. Offline

    X_angelz_X


    Testing this out as we speak! Thank you if it works!

    Also, thank you very much for allowing me to look to see how that works, seems very interesting say the least. Mine is no where near as good as yours xD
     
Thread Status:
Not open for further replies.

Share This Page