Rdm teleport

Discussion in 'Plugin Development' started by Chakkar, Apr 18, 2015.

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

    Chakkar

    so I've made just a random plugin for my entertainment and I have come across a problem probably very simple that i can't figure out... it's a random teleport plugin and I've been trying to add a cooldown to it, so yeah. ( I'm very new to this)

    -- Problem is for some reason command on the second line "command.getName" the command part is underlined and has no actual options to fix it== ( yes i have tried "cmd" instead and that just creates other messed up error)

    Code:
      if (cmd.getName().equalsIgnoreCase("teleportme") && sender instanceof Player) {
               CommandSender p;
              if (command.getName().equalIsIgnoreCase("cooldown")){
    Code:
    package me.kalo121;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Random;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class Teleporter extends JavaPlugin {
     
        private Map<String, Long> lastUsage = new HashMap<String, Long>();
        private final int cdtime = 600;
     
        @Override
        public void onEnable() {
         
     
        }
    
        @Override
        public void onDisable() {
         
         
        }
     
     
    
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         
            if (cmd.getName().equalsIgnoreCase("teleportme") && sender instanceof Player) {
               CommandSender p;
              if (command.getName().equalIsIgnoreCase("cooldown")){
                 
                
                  long lastUsed = 0;
                   if(lastUsage.containsKey(sender.getName())){
                     lastUsed = lastUsage.get(sender.getName());
                    
                   }
                
                   int cdmillis = cdtime = 3600000;
                
                   if(System.currentTimeMillis()-lastUsed>cdmillis) {
                    
                      sender.sendMessage("Okay! Command run!");
                      lastUsage.put(sender.getName(), System.currentTimeMillis());
                    
                
                   }
                   else{
                       int timeLeft = (int) (cdtime-((System.currentTimeMillis()-lastUsed)/3600000));
                       sender.sendMessage("This command is on cooldown for you for another "+ timeLeft +" seconds ");
                   }
                        
                
               }
         
                
               }
             
             
             Player player = (Player) sender;
             
                Location originalLocation = player.getLocation();
             
                Random random = new Random();
             
                Location teleportLocation = null;
             
                int x = random.nextInt(1000) + 1;
                int y = 150;
                int z = random.nextInt(1000) + 1;
             
                boolean isOnLand = false;
             
                while (isOnLand == false) {
    
                    teleportLocation = new Location(player.getWorld(), x, y, z);
                 
                    if (teleportLocation.getBlock().getType() != Material.AIR) {
                        isOnLand = true;
                    } else y--;
             
                }
             
                player.teleport(new Location(player.getWorld(), teleportLocation.getX(), teleportLocation.getY() + 1, teleportLocation.getZ()));
             
                player.sendMessage(ChatColor.GREEN + "You have been teleported " + (int)teleportLocation.distance(originalLocation) + " blocks away!");
             
                return true;
             
             
         
            }
     
     
    
    }
    
     
    Last edited: Apr 18, 2015
  2. Offline

    I Al Istannen

    @Chakkar You did not define the variable command. "cmd.getName()" should work, post the error you get. You wrote the ".equalsIgnoreCase" wrong(case SENstitve, Line 39). Further, this if will never be true. Because if the command is "teleportme" it can't be "cooldown". And what do you have "CommandSender p;" (Line 38) for?
     
  3. Your cooldown system looks very complicated. I make them like this:
    Code:java
    1. private final List<UUID> cooldown = new ArrayList<UUID>();
    2.  
    3. //And put this where you want the cooldown to be:
    4.  
    5. if (cooldown.contains(p.getUniqueId())){
    6. //send message, player is still in cooldown
    7. } else {
    8. //add player to cooldown
    9. cooldown.add(p.getUniqueId());
    10. //Remove it after, for example, 3 seconds:
    11. new BukkitRunnable() {
    12. public void run() {
    13. cooldown.remove(p.getUniqueId());
    14. }
    15. }.runTaskLater(this, 3 * 20);
    16.  
    17. //your normal code
    18. }

    The List & Player must be final, to use them inside the runnable.
     
  4. Offline

    Chakkar

    @I Al Istannen
    Thanks a ton on that information
    okay so now I'm getting this error
    it says "Remove "final" modifier" and it removes the final part up at the start of the code.. and then no error? so does that mean everything is fixed can I actually run it without the final being before the "int cdtime" ??

    Code:
      int cdmillis = cdtime = 3600000;

    and

    @FisheyLP

    how do I actually put that code to play inside my code? I'm so used to doing my hard stupid complicated methods which are crap :p wish I was better with java

    Okay thanks for all the help again and so i've gone in and now it's saying that This command is on cooldown for 36000 seconds but its not actually on cooldown like I can keep using it? just wondering what's happening lol...
     
    Last edited by a moderator: Apr 18, 2015
  5. Offline

    I Al Istannen

    @Chakkar Add a "return;" after the "Is on cooldown message" This way the onCommand() will break at that line and execute no more code. Currently it just continues. And FisheyLp's code is quite good to understand. What do you don't understand?
     
  6. Offline

    Chakkar

    @I Al Istannen

    yay got it working, thanks a ton dude and sorry I'm just not very good at java I've been doing it for about a week now maybe, hardly and I just don't really get anything yet :/ would be a ton easier to use hid code though it looks a lot neater
     
  7. Offline

    I Al Istannen

    @Chakkar
    You created a Map which has a String as Key and a Long as Value.
    He created a List with the UUID of the player. That is an unique id, which definitly defines the player. No other player in this world will have the same UUID.

    You put the player name in the map and the time the player used the command.
    He put the UUID of the player in the list.

    You check if the Long value is smaller than the current time.
    He checks if the player is on the list.

    You update the time if he can and does use the command again.
    He adds the UUID everytime the command is used and the UUID isnt on the list.

    But why does his work? From what i told he never removes the player. Yes, but he does in the last bit of his code.
    He creates a new BukkitRunnable, another task. This task will run parallel to the normal Server. And this task will start after the delay in line 15. The delay is measured in "Ticks". Each tick is a 20th of a second. So you need to multiply the secods with 20, like he does. But what happens if this task gets called?
    It just removes the player from the list. So after the delay the list Won't contain the player anymore. And that means the player can use the command again, because the check for cooldown is, like i explained above: If player is in List => still on cooldown.

    Did you understand this? If not, feel free to ask! And good luck learning Java, there a few topics about where to learn in the forum :).
     
    FisheyLP likes this.
  8. Offline

    Chakkar

    @I Al Istannen

    Very nice explanation there! thanks a for going out of your way to help :) I'll run a tag here if I need you at all, thanks a ton.
     
  9. Offline

    ResultStatic

    do not use runnable cooldowns... use system time. you can compare system time and its 1000x more accurate and doesnt use as much cpu.
    @Chakkar
     
    nj2miami likes this.
  10. Offline

    Chakkar

Thread Status:
Not open for further replies.

Share This Page