Skip to content

Commit

Permalink
fix crash & the rest of the positioning issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyfts committed Oct 28, 2024
1 parent 0f38225 commit 20b5bac
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 66 deletions.
19 changes: 19 additions & 0 deletions src/main/java/com/github/lunatrius/ingameinfo/client/gui/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public abstract class Info {
public int offsetX;
public int offsetY;
private String identifier = "";
private String iconSpacing = "";
public boolean hasPosition = false;
private int oldWidth;

protected Info(int x, int y) {
this.x = x;
Expand Down Expand Up @@ -46,6 +49,14 @@ public int getY() {
return this.y + this.offsetY;
}

public String getIconSpacing() {
if (oldWidth != getWidth()) {
setIconSpacing();
oldWidth = getWidth();
}
return iconSpacing;
}

public int getWidth() {
return 0;
}
Expand All @@ -64,6 +75,14 @@ public String getIdentifier() {
return this.identifier;
}

private void setIconSpacing() {
String str = "";
for (int i = 0; i < getWidth() && fontRenderer.getStringWidth(str) < getWidth(); i++) {
str += " ";
}
iconSpacing = str;
}

@Override
public String toString() {
return "Info";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ public class InfoText extends Info {
private final List<Value> values;
private final Alignment alignment;
private final int index;
private int scaledWidth, scaledHeight;
private boolean updatePos = true;

public InfoText(int index, Alignment alignment, List<Value> values) {
super(0, 0);
Expand All @@ -35,15 +33,6 @@ public InfoText(int index, Alignment alignment, List<Value> values) {
}

public void update() {
int newHeight = InGameInfoCore.INSTANCE.scaledHeight;
int newWidth = InGameInfoCore.INSTANCE.scaledWidth;
if (newHeight != scaledHeight || newWidth != scaledWidth) {
scaledHeight = newHeight;
scaledWidth = newWidth;
updatePos = true;
attachedValues.clear();
}

StringBuilder builder = new StringBuilder();
for (Value value : this.values) {
builder.append(getValue(value));
Expand Down Expand Up @@ -76,32 +65,21 @@ private void updateChildren(StringBuilder builder) {
}

for (Info child : attachedValues.values()) {
int index = builder.indexOf(ICON_START);
child.x = fontRenderer.getStringWidth(builder.substring(0, index));
int end = builder.indexOf("}", index) + 1;
builder.replace(index, end + 1, "");
updatePos = true;
if (child.hasPosition) continue;
int iconStart = builder.indexOf(ICON_START);
int widthStart = builder.indexOf("|", iconStart) + 1;
child.hasPosition = true;
child.x = fontRenderer.getStringWidth(builder.substring(0, iconStart));
builder.replace(iconStart, widthStart, "");
builder.deleteCharAt(builder.indexOf("}"));
}
}

private void updatePosition() {
if (!updatePos) return;
updatePos = false;
int scaledWidth = InGameInfoCore.INSTANCE.scaledWidth;
int scaledHeight = InGameInfoCore.INSTANCE.scaledHeight;
x = alignment.getX(scaledWidth, fontRenderer.getStringWidth(text));
y = alignment.getY(scaledHeight, getHeight());

for (Info child : attachedValues.values()) {
if (child.x == 0) {
offsetX = child.getWidth();
}

int actualX = child.x + x + child.getWidth();
if (actualX > scaledWidth) {
int diff = actualX + 1 - scaledWidth;
child.x = child.x - diff;
offsetX = -diff;
}
}
}

public @Nullable Info getAttachedValue(String tag) {
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/com/github/lunatrius/ingameinfo/tag/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ public static void releaseResources() {
}

public static String getIconTag(Info info) {
String str = "";
for (int i = 0; i < info.getWidth() && minecraft.fontRenderer.getStringWidth(str) < info.getWidth(); i++) {
str += " ";
}
return String.format("{ICON|%s}", str);
return String.format("{ICON|%s}", info.getIconSpacing());
}
}
38 changes: 15 additions & 23 deletions src/main/java/com/github/lunatrius/ingameinfo/tag/TagMisc.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import net.minecraft.client.gui.GuiPlayerInfo;
import net.minecraft.client.resources.ResourcePackRepository;
import net.minecraft.util.EnumChatFormatting;

import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -174,31 +173,24 @@ public String getValue() {

public static class PingIcon extends TagMisc {

private boolean needsUpdate(InfoText caller, int ping) {
Info value = caller.getAttachedValue(getName());
if (value == null) return true;
return Integer.parseInt(value.getIdentifier()) != ping;
}

@Override
public @NotNull String getValue(@NotNull InfoText caller) {
List<GuiPlayerInfo> list = player.sendQueue.playerInfoList;
for (GuiPlayerInfo playerInfo : list) {
if (player.getGameProfile().getName()
.equals(EnumChatFormatting.getTextWithoutFormattingCodes(playerInfo.name))) {
int pingIndex = getPingIndex(playerInfo);
if (needsUpdate(caller, playerInfo.responseTime)) {
InfoIcon icon = new InfoIcon("textures/gui/icons.png");
icon.setIdentifier(String.valueOf(playerInfo.responseTime));
icon.setDisplayDimensions(0, 0, 10, 8);
icon.setTextureData(0, 176 + pingIndex * 8, 10, 8, 256, 256);
caller.attachValue(getName(), icon);
return getIconTag(icon);
}
return "";
}
GuiPlayerInfo playerInfo = (GuiPlayerInfo) player.sendQueue.playerInfoMap
.get(player.getCommandSenderName());
if (playerInfo == null) return "-1";
int pingIndex = getPingIndex(playerInfo);
Info value = caller.getAttachedValue(getName());
if (value == null) {
InfoIcon icon = new InfoIcon("textures/gui/icons.png");
icon.setIdentifier(String.valueOf(playerInfo.responseTime));
icon.setDisplayDimensions(0, 0, 10, 8);
icon.setTextureData(0, 176 + pingIndex * 8, 10, 8, 256, 256);
caller.attachValue(getName(), icon);
return getIconTag(icon);
} else {
((InfoIcon) value).setTextureData(0, 176 + pingIndex * 8, 10, 8, 256, 256);
return value.getIconSpacing();
}
return "-1";
}

private static int getPingIndex(GuiPlayerInfo playerInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public Icon(int slot, boolean large) {

if (value != null) {
value.setValue(itemStack);
return "";
return value.getIconSpacing();
}

InfoItem item = new InfoItem(itemStack, this.large);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,11 @@ public Icon(int index, boolean large) {
@Override
public @NotNull String getValue(@NotNull InfoText parent) {
updatePotionEffects();
Info value = parent.getAttachedValue(getName());
if (potionEffects.length > this.index) {
Info value = parent.getAttachedValue(getName());
Potion potion = Potion.potionTypes[potionEffects[this.index].getPotionID()];
if (potion.hasStatusIcon() && shouldUpdate(value, potion.id)) {
if (!potion.hasStatusIcon()) return "";
if (shouldUpdate(value, potion.id)) {
InfoIcon icon;
if (value == null) {
icon = new InfoIcon("textures/gui/container/inventory.png");
Expand All @@ -179,11 +180,13 @@ public Icon(int index, boolean large) {
icon.setTextureData((i % 8) * 18, 198 + (i / 8) * 18, 18, 18, 256, 256);

if (value != null) {
return "";
return value.getIconSpacing();
}

parent.attachValue(getName(), icon);
return getIconTag(icon);
} else {
return value.getIconSpacing();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public String getValue() {

Info value = parent.getAttachedValue(getName());
if (value != null && value.getIdentifier().equals(what)) {
return "";
return value.getIconSpacing();
}

if ((size == 1 || size == 2) && !what.endsWith(".png")) {
Expand Down Expand Up @@ -294,7 +294,7 @@ public String getValue() {
} else {
value.setIdentifier(what);
value.setValue(itemStack);
return "";
return value.getIconSpacing();
}
}

Expand Down Expand Up @@ -331,7 +331,7 @@ public String getValue() {
return Tag.getIconTag(icon);
} else {
value.setValue(what);
return "";
return value.getIconSpacing();
}
} catch (Exception e) {
return "?";
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/igi_at.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# InGameInfoXML
public net.minecraft.client.multiplayer.PlayerControllerMP field_78779_k # currentGameType
public net.minecraft.util.FoodStats field_75126_c # foodExhaustionLevel
public net.minecraft.client.network.NetHandlerPlayClient field_147310_i # playerInfoMap

0 comments on commit 20b5bac

Please sign in to comment.