generated from neoforged/MDK
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
code cleanup and hard core torches support
- Loading branch information
1 parent
848d4aa
commit 1d5a87d
Showing
6 changed files
with
165 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/com/leclowndu93150/duradisplay/compat/BuiltinCompat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.leclowndu93150.duradisplay.compat; | ||
|
||
import net.minecraft.world.item.ItemStack; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
public record BuiltinCompat(double percentage, int color, boolean active) { | ||
@FunctionalInterface | ||
public interface CompatSupplier { | ||
@Nullable | ||
BuiltinCompat getCompat(ItemStack itemStack); | ||
} | ||
} |
19 changes: 0 additions & 19 deletions
19
src/main/java/com/leclowndu93150/duradisplay/compat/DependencyHandler.java
This file was deleted.
Oops, something went wrong.
47 changes: 28 additions & 19 deletions
47
src/main/java/com/leclowndu93150/duradisplay/compat/HTCompat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,39 @@ | ||
package com.leclowndu93150.duradisplay.compat; | ||
|
||
import com.github.wolfiewaffle.hardcore_torches.config.Config; | ||
import com.github.wolfiewaffle.hardcore_torches.item.LanternItem; | ||
import com.github.wolfiewaffle.hardcore_torches.item.OilCanItem; | ||
import com.github.wolfiewaffle.hardcore_torches.item.TorchItem; | ||
import com.leclowndu93150.duradisplay.Main; | ||
import net.minecraft.client.gui.Font; | ||
import net.minecraft.client.gui.GuiGraphics; | ||
import net.minecraft.world.item.Item; | ||
import net.minecraft.world.item.ItemStack; | ||
|
||
public class HTCompat { | ||
import javax.annotation.Nullable; | ||
|
||
|
||
HTCompat(ItemStack stack, Main.DuraDisplay display, GuiGraphics guiGraphics, Font font,int xPosition, int yPosition){ | ||
int fuelStored; | ||
int maxFuelStorage; | ||
if (stack.getItem() instanceof TorchItem item) { | ||
fuelStored = TorchItem.getFuel(stack); | ||
maxFuelStorage = item.getMaxFuel(); | ||
double fuelPercentage = ((double) fuelStored / (double) maxFuelStorage) * 100D; | ||
display.renderText(guiGraphics, font, String.format("%.0f%%", fuelPercentage), xPosition, yPosition, 0xe3bb56); // Custom color for energy display | ||
} else if (stack.getItem() instanceof LanternItem item) { | ||
fuelStored = LanternItem.getFuel(stack); | ||
maxFuelStorage = item.getMaxFuel(); | ||
double fuelPercentage = ((double) fuelStored / (double) maxFuelStorage) * 100D; | ||
display.renderText(guiGraphics, font, String.format("%.0f%%", fuelPercentage), xPosition, yPosition, 0xe3bb56); // Custom color for energy display | ||
} | ||
public record HTCompat(ItemStack itemStack) { | ||
public BuiltinCompat registerCompat() { | ||
// TODO: in 1.20.5+, replace this with a pattern switch | ||
int stored, maxStored; | ||
Item item1 = itemStack.getItem(); | ||
if (item1 instanceof TorchItem item) { | ||
stored = TorchItem.getFuel(itemStack); | ||
maxStored = item.getMaxFuel(); | ||
} else if (item1 instanceof LanternItem item) { | ||
stored = LanternItem.getFuel(itemStack); | ||
maxStored = item.getMaxFuel(); | ||
} else if (item1 instanceof OilCanItem) { | ||
stored = OilCanItem.getFuel(itemStack); | ||
maxStored = Config.maxCanFuel.get(); | ||
} else { | ||
stored = 0; | ||
maxStored = 0; | ||
} | ||
return new BuiltinCompat(((double) stored / (double) maxStored) * 100D,0xe3bb56, itemStack.isBarVisible()); | ||
} | ||
|
||
public static @Nullable HTCompat from(ItemStack itemStack) { | ||
Item item = itemStack.getItem(); | ||
if (item instanceof TorchItem || item instanceof LanternItem || item instanceof OilCanItem) | ||
return new HTCompat(itemStack); | ||
return null; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/com/leclowndu93150/duradisplay/renderer/DuraDisplay.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.leclowndu93150.duradisplay.renderer; | ||
|
||
import com.leclowndu93150.duradisplay.KeyBind; | ||
import com.leclowndu93150.duradisplay.Main; | ||
import com.leclowndu93150.duradisplay.compat.BuiltinCompat; | ||
import com.mojang.blaze3d.vertex.PoseStack; | ||
import com.mojang.blaze3d.vertex.Tesselator; | ||
import net.minecraft.client.gui.Font; | ||
import net.minecraft.client.gui.GuiGraphics; | ||
import net.minecraft.client.renderer.MultiBufferSource; | ||
import net.minecraft.client.renderer.RenderType; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraftforge.client.IItemDecorator; | ||
|
||
import static com.leclowndu93150.duradisplay.Main.BUILTIN_COMPATS; | ||
|
||
public class DuraDisplay implements IItemDecorator { | ||
@Override | ||
public boolean render(GuiGraphics guiGraphics, Font font, ItemStack stack, int xPosition, int yPosition) { | ||
if (!stack.isEmpty() && stack.isBarVisible()) { | ||
if (KeyBind.ForgeClient.modEnabled) { | ||
for (BuiltinCompat.CompatSupplier supplier : BUILTIN_COMPATS) { | ||
BuiltinCompat compat = supplier.getCompat(stack); | ||
if (compat != null && compat.active()) { | ||
double percentage = compat.percentage(); | ||
renderText(guiGraphics, font, String.format("%.0f%%", percentage), xPosition, yPosition, compat.color()); | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
renderItemBar(stack, xPosition, yPosition, guiGraphics); | ||
} | ||
return true; | ||
} | ||
|
||
public void renderText(GuiGraphics guiGraphics, Font font, String text, int xPosition, int yPosition, int color) { | ||
PoseStack poseStack = guiGraphics.pose(); | ||
int stringWidth = font.width(text); | ||
int x = ((xPosition + 8) * 2 + 1 + stringWidth / 2 - stringWidth); | ||
int y = (yPosition * 2) + 22; | ||
poseStack.pushPose(); | ||
poseStack.scale(0.5F, 0.5F, 0.5F); | ||
poseStack.translate(0.0D, 0.0D, 500.0D); | ||
MultiBufferSource.BufferSource multibuffersource$buffersource = MultiBufferSource.immediate(Tesselator.getInstance().getBuilder()); | ||
font.drawInBatch(text, x, y, color, true, poseStack.last().pose(), multibuffersource$buffersource, Font.DisplayMode.NORMAL, 0, 15728880, false); | ||
multibuffersource$buffersource.endBatch(); | ||
poseStack.popPose(); | ||
} | ||
|
||
private void renderItemBar(ItemStack stack, int xPosition, int yPosition, GuiGraphics guiGraphics) { | ||
int l = stack.getBarWidth(); | ||
int i = stack.getBarColor(); | ||
int j = xPosition + 2; | ||
int k = yPosition + 13; | ||
guiGraphics.fill(RenderType.guiOverlay(), j, k, j + 13, k + 2, -16777216); | ||
guiGraphics.fill(RenderType.guiOverlay(), j, k, j + l, k + 1, i | 0xFF000000); | ||
} | ||
} |