package me.fridtjof.minecarttrains.events; import me.fridtjof.minecarttrains.managers.LinkageManager; import me.fridtjof.minecarttrains.MinecartTrains; import me.fridtjof.minecarttrains.utils.FurnaceCarts; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Tag; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.data.type.RedstoneRail; import org.bukkit.entity.Entity; import org.bukkit.entity.Minecart; import org.bukkit.entity.Vehicle; import org.bukkit.entity.minecart.PoweredMinecart; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.vehicle.VehicleUpdateEvent; import org.bukkit.util.Vector; public class OnVehicleUpdateEvent implements Listener { static MinecartTrains plugin = MinecartTrains.getInstance(); LinkageManager linkageManager = plugin.linkageManager; // config vars int fuelPerTick = plugin.configManager.mainConfig.getConfig().getInt("trains.fuel.consumption_per_tick"); double maxDistance = plugin.configManager.physicsConfig.getConfig().getDouble("link.distance.max"); double minDistance = plugin.configManager.physicsConfig.getConfig().getDouble("link.distance.min"); double couplingPullSpeedLowSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.low_speed.speed.pull"); double couplingPushSpeedLowSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.low_speed.speed.push"); double aimedDistanceLowSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.low_speed.distance.aimed"); double aimedDistanceToleranceLowSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.low_speed.distance.aimed_tolerance"); double couplingPullSpeedHighSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.high_speed.speed.pull"); double couplingPushSpeedHighSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.high_speed.speed.push"); double aimedDistanceHighSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.high_speed.distance.aimed"); double aimedDistanceToleranceHighSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.high_speed.distance.aimed_tolerance"); double highSpeedGate = plugin.configManager.physicsConfig.getConfig().getDouble("link.high_speed.gate_speed"); @EventHandler public void onVehicleUpdate(VehicleUpdateEvent event) { Vehicle vehicle = event.getVehicle(); Entity parent; if(!(vehicle instanceof Minecart minecart)) { return; } linkageManager.remember(minecart); if (minecart instanceof PoweredMinecart poweredCart) { handlePoweredCart(poweredCart); } // ensures the below function only works on trains, not on single minecarts if(!linkageManager.hasLink((minecart))) { return; } parent = linkageManager.getParent(minecart); // breaks link if parent cart is broken if(parent == null) { linkageManager.removeLink(minecart); return; } // breaks link if the parent and child are the same cart if(parent == minecart) { plugin.getLogger().warning(plugin.configManager.mainConfig.getConfig().getString("trains.coupling_already_coupled_with_itself")); linkageManager.removeLink(minecart); return; } //check if parent is in same world World childWorld = minecart.getWorld(); World parentWorld = parent.getWorld(); if(childWorld != parentWorld) { linkageManager.removeLink(minecart); return; } Block parentBlock = parent.getLocation().getBlock(); if (!isRail(parentBlock.getType()) && !isRail(parentBlock.getRelative(BlockFace.DOWN).getType())) { linkageManager.removeLink(minecart); return; } double distanceSquared = minecart.getLocation().distanceSquared(parent.getLocation()); double maxDistanceSquared = maxDistance * maxDistance; double minDistanceSquared = minDistance * minDistance; /* Sets the speed at which carts move towards their dependant. At HSRail speeds, this value must be high to avoid breaking trains during acceleration. Values in excess of 5 require more precision for the 'else if' statements, or else carts will overshoot and collide, disrupting momentum and causing the 'jiggle' bug.*/ double pullSpeed; double pushSpeed; double aimedDistance; double aimedDistanceTolerance; double highSpeedGateSquared = highSpeedGate * highSpeedGate; if(parent.getVelocity().lengthSquared() <= highSpeedGateSquared) { pullSpeed = couplingPullSpeedLowSpeed; aimedDistance = this.aimedDistanceLowSpeed; aimedDistanceTolerance = this.aimedDistanceToleranceLowSpeed; } else { pullSpeed = couplingPullSpeedHighSpeed; aimedDistance = this.aimedDistanceHighSpeed; aimedDistanceTolerance = this.aimedDistanceToleranceHighSpeed; } if(minecart.getVelocity().lengthSquared() <= highSpeedGateSquared) { pushSpeed = couplingPushSpeedLowSpeed; } else { pushSpeed = couplingPushSpeedHighSpeed; } if((distanceSquared > maxDistanceSquared) || distanceSquared < minDistanceSquared) { linkageManager.removeLink(minecart); minecart.setVelocity(new Vector(0, 0, 0)); parent.setVelocity(new Vector(0, 0, 0)); return; } double distance = Math.sqrt(distanceSquared); double aimedLow = aimedDistance - aimedDistanceTolerance; double aimedHigh = aimedDistance + aimedDistanceTolerance; if (distance >= aimedLow && distance <= aimedHigh && minecart.getVelocity().lengthSquared() < 0.0001 && parent.getVelocity().lengthSquared() < 0.0001) { return; } Entity follower = minecart; Entity anchor = parent; Minecart leader = linkageManager.findLeader(minecart); if (leader != null && linkageManager.isCloserToLeader(minecart, (Minecart) parent, leader)) { follower = parent; anchor = minecart; } if (leader != null && follower.getUniqueId().equals(leader.getUniqueId())) { Entity swap = follower; follower = anchor; anchor = swap; } if (distance < aimedLow) { moveToward(follower, anchor, pushSpeed, (distance - aimedDistance)); } else if (distance > aimedHigh) { moveToward(follower, anchor, pullSpeed, (distance - aimedDistance)); } } private void handlePoweredCart(PoweredMinecart poweredCart) { int fuelAfterConsumption = poweredCart.getFuel() - fuelPerTick + 1; if (fuelAfterConsumption < 0) { fuelAfterConsumption = 0; } poweredCart.setFuel(fuelAfterConsumption); FurnaceCarts.restorePushFromMotion(poweredCart); Block block = poweredCart.getLocation().getBlock(); if (block.getType() != Material.POWERED_RAIL) { return; } RedstoneRail redstoneRail = (RedstoneRail) block.getBlockData(); if (redstoneRail.isPowered()) { return; } poweredCart.setVelocity(new Vector(0, 0, 0)); poweredCart.setFuel(poweredCart.getFuel() + fuelPerTick); } // defines moveToward, which handles the elastic movement of dependant carts. Uses the speed and direction set earlier public void moveToward(Entity child, Entity parent, double speed, double distance) { Location childLoc = child.getLocation(); Location parentLoc = parent.getLocation(); double x = childLoc.getX() - parentLoc.getX(); double y = childLoc.getY() - parentLoc.getY(); double z = childLoc.getZ() - parentLoc.getZ(); Vector velocity = new Vector(x, y, z).normalize().multiply(-speed); child.setVelocity(velocity.multiply((distance * distance * distance))); } private boolean isRail(Material material) { return Tag.RAILS.isTagged(material); } }