Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Finish food stats changes - Fixed sync issues, Minor start to balanci…
…ng nutrition and thirst. Fixes #108
  • Loading branch information
alcatrazEscapee committed Jun 29, 2019
1 parent e7bf066 commit c32cd1f
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 31 deletions.
21 changes: 11 additions & 10 deletions src/main/java/net/dries007/tfc/CommonEventHandler.java
Expand Up @@ -14,7 +14,6 @@
import net.minecraft.init.SoundEvents;
import net.minecraft.item.*;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.FoodStats;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
Expand All @@ -31,7 +30,6 @@
import net.minecraftforge.fml.common.gameevent.PlayerEvent;

import net.dries007.tfc.api.capability.ItemStickCapability;
import net.dries007.tfc.api.capability.food.FoodStatsTFC;
import net.dries007.tfc.api.capability.food.IFoodStatsTFC;
import net.dries007.tfc.api.capability.size.CapabilityItemSize;
import net.dries007.tfc.api.capability.size.Size;
Expand Down Expand Up @@ -183,13 +181,23 @@ public static void onUseHoe(UseHoeEvent event)
public static void onLivingHurt(LivingHurtEvent event)
{
float actualDamage = event.getAmount();
// Modifier for damage type + damage resistance
actualDamage *= DamageType.getModifier(event.getSource(), event.getEntityLiving());
if (event.getEntityLiving() instanceof EntityPlayer)
{
EntityPlayer player = (EntityPlayer) event.getEntityLiving();
if (player.getFoodStats() instanceof IFoodStatsTFC)
{
actualDamage /= ((IFoodStatsTFC) player.getFoodStats()).getHealthModifier();
float healthModifier = ((IFoodStatsTFC) player.getFoodStats()).getHealthModifier();
if (healthModifier < ConfigTFC.GENERAL.playerMinHealthModifier)
{
healthModifier = (float) ConfigTFC.GENERAL.playerMinHealthModifier;
}
if (healthModifier > ConfigTFC.GENERAL.playerMaxHealthModifier)
{
healthModifier = (float) ConfigTFC.GENERAL.playerMaxHealthModifier;
}
actualDamage /= healthModifier;
}
}
event.setAmount(actualDamage);
Expand Down Expand Up @@ -239,13 +247,6 @@ public static void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event)
// World Data (Calendar) Sync Handler
TerraFirmaCraft.getNetwork().sendTo(new PacketCalendarUpdate(), player);
}

// Replace food stats, should happen on both logical sides
FoodStats originalStats = event.player.getFoodStats();
if (!(originalStats instanceof FoodStatsTFC))
{
event.player.foodStats = new FoodStatsTFC(event.player, originalStats);
}
}

@SubscribeEvent
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/dries007/tfc/ConfigTFC.java
Expand Up @@ -107,7 +107,7 @@ public static class GeneralCFG
@Config.Comment("Modifier for how quickly the players nutrition values will decay")
@Config.RangeDouble(min = 0, max = 10)
@Config.LangKey("config." + MOD_ID + ".general.playerNutritionDecayModifier")
public double playerNutritionDecayModifier = 0.01;
public double playerNutritionDecayModifier = 0.0003;

@Config.Comment("Minimum health modifier player can obtain with low stats")
@Config.RangeDouble(min = 0.1d, max = 1d)
Expand All @@ -120,9 +120,9 @@ public static class GeneralCFG
public double playerMaxHealthModifier = 3d;

@Config.Comment("Modifier for how quickly the players becomes thirsty")
@Config.RangeDouble(min = 0, max = 10)
@Config.RangeDouble(min = 0, max = 100)
@Config.LangKey("config." + MOD_ID + ".general.playerThirstModifier")
public double playerThirstModifier = 1.0;
public double playerThirstModifier = 8.0;

@Config.Comment("Damage Source Types that will default to Slashing damage")
@Config.LangKey("config." + MOD_ID + ".general.slashingDamageSources")
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/dries007/tfc/TerraFirmaCraft.java
Expand Up @@ -135,6 +135,8 @@ public void preInit(FMLPreInitializationEvent event)
network.registerMessage(new PacketCapabilityContainerUpdate.Handler(), PacketCapabilityContainerUpdate.class, ++id, Side.CLIENT);
network.registerMessage(new PacketCalendarUpdate.Handler(), PacketCalendarUpdate.class, ++id, Side.CLIENT);
network.registerMessage(new PacketBarrelUpdate.Handler(), PacketBarrelUpdate.class, ++id, Side.CLIENT);
network.registerMessage(new PacketFoodStatsUpdate.Handler(), PacketFoodStatsUpdate.class, ++id, Side.CLIENT);
network.registerMessage(new PacketFoodStatsReplace.Handler(), PacketFoodStatsReplace.class, ++id, Side.CLIENT);

EntitiesTFC.preInit();
CalendarTFC.preInit();
Expand Down
Expand Up @@ -5,17 +5,24 @@

package net.dries007.tfc.api.capability.food;

import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemStack;
import net.minecraft.util.FoodStats;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerChangedDimensionEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent;

import net.dries007.tfc.TerraFirmaCraft;
import net.dries007.tfc.api.capability.DumbStorage;
import net.dries007.tfc.network.PacketFoodStatsReplace;
import net.dries007.tfc.util.Helpers;
import net.dries007.tfc.world.classic.CalendarTFC;

Expand Down Expand Up @@ -48,5 +55,47 @@ public static void attachItemCapabilities(AttachCapabilitiesEvent<ItemStack> eve
event.addCapability(KEY, new FoodHandler(stack.getTagCompound(), new float[] {1, 0, 0, 0, 0}, 0, 0, 1));
}
}

@SubscribeEvent
public static void onPlayerLoggedInEvent(PlayerLoggedInEvent event)
{
FoodStats originalStats = event.player.getFoodStats();
if (!(originalStats instanceof FoodStatsTFC))
{
event.player.foodStats = new FoodStatsTFC(event.player, originalStats);
if (event.player instanceof EntityPlayerMP)
{
TerraFirmaCraft.getNetwork().sendTo(new PacketFoodStatsReplace(), (EntityPlayerMP) event.player);
}
}
}

@SubscribeEvent
public static void onPlayerCloneEvent(PlayerEvent.Clone event)
{
FoodStats originalStats = event.getEntityPlayer().getFoodStats();
if (!(originalStats instanceof FoodStatsTFC))
{
event.getEntityPlayer().foodStats = new FoodStatsTFC(event.getEntityPlayer(), originalStats);
if (event.getEntityPlayer() instanceof EntityPlayerMP)
{
TerraFirmaCraft.getNetwork().sendTo(new PacketFoodStatsReplace(), (EntityPlayerMP) event.getEntityPlayer());
}
}
}

@SubscribeEvent
public static void onPlayerChangeDimensionEvent(PlayerChangedDimensionEvent event)
{
FoodStats originalStats = event.player.getFoodStats();
if (!(originalStats instanceof FoodStatsTFC))
{
event.player.foodStats = new FoodStatsTFC(event.player, originalStats);
if (event.player instanceof EntityPlayerMP)
{
TerraFirmaCraft.getNetwork().sendTo(new PacketFoodStatsReplace(), (EntityPlayerMP) event.player);
}
}
}
}
}
Expand Up @@ -9,6 +9,7 @@
import javax.annotation.ParametersAreNonnullByDefault;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.MobEffects;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemStack;
Expand All @@ -22,6 +23,8 @@

import net.dries007.tfc.ConfigTFC;
import net.dries007.tfc.Constants;
import net.dries007.tfc.TerraFirmaCraft;
import net.dries007.tfc.network.PacketFoodStatsUpdate;
import net.dries007.tfc.world.classic.CalendarTFC;

@ParametersAreNonnullByDefault
Expand Down Expand Up @@ -114,9 +117,9 @@ public void onUpdate(EntityPlayer player)
if (difficulty != EnumDifficulty.PEACEFUL)
{
// First, we check exhaustion, to decrement thirst
if (player.getFoodStats().foodExhaustionLevel >= 4.0F)
if (originalStats.foodExhaustionLevel >= 4.0F)
{
addThirst((float) ConfigTFC.GENERAL.playerThirstModifier);
addThirst(-(float) ConfigTFC.GENERAL.playerThirstModifier);
}

// Then, we decrement nutrients
Expand All @@ -136,8 +139,8 @@ public void onUpdate(EntityPlayer player)
{
if (thirst < 10f)
{
player.addPotionEffect(new PotionEffect(MobEffects.SLOWNESS, 160, 1));
player.addPotionEffect(new PotionEffect(MobEffects.MINING_FATIGUE, 160, 1));
player.addPotionEffect(new PotionEffect(MobEffects.SLOWNESS, 160, 1, false, false));
player.addPotionEffect(new PotionEffect(MobEffects.MINING_FATIGUE, 160, 1, false, false));
if (thirst <= 0f)
{
//Hurt the sourcePlayer
Expand All @@ -146,11 +149,23 @@ public void onUpdate(EntityPlayer player)
}
else if (thirst < 20f)
{
player.addPotionEffect(new PotionEffect(MobEffects.SLOWNESS, 160, 0));
player.addPotionEffect(new PotionEffect(MobEffects.MINING_FATIGUE, 160, 0));
player.addPotionEffect(new PotionEffect(MobEffects.SLOWNESS, 160, 0, false, false));
player.addPotionEffect(new PotionEffect(MobEffects.MINING_FATIGUE, 160, 0, false, false));
}
}
}

if (player instanceof EntityPlayerMP)
{
TerraFirmaCraft.getNetwork().sendTo(new PacketFoodStatsUpdate(nutrients, thirst), (EntityPlayerMP) player);
}
}

@SideOnly(Side.CLIENT)
public void onReceivePacket(float[] nutrients, float thirst)
{
System.arraycopy(nutrients, 0, this.nutrients, 0, this.nutrients.length);
this.thirst = thirst;
}

@Override
Expand Down Expand Up @@ -244,7 +259,7 @@ public float getHealthModifier()
{
totalNutrients += nutrient;
}
return totalNutrients / (MAX_PLAYER_NUTRIENTS * Nutrient.TOTAL);
return 0.2f + totalNutrients / (MAX_PLAYER_NUTRIENTS * Nutrient.TOTAL);
}

@Override
Expand Down
Expand Up @@ -39,10 +39,6 @@ public final class PlayerDataOverlay

public static PlayerDataOverlay getInstance() { return INSTANCE; }

private float maxHealth = 1000, curThirst = 100;

private PlayerDataOverlay() {}

@SubscribeEvent
public void render(RenderGameOverlayEvent.Pre event)
{
Expand All @@ -57,11 +53,13 @@ public void render(RenderGameOverlayEvent.Pre event)
}

FoodStats foodStats = player.getFoodStats();
float baseMaxHealth = 1000;
float currentThirst = 100;
if (foodStats instanceof IFoodStatsTFC)
{
IFoodStatsTFC foodStatsTFC = (IFoodStatsTFC) foodStats;
maxHealth = 20 * foodStatsTFC.getHealthModifier() * 50; //20 = 1000 HP in overlay
curThirst = foodStatsTFC.getThirst();
baseMaxHealth = 20 * foodStatsTFC.getHealthModifier() * 50; //20 = 1000 HP in overlay
currentThirst = foodStatsTFC.getThirst();
}
// This is for air to be drawn above our bars
GuiIngameForge.right_height += 10;
Expand All @@ -82,9 +80,8 @@ public void render(RenderGameOverlayEvent.Pre event)
//Draw Health
GL11.glEnable(GL11.GL_BLEND);
this.drawTexturedModalRect(mid - 91, healthRowHeight, 0, 0, 90, 10);
float maxHealth = this.maxHealth;
float curHealth = player.getHealth() * maxHealth / (float) 20;
float percentHealth = curHealth / maxHealth;
float curHealth = player.getHealth() * baseMaxHealth / (float) 20;
float percentHealth = curHealth / baseMaxHealth;
float surplusPercent = Math.max(percentHealth - 1, 0);
int uSurplus = 90;
if (percentHealth > 1) percentHealth = 1;
Expand All @@ -103,7 +100,7 @@ public void render(RenderGameOverlayEvent.Pre event)
//Draw Food and Water
float foodLevel = player.getFoodStats().getFoodLevel();
float percentFood = foodLevel / 20f;
float percentThirst = this.curThirst / 100f;
float percentThirst = currentThirst / 100f;


GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
Expand All @@ -116,7 +113,7 @@ public void render(RenderGameOverlayEvent.Pre event)
this.drawTexturedModalRect(mid + 1, healthRowHeight + 5, 90, 25, (int) (90 * percentThirst), 5);

//Draw Notifications
String healthString = ((int) curHealth) + "/" + ((int) (maxHealth));
String healthString = ((int) curHealth) + "/" + ((int) (baseMaxHealth));
fontrenderer.drawString(healthString, mid - 45 - (fontrenderer.getStringWidth(healthString) / 2), healthRowHeight + 2, Color.white.getRGB());

GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/net/dries007/tfc/network/PacketFoodStatsReplace.java
@@ -0,0 +1,54 @@
/*
* Work under Copyright. Licensed under the EUPL.
* See the project README.md and LICENSE.txt for more information.
*/

package net.dries007.tfc.network;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.FoodStats;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

import io.netty.buffer.ByteBuf;
import net.dries007.tfc.TerraFirmaCraft;
import net.dries007.tfc.api.capability.food.CapabilityFood;
import net.dries007.tfc.api.capability.food.FoodStatsTFC;
import net.dries007.tfc.api.capability.food.IFoodStatsTFC;

/**
* This packet is send to client to signal that it may need to replace the vanilla FoodStats with a TFC version
* Since all the relevant events where this is listened for are server only
* {@link CapabilityFood}
*/
public class PacketFoodStatsReplace implements IMessage
{
public PacketFoodStatsReplace() {}

@Override
public void fromBytes(ByteBuf buf) {}

@Override
public void toBytes(ByteBuf buf) {}

public static final class Handler implements IMessageHandler<PacketFoodStatsReplace, IMessage>
{
@Override
public IMessage onMessage(PacketFoodStatsReplace message, MessageContext ctx)
{
TerraFirmaCraft.getProxy().getThreadListener(ctx).addScheduledTask(() -> {
EntityPlayer player = TerraFirmaCraft.getProxy().getPlayer(ctx);
if (player != null)
{
FoodStats stats = player.getFoodStats();
if (!(stats instanceof IFoodStatsTFC))
{
player.foodStats = new FoodStatsTFC(player, stats);
}
}
});
return null;
}
}
}

0 comments on commit c32cd1f

Please sign in to comment.