First plugin help please

Discussion in 'Plugin Development' started by 2toetommy, Aug 12, 2014.

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

    2toetommy

    I am messing around with my first plugin. Done some tuts on player commands and handling events. Now I wanted to do something with PlayerDeath. So soemthing like on player death collect inventory drop chest @location fill chest with inventory.
    I get no errors and nothing in my death even happens as far as I can tell.

    Here is the code - This is litteraly the first time I have written ANY java so be nice please :oops:
    Code:
    package io.github.bloodynoseproduction;
     
    import java.util.Arrays;
     
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.block.Chest;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.inventory.ItemStack;
     
    public class GraveplaceListener implements Listener {
        @EventHandler
        public void PlayerDeath(PlayerDeathEvent event) {
            Player player = event.getEntity();
            LivingEntity player = event.getEntity();
            int x = player.getLocation().getBlockX();
            int y = player.getLocation().getBlockY();
            int z = player.getLocation().getBlockZ();       
            player.sendMessage("You died at " + "X: " + x + "Y: " + y + "Z: " + z);
            World w = player.getWorld();
            Location loc = new Location(w, x, y, z);
            loc.getBlock().setType(Material.CHEST);
            player.sendMessage("A chest was placed at death point");
            ItemStack[] drops = toItemStack(event.getDrops());
            String DropsString = Arrays.toString(drops);
            player.sendMessage("Your inventory was stored in an array.");
            player.sendMessage("///////////////////////////////////////////");
            player.sendMessage(DropsString);
            player.sendMessage("///////////////////////////////////////////");
            Chest c = (Chest) loc.getBlock().getState();
                c.getBlockInventory().[U]setContents[/U]((ItemStack)drops);
            player.sendMessage("A chest has been placed with your items in it at" + "X: " + x + " Y: " + y + " Z: " + z);
     
            }
        public ItemStack[] toItemsStack(List<ItemStack> list) {
            ItemStack[] items = new ItemStack[list.size()];
            int index = 0;
            for (ItemStack i : list) {
                index++;
            }
        }
     
    }
    Link to github HERE
     
  2. Have you put a
    Code:java
    1.  
    2. getServer().getPluginManager().registerEvents(new GraveplaceListener(this), this);

    In your onEnable in your main class?
     
  3. Offline

    2toetommy

    I have
    Code:java
    1. @Override
    2. public void onEnable() {
    3. // TODO
    4. getLogger().info("onEnable has been invoked!");
    5.  
    6. //new PlayerListener(this);
    7. getServer().getPluginManager().registerEvents(new GraveplaceListener(this), this);
    8. }
    9.  


    and your saying it should be
     
  4. 2toetommy
    I can't imagine why you wouldn't be at least getting an error, if it doesn't work, and you've registered your events...
    Does it send you the messages when you die?
     
  5. Offline

    Skye

    2toetommy It doesn't work because you're passing your plugin instance when creating a new GraveplaceListener, but that class has no constructor for that argument.
     
  6. Skye This guy says he's new... He may not understand what you're talkin bout...
    Heck.... I'm not new, and i can only guess that you mean that......
    In the graveplacelistener class, he should have a constructor such as...
    Code:java
    1.  
    2. public GraveplaceListener(NameOfMainClass plugin){
    3. this.main = plugin;
    4. }
    5. NameOfMainClass main;
     
  7. Offline

    2toetommy

    I do not

    Code:java
    1.  
    2. public class GraveplaceListener implements Listener {
    3. @EventHandler
    4. public void PlayerDeath(PlayerDeathEvent event) {
    5. Player player = event.getEntity();
     
  8. 2toetommy
    Welp... Make one.
    You can even use the example I gave you... Just replace the "NameOfMainClass" with the actual name of your main class (caps count). And you should be all set.
     
  9. Offline

    Skye

    xYourFreindx If there was something he didn't understand, he'd look up the word, like "instance" or "constructor," both words which should be understood before doing anything in an OO language.

    While your solution would work, there's not much point in creating a constructor for a class asking for arguments they don't need. The better solution would be to remove the arguments he passes when he instantiates the listener class.
     
    Lurvik and ZodiacTheories like this.
  10. Skye
    I think it's more complex than looking up the word.
    Java, you need to learn two new languages... The java code, and how to read/write it in english.
    Most self taught people seldom hear java spoken in english terms, so while you might be talking about doing something they know how to do, they won't understand what you're trying to say.
    So, in response to your better suggestion... Could you give us an example?
    We'd both be interested in knowing how to better improve our code, I'm sure.
     
  11. Offline

    ZodiacTheories

    xYourFreindx

    What about those that don't speak english?
     
  12. ZodiacTheories Now there, you bring up a good point. So good in fact, that I cannot possibly comment on it.
     
    ZodiacTheories likes this.
  13. Offline

    Skye

    xYourFreindx You already posted an example of a constructor, which requires an understanding of class instantiation to use. I'm speaking in Java terms that are pretty much the first thing people learn when they pick up the language. This forum is for help with the Bukkit API, not teaching people how to program.
     
    Lurvik likes this.
  14. Offline

    2toetommy

    Okay so this don't feel right but I compleatly deleted the class and am starting over. here is my graveplaceListener class -- also is the structure all wrong here ?
    [​IMG]

    Code:java
    1. package io.github.bloodynoseproduction;
    2.  
    3. public class GraveplaceListener(UsefullStuff plugin) {
    4. this.main = plugin;
    5.  
    6. }
    7.  
    8. UsefullStuff main;


    Not to knock gettign help or anything, but it seems that if you didnt want to help then you would just not reply. Using straight java terms and talking down to someone trying to learn is really not helping you or my self.
    So I guess thanks but no need to continue if you dont like the questions I am asking. Thank you.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  15. Nonono.... The constructor does not replace your class....
    Here... Like this....
    Code:java
    1. package io.github.bloodynoseproduction;
    2.  
    3. import java.util.Arrays;
    4.  
    5. import org.bukkit.Location;
    6. import org.bukkit.Material;
    7. import org.bukkit.World;
    8. import org.bukkit.block.Block;
    9. import org.bukkit.block.Chest;
    10. import org.bukkit.entity.LivingEntity;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.entity.EntityDeathEvent;
    15. import org.bukkit.event.entity.PlayerDeathEvent;
    16. import org.bukkit.inventory.ItemStack;
    17.  
    18. public class GraveplaceListener implements Listener {
    19.  
    20. public GraveplaceListener(UsefullStuff plugin){
    21. this.main= plugin;
    22. UsefullStuff main;
    23.  
    24. @EventHandler
    25. public void PlayerDeath(PlayerDeathEvent event) {
    26. Player player = event.getEntity();
    27. LivingEntity player = event.getEntity();
    28. int x = player.getLocation().getBlockX();
    29. int y = player.getLocation().getBlockY();
    30. int z = player.getLocation().getBlockZ();
    31. player.sendMessage("You died at " + "X: " + x + "Y: " + y + "Z: " + z);
    32. World w = player.getWorld();
    33. Location loc = new Location(w, x, y, z);
    34. loc.getBlock().setType(Material.CHEST);
    35. player.sendMessage("A chest was placed at death point");
    36. ItemStack[] drops = toItemStack(event.getDrops());
    37. String DropsString = Arrays.toString(drops);
    38. player.sendMessage("Your inventory was stored in an array.");
    39. player.sendMessage("///////////////////////////////////////////");
    40. player.sendMessage(DropsString);
    41. player.sendMessage("///////////////////////////////////////////");
    42. Chest c = (Chest) loc.getBlock().getState();
    43. c.getBlockInventory().[U]setContents[/U]((ItemStack)drops);
    44. player.sendMessage("A chest has been placed with your items in it at" + "X: " + x + " Y: " + y + " Z: " + z);
    45.  
    46. }
    47. public ItemStack[] toItemsStack(List<ItemStack> list) {
    48. ItemStack[] items = new ItemStack[list.size()];
    49. int index = 0;
    50. for (ItemStack i : list) {
    51. index++;
    52. }
    53. }
    54.  
    55. }

    I personally like this constructor over what whatshisface said, because if you ever need to call back to the main class, to say, idk, get something from the config file... You can just to "main.getConfig()". Very simple.
     
  16. Offline

    Necrodoom

  17. Offline

    2toetommy

    Ahh okay that make so much more sence. Thanks a bunch. I will try that.
     
  18. Offline

    Skye

    Therefore I must want to help. I was trying to bring your attention to something you should learn before proceeding, with a self-help overtone. The key to becoming a better programmer is the ability to teach yourself. Giving you just enough to solve your problem on your own does way more for you than pasting code snippets, but I guess you're speaking from a position of experience. :rolleyes:
     
    xYourFreindx likes this.
  19. Necrodoom It would not?
    Where have I errored?

    Skye I prefer your style of teaching... But, the reality is, most people on here, (in need of help) start learning java through bukkit api, and have never taken a legitimate course. So, therfore, they have little to no knowledge of the terms you speak with.
    I've been the speaker, and the listener in the cases where these terms are not understood. And it is frusterating. It is frustrating for both sides, and I don't have the patience for that. So when speaking, I use very non-technical terms, and use code as example for what I mean.
    I'd love to learn the term, what they mean in java, everything in between, and if you've got the patience, I've been looking for a java tutor (see my signature :p)

    oops, 2toetommy , Necrodoom was right! I just looked up... I forgot the close bracket!!
    It should look like this...
    Code:java
    1. public GraveplaceListener(UsefullStuff plugin){
    2. this.main= plugin;
    3. }
    4. UsefullStuff main;
    5.  

    NOT
    Code:java
    1. public GraveplaceListener(UsefullStuff plugin){
    2. this.main= plugin;
    3. UsefullStuff main;
    4.  


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

    2toetommy

    I differently rather learn then copy paste that's for sure. It's just pretty clear that this is my very first java anything. And I do appreciate that help really. My biggest issue is that I don't learn like most and kinda have to pick things apart in no spacific order, so grabbing a TUT or class lesson do not help me at all hence why I didn't know what a "instance" or "constructor" is till now/ish.But hey thanks.

    ya caught that figured it was a fast type mistake. np.

    So it is triggering it now so thats progress lol but I get errors...as expected. If anyone wants to point me in the right direction.

    Code:java
    1. [01:44:55 ERROR]: Could not pass event PlayerDeathEvent to UsefullStuff v0.0.1b
    2. org.bukkit.event.EventException
    3. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    4. va:294) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    5. at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    6. a:62) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    7. at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    8. ava:501) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    9. at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    10. ava:486) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    11. at org.bukkit.craftbukkit.v1_7_R4.event.CraftEventFactory.callPlayerDeat
    12. hEvent(CraftEventFactory.java:379) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga0
    13. 4b586-b3107jnks]
    14. at net.minecraft.server.v1_7_R4.EntityPlayer.die(EntityPlayer.java:369)
    15. [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    16. at net.minecraft.server.v1_7_R4.EntityLiving.damageEntity(EntityLiving.j
    17. ava:736) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    18. at net.minecraft.server.v1_7_R4.EntityHuman.damageEntity(EntityHuman.jav
    19. a:758) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    20. at net.minecraft.server.v1_7_R4.EntityPlayer.damageEntity(EntityPlayer.j
    21. ava:448) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    22. at net.minecraft.server.v1_7_R4.EntityLiving.b(EntityLiving.java:865) [c
    23. raftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    24. at net.minecraft.server.v1_7_R4.EntityHuman.b(EntityHuman.java:1362) [cr
    25. aftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    26. at net.minecraft.server.v1_7_R4.Entity.a(Entity.java:784) [craftbukkit.j
    27. ar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    28. at net.minecraft.server.v1_7_R4.EntityLiving.a(EntityLiving.java:147) [c
    29. raftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    30. at net.minecraft.server.v1_7_R4.EntityPlayer.b(EntityPlayer.java:557) [c
    31. raftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    32. at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java
    33. :419) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    34. at net.minecraft.server.v1_7_R4.PacketPlayInFlying.a(SourceFile:137) [cr
    35. aftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    36. at net.minecraft.server.v1_7_R4.PacketPlayInPosition.handle(SourceFile:6
    37. 3) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    38. at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:157
    39. ) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    40. at net.minecraft.server.v1_7_R4.ServerConnection.c(SourceFile:134) [craf
    41. tbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    42. at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:6
    43. 67) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    44. at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    45. 58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    46. at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    47. 58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    48. at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    49. :469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    50. at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    51. 28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    52. Caused by: java.lang.Error: Unresolved compilation problems:
    53. Duplicate local variable player
    54. The method sendMessage(String) is undefined for the type LivingEntity
    55. The method sendMessage(String) is undefined for the type LivingEntity
    56. The method toItemStack(List<ItemStack>) is undefined for the type Gravep
    57. laceListener
    58. The method sendMessage(String) is undefined for the type LivingEntity
    59. The method sendMessage(String) is undefined for the type LivingEntity
    60. The method sendMessage(String) is undefined for the type LivingEntity
    61. The method sendMessage(String) is undefined for the type LivingEntity
    62. Syntax error on token ".", Identifier expected after this token
    63. U cannot be resolved to a variable
    64. Syntax error, insert "AssignmentOperator Expression" to complete Assignm
    65. ent
    66. Syntax error, insert ";" to complete Statement
    67. setContents cannot be resolved to a variable
    68. Syntax error on token "/", delete this token
    69. U cannot be resolved to a variable
    70. Syntax error on token "]", AssignmentOperator expected after this token
    71. Cannot cast from ItemStack[] to ItemStack
    72. The method sendMessage(String) is undefined for the type LivingEntity
    73.  
    74. at io.github.bloodynoseproduction.GraveplaceListener.PlayerDeath(Gravepl
    75. aceListener.java:30) ~[?:?]
    76. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    77. _11]
    78. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    79. _11]
    80. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    81. .8.0_11]
    82. at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_11]
    83. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    84. va:292) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    85. ... 23 more
    86. [01:44:55 INFO]: 2toetommy fell from a high place


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  21. This whole area here tells you all your problems...
    Code:
    Caused by: java.lang.Error: Unresolved compilation problems:
            Duplicate local variable player
            The method sendMessage(String) is undefined for the type LivingEntity
            The method sendMessage(String) is undefined for the type LivingEntity
            The method toItemStack(List<ItemStack>) is undefined for the type Gravep
    laceListener
            The method sendMessage(String) is undefined for the type LivingEntity
            The method sendMessage(String) is undefined for the type LivingEntity
            The method sendMessage(String) is undefined for the type LivingEntity
            The method sendMessage(String) is undefined for the type LivingEntity
            Syntax error on token ".", Identifier expected after this token
            U cannot be resolved to a variable
            Syntax error, insert "AssignmentOperator Expression" to complete Assignm
    ent
            Syntax error, insert ";" to complete Statement
            setContents cannot be resolved to a variable
            Syntax error on token "/", delete this token
            U cannot be resolved to a variable
            Syntax error on token "]", AssignmentOperator expected after this token
            Cannot cast from ItemStack[] to ItemStack
            The method sendMessage(String) is undefined for the type LivingEntity
    
     
  22. Offline

    MCMatters

    in ur code remove LivingEntity player = event.getEntity();
     
    xYourFreindx likes this.
  23. Offline

    2toetommy

    I made these changes, no it is not triggering even a error.

    Code:java
    1. package io.github.bloodynoseproduction;
    2.  
    3. import java.util.Arrays;
    4.  
    5. import java.util.List;
    6.  
    7. import org.bukkit.Location;
    8. import org.bukkit.Material;
    9. import org.bukkit.World;
    10. import org.bukkit.block.Block;
    11. import org.bukkit.block.Chest;
    12. import org.bukkit.entity.LivingEntity;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.EventHandler;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.entity.EntityDeathEvent;
    17. import org.bukkit.event.entity.PlayerDeathEvent;
    18. import org.bukkit.inventory.ItemStack;
    19.  
    20. public class GraveplaceListener implements Listener {
    21.  
    22. private UsefullStuff main;
    23. public GraveplaceListener(UsefullStuff plugin){
    24. this.main= plugin;
    25. UsefullStuff main;
    26. }
    27. @EventHandler
    28. public void PlayerDeath(PlayerDeathEvent event) {
    29. Player player = event.getEntity();
    30.  
    31. int x = player.getLocation().getBlockX();
    32. int y = player.getLocation().getBlockY();
    33. int z = player.getLocation().getBlockZ();
    34. player.sendMessage("You died at " + "X: " + x + "Y: " + y + "Z: " + z);
    35. World w = player.getWorld();
    36. Location loc = new Location(w, x, y, z);
    37. loc.getBlock().setType(Material.CHEST);
    38. player.sendMessage("A chest was placed at death point");
    39. ItemStack[] drops = toItemsStack(event.getDrops());
    40. String DropsString = Arrays.toString(drops);
    41. player.sendMessage("Your inventory was stored in an array.");
    42. player.sendMessage("///////////////////////////////////////////");
    43. player.sendMessage(DropsString);
    44. player.sendMessage("///////////////////////////////////////////");
    45. Chest c = (Chest) loc.getBlock().getState();
    46. c.getBlockInventory().setContents((ItemStack[])drops);
    47. player.sendMessage("A chest has been placed with your items in it at" + "X: " + x + " Y: " + y + " Z: " + z);
    48.  
    49. }
    50. public ItemStack[] toItemsStack(List<ItemStack> list) { //<--- this is only line with error in Eclips "Must return a result of type ItemStack[]
    51. ItemStack[] items = new ItemStack[list.size()];
    52. int index = 0;
    53. for (ItemStack i : list) {
    54. index++;
    55. }
    56. return ItemStack[]; //<--- this is what I assume it is asking for
    57. }
    58.  
    59. }
     
  24. Code:java
    1.  
    2. public GraveplaceListener(UsefullStuff plugin){
    3. this.main= plugin;
    4. }
    5. UsefullStuff main;
    6.  


    Notice that ^ is outside the brackets.

    Everything else looks fine from a glance.
     
  25. Offline

    2toetommy

    HAHA didn't see that before nice.
    not one of the last things to throw me a error is this.

    Code:java
    1. public ItemStack[] toItemsStack(List<ItemStack> list) { //<--- This method must return a result of type ItemStack[]
    2. ItemStack[] items = new ItemStack[list.size()];
    3. int index = 0;
    4. for (ItemStack i : list) {
    5. index++;
    6. }


    So I tryed , neather worked no consol errors

    Code:java
    1. public ItemStack[] toItemsStack(List<ItemStack> list) {
    2. ItemStack[] items = new ItemStack[list.size()];
    3. int index = 0;
    4. for (ItemStack i : list) {
    5. index++;
    6. }
    7. return items;
    8. // &
    9. return ItemStack[];
     
  26. 2toetommy
    I've not dealt with item handling... I'm sure there are a thousand people better qualified to diagnose your problem than I....
    But I do have a suggestion...
    When the players die, take their exp, and store it in an exp bottle in the chest.
     
  27. Offline

    MCMatters

     
  28. Offline

    2toetommy

    SO keeping this the way it is should work ?

    Code:java
    1. public ItemStack[] toItemsStack(List<ItemStack> list) { //<--- This method must return a result of type ItemStack[]
    2. ItemStack[] items = new ItemStack[list.size()];
    3. int index = 0;
    4. for (ItemStack i : list) {
    5. index++;
    6. }
     
  29. Offline

    MCMatters

  30. Offline

    2toetommy

    Sorry I havent replyed in a minute but I actually mad a lot of progress in the last day or 2.
    I have the plugin working with more features now to.
    Thanks for all the help you have given.

    You can check it out in my github in my signature. UsefullStuff is the project.
     
Thread Status:
Not open for further replies.

Share This Page