Push next version of WMD

This commit is contained in:
Zontreck 2022-10-12 23:50:06 -07:00
parent 689485e337
commit b8a065590c
10 changed files with 427 additions and 6 deletions

View file

@ -4,7 +4,7 @@ plugins {
id 'net.minecraftforge.gradle' version '5.1.+'
}
version = '1.1.0'
version = "${myversion}"
group = 'dev.zontreck.mcmods' // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = 'WatchMyDurability'
@ -26,7 +26,7 @@ minecraft {
//
// Use non-default mappings at your own risk. They may not always work.
// Simply re-run your setup task after changing the mappings to update your workspace.
mappings channel: 'official', version: '1.19.2'
mappings channel: 'official', version: "${mc_version}"
// accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg') // Currently, this location cannot be changed from the default.
@ -123,14 +123,21 @@ repositories {
// flatDir {
// dir 'libs'
// }
maven {
name = "ZNI Creations"
url = "https://maven.zontreck.dev"
}
}
dependencies {
// Specify the version of Minecraft to use. If this is any group other than 'net.minecraft', it is assumed
// that the dep is a ForgeGradle 'patcher' dependency, and its patches will be applied.
// The userdev artifact is a special name and will get all sorts of transformations applied to it.
minecraft 'net.minecraftforge:forge:1.19.2-43.1.1'
minecraft "net.minecraftforge:forge:${mc_version}-${forge_version}"
implementation fg.deobf("dev.zontreck:libzontreck:1.0.0.1:dev")
runtimeOnly fg.deobf("dev.zontreck:libzontreck:1.0.0.1")
compileOnly fg.deobf("dev.zontreck:libzontreck:1.0.0.1:dev")
// Real mod deobf dependency examples - these get remapped to your current mappings
// compileOnly fg.deobf("mezz.jei:jei-${mc_version}:${jei_version}:api") // Adds JEI API as a compile dependency
// runtimeOnly fg.deobf("mezz.jei:jei-${mc_version}:${jei_version}") // Adds the full JEI mod as a runtime dependency

View file

@ -1,4 +1,8 @@
# Sets default memory used for gradle commands. Can be overridden by user or command line properties.
# This is required to provide enough memory for the Minecraft decompilation process.
org.gradle.jvmargs=-Xmx3G
org.gradle.daemon=false
org.gradle.daemon=false
mc_version=1.19.2
forge_version=43.1.16
myversion = 1.1.1

View file

@ -5,6 +5,9 @@ import java.util.List;
import java.util.Map;
import java.util.TimerTask;
import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.libzontreck.chat.ChatColorFactory;
import dev.zontreck.libzontreck.chat.ChatColor.ColorOptions;
import dev.zontreck.mcmods.configs.WMDClientConfig;
import net.minecraft.client.Minecraft;
import net.minecraft.core.NonNullList;
@ -40,6 +43,25 @@ public class CheckInventory extends TimerTask
WatchMyDurability.LOGGER.warn("A error in the WatchMyDurability timer code has occurred. This could happen with hub worlds and the server transfers that occur. If this happened in another instance, please report this error to the developer, along with what you were doing.");
}
// Hijack this timer so we dont need to register yet another
if(!WMDClientConfig.EnableHealthAlert.get())return;
Health current = Health.of(Minecraft.getInstance().player);
if(WatchMyDurability.LastHealth == null)WatchMyDurability.LastHealth = current;
else{
if(current.identical(WatchMyDurability.LastHealth))return;
}
// Good to proceed
if(current.shouldGiveAlert())
{
Minecraft.getInstance().player.displayClientMessage(Component.literal(ChatColorFactory.MakeBuilder().set(ColorOptions.Dark_Red).set(ColorOptions.Bold).toString()+"You need to eat!"), false);
SoundEvent sv = SoundEvents.WARDEN_ROAR;
Soundify(sv);
}
}
public void PushItems(String type, List<ItemStack> stack)
@ -84,7 +106,7 @@ public class CheckInventory extends TimerTask
String entryStr = WMDClientConfig.alertMessages.get().get(idx);
if(percent <= entry){
String replaced = entryStr.replaceAll("!item!", is1.getDisplayName().getString());
String replaced = WatchMyDurability.WMDPrefix + ChatColor.DARK_RED + entryStr.replaceAll("!item!", is1.getDisplayName().getString());
WatchMyDurability.LOGGER.info("Enqueue alert for an item. Playing sound for item: "+is1.getDisplayName().getString());
SoundEvent theSound = SoundEvents.ITEM_BREAK;

View file

@ -0,0 +1,36 @@
package dev.zontreck.mcmods;
import net.minecraft.world.entity.player.Player;
public class Health {
public float maximum;
public float current;
public int asPercent()
{
return (int)Math.round(Math.abs((current * 100 / maximum)));
}
public Health lastHealthState;
public static Health of(Player player){
Health obj = new Health();
obj.current = player.getHealth();
obj.maximum = player.getMaxHealth();
return obj;
}
public boolean shouldGiveAlert()
{
if(asPercent()<=50){
return true;
}else return false;
}
public boolean identical(Health other)
{
if(other.current == current && other.maximum == maximum)return true;
else return false;
}
}

View file

@ -21,6 +21,9 @@ public class ItemRegistry {
}
}
public class Health {
}
public Map<String,Map<Integer, Item>> CachedItems;
public ItemRegistry()

View file

@ -2,7 +2,10 @@ package dev.zontreck.mcmods;
import com.mojang.logging.LogUtils;
import dev.zontreck.libzontreck.chat.ChatColorFactory;
import dev.zontreck.libzontreck.chat.ChatColor.ColorOptions;
import dev.zontreck.mcmods.configs.WMDClientConfig;
import dev.zontreck.mcmods.gui.HeartsRenderer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.User;
import net.minecraftforge.common.MinecraftForge;
@ -34,17 +37,21 @@ public class WatchMyDurability
public static User CurrentUser = null; // This is initialized by the client
public static boolean isInGame = false; // This locks the timer thread
public static ItemRegistry REGISTRY;
public static Health LastHealth;
public static String WMDPrefix;
public WatchMyDurability()
{
WMDPrefix = ChatColorFactory.MakeBuilder().set(ColorOptions.Dark_Gray).toString() + "[" + ChatColorFactory.MakeBuilder().set(ColorOptions.Dark_Green) + "WMD" + ChatColorFactory.MakeBuilder().set(ColorOptions.Dark_Gray) + "]";
IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
// Register the commonSetup method for modloading
modEventBus.addListener(this::commonSetup);
ModLoadingContext.get().registerConfig(Type.CLIENT, WMDClientConfig.SPEC, "watchmydurability-client.toml");
MinecraftForge.EVENT_BUS.register(new HeartsRenderer());
// Register ourselves for server and other game events we are interested in
MinecraftForge.EVENT_BUS.register(this);
}

View file

@ -12,6 +12,9 @@ public class WMDClientConfig {
public static ForgeConfigSpec.ConfigValue<List<Integer>> alertPercents;
public static ForgeConfigSpec.ConfigValue<List<String>> alertMessages;
public static ForgeConfigSpec.ConfigValue<Integer> TimerVal;
public static ForgeConfigSpec.ConfigValue<Boolean> EnableExtraHearts;
public static ForgeConfigSpec.ConfigValue<Boolean> EnableHealthAlert;
static{
List<Integer> alerts1 = new ArrayList<>();
alerts1.add(10);
@ -28,6 +31,12 @@ public class WMDClientConfig {
TimerVal = BUILDER.comment("How many seconds between timer ticks to check your inventory items?").define("Timer", 5);
BUILDER.pop();
BUILDER.push("General");
EnableExtraHearts = BUILDER.comment("Whether to enable the extra hearts rendering").define("compress_hearts", false);
EnableHealthAlert = BUILDER.comment("The following was added for a friend. If you need reminders to eat in order to heal, turn the below option on").define("watchMyHunger", false);
SPEC=BUILDER.build();
}
}

View file

@ -0,0 +1,333 @@
/*
*
* DISCLAIMER: This code was taken from Mantle, and will be modified to fit the needs of this mod, such as adding more heat options. This code is subject to Mantle's license of MIT.
* Despite this code being taken from, and modified/updated to be modern, all textures are my own creation
* This disclaimer is here to give credit where credit is due. The author(s) of mantle have done a absolutely fantastic job. And if Mantle gets updated this shall be removed along with future plans of extra hearts and color options.
*
*
*/
package dev.zontreck.mcmods.gui;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import dev.zontreck.mcmods.WatchMyDurability;
import dev.zontreck.mcmods.configs.WMDClientConfig;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.client.event.RenderGuiOverlayEvent;
import net.minecraftforge.client.gui.overlay.ForgeGui;
import net.minecraftforge.client.gui.overlay.GuiOverlayManager;
import net.minecraftforge.client.gui.overlay.NamedGuiOverlay;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import java.util.Random;
public class HeartsRenderer {
private static final ResourceLocation ICON_HEARTS = new ResourceLocation(WatchMyDurability.MODID,
"textures/gui/hearts.png");
private static final ResourceLocation ICON_ABSORB = new ResourceLocation(WatchMyDurability.MODID,
"textures/gui/absorb.png");
private static final ResourceLocation ICON_VANILLA = GuiComponent.GUI_ICONS_LOCATION;
private final Minecraft mc = Minecraft.getInstance();
private int playerHealth = 0;
private int lastPlayerHealth = 0;
private long healthUpdateCounter = 0;
private long lastSystemTime = 0;
private final Random rand = new Random();
private int regen;
/**
* Draws a texture to the screen
*
* @param matrixStack Matrix stack instance
* @param x X position
* @param y Y position
* @param textureX Texture X
* @param textureY Texture Y
* @param width Width to draw
* @param height Height to draw
*/
private void blit(PoseStack matrixStack, int x, int y, int textureX, int textureY, int width, int height) {
Minecraft.getInstance().gui.blit(matrixStack, x, y, textureX, textureY, width, height);
}
/* HUD */
/**
* Event listener
*
* @param event Event instance
*/
@SubscribeEvent(priority = EventPriority.LOW)
public void renderHealthbar(RenderGuiOverlayEvent.Pre event) {
NamedGuiOverlay ActualOverlay = GuiOverlayManager.findOverlay(new ResourceLocation("minecraft:player_health"));
if (ActualOverlay == null) {
if (GuiOverlayManager.getOverlays() == null) {
WatchMyDurability.LOGGER.info("Overlays non existent?!");
}
for (NamedGuiOverlay overlay : GuiOverlayManager.getOverlays()) {
// Next print
// LibZontreck.LOGGER.info("GUI OVERLAY: "+overlay.id().getPath());
if (overlay.id().getPath().equals("player_health")) {
ActualOverlay = overlay;
break;
}
}
}
if (event.isCanceled() || !WMDClientConfig.EnableExtraHearts.get() || event.getOverlay() != ActualOverlay) {
return;
}
// ensure its visible
if (!(mc.gui instanceof ForgeGui gui) || mc.options.hideGui || !gui.shouldDrawSurvivalElements()) {
return;
}
Entity renderViewEnity = this.mc.getCameraEntity();
if (!(renderViewEnity instanceof Player player)) {
return;
}
gui.setupOverlayRenderState(true, false);
this.mc.getProfiler().push("health");
// extra setup stuff from us
int left_height = gui.leftHeight;
int width = this.mc.getWindow().getGuiScaledWidth();
int height = this.mc.getWindow().getGuiScaledHeight();
int updateCounter = this.mc.gui.getGuiTicks();
// start default forge/mc rendering
// changes are indicated by comment
int health = Mth.ceil(player.getHealth());
boolean highlight = this.healthUpdateCounter > (long) updateCounter
&& (this.healthUpdateCounter - (long) updateCounter) / 3L % 2L == 1L;
if (health < this.playerHealth && player.invulnerableTime > 0) {
this.lastSystemTime = Util.getMillis();
this.healthUpdateCounter = (updateCounter + 20);
} else if (health > this.playerHealth && player.invulnerableTime > 0) {
this.lastSystemTime = Util.getMillis();
this.healthUpdateCounter = (updateCounter + 10);
}
if (Util.getMillis() - this.lastSystemTime > 1000L) {
this.playerHealth = health;
this.lastPlayerHealth = health;
this.lastSystemTime = Util.getMillis();
}
this.playerHealth = health;
int healthLast = this.lastPlayerHealth;
AttributeInstance attrMaxHealth = player.getAttribute(Attributes.MAX_HEALTH);
float healthMax = attrMaxHealth == null ? 0 : (float) attrMaxHealth.getValue();
float absorb = Mth.ceil(player.getAbsorptionAmount());
// CHANGE: simulate 10 hearts max if there's more, so vanilla only renders one
// row max
healthMax = Math.min(healthMax, 20f);
health = Math.min(health, 20);
absorb = Math.min(absorb, 20);
int healthRows = Mth.ceil((healthMax + absorb) / 2.0F / 10.0F);
int rowHeight = Math.max(10 - (healthRows - 2), 3);
this.rand.setSeed(updateCounter * 312871L);
int left = width / 2 - 91;
int top = height - left_height;
// change: these are unused below, unneeded? should these adjust the Forge
// variable?
// left_height += (healthRows * rowHeight);
// if (rowHeight != 10) left_height += 10 - rowHeight;
this.regen = -1;
if (player.hasEffect(MobEffects.REGENERATION)) {
this.regen = updateCounter % 25;
}
assert this.mc.level != null;
final int TOP = 9 * (this.mc.level.getLevelData().isHardcore() ? 5 : 0);
final int BACKGROUND = (highlight ? 25 : 16);
int MARGIN = 16;
if (player.hasEffect(MobEffects.POISON))
MARGIN += 36;
else if (player.hasEffect(MobEffects.WITHER))
MARGIN += 72;
float absorbRemaining = absorb;
PoseStack matrixStack = event.getPoseStack();
for (int i = Mth.ceil((healthMax + absorb) / 2.0F) - 1; i >= 0; --i) {
int row = Mth.ceil((float) (i + 1) / 10.0F) - 1;
int x = left + i % 10 * 8;
int y = top - row * rowHeight;
if (health <= 4)
y += this.rand.nextInt(2);
if (i == this.regen)
y -= 2;
this.blit(matrixStack, x, y, BACKGROUND, TOP, 9, 9);
if (highlight) {
if (i * 2 + 1 < healthLast) {
this.blit(matrixStack, x, y, MARGIN + 54, TOP, 9, 9); // 6
} else if (i * 2 + 1 == healthLast) {
this.blit(matrixStack, x, y, MARGIN + 63, TOP, 9, 9); // 7
}
}
if (absorbRemaining > 0.0F) {
if (absorbRemaining == absorb && absorb % 2.0F == 1.0F) {
this.blit(matrixStack, x, y, MARGIN + 153, TOP, 9, 9); // 17
absorbRemaining -= 1.0F;
} else {
this.blit(matrixStack, x, y, MARGIN + 144, TOP, 9, 9); // 16
absorbRemaining -= 2.0F;
}
} else {
if (i * 2 + 1 < health) {
this.blit(matrixStack, x, y, MARGIN + 36, TOP, 9, 9); // 4
} else if (i * 2 + 1 == health) {
this.blit(matrixStack, x, y, MARGIN + 45, TOP, 9, 9); // 5
}
}
}
this.renderExtraHearts(matrixStack, left, top, player);
this.renderExtraAbsorption(matrixStack, left, top - rowHeight, player);
RenderSystem.setShaderTexture(0, ICON_VANILLA);
gui.leftHeight += 10;
if (absorb > 0) {
gui.leftHeight += 10;
}
event.setCanceled(true);
RenderSystem.disableBlend();
this.mc.getProfiler().pop();
MinecraftForge.EVENT_BUS
.post(new RenderGuiOverlayEvent.Post(mc.getWindow(), matrixStack, event.getPartialTick(), ActualOverlay));
}
/**
* Gets the texture from potion effects
*
* @param player Player instance
* @return Texture offset for potion effects
*/
private int getPotionOffset(Player player) {
int potionOffset = 0;
MobEffectInstance potion = player.getEffect(MobEffects.WITHER);
if (potion != null) {
potionOffset = 18;
}
potion = player.getEffect(MobEffects.POISON);
if (potion != null) {
potionOffset = 9;
}
assert this.mc.level != null;
if (this.mc.level.getLevelData().isHardcore()) {
potionOffset += 27;
}
return potionOffset;
}
/**
* Renders the health above 10 hearts
*
* @param matrixStack Matrix stack instance
* @param xBasePos Health bar top corner
* @param yBasePos Health bar top corner
* @param player Player instance
*/
private void renderExtraHearts(PoseStack matrixStack, int xBasePos, int yBasePos, Player player) {
int potionOffset = this.getPotionOffset(player);
// Extra hearts
RenderSystem.setShaderTexture(0, ICON_HEARTS);
int hp = Mth.ceil(player.getHealth());
this.renderCustomHearts(matrixStack, xBasePos, yBasePos, potionOffset, hp, false);
}
/**
* Renders the absorption health above 10 hearts
*
* @param matrixStack Matrix stack instance
* @param xBasePos Health bar top corner
* @param yBasePos Health bar top corner
* @param player Player instance
*/
private void renderExtraAbsorption(PoseStack matrixStack, int xBasePos, int yBasePos, Player player) {
int potionOffset = this.getPotionOffset(player);
// Extra hearts
RenderSystem.setShaderTexture(0, ICON_ABSORB);
int absorb = Mth.ceil(player.getAbsorptionAmount());
this.renderCustomHearts(matrixStack, xBasePos, yBasePos, potionOffset, absorb, true);
}
/**
* Gets the texture offset from the regen effect
*
* @param i Heart index
* @param offset Current offset
*/
private int getYRegenOffset(int i, int offset) {
return i + offset == this.regen ? -2 : 0;
}
/**
* Shared logic to render custom hearts
*
* @param matrixStack Matrix stack instance
* @param xBasePos Health bar top corner
* @param yBasePos Health bar top corner
* @param potionOffset Offset from the potion effect
* @param count Number to render
* @param absorb If true, render absorption hearts
*/
private void renderCustomHearts(PoseStack matrixStack, int xBasePos, int yBasePos, int potionOffset, int count,
boolean absorb) {
int regenOffset = absorb ? 10 : 0;
for (int iter = 0; iter < count / 20; iter++) {
int renderHearts = (count - 20 * (iter + 1)) / 2;
int heartIndex = iter % 11;
if (renderHearts > 10) {
renderHearts = 10;
}
for (int i = 0; i < renderHearts; i++) {
int y = this.getYRegenOffset(i, regenOffset);
if (absorb) {
this.blit(matrixStack, xBasePos + 8 * i, yBasePos + y, 0, 54, 9, 9);
}
this.blit(matrixStack, xBasePos + 8 * i, yBasePos + y, 18 * heartIndex, potionOffset, 9, 9);
}
if (count % 2 == 1 && renderHearts < 10) {
int y = this.getYRegenOffset(renderHearts, regenOffset);
if (absorb) {
this.blit(matrixStack, xBasePos + 8 * renderHearts, yBasePos + y, 0, 54, 9, 9);
}
this.blit(matrixStack, xBasePos + 8 * renderHearts, yBasePos + y, 9 + 18 * heartIndex, potionOffset, 9, 9);
}
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB