#Minecraft plugin development

7 messages · Page 1 of 1 (latest)

frank dagger
#

Hello ! I'm coming to you because I'm brand new to Java and I'm encountering a problem.
My goal is to create a plugin that will allow boat races! So the first challenge, which seemed simple to me, is to be able to assemble blocks with a boat ☠️
The problem is that you can't teleport a boat or a player when a player is in a boat! You have to modify the boat's velocity, and these values are quite complex 😅

So I did a few tests, which turned out to be pretty good, with a block of sponge under the track:

public final class BoatRace extends JavaPlugin implements Listener {
    private final HashMap<UUID, BukkitTask> raceTimers = new HashMap<>();

    @Override
    public void onEnable() {
        getLogger().info("BoatRace has been enabled!");

        getServer().getPluginManager().registerEvents(this, this);
    }

    @Override
    public void onDisable() {
        getLogger().info("BoatRace has been disabled!");
    }

    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
        Player player = event.getPlayer();
        if (player.isInsideVehicle() && player.getVehicle() instanceof Boat) {
            Boat boat = (Boat) player.getVehicle();
            Location boatLocation = boat.getLocation();

            // Vérifier le bloc deux blocs en dessous du bateau
            Block blockTwoBelow = boatLocation.getBlock().getRelative(BlockFace.DOWN, 2);
            if (blockTwoBelow.getType() == Material.SPONGE) {
                boat.setVelocity(boat.getVelocity().add(new Vector(0, 0.6, 0)));
            }
        }
    }
}

But....

forest boughBOT
#

This post has been reserved for your question.

Hey @frank dagger! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

frank dagger
#

Wanting to test a more versatile method, I created this code:

public class BoatMoveListener implements Listener {

    @EventHandler
    public void onVehicleMove(VehicleMoveEvent event) {
        Player player = (Player) event.getVehicle().getPassenger(); // Récupère le joueur à bord du véhicule
        if (player == null || !(event.getVehicle() instanceof Boat)) {
            return;
        }

        Boat boat = (Boat) event.getVehicle();
        Location from = event.getFrom();
        Location to = event.getTo();

        // Vérifier si le bateau se déplace horizontalement (pas de montée/descente)
        if (from.getBlockY() == to.getBlockY()) {
            Block blockBelow = to.clone().add(0, -1, 0).getBlock();

            if(isIceBlock(blockBelow)){
                player.sendMessage(">> Boat: move");

                Vector boatDirection = boat.getLocation().getDirection().normalize();

                Location blockTwoAheadLocation = boat.getLocation().clone().add(boatDirection.multiply(2));

                Block blockTwoAhead = blockTwoAheadLocation.getBlock();

                boat.setGravity(false);
                if (isIceBlock(blockBelow) && blockTwoAhead.getType() != Material.AIR) {
                    // Modifier la vélocité du bateau pour le faire monter d'un bloc
                    Vector currentVelocity = boat.getVelocity();
                    Vector newVelocity = new Vector(boatDirection.getX(), 2, boatDirection.getZ()); // Maint X, Z, mais edit Y
                    player.sendMessage(">> Boat: X; Y+1; Z");
                    boat.setVelocity(newVelocity);
                }
            }
            else{
                boat.setGravity(true);
            }
        }
    }

    private boolean isIceBlock(Block block) {
        Material type = block.getType();
        return type == Material.ICE || type == Material.PACKED_ICE || type == Material.BLUE_ICE;
    }
}
#

And as you can see, we have the same boat mounting system:

Code n°1:

            if (blockTwoBelow.getType() == Material.SPONGE) {
                boat.setVelocity(boat.getVelocity().add(new Vector(0, 0.6, 0)));

Code n°2:

                    Vector currentVelocity = boat.getVelocity();
                    Vector newVelocity = new Vector(boatDirection.getX(), 2, boatDirection.getZ()); // Maint X, Z, mais edit Y
                    player.sendMessage(">> Boat: X; Y+1; Z");
                    boat.setVelocity(newVelocity);

And yet! Code n°2 doesn't move my boat to Y and I don't know why....

#

If you have any ideas or suggestions I'll take it all 🙏🏻
Thanks in advance! 😇

forest boughBOT