How to resume any BukkitRunnables after a reload

Discussion in 'Plugin Development' started by raunak114, Mar 26, 2017.

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

    timtower Administrator Administrator Moderator

  2. Offline

    raunak114

    I tried looping through the set, and using the get method of the hashmap, and it didn't work
     
  3. Offline

    timtower Administrator Administrator Moderator

  4. Offline

    raunak114

    I'll send the code once I get home

    Code:
     public static HashMap<String,Integer> Flylands = new HashMap<String, Integer>();
    
            for(String key : plugin.getConfig().getConfigurationSection("FlyLands").getKeys(false))
            {
               Flylands.put(key, plugin.getConfig().getIntegerList("FlyLands").indexOf(key));
            }
    
    
    
    BTW, sorry im late

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 29, 2017
  5. Offline

    timtower Administrator Administrator Moderator

    @raunak114 Don't apologize for being late.
    FlyLands is not an integerlist, it is a configurationSection.
    use getInt("FlyLands."+key);
     
  6. Offline

    raunak114

    ok Let me try this :D

    Edit: I have an error saying the key is a string, do i just use the Integer.parseint here?

    EDIT: didnt work @timtower
     
    Last edited: Mar 29, 2017
  7. Offline

    Caderape2

    @raunak114

     
  8. Offline

    raunak114

    Idk what my error was, but i restarted eclipse and its gone, (naice!)
    this is what i am using:
    Code:
    //hashmap declaration
    HashMap<String, Integer> flylands = new HashMap<String, Integer>();
    
    //The enhanced for
            for(String key : plugin.getConfig().getConfigurationSection("FlyLands").getKeys(false))
            {
                flylands.put(key, plugin.getConfig().getInt("Flylands." + key));
            }
    
    But i am getting a null pointer exception:
    Code:
    [04:07:29] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to Skyblock_Commands v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot1.10.jar:git-Spigot-72c2605-251a5b6]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot1.10.jar:git-Spigot-72c2605-251a5b6]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
        at org.bukkit.craftbukkit.v1_10_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:231) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
        at org.bukkit.craftbukkit.v1_10_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:198) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
        at org.bukkit.craftbukkit.v1_10_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:194) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
        at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:953) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
        at net.minecraft.server.v1_10_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:26) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
        at net.minecraft.server.v1_10_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
        at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_111]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_111]
        at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
        at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:732) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
        at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
        at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:668) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
        at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:567) [spigot1.10.jar:git-Spigot-72c2605-251a5b6]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_111]
    Caused by: java.lang.NullPointerException
        at ShopEvents.InventoryListener.onRightClick(InventoryListener.java:47) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_111]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot1.10.jar:git-Spigot-72c2605-251a5b6]
        ... 18 more
    [04:07:30] [Craft Scheduler Thread - 44/INFO]: 
    Where am i going wrong?
     
  9. Offline

    Caderape2

    @raunak114
    there something not initialized in your event onRightClick line 47
     
  10. Offline

    raunak114

    Ya, ik, but line 47 is the first line of the for loop
    Code:
    for(String key : plugin.getConfig().getConfigurationSection("FlyLands").getKeys(false))
    


    there something not initialized in your event onRightClick line 47[/QUOTE]
     
  11. Offline

    timtower Administrator Administrator Moderator

    @raunak114 plugin can be null, the section might not exist.
     
  12. Offline

    raunak114

    The plugin cannot be null, as I saved the hashmap flylands into the section FlyLands many times
     
  13. Offline

    timtower Administrator Administrator Moderator

  14. Offline

    raunak114

    OK, once I get back from school

    Code:
    package ShopEvents;
    
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.scheduler.BukkitTask;
    
    import MenuItems.fpot20;
    import MenuItems.fpot60;
    import SBc.Mainclass;
    import library.CustomPlayer;
    import menuLib.MenuItem;
    import net.md_5.bungee.api.ChatColor;
    
    public class InventoryListener implements Listener{
        int ETA =1;
        HashMap<String, Integer> flylands = new HashMap<String, Integer>();
        Mainclass plugin;
        CustomPlayer pl;
        public InventoryListener(Mainclass plugin, CustomPlayer pl){
            this.plugin = plugin;
            this.pl = pl;
        }
    @SuppressWarnings("deprecation")
    @EventHandler(priority = EventPriority.LOW)
    public void onRightClick(PlayerInteractEvent e){
        MenuItem Fpot20 = new fpot20(plugin,pl);
        if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
        Player p = e.getPlayer();
        ItemStack clicked = p.getItemInHand();
        if (clicked.getItemMeta().getDisplayName().equals(Fpot20.getDisplayName()) && (clicked.getItemMeta().getLore().equals(Fpot20.getLore()))){
            if(p.getItemInHand().getAmount() > 1){
                p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);}
            else {p.setItemInHand(new ItemStack(Material.AIR));}
           
            p.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD.toString() + "You and your island members can now fly on your island for 20 minutes!");
            p.sendMessage(ChatColor.GREEN + "Please reconnect to your island in order for the fly to take in effect");
            for(String key : plugin.getConfig().getConfigurationSection("FlyLands").getKeys(false))
            {
                flylands.put(key, plugin.getConfig().getInt("Flylands." + key));
            }
    
        Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable(){@Override public void run(){
            ETA = ETA + (20 * 60);
            if(flylands.containsKey(p.getName())){
            flylands.remove(p.getName());
            }
        flylands.put(p.getName(), ETA);
        plugin.getConfig().addDefault("FlyLands", flylands);
        plugin.saveConfig();}}, 1);
       
       
    
       
       
        if (ETA <= 1){
        new BukkitRunnable(){
             public int taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this, 1L, 20L);
            
                @Override
                public void run(){
                    --ETA;
                    p.sendMessage(ETA + "");
                    if (ETA <0){
                        flylands.remove(p.getName());
                        plugin.getConfig().addDefault("FlyLands", flylands);
                        plugin.saveConfig();
                        Bukkit.getScheduler().cancelTask(taskID);
                    }
                }
            };
        }
        }
        else if(clicked == new fpot60(plugin,pl).getIcon()){
           
        }
        }//action right click block
    }
       
       
       
       
       
       
       
       
       
       
        /*implements Listener*/
        /*Cgive inv;
        public InventoryListener(Cgive inv){
            this.inv = inv;
        }
        Mainclass plugin;
        public InventoryListener(Mainclass plugin){
            this.plugin = plugin;
        }
        Items item;
        public InventoryListener(Items item){
            this.item = item;
        }
        CustomPlayer pl;
        public InventoryListener(CustomPlayer pl){
            this.pl = pl;
        }
        List<String> TimerPlayers = new ArrayList<String>();
    @EventHandler
    public void onInventoryClick(InventoryClickEvent e){
        Player p = (Player) e.getWhoClicked();
        ItemStack Clicked = e.getCurrentItem();
        Inventory inve = e.getInventory();
        if(inve.getName() == inv.spellgui.getName()){
            /*if((Clicked.getItemMeta().getDisplayName() == ChatColor.RED + "Potion of Flight")){
            p.sendMessage("test");
            if(pl.hasFunds(p, 15000.00) == true){
                pl.withdraw(p, 15/000.00);
                p.sendMessage(ChatColor.GREEN + "Thou has been charged 20000$ for your Flight spell!");
                p.sendMessage(ChatColor.GREEN + "Right click to activate your spell!");
                p.closeInventory();
                p.getInventory().addItem(Fpot20());
            }else{
                p.sendMessage(ChatColor.RED + "You dont have enough funds!");
            }
            }*//*
            if(Clicked.getAmount() == 1 && Clicked.getData().getItemType() == Material.SLIME_BALL && Clicked.containsEnchantment(plugin.glow)){
                e.setCancelled(true);
                p.closeInventory();
            }
        }
        p.sendMessage("You clicked your inv");
        }
           
    public ItemStack Fpot20(){
        ItemStack Fpot20 = new ItemStack(Material.SLIME_BALL);
        ItemMeta Fmeta20 = Fpot20.getItemMeta();
        Fmeta20.setDisplayName(ChatColor.RED + "Potion of Flight");
        Fmeta20.setLore(Arrays.asList(ChatColor.GOLD.toString() +"Emerged from the Sky in the Ancient time",ChatColor.GOLD.toString() +"This elemental potion will let you",ChatColor.GOLD.toString() +"and your Island mates fly for a brief time" , ChatColor.RED.toString() + ChatColor.BOLD.toString() + "Effect time: " + ChatColor.GREEN.toString() + "20 minutes",ChatColor.GOLD.toString() + ChatColor.BOLD.toString() + "Cost: " + ChatColor.GREEN.toString() + "15000$"));
    
        Fpot20.setItemMeta(Fmeta20);
        Fpot20.addEnchantment(plugin.glow, 1);
        return Fpot20;
    }*/
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 30, 2017
  15. Offline

    raunak114

Thread Status:
Not open for further replies.

Share This Page