From d3a78a9d423b6044b782e35d854592e846f8e1a1 Mon Sep 17 00:00:00 2001 From: Cubical box <67639725+BlueTheCube@users.noreply.github.com> Date: Tue, 2 Apr 2024 22:03:14 +0800 Subject: [PATCH] Fetching unit by type, build without type, and fixing a nullpointer crash (#9703) * Pluh * Pluh * Update LExecutor.java * fixed the null * Update LExecutor.java * I will point your null exception * is it null or nah * it is nah * Update LExecutor.java * Update LExecutor.java * null zero --- core/src/mindustry/logic/LExecutor.java | 14 +++++++++++--- core/src/mindustry/logic/LStatements.java | 8 +++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/core/src/mindustry/logic/LExecutor.java b/core/src/mindustry/logic/LExecutor.java index ba9b194364..91cb4575e2 100644 --- a/core/src/mindustry/logic/LExecutor.java +++ b/core/src/mindustry/logic/LExecutor.java @@ -1363,13 +1363,21 @@ public class LExecutor{ TeamData data = t.data(); switch(type){ - case unit -> exec.setobj(result, i < 0 || i >= data.units.size ? null : data.units.get(i)); + case unit -> { + UnitType type = exec.obj(extra) instanceof UnitType u ? u : null; + if(type == null){ + exec.setobj(result, i < 0 || i >= data.units.size ? null : data.units.get(i)); + }else{ + var units = data.unitCache(type); + exec.setobj(result, units == null || i < 0 || i >= units.size ? null : units.get(i)); + } + } case player -> exec.setobj(result, i < 0 || i >= data.players.size || data.players.get(i).unit().isNull() ? null : data.players.get(i).unit()); case core -> exec.setobj(result, i < 0 || i >= data.cores.size ? null : data.cores.get(i)); case build -> { Block block = exec.obj(extra) instanceof Block b ? b : null; if(block == null){ - exec.setobj(result, null); + exec.setobj(result, i < 0 || i >= data.buildings.size ? null : data.buildings.get(i)); }else{ var builds = data.getBuildings(block); exec.setobj(result, i < 0 || i >= builds.size ? null : builds.get(i)); @@ -1380,7 +1388,7 @@ public class LExecutor{ if(type == null){ exec.setnum(result, data.units.size); }else{ - exec.setnum(result, data.unitsByType[type.id].size); + exec.setnum(result, data.unitCache(type) == null ? 0 : data.unitCache(type).size); } } case coreCount -> exec.setnum(result, data.cores.size); diff --git a/core/src/mindustry/logic/LStatements.java b/core/src/mindustry/logic/LStatements.java index 29dde175b5..31cce56e0c 100644 --- a/core/src/mindustry/logic/LStatements.java +++ b/core/src/mindustry/logic/LStatements.java @@ -1856,11 +1856,17 @@ public class LStatements{ fields(table, index, i -> index = i); } - if(type == FetchType.buildCount || type == FetchType.build || type == FetchType.unitCount){ + if(type == FetchType.buildCount || type == FetchType.build){ row(table); fields(table, "block", extra, i -> extra = i); } + + if(type == FetchType.unitCount || type == FetchType.unit){ + row(table); + + fields(table, "unit", extra, i -> extra = i); + } } @Override