diff --git a/build.gradle b/build.gradle index 89cfecd..1819740 100644 --- a/build.gradle +++ b/build.gradle @@ -33,6 +33,7 @@ jar { dependencies { compileOnly "com.github.Anuken.Arc:arc-core:$pMindustryVersion" compileOnly "com.github.Anuken.Mindustry:core:$pMindustryVersion" + implementation 'org.hjson:hjson:3.0.0' } task dexify(type: Jar) { diff --git a/src/UnitInfo/SVars.java b/src/UnitInfo/SVars.java new file mode 100644 index 0000000..e2fc8a1 --- /dev/null +++ b/src/UnitInfo/SVars.java @@ -0,0 +1,17 @@ +package UnitInfo; + +import UnitInfo.core.HudUi; +import UnitInfo.core.PlayerParser; +import UnitInfo.core.Setting; +import arc.Core; +import arc.files.Fi; +import arc.struct.Seq; + +public class SVars { + public static Fi modRoot = Core.settings.getDataDirectory().child("mods/UnitInfo"); + public static Seq playerInfos = new Seq<>(); + + public static Setting settingAdder = new Setting(); + public static HudUi hud = new HudUi(); + public static PlayerParser playerinfo = new PlayerParser(); +} diff --git a/src/UnitInfo/core/BarInfo.java b/src/UnitInfo/core/BarInfo.java index 10b51dc..43b0b6d 100644 --- a/src/UnitInfo/core/BarInfo.java +++ b/src/UnitInfo/core/BarInfo.java @@ -1,5 +1,6 @@ package UnitInfo.core; +import UnitInfo.SVars; import arc.Core; import arc.graphics.Color; import arc.math.Mathf; @@ -24,24 +25,24 @@ import static mindustry.Vars.content; import static mindustry.Vars.state; public class BarInfo { - Seq strings = new Seq<>(new String[]{"","","","","",""}); - Seq numbers = new Seq<>(new Float[]{0f,0f,0f,0f,0f,0f}); - Seq colors = new Seq<>(new Color[]{Color.clear,Color.clear,Color.clear,Color.clear,Color.clear,Color.clear}); + static Seq strings = new Seq<>(new String[]{"","","","","",""}); + static Seq numbers = new Seq<>(new Float[]{0f,0f,0f,0f,0f,0f}); + static Seq colors = new Seq<>(new Color[]{Color.clear,Color.clear,Color.clear,Color.clear,Color.clear,Color.clear}); - public Seq returnStrings(T target){ + public static Seq returnStrings(T target){ getInfo(target); return strings; } - public Seq returnColors(T target){ + public static Seq returnColors(T target){ getInfo(target); return colors; } - public Seq returnNumbers(T target){ + public static Seq returnNumbers(T target){ getInfo(target); return numbers; } - public void getInfo(T target){ + public static void getInfo(T target){ for(int i = 0; i < 6; i++) { //init strings.set(i, "[lightgray][]"); colors.set(i, Color.clear); @@ -133,10 +134,10 @@ public class BarInfo { if(target instanceof Turret.TurretBuild){ Turret turret = (Turret)((Turret.TurretBuild)target).block; if(turret.chargeTime > 0f) { - float value = Mathf.clamp(Main.hud.charge / turret.chargeTime) * 100f; + float value = Mathf.clamp(SVars.hud.charge / turret.chargeTime) * 100f; strings.set(3, Core.bundle.format("shar-stat.charge", Strings.fixed(value, (Math.abs((int)value - value) <= 0.001f ? 0 : Math.abs((int)(value * 10) - value * 10) <= 0.001f ? 1 : 2)))); - colors.set(3, Pal.surge.cpy().lerp(Pal.accent, Main.hud.charge / turret.chargeTime)); - numbers.set(3, Main.hud.charge / turret.chargeTime); + colors.set(3, Pal.surge.cpy().lerp(Pal.accent, SVars.hud.charge / turret.chargeTime)); + numbers.set(3, SVars.hud.charge / turret.chargeTime); } } else if(target instanceof Unit && ((Unit) target).type != null) { diff --git a/src/UnitInfo/core/HudUi.java b/src/UnitInfo/core/HudUi.java index 1289d6f..a7d680f 100644 --- a/src/UnitInfo/core/HudUi.java +++ b/src/UnitInfo/core/HudUi.java @@ -46,7 +46,6 @@ public class HudUi { float tileScrollPos; float itemScrollPos; - Element image; Color lastItemColor = Pal.items; Color lastAmmoColor = Pal.ammo; Teamc lockedTarget; @@ -191,7 +190,6 @@ public class HudUi { update(() -> { if(!(getTarget() instanceof ItemTurret.ItemTurretBuild) && !(getTarget() instanceof LiquidTurret.LiquidTurretBuild) && !(getTarget() instanceof PowerTurret.PowerTurretBuild)){ clearChildren(); - image = null; return; } if(getTarget() instanceof Turret.TurretBuild){ @@ -216,31 +214,27 @@ public class HudUi { imaget = new Image(entity.liquids.current().uiIcon).setScaling(Scaling.fit); } else if(getTarget() instanceof PowerTurret.PowerTurretBuild){ - imaget = new ReqImage(Icon.power.getRegion(), () -> ((PowerTurret.PowerTurretBuild) getTarget()).power.status * ((PowerTurret.PowerTurretBuild) getTarget()).power.graph.getLastScaledPowerIn() > 0f){{ - add(new Image(Icon.power.getRegion())); - add(new Element(){ - @Override - public void draw(){ - Building entity = getTarget(); - float max = entity.block.consumes.getPower().usage; - float v = entity.power.status * entity.power.graph.getLastScaledPowerIn(); + imaget = new Image(Icon.power.getRegion()){ + @Override + public void draw(){ + Building entity = getTarget(); + float max = entity.block.consumes.getPower().usage; + float v = entity.power.status * entity.power.graph.getLastScaledPowerIn(); - Lines.stroke(Scl.scl(2f), Pal.removeBack); - Draw.alpha(1 - v/max); - Lines.line(x, y - 2f + height, x + width, y - 2f); - Draw.color(Pal.remove); - Draw.alpha(1 - v/max); - Lines.line(x, y + height, x + width, y); - Draw.reset(); - } - }); - }}; + super.draw(); + Lines.stroke(Scl.scl(2f), Pal.removeBack); + Draw.alpha(1 - v/max); + Lines.line(x, y - 2f + height, x + width, y - 2f); + Draw.color(Pal.remove); + Draw.alpha(1 - v/max); + Lines.line(x, y + height, x + width, y); + Draw.reset(); + } + }; } - if(image != null && (imaget.getClass() != image.getClass() || imaget.getClass() == Image.class)) - clearChildren(); + clearChildren(); add(imaget).size(iconSmall).padBottom(2 * 8f).padRight(3 * 8f); - image = imaget; } }); pack(); diff --git a/src/UnitInfo/core/Main.java b/src/UnitInfo/core/Main.java index 823bec1..70c3660 100644 --- a/src/UnitInfo/core/Main.java +++ b/src/UnitInfo/core/Main.java @@ -3,6 +3,7 @@ package UnitInfo.core; import UnitInfo.ui.FreeBar; import arc.Core; import arc.Events; +import arc.files.Fi; import arc.graphics.Color; import arc.graphics.g2d.Draw; import arc.graphics.g2d.Fill; @@ -30,12 +31,11 @@ import mindustry.world.Block; import mindustry.world.blocks.defense.turrets.BaseTurret; import mindustry.world.blocks.defense.turrets.Turret; +import static UnitInfo.SVars.*; import static arc.Core.*; import static mindustry.Vars.*; public class Main extends Mod { - public static Setting settingAdder = new Setting(); - public static HudUi hud = new HudUi(); @Override public void init(){ @@ -48,6 +48,8 @@ public class Main extends Mod { hud.addTileTable(); hud.addTable(); hud.setEvent(); + playerinfo.createFile(); + playerinfo.setEvent(); }); Events.on(WorldLoadEvent.class, e -> { @@ -62,8 +64,6 @@ public class Main extends Mod { hud.addWaveTable(); }); - - Events.run(Trigger.draw, () -> { if(settings.getBool("blockstatus")) Groups.build.each(build -> { if(Vars.player != null && Vars.player.team() == build.team) return; diff --git a/src/UnitInfo/core/PlayerParser.java b/src/UnitInfo/core/PlayerParser.java new file mode 100644 index 0000000..9bb85a5 --- /dev/null +++ b/src/UnitInfo/core/PlayerParser.java @@ -0,0 +1,66 @@ +package UnitInfo.core; + +import UnitInfo.SVars; +import arc.Events; +import arc.struct.Seq; +import mindustry.Vars; +import mindustry.game.EventType; +import mindustry.gen.Groups; +import mindustry.gen.Player; +import org.hjson.JsonArray; +import org.hjson.JsonObject; +import org.hjson.Stringify; + +import static UnitInfo.SVars.modRoot; + +public class PlayerParser{ + + public void setEvent() { + Events.on(EventType.PlayerJoin.class, e -> { + writeJson(e.player); + }); + Events.on(EventType.ServerLoadEvent.class, e->{ + Groups.player.each(this::writeJson); + }); + Events.on(EventType.WorldLoadEvent.class, e->{ + if(Vars.net.active()) Groups.player.each(this::writeJson); + }); + Events.run(EventType.Trigger.update, ()->{ + if(Vars.net.active()) + Groups.player.each(this::writeJson); + }); + } + + public void createFile() { + if(!modRoot.child("players.hjson").exists()) save(); + } + + public void writeJson(Player player1){ + PlayerInfo info = SVars.playerInfos.find(pi -> pi.player == player1); + if(info != null){ + if(!info.names.contains(player1.name)) info.names.add(player1.name); + } + else{ + SVars.playerInfos.add(new PlayerInfo(){{ + player = player1; + names.add(player1.name); + }}); + } + save(); + } + + public void save() { + JsonObject data = new JsonObject(); + SVars.playerInfos.each(pi->{ + JsonArray arr = new JsonArray(); + pi.names.each(arr::add); + data.add("names", arr); + }); + + modRoot.child("players.hjson").writeString(data.toString(Stringify.HJSON)); + } + public static class PlayerInfo{ + Player player; + Seq names = new Seq<>(); + } +} diff --git a/src/UnitInfo/core/Setting.java b/src/UnitInfo/core/Setting.java index 6782fb0..6ed4fc4 100644 --- a/src/UnitInfo/core/Setting.java +++ b/src/UnitInfo/core/Setting.java @@ -9,7 +9,6 @@ import arc.struct.Seq; import arc.util.*; import mindustry.*; import mindustry.core.Version; -import mindustry.game.Team; import mindustry.gen.*; import mindustry.ui.Styles; import mindustry.ui.dialogs.*;