Skip to content

Commit

Permalink
Add custom tooltip and LED to bulk cell
Browse files Browse the repository at this point in the history
TODO: fix custom LEDs not displaying correctly on block (chest/drive) models
  • Loading branch information
62832 committed Jul 4, 2022
1 parent a448838 commit 6aa7041
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ninety.megacells.init;
package ninety.megacells.init.client;

import java.util.stream.Stream;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;

import ninety.megacells.integration.appmek.AppMekCellType;
import ninety.megacells.item.MEGABulkCell;
import ninety.megacells.item.MEGACellType;
import ninety.megacells.item.MEGAItems;
import ninety.megacells.item.MEGAPortableCell;
Expand All @@ -24,7 +25,7 @@ private static void initItemColors(ColorHandlerEvent.Item event) {
AppMekCellType.CHEMICAL.getCells().stream()).flatMap(s -> s).toList()) {
event.getItemColors().register(MEGAStorageCell::getColor, cell);
}
event.getItemColors().register(MEGAStorageCell::getColor, MEGAItems.BULK_ITEM_CELL.asItem());
event.getItemColors().register(MEGABulkCell::getColor, MEGAItems.BULK_ITEM_CELL.asItem());

for (var cell : Stream.of(
MEGACellType.ITEM.getPortableCells().stream(),
Expand Down
54 changes: 48 additions & 6 deletions src/main/java/ninety/megacells/item/MEGABulkCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import appeng.api.storage.cells.IBasicCellItem;
import appeng.api.upgrades.IUpgradeInventory;
import appeng.api.upgrades.UpgradeInventories;
import appeng.core.localization.Tooltips;
import appeng.items.AEBaseItem;
import appeng.items.contents.CellConfig;
import appeng.me.cells.BasicCellHandler;
import appeng.util.ConfigInventory;

public class MEGABulkCell extends AEBaseItem implements IBasicCellItem {
Expand All @@ -26,8 +28,22 @@ public MEGABulkCell(Properties properties) {

@OnlyIn(Dist.CLIENT)
@Override
public void appendHoverText(ItemStack stack, Level level, List<Component> lines, TooltipFlag advancedTooltips) {
addCellInformationToTooltip(stack, lines);
public void appendHoverText(ItemStack is, Level level, List<Component> lines, TooltipFlag advancedTooltips) {
var handler = BasicCellHandler.INSTANCE.getCellInventory(is, null);
if (handler == null) {
return;
}

double used = handler.getUsedBytes();
lines.add(used == 0 ? Tooltips.of("Empty")
: Tooltips.of(Tooltips.of("Capacity Used: "),
Tooltips.ofPercent(used / (double) Integer.MAX_VALUE, false)));

var containedType = handler.getAvailableStacks().getFirstKey();
if (containedType != null) {
var item = containedType.wrapForDisplayOrFilter();
lines.add(Tooltips.of(Tooltips.of("Contains: "), Tooltips.of(item.getHoverName())));
}
}

@Override
Expand All @@ -36,17 +52,17 @@ public AEKeyType getKeyType() {
}

@Override
public int getBytes(ItemStack cellItem) {
public int getBytes(ItemStack is) {
return Integer.MAX_VALUE;
}

@Override
public int getBytesPerType(ItemStack cellItem) {
public int getBytesPerType(ItemStack is) {
return 1;
}

@Override
public int getTotalTypes(ItemStack cellItem) {
public int getTotalTypes(ItemStack is) {
return 1;
}

Expand Down Expand Up @@ -76,5 +92,31 @@ public FuzzyMode getFuzzyMode(ItemStack is) {
}

@Override
public void setFuzzyMode(ItemStack is, FuzzyMode fzMode) {}
public void setFuzzyMode(ItemStack is, FuzzyMode fzMode) {
}

public static int getColor(ItemStack is, int tintIndex) {
if (tintIndex == 1) {
// Determine LED color

var handler = BasicCellHandler.INSTANCE.getCellInventory(is, null);
if (handler == null) {
return 0xFFFFFF;
}

double used = handler.getUsedBytes();

if (used == 0) {
return 0xFFFFFF;
} else {
double p = 1 - used / (double) Integer.MAX_VALUE;
int r = (int) (255d * (Math.max(0, Math.min(2 - 2 * p, 1))));
int g = (int) (255d * (Math.max(0, Math.min(2 * p, 1))));
return 0xFF000000 + (r << 16) + (g << 8);
}
} else {
// White
return 0xFFFFFF;
}
}
}

0 comments on commit 6aa7041

Please sign in to comment.