Search code examples
javapluginsminecraftbukkit

I want to teleport Players if he respawns, but its not working, the player don't get teleported


So I made a plugin that registers events after doing /pvpeventon and unregisters all after doing /pvpeventoff. It is a PVPeventplugin. I want to teleport Players after they respawn, but its not working. they don't get teleported. btw: I don't code Spigot a long time. here is my code:

package me.leopa.R1.FFA;




import org.bukkit.Location;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;

import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.plugin.java.JavaPlugin;

import net.md_5.bungee.api.ChatColor;




public class MainFFA extends JavaPlugin implements Listener{

@Override
public void onEnable() {
    System.out.println("[INFO Leopa] Start");
    super.onEnable();
}

@Override
public void onDisable() {
System.out.println("[INFO Leopa] Stop");
super.onDisable();
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {


    if(command.getName().equalsIgnoreCase("PVPEVENTon")) {
        if(sender instanceof Player) {
        getServer().getPluginManager().registerEvents(this, this);

        }
    }



    if(command.getName().equalsIgnoreCase("PVPEVENToff")) {
        HandlerList.unregisterAll();
    }
    return true;


    }
@EventHandler
public void onRespawnPVPEVENT(PlayerRespawnEvent pvpevent) {
    Player p = pvpevent.getPlayer();
    double x = 22;
    double y = 82;
    double z = 22;
    Location loc = new Location(Bukkit.getWorld("world"), x, y, z);
    p.teleport(loc);


}
@EventHandler
public void sed(PlayerDeathEvent totevent) {
    Player p1 = totevent.getEntity();
    p1.sendMessage(ChatColor.GOLD + "[PvP Event] " + ChatColor.AQUA + "Du Bist Gestorben und somit raus");
}


}

Solution

  • The PlayerRespawnEvent has a method, setRespawnLocation(), that allows you to change where the player will respawn. Your handler for PlayerRespawnEvent should look something like this:

    @EventHandler
    public void onRespawnPVPEVENT(PlayerRespawnEvent pvpevent) {
        Player p = pvpevent.getPlayer();
        double x = 22;
        double y = 82;
        double z = 22;
        Location loc = new Location(Bukkit.getWorld("world"), x, y, z);
        pvpevent.setRespawnLocation(loc);
    }