import net.minecraftforge.event.entity.minecart.MinecartUpdateEvent; //导入依赖的package包/类
public void onCartUpdate(MinecartUpdateEvent event){
EntityMinecart cart = event.getMinecart();
if(!cart.world.isRemote) {
if(isMotorized()) {
boolean shouldRun = true;
EnumFacing cartDir = cart.getAdjustedHorizontalFacing();
if(new Vec3d(cart.motionX, cart.motionY, cart.motionZ).lengthVector() < 0.05) {
shouldRun = false;
if(hopperTimer > 0) {
hopperTimer--;
}
if(hopperTimer == 0) {
hopperTimer = extractFuelFromHopper(cart, event.getPos()) ? 8 : 40;
}
} else {
hopperTimer = 0;
RailWrapper rail = RailCacheManager.getInstance(cart.world).getRail(cart.world, event.getPos());
if(rail == null) {
shouldRun = false;
} else {
TileEntitySignalBase signal = TileEntitySignalBase.getNeighborSignal(rail, cartDir.getOpposite());
shouldRun = signal == null || signal.getLampStatus() != EnumLampStatus.RED;
if(!shouldRun) {
cart.motionX = 0;
cart.motionZ = 0;
}
}
}
if(shouldRun && useFuel(cart)) {
if(!motorActive) NetworkHandler.sendToAllAround(new PacketUpdateMinecartEngineState(cart, true), new NetworkRegistry.TargetPoint(cart.world.provider.getDimension(), cart.getPositionVector().x, cart.getPositionVector().y, cart.getPositionVector().z, 64));
motorActive = true;
double acceleration = 0.03D;
cart.motionX += cartDir.getFrontOffsetX() * acceleration;
cart.motionZ += cartDir.getFrontOffsetZ() * acceleration;
cart.motionX = MathHelper.clamp(cart.motionX, -cart.getMaxCartSpeedOnRail(), cart.getMaxCartSpeedOnRail());
cart.motionZ = MathHelper.clamp(cart.motionZ, -cart.getMaxCartSpeedOnRail(), cart.getMaxCartSpeedOnRail());
} else {
if(motorActive) NetworkHandler.sendToAllAround(new PacketUpdateMinecartEngineState(cart, true), new NetworkRegistry.TargetPoint(cart.world.provider.getDimension(), cart.getPositionVector().x, cart.getPositionVector().y, cart.getPositionVector().z, 64));
motorActive = false;
}
}
} else {
if(motorActive) {
cart.world.spawnParticle(EnumParticleTypes.SMOKE_LARGE, cart.getPositionVector().x, cart.getPositionVector().y, cart.getPositionVector().z, 0, 0, 0);
}
}
}
请发表评论