Solved Need help with config

Discussion in 'Plugin Development' started by Calebizzthaman, Aug 16, 2017.

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

    Calebizzthaman

    I've been trying to make it to where I can set the amount of ticks of a chest de-spawn from the config of my plugin but it keeps giving me errors. Can someone help?

    Main Class:
    Code:
    public class Main extends JavaPlugin{
        private static Plugin plugin;
       
        public void onEnable() {
            plugin = this;
            getLogger().info("Player Loot Chests enabled");
            new Listeners(this);
           
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
       
        public void onDisable() {
            plugin = null;
            getLogger().info("Player Loot Chests disabled");
        }
       
    
        public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
            for (Listener listener : listeners) {
            Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
            }
            }
       
    
        public static Plugin getPlugin()
        {
            return plugin;
        }
    }
    Listeners Class:
    Code:
    public class Listeners implements Listener{
       
        private final Main plugin;
        private Main main;
       
        public Listeners(Main plugin) {
            this.plugin = plugin;
            this.plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
       
    
    
        @EventHandler
        public void dropPlayerDeathChest(PlayerDeathEvent e) {
            Player p = e.getEntity();
            String pname = p.getName();
            p.getInventory();
            ItemStack[] items = p.getInventory().getContents();
            Location loc = p.getLocation();
            Block block = loc.getBlock();
            loc.getBlock().setType(Material.CHEST);
            block.getRelative(BlockFace.NORTH).setType(Material.CHEST);
            Chest chest = (Chest)block.getState();
            Inventory inv = chest.getInventory();
            e.getDrops().clear();
            inv.setContents(items);
            chest.setCustomName(ChatColor.DARK_RED + pname + "'s loot");
    
           
            new BukkitRunnable() {
                @Override
                public void run() {
                    chest.getInventory().clear();
                    chest.getBlockInventory().clear();
                    block.setType(Material.AIR);
                    block.getRelative(BlockFace.NORTH).setType(Material.AIR);
                   
                }
            }.runTaskLater(plugin, main.getConfig().getInt("ticks"));
        }
    }
    Also, how would I convert ticks into minutes so that when a user is changing the despawn time they can just set it in minutes and not have to do the math with ticks?
     
  2. Offline

    Zombie_Striker

    @Calebizzthaman
    1. You should not use static variables for this. If you need to pass variables around, use constructors. Remove the "plugin" instance and the "getPlugin" method in the main class
    2. You have to fields for the Main class in the listener. Choose one and remove the other.
    3. registerEvents is never used, and is not needed. Remove that method.
    4. Do not log your own plugins. Bukkit does this for you. What you are currently doing is printing out twice that your plugin is enabled.
    5. p.getInventory() does nothing. Remove this line.
    6. For the config "main" is null, it is plugin that gets set. Change main to plugin.
    7. You do not need loc. It is only used once. Remove it.
    8. You do not need pname. Is is just one method call to get the name, and is only used once. Remove it.
     
  3. Offline

    Calebizzthaman

    @Zombie_Striker how would I make it to where the user could just set the amount of minutes of the chest despawn in the config instead of having to do the math in ticks?
     
  4. @Calebizzthaman
    Well you do the maths. Take the number of minutes, multiply by 60 to get the amount of seconds, and multiply by 20 again to get the amount of ticks.
     
Thread Status:
Not open for further replies.

Share This Page