Being able to only use command 3 times on the server?

Discussion in 'Plugin Development' started by ZderKi, Jan 31, 2019.

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

    ZderKi

    Okay i have this /wild plugin that teleports you to a random location within 100000 blocks... i want that command to be usable only 3 times for the player for the whole time he is on the server. how do i do it? heres the code btw:

    Code:
    package me.zderki.customessentials.commands;
    
    import java.util.Random;
    
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    import me.zderki.customessentials.Main;
    
    @SuppressWarnings("unused")
    public class Wild implements CommandExecutor {
       
        private Main plugin;
       
        public Wild(Main plugin) {
            this.plugin = plugin;
            plugin.getCommand("wild").setExecutor(this);
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            Player player = (Player) sender;
           
            if(player.hasPermission("cessentials.wild")) {
           
            if(sender instanceof Player) {
               
                Location originalLocation = player.getLocation();
               
                Random random = new Random();
               
                int x = random.nextInt(100000) + 1;
                int y = 150;
                int z = random.nextInt(100000) + 1;
               
                Location teleportLocation = new Location(player.getWorld(), x, y, z);
               
               
                player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 1000, 5));
                player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 1000, 5));
               
    
                player.teleport(teleportLocation);
               
                player.sendMessage(ChatColor.GREEN + "You have been teleported " + ChatColor.GOLD + (int)teleportLocation.distance(originalLocation) + ChatColor.GREEN + " blocks away!");
               
            } else
                sender.sendMessage(ChatColor.RED + "Only players may execute this command!");
           
            } else
                sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command!");
               
            return false;
        }
    
    }
    
    
     
  2. Offline

    KarimAKL

    @ZderKi You could save the amount of times the player has used the command in a yaml file.
     
  3. Offline

    ZderKi

    Yeah i tought of that butt... ehm... any "easier" ways?
     
  4. Offline

    timtower Administrator Administrator Moderator

    You need to save it somewhere
     
  5. Offline

    ZderKi

    Okay yeah... i tried to save it in yaml config file but im just so bad with coding it i didnt get anywhere... is there any way to save it to regular .yml config ?

    Okay i tried to do this... its working and even saves the config but whenever i reconnect or reload the server it doesnt work... any fixes? (btw. i know its a big chunk of text and its not useful at all but meh idk how to do it -\_(._.)_/- )

    Code:
    package me.zderki.customessentials.commands;
    
    import java.io.File;
    import java.util.Random;
    import java.util.logging.Logger;
    
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    import me.zderki.customessentials.Main;
    
    @SuppressWarnings("unused")
    public class Wild implements CommandExecutor {
       
        public void onEnable() {
            plugin.saveDefaultConfig();
        }
       
           
       
        private Main plugin;
       
        public Wild(Main plugin) {
            this.plugin = plugin;
            plugin.getCommand("wild").setExecutor(this);
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            Player player = (Player) sender;
           
            if(player.hasPermission("cessentials.wild")) {
           
            if(sender instanceof Player) {
    
                String name3 = plugin.getConfig().getString("player-name3");
               
                String name2 = plugin.getConfig().getString("player-name2");
               
                String name = plugin.getConfig().getString("player-name");
               
                if(name == player.getName() && name2 == player.getName() && name3 == player.getName()) {
                    player.sendMessage(ChatColor.RED + "You have no uses of " + ChatColor.GOLD + "/wild " + ChatColor.RED + "left!");
                } else {
               
                if(name == player.getName() && name2 == player.getName()) {
                    plugin.getConfig().set("player-name3", player.getName());
                    plugin.saveConfig();
                    Location originalLocation = player.getLocation();
                   
                    Random random = new Random();
                   
                    int x = random.nextInt(100000) + 1;
                    int y = 150;
                    int z = random.nextInt(100000) + 1;
                   
                    Location teleportLocation = new Location(player.getWorld(), x, y, z);
                   
                   
                    player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 1000, 5));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 1000, 5));
                   
    
                    player.teleport(teleportLocation);
                   
                    player.sendMessage(ChatColor.GREEN + "You have been teleported " + ChatColor.GOLD + (int)teleportLocation.distance(originalLocation) + ChatColor.GREEN + " blocks away!");
                    player.sendMessage(ChatColor.RED + "You have 0 uses of " + ChatColor.GOLD + "/wild " + ChatColor.RED + "left!");
                } else {
    
                if(name == player.getName()) {
                   
                    plugin.getConfig().set("player-name2", player.getName());
                    plugin.saveConfig();
                    Location originalLocation = player.getLocation();
                   
                    Random random = new Random();
                   
                    int x = random.nextInt(100000) + 1;
                    int y = 150;
                    int z = random.nextInt(100000) + 1;
                   
                    Location teleportLocation = new Location(player.getWorld(), x, y, z);
                   
                   
                    player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 1000, 5));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 1000, 5));
                   
    
                    player.teleport(teleportLocation);
                   
                    player.sendMessage(ChatColor.GREEN + "You have been teleported " + ChatColor.GOLD + (int)teleportLocation.distance(originalLocation) + ChatColor.GREEN + " blocks away!");
                    player.sendMessage(ChatColor.RED + "You have 1 use of " + ChatColor.GOLD + "/wild " + ChatColor.RED + "left!");
                   
                } else {
               
                plugin.getConfig().set("player-name", player.getName());
                plugin.saveConfig();
                Location originalLocation = player.getLocation();
               
                Random random = new Random();
               
                int x = random.nextInt(100000) + 1;
                int y = 150;
                int z = random.nextInt(100000) + 1;
               
                Location teleportLocation = new Location(player.getWorld(), x, y, z);
               
               
                player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 1000, 5));
                player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 1000, 5));
               
                player.teleport(teleportLocation);
               
                player.sendMessage(ChatColor.GREEN + "You have been teleported " + ChatColor.GOLD + (int)teleportLocation.distance(originalLocation) + ChatColor.GREEN + " blocks away!");
                player.sendMessage(ChatColor.RED + "You have 2 uses of " + ChatColor.GOLD + "/wild " + ChatColor.RED + "left!");
                        }
                   
                    }
                }
            } else
                sender.sendMessage(ChatColor.RED + "Only players may execute this command!");
           
            } else
                sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command!");
               
            return false;
        }
    
    }
    
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 31, 2019
  6. Offline

    timtower Administrator Administrator Moderator

    @ZderKi I am gonna assume that it doesn't work because a string compare will always return false when using ==
    Use equals or equalsIgnoreCase instead.
     
  7. Offline

    ZderKi

    Well it worked before... it even wrote it in the config but it looks like it didnt read from the config or something because everytime i disconnected it counted from 3 uses again... and now that i've changed it to equalsIgnoreCase it just throws a NullPointerException .-.
     
  8. Offline

    timtower Administrator Administrator Moderator

    @ZderKi Does it keep resetting the config?
     
  9. Offline

    ZderKi

    Nope... everything stays in the config but the player just has another 3 uses left

    so... any ideas?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 1, 2019
Thread Status:
Not open for further replies.

Share This Page