Skip to content

Commit 23ab1d9

Browse files
authored
Merge branch 'AE2-UEL:master' into master
2 parents 5abea3b + 7f2318f commit 23ab1d9

File tree

20 files changed

+586
-105
lines changed

20 files changed

+586
-105
lines changed

src/main/java/appeng/block/AEBaseItemBlockChargeable.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@
2121

2222
import appeng.api.config.AccessRestriction;
2323
import appeng.api.config.Actionable;
24-
import appeng.api.config.PowerUnits;
2524
import appeng.api.definitions.IBlockDefinition;
2625
import appeng.api.implementations.items.IAEItemPowerStorage;
2726
import appeng.core.Api;
28-
import appeng.core.localization.GuiText;
27+
import appeng.core.localization.Tooltips;
2928
import appeng.util.Platform;
3029
import net.minecraft.block.Block;
3130
import net.minecraft.client.util.ITooltipFlag;
@@ -35,7 +34,6 @@
3534
import net.minecraftforge.fml.relauncher.Side;
3635
import net.minecraftforge.fml.relauncher.SideOnly;
3736

38-
import java.text.MessageFormat;
3937
import java.util.List;
4038

4139

@@ -57,10 +55,7 @@ public void addCheckedInformation(final ItemStack stack, final World world, fina
5755
internalCurrentPower = tag.getDouble("internalCurrentPower");
5856
}
5957

60-
final double percent = internalCurrentPower / internalMaxPower;
61-
62-
lines.add(GuiText.StoredEnergy.getLocal() + ':' + MessageFormat.format(" {0,number,#} ", internalCurrentPower) + Platform
63-
.gui_localize(PowerUnits.AE.unlocalizedName) + " - " + MessageFormat.format(" {0,number,#.##%} ", percent));
58+
lines.add(Tooltips.energyStorageComponent(internalCurrentPower, internalMaxPower).getFormattedText());
6459
}
6560
}
6661

src/main/java/appeng/core/api/ApiClientHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import appeng.api.util.IClientHelper;
1414
import appeng.core.AEConfig;
1515
import appeng.core.localization.GuiText;
16+
import appeng.core.localization.Tooltips;
1617
import appeng.fluids.items.FluidDummyItem;
1718
import appeng.fluids.util.AEFluidStack;
1819
import appeng.util.ReadableNumberConverter;
@@ -44,10 +45,9 @@ public <T extends IAEStack<T>> void addCellInformation(ICellInventoryHandler<T>
4445
final ICellInventory<?> cellInventory = handler.getCellInv();
4546

4647
if (cellInventory != null) {
47-
lines.add(cellInventory.getUsedBytes() + " " + GuiText.Of.getLocal() + ' ' + cellInventory.getTotalBytes() + ' ' + GuiText.BytesUsed.getLocal());
48+
lines.add(Tooltips.bytesUsed(cellInventory.getUsedBytes(),cellInventory.getTotalBytes()).getFormattedText());
4849

49-
lines.add(cellInventory.getStoredItemTypes() + " " + GuiText.Of.getLocal() + ' ' + cellInventory.getTotalItemTypes() + ' ' + GuiText.Types
50-
.getLocal());
50+
lines.add(Tooltips.typesUsed(cellInventory.getStoredItemTypes(),cellInventory.getTotalItemTypes()).getFormattedText());
5151
}
5252

5353
IItemList<?> itemList = cellInventory.getChannel().createList();

src/main/java/appeng/core/localization/GuiText.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
package appeng.core.localization;
2020

2121

22+
import net.minecraft.util.text.ITextComponent;
23+
import net.minecraft.util.text.TextComponentTranslation;
2224
import net.minecraft.util.text.translation.I18n;
2325

2426

@@ -208,7 +210,11 @@ public enum GuiText {
208210

209211
// Used in Crafting Toasts
210212
CraftingToastDone,
211-
CraftingToastCancelled;
213+
CraftingToastCancelled,
214+
215+
CanBeEnchanted,
216+
IncreasedEnergyUseFromEnchants,
217+
Deprecated;
212218

213219
private final String root;
214220

@@ -224,6 +230,10 @@ public String getLocal() {
224230
return I18n.translateToLocal(this.getUnlocalized());
225231
}
226232

233+
public ITextComponent getLocalizedWithArgs(Object... args) {
234+
return new TextComponentTranslation(this.getUnlocalized(), args);
235+
}
236+
227237
public String getUnlocalized() {
228238
return this.root + '.' + this;
229239
}
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
package appeng.core.localization;
2+
3+
import appeng.api.config.PowerUnits;
4+
import com.github.bsideup.jabel.Desugar;
5+
import net.minecraft.util.text.*;
6+
7+
import java.text.DecimalFormat;
8+
import java.text.MessageFormat;
9+
import java.util.Arrays;
10+
11+
/**
12+
* Static utilities for constructing tooltips in various places.
13+
*/
14+
public final class Tooltips {
15+
16+
private static final char SEP;
17+
static {
18+
var format = (DecimalFormat) DecimalFormat.getInstance();
19+
var symbols = format.getDecimalFormatSymbols();
20+
SEP = symbols.getDecimalSeparator();
21+
}
22+
23+
public static final Style UNIT_TEXT = new Style().setColor(TextFormatting.YELLOW).setItalic(false);
24+
public static final Style NORMAL_TOOLTIP_TEXT = new Style().setColor(TextFormatting.GRAY).setItalic(false);
25+
public static final Style NUMBER_TEXT = new Style().setColor(TextFormatting.LIGHT_PURPLE).setItalic(false);
26+
27+
28+
public static final String[] units = new String[] { "k", "M", "G", "T", "P", "E" };
29+
public static final long[] DECIMAL_NUMS = new long[] { 1000L, 1000_000L, 1000_000_000L, 1000_000_000_000L,
30+
1000_000_000_000_000L,
31+
1000_000_000_000_000_000L };
32+
33+
public static ITextComponent of(ITextComponent... components) {
34+
ITextComponent s = new TextComponentString("");
35+
for (var c : components) {
36+
s = s.appendSibling(c);
37+
}
38+
return s;
39+
}
40+
41+
public static ITextComponent of(String s) {
42+
return new TextComponentString(s);
43+
}
44+
45+
public static ITextComponent ofPercent(double percent) {
46+
return ofPercent(percent,true);
47+
}
48+
49+
public static ITextComponent ofPercent(double percent, boolean oneIsGreen) {
50+
return new TextComponentString(MessageFormat.format("{0,number,#.##%}", percent))
51+
.setStyle(colorFromRatio(percent, oneIsGreen));
52+
}
53+
54+
public static ITextComponent of(PowerUnits pU) {
55+
return new TextComponentTranslation(pU.unlocalizedName).setStyle(UNIT_TEXT);
56+
}
57+
58+
public static ITextComponent ofNumber(double number, double max) {
59+
MaxedAmount amount = getMaxedAmount(number, max);
60+
return ofNumber(amount);
61+
}
62+
63+
public static ITextComponent of(GuiText guiText, Object... args) {
64+
return of(guiText, NORMAL_TOOLTIP_TEXT, args);
65+
}
66+
67+
public static ITextComponent of(GuiText guiText, Style style, Object... args) {
68+
69+
if (args.length > 0 && args[0] instanceof Integer) {
70+
return guiText.getLocalizedWithArgs(Arrays.stream(args).map((o) -> ofUnformattedNumber((Integer) o)).toArray()).createCopy()
71+
.setStyle(style);
72+
} else if (args.length > 0 && args[0] instanceof Long) {
73+
return guiText.getLocalizedWithArgs(Arrays.stream(args).map((o) -> ofUnformattedNumber((Long) o)).toArray()).createCopy()
74+
.setStyle(style);
75+
}
76+
return guiText.getLocalizedWithArgs(args).createCopy().setStyle(style);
77+
78+
}
79+
80+
public static ITextComponent ofUnformattedNumber(long number) {
81+
return new TextComponentString(String.valueOf(number)).setStyle(NUMBER_TEXT);
82+
}
83+
84+
public static ITextComponent ofUnformattedNumberWithRatioColor(long number, double ratio, boolean oneIsGreen) {
85+
return new TextComponentString(String.valueOf(number)).setStyle(colorFromRatio(ratio, oneIsGreen));
86+
}
87+
88+
private static ITextComponent ofNumber(MaxedAmount number) {
89+
boolean numberUnit = !number.digit().equals("0");
90+
return new TextComponentString(number.digit() + (numberUnit ? number.unit() : "")).setStyle(NUMBER_TEXT)
91+
.appendSibling(new TextComponentString("/")
92+
.setStyle(NORMAL_TOOLTIP_TEXT))
93+
.appendText(number.maxDigit() + number.unit()).setStyle(NUMBER_TEXT);
94+
}
95+
@Desugar
96+
public record MaxedAmount(String digit, String maxDigit, String unit) {
97+
}
98+
99+
@Desugar
100+
public record Amount(String digit, String unit) {
101+
}
102+
103+
public static Style colorFromRatio(double ratio, boolean oneIsGreen) {
104+
double p = ratio;
105+
106+
if (!oneIsGreen) {
107+
p = 1 - p;
108+
}
109+
110+
TextFormatting colorCode = getColorCode(p);
111+
112+
return new Style().setItalic(false).setColor(colorCode);
113+
}
114+
115+
public static String getAmount(double amount, long num) {
116+
double fract = amount / num;
117+
String returned;
118+
if (fract < 10) {
119+
returned = String.format("%.3f", fract);
120+
} else if (fract < 100) {
121+
returned = String.format("%.2f", fract);
122+
} else {
123+
returned = String.format("%.1f", fract);
124+
}
125+
while (returned.endsWith("0")) {
126+
returned = returned.substring(0, returned.length() - 1);
127+
}
128+
if (returned.endsWith(String.valueOf(SEP))) {
129+
returned = returned.substring(0, returned.length() - 1);
130+
}
131+
return returned;
132+
133+
}
134+
135+
public static Amount getAmount(double amount) {
136+
if (amount < 10000) {
137+
return new Amount(getAmount(amount, 1), "");
138+
} else {
139+
int i = 0;
140+
while (amount / DECIMAL_NUMS[i] >= 1000) {
141+
i++;
142+
}
143+
return new Amount(getAmount(amount, DECIMAL_NUMS[i]), units[i]);
144+
}
145+
}
146+
147+
public static MaxedAmount getMaxedAmount(double amount, double max) {
148+
if (max < 10000) {
149+
return new MaxedAmount(getAmount(amount, 1), getAmount(max, 1), "");
150+
} else {
151+
int i = 0;
152+
while (max / DECIMAL_NUMS[i] >= 1000) {
153+
i++;
154+
}
155+
return new MaxedAmount(getAmount(amount, DECIMAL_NUMS[i]), getAmount(max, DECIMAL_NUMS[i]), units[i]);
156+
}
157+
}
158+
159+
private static TextFormatting getColorCode(double p) {
160+
if (p < 0.33) {
161+
return TextFormatting.RED;
162+
} else if (p < 0.66) {
163+
return TextFormatting.YELLOW;
164+
} else {
165+
return TextFormatting.GREEN;
166+
}
167+
}
168+
169+
public static ITextComponent energyStorageComponent(double energy, double max) {
170+
return Tooltips.of(
171+
Tooltips.of(GuiText.StoredEnergy.getLocal()),
172+
Tooltips.of(": "),
173+
Tooltips.ofNumber(energy, max),
174+
Tooltips.of(" "),
175+
Tooltips.of(PowerUnits.AE),
176+
Tooltips.of(" ("),
177+
Tooltips.ofPercent(energy / max),
178+
Tooltips.of(")"));
179+
}
180+
181+
public static ITextComponent bytesUsed(long bytes, long max) {
182+
return of(
183+
Tooltips.of(
184+
ofUnformattedNumberWithRatioColor(bytes, (double) bytes / max, false),
185+
of(" "),
186+
of(GuiText.Of),
187+
of(" "),
188+
ofUnformattedNumber(max),
189+
of(" "),
190+
of(GuiText.BytesUsed)));
191+
}
192+
193+
public static ITextComponent typesUsed(long types, long max) {
194+
return Tooltips.of(
195+
ofUnformattedNumberWithRatioColor(types, (double) types / max, false),
196+
of(" "),
197+
of(GuiText.Of),
198+
of(" "),
199+
ofUnformattedNumber(max),
200+
of(" "),
201+
of(GuiText.Types));
202+
}
203+
}

src/main/java/appeng/core/localization/WailaText.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ public enum WailaText {
4141
Showing,
4242

4343
Contains,
44-
Channels;
44+
Channels,
45+
EnchantedWith,
46+
IdentityDeprecated;
4547

4648
private final String root;
4749

src/main/java/appeng/integration/modules/waila/PartWailaDataProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ public PartWailaDataProvider() {
6969
final IPartWailaDataProvider powerState = new PowerStateWailaDataProvider();
7070
final IPartWailaDataProvider p2pState = new P2PStateWailaDataProvider();
7171
final IPartWailaDataProvider partStack = new PartStackWailaDataProvider();
72+
final IPartWailaDataProvider annihilationPlane = new AnnihilationPlaneDataProvider();
7273

73-
this.providers = Lists.newArrayList(channel, storageMonitor, powerState, partStack, p2pState);
74+
this.providers = Lists.newArrayList(channel, storageMonitor, powerState, partStack, p2pState, annihilationPlane);
7475
}
7576

7677
@Override
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package appeng.integration.modules.waila.part;
2+
3+
import appeng.api.parts.IPart;
4+
import appeng.core.localization.WailaText;
5+
import appeng.parts.automation.PartAnnihilationPlane;
6+
import appeng.parts.automation.PartIdentityAnnihilationPlane;
7+
import appeng.util.EnchantmentUtil;
8+
import mcp.mobius.waila.api.IWailaConfigHandler;
9+
import mcp.mobius.waila.api.IWailaDataAccessor;
10+
import net.minecraft.enchantment.Enchantment;
11+
import net.minecraft.entity.player.EntityPlayerMP;
12+
import net.minecraft.nbt.NBTTagCompound;
13+
import net.minecraft.tileentity.TileEntity;
14+
import net.minecraft.util.math.BlockPos;
15+
import net.minecraft.world.World;
16+
17+
import java.util.List;
18+
import java.util.Map;
19+
20+
public class AnnihilationPlaneDataProvider extends BasePartWailaDataProvider {
21+
@Override
22+
public List<String> getWailaBody(IPart part, List<String> currentToolTip, IWailaDataAccessor accessor, IWailaConfigHandler config) {
23+
if (part instanceof PartIdentityAnnihilationPlane) {
24+
currentToolTip.add(WailaText.IdentityDeprecated.getLocal());
25+
} else if (part instanceof PartAnnihilationPlane plane) {
26+
NBTTagCompound nbtData = accessor.getNBTData();
27+
Map<Enchantment, Integer> enchantments = EnchantmentUtil.getEnchantments(nbtData);
28+
if (!enchantments.isEmpty()) {
29+
currentToolTip.add(WailaText.EnchantedWith.getLocal());
30+
for (var enchantment : enchantments.keySet()) {
31+
currentToolTip.add(enchantment.getTranslatedName(enchantments.get(enchantment)));
32+
}
33+
}
34+
}
35+
36+
return currentToolTip;
37+
}
38+
39+
@Override
40+
public NBTTagCompound getNBTData(EntityPlayerMP player, IPart part, TileEntity te, NBTTagCompound tag, World world, BlockPos pos) {
41+
if (part instanceof PartAnnihilationPlane plane) {
42+
plane.writeEnchantments(tag);
43+
}
44+
45+
return tag;
46+
}
47+
}

src/main/java/appeng/items/AEBaseItem.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ public final void getSubItems(final CreativeTabs creativeTab, final NonNullList<
5757
}
5858
}
5959

60-
@Override
61-
public boolean isBookEnchantable(final ItemStack itemstack1, final ItemStack itemstack2) {
62-
return false;
63-
}
64-
6560
@SideOnly(Side.CLIENT)
6661
protected void addCheckedInformation(final ItemStack stack, final World world, final List<String> lines, final ITooltipFlag advancedTooltips) {
6762
super.addInformation(stack, world, lines, advancedTooltips);

0 commit comments

Comments
 (0)