Respawn Help

Discussion in 'Spigot Plugin Development' started by DotRar, May 20, 2015.

  1. Hey, I have another bug in my minigame, when a player dies and respawns, it sends them to a random location in the world. Here it is basically a vanilla server, with land, trees ect. Here's my code:

    Code (Text):
    package net.ultimamc.dev.earthgames.handlers;

    import net.ultimamc.dev.earthgames.Core;
    import net.ultimamc.dev.earthgames.methods.Join;

    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerRespawnEvent;

    public class RespawnHandler implements Listener {
       
        Core plugin;
       
        public RespawnHandler(Core plugin) {
            this.plugin = plugin;
        }
       
        @EventHandler
        public void respawnEvent(PlayerRespawnEvent e) {
            Join.getInstance().spawn(e.getPlayer());
        }
    }
     
    And obviously you want to see the Join class:

    Code (Text):
    package net.ultimamc.dev.earthgames.methods;

    import java.util.Random;

    import net.ultimamc.dev.earthgames.economy.Eco;
    import net.ultimamc.dev.earthgames.utilities.TeamHandler;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.scheduler.BukkitScheduler;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;

    import com.google.common.collect.Sets;

    public class Join {
       
        private Join() {}
        private static Join instance = new Join();
        public static Join getInstance() {
            return instance;
        }
       
        private String team;
       
        public void handle(Player p, Plugin pl) {
            PlayerInventory pi = p.getInventory();
            pi.clear();
            Eco.setup(p);
            handleTeams(p);
            spawn(p);
        }
       
        public void spawn(Player p) {
            if (TeamHandler.getInstance().getTeam(p) == "red") {
                GiveDefault.give(p, "red");
                p.teleport(new Location(Bukkit.getWorld("world"), -294.3, 71, -296.7));
                p.getLocation().setPitch(45);
                p.getLocation().setYaw(5);
            }
            if (TeamHandler.getInstance().getTeam(p) == "blue") {
                GiveDefault.give(p, "blue");
                p.teleport(new Location(Bukkit.getWorld("world"), -327.7, 71, -256.3));
                p.getLocation().setPitch(-135);
                p.getLocation().setYaw(5);
            }
        }
       
        private void handleTeams(Player p) {
            if (TeamHandler.getInstance().redSize() > TeamHandler.getInstance().blueSize()) {
                TeamHandler.getInstance().add(p, "blue");
            }
            if (TeamHandler.getInstance().redSize() < TeamHandler.getInstance().blueSize()) {
                TeamHandler.getInstance().add(p, "red");
            }
            if (TeamHandler.getInstance().redSize() == TeamHandler.getInstance().blueSize()) {
                Random rand = new Random();
                int n = rand.nextInt((2 - 1) + 1) + 1;
                if (n == 1) {
                    TeamHandler.getInstance().add(p, "blue");
                    this.team = "blue";
                }
                else {
                    TeamHandler.getInstance().add(p, "red");
                    this.team = "red";
                }
            }
        }
    }
     
    And yes, it's correctly registered in the Core class.
     
  2. So you are using Join as an object? It seems you never initialize a new Join object.. though.. I am honestly confused why you would do it this way :/.
     
  3. All you need to do is catch the PlayerRespawnEvent, and put a one second delay on it, then teleport the players.
     
  4. Why not using the PlayerRespawnEvent#setRespawnLocation?
     
  5. EntityDamageByEntityEvent
    if the health - the damage >= 0 then refill health and run it as if they died and respawn them, another advantage is people don't need to click respawn (i find that quite annoying having to anyhow)
     
  6. You then can use the Spigot#respawn method or send a packet to respawn the player directly (without clicking the respawn button) when using craftbukkit.
     
    • Agree Agree x 1