Solved API Help

Discussion in 'Plugin Development' started by IcyRelic, Jul 24, 2013.

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

    IcyRelic

    How do i reference an api i created in another plugin so 2 of my plugins working together so i would need help with the api class and using it

    currently im calling the plugin like this

    Code:
    private final LegendaryBans lb = (LegendaryBans) Bukkit.getPluginManager().getPlugin("LegendaryBans");
    and im using this to use the api
    Code:
        private final void bans(){
               
                if(lb.api.createBan("Player", "LegendaryOreLogs", "0", "0", "AutoBan: Xray")){
                    lb.api.sendBanMessage("LegendaryOreLogs", "AutoBan: Xray", false, "", "Player");
                }else{
                    System.out.println("Ban Failed");
                }
        }
        

    Here is the api class

    Code:
    package me.icyrelic.com.API;
     
    import org.bukkit.ChatColor;
     
    import me.icyrelic.com.LegendaryBans;
     
     
     
    public class LBApi {
       
        private LegendaryBans plugin;
       
       
        public boolean createBan(String name, String admin, String type, String unban, String reason){
            return plugin.mysql.createBan(name, admin, type, unban, reason);
        }
       
        public void sendBanMessage(String admin, String reason, Boolean tempban, String tempbantime, String player){
           
            if(tempban){
                plugin.getServer().broadcastMessage(ChatColor.YELLOW+player.toLowerCase()+ChatColor.GOLD+" was tempbanned by "+ChatColor.YELLOW+admin+ChatColor.GOLD+". Reason: "+ChatColor.YELLOW+reason);
                plugin.getServer().broadcastMessage(ChatColor.YELLOW + player + ChatColor.GOLD + " is temporarily banned for " +ChatColor.YELLOW+  tempbantime);
            }else{
                plugin.getServer().broadcastMessage(ChatColor.YELLOW+player.toLowerCase()+ChatColor.GOLD+" was banned by "+ChatColor.YELLOW+admin+ChatColor.GOLD+". Reason: "+ChatColor.YELLOW+reason);
            }
           
        }
       
     
    }
    

    and here is the error im getting

    Code:
    13:37:05 [SEVERE] Error occurred while enabling LegendaryOreLogs v1.0 (Is it up
    to date?)
    java.lang.NullPointerException
            at me.icyrelic.com.LegendaryOreLogs.bans(LegendaryOreLogs.java:42)
            at me.icyrelic.com.LegendaryOreLogs.onEnable(LegendaryOreLogs.java:52)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:457)
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:381)
            at org.bukkit.craftbukkit.v1_6_R2.CraftServer.loadPlugin(CraftServer.jav
    a:282)
            at org.bukkit.craftbukkit.v1_6_R2.CraftServer.enablePlugins(CraftServer.
    java:264)
            at org.bukkit.craftbukkit.v1_6_R2.CraftServer.reload(CraftServer.java:60
    5)
            at org.bukkit.Bukkit.reload(Bukkit.java:275)
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
    23)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    9)
            at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServe
    r.java:523)
            at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchServerCommand(Craf
    tServer.java:512)
            at net.minecraft.server.v1_6_R2.DedicatedServer.ar(DedicatedServer.java:
    262)
            at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:2
    27)
            at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:4
    86)
            at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java
    :419)
            at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:5
    82)
     
  2. Offline

    Mattredsox

    Try just directly using the API class and adding your LegendaryBans plugin to your build path and referencing your API class from that.
     
  3. Offline

    xTrollxDudex

    Icyrelic
    Post the full code for LegendaryOreLogs
     
  4. Offline

    IcyRelic



    there is no other code relevant to the api in the onEnable it calls the bans(); method

    Full code (made some changes)

    Code:
    package me.icyrelic.com;
     
    import java.util.HashMap;
     
    import me.icyrelic.com.Commands.orelogs;
    import me.icyrelic.com.Listeners.blockbreak;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class LegendaryOreLogs extends JavaPlugin{
       
        public String prefix = ("[" + ChatColor.GREEN + "LegendaryOreLogs" + ChatColor.WHITE + "]");
        public HashMap<String, Long> diamonds = new HashMap<String, Long>();
        public HashMap<String, Long> gold = new HashMap<String, Long>();
        public HashMap<String, Long> iron = new HashMap<String, Long>();
        public HashMap<String, Long> coal = new HashMap<String, Long>();
        public HashMap<String, Long> redstone = new HashMap<String, Long>();
        public HashMap<String, Long> lapis = new HashMap<String, Long>();
        public HashMap<String, Long> emerald = new HashMap<String, Long>();
        public HashMap<String, Long> diamondsv = new HashMap<String, Long>();
        public HashMap<String, Long> goldv = new HashMap<String, Long>();
        public HashMap<String, Long> ironv = new HashMap<String, Long>();
        public HashMap<String, Long> coalv = new HashMap<String, Long>();
        public HashMap<String, Long> redstonev = new HashMap<String, Long>();
        public HashMap<String, Long> lapisv = new HashMap<String, Long>();
        public HashMap<String, Long> emeraldv = new HashMap<String, Long>();
        public HashMap<String, String> players = new HashMap<String, String>();
       
        public HashMap<Block, Long> blocksntime = new HashMap<Block, Long>();
        public HashMap<String, Block> blocksnplayer = new HashMap<String, Block>();
        public HashMap<String, Long> blockids = new HashMap<String, Long>();
       
     
        private final LegendaryBans lb = (LegendaryBans) Bukkit.getPluginManager().getPlugin("LegendaryBans");
       
        public void onEnable(){
     
           
            lb.api.test();
           
           
            getServer().getPluginManager().registerEvents(new blockbreak(this), this);
            getCommand("orelogs").setExecutor(new orelogs(this));
            sendMsg();
            resetLists();
        }
       
       
       
        public void resetLists(){
            getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                   
                   
                   
                   
                    //Reset lists
                   
                    diamonds.clear();
                    gold.clear();
                    iron.clear();
                    coal.clear();
                    redstone.clear();
                    lapis.clear();
                    emerald.clear();
                    diamondsv.clear();
                    goldv.clear();
                    ironv.clear();
                    coalv.clear();
                    redstonev.clear();
                    lapisv.clear();
                    emeraldv.clear();
                    players.clear();
                   
                }
            }, 0L, 1200*60);
                       
                        //30 mins 36000L
                        //1 min 1200
        }   
       
        public void sendMsg(){
            getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                   
                    for(Player p : getServer().getOnlinePlayers()){
                        if(p.hasPermission("LegendaryOreLogs.Logs")){
                            p.sendMessage(prefix + ChatColor.GRAY + " Logs are being reset in 5 minutes! View Them With /orelogs");
                        }
                    }
                   
                }
            }, 0L, 1200*55);
                       
                        //30 mins 36000L
                        //1 min 1200
        }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  5. Offline

    xTrollxDudex

    Icyrelic
    I dont have line numbers, and since you made some changes, they wont be accurate anyway.

    I would expect the npe to come from setting the command executor, or the api.
    And from the bans method, since I am nt experienced with mysql, you need to return a boolean in the createBans method. You also never initialized the plugin variable in the api either.
     
  6. Offline

    IcyRelic

    Everything is working except for the 1 method and its the createBan one the other one works fine is there something else i have to do with my mysql methods?

    here is the createBan mysql method

    Code:java
    1. public boolean createBan(String name, String admin, String type, String unban, String reason){
    2.  
    3. /**
    4.   * Types
    5.   *
    6.   * Ban - 0
    7.   * Tempban - 1
    8.   * Permban - 2
    9.   */
    10.  
    11. try {
    12.  
    13. String sql = "INSERT INTO banlist (`id` , `username` , `date`, `admin` , `type` , `unban`, `reason`) VALUES (NULL,'"+name+"','"+System.currentTimeMillis()+"','"+admin+"','"+type+"','"+unban+"', '"+reason+"');";
    14.  
    15. st.executeUpdate(sql);
    16.  
    17. return true;
    18. } catch (SQLException e) {
    19.  
    20. e.getStackTrace();
    21.  
    22. return false;
    23. }
    24.  
    25.  
    26. }


    FIXED Finaly!!

    here is how i fixed it
    i added this line of code to the plugin.yml of LegendaryBans

    Code:
    loadbefore: [LegendaryOreLogs]
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
Thread Status:
Not open for further replies.

Share This Page