Solved Code not working (checks if location is the same in config on an event)

Discussion in 'Plugin Development' started by gomeow, Oct 3, 2012.

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

    gomeow

    Can't get any message to send to player if all of my if statements are false...

    Code:
    package me.gomeow.bedtele;
     
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerBedLeaveEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class myListener extends JavaPlugin implements Listener {
       
        @EventHandler
        //Check if player Leaves Bed
        public void onBedLeave(PlayerBedLeaveEvent event) {
           
            //allows player.
            Player player = event.getPlayer();
           
            //Get X, Y, and Z
            int locationX = getConfig().getInt("Location1.X");
            int locationY = getConfig().getInt("Location1.Y");
            int locationZ = getConfig().getInt("Location1.Z");
           
            //Test Location
            if(player.getLocation().getBlockX() == locationX) {
                //Found X, Verification Success
                player.sendMessage("X");
                if(player.getLocation().getBlockY() == locationY) {
                    //Found Y, Verification Success
                    player.sendMessage("Y");
                    if(player.getLocation().getBlockZ() == locationZ) {
                        //Found all co-ords, Verification Success
                        player.sendMessage("It Worked!");
                    }
                    else{
                        //Found X and Y, but not Z
                        player.sendMessage("No on Z");
                    }
                }
                else{
                    //Found X, but not Y, Z not checked
                    player.sendMessage("No on Y");
                }
            }
                else{
                    //Didn't find X, Y and Z not checked
                    player.sendMessage("No on X");
                    //This should come up if all if statements return false
                }
        }
     
       
       
    }
    
    Not sure if its ok to add the extends JavaPlugin on the Listener, but I didn't know how to get the config otherwise...

    Config:
    Code:
    Location1:
        X:
        Y:
        Z:
    Location2:
        X:
        Y:
        Z:
    Assuming the #s are there, which they were
     
  2. Offline

    Jogy34

    You can have your main class be a listener class if you want but just extinding JavaPlugin won't do anything for what you want. What I do is make a constructor that takes in my main class then have the listener save it to a local variable of my main class and register the events in the constructor. I then call new ListenerClass(this); in my onEnable method.
     
  3. Offline

    gomeow

    Can you explain better?

    ok, I narrowed down the problem to when I access the config:

    Code:
    [SEVERE] Could not pass event PlayerBedLeaveEvent to Bedtele
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:341)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
        at net.minecraft.server.EntityHuman.a(EntityHuman.java:985)
        at net.minecraft.server.EntityPlayer.a(EntityPlayer.java:444)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:965)
        at net.minecraft.server.Packet19EntityAction.handle(SourceFile:39)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:282)
        at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:109)
        at net.minecraft.server.ServerConnection.b(SourceFile:35)
        at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:577)
        at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:213)
        at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:473)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:405)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.IllegalArgumentException: File cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:203)
        at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:170)
        at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:117)
        at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:111)
        at me.gomeow.bedtele.myListener.onBedLeave(myListener.java:20)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:339)
        ... 16 more
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  4. Offline

    Jogy34

    Your main class is the class that extends JavaPlugin. You are able to implement listener onto your main class but if you just extend JavaPlugin on another class you are basically doing nothing except making the entire thing a giant headache. I do something like this:
    Code:
    //Main Class onEnable()
    ...
    new MyListener(this);
    ...
    //Listener class
    private MyMainClass plugin;
    public MyListener(MyMainClass plugin)
    {
        plugin.getServer().getPluginManager().registerEvents(this, plugin);
        this.plugin = plugin;
    }
    //later in the code where I would have to get the config
    ...
    int thing = plugin.getConfig().getInt("Path.To.Thing");
    ...
    
     
  5. Offline

    gomeow

    Still confused...

    Main Class:

    Code:
    package me.gomeow.bedtele;
     
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerBedLeaveEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
     
    public class bedtele extends JavaPlugin implements Listener {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static bedtele plugin;
       
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " has Been Disabled!");
        }
       
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " has Been Enabled!");
            saveDefaultConfig();
            this.getServer().getPluginManager().registerEvents(this, plugin);
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
           
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("bedtele")){
                if(!(sender instanceof Player)){
                    sender.sendMessage("This command can only be run by a player!");
                }
                else if(args.length == 0){
                    if(player.isOp()){
                        player.sendMessage(ChatColor.RED + "Correct Usage:");
                        player.sendMessage(ChatColor.RED + "/bedtele create - Use this to make the bed teleport you.");
                        player.sendMessage(ChatColor.RED + "/bedtele - Brings up this page.");
                    }
                    else {
                        player.sendMessage(ChatColor.RED + "You do not have permission to use this command");
                    }
                }
               
                if(args.length == 1){
                    if(args[0].equalsIgnoreCase("create")){
                        player.sendMessage(ChatColor.GREEN + "Please point to a bed and type " + ChatColor.ITALIC + "/bedtele setbed");
                       
                    }
                    if(args[0].equalsIgnoreCase("setbed")){
                        if(player.getTargetBlock(null, 200).getTypeId() == 26){
                            player.sendMessage(ChatColor.GREEN + "Success!");
                           
                            int bedlocX = player.getTargetBlock(null, 0).getLocation().getBlockX();
                            int bedlocY = player.getTargetBlock(null, 0).getLocation().getBlockY();
                            int bedlocZ = player.getTargetBlock(null, 0).getLocation().getBlockZ();
                           
                            this.getConfig().set("Location1.X", bedlocX);
                            this.getConfig().set("Location1.Y", bedlocY);
                            this.getConfig().set("Location1.Z", bedlocZ);
                            this.saveConfig();
                            player.sendMessage("You set a bed at " + bedlocX + " " + bedlocY + " " + bedlocZ + "!");
                        }
                        else {
                            player.sendMessage(ChatColor.RED + "You need to point to a bed!");
                        }
                    }
                }
               
               
            }
            return false;
        }
       
        public void onBedLeave(PlayerBedLeaveEvent event) {
     
            Player teleported = event.getPlayer();
           
        int locationX = getConfig().getInt("Location1.X");
        int locationY = getConfig().getInt("Location1.Y");
        int locationZ = getConfig().getInt("Location1.Z");
       
        if(teleported.getLocation().getBlockX() == locationX) {
            //Found X, Verification Success
            teleported.sendMessage("X");
            if(teleported.getLocation().getBlockY() == locationY) {
                //Found Y, Verification Success
                teleported.sendMessage("Y");
                if(teleported.getLocation().getBlockZ() == locationZ) {
                    //Found all co-ords, Verification Success
                    teleported.sendMessage("It Worked!");
                }
                else{
                    //Found X and Y, but not Z
                    teleported.sendMessage("No on Z");
                }
            }
            else{
                //Found X, but not Y, Z not checked
                teleported.sendMessage("No on Y");
            }
        }
            else{
                //Didn't find X, Y and Z not checked
                teleported.sendMessage("No on X");
                //This should come up if all if statements return false
            }
       
        }
    }
    myListener class:

    Code:
    package me.gomeow.bedtele;
     
    import org.bukkit.event.Listener;
     
    public class myListener implements Listener {
       
    private bedtele plugin;
     
    new MyListener(bedtele plugin) {
        plugin.getServer().getPluginManager().registerEvents(this, plugin);
        this.plugin = plugin;
       
    }
    }
    I'm just really confused by that
     
  6. Offline

    Jogy34

    Get rid of your Listener class and add @EventHandler above your onBedLeave method.

    I'll just go step by step for what I said:


    Your main class is the class that extends JavaPlugin.

    It is possible for you main class to also be a listener.

    If you have another class that is not your original main class and you have that class extend JavaPlugin (You have 2 or more classes that extend JavaPlugin) it just becomes a giant Headache.

    Code:
    //Main Class onEnable()
    ...
    new MyListener(this); // This is in onEnable and is just calling the constructor of my Listener class
    ...
    //Listener class
    private MyMainClass plugin; //create a variable to call methods that belong to my main class
    public MyListener(MyMainClass plugin) //Constructor for my Listener class. It takes in my main class as a parameter
    {
        plugin.getServer().getPluginManager().registerEvents(this, plugin); //registering the events
        this.plugin = plugin; //initializing my main class variable to what was sent into the constructor
    }
    //later in the code where I would have to get the config
    ...
    int thing = plugin.getConfig().getInt("Path.To.Thing"); //This is an example of some arbitrary use of getting the config from within this class
    ...
    
    Hope that cleared things up. Tell me if you are still confused about anything
     
  7. Offline

    gomeow

    sorta confuse still, you said delete the listener class, but your code says //Listener Class

    my code from what I could understand from that:

    Code:
    package me.gomeow.bedtele;
     
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerBedLeaveEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
     
    public class bedtele extends JavaPlugin implements Listener {
    public final Logger logger = Logger.getLogger("Minecraft");
    public static bedtele plugin;
    public void myListener(bedtele plugin) {
    plugin.getServer().getPluginManager().registerEvents(this, plugin);
    }
     
    @Override
    public void onDisable() {
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " has Been Disabled!");
    }
     
    @Override
    public void onEnable() {
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " has Been Enabled!");
    saveDefaultConfig();
    this.getServer().getPluginManager().registerEvents(this, plugin);
    }
     
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
     
    Player player = (Player) sender;
    if(commandLabel.equalsIgnoreCase("bedtele")){
    if(!(sender instanceof Player)){
    sender.sendMessage("This command can only be run by a player!");
    }
    else if(args.length == 0){
    if(player.isOp()){
    player.sendMessage(ChatColor.RED + "Correct Usage:");
    player.sendMessage(ChatColor.RED + "/bedtele create - Use this to make the bed teleport you.");
    player.sendMessage(ChatColor.RED + "/bedtele - Brings up this page.");
    }
    else {
    player.sendMessage(ChatColor.RED + "You do not have permission to use this command");
    }
    }
     
    if(args.length == 1){
    if(args[0].equalsIgnoreCase("create")){
    player.sendMessage(ChatColor.GREEN + "Please point to a bed and type " + ChatColor.ITALIC + "/bedtele setbed");
     
    }
    if(args[0].equalsIgnoreCase("setbed")){
    if(player.getTargetBlock(null, 200).getTypeId() == 26){
    player.sendMessage(ChatColor.GREEN + "Success!");
     
    int bedlocX = player.getTargetBlock(null, 0).getLocation().getBlockX();
    int bedlocY = player.getTargetBlock(null, 0).getLocation().getBlockY();
    int bedlocZ = player.getTargetBlock(null, 0).getLocation().getBlockZ();
     
    this.getConfig().set("Location1.X", bedlocX);
    this.getConfig().set("Location1.Y", bedlocY);
    this.getConfig().set("Location1.Z", bedlocZ);
    this.saveConfig();
    player.sendMessage("You set a bed at " + bedlocX + " " + bedlocY + " " + bedlocZ + "!");
    }
    else {
    player.sendMessage(ChatColor.RED + "You need to point to a bed!");
    }
    }
    }
     
     
    }
    return false;
    }
     
     
     
     
    @EventHandler
    public void onBedLeave(PlayerBedLeaveEvent event) {
     
    Player teleported = event.getPlayer();
     
    int locationX = plugin.getConfig().getInt("Location1.X");
    int locationY = plugin.getConfig().getInt("Location1.Y");
    int locationZ = plugin.getConfig().getInt("Location1.Z");
     
    if(teleported.getLocation().getBlockX() == locationX) {
    //Found X, Verification Success
    teleported.sendMessage("X");
    if(teleported.getLocation().getBlockY() == locationY) {
    //Found Y, Verification Success
    teleported.sendMessage("Y");
    if(teleported.getLocation().getBlockZ() == locationZ) {
    //Found all co-ords, Verification Success
    teleported.sendMessage("It Worked!");
    }
    else{
    //Found X and Y, but not Z
    teleported.sendMessage("No on Z");
    }
    }
    else{
    //Found X, but not Y, Z not checked
    teleported.sendMessage("No on Y");
    }
    }
    else{
    //Didn't find X, Y and Z not checked
    teleported.sendMessage("No on X");
    //This should come up if all if statements return false
    }
     
    }
     
     
     
    }
    
     
  8. Offline

    Jogy34

    Get rid of this stuff:
    Code:
    public static bedtele plugin;
    public void myListener(bedtele plugin) {
    plugin.getServer().getPluginManager().registerEvents(this, plugin);
    }
     
    //in your onEnable
    this.getServer().getPluginManager().registerEvents(this, plugin);
    Then add this:
    Code:
    //In your onEnable
    this.getServer().getPluginManager().registerEvents(this, this);
    And in your PlayerBedLeaveEvent change these:
    Code:
     
    plugin.getConfig()...
    //To
    this.getConfig...

    Also sorry about confusing you. I was writing that late at night and I originally thought you had a different class for your event listener and that you just made it extend JavaPlugin
     
  9. Offline

    gomeow

    THANK YOU SO MUCH! It works now!

    One last unrelated thing...
    How can you say +-2 or something?

    I know this is wrong, so I am wondering how to do it?
    Code:
    if(teleported.getLocation().getBlockX() == locationX +-2) {
    //code to be run
    }
     
  10. Offline

    MrFigg

    Code:
    if(teleported.getLocation().getBlockX() >= (locationX-2) && teleported.getLocation().getBlockX()<= (locationX+2)) {
    //code to be run
    }
     
  11. Offline

    gomeow

    Yes, that works, thanks for the help everyone
     
Thread Status:
Not open for further replies.

Share This Page