Skip to content

Commit 2fdad72

Browse files
committed
Update autopilot restrictions and bump version
1 parent 06af3ad commit 2fdad72

File tree

13 files changed

+134
-1
lines changed

13 files changed

+134
-1
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ immersive_aircraft_version=1.2.2+1.21.1
66

77
mod_id=immersive_autopilot
88
mod_name=Immersive Autopilot
9-
mod_version=0.9.0
9+
mod_version=1.0-beta1
1010
mod_group=com.immersiveautopilot

src/main/java/com/immersiveautopilot/autopilot/AutopilotSupport.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.immersiveautopilot.autopilot;
22

33
import com.immersiveautopilot.item.ModItems;
4+
import immersive_aircraft.entity.AirshipEntity;
5+
import immersive_aircraft.entity.GyrodyneEntity;
46
import immersive_aircraft.entity.InventoryVehicleEntity;
7+
import immersive_aircraft.entity.QuadrocopterEntity;
58
import immersive_aircraft.entity.VehicleEntity;
69
import immersive_aircraft.entity.inventory.VehicleInventoryDescription;
710
import net.minecraft.world.item.ItemStack;
@@ -21,6 +24,19 @@ public static boolean hasAutopilot(VehicleEntity vehicle) {
2124
return false;
2225
}
2326

27+
public static boolean isAutopilotSupported(VehicleEntity vehicle) {
28+
if (vehicle instanceof QuadrocopterEntity) {
29+
return false;
30+
}
31+
if (vehicle instanceof AirshipEntity) {
32+
return false;
33+
}
34+
if (vehicle instanceof GyrodyneEntity) {
35+
return false;
36+
}
37+
return true;
38+
}
39+
2440
public static boolean isAutopilotEnabled(VehicleEntity vehicle) {
2541
if (vehicle instanceof AutopilotStateAccess access) {
2642
return access.immersiveAutopilot$isAutopilotEnabled();

src/main/java/com/immersiveautopilot/client/AutoRouteClient.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ public static int getIndex(int vehicleId) {
5555
return INDICES.getOrDefault(vehicleId, 0);
5656
}
5757

58+
public static void cycleRoute(int vehicleId) {
59+
List<Entry> entries = ROUTES.getOrDefault(vehicleId, Collections.emptyList());
60+
if (entries.isEmpty()) {
61+
return;
62+
}
63+
int current = INDICES.getOrDefault(vehicleId, 0);
64+
int next = (current + 1) % entries.size();
65+
INDICES.put(vehicleId, next);
66+
ACCEPTED.put(vehicleId, false);
67+
PENDING.remove(vehicleId);
68+
if (!trySelectByIndex(vehicleId, next)) {
69+
requestRoutes(vehicleId);
70+
}
71+
}
72+
5873
public static boolean tryAutoAccept(int vehicleId, List<RouteEntry> offers) {
5974
return false;
6075
}
@@ -157,6 +172,44 @@ private static void evaluateOffers(int vehicleId) {
157172
}
158173
}
159174

175+
private static boolean trySelectByIndex(int vehicleId, int index) {
176+
List<Entry> entries = ROUTES.get(vehicleId);
177+
if (entries == null || entries.isEmpty()) {
178+
return false;
179+
}
180+
if (index < 0 || index >= entries.size()) {
181+
return false;
182+
}
183+
String desired = entries.get(index).routeName();
184+
if (desired == null || desired.isBlank()) {
185+
return false;
186+
}
187+
List<OfferCandidate> offers = OFFERS.getOrDefault(vehicleId, Collections.emptyList());
188+
if (offers.isEmpty()) {
189+
return false;
190+
}
191+
List<OfferCandidate> matches = new ArrayList<>();
192+
for (OfferCandidate candidate : offers) {
193+
if (desired.equals(candidate.entry().name())) {
194+
matches.add(candidate);
195+
}
196+
}
197+
if (matches.isEmpty()) {
198+
return false;
199+
}
200+
if (matches.size() == 1) {
201+
acceptOffer(vehicleId, matches.get(0), index);
202+
return true;
203+
}
204+
List<Entry> pending = new ArrayList<>();
205+
for (OfferCandidate match : matches) {
206+
pending.add(new Entry(match.entry().name(), match.operatorName()));
207+
}
208+
PENDING.put(vehicleId, pending);
209+
ClientRouteGuidance.requestRouteChoice(vehicleId, pending);
210+
return true;
211+
}
212+
160213
private static void acceptOffer(int vehicleId, OfferCandidate candidate, int index) {
161214
RouteEntry entry = candidate.entry();
162215
INDICES.put(vehicleId, index);

src/main/java/com/immersiveautopilot/client/AutoRouteClientTracker.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public static void onClientTick(ClientTickEvent.Post event) {
3131
if (vehicleId != -1 && vehicleId != lastVehicleId) {
3232
AutoRouteClient.requestRoutes(vehicleId);
3333
}
34+
if (vehicleId == -1 && lastVehicleId != -1) {
35+
ClientRouteGuidance.onAirspaceExit(lastVehicleId);
36+
XaeroBridge.clearTemporaryWaypoints();
37+
}
3438
lastVehicleId = vehicleId;
3539
}
3640
}

src/main/java/com/immersiveautopilot/client/AutoRouteScreenOverlay.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.immersiveautopilot.ImmersiveAutopilot;
44
import immersive_aircraft.client.gui.VehicleScreen;
5+
import immersive_aircraft.entity.CargoAirshipEntity;
6+
import immersive_aircraft.entity.WarshipEntity;
57
import net.minecraft.client.gui.GuiGraphics;
68
import net.minecraft.client.gui.components.Button;
79
import net.minecraft.client.gui.components.EditBox;
@@ -37,6 +39,9 @@ public static void onScreenInit(ScreenEvent.Init.Post event) {
3739
if (!(event.getScreen() instanceof VehicleScreen screen)) {
3840
return;
3941
}
42+
if (!supportsAutoRoutes(screen)) {
43+
return;
44+
}
4045
Controller controller = new Controller(screen);
4146
controller.init(event);
4247
CONTROLLERS.put(screen, controller);
@@ -57,6 +62,19 @@ public static void onScreenClose(ScreenEvent.Closing event) {
5762
CONTROLLERS.remove(event.getScreen());
5863
}
5964

65+
private static boolean supportsAutoRoutes(VehicleScreen screen) {
66+
if (screen.getMenu() == null || screen.getMenu().getVehicle() == null) {
67+
return true;
68+
}
69+
if (screen.getMenu().getVehicle() instanceof CargoAirshipEntity) {
70+
return false;
71+
}
72+
if (screen.getMenu().getVehicle() instanceof WarshipEntity) {
73+
return false;
74+
}
75+
return true;
76+
}
77+
6078
private static final class Controller {
6179
private final VehicleScreen screen;
6280
private final List<EditBox> fields = new ArrayList<>();

src/main/java/com/immersiveautopilot/client/AutopilotKeyBindings.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ public final class AutopilotKeyBindings {
1212
GLFW.GLFW_KEY_COMMA,
1313
CATEGORY
1414
);
15+
public static final KeyMapping CYCLE_ROUTE = new KeyMapping(
16+
"key.immersive_autopilot.route_cycle",
17+
InputConstants.Type.KEYSYM,
18+
GLFW.GLFW_KEY_APOSTROPHE,
19+
CATEGORY
20+
);
1521

1622
private AutopilotKeyBindings() {
1723
}

src/main/java/com/immersiveautopilot/client/AutopilotStatusOverlay.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ public final class AutopilotStatusOverlay {
2222
private static final int COLOR_WARN = 0xFFFFA142;
2323
private static final int COLOR_ALERT = 0xFFFF4040;
2424
private static final double TERRAIN_CHECK_DISTANCE = 16.0;
25+
private static final long UNSUPPORTED_DURATION_MS = 3000L;
26+
private static long unsupportedUntil = 0L;
2527

2628
private AutopilotStatusOverlay() {
2729
}
2830

31+
public static void showUnsupported() {
32+
unsupportedUntil = System.currentTimeMillis() + UNSUPPORTED_DURATION_MS;
33+
}
34+
2935
@SubscribeEvent
3036
public static void onRenderGui(RenderGuiEvent.Post event) {
3137
Minecraft mc = Minecraft.getInstance();
@@ -41,6 +47,11 @@ public static void onRenderGui(RenderGuiEvent.Post event) {
4147
int baseY = height - 68;
4248

4349
int line = 0;
50+
if (System.currentTimeMillis() < unsupportedUntil) {
51+
Component text = Component.translatable("hud.immersive_autopilot.autopilot_unsupported");
52+
drawCentered(graphics, mc, text, baseY + line * (mc.font.lineHeight + 2), COLOR_ALERT);
53+
line++;
54+
}
4455
if (AutopilotSupport.isAutopilotEnabled(vehicle)) {
4556
Component text;
4657
int color = COLOR_AUTOPILOT;

src/main/java/com/immersiveautopilot/client/AutopilotToggleHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public static void toggleAutopilot() {
2323
if (!AutopilotSupport.hasAutopilot(vehicle)) {
2424
return;
2525
}
26+
if (!AutopilotSupport.isAutopilotSupported(vehicle)) {
27+
AutopilotStatusOverlay.showUnsupported();
28+
return;
29+
}
2630
boolean next = !AutopilotSupport.isAutopilotEnabled(vehicle);
2731
AutopilotSupport.setAutopilotEnabled(vehicle, next);
2832
NetworkHandler.sendToServer(new C2SToggleAutopilot(vehicle.getId(), next));

src/main/java/com/immersiveautopilot/client/ClientEvents.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ public static void onRegisterScreens(RegisterMenuScreensEvent event) {
2424
@SubscribeEvent
2525
public static void onRegisterKeyMappings(RegisterKeyMappingsEvent event) {
2626
event.register(AutopilotKeyBindings.TOGGLE_AUTOPILOT);
27+
event.register(AutopilotKeyBindings.CYCLE_ROUTE);
2728
}
2829
}

src/main/java/com/immersiveautopilot/client/ClientRuntimeEvents.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.immersiveautopilot.client;
22

33
import com.immersiveautopilot.ImmersiveAutopilot;
4+
import immersive_aircraft.entity.VehicleEntity;
5+
import net.minecraft.client.Minecraft;
6+
import net.minecraft.world.entity.player.Player;
47
import net.neoforged.api.distmarker.Dist;
58
import net.neoforged.bus.api.SubscribeEvent;
69
import net.neoforged.fml.common.EventBusSubscriber;
@@ -17,5 +20,15 @@ public static void onClientTick(ClientTickEvent.Post event) {
1720
while (AutopilotKeyBindings.TOGGLE_AUTOPILOT.consumeClick()) {
1821
AutopilotToggleHandler.toggleAutopilot();
1922
}
23+
while (AutopilotKeyBindings.CYCLE_ROUTE.consumeClick()) {
24+
Minecraft mc = Minecraft.getInstance();
25+
Player player = mc.player;
26+
if (player == null) {
27+
continue;
28+
}
29+
if (player.getVehicle() instanceof VehicleEntity vehicle) {
30+
AutoRouteClient.cycleRoute(vehicle.getId());
31+
}
32+
}
2033
}
2134
}

0 commit comments

Comments
 (0)