How do i create a warmup for /home or /spawn

Discussion in 'Plugin Development' started by Michiman, Jun 27, 2015.

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

    Michiman

    How do i create a 3 second warmup for /home and /spawn. And if they get hit or move it cancels. will it be in a listener or on the main page?
    But i do not know how to set it up. Like i keep seeing different posts but they leave out the part of the code that i need to see.
     
  2. Offline

    tytwining

    @Michiman One thing you can do is make a boolean and turn the boolean to false if player move event is called and use a runnable to wait 3 seconds and if the boolean is true then teleport the player, if not then do nothing.
     
  3. Offline

    Michiman

    Im sorry to be a scrub at this but can you make like a template sort of so i know what to follow. Cause thats the information that i keep getting but like i do not know how to put that into the code.
     
  4. Offline

    tytwining

    I'm not sure if this is a great way to do this, but it's how I would do it:

    1) Create a HashMap<String,boolean> in a class
    2) If a player is "warming up" to teleporting, set the player name to be "true"
    3) Make a runnable to delay 3 seconds.
    4) If the boolean is true, teleport the player

    5) You also need a PlayerMoveEvent turning the string to "false" if the player has moved.
     
  5. Offline

    Michiman

    ok i looked at a few things and put something together. but it is not working. Im just looking for a template that i can use other places. Cause i dont know how to do the hashmap and then also add in other things for it to work
     
  6. Offline

    tytwining

    You can look it up here. https://hub.spigotmc.org/javadocs/bukkit/
     
  7. Offline

    Michiman

    Here is what someone said, Do i put this in a listener and what goes into the //code area

    1. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    2. public void run() {
    3. //code
    4. }
    5. }, 3 * 20);
     
  8. Offline

    tytwining

    Actually, I always try to over complicate things. You can just have the location of the player stored when they execute it then after if their location is that location then teleport them.
     
  9. Offline

    Michiman

    hmm ok would that go in the //code area?
     
  10. Offline

    tytwining

    A part of it would.
     
  11. Offline

    Michiman

    What part of it would not go in there? Sorry im new to this also im trying to just learn how to do this certain part
     
  12. Offline

    tytwining

    The part storing the previous location and the part saving the location.
     
  13. Offline

    ArmyArmy

    Here's the code for ya.

    This is the Main class
    Code:
    package com.google.army.exampleplugin;
    
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class ExamplePlugin extends JavaPlugin{
       
       public static ExamplePlugin instance;
       
       private ExampleListener listener;
       
       public HashMap<String, Integer> map = new HashMap<String, Integer>(); // Map to save the players in
       // We're saving the int for later cancellation of the Task
       
       public void onEnable(){ // Basic onEnable()
         listener = new ExampleListener(this);
         getServer().getPluginManager().registerEvents(listener, this);
         instance = this;
       }
       
       public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
         if(sender instanceof Player){
           final Player player = (Player)sender;
           if(commandLabel.equalsIgnoreCase("spawn")){
             if(!map.containsKey(player.getName())){
               player.sendMessage("[ExamplePlugin] §bPlease stand still for 3 seconds.");
               int ID = Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    
                 @Override
                 public void run() {
                   player.teleport(Bukkit.getWorlds().get(0).getSpawnLocation()); // You should probably place your own location here
                 }
               }, 60L);
               map.put(player.getName(), Integer.valueOf(ID)); // Add player to the map, so we know he's suposed to be still
               return true;
             }else{
               player.sendMessage("[ExamplePlugin] §cAlready teleporting!");
               return false;
             }
           }
           return false;
         }else{
           return false;
         }
       }
    }
    
    And this is the Listener
    Code:
    package com.google.army.exampleplugin;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    
    public class ExampleListener implements Listener{
       
       private ExamplePlugin instance;
       
       public ExampleListener(ExamplePlugin instance){
         this.instance = instance;
       }
       
       @EventHandler
       public void onMove(PlayerMoveEvent e){
         Player player = e.getPlayer();
         if(instance.map.containsKey(player.getName())){
           Bukkit.getScheduler().cancelTask(instance.map.get(player.getName()));
           player.sendMessage("[ExamplePlugin] §cTeleportation cancelled!");
           instance.map.remove(player.getName());
         }
       }
       
       @EventHandler
       public void onDamage(EntityDamageEvent e){
         Entity entity = e.getEntity();
         if(entity instanceof Player){
           Player player = (Player)entity;
           if(instance.map.containsKey(player.getName())){
             Bukkit.getScheduler().cancelTask(instance.map.get(player.getName()));
             player.sendMessage("[ExamplePlugin] §cTeleportation cancelled!");
             instance.map.remove(player.getName());
           }
         }
       }
    }
    
    It's not tested, but you should get the idea.
     
Thread Status:
Not open for further replies.

Share This Page