Skip to content

Commit

Permalink
Player health, food and thrist + mechanics complete
Browse files Browse the repository at this point in the history
  • Loading branch information
DisasterMoo committed Jun 21, 2019
1 parent 003c3b9 commit 9970fc5
Show file tree
Hide file tree
Showing 11 changed files with 299 additions and 183 deletions.
134 changes: 130 additions & 4 deletions src/main/java/net/dries007/tfc/CommonEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Items;
import net.minecraft.init.MobEffects;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.*;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.event.entity.living.LivingEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.event.entity.player.PlayerContainerEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
Expand All @@ -33,6 +39,7 @@
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import net.dries007.tfc.api.capability.ItemStickCapability;
import net.dries007.tfc.api.capability.player.CapabilityPlayer;
Expand Down Expand Up @@ -77,6 +84,39 @@ public static void onBlockHarvestDrops(BlockEvent.HarvestDropsEvent event)
}
}

@SubscribeEvent
public static void onRightClickWater(PlayerInteractEvent event)
{
if(event.getSide() == Side.CLIENT)return;
if(!(event instanceof PlayerInteractEvent.RightClickBlock || event instanceof PlayerInteractEvent.RightClickEmpty))return;
EntityPlayer player = event.getEntityPlayer();
if(!(player.getHeldItemMainhand().isEmpty() && player.getHeldItemOffhand().isEmpty()))return; //Need both hands to drink water
RayTraceResult result = Helpers.rayTrace(event.getWorld(), player, true);
if(result != null && result.typeOfHit == RayTraceResult.Type.BLOCK)
{
BlockPos blockpos = result.getBlockPos();
IBlockState state = event.getWorld().getBlockState(blockpos);
IPlayerData cap = event.getEntityLiving().getCapability(CapabilityPlayer.CAPABILITY_PLAYER_DATA, null);
if(cap != null)
{
if(BlocksTFC.isFreshWater(state))
{
if(cap.drink(15)){
player.world.playSound(null, player.getPosition(), SoundEvents.ENTITY_GENERIC_DRINK, SoundCategory.PLAYERS, 1.0f, 1.0f);
TerraFirmaCraft.getNetwork().sendTo(new PacketPlayerDataUpdate(cap), (EntityPlayerMP)player);
}
}else if(BlocksTFC.isSaltWater(state))
{
if(cap.drink(-15)){
//Salty water drains thirst bar.
player.world.playSound(null, player.getPosition(), SoundEvents.ENTITY_GENERIC_DRINK, SoundCategory.PLAYERS, 1.0f, 1.0f);
TerraFirmaCraft.getNetwork().sendTo(new PacketPlayerDataUpdate(cap), (EntityPlayerMP)player);
}
}
}
}
}

/**
* Handler for {@link IPlaceableItem}
* To add a new placeable item effect, either implement {@link IPlaceableItem} or see {@link IPlaceableItem.Impl} for vanilla item usages
Expand Down Expand Up @@ -156,10 +196,12 @@ public static void onUseHoe(UseHoeEvent event)

}


@SubscribeEvent
public static void onLivingHurt(LivingHurtEvent event)
{
if(event.getEntityLiving() instanceof EntityPlayer)
//Starvation should not be scaled, you receive damage based on % anyway.
if(event.getEntityLiving() instanceof EntityPlayer && !(event.getSource() == DamageSource.STARVE))
{
EntityPlayer player = (EntityPlayer)event.getEntityLiving();
IPlayerData cap = event.getEntityLiving().getCapability(CapabilityPlayer.CAPABILITY_PLAYER_DATA, null);
Expand All @@ -175,11 +217,12 @@ public static void onLivingHurt(LivingHurtEvent event)
//will return 6.6667(1/3rd of 20) and apply that damage to the player. Voila :D
float finalDamage = DamageManager.rescaleDamage(damage, cap.getMaxHealth(), 20);
event.setAmount(finalDamage);
event.getSource().setDamageBypassesArmor(); //Armor calculations is already done
event.getSource().setDamageBypassesArmor(); //Armor calculation is already done
}
}
}


@SubscribeEvent
public static void attachItemCapabilities(AttachCapabilitiesEvent<ItemStack> e)
{
Expand Down Expand Up @@ -225,11 +268,11 @@ public static void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event)
// World Data (Calendar) Sync Handler
TerraFirmaCraft.getNetwork().sendTo(new PacketCalendarUpdate(), player);

// Player nutrients
// Player data
IPlayerData cap = player.getCapability(CapabilityPlayer.CAPABILITY_PLAYER_DATA, null);
if (cap != null)
{
cap.updateNutrientsFastForward();
cap.update();
TerraFirmaCraft.getNetwork().sendTo(new PacketPlayerDataUpdate(cap), player);
}
}
Expand All @@ -256,4 +299,87 @@ public static void onContainerOpen(PlayerContainerEvent.Open event)
event.getContainer().addListener(new CapabilityContainerListener(player));
}
}

//Add exhaustion on player jumping
@SubscribeEvent
public static void onPlayerJump(LivingEvent.LivingJumpEvent event)
{
World world = event.getEntity().world;

if (!world.isRemote && event.getEntity() instanceof EntityPlayer)
{
EntityPlayer player = (EntityPlayer) event.getEntity();
IPlayerData cap = event.getEntityLiving().getCapability(CapabilityPlayer.CAPABILITY_PLAYER_DATA, null);
if (cap != null && !player.isCreative()) {
if (player.isSprinting()) {
cap.addExhaustion(4.0F);
} else {
cap.addExhaustion(1.0F);
}
}
}
}


@SubscribeEvent
public static void onPlayerLivingUpdate(LivingEvent.LivingUpdateEvent event)
{
World world = event.getEntity().world;

if (!world.isRemote && event.getEntity() instanceof EntityPlayer)
{
EntityPlayer player = (EntityPlayer) event.getEntity();
if(player.ticksExisted % 20 == 0)
{
IPlayerData cap = event.getEntityLiving().getCapability(CapabilityPlayer.CAPABILITY_PLAYER_DATA, null);
if (cap != null && !player.isCreative())
{
//Add exhaustion each second of sprint, also, each 5 secs sync player data for HUD
if(player.isSprinting())cap.addExhaustion(1.5F);
if(player.ticksExisted % 100 == 0)
{
cap.update();
//Apply Debuff
if(cap.getThirst() < 10f){
player.addPotionEffect(new PotionEffect(MobEffects.SLOWNESS, 160, 1));
player.addPotionEffect(new PotionEffect(MobEffects.MINING_FATIGUE, 160, 1));
if(cap.getThirst() <= 0f){
//Hurt the player
player.attackEntityFrom(DamageSource.STARVE, 1); //5% life/5secs
}
}else if(cap.getThirst() < 40f){
player.addPotionEffect(new PotionEffect(MobEffects.SLOWNESS, 160, 0));
player.addPotionEffect(new PotionEffect(MobEffects.MINING_FATIGUE, 160, 0));
}
TerraFirmaCraft.getNetwork().sendTo(new PacketPlayerDataUpdate(cap), (EntityPlayerMP) player);
}
}
}
}
}

//Add exhaustion on player breaking blocks
@SubscribeEvent
public static void onBlockBreak(BlockEvent.BreakEvent event)
{
World world = event.getWorld();
EntityPlayer player = event.getPlayer();
BlockPos pos = event.getPos();
IBlockState state = event.getState();

if (!world.isRemote && !player.isCreative())
{
boolean canHarvestBlock = state.getBlock().canHarvestBlock(world, pos, player);

if (canHarvestBlock)
{
IPlayerData cap = player.getCapability(CapabilityPlayer.CAPABILITY_PLAYER_DATA, null);
if (cap != null) {
cap.addExhaustion(1.0F);
}
}
}
}


}
14 changes: 8 additions & 6 deletions src/main/java/net/dries007/tfc/TerraFirmaCraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ public void preInit(FMLPreInitializationEvent event)
network.registerMessage(new PacketPlaceBlockSpecial.Handler(), PacketPlaceBlockSpecial.class, ++id, Side.SERVER);
network.registerMessage(new PacketSwitchPlayerInventoryTab.Handler(), PacketSwitchPlayerInventoryTab.class, ++id, Side.SERVER);
network.registerMessage(new PacketOpenCraftingGui.Handler(), PacketOpenCraftingGui.class, ++id, Side.SERVER);
network.registerMessage(new PacketDrinkWater.Handler(), PacketDrinkWater.class, ++id, Side.SERVER);

// Received on client
network.registerMessage(new PacketAnvilUpdate.Handler(), PacketAnvilUpdate.class, ++id, Side.CLIENT);
Expand Down Expand Up @@ -160,11 +159,14 @@ public void init(FMLInitializationEvent event)
log.warn("You are not running an official build. Please do not use this and then report bugs or issues.");
}

//Enable overlay for rendering health, hunger and thirst bars
MinecraftForge.EVENT_BUS.register(PlayerDataOverlay.getInstance());
GuiIngameForge.renderHealth = false;
GuiIngameForge.renderArmor = false;
GuiIngameForge.renderExperiance = false;
if(event.getSide() == Side.CLIENT)
{
//Enable overlay for rendering health, hunger and thirst bars
MinecraftForge.EVENT_BUS.register(PlayerDataOverlay.getInstance());
GuiIngameForge.renderHealth = false;
GuiIngameForge.renderArmor = false;
GuiIngameForge.renderExperiance = false;
}

OreDictionaryHelper.init();
ItemsTFC.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface IFood extends INBTSerializable<NBTTagCompound>
* Gets the player value (only a single item, not the sum of the stack)
*
* @param stack the stack to get the player of
* @param nutrient the player in question
* @param nutrient the nutrient in question
* @return a value, current range is around 0 - 3
*/
float getNutrient(ItemStack stack, Nutrient nutrient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface IPlayerData

void addNutrient(Nutrient nutrient, float amount);

void updateNutrientsFastForward();
void update();

float getMaxHealth();

Expand All @@ -31,5 +31,22 @@ public interface IPlayerData
* Drinks fluid(ie: water) to fill or drain(salt water) thirst bar.
* @param value value to fill/drain(negative)
*/
void drink(float value);

/**
* Drinks fluid(ie: water) to fill or drain(salt water) thirst bar.
* @param value value to fill/drain(negative)
* @return true if the fluid was drank(ie: cooldown)
*/
boolean drink(float value);

float getExhaustion();

void setExhaustion(float value);

/**
* Adds an exhaustion(consumes thirst bar faster) modifier
* The exhaustion worn off over time
* @param value
*/
void addExhaustion(float value);
}
Loading

0 comments on commit 9970fc5

Please sign in to comment.