[SOLVED]Creating/Calling a Configuration File?

Discussion in 'Plugin Development' started by HappyPikachu, Nov 15, 2011.

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

    HappyPikachu

    Essentially, if the block under the vehicle has a block ID of 18 (Leaves), something cool happens. However, I'd like the user to be able to change the block ID that's set to be used via some sort of external configuration file.

    Code:java
    1. public class RandomPluginVehicleListener extends VehicleListener{
    2. public void onVehicleUpdate(VehicleUpdateEvent event){
    3. if (event.getVehicle() instanceof Vehicle){
    4. Location vehicleLocation = event.getVehicle().getLocation();
    5. vehicleLocation.setY(Math.floor(block.getY())-1);
    6. if (vehicleLocation.getBlock().getTypeId() == 18){
    7. //Profit
    8. }
    9. }
    10. }
    11. }

    I tried screwing around with YAML, but I can't figure how to get the VehicleListener to call anything from the file that's generated. How can I make this work? I'm open to new ideas.

    Code:java
    1. public class RandomPlugin extends JavaPlugin{
    2. public void onEnable(){
    3. try{
    4. final FileConfiguration fc = this.getConfig();
    5. //If config.yml doesn't already exist
    6. if (!new File(this.getDataFolder(), "config.yml").exists()){
    7. fc.addDefault("BlockIDs.Action1", 18);
    8. fc.options().copyDefaults(true);
    9. this.saveConfig();
    10. fc.options().copyDefaults(false);
    11. }
    12. }catch(Exception e1){
    13. e1.printStackTrace();
    14. }
    15. }
    16. }
     
  2. Offline

    Lex Talionis

    Where you have 'this.saveConfig()', you need to tell it to copy the defaults to the config.yml. This is just how I do it:
    Code:
    this.getConfig().options().copyDefaults(true);
    this.saveConfig();
    this.getConfig().options().copyDefaults(false);
    When getConfig() is called for the first time it automagically loads the config.yml into memory. Then you just have to use:
    Code:
    this.getConfig().getInt("BlockIDs.Action1");
     
  3. Offline

    HappyPikachu

    I actually had that in my code. I took it out when I posted here because I thought it wasn't doing anything. I'll fix the post. ;-;

    As for your second suggestion, the first block of code I posted is in a separate class than that of the other. The first extends VehicleListener, the other extends JavaPlugin. You obviously know more about this than I do, but I suspect that's why I get errors when I try to implement your code - I can't extend JavaPlugin in the first one.
     
  4. Offline

    Lex Talionis

    I'm no expert at anything, just learning myself. I've spent a LOT of time pouring over the config.yml info over the past few weeks though. Anywho, I don't think you need to extend JavaPlugin. In the constructor of the first file that you posted you need to set it up to get the instance of your main file. Like:
    Code:
    private MyMainClass plugin;
    MyListenerClass(MyMainClass myPlugin) {
        plugin = myPlugin;
    }
    'if (block.getBlock().getTypeId() == 18){' becomes:
    Code:
    if (block.getBlock().getTypeId() == plugin.getConfig().getInt("BlockIDs.Action1"){
    Then in your main class file when you register your listener you would use:
    Code:
    pluginManager.registerEvent(Type.EVENT_TYPE, new MyListenerClass(this), Priority.Lowest, this);
     
  5. Offline

    HappyPikachu

    Time well spent.

    I've got the listener registered. Everything looks alright in Eclipse, but after exporting, starting up the server, and entering a vehicle, I start getting error spam once the server reads the line containing:

    Code:java
    1. if (block.getBlock().getTypeId() == plugin.getConfig().getInt("BlockIDs.Action1")){

    Show Spoiler
    [SEVERE] Could not pass event VEHICLE_UPDATE to RandomPlugin
    java.lang.NullPointerException
    at me.happypikachu.RandomPlugin.RandomPluginVehicleListener.onVehicleUpdat
    e(RandomPluginVehicleListener.java:69)
    at org.bukkit.plugin.java.JavaPluginLoader$90.execute(JavaPluginLoader.j
    ava:890)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:58)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:339)
    at net.minecraft.server.EntityMinecart.s_(EntityMinecart.java:561)
    at net.minecraft.server.World.entityJoinedWorld(World.java:1193)
    at net.minecraft.server.WorldServer.vehicleEnteredWorld(WorldServer.java
    :109)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:247)
    at net.minecraft.server.Packet10Flying.a(SourceFile:126)
    at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:92)
    at net.minecraft.server.NetworkListenThread.a(SourceFile:108)
    at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:471)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:374)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:417)

    Is there a certain event type that I need to register besides VEHICLE_UPDATE? Confusion ensues.
     
  6. Offline

    Lex Talionis

    This is probably about where my thorough study ends, but if you don't have it already you may need:
    Code:
    if (event.getEntity() instanceOf Vehicle) {
        if (block.getBlock().getTypeId() == plugin.getConfig().getInt("BlockIDs.Action1") {
            //Whatever other code is in there...
        }
    }
    The only other time I've encountered the "Could not pass event XYZ to MyPlugin..." error was because I left this out. I don't think the method knows that you're trying to change how the vehicle behaves, only that you trying to change something somewhere -when- the vehicle updates.

    If I'm wrong, there's probably not much further help I can give. ^_^'
     
  7. So line 69 is this line:
    Then I have a hint: Change that line to:
    Code:java
    1. if (block.
    2. getBlock().
    3. getTypeId() ==
    4. plugin.
    5. getConfig().
    6. getInt("BlockIDs.Action1"){

    I know it looks strange but you will see very exact where the error is.
    BTW: block.getBlock() looks strange, how can you get a block from a block? Maybe that's the error... ;)
     
  8. Offline

    HappyPikachu

    That's my fault. I tried to simplify the code for these forums, but in doing so, I left out that if statement (it's already in the code). I see where you were headed, though. Anyway, thanks - you were a really great help! :]
    I also screwed up by replacing "Location" with "block"... I forget what my train of thought was there (fixed). Lovin' your idea about breaking up the line... I'll try that and get back to you.

    @V10lator - It's narrowed down to line 73:
    Code:java
    1. if (vehicleLocation.
    2. getBlock().
    3. getTypeId() ==
    4. plugin.
    5. getConfig(). //Line 73... problem?
    6. getInt("BlockIDs.Action1")){

    I'll keep looking. Any ideas?
    Useful link: http://wiki.bukkit.org/index.php?title=Introduction_to_the_New_Configuration

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

    Lex Talionis

    Glad to assist where I can. :3
     
  10. It feels like plugin == null but without seeing the full code I can't be sure.
     
  11. Offline

    Lex Talionis

    If the problem is that it thinks plugin == null, either the constructor in your Listener or the event registration in your main class is missing info. As V10lator said though, it's too hard to tell without seeing the code. You could try checking post #4 here again and make sure that those are set up right with no typos.
     
  12. Offline

    HappyPikachu

    Mmk, I checked. Everything appears the same, minus a few aesthetic changes. Without modification, here's the relevant chunk of code from my VehicleListener...

    Code:java
    1. public class RandomPluginVehicleListener extends VehicleListener{
    2. private RandomPlugin plugin;
    3. public void herp(RandomPlugin instance){
    4. plugin = instance;
    5.  
    6. public void onVehicleUpdate(VehicleUpdateEvent event){
    7. if (event.getVehicle() instanceof Vehicle){
    8. Location vehicleLocation = event.getVehicle().getLocation();
    9. vehicleLocation.setY(Math.floor(block.getY())-1);
    10. if (vehicleLocation.getBlock().getTypeId() == plugin.
    11. getConfig(). //Line 73... problem?
    12. getInt("BlockIDs.Action1")) {
    13. //Profit
    14. }
    15. }
    16. }
    17. }

    and from the main class:

    Code:java
    1. public class RandomPlugin extends JavaPlugin{
    2. public final Logger logger = Logger.getLogger("Minecraft");
    3. public final RandomPluginVehicleListener vehicleListener = new RandomPluginVehicleListener();
    4.  
    5. public void onEnable(){
    6. try{
    7. final FileConfiguration fc = this.getConfig();
    8. //If config.yml doesn't already exist
    9. if (!new File(this.getDataFolder(), "config.yml").exists()){
    10. fc.addDefault("BlockIDs.Action1", 18);
    11. fc.options().copyDefaults(true);
    12. this.saveConfig();
    13. fc.options().copyDefaults(false);
    14. }
    15. }catch(Exception e1){
    16. e1.printStackTrace();
    17. }
    18.  
    19. PluginManager pm = getServer().getPluginManager();
    20. pm.registerEvent(Event.Type.VEHICLE_ENTER, this.vehicleListener, Event.Priority.Normal, this);
    21. pm.registerEvent(Event.Type.VEHICLE_DAMAGE, this.vehicleListener, Event.Priority.Normal, this);
    22. pm.registerEvent(Event.Type.VEHICLE_UPDATE, this.vehicleListener, Event.Priority.Normal, this);
    23.  
    24. PluginDescriptionFile pdFile = this.getDescription();
    25. this.logger.info(pdFile.getName() + " version" + pdFile.getVersion() + " enabled.");
    26. }
    27. }


    EDIT: Fixed the VehicleListener.
     
  13. Offline

    Lex Talionis

    Hehe. You are missing the very two pieces of code suggested in post #4 here. In RandomPlugin.class, at the end of line 3, 'new RandomPluginVehicleListener();' must be 'new RandomPluginVehicleListener(this);'.

    In RandomPluginVehicleListener.class after line 1 you need the constructor:
    Code:
    private RandomPlugin plugin;
    RandomPluginVehicleListener(RandomPlugin instance) {
        plugin = instance;
    }
     
  14. Offline

    HappyPikachu

    Ah! I'm definitely missing 'this'! As for the the constructor... I don't know what happened thar. It's in my code once again, but it seems like it didn't copy over or something. Fml.

    I'll add 'this' and report back. Thanks for putting up with me. ;-;
     
  15. Offline

    Lex Talionis

    That's alright. Sometimes changes are so small you have to really read carefully to catch what's going on when you're asking for help.
     
  16. Offline

    HappyPikachu

    Details are all what I'm about. I'm ashamed that slipped past me, especially given how many screwup posts I've made thus far. Anywho~ I did find something sorta interesting. Up until now, I've been using:

    Code:
    private RandomPlugin plugin;
    public void herp(RandomPlugin instance) {
        plugin = instance;
    }
    This was because when I tried using your code without the 'this', it gave me an error. I'm now 100% on the same track as you, but I'm afraid the server still freaks out when it hits:

    Code:java
    1. if (vehicleLocation.
    2. getBlock().
    3. getTypeId() ==
    4. plugin.
    5. getConfig(). //Line 73... problem?
    6. getInt("BlockIDs.Action1")){

    I'm not getting any errors in Eclipse whatsoever. I just tried setting the .getInt() to a variable first, but that didn't do shiz. The Internetz, they fail me.
     
  17. Offline

    DDoS

    This is wrong:

    Code:java
    1.  
    2. public class RandomPluginVehicleListener extends VehicleListener{
    3. private RandomPlugin plugin;
    4. public void herp(RandomPlugin instance){
    5. plugin = instance;
    6.  


    It should be:

    Code:java
    1.  
    2. public class RandomPluginVehicleListener extends VehicleListener {
    3.  
    4. private RandomPlugin plugin;
    5.  
    6. public RandomPluginVehicleListener (RandomPlugin instance) {
    7.  
    8. plugin = instance;
    9.  
    10. }
    11.  


    Your constructor was named 'herp' and wasn't declared properly. It was being treated as method 'herp(RandomPlugin random)' and not as a constructor for the class 'RandomPluginVehicleListener'.

    Also, your curly braces were incorrect. Make sure a method start AND ends with a curly brace.
     
  18. Offline

    HappyPikachu

    @DDoS I figured that out in my last post (the constructor; not the curly braces - that's another goddamn screwup). Sorry to confuse you - I might not have been totally clear on that.

    Explanation: Until this thread, I've had too much faith in Eclipse to resolve errors correctly. When I was missing the 'this' (see previous posts), I figured the correction (treating the situation as a method) was somehow correct (I renamed it 'herp' for the lulz). Seeing as we're still having the same problem as before, it may have actually made no difference. Regardless, the constructor is, as of right now, set up 100% properly.

    The problem is (apparently) here:

    Code:java
    1. if (vehicleLocation.
    2. getBlock().
    3. getTypeId() ==
    4. plugin.
    5. getConfig(). //Line 73... problem?
    6. getInt("BlockIDs.Action1")){

    It produces the following error:

    Show Spoiler
    [SEVERE] Could not pass event VEHICLE_UPDATE to RandomPlugin
    java.lang.NullPointerException
    at me.happypikachu.RandomPlugin.RandomPluginVehicleListener.onVehicleUpdat
    e(RandomPluginVehicleListener.java:73)
    at org.bukkit.plugin.java.JavaPluginLoader$90.execute(JavaPluginLoader.j
    ava:890)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:58)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:339)
    at net.minecraft.server.EntityMinecart.s_(EntityMinecart.java:561)
    at net.minecraft.server.World.entityJoinedWorld(World.java:1193)
    at net.minecraft.server.WorldServer.vehicleEnteredWorld(WorldServer.java
    :109)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:247)
    at net.minecraft.server.Packet10Flying.a(SourceFile:126)
    at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:92)
    at net.minecraft.server.NetworkListenThread.a(SourceFile:108)
    at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:471)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:374)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:417)
     
  19. Offline

    DDoS

    Can you post your updated listener and main class, there's still something wrong with your constructor. Did you remove the 'void' keyword from the declaration?
     
  20. Offline

    HappyPikachu

    I did. The constructor is working properly (assuming Lex is right - which I'd be tempted to bet money on). My post starting with "Mmk, I..." is still up-to-date, but I'll repost it below w/ the corrected declaration.

    Ze VehicleListener:

    Code:java
    1. public class RandomPluginVehicleListener extends VehicleListener{
    2. private RandomPlugin plugin;
    3. RandomPluginVehicleListener(RandomPlugin instance) {
    4. instance = plugin;
    5. }
    6.  
    7. public void onVehicleUpdate(VehicleUpdateEvent event){
    8. if (event.getVehicle() instanceof Vehicle){
    9. Location vehicleLocation = event.getVehicle().getLocation();
    10. vehicleLocation.setY(Math.floor(block.getY())-1);
    11. if (vehicleLocation.getBlock().getTypeId() == plugin.
    12. getConfig(). //Line 73... problem?
    13. getInt("BlockIDs.Action1")) {
    14. //Profit
    15. }
    16. }
    17. }
    18. }

    Ze main class:

    Code:java
    1. public class RandomPlugin extends JavaPlugin{
    2. public final Logger logger = Logger.getLogger("Minecraft");
    3. public final RandomPluginVehicleListener vehicleListener = new RandomPluginVehicleListener();
    4.  
    5. public void onEnable(){
    6. try{
    7. final FileConfiguration fc = this.getConfig();
    8. //If config.yml doesn't already exist
    9. if (!new File(this.getDataFolder(), "config.yml").exists()){
    10. fc.addDefault("BlockIDs.Action1", 18);
    11. fc.options().copyDefaults(true);
    12. this.saveConfig();
    13. fc.options().copyDefaults(false);
    14. }
    15. }catch(Exception e1){
    16. e1.printStackTrace();
    17. }
    18.  
    19. PluginManager pm = getServer().getPluginManager();
    20. pm.registerEvent(Event.Type.VEHICLE_ENTER, this.vehicleListener, Event.Priority.Normal, this);
    21. pm.registerEvent(Event.Type.VEHICLE_DAMAGE, this.vehicleListener, Event.Priority.Normal, this);
    22. pm.registerEvent(Event.Type.VEHICLE_UPDATE, this.vehicleListener, Event.Priority.Normal, this);
    23.  
    24. PluginDescriptionFile pdFile = this.getDescription();
    25. this.logger.info(pdFile.getName() + " version" + pdFile.getVersion() + " enabled.");
    26. }
    27. }
     
  21. Offline

    DDoS

    Nope, still wrong, else you'd need to add 'this' to the arguments you're passing to the constructor:

    Code:java
    1.  
    2. public final RandomPluginVehicleListener vehicleListener = new RandomPluginVehicleListener();
    3.  


    Should be this:

    Code:java
    1.  
    2. public final RandomPluginVehicleListener vehicleListener = new RandomPluginVehicleListener(this);
    3.  


    So, here's the format for a constructor:

    Code:java
    1.  
    2. public [nameOfClass]([Arguments]) {
    3.  
    4. //code here, usually:
    5. this.plugin = plugin;
    6.  
    7. }
    8.  


    Now, I'm gonna fix it, use exactly what I'm giving you:


    Code:java
    1. public class RandomPluginVehicleListener extends VehicleListener{
    2. private RandomPlugin plugin;
    3. public RandomPluginVehicleListener(RandomPlugin plugin) {
    4. this.plugin = plugin;
    5. }
    6.  
    7. public void onVehicleUpdate(VehicleUpdateEvent event){
    8. if (event.getVehicle() instanceof Vehicle){
    9. Location vehicleLocation = event.getVehicle().getLocation();
    10. vehicleLocation.setY(Math.floor(block.getY())-1);
    11. if (vehicleLocation.getBlock().getTypeId() == plugin.
    12. getConfig(). //Line 73... problem?
    13. getInt("BlockIDs.Action1")) {
    14. //Profit
    15. }
    16. }
    17. }
    18. }



    Your mistakes:

    1) You didn't rename the classes correctly. Use the refactor system to rename your classes, it will rename everything correctly for you.
    2) You were setting the argument 'instance' to the value of 'plugin', basically, taking your argument and setting it to your class variable's value, the complete opposite of a constructor...
    3) You didn't put 'public' in your constructor declaration (not really necessary, but I strongly recommend it).

    Please, revise your code! I don't want to sound mean, but learn Java basics before starting to code plugins.
     
  22. Offline

    HappyPikachu

    I'm well-acquainted with the basics, minus constructors. I've just been posting a lot of mistakes which aren't present in the actual code. For example, I neglected to add-in the 'this' arg when I chose to c/p from the "Mmk, I..." post (I stated that we'd already solved that sitch in my first response to you, but I digress).

    I'll certainly test out your 3 suggestions (they look promising). If I post another mistake, imma go insane. Again, I apologize for being difficult to work with (idk wtf is wrong with me). Thanks in advance! ^_^
     
  23. Offline

    DDoS

    I guess to clear everything up, it would be best if you posted the most up to date and accurate code you have. This would make debugging much more easier.

    Let's see if we can get this thing working!
     
  24. Offline

    HappyPikachu

    I thought I had, but things keep on slipping through. I usually notice this stuff from a mile away. :(

    You sir, get a cookie! Your 2nd suggestion did the trick (and I took your advice on 3)! Once we overcame the initial constructor obstacle, I didn't bother checking it (it made so much sense, plus Eclipse didn't complain). Still, I see how this is proper. To clarify for all:

    Code:java
    1. private RandomPlugin plugin;
    2. RandomPluginVehicleListener(RandomPlugin instance) {
    3. instance = plugin;
    4. }

    is now...

    Code:java
    1. private RandomPlugin plugin;
    2. public RandomPluginVehicleListener(RandomPlugin plugin) {
    3. this.plugin = plugin;
    4. }

    I'll be memorizing constructors soon, that's for sure. A huge thanks to those whom lent me a hand. I can't thank you all enough. You rock ♪~
     
  25. Offline

    Sagacious_Zed Bukkit Docs

  26. Offline

    Lex Talionis

    @HappyPikachu - Gratz! And I'd take Sagacious_Zed's advice too. It's much better to understand how something works than to just have a mental library of all the ways you've seen it used. :3
     
  27. Offline

    HappyPikachu

    Advice taken! Looking back at the thread, I'll be taking constructors much more seriously~
     
Thread Status:
Not open for further replies.

Share This Page