Skip to content

Commit

Permalink
fix: every possible NPE #102
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharlottes committed Dec 6, 2023
1 parent 42a76ab commit f260624
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 90 deletions.
17 changes: 17 additions & 0 deletions src/informatis/SVars.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package informatis;

import arc.Events;
import informatis.shaders.*;
import arc.graphics.g2d.TextureRegion;
import mindustry.entities.abilities.ShieldRegenFieldAbility;
import mindustry.game.EventType;
import mindustry.type.UnitType;

import static arc.Core.atlas;
import static mindustry.Vars.content;

public class SVars {
public static final TextureRegion
Expand All @@ -15,4 +20,16 @@ public class SVars {
public static void init() {
pathfinder = new informatis.core.Pathfinder();
}

public static float maxShieldAmongUnits = 0;

static {
Events.on(EventType.ClientLoadEvent.class, e -> {
for(UnitType unitType : content.units()) {
ShieldRegenFieldAbility ability = (ShieldRegenFieldAbility) unitType.abilities.find(abil -> abil instanceof ShieldRegenFieldAbility);
if(ability == null) continue;
if(ability.max > maxShieldAmongUnits) maxShieldAmongUnits = ability.max;
}
});
}
}
16 changes: 11 additions & 5 deletions src/informatis/ui/components/FreeBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import mindustry.type.*;
import mindustry.world.blocks.payloads.Payload;

import informatis.SVars;

import static arc.Core.*;
import static mindustry.Vars.*;

Expand Down Expand Up @@ -93,11 +95,15 @@ public static void draw(Unit unit){
x - width / 2, y - height));
}

{
float max1 = ((ShieldRegenFieldAbility)content.units().copy().filter(ut -> ut.abilities.find(abil -> abil instanceof ShieldRegenFieldAbility) != null).sort(ut -> ((ShieldRegenFieldAbility)ut.abilities.find(abil -> abil instanceof ShieldRegenFieldAbility)).max).peek().abilities.find(abil -> abil instanceof ShieldRegenFieldAbility)).max;
float max2 = 0f;
if(unit.type.abilities.find(abil -> abil instanceof ForceFieldAbility) != null) max2 = ((ForceFieldAbility) unit.type.abilities.find(abil -> abil instanceof ForceFieldAbility)).max;
float max = Mathf.clamp(unit.shield / Math.max(max1, max2));
float maxShield = -1;
for(Ability ability : unit.abilities) {
if(ability instanceof ForceFieldAbility forceFieldAbility) {
maxShield = forceFieldAbility.max;
}
}
if(maxShield != -1) {

float max = Mathf.clamp(unit.shield / Math.max(SVars.maxShieldAmongUnits, maxShield));

float topWidth = - width / 2 + width * max;
float moser = topWidth + height;
Expand Down
4 changes: 3 additions & 1 deletion src/informatis/ui/fragments/QuickSchemFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import mindustry.game.*;
import mindustry.gen.*;
import mindustry.graphics.Pal;
import mindustry.mod.Mods;
import mindustry.ui.Styles;
import mindustry.ui.dialogs.*;

Expand Down Expand Up @@ -53,7 +54,8 @@ public QuickSchemFragment() {

public Table rebuildBody() {
// scheme-size crashed the mod
if(mods.getMod("scheme-size") == null || mods.getMod("scheme-size").enabled()) return new Table();
Mods.LoadedMod schemSizeMod = mods.getMod("scheme-size");
if(schemSizeMod == null || schemSizeMod.enabled()) return new Table();

visible = settings.getBool(("schem"));
if(!visible) return new Table();
Expand Down
23 changes: 14 additions & 9 deletions src/informatis/ui/fragments/sidebar/windows/CoreWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,12 @@ public Table setTable(Team team){
int row = 0;

CoreBlock.CoreBuild core = team.core();
if(core == null || core.items == null) {
return;
}
for(int i = 0; i < Vars.content.items().size; i++){
Item item = Vars.content.item(i);
if(!team.items().has(item)) return;
if(!team.items().has(item)) continue;
itemTable.stack(
new Table(ttt -> {
ttt.image(item.uiIcon).size(iconSmall).tooltip(tttt -> tttt.background(Styles.black6).add(item.localizedName).style(Styles.outlineLabel).margin(2f));
Expand Down Expand Up @@ -189,16 +192,18 @@ public void resetItems(){

public void updateItems(Team team){
CoreBlock.CoreBuild core = team.core();
if (core != null) {
Seq<ItemStack> stack = updateItems;
if(stack.isEmpty()) Vars.content.items().each(i -> stack.add(new ItemStack(i, 0)));
for (Item item : Vars.content.items()) {
stack.get(item.id).set(item, core.items.get(item) - (prevItems != null ? prevItems.get(item.id).amount : 0));
if (prevItems != null) prevItems.get(item.id).set(item, core.items.get(item));
}
if (core == null || core.items == null) return;

Seq<ItemStack> stack = updateItems;
if(stack.isEmpty()) {
Vars.content.items().each(i -> stack.add(new ItemStack(i, 0)));
}

if(prevItems != null) prevItems.clear().addAll(Vars.content.items().map(i -> new ItemStack(i, core.items.get(i))));
for (Item item : Vars.content.items()) {
stack.get(item.id).set(item, core.items.get(item) - prevItems.get(item.id).amount);
prevItems.get(item.id).set(item, core.items.get(item));
}
prevItems.clear().addAll(Vars.content.items().map(i -> new ItemStack(i, core.items.get(i))));
}
}
}
28 changes: 15 additions & 13 deletions src/informatis/ui/fragments/sidebar/windows/MapEditorWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,9 @@ public void drawBlocks(int x, int y, boolean square, Boolf<Tile> tester){
if(drawBlock.isMultiblock()){
x = Mathf.clamp(x, (drawBlock.size - 1) / 2, world.width() - drawBlock.size / 2 - 1);
y = Mathf.clamp(y, (drawBlock.size - 1) / 2, world.height() - drawBlock.size / 2 - 1);
if(!hasOverlap(x, y)){
world.tile(x, y).setBlock(drawBlock, drawTeam, rotation);
Tile tile = world.tile(x, y);
if(tile != null && !hasOverlap(tile)){
tile.setBlock(drawBlock, drawTeam, rotation);
}
}else{
boolean isFloor = drawBlock.isFloor() && drawBlock != Blocks.air;
Expand Down Expand Up @@ -439,21 +440,20 @@ public void drawSquare(int x, int y, Cons<Tile> drawer){
}
}

boolean hasOverlap(int x, int y){
Tile tile = world.tile(x, y);
boolean hasOverlap(Tile tile){
//allow direct replacement of blocks of the same size
if(tile != null && tile.isCenter() && tile.block() != drawBlock && tile.block().size == drawBlock.size && tile.x == x && tile.y == y){
if(tile.isCenter() && tile.block() != drawBlock && tile.block().size == drawBlock.size && tile.x == x && tile.y == y){
return false;
}

//else, check for overlap
int offsetx = -(drawBlock.size - 1) / 2;
int offsety = -(drawBlock.size - 1) / 2;
int offsetX = -(drawBlock.size - 1) / 2;
int offsetY = -(drawBlock.size - 1) / 2;
for(int dx = 0; dx < drawBlock.size; dx++){
for(int dy = 0; dy < drawBlock.size; dy++){
int worldx = dx + offsetx + x;
int worldy = dy + offsety + y;
Tile other = world.tile(worldx, worldy);
int worldX = dx + offsetX + tile.x;
int worldY = dy + offsetY + tile.y;
Tile other = world.tile(worldX, worldY);

if(other != null && other.block().isMultiblock()){
return true;
Expand All @@ -468,8 +468,9 @@ enum EditorTool{
pick(KeyCode.i){
public void touched(int x, int y){
if(!Structs.inBounds(x, y, world.width(), world.height())) return;

Tile tile = world.tile(x, y);
if(tile == null) return;

drawBlock = tile.block() == Blocks.air || !tile.block().inEditor ? tile.overlay() == Blocks.air ? tile.floor() : tile.overlay() : tile.block();
}
},
Expand Down Expand Up @@ -549,8 +550,9 @@ public void touched(int x, int y){

@Override
public void touched(int x, int y){
if(!Structs.inBounds(x, y, world.width(), world.height()) || world.tile(x, y).block()!=null&&world.tile(x, y).block().isMultiblock()) return;
if(!Structs.inBounds(x, y, world.width(), world.height())) return;
Tile tile = world.tile(x, y);
if(tile == null) return;

//mode 0 or 1, fill everything with the floor/tile or replace it
if(mode == 0 || mode == -1){
Expand Down Expand Up @@ -579,7 +581,7 @@ public void touched(int x, int y){

//replace only when the mode is 0 using the specified functions
fill(x, y, mode == 0, tester, setter);
}else if(mode == 1){ //mode 1 is team fill
} else if(mode == 1){ //mode 1 is team fill
//only fill synthetic blocks, it's meaningless otherwise
if(tile.synthetic()){
Team dest = tile.team();
Expand Down
Loading

0 comments on commit f260624

Please sign in to comment.