Add a hunger watcher instead of just health

This commit is contained in:
zontreck 2023-11-19 08:00:37 -07:00
parent 0b94cbbc2f
commit 5d62d175af
6 changed files with 130 additions and 30 deletions

View file

@ -0,0 +1,49 @@
package dev.zontreck.mcmods;
import dev.zontreck.ariaslib.terminal.Task;
import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.mcmods.configs.WMDClientConfig;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
public class CheckHealth extends Task
{
private static CheckHealth inst = new CheckHealth();
public static CheckHealth getInstance()
{
return inst;
}
public CheckHealth() {
super("CheckHealth", true);
}
@Override
public void run() {
// 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())
{
String Msg = ChatColor.doColors("!Dark_Red!!bold!You need to eat!");
Component chat = Component.literal(Msg);
Minecraft.getInstance().player.displayClientMessage(chat, false);
SoundEvent sv = SoundEvents.WARDEN_ROAR; // It sounds like a growling stomach
WatchMyDurability.Soundify(sv);
}
WatchMyDurability.LastHealth=current;
}
}

View file

@ -0,0 +1,40 @@
package dev.zontreck.mcmods;
import dev.zontreck.ariaslib.terminal.Task;
import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.mcmods.configs.WMDClientConfig;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
public class CheckHunger extends Task
{
public CheckHunger()
{
super("CheckHunger", true);
}
@Override
public void run() {
if(!WMDClientConfig.EnableHungerAlert.get()) return;
Hunger current = Hunger.of(Minecraft.getInstance().player);
if(current.identical()) return;
if(current.shouldGiveAlert())
{
String Msg = ChatColor.doColors("!Dark_Red!!bold!You need to eat!");
Component chat = Component.literal(Msg);
Minecraft.getInstance().player.displayClientMessage(chat, false);
SoundEvent sv = SoundEvents.WARDEN_ROAR; // It sounds like a growling stomach
WatchMyDurability.Soundify(sv);
}
WatchMyDurability.LastHunger = current;
}
}

View file

@ -58,27 +58,6 @@ public class CheckInventory extends Task {
}
// 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())
{
String Msg = ChatColor.doColors("!Dark_Red!!bold!You need to eat!");
Component chat = Component.literal(Msg);
Minecraft.getInstance().player.displayClientMessage(chat, false);
SoundEvent sv = SoundEvents.WARDEN_ROAR; // It sounds like a growling stomach
Soundify(sv);
}
WatchMyDurability.LastHealth=current;
}
public void PushItems(String type, List<ItemStack> stack)
@ -99,12 +78,6 @@ public class CheckInventory extends Task {
ItemRegistry.register(type,items);
}
public void Soundify(SoundEvent sound)
{
//WatchMyDurability.LOGGER.info("PLAY ALERT SOUND");
Minecraft.getInstance().player.playSound(sound, 1.0f, 1.0f);
}
public void checkList(String type, NonNullList<ItemStack> stacks){
Integer slotNum = 0;
//boolean ret=false;
@ -127,7 +100,7 @@ public class CheckInventory extends Task {
WatchMyDurability.LOGGER.info("Enqueue alert for an item. Playing sound for item: "+is1.getDisplayName().getString());
SoundEvent theSound = SoundEvents.ITEM_BREAK;
Soundify(theSound);
WatchMyDurability.Soundify(theSound);

View file

@ -0,0 +1,27 @@
package dev.zontreck.mcmods;
import net.minecraft.world.entity.player.Player;
public class Hunger
{
public boolean needsToEat;
public static Hunger of(Player player)
{
Hunger hunger = new Hunger();
hunger.needsToEat = player.getFoodData().needsFood();
return hunger;
}
public boolean shouldGiveAlert()
{
if(needsToEat && !WatchMyDurability.LastHunger.needsToEat) return true;
else return false;
}
public boolean identical()
{
if(needsToEat == WatchMyDurability.LastHunger.needsToEat) return true;
return false;
}
}

View file

@ -7,6 +7,7 @@ import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.mcmods.configs.WMDClientConfig;
import net.minecraft.client.Minecraft;
import net.minecraft.client.User;
import net.minecraft.sounds.SoundEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.IEventBus;
@ -39,6 +40,7 @@ public class WatchMyDurability
public static boolean isInGame = false; // This locks the timer thread
public static ItemRegistry REGISTRY;
public static Health LastHealth;
public static Hunger LastHunger;
public static String WMDPrefix;
@ -64,6 +66,12 @@ public class WatchMyDurability
//LOGGER.info("HELLO FROM COMMON SETUP");
}
public static void Soundify(SoundEvent sound)
{
//WatchMyDurability.LOGGER.info("PLAY ALERT SOUND");
Minecraft.getInstance().player.playSound(sound, 1.0f, 1.0f);
}
// You can use SubscribeEvent and let the Event Bus discover methods to call
@SubscribeEvent
public void onServerStarting(ServerStartingEvent event)
@ -96,6 +104,7 @@ public class WatchMyDurability
ItemRegistry.Initialize();
}
}
@Mod.EventBusSubscriber(modid = MODID, bus = Mod.EventBusSubscriber.Bus.FORGE)
@ -111,6 +120,7 @@ public class WatchMyDurability
DelayedExecutorService.start();
DelayedExecutorService.getInstance().schedule(CheckInventory.getInstance(), WMDClientConfig.TimerVal.get());
DelayedExecutorService.getInstance().schedule(CheckHealth.getInstance(), WMDClientConfig.TimerVal.get());
}
@SubscribeEvent

View file

@ -14,6 +14,7 @@ public class WMDClientConfig {
public static ForgeConfigSpec.ConfigValue<Integer> TimerVal;
//public static ForgeConfigSpec.ConfigValue<Boolean> EnableExtraHearts;
public static ForgeConfigSpec.ConfigValue<Boolean> EnableHealthAlert;
public static ForgeConfigSpec.ConfigValue<Boolean> EnableHungerAlert;
static{
List<Integer> alerts1 = new ArrayList<>();
@ -34,8 +35,8 @@ public class WMDClientConfig {
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);
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("watchMyHealth", false);
EnableHungerAlert = BUILDER.comment("This is a newer setting to watch your hunger status instead of your hunger to alert when you need to eat").define("watchMyHunger", true);
SPEC=BUILDER.build();
}