简体   繁体   中英

How can I check if the player is in a boat?

Heyo. I want to check if the player is in a boat? How can I do that. The code should kill the player if he touches ice and isn't in a boat. It's for an ice race game. I tried

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;

public class icetouch implements Listener {
    @EventHandler
    public void onMove(PlayerMoveEvent event) {
        Player p = event.getPlayer();
            if (p.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.DIRT) {
                if (p.isInsideVehicle()) {
                    if(p.getVehicle() instanceof boat) {
                        
                    } else {
                        p.setHealth(0);
                    }
                    
                }
            }
            
            
            }

but it doesn't detect the "boat" vehicle.

You should use Boat (with Upper case at begin) and add import org.bukkit.entity.Boat;

Like that:

@EventHandler
public void onMove(PlayerMoveEvent event) {
    Player p = event.getPlayer();
    if (p.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.DIRT) {
        if (p.isInsideVehicle() && p.getVehicle() instanceof Boat) {
            // on boat and on dirt
        } else { // everytime else
            p.setHealth(0); // kill
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM