First plugin help

Discussion in 'Plugin Development' started by dadus33, Aug 15, 2014.

Thread Status:
Not open for further replies.
  1. Hello there guys!
    As the title suggests, I am new to making plugins and coding in java. I watched some tutorials on how to learn java/do plugins and such but there are only 2 days since I really started to understand what the guys in the tutorials were doing.
    The plugin I am trying to make aims to make fully-automated wheat farming possible in vanilla minecraft (with plugins, of course). Here's what it should do:
    1. allow players to select a chest with seeds
    2. keep the seed chest in memory (for each player)
    3. take seeds from the chest whenever a users activates redstone placed under unplanted farmland
    4. place seeds above the farmland

    I managed to get the features 3 and 4 working (without the part with taking seeds from the chest) and I know how to get the block a user interacts with, but the problem is, how to keep the chest's location in memory for each player (even after the player logs of or after the server restarts) and how to take seeds out of the chest? I know how to take items out of a player's inventory, but blocks don't allow you to get their inventory (at last not from what I know).

    Sorry for my bad english and thanks in advance!

    Please, I really need help with this guys...

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

    teej107

    dadus33 Patience. If you are new to Java, continue learning it until you feel like you understand it. Then it will be easy to make plugins.
     
  3. I just want to find a way to store data....
     
  4. Offline

    teej107

  5. I know how to create a config file and read/write it. But I though there must be another way plugins store data. I mean, let's take LWC. It uses a .db file to store data. How do I do this?
     
  6. Offline

    teej107

  7. Offline

    BeastCraft3

    dadus33
    We dont give out the codes for you, You'll need to show us how far you've got and we will give you tips.
     
  8. Offline

    teej107

    BeastCraft3 Unfortunately some people do. I myself have only coded with databases once and I am very rusty on it. I am giving dadus33 the same approach I would use.
     
  9. I don't want you to give me your code 0_0. This would be stupid. I only wanted to ask if anyone knows how can I do what I want. That means, I'm requesting some tips.
     
  10. Offline

    teej107

    dadus33 The tips I gave you are links on how to do what you want to achieve.
     
  11.  
  12. Offline

    teej107

  13. Ah, sorry, I didn't see that link, I only sawy the one about the config API.
     
  14. Offline

    TopTobster5

    dadus33 If you are new to Bukkit and Java, I would say writing to a yml file is easier to learn/do rather than a database. Especially for your first plugin.

    http://wiki.bukkit.org/Configuration_API_Reference#Mirroring_the_JavaPlugin_implementation : This is useful for creating your own yml files, that are different from your config.

    https://forums.bukkit.org/threads/tutorial-using-mysql-in-your-plugins.132309/ If you really want to use a database, I found this helpful for my plugin which used it.
     
  15. I think I will use a yml. Thanks!

    Guys, I need some hep again. I tried to use HashMaps to keep a boolean (for each player) which defaults to false, but after a player uses /dirt select it sets to true. I should then use the boolean inside an EventHandler, but for some reason I can't acces the hashmap. Here's my code:
    Code:java
    1. package me.dadus33.DIRT;
    2.  
    3. import java.io.File;
    4. import java.util.HashMap;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Material;
    9. import org.bukkit.block.Block;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.block.BlockRedstoneEvent;
    16. import org.bukkit.event.player.PlayerInteractEvent;
    17. import org.bukkit.plugin.PluginManager;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20. public class main extends JavaPlugin implements Listener {
    21.  
    22. public void onEnable(){
    23. if(!new File(this.getDataFolder(), "config.yml").exists()) {
    24. this.saveDefaultConfig();
    25. }
    26. PluginManager pm = Bukkit.getPluginManager();
    27. pm.registerEvents(this, this);
    28. getLogger().info("DIRT has been enabled!");
    29. }
    30. public void onDisable(){
    31. getLogger().info("DIRT has been disabled!");
    32. }
    33.  
    34. static HashMap<String, Boolean> map = new HashMap<>();
    35. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    36. if(args.length==0){
    37. if (cmd.getName().equalsIgnoreCase("dirt")){
    38. sender.sendMessage(ChatColor.GREEN+"This server is using AwesomeDIRT v"+getDescription().getVersion());
    39. }
    40.  
    41. }
    42. else
    43. {
    44. if(sender instanceof Player && args[0].equalsIgnoreCase("select")){
    45. Player p = (Player) sender;
    46. map.put(p.getName(), true); //sets to true
    47. p.sendMessage(ChatColor.GREEN+"Please select a chest!");
    48. }
    49. }
    50.  
    51. return false;
    52. }
    53.  
    54. Material farmland = Material.SOIL;
    55. @EventHandler
    56.  
    57. public void onRedstone(BlockRedstoneEvent event) {
    58. Block block = event.getBlock();
    59. Material above = block.getLocation().add(0, 1, 0).getBlock().getType();
    60. if(above == farmland){
    61. Block above2 = block.getLocation().add(0,2,0).getBlock();
    62. if(above2.getType() == Material.AIR){
    63. above2.setType(Material.CROPS);
    64. }
    65. }
    66.  
    67.  
    68. }
    69. public void onRgihtClick(PlayerInteractEvent event) {
    70. for (Player i : Bukkit.getOnlinePlayers()){
    71. if(map.get(i.getName())==true){ //the if doesn't work! Why?
    72. i.sendMessage(ChatColor.GREEN+"Seed chest selected!");
    73. getConfig().getList("players").add(i.getName());
    74. }
    75. }
    76. }
    77.  
    78.  
    79. }


    Bump.

    anybody?

    bump again.

    Why nobody answers?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  16. dadus33 you can only bump every 24 hours....

    answer: are you getting any errors?
     
  17. no. just nothing happens.
     
  18. dadus33 in that case, its just not true... (the boolean)
     
  19. it must be true. it is true outside the EventHandler. I tried to log it's value to the console outside and inside the event handler. outside it shows true, inside shows nothing, nor true nor false!
     
  20. dadus33 is the event even firing?
     
  21. Hmm, it should. I mean, it shoud fire whenever I interact with a block. I'm checking it now

    Umm, yeah, the event doesn't fire, but why?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  22. dadus33 I think you have to add a @EventHandler
     
  23. I have.

    And by the way, in the EventHandler, I've got two events. One of them fires, one of them doesn't.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  24. dadus33 add another one, I ment (and: use the edit button and prevent spam!)
     
  25. Oh, nevermind, it was my fault.... I forgot I had to put an @EventHandler before each event handler. Sorry, and thx for help!
     
  26. dadus33 you're welcome :D (Please mark le thread as solved!)
     
  27. Apparently, it throws an exception. It was caused by a compilation error, at line 73 in the code I posted. I can't use "add" for strings. But then how do I add a string to a list in the config.yml?
     
  28. dadus33 I think doing getConfig().getStringList("players").add(string) does work
     
    dadus33 likes this.
  29. Now I get NullPointerException on line 73.... That is the line with the if.

    Ah, I managed to fix that, thanxs for the help anyways! [diamond]

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  30. dadus33 there's no IF on line 73? (I'll help you if I can, but we should do it via IRC, or skype. If you have any of those, tell me)

    edit: ohloal, you fixed it, nevermind
     
Thread Status:
Not open for further replies.

Share This Page