Skip to content

Commit

Permalink
Fixes before merge: Fix crash when drinking without targeting block. …
Browse files Browse the repository at this point in the history
…Fixed player going spastic when you tried to drink on cooldown. Fixed desync caused by RightClickEmpty only firing on client
  • Loading branch information
alcatrazEscapee committed Jun 27, 2019
1 parent a20ba6a commit 728c029
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 83 deletions.
65 changes: 27 additions & 38 deletions src/main/java/net/dries007/tfc/CommonEventHandler.java
Expand Up @@ -17,13 +17,11 @@
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
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 @@ -34,7 +32,6 @@
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.relauncher.Side;

import net.dries007.tfc.api.capability.ItemStickCapability;
import net.dries007.tfc.api.capability.player.CapabilityPlayer;
Expand Down Expand Up @@ -79,48 +76,16 @@ public static void onBlockHarvestDrops(BlockEvent.HarvestDropsEvent event)
}
}

@SubscribeEvent
public static void onRightClickWater(PlayerInteractEvent event)
{
if (!(event instanceof PlayerInteractEvent.RightClickBlock || event instanceof PlayerInteractEvent.RightClickEmpty))
return;
EntityPlayer player = event.getEntityPlayer();
if (!(player.getHeldItemMainhand().isEmpty()))
return;
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 (BlocksTFC.isFreshWater(state))
{
if (event.getSide().isServer() && cap != null && 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);
}
event.setCancellationResult(EnumActionResult.SUCCESS);
event.setCanceled(true);
}else if (BlocksTFC.isSaltWater(state))
{
if (event.getSide().isServer() && cap != null && 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);
}
event.setCancellationResult(EnumActionResult.SUCCESS);
event.setCanceled(true);
}
}
}

/**
* Handler for {@link IPlaceableItem}
* To add a new placeable item effect, either implement {@link IPlaceableItem} or see {@link IPlaceableItem.Impl} for vanilla item usages
* Notes:
* 1) `onBlockActivate` doesn't get called when the player is sneaking, unless doesSneakBypassUse returns true.
* 2) This event handler is fired first with the main hand as event.getStack()
* If nothing happens (as per vanilla behavior, even if this event causes something to happen),
* If nothing happens (i.e. the event is not cancelled + set cancellation result to success
* The event will fire AGAIN with the offhand and offhand stack.
*
* Also handles drinking water when right clicking an underwater block
*/
@SubscribeEvent
public static void onRightClickBlock(PlayerInteractEvent.RightClickBlock event)
Expand All @@ -141,6 +106,30 @@ public static void onRightClickBlock(PlayerInteractEvent.RightClickBlock event)
}
event.setCancellationResult(EnumActionResult.SUCCESS);
event.setCanceled(true);
return;
}
}

// Try to drink water
if (stack.isEmpty())
{
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);
boolean isFreshWater = BlocksTFC.isFreshWater(state), isSaltWater = BlocksTFC.isSaltWater(state);
if (cap != null && ((isFreshWater && cap.drink(15)) || (isSaltWater && cap.drink(-5))))
{
if (!world.isRemote)
{
player.world.playSound(null, player.getPosition(), SoundEvents.ENTITY_GENERIC_DRINK, SoundCategory.PLAYERS, 1.0f, 1.0f);
TerraFirmaCraft.getNetwork().sendTo(new PacketPlayerDataUpdate(cap), (EntityPlayerMP) player);
}
event.setCancellationResult(EnumActionResult.SUCCESS);
event.setCanceled(true);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/dries007/tfc/ConfigTFC.java
Expand Up @@ -112,7 +112,7 @@ public static class GeneralCFG
@Config.Comment("Minimum health modifier player can obtain with low stats")
@Config.RangeDouble(min = 0.1d, max = 1d)
@Config.LangKey("config." + MOD_ID + ".general.playerMinHealthModifier")
public double playerMinHealthModifier = 0.5d;
public double playerMinHealthModifier = 0.2d;

@Config.Comment("Maximum health modifier player can obtain with high stats")
@Config.RangeDouble(min = 1d, max = 5d)
Expand Down
Expand Up @@ -5,25 +5,36 @@

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

import javax.annotation.Nonnull;

import net.minecraft.entity.player.EntityPlayer;

import net.dries007.tfc.util.agriculture.Nutrient;

public interface IPlayerData
{
float getNutrient(Nutrient nutrient);
float getNutrient(@Nonnull Nutrient nutrient);

float[] getNutrients();

void setNutrients(float[] nutrients);

void setNutrient(Nutrient nutrient, float amount);
void setNutrient(@Nonnull Nutrient nutrient, float amount);

void addNutrient(Nutrient nutrient, float amount);
void addNutrient(@Nonnull Nutrient nutrient, float amount);

/**
* Called when a player loads in the world.
* Updates nutrients assuming that no time had passed between player log out + log in
*/
void updateTicksFastForward();

void onUpdate(EntityPlayer player);
/**
* Called during player tick, used to calculate thirst
*
* @param player the player that is ticking
*/
void onUpdate(@Nonnull EntityPlayer player);

float getHealthModifier();

Expand Down
Expand Up @@ -54,7 +54,7 @@ public PlayerDataHandler(@Nullable NBTTagCompound nbt)
}

@Override
public float getNutrient(Nutrient nutrient)
public float getNutrient(@Nonnull Nutrient nutrient)
{
return nutrients[nutrient.ordinal()];
}
Expand All @@ -72,7 +72,7 @@ public void setNutrients(float[] nutrients)
}

@Override
public void setNutrient(Nutrient nutrient, float amount)
public void setNutrient(@Nonnull Nutrient nutrient, float amount)
{
if (amount < MIN_PLAYER_NUTRIENTS)
{
Expand All @@ -89,7 +89,7 @@ else if (amount > MAX_PLAYER_NUTRIENTS)
}

@Override
public void addNutrient(Nutrient nutrient, float amount)
public void addNutrient(@Nonnull Nutrient nutrient, float amount)
{
float newAmount = nutrients[nutrient.ordinal()] + amount;
setNutrient(nutrient, newAmount);
Expand Down Expand Up @@ -157,8 +157,14 @@ public float getThirst()
public void setThirst(float value)
{
this.thirst = value;
if (this.thirst > 100) this.thirst = 100;
if (this.thirst < 0) this.thirst = 0;
if (this.thirst > 100)
{
this.thirst = 100;
}
if (this.thirst < 0)
{
this.thirst = 0;
}
}

@Override
Expand Down
@@ -1,3 +1,8 @@
/*
* Work under Copyright. Licensed under the EUPL.
* See the project README.md and LICENSE.txt for more information.
*/

package net.dries007.tfc.client.gui.overlay;

import java.awt.*;
Expand All @@ -18,49 +23,52 @@
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import net.dries007.tfc.api.capability.player.CapabilityPlayer;
import net.dries007.tfc.api.capability.player.IPlayerData;
import net.dries007.tfc.util.DamageManager;

import static net.dries007.tfc.api.util.TFCConstants.MOD_ID;

@SideOnly(Side.CLIENT)
public final class PlayerDataOverlay
{
private static final ResourceLocation ICONS = new ResourceLocation(MOD_ID, "textures/gui/overlay/icons.png");
private static final PlayerDataOverlay INSTANCE = new PlayerDataOverlay();

public static PlayerDataOverlay getInstance() { return INSTANCE; }
private FontRenderer fontrenderer = null;

private float maxHealth = 1000, curThirst = 100;

private PlayerDataOverlay() {}

public void setMaxHealth(float value) { this.maxHealth = value; }

public void setCurThirst(float value) { this.curThirst = value; }

@SubscribeEvent
public void render(RenderGameOverlayEvent.Pre event)
{
Minecraft mc = Minecraft.getMinecraft();
EntityPlayer player = mc.player.inventory.player;
GuiIngameForge.renderFood = false;

// We check for crosshairs just because it's always drawn and is before air bar
if (event.getType() != ElementType.CROSSHAIRS)
{
return;
IPlayerData capb = player.getCapability(CapabilityPlayer.CAPABILITY_PLAYER_DATA, null);
if (capb != null)
}

IPlayerData playerData = player.getCapability(CapabilityPlayer.CAPABILITY_PLAYER_DATA, null);
if (playerData != null)
{
maxHealth = 20 * capb.getHealthModifier() * 50; //20 = 1000 HP in overlay
curThirst = capb.getThirst();
maxHealth = 20 * playerData.getHealthModifier() * 50; //20 = 1000 HP in overlay
curThirst = playerData.getThirst();
}
// This is for air to be drawn above our bars
GuiIngameForge.right_height += 10;

ScaledResolution sr = event.getResolution();

fontrenderer = mc.fontRenderer;
FontRenderer fontrenderer = mc.fontRenderer;

int healthRowHeight = sr.getScaledHeight() - 40;
int armorRowHeight = healthRowHeight - 10;
Expand Down Expand Up @@ -117,8 +125,7 @@ public void render(RenderGameOverlayEvent.Pre event)
//Draw experience bar when not riding anything, riding a non-living entity such as a boat/minecart, or riding a pig.
if (!(player.getRidingEntity() instanceof EntityLiving))
{
int cap = 0;
cap = player.xpBarCap();
int cap = player.xpBarCap();
int left = mid - 91;

if (cap > 0)
Expand All @@ -133,8 +140,7 @@ public void render(RenderGameOverlayEvent.Pre event)

if (player.experienceLevel > 0)
{
boolean flag1 = false;
int color = flag1 ? 16777215 : 8453920;
int color = 8453920;
String text = Integer.toString(player.experienceLevel);
int x = (sr.getScaledWidth() - fontrenderer.getStringWidth(text)) / 2;
int y = sr.getScaledHeight() - 30;
Expand Down Expand Up @@ -169,17 +175,18 @@ public void render(RenderGameOverlayEvent.Pre event)
}
}

@SuppressWarnings("PointlessArithmeticExpression")
public void drawTexturedModalRect(float xCoord, float yCoord, int minU, int minV, int maxU, int maxV)
{
float f = 0.00390625F;
float f1 = 0.00390625F;
float textureScaleU = 0.00390625F;
float textureScaleV = 0.00390625F;
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder vb = tessellator.getBuffer();
vb.begin(7, DefaultVertexFormats.POSITION_TEX);
vb.pos(xCoord + 0.0F, yCoord + maxV, 0).tex((minU + 0) * f, (minV + maxV) * f1).endVertex();
vb.pos(xCoord + maxU, yCoord + maxV, 0).tex((minU + maxU) * f, (minV + maxV) * f1).endVertex();
vb.pos(xCoord + maxU, yCoord + 0.0F, 0).tex((minU + maxU) * f, (minV + 0) * f1).endVertex();
vb.pos(xCoord + 0.0F, yCoord + 0.0F, 0).tex((minU + 0) * f, (minV + 0) * f1).endVertex();
vb.pos(xCoord + 0.0F, yCoord + maxV, 0).tex((minU + 0) * textureScaleU, (minV + maxV) * textureScaleV).endVertex();
vb.pos(xCoord + maxU, yCoord + maxV, 0).tex((minU + maxU) * textureScaleU, (minV + maxV) * textureScaleV).endVertex();
vb.pos(xCoord + maxU, yCoord + 0.0F, 0).tex((minU + maxU) * textureScaleU, (minV + 0) * textureScaleV).endVertex();
vb.pos(xCoord + 0.0F, yCoord + 0.0F, 0).tex((minU + 0) * textureScaleU, (minV + 0) * textureScaleV).endVertex();
tessellator.draw();
}
}
23 changes: 8 additions & 15 deletions src/main/java/net/dries007/tfc/util/Helpers.java
Expand Up @@ -123,21 +123,14 @@ else if (current.getBlock() instanceof BlockRockVariant)
*/
public static RayTraceResult rayTrace(World worldIn, EntityPlayer playerIn, boolean useLiquids)
{
float f = playerIn.rotationPitch;
float f1 = playerIn.rotationYaw;
double d0 = playerIn.posX;
double d1 = playerIn.posY + (double) playerIn.getEyeHeight();
double d2 = playerIn.posZ;
Vec3d vec3d = new Vec3d(d0, d1, d2);
float f2 = MathHelper.cos(-f1 * 0.017453292F - (float) Math.PI);
float f3 = MathHelper.sin(-f1 * 0.017453292F - (float) Math.PI);
float f4 = -MathHelper.cos(-f * 0.017453292F);
float f5 = MathHelper.sin(-f * 0.017453292F);
float f6 = f3 * f4;
float f7 = f2 * f4;
double d3 = playerIn.getEntityAttribute(EntityPlayer.REACH_DISTANCE).getAttributeValue();
Vec3d vec3d1 = vec3d.add((double) f6 * d3, (double) f5 * d3, (double) f7 * d3);
return worldIn.rayTraceBlocks(vec3d, vec3d1, useLiquids, !useLiquids, false);
Vec3d playerVec = new Vec3d(playerIn.posX, playerIn.posY + playerIn.getEyeHeight(), playerIn.posZ);
float cosYaw = MathHelper.cos(-playerIn.rotationYaw * 0.017453292F - (float) Math.PI);
float sinYaw = MathHelper.sin(-playerIn.rotationYaw * 0.017453292F - (float) Math.PI);
float cosPitch = -MathHelper.cos(-playerIn.rotationPitch * 0.017453292F);
float sinPitch = MathHelper.sin(-playerIn.rotationPitch * 0.017453292F);
double reachDistance = playerIn.getEntityAttribute(EntityPlayer.REACH_DISTANCE).getAttributeValue();
Vec3d targetVec = playerVec.add((sinYaw * cosPitch) * reachDistance, sinPitch * reachDistance, (cosYaw * cosPitch) * reachDistance);
return worldIn.rayTraceBlocks(playerVec, targetVec, useLiquids, !useLiquids, false);
}

public static boolean containsAnyOfCaseInsensitive(Collection<String> input, String... items)
Expand Down

0 comments on commit 728c029

Please sign in to comment.