Solved moving config from Listener Class to MainClass

Discussion in 'Plugin Development' started by pingpong1999, Apr 10, 2016.

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

    pingpong1999

    Hey bukkit forums, I know I posted a few hours ago but I need more help! (Yep I'm an amateur)
    here are my two classes, Main and listener
    Code:
    package me.firstflames;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class MainClass extends JavaPlugin {
        public Permission playerPermission = new Permission("Enchants.Admin");
        @Override
        public void onEnable() {
            new ListenerClass(this);
            PluginManager pm = getServer().getPluginManager();
            pm.addPermission(playerPermission);
        }
      
        @Override
        public void onDisable() {
            saveConfig();
        }
      
        public boolean onCommand(CommandSender sender, Command cmd, String lable, String[] args){
          
            FileConfiguration config = this.getConfig();
            if (cmd.getName().equalsIgnoreCase("et") && sender instanceof Player && args.length == 0){
                Player player = (Player) sender;
      
                int total = config.getInt("tokens." + player.getName().toLowerCase());
                player.sendMessage(ChatColor.GREEN + "You have " + ChatColor.GRAY + total + ChatColor.GREEN + " Tokens");
            return true;
          
            }else if (args.length == 3 && args[0].equalsIgnoreCase("give") && sender instanceof Player){
                Player player = (Player) sender;
                if(player.hasPermission("Enchants.Admin")){
                int length = args.length;
                if (length == 3) {
                    for (Player playerToGiveToken : Bukkit.getServer().getOnlinePlayers()){
                        if(playerToGiveToken.getName().equalsIgnoreCase(args[1])){
                            playerToGiveToken.getInventory().addItem(new ItemStack(Material.BEDROCK, Integer.parseInt(args[2])));
                            playerToGiveToken.sendMessage(ChatColor.GOLD + player.getName() + " has given you " + ChatColor.GRAY + args[2] + ChatColor.GOLD + " Tokens!");
                            player.sendMessage(ChatColor.GOLD + "You have given " + playerToGiveToken.getName()+ " " + ChatColor.GRAY + args[2] + ChatColor.GOLD + " Tokens!");
                        }
                    }
                }      
            }
            return true;
        }else if (args.length == 1 && args[0].equalsIgnoreCase("help") && sender instanceof Player){
            Player player = (Player) sender;
            int length = args.length;
                    if (length == 1){
                        player.sendMessage(ChatColor.GRAY + "/et :" + ChatColor.GOLD + " To Check your eToken Balance");
                        player.sendMessage(ChatColor.GRAY + "Admin Commands");
                        player.sendMessage(ChatColor.GRAY + "/et give (player) (amount) :" + ChatColor.GOLD + " Give Player eTokens");
                    }
        }
        return false;
    }
    }
    
    And my Listener Class
    Code:
    package me.firstflames;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class ListenerClass implements Listener {
      
        MainClass plugin;
        FileConfiguration config;
      
        public ListenerClass(MainClass plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);  
            this.plugin = plugin;
            config = plugin.getConfig();
        }
      
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e){
          
            Player player = e.getPlayer();
                  
            e.setJoinMessage(ChatColor.GOLD + "Welcome, " + player.getName() + ", to Palm Life MC!");
          
            if (player.hasPlayedBefore() == false){
                player.sendMessage(ChatColor.GREEN + "Welcome for the first time to Palm Life MC, Here is a free 20 E-Tokens!");
        }
        }
            @EventHandler
            public void onBlockPlace(BlockPlaceEvent d){
                Player player = d.getPlayer();
              
                int tokens = 20;
                String path = "tokens." + player.getName().toLowerCase();
              
                if (d.getBlock().getType() == Material.BEDROCK){
                    d.setCancelled(true);
                    player.sendMessage(ChatColor.GREEN + " You have gained " + ChatColor.GRAY + "1 " + ChatColor.GREEN + "Token");
                    player.getInventory().removeItem(new ItemStack(Material.BEDROCK, 1));
                    if(config.contains(path)){
                        tokens = config.getInt(path);
                    }
                    config.set(path, tokens + 1);
                  
                    plugin.saveConfig();
                }
        }
    
    }
    
    I am trying to find out how to manipulate "tokens" in the Main class. I have tried
    Code:
    this.MainClass = plugin; 
    which i doubt would actually work, but i tried it. I'm sorry if this is poorly worded, it is the best i could explain
    I want to make it so when i do /et give (player) (amount) instead of giving bedrock, which the player has to manually place down, it gives it strait to their 'account'
     
  2. Online

    timtower Administrator Administrator Moderator

    @pingpong1999 What you have is the construction that most developers have here.
    Should work just fine.
     
  3. Offline

    pingpong1999

    Thanks, But I found a better way around it. Instead of 'summoning' the config file at the Listener file, I did it at my main where I would refer to it more. I have a good understanding of how this works
     
Thread Status:
Not open for further replies.

Share This Page