Jump to content

Doing something when the player's foodlevel changes


ItsAMysteriousYT

Recommended Posts

Hey there,i am trying to add 5 to a variable in my extendedPlayerProps when the player looses some food. For this i look at player.getFoodStats.getFoodLevel&player.getFoodStats().getPrevFoodLevel. So now i check if the prevLevel is larger then the current foodlevel and add 5 to the value. But somehow either the value does only change on the server but not on the client (i saw that prevfoodlevel is @sideonly(side.CLIENT) so i change the value with packethandling under a world.isRemote-check) or it is changed instantly, but i checked - the prevFoodLevel only is changed when the player looses or gets some food - so when i substract the current foodlevel from the previous the sys.out looks like 0.0,0.0,0.0,0.0,0.0,0.0,-1,-1,0.0...).

So - this is my code:

The updatemehtod in my extendedplayerProperties:

 

 

if (world.isRemote)

if (world.isRemote)

if (player.getFoodStats().getPrevFoodLevel() - player.getFoodStats().getFoodLevel() == 1D) {

System.out.println(player.getFoodStats().getPrevFoodLevel()-player.getFoodStats.getFoodLevel());

this.poop_value += 5; //This is so the client value changes

RealLifeMod.network.sendToServer(new UpdateToiletPacket(this.poop_value

+ ((player.getFoodStats().getPrevFoodLevel() - player.getFoodStats().getFoodLevel()) * 5)));

}

 

 

 

 

The packet:

 

 

package itsamysterious.mods.reallifemod.core.packets;

 

import io.netty.buffer.ByteBuf;

import net.minecraftforge.fml.common.network.simpleimpl.IMessage;

 

public class UpdateToiletPacket implements IMessage {

double newpoopvalue;

 

public UpdateToiletPacket() {

}

 

public UpdateToiletPacket(double newpoopvalue) {

this.newpoopvalue = newpoopvalue;

}

 

@Override

public void fromBytes(ByteBuf buf) {

this.newpoopvalue = buf.readDouble();

}

 

@Override

public void toBytes(ByteBuf buf) {

buf.writeDouble(this.newpoopvalue);

}

 

}

 

 

 

 

And the packethandler:

 

 

package itsamysterious.mods.reallifemod.core.packets;

 

import itsamysterious.mods.reallifemod.core.lifesystem.RLMPlayerProps;

import net.minecraft.util.IThreadListener;

import net.minecraft.world.World;

import net.minecraft.world.WorldServer;

import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;

import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

 

public class UpdateToiletHandler implements IMessageHandler<UpdateToiletPacket, UpdateToiletPacket>{

@Override

public UpdateToiletPacket onMessage(final UpdateToiletPacket message, final MessageContext ctx) {

IThreadListener mainThread = (WorldServer) ctx.getServerHandler().playerEntity.worldObj;

mainThread.addScheduledTask(new Runnable() {

@Override

public void run() {

World world = ctx.getServerHandler().playerEntity.worldObj;

RLMPlayerProps.get(ctx.getServerHandler().playerEntity).poop_value = message.newpoopvalue;

System.out.println("New poopvalue is"+message.newpoopvalue);

 

}

});

 

return null;

}

}

 

 

 

 

Btw - YES i registered the packet and the handler properly(To side.SERVER cuz it is send to the serverSide) and the props are working too (except from the saving,but thats not necessary atm ).

 

Hopefully this time i gave enough and detailed information about what i try, what is not working and all that :D

 

Merry Christmas & Greetings - ItsAMysterious

Link to comment
Share on other sites

Well.  A couple things.

 

1) Why are you doing this:

RealLifeMod.network.sendToServer(new UpdateToiletPacket(this.poop_value
                  + ((player.getFoodStats().getPrevFoodLevel() - player.getFoodStats().getFoodLevel()) * 5)));

Why not:

RealLifeMod.network.sendToServer(new UpdateToiletPacket(this.poop_value));

 

Second, same code, you're sending "this.poop" (5 more than last time) plus the change that occurred times five which as your trace statement shows, is negative.  So you're actually sending a packet that contains the value "0."

 

Then we get here:

RLMPlayerProps.get(ctx.getServerHandler().playerEntity).poop_value = message.newpoopvalue;

 

So we set the current poop value to whatever the packet said it should be, which is 0.  Good jorb, you let the client dictate the server state and it broke.  Assume the client's data always lies.  Only trust it for keyboard and mouse input.

 

So.  Here's what you should do:

 

In your IEEP store the previous food value yourself.  You can do that, its easy.  Compare with the saved value, then store the new value.  If it changes on the server, increment your poop value.  Send this new value to the client.  Oh, right, IEEP already synchronizes.  Now you're done!

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Cuz the prevFoodLevel only exists on ClientSide...

prevFoodLevel should exist AT LEAST on server and MAYBE on client (only if you need it). Not other way around.

 

Read up about enum. You can save it as String or as byte/int. Then to read it you use Enum.valueOf (or other methods).

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

When i save the previous foodlevel to the IEEP's, do i have to add a client check to it? Cuz the prevFoodLevel only exists on ClientSide... Also, do you know how to save a custom enum to NBT?

 

For fuck's sake, you're making your own variable called

myPreviousFoodLevel

.  After you do all of your checks, you set

myPreviousFoodLevel = player.getFoodStats().getFoodLevel()

.  Done, now it's the previous tick's food level, no client side method bullshit required.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Thank you all - i solved it :) Now the only thing that needs to be solved is that the IEEP's do not persist through world save :/

So - here is the complete IEEP class:

 

 

 

package itsamysterious.mods.reallifemod.core.lifesystem;

 

import com.sun.media.jfxmedia.events.PlayerEvent;

 

import itsamysterious.mods.reallifemod.RealLifeMod;

import itsamysterious.mods.reallifemod.core.blocks.tiles.TileEntity_Electric;

import itsamysterious.mods.reallifemod.core.gui.GuiGamestart;

import itsamysterious.mods.reallifemod.core.gui.GuiModInit;

import itsamysterious.mods.reallifemod.core.lifesystem.enums.EnumFeelings;

import itsamysterious.mods.reallifemod.core.lifesystem.enums.EnumGender;

import itsamysterious.mods.reallifemod.core.packets.UpdateToiletHandler;

import itsamysterious.mods.reallifemod.core.packets.UpdateToiletPacket;

import net.minecraft.block.material.Material;

import net.minecraft.client.Minecraft;

import net.minecraft.client.entity.EntityPlayerSP;

import net.minecraft.client.particle.EntityDropParticleFX;

import net.minecraft.entity.Entity;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.network.NetworkManager;

import net.minecraft.network.Packet;

import net.minecraft.network.play.server.S35PacketUpdateTileEntity;

import net.minecraft.potion.Potion;

import net.minecraft.potion.PotionEffect;

import net.minecraft.util.ChatComponentText;

import net.minecraft.util.EnumChatFormatting;

import net.minecraft.util.FoodStats;

import net.minecraft.world.World;

import net.minecraftforge.common.IExtendedEntityProperties;

 

public class RLMPlayerProps implements IExtendedEntityProperties {

 

public static final String EXT_PROP_NAME = "RealLifeProperties";

public TileEntity_Electric lastTile;

 

public EntityPlayer player;

public World world;

 

/** Health */

public double energy;

public double stamina;

public EnumGender gender;

 

/** Bodyparts */

public float Head;

public float Chest;

public float Arm_Right;

public float Arm_Left;

public float Leg_Right;

public float Leg_Left;

public float foot_Right;

public float foot_Left;

 

/** Economy and Community */

public String name;

public String surname;

public float money;

 

public boolean doneTutorial;

public String partner;

 

/** Counters */

public double water; // Holds the current saturation of water in the player

 

public double pee_value; // How much the player has to pee

public double poop_value; // How much the player has to defecate

 

public int timeWaterless;

public int waterlowmessagetime;

 

/** Temporary variables */

public boolean pooping; // Determines wether the player should poop or not

public boolean peeing; // Determines wether the player should poop or not

public EnumFeelings feeling;

private int updatetick;

private double prevFoodLevel;

 

public RLMPlayerProps() {

}

 

public RLMPlayerProps(EntityPlayer player) {

this.player = player;

}

 

public static final void register(EntityPlayer player) {

player.registerExtendedProperties(RLMPlayerProps.EXT_PROP_NAME, new RLMPlayerProps(player));

}

 

public static final RLMPlayerProps get(EntityPlayer player) {

return (RLMPlayerProps) player.getExtendedProperties(EXT_PROP_NAME);

}

 

public void circleOfLife() {

updatetick++;

world = player.worldObj;

if (world.isRemote && updatetick > 2) {

if (this.doneTutorial == false) {

this.doneTutorial = true;

// Minecraft.getMinecraft().displayGuiScreen(new GuiModInit());

}

}

 

if (player != null && !player.capabilities.isCreativeMode) {

updateEffects();

}

if (world.isRemote) {

if (this.prevFoodLevel != this.player.getFoodStats().getFoodLevel()) {

System.out.println("Foodlevel now is " + this.player.getFoodStats().getFoodLevel() + "!");

if(prevFoodLevel>this.player.getFoodStats().getFoodLevel()){

poop_value+=2.5;

}

this.prevFoodLevel = this.player.getFoodStats().getFoodLevel();

}

 

}

 

if (this.prevFoodLevel > this.player.getFoodStats().getFoodLevel()) {

this.poop_value += 2.5;

System.out.println("Increasingfoodlevel by 5!");

}

 

if (peeing) {

pee_value--;

water -= 2.5;

}

 

}

 

private void updateEffects() {

if (getWater(player) < 40 && getWater(player) > 10 && waterlowmessagetime % 200 == 0) {

player.addChatComponentMessage(LinesHelper.ThirstWarning);

 

}

if (getWater(player) < 10 && getWater(player) > 0.1) {

player.addChatComponentMessage(LinesHelper.ThirstWarning2);

player.addPotionEffect(new PotionEffect(Potion.confusion.getId(), 100));

} else if (player.getActivePotionEffect(Potion.confusion) != null) {

player.removePotionEffect(Potion.confusion.id);

}

 

if (getWater(player) < 0.1) {

player.addPotionEffect(new PotionEffect(Potion.weakness.getId(), 100));

timeWaterless++;

if (timeWaterless == 200) {

player.addChatComponentMessage(

new ChatComponentText("Look at the empty bar at the top. It will hold your current amount of "

+ EnumChatFormatting.AQUA + "Water" + EnumChatFormatting.RESET + "later on. "));

// player.addChatComponentMessage(LinesHelper.DyingOfThirst);

// player.setHealth(player.getHealth() - 1);

}

 

}

 

// poop_value = (100/19)*(19-player.getFoodStats().getFoodLevel());

if (pee_value > 50) {

player.addPotionEffect(new PotionEffect(Potion.digSlowdown.getId(), 1));

} else {

player.removePotionEffect(Potion.digSlowdown.getId());

 

}

 

CalculateFeelingFromSurroundings(this.player);

 

}

 

private void CalculateFeelingFromSurroundings(EntityPlayer player) {

World world = player.worldObj;

 

if (world.provider.isDaytime()) {

feeling = EnumFeelings.Happy;

}

 

if (!world.provider.isDaytime()) {

feeling = EnumFeelings.Tense;

}

 

}

 

@Override

public void saveNBTData(NBTTagCompound compound) {

NBTTagCompound properties = new NBTTagCompound();

 

// Saving the players name,gender & health

properties.setString("Name", this.name);

properties.setString("Surname", this.surname);

properties.setString("Gender", EnumGender.toString(this.gender));

properties.setDouble("Waterlevel", this.water);

properties.setDouble("Peevalue", this.pee_value);

properties.setDouble("Poopvalue", this.poop_value);

properties.setDouble("Energy", this.energy);

properties.setFloat("Money", this.money);

 

// Saving all the counters

properties.setDouble("Time_Waterless", this.timeWaterless);

properties.setDouble("WaterMessage_Time", this.waterlowmessagetime);

properties.setBoolean("Tutorial_Done", this.doneTutorial);

 

// Saving the players cars -> for-loop through list of car entity-ids

 

compound.setTag(EXT_PROP_NAME, properties);

System.out.println("Gender is: " + EnumGender.toString(gender));

System.out.println("Succesfully saved the props!");

}

 

@Override

public void loadNBTData(NBTTagCompound compound) {

NBTTagCompound tag = compound.getCompoundTag(EXT_PROP_NAME);

System.out.println("Succesfully loaded tag");

// Loading the players name,gender & health

this.name = tag.getString("Name");

System.out.println("Setting name to:"+tag.getString("Name"));

 

this.surname = tag.getString("Surname");

this.gender = EnumGender.getFromString(tag.getString("Gender"));

this.water = tag.getDouble("Waterlevel");

this.pee_value = tag.getDouble("Peevalue");

this.poop_value = tag.getDouble("Poopvalue");

this.energy = tag.getDouble("Energy");

this.money = tag.getFloat("Money");

 

// Saving all the counters

this.timeWaterless = tag.getInteger("Time_Waterless");

this.waterlowmessagetime = tag.getInteger("WaterMessage_Time");

this.doneTutorial = tag.getBoolean("Tutorial_Done");

 

}

 

@Override

public void init(Entity entity, World world) {

if (entity instanceof EntityPlayer) {

player = (EntityPlayer) entity;

water = 2000;

poop_value = 0;

pee_value = 0;

}

}

 

public void copy(RLMPlayerProps props, EntityPlayer player) {

NBTTagCompound compound = new NBTTagCompound();

props.saveNBTData(compound);

RLMPlayerProps.get(player).loadNBTData(compound);

 

}

 

public void setGender(EnumGender gender) {

this.gender = gender;

}

 

public static final String getFullname(EntityPlayerSP thePlayer) {

return get(thePlayer).getName() + " " + get(thePlayer).getSurname();

}

 

public String getName() {

return this.name;

}

 

public void setName(String name) {

this.name = name;

}

 

public String getSurname() {

return surname;

}

 

public void setSurname(String surname) {

this.surname = surname;

}

 

public static double getWater(EntityPlayer player) {

return RLMPlayerProps.get(player).water;

}

 

public static double getPee_Value(EntityPlayer player) {

return RLMPlayerProps.get(player).pee_value;

}

 

public static double getPoop_Value(EntityPlayer player) {

return RLMPlayerProps.get(player).poop_value;

}

 

public void setWater(double water) {

this.water = water;

}

 

}

 

 

 

 

And the method where i apply them in the event handler:

@SubscribeEvent

public void onEntityConstructing(EntityConstructing event) {

 

if (event.entity instanceof EntityPlayer)

if (RLMPlayerProps.get((EntityPlayer) event.entity) == null) {

RLMPlayerProps.register((EntityPlayer) event.entity);

System.out.println("Succesfully registered RLMProps");

 

}

 

}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.