How can I move minecart with vector not 45 degrees?

Discussion in 'Plugin Development' started by Jaknog, Aug 15, 2017.

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

    Jaknog

    [​IMG]

    Red : Wrong path
    Blue : Correct path

    [​IMG]
    Minecart move like this.

    my code is here

    boolean toward = RandomUtils.nextBoolean();

    double startX = -1000;
    double endX = 1000;

    double startZ = RandomUtils.nextDouble(0,2000)-1000;
    double endZ = RandomUtils.nextDouble(0,2000)-1000;

    if (toward == true) {
    startX = RandomUtils.nextDouble(0,2000)-1000;
    endX = RandomUtils.nextDouble(0,2000)-1000;

    startZ = -1000;
    endZ = 1000;
    }
    complete = false;
    boolean startToEnd = RandomUtils.nextBoolean();


    if(startToEnd) {
    startLocation = new Location(w, startX,200,startZ);
    endLocation = new Location(w, endX,200,endZ);
    } else {
    endLocation = new Location(w, startX,200,startZ);
    startLocation = new Location(w, endX,200,endZ);
    }

    p.teleport(startLocation);
    Minecart cart = p.getWorld().spawn(startLocation, Minecart.class);
    cart.setGlowing(true);
    cart.setPassenger(p);
    cart.setGravity(false);
    cart.setMaxSpeed(2);
    cart.setFlyingVelocityMod(new Vector(1,0,1));
    int id = Bukkit.getScheduler().scheduleSyncRepeatingTask(pubg,new Runnable() {

    @Override
    public void run() {
    cart.setVelocity(endLocation.toVector().subtract(cart.getLocation().toVector()));
    }

    },1, 1);
     
  2. Offline

    HeartandSoul

    Lemme just
    Code:
        boolean toward = RandomUtils.nextBoolean();
    
        double startX = -1000;
        double endX = 1000;
    
        double startZ = RandomUtils.nextDouble(0,2000)-1000;
        double endZ = RandomUtils.nextDouble(0,2000)-1000;
    
        if (toward == true) {
            startX = RandomUtils.nextDouble(0,2000)-1000;
            endX = RandomUtils.nextDouble(0,2000)-1000;
    
            startZ = -1000;
            endZ = 1000;
        }
        complete = false;
        boolean startToEnd = RandomUtils.nextBoolean();
    
    
        if(startToEnd) {
            startLocation = new Location(w, startX,200,startZ);
            endLocation = new Location(w, endX,200,endZ);
        } else {
            endLocation = new Location(w, startX,200,startZ);
            startLocation = new Location(w, endX,200,endZ);
        }
    
        p.teleport(startLocation);
        Minecart cart = p.getWorld().spawn(startLocation, Minecart.class);
        cart.setGlowing(true);
        cart.setPassenger(p);
        cart.setGravity(false);
        cart.setMaxSpeed(2);
        cart.setFlyingVelocityMod(new Vector(1,0,1));
        int id = Bukkit.getScheduler().scheduleSyncRepeatingTask(pubg,new Runnable() {
    
            @Override
            public void run() {
                cart.setVelocity(endLocation.toVector().subtract(cart.getLocation().toVector()));
            }
    
        },1, 1);
    There, formatted. Now, I'm not understanding the problem. Are you saying that the path on your map should be the wrong path as shown in the drawing?
     
  3. Offline

    Jaknog

    no. I code to move minecart with code
    Code:
    cart.setVelocity(endLocation.toVector().subtract(cart.getLocation().toVector()));
    
    but cart moving only 45 degrees.
     
  4. Offline

    HeartandSoul

    Can I see the entire class please?
     
  5. Offline

    Jaknog

    Code:
    package jn.pubg;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class PUBG extends JavaPlugin {
       
        Airplane plane = new Airplane(this);
        Command command = new Command(this);
        public void onEnable() {
            getServer().getPluginManager().registerEvents(plane, this);
            getServer().getPluginManager().registerEvents(command, this);
       
        }
    
    }
    
    Code:
    package jn.pubg;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.map.MapView;
    import org.bukkit.map.MapView.Scale;
    
    
    public class Command implements Listener{
       
        PUBG pubg;
       
        public Command(PUBG pub) {
            pubg = pub;
        }
       
        @EventHandler
        public void onCommand(PlayerCommandPreprocessEvent e) {
            if(e.getMessage().equalsIgnoreCase("/t")) {
                pubg.plane.setRideCartAllPlayers(e.getPlayer().getWorld());
                e.setCancelled(true);
            } else if (e.getMessage().equalsIgnoreCase("/m")) {
                for(Player p : pubg.getServer().getOnlinePlayers()) {
                    MapView map = pubg.getServer().createMap(p.getWorld());
                    map.setCenterX(0);
                    map.setCenterZ(0);
                    map.setScale(Scale.FARTHEST);
                    @SuppressWarnings("deprecation")
                    ItemStack stack = new ItemStack(Material.MAP, 1, map.getId());
                    p.getInventory().addItem(stack);
                }
                e.setCancelled(true);
            }
        }
    
    }
    
    Code:
    package jn.pubg;
    
    import java.util.HashMap;
    
    import org.apache.commons.lang3.RandomUtils;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Minecart;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.vehicle.VehicleExitEvent;
    import org.bukkit.event.world.ChunkLoadEvent;
    import org.bukkit.util.Vector;
    
    public class Airplane implements Listener{
       
        PUBG pubg;
       
        public Airplane(PUBG pub) {
            pubg = pub;
        }
       
        @EventHandler
        public void onLeave(VehicleExitEvent e) {
            LivingEntity le = e.getExited();
            if(le instanceof Player) {
                if(e.getVehicle().getType() == EntityType.MINECART) {
                    e.getVehicle().remove();
                    Bukkit.getScheduler().cancelTask(playerCart.get((Player)le));
                    playerCart.remove((Player)le);
                }
            }
        }
       
        @EventHandler
        public void onDamageVehicle(EntityDamageByEntityEvent event) {
            if(event.getDamager() instanceof Player && event.getEntity() instanceof Minecart) {
                event.setCancelled(true);
            }
        }
       
        HashMap<Player, Integer> playerCart = new HashMap<Player, Integer>();
        Location startLocation;
        Location endLocation;
        boolean complete=false;
       
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onChunkLoad(ChunkLoadEvent e) {
            if(startLocation == null) return;
            if(startLocation.getChunk() == e.getChunk() && complete == false) {
                for(Player p : pubg.getServer().getOnlinePlayers()) {
                    p.teleport(startLocation);
                    Minecart cart = p.getWorld().spawn(startLocation, Minecart.class);
                    cart.setGlowing(true);
                    cart.setPassenger(p);
                    cart.setGravity(false);
                    cart.setMaxSpeed(2);
                    cart.setFlyingVelocityMod(new Vector(1,0,1));
                    int id = Bukkit.getScheduler().scheduleSyncRepeatingTask(pubg,new Runnable() {
       
                        @Override
                        public void run() {
                            cart.setVelocity(endLocation.toVector().subtract(cart.getLocation().toVector()));
                        }
                       
                    },1, 1);
                    playerCart.put(p, id);
                }
                complete = true;
            }
        }
       
        public void setRideCartAllPlayers(World w) {
            boolean toward = RandomUtils.nextBoolean();
           
            double startX = -1000;
            double endX =  1000;
           
            double startZ = RandomUtils.nextDouble(0,2000)-1000;
            double endZ =  RandomUtils.nextDouble(0,2000)-1000;
    
            if (toward == true) {
                startX = RandomUtils.nextDouble(0,2000)-1000;
                endX = RandomUtils.nextDouble(0,2000)-1000;
               
                startZ = -1000;
                endZ = 1000;
            }
            complete = false;
            boolean startToEnd = RandomUtils.nextBoolean();
           
           
            if(startToEnd) {
                startLocation = new Location(w, startX,200,startZ);
                endLocation = new Location(w, endX,200,endZ);
            } else {
                endLocation = new Location(w, startX,200,startZ);
                startLocation = new Location(w, endX,200,endZ);
            }
                startLocation.getChunk().load();
        }
    
    }
    
    Here.
     
  6. Offline

    HeartandSoul

    Quoted him (open)


    It looks OK to me. Are you sure your scheduler is getting started? Try changing the type of the scheduler from int to BukkitTask (That probably won't help though).
     
  7. Offline

    Jaknog

    No..I know that code is work but why cart move 45 degrees only?
     
    Last edited by a moderator: Aug 15, 2017
  8. Offline

    timtower Administrator Administrator Moderator

    @Jaknog Does it work with a different entity?
     
Thread Status:
Not open for further replies.

Share This Page