123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- package me.km.networking;
- import java.util.Collection;
- import java.util.LinkedList;
- import java.util.TreeMap;
- import net.minecraft.client.Minecraft;
- import net.minecraft.client.gui.AbstractGui;
- import net.minecraft.potion.EffectInstance;
- import net.minecraft.util.text.TextFormatting;
- import net.minecraft.util.text.LanguageMap;
- import net.minecraftforge.api.distmarker.Dist;
- import net.minecraftforge.api.distmarker.OnlyIn;
- @OnlyIn(Dist.CLIENT)
- public class StatusDisplayGui extends AbstractGui
- {
- public final static StatusDisplayGui INSTANCE = new StatusDisplayGui(Minecraft.getInstance());
- private class LineData implements Comparable<LineData>
- {
- private int time;
- private final String text;
-
- public LineData(int time, String text)
- {
- this.time = time;
- int index = text.indexOf('#');
- if(index != -1)
- {
- this.text = text.substring(0, index) + LanguageMap.getInstance().translateKey(text.substring(index + 1));
- }
- else
- {
- this.text = text;
- }
- }
-
- public String getText()
- {
- return text;
- }
-
- public String getTimeText()
- {
- if(time == Integer.MAX_VALUE)
- {
- return "";
- }
- return (time / 20) + "s ";
- }
-
- public int getTimeWidth()
- {
- return time == Integer.MAX_VALUE ? 0 : mc.fontRenderer.getStringWidth(getTimeText());
- }
-
- public int getTextWidth()
- {
- return mc.fontRenderer.getStringWidth(getText());
- }
- @Override
- public int compareTo(LineData o)
- {
- return -Integer.compare(time, o.time);
- }
- }
-
- private final TreeMap<Integer, LineData> strings;
- private final Minecraft mc;
-
- public StatusDisplayGui(Minecraft mc)
- {
- this.mc = mc;
- this.strings = new TreeMap<>();
- }
-
- public void tick()
- {
- strings.entrySet().removeIf(e ->
- {
- if(e.getValue().time < Integer.MAX_VALUE)
- {
- e.getValue().time--;
- if(e.getValue().time < 0)
- {
- return true;
- }
- }
- return false;
- });
- }
-
- public void add(int i, int time, String s)
- {
- strings.put(i, new LineData(time, s));
- }
-
- public void remove(int i)
- {
- strings.remove(i);
- }
-
- public void clear()
- {
- strings.clear();
- }
- public void paint()
- {
- Collection<EffectInstance> collection = this.mc.player.getActivePotionEffects();
- if(strings.isEmpty() && collection.isEmpty())
- {
- return;
- }
- // getting data
- LinkedList<LineData> list = new LinkedList<>(strings.values());
- EffectInstance e;
- collection.forEach(effect ->
- {
- StringBuilder sb = new StringBuilder();
- if(effect.getPotion().isBeneficial())
- {
- sb.append(TextFormatting.GREEN);
- }
- else
- {
- sb.append(TextFormatting.RED);
- }
- sb.append(LanguageMap.getInstance().translateKey(effect.getEffectName()));
- list.add(new LineData(effect.getDuration(), sb.toString()));
- });
- // sorting by time
- list.sort(null);
-
- int lineHeight = mc.fontRenderer.FONT_HEIGHT;
- int height = 4 + lineHeight * (strings.size() + collection.size());
-
- int maxTextWidth = 0;
- int maxTimeWidth = 0;
-
- for(LineData data : list)
- {
- maxTextWidth = Math.max(data.getTextWidth(), maxTextWidth);
- maxTimeWidth = Math.max(data.getTimeWidth(), maxTimeWidth);
- }
-
- int screenWidth = mc.getMainWindow().getScaledWidth();
-
- int x = screenWidth - maxTextWidth - maxTimeWidth - 4;
- int y = 2;
-
- fill(x, 0, screenWidth, height, 1342177280);
-
- x += 2;
- String time;
- for(LineData data : list)
- {
- time = data.getTimeText();
- mc.fontRenderer.drawString(time + data.getText(), x + maxTimeWidth - mc.fontRenderer.getStringWidth(time), y + 1, 0xFFFFFF);
- y += lineHeight;
- }
- }
- }
|