From e7fb530caf15236026808ffaecaf1b2cae140776 Mon Sep 17 00:00:00 2001 From: Alkalus <3060479+draknyte1@users.noreply.github.com> Date: Sat, 29 Jan 2022 13:33:21 +0000 Subject: Re-allow removal of debuffs when sleeping. Localized chat strings. --- .../handler/events/PlayerSleepEventHandler.java | 70 ++++++++++++++-------- .../core/util/minecraft/PlayerUtils.java | 5 ++ .../resources/assets/miscutils/lang/en_US.lang | 8 ++- 3 files changed, 56 insertions(+), 27 deletions(-) diff --git a/src/main/java/gtPlusPlus/core/handler/events/PlayerSleepEventHandler.java b/src/main/java/gtPlusPlus/core/handler/events/PlayerSleepEventHandler.java index d3700e1373..b9419a5ba6 100644 --- a/src/main/java/gtPlusPlus/core/handler/events/PlayerSleepEventHandler.java +++ b/src/main/java/gtPlusPlus/core/handler/events/PlayerSleepEventHandler.java @@ -17,6 +17,7 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.potion.Potion; import net.minecraft.potion.PotionEffect; +import net.minecraft.util.ChatComponentTranslation; import net.minecraftforge.event.entity.player.PlayerSleepInBedEvent; import net.minecraftforge.event.entity.player.PlayerWakeUpEvent; @@ -49,45 +50,62 @@ public class PlayerSleepEventHandler { @SubscribeEvent public void sleep(PlayerSleepInBedEvent event) { - event.setResult(Result.ALLOW); + } @SubscribeEvent public void wake(PlayerWakeUpEvent event) { EntityPlayer aPlayer = event.entityPlayer; - if (aPlayer != null && !aPlayer.worldObj.isRemote) { - // Try Heal - float aCurrentHP = aPlayer.getHealth(); - float aMaxHP = aPlayer.getMaxHealth(); - if (aCurrentHP < aMaxHP) { - float aDamage = aMaxHP - aCurrentHP; - float aToHeal = MathUtils.randFloat(1, aDamage); - if (aToHeal > 0) { - aPlayer.heal(aToHeal); - PlayerUtils.messagePlayer(aPlayer, "You slept well and now feel " + (aToHeal >= aDamage / 2 ? "much" : "a little") + " better."); + if (aPlayer != null && !aPlayer.worldObj.isRemote) { + boolean aRemovedBad = false; + try { + Collection aActive = aPlayer.getActivePotionEffects(); + for (PotionEffect aEffect : aActive) { + for (Potion aBadPotion : sNegativeEffects) { + if (aEffect.getPotionID() == aBadPotion.getId()) { + ReflectionUtils.setField(aEffect, sEffectDuration, 1); + aRemovedBad = true; + Logger.INFO("Set duration of " + aEffect.getEffectName() + " to 1 tick"); + } + } } } - // Already healed, try give a buff - else { - int aRandomBuff = MathUtils.randInt(0, sPositiveEffects.size() - 1); - Potion aPotionToApply = sPositiveEffects.get(aRandomBuff); - if (aPotionToApply != null) { - aPlayer.addPotionEffect(new GtPotionEffect(aPotionToApply.id, MathUtils.randInt(60, 180), MathUtils.randInt(0, 2))); - PlayerUtils.messagePlayer(aPlayer, "You feel really well rested."); - } + catch (Throwable t) { + t.printStackTrace(); } - boolean aRemovedBad = false; - /*for (Potion aBadPotion : sNegativeEffects) { - if (curePotionEffect(aPlayer, aBadPotion)) { - aRemovedBad = true; - } - }*/ if (aRemovedBad) { - PlayerUtils.messagePlayer(aPlayer, "The downsides of life no longer effect you."); + messagePlayer(aPlayer, "sleep.event.downsides"); } + else { + // Try Heal + float aCurrentHP = aPlayer.getHealth(); + float aMaxHP = aPlayer.getMaxHealth(); + if (aCurrentHP < aMaxHP) { + float aDamage = aMaxHP - aCurrentHP; + float aToHeal = MathUtils.randFloat(1, aDamage); + if (aToHeal > 0) { + aPlayer.heal(aToHeal); + messagePlayer(aPlayer, (aToHeal >= aDamage / 2 ? "sleep.event.good" : "sleep.event.okay")); + } + } + // Already healed, try give a buff + else { + int aRandomBuff = MathUtils.randInt(0, sPositiveEffects.size() - 1); + Potion aPotionToApply = sPositiveEffects.get(aRandomBuff); + if (aPotionToApply != null) { + aPlayer.addPotionEffect(new GtPotionEffect(aPotionToApply.id, MathUtils.randInt(60, 180), MathUtils.randInt(0, 2))); + messagePlayer(aPlayer, "sleep.event.wellrested"); + } + } + } } } + + private static void messagePlayer(EntityPlayer aPlayer, String aChatKey) { + PlayerUtils.messagePlayer(aPlayer, new ChatComponentTranslation(aChatKey, new Object[0])); + } + private static Field sEffectDuration = ReflectionUtils.getField(PotionEffect.class, "duration"); private static Field sActivePotionEffects = ReflectionUtils.getField(EntityPlayer.class, "activePotionsMap"); private static Method sOnFinishedPotionEffect = ReflectionUtils.getMethod(EntityPlayer.class, "onFinishedPotionEffect", PotionEffect.class); diff --git a/src/main/java/gtPlusPlus/core/util/minecraft/PlayerUtils.java b/src/main/java/gtPlusPlus/core/util/minecraft/PlayerUtils.java index 24ffa295b7..f2be723726 100644 --- a/src/main/java/gtPlusPlus/core/util/minecraft/PlayerUtils.java +++ b/src/main/java/gtPlusPlus/core/util/minecraft/PlayerUtils.java @@ -14,6 +14,7 @@ import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.server.MinecraftServer; import net.minecraft.util.ChunkCoordinates; +import net.minecraft.util.IChatComponent; import net.minecraft.world.World; import net.minecraftforge.common.util.FakePlayer; @@ -34,6 +35,10 @@ public class PlayerUtils { public static void messagePlayer(final EntityPlayer P, final String S){ gregtech.api.util.GT_Utility.sendChatToPlayer(P, S); } + + public static void messagePlayer(final EntityPlayer P, final IChatComponent S){ + P.addChatComponentMessage(S); + } public static EntityPlayer getPlayer(final String name){ try{ diff --git a/src/main/resources/assets/miscutils/lang/en_US.lang b/src/main/resources/assets/miscutils/lang/en_US.lang index a3639062f8..06b47f381e 100644 --- a/src/main/resources/assets/miscutils/lang/en_US.lang +++ b/src/main/resources/assets/miscutils/lang/en_US.lang @@ -3368,4 +3368,10 @@ item.FrothMonaziteflotation.name=Monazite Flotation Froth Cell //Added 18/12/21 item.BasicGenericChemItem.13.name=Formaldehyde Catalyst -item.hydrogenchloridemix.name=Hydrogen Chlorine Mix \ No newline at end of file +item.hydrogenchloridemix.name=Hydrogen Chlorine Mix + +//Added 29/01/22 +sleep.event.okay=You slept well and now feel a little better. +sleep.event.good=You slept well and now feel much better. +sleep.event.wellrested=You feel really well rested. +sleep.event.downsides=The downsides of life no longer effect you. \ No newline at end of file -- cgit