| 1 | package me.fridtjof.minecarttrains.utils; |
| 2 | |
| 3 | import org.bukkit.entity.minecart.PoweredMinecart; |
| 4 | import org.bukkit.util.Vector; |
| 5 | |
| 6 | public final class FurnaceCarts |
| 7 | { |
| 8 | |
| 9 | private static final double MIN_PUSH = 1.0E-4; |
| 10 | |
| 11 | private FurnaceCarts() |
| 12 | { |
| 13 | } |
| 14 | |
| 15 | public static boolean hasDrive(PoweredMinecart cart) |
| 16 | { |
| 17 | return cart.getFuel() > 0 && Math.hypot(cart.getPushX(), cart.getPushZ()) >= MIN_PUSH; |
| 18 | } |
| 19 | |
| 20 | public static void setPushFromDirection(PoweredMinecart cart, Vector direction) |
| 21 | { |
| 22 | double x = direction.getX(); |
| 23 | double z = direction.getZ(); |
| 24 | double length = Math.hypot(x, z); |
| 25 | if (length < MIN_PUSH) |
| 26 | { |
| 27 | return; |
| 28 | } |
| 29 | cart.setPushX(x / length); |
| 30 | cart.setPushZ(z / length); |
| 31 | } |
| 32 | |
| 33 | public static void reversePush(PoweredMinecart cart) |
| 34 | { |
| 35 | if (Math.hypot(cart.getPushX(), cart.getPushZ()) < MIN_PUSH) |
| 36 | { |
| 37 | setPushFromDirection(cart, cart.getVelocity()); |
| 38 | } |
| 39 | cart.setPushX(-cart.getPushX()); |
| 40 | cart.setPushZ(-cart.getPushZ()); |
| 41 | } |
| 42 | |
| 43 | public static void restorePushFromMotion(PoweredMinecart cart) |
| 44 | { |
| 45 | if (cart.getFuel() <= 0) |
| 46 | { |
| 47 | return; |
| 48 | } |
| 49 | if (Math.hypot(cart.getPushX(), cart.getPushZ()) >= MIN_PUSH) |
| 50 | { |
| 51 | return; |
| 52 | } |
| 53 | setPushFromDirection(cart, cart.getVelocity()); |
| 54 | } |
| 55 | } |