Skip to content

Commit

Permalink
Merge pull request #4 from GTNewHorizons/dev
Browse files Browse the repository at this point in the history
Fix more types of items
  • Loading branch information
Caedis authored Jun 8, 2023
2 parents b5d062b + 0e9eb4f commit 786b5c2
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 16 deletions.
3 changes: 2 additions & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
api("net.industrial-craft:industrialcraft-2:2.2.828-experimental:dev")
api("com.github.GTNewHorizons:EnderIO:2.4.18:dev")
api("com.github.GTNewHorizons:Botania:1.9.23-GTNH:dev")
api("com.github.GTNewHorizons:TinkersConstruct:1.9.36-GTNH:dev")

runtimeOnlyNonPublishable("com.github.GTNewHorizons:TinkersConstruct:1.9.36-GTNH:dev")
runtimeOnlyNonPublishable("com.github.GTNewHorizons:GTplusplus:1.9.38:dev")
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public String getMixinConfig() {

@Override
public List<String> getMixins(Set<String> loadedCoreMods) {
return Arrays.asList("minecraft.MixinRenderItem");
return Arrays.asList("minecraft.MixinRenderItem", "minecraft.MixinGuiScreen");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.caedis.duradisplay.mixins.minecraft;

import net.minecraft.client.gui.GuiScreen;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.caedis.duradisplay.render.DurabilityRenderer;

@Mixin(value = GuiScreen.class)
public class MixinGuiScreen {

@SuppressWarnings("UnresolvedMixinReference")
@Inject(method = "drawScreen(IIF)V", at = @At("HEAD"))
private void drawScreenStart(CallbackInfo cbi) {
DurabilityRenderer.ShouldRun = false;
}

@SuppressWarnings("UnresolvedMixinReference")
@Inject(method = "drawScreen(IIF)V", at = @At("RETURN"))
private void drawScreenEnd(CallbackInfo cbi) {
DurabilityRenderer.ShouldRun = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public abstract class MixinRenderItem {
target = "Lnet/minecraft/item/Item;showDurabilityBar(Lnet/minecraft/item/ItemStack;)Z"))
private boolean showDurabilityBar(Item item0, ItemStack stack0, FontRenderer fontRenderer,
TextureManager textureManager, ItemStack stack, int xPosition, int yPosition, String string) {
if (!DurabilityRenderer.ShouldRun) return item0.showDurabilityBar(stack0);
if (!DuraDisplayConfig.Durability_Enable && !DuraDisplayConfig.Charge_Enable)
return item0.showDurabilityBar(stack0);

Expand All @@ -49,6 +50,7 @@ private boolean showDurabilityBar(Item item0, ItemStack stack0, FontRenderer fon
ordinal = 0))
private void renderItemAndEffectIntoGUI(FontRenderer fontRenderer, TextureManager textureManager, ItemStack stack,
int xPosition, int yPosition, CallbackInfo ci) {
if (!DurabilityRenderer.ShouldRun) return;
if (!DuraDisplayConfig.Durability_Enable && !DuraDisplayConfig.Charge_Enable) return;
if (stack == null || stack.getItem() == null || !(stack.getItem() instanceof GT_MetaBase_Item)) return;

Expand Down
61 changes: 48 additions & 13 deletions src/main/java/com/caedis/duradisplay/render/DurabilityRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
import ic2.api.item.ICustomDamageItem;
import ic2.api.item.IElectricItem;
import ic2.core.item.armor.ItemArmorFluidTank;
import tconstruct.library.tools.ToolCore;
import tconstruct.library.weaponry.AmmoItem;
import vazkii.botania.common.item.brew.ItemBrewBase;

public class DurabilityRenderer {

// private static final Map<Class<?>, Function<ItemStack, List<ItemStackOverlay>>> itemHandlers;
//
public static boolean ShouldRun = true;

// Linked so that classes are checked in order
private static final Map<Class<?>, ItemHandler> itemHandlers = new LinkedHashMap<>();
Expand All @@ -49,8 +52,10 @@ interface ItemHandler {
itemHandlers.put(GT_MetaBase_Item.class, DurabilityRenderer::handleGregTech);
itemHandlers.put(GT_RadioactiveCell_Item.class, DurabilityRenderer::handleGregTechRadioactiveCell);
itemHandlers.put(IDarkSteelItem.class, DurabilityRenderer::handleDarkSteelItems);
itemHandlers.put(IElectricItem.class, DurabilityRenderer::handleIElectricItem);
itemHandlers.put(AmmoItem.class, (is -> null));
itemHandlers.put(ToolCore.class, DurabilityRenderer::handleToolCore);
itemHandlers.put(ItemArmorFluidTank.class, DurabilityRenderer::handleItemArmorFluidTank);
itemHandlers.put(IElectricItem.class, DurabilityRenderer::handleIElectricItem);
itemHandlers.put(ICustomDamageItem.class, DurabilityRenderer::handleICustomDamageItem);
itemHandlers.put(IEnergyContainerItem.class, DurabilityRenderer::handleEnergyContainer);
itemHandlers.put(ItemBrewBase.class, DurabilityRenderer::handleBotaniaBrew);
Expand Down Expand Up @@ -103,15 +108,16 @@ public static int getRGBDurabilityForDisplay(double dur) {
}

private static List<ItemStackOverlay> handleDefault(@NotNull ItemStack stack) {
if (!DuraDisplayConfig.Durability_Enable || !(stack.isItemStackDamageable()
&& (DuraDisplayConfig.Durability_PercentageWhenFull || stack.isItemDamaged()))) return null;
assert stack.getItem() != null;
Item item = stack.getItem();
assert item != null;
if (!DuraDisplayConfig.Durability_Enable
|| !(item.isDamageable() && (DuraDisplayConfig.Durability_PercentageWhenFull || item.isDamaged(stack))))
return null;

List<ItemStackOverlay> overlays = new ArrayList<>();

ItemStackOverlay durabilityOverlay = new ItemStackOverlay.DurabilityOverlay();
double durability = (1 - stack.getItem()
.getDurabilityForDisplay(stack));
double durability = (1 - item.getDurabilityForDisplay(stack));
if (Double.isNaN(durability)) return null;
durabilityOverlay.color = getRGBDurabilityForDisplay(durability);
durability *= 100;
Expand Down Expand Up @@ -164,6 +170,37 @@ private static List<ItemStackOverlay> handleGregTech(@NotNull ItemStack stack) {
return overlays;
}

private static List<ItemStackOverlay> handleToolCore(@NotNull ItemStack stack) {
if (!stack.hasTagCompound() || !stack.getTagCompound()
.hasKey("InfiTool")) return null;
NBTTagCompound tags = stack.getTagCompound()
.getCompoundTag("InfiTool");
List<ItemStackOverlay> overlays = new ArrayList<>();

if (tags.hasKey("Unbreaking")) {
if (tags.getInteger("Unbreaking") < 10) {
List<ItemStackOverlay> defaultOverlays = handleDefault(stack);
if (defaultOverlays != null) {
overlays.addAll(defaultOverlays);
}
}
}

if (!DuraDisplayConfig.Charge_Enable || !stack.getTagCompound()
.hasKey("Energy")) return overlays;
IEnergyContainerItem eci = ((IEnergyContainerItem) stack.getItem());
assert eci != null;

ItemStackOverlay chargeOverlay = new ItemStackOverlay.ChargeOverlay();
double durability = ((double) eci.getEnergyStored(stack) / eci.getMaxEnergyStored(stack)) * 100;
if (Double.isNaN(durability)) return null;
chargeOverlay.isFull = durability == 100.0;
chargeOverlay.value = nf.format(durability) + "%";
overlays.add(chargeOverlay);

return overlays;
}

private static List<ItemStackOverlay> handleGregTechRadioactiveCell(@NotNull ItemStack stack) {
if (!DuraDisplayConfig.Durability_Enable) return null;
GT_RadioactiveCell_Item bei = ((GT_RadioactiveCell_Item) stack.getItem());
Expand Down Expand Up @@ -263,13 +300,13 @@ private static List<ItemStackOverlay> handleEnergyContainer(@NotNull ItemStack s
}

if (!DuraDisplayConfig.Charge_Enable || !(stack.hasTagCompound() && stack.getTagCompound()
.hasKey("Energy"))) return overlays; // because TiCon tools have the interface
.hasKey("Energy"))) return overlays;
IEnergyContainerItem eci = ((IEnergyContainerItem) stack.getItem());
assert eci != null;

ItemStackOverlay chargeOverlay = new ItemStackOverlay.ChargeOverlay();
double durability = ((double) eci.getEnergyStored(stack) / eci.getMaxEnergyStored(stack)) * 100;
if (Double.isNaN(durability)) return null;
if (Double.isNaN(durability)) return overlays;
chargeOverlay.isFull = durability == 100.0;
chargeOverlay.value = nf.format(durability) + "%";
overlays.add(chargeOverlay);
Expand All @@ -284,7 +321,7 @@ private static List<ItemStackOverlay> handleDarkSteelItems(@NotNull ItemStack st
if (defaultOverlays != null) {
overlays.addAll(defaultOverlays);
}
if (!DuraDisplayConfig.Charge_Enable || !stack.hasTagCompound()) return null;
if (!DuraDisplayConfig.Charge_Enable || !stack.hasTagCompound()) return overlays;

NBTTagCompound nbt = stack.getTagCompound();
if (nbt.hasKey("enderio.darksteel.upgrade.energyUpgrade")) {
Expand All @@ -294,15 +331,13 @@ private static List<ItemStackOverlay> handleDarkSteelItems(@NotNull ItemStack st

ItemStackOverlay chargeOverlay = new ItemStackOverlay.ChargeOverlay();
double durability = ((double) energy / capacity) * 100;
if (Double.isNaN(durability)) return null;
if (Double.isNaN(durability)) return overlays;
chargeOverlay.isFull = durability == 100.0;
chargeOverlay.value = nf.format(durability) + "%";

overlays.add(chargeOverlay);
}

// normal item durability is handled in default case

return overlays;
}
}
3 changes: 2 additions & 1 deletion src/main/resources/mixins.duradisplay.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"target": "@env(DEFAULT)",
"compatibilityLevel": "JAVA_8",
"client": [
"minecraft.MixinRenderItem"
"minecraft.MixinRenderItem",
"minecraft.MixinGuiScreen"
]
}

0 comments on commit 786b5c2

Please sign in to comment.