The IEEP is vanilla. My packets work for other things, so I'm assuming that's not what's null.
I used this tutorial before and got it working but lost the code months ago so I'm guessing something's changed?
Nothing's changed, and IExtendedEntityProperties is NOT vanilla - it's added by Forge so modders can add additional properties to vanilla entities (such as players). You'll need to show some code.
Nothing's changed, and IExtendedEntityProperties is NOT vanilla - it's added by Forge so modders can add additional properties to vanilla entities (such as players). You'll need to show some code.
Depends - did you determine what, exactly, is causing your render overlay method to return before it finishes? You have a couple of 'return' statements in there, and I asked you some questions a few posts back that you have yet to answer.
Once you can answer those questions, you will know what part of your code is failing and thus which part to post (you'll probably even be able to figure out how to fix it at that point).
Don't know how to figure out the answers to those questions? Use your IDEs debugger or, if you don't know how (and can't Google it), put System.out.println statements in your render overlay method that print out the values of each variable in question.
I can't print out any variables because 'props' is null. I don't understand why. I've tried using both packets and the datawatcher.
Am I just missing some stupid little thing?
public class ExtendedPlayer implements IExtendedEntityProperties
{
public final static String EXT_PROP_NAME = "ExtendedPlayer";
public static final int SANITYWATCHER = 20;
public static final void register(EntityPlayer player)
{
player.registerExtendedProperties(ExtendedPlayer.EXT_PROP_NAME, new ExtendedPlayer(player));
}
public static final ExtendedPlayer get(EntityPlayer player)
{
return (ExtendedPlayer) player.getExtendedProperties(EXT_PROP_NAME);
}
@Override
public void saveNBTData(NBTTagCompound compound)
{
NBTTagCompound properties = new NBTTagCompound();
public final void replenishSanity()
{
this.player.getDataWatcher().updateObject(SANITYWATCHER, this.maxSanity);
}
public final int getCurrentSanity()
{
return this.player.getDataWatcher().getWatchableObjectInt(SANITYWATCHER);
}
public final int getMaxSanity()
{
return maxSanity;
}
public void addCurrentSanity(int amount)
{
int currentSanity = this.player.getDataWatcher().getWatchableObjectInt(SANITYWATCHER);
this.player.getDataWatcher().updateObject(SANITYWATCHER, currentSanity + (amount < this.maxSanity ? amount : this.maxSanity));
}
public void setMaxSanity(int amount)
{
this.maxSanity = (amount > 0 ? amount : 0);
this.sync();
}
public void addMaxSanity(int amount)
{
this.maxSanity = this.maxSanity + (amount);
this.sync();
}
public final void sync()
{
ByteArrayOutputStream bos = new ByteArrayOutputStream(8);
DataOutputStream outputStream = new DataOutputStream(bos);
Did you register your IEEP class? If not, it will return null and your render code will not get called.
I.e., if ExtendedPlayer.get(player) returns null, the problem is very likely that you did not register your IEEP during EntityConstructing event. Given that, I would need to see your event handler code and registration (registration is usually done in the main mod file during one of the FML events).
MissApocalypse joined the game
[14:11:11] [Server thread/ERROR] [FML]: FMLIndexedMessageCodec exception caught
io.netty.handler.codec.EncoderException: java.lang.IndexOutOfBoundsException: readerIndex(0) + length(4) exceeds writerIndex(1): UnpooledHeapByteBuf(ridx: 0, widx: 1, cap: 256)
at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:107) ~[MessageToMessageEncoder.class:?]
at io.netty.handler.codec.MessageToMessageCodec.write(MessageToMessageCodec.java:116) ~[MessageToMessageCodec.class:?]
at io.netty.channel.DefaultChannelHandlerContext.invokeWrite(DefaultChannelHandlerContext.java:644) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.write(DefaultChannelHandlerContext.java:698) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.writeAndFlush(DefaultChannelHandlerContext.java:688) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.writeAndFlush(DefaultChannelHandlerContext.java:717) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelPipeline.writeAndFlush(DefaultChannelPipeline.java:893) ~[DefaultChannelPipeline.class:?]
at io.netty.channel.AbstractChannel.writeAndFlush(AbstractChannel.java:239) ~[AbstractChannel.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper.sendToServer(SimpleNetworkWrapper.java:236) [SimpleNetworkWrapper.class:?]
at apocalypse.fandomcraft.player.ExtendedPlayer.sync(ExtendedPlayer.java:129) [ExtendedPlayer.class:?]
at apocalypse.fandomcraft.player.ExtendedPlayerEvents.onEntityJoinWorld(ExtendedPlayerEvents.java:56) [ExtendedPlayerEvents.class:?]
at cpw.mods.fml.common.eventhandler.ASMEventHandler_20_ExtendedPlayerEvents_onEntityJoinWorld_EntityJoinWorldEvent.invoke(.dynamic) [?:?]
at cpw.mods.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:54) [ASMEventHandler.class:?]
at cpw.mods.fml.common.eventhandler.EventBus.post(EventBus.java:138) [EventBus.class:?]
at net.minecraft.world.World.spawnEntityInWorld(World.java:1503) [World.class:?]
at net.minecraft.server.management.ServerConfigurationManager.playerLoggedIn(ServerConfigurationManager.java:309) [ServerConfigurationManager.class:?]
at net.minecraft.server.management.ServerConfigurationManager.initializeConnectionToPlayer(ServerConfigurationManager.java:170) [ServerConfigurationManager.class:?]
at cpw.mods.fml.common.network.handshake.NetworkDispatcher.completeServerSideConnection(NetworkDispatcher.java:173) [NetworkDispatcher.class:?]
at cpw.mods.fml.common.network.handshake.NetworkDispatcher.completeHandshake(NetworkDispatcher.java:446) [NetworkDispatcher.class:?]
at cpw.mods.fml.common.network.internal.HandshakeCompletionHandler.channelRead0(HandshakeCompletionHandler.java:17) [HandshakeCompletionHandler.class:?]
at cpw.mods.fml.common.network.internal.HandshakeCompletionHandler.channelRead0(HandshakeCompletionHandler.java:11) [HandshakeCompletionHandler.class:?]
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:98) [SimpleChannelInboundHandler.class:?]
at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323) [DefaultChannelHandlerContext.class:?]
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103) [MessageToMessageDecoder.class:?]
at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111) [MessageToMessageCodec.class:?]
at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785) [DefaultChannelPipeline.class:?]
at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:169) [EmbeddedChannel.class:?]
at cpw.mods.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:86) [FMLProxyPacket.class:?]
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:241) [NetworkManager.class:?]
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182) [NetworkSystem.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:726) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:614) [MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) [IntegratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:485) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:752) [MinecraftServer$2.class:?]
Caused by: java.lang.IndexOutOfBoundsException: readerIndex(0) + length(4) exceeds writerIndex(1): UnpooledHeapByteBuf(ridx: 0, widx: 1, cap: 256)
at io.netty.buffer.AbstractByteBuf.checkReadableBytes(AbstractByteBuf.java:1160) ~[AbstractByteBuf.class:?]
at io.netty.buffer.AbstractByteBuf.readInt(AbstractByteBuf.java:611) ~[AbstractByteBuf.class:?]
at apocalypse.fandomcraft.network.altpacket.APacketSanity$SanityMesage.toBytes(APacketSanity.java:50) ~[APacketSanity$SanityMesage.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleIndexedCodec.encodeInto(SimpleIndexedCodec.java:11) ~[SimpleIndexedCodec.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleIndexedCodec.encodeInto(SimpleIndexedCodec.java:7) ~[SimpleIndexedCodec.class:?]
at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.encode(FMLIndexedMessageToMessageCodec.java:51) ~[FMLIndexedMessageToMessageCodec.class:?]
at io.netty.handler.codec.MessageToMessageCodec$1.encode(MessageToMessageCodec.java:67) ~[MessageToMessageCodec$1.class:?]
at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:89) ~[MessageToMessageEncoder.class:?]
... 37 more
[14:11:11] [Server thread/ERROR] [FML]: SimpleChannelHandlerWrapper exception
io.netty.handler.codec.EncoderException: java.lang.IndexOutOfBoundsException: readerIndex(0) + length(4) exceeds writerIndex(1): UnpooledHeapByteBuf(ridx: 0, widx: 1, cap: 256)
at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:107) ~[MessageToMessageEncoder.class:?]
at io.netty.handler.codec.MessageToMessageCodec.write(MessageToMessageCodec.java:116) ~[MessageToMessageCodec.class:?]
at io.netty.channel.DefaultChannelHandlerContext.invokeWrite(DefaultChannelHandlerContext.java:644) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.write(DefaultChannelHandlerContext.java:698) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.writeAndFlush(DefaultChannelHandlerContext.java:688) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.writeAndFlush(DefaultChannelHandlerContext.java:717) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelPipeline.writeAndFlush(DefaultChannelPipeline.java:893) ~[DefaultChannelPipeline.class:?]
at io.netty.channel.AbstractChannel.writeAndFlush(AbstractChannel.java:239) ~[AbstractChannel.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper.sendToServer(SimpleNetworkWrapper.java:236) [SimpleNetworkWrapper.class:?]
at apocalypse.fandomcraft.player.ExtendedPlayer.sync(ExtendedPlayer.java:129) [ExtendedPlayer.class:?]
at apocalypse.fandomcraft.player.ExtendedPlayerEvents.onEntityJoinWorld(ExtendedPlayerEvents.java:56) [ExtendedPlayerEvents.class:?]
at cpw.mods.fml.common.eventhandler.ASMEventHandler_20_ExtendedPlayerEvents_onEntityJoinWorld_EntityJoinWorldEvent.invoke(.dynamic) [?:?]
at cpw.mods.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:54) [ASMEventHandler.class:?]
at cpw.mods.fml.common.eventhandler.EventBus.post(EventBus.java:138) [EventBus.class:?]
at net.minecraft.world.World.spawnEntityInWorld(World.java:1503) [World.class:?]
at net.minecraft.server.management.ServerConfigurationManager.playerLoggedIn(ServerConfigurationManager.java:309) [ServerConfigurationManager.class:?]
at net.minecraft.server.management.ServerConfigurationManager.initializeConnectionToPlayer(ServerConfigurationManager.java:170) [ServerConfigurationManager.class:?]
at cpw.mods.fml.common.network.handshake.NetworkDispatcher.completeServerSideConnection(NetworkDispatcher.java:173) [NetworkDispatcher.class:?]
at cpw.mods.fml.common.network.handshake.NetworkDispatcher.completeHandshake(NetworkDispatcher.java:446) [NetworkDispatcher.class:?]
at cpw.mods.fml.common.network.internal.HandshakeCompletionHandler.channelRead0(HandshakeCompletionHandler.java:17) [HandshakeCompletionHandler.class:?]
at cpw.mods.fml.common.network.internal.HandshakeCompletionHandler.channelRead0(HandshakeCompletionHandler.java:11) [HandshakeCompletionHandler.class:?]
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:98) [SimpleChannelInboundHandler.class:?]
at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323) [DefaultChannelHandlerContext.class:?]
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103) [MessageToMessageDecoder.class:?]
at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111) [MessageToMessageCodec.class:?]
at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785) [DefaultChannelPipeline.class:?]
at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:169) [EmbeddedChannel.class:?]
at cpw.mods.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:86) [FMLProxyPacket.class:?]
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:241) [NetworkManager.class:?]
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182) [NetworkSystem.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:726) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:614) [MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) [IntegratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:485) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:752) [MinecraftServer$2.class:?]
Caused by: java.lang.IndexOutOfBoundsException: readerIndex(0) + length(4) exceeds writerIndex(1): UnpooledHeapByteBuf(ridx: 0, widx: 1, cap: 256)
at io.netty.buffer.AbstractByteBuf.checkReadableBytes(AbstractByteBuf.java:1160) ~[AbstractByteBuf.class:?]
at io.netty.buffer.AbstractByteBuf.readInt(AbstractByteBuf.java:611) ~[AbstractByteBuf.class:?]
at apocalypse.fandomcraft.network.altpacket.APacketSanity$SanityMesage.toBytes(APacketSanity.java:50) ~[APacketSanity$SanityMesage.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleIndexedCodec.encodeInto(SimpleIndexedCodec.java:11) ~[SimpleIndexedCodec.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleIndexedCodec.encodeInto(SimpleIndexedCodec.java:7) ~[SimpleIndexedCodec.class:?]
at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.encode(FMLIndexedMessageToMessageCodec.java:51) ~[FMLIndexedMessageToMessageCodec.class:?]
at io.netty.handler.codec.MessageToMessageCodec$1.encode(MessageToMessageCodec.java:67) ~[MessageToMessageCodec$1.class:?]
at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:89) ~[MessageToMessageEncoder.class:?]
... 37 more
Packet Handler
public class APacketHandler
{
public static SimpleNetworkWrapper net;
public static void initPackets()
{
net = NetworkRegistry.INSTANCE.newSimpleChannel(Main.modid.toUpperCase());
registerMessage(APacketSanity.class, SanityMesage.class);
}
Never mind my last post. It was a stupid fix in my packet.
It works now, but unfortunately my sanity bar is also somehow displaying over the armour bar as well as in the corner....
Any ideas how that happened?
Yep - you are rendering your bar during EVERY single overlay, of which there are many; that is why in my example I return if the event's element type is not the XP bar - you only want to render ONCE.
Doesn't really matter which element you choose to do your rendering during, but do choose one.
I got all of it working except they still reset on death and world load. Not sure where that's going wrong...
public class ExtendedPlayer implements IExtendedEntityProperties
{
public final static String EXT_PROP_NAME = "ExtendedPlayer";
public static final int SANITYWATCHER = 20;
public static final int MONEYWATCHER = 21;
public static final int FUELWATCHER = 22;
public static final void register(EntityPlayer player)
{
player.registerExtendedProperties(ExtendedPlayer.EXT_PROP_NAME, new ExtendedPlayer(player));
}
public static final ExtendedPlayer get(EntityPlayer player)
{
return (ExtendedPlayer) player.getExtendedProperties(EXT_PROP_NAME);
}
@Override
public void saveNBTData(NBTTagCompound compound)
{
NBTTagCompound properties = new NBTTagCompound();
}
public void spendMoney(int amount)
{
int money = this.player.getDataWatcher().getWatchableObjectInt(MONEYWATCHER);
this.player.getDataWatcher().updateObject(MONEYWATCHER, money - amount);
}
public final void sync()
{
ByteArrayOutputStream bos1 = new ByteArrayOutputStream(8);
DataOutputStream outputStream1 = new DataOutputStream(bos1);
ByteArrayOutputStream bos2 = new ByteArrayOutputStream(8);
DataOutputStream outputStream2 = new DataOutputStream(bos2);
if (!player.worldObj.isRemote)
{
// Sending packet to server
IMessage msg1 = new SanityMesage(500, player);
APacketHandler.net.sendToServer(msg1);
IMessage msg2 = new SanityMesage(501, player);
APacketHandler.net.sendToServer(msg2);
@Ms_Raven - why are you still using the old Proxy.save/loadData method? You need to subscribe to PlayerEvent.Clone and copy the data there, as I showed several pages back and in the online repository.
coolAlias do you know how to make property which decreases like hunger does? by itself, from time to time.
Sure:
int decreasesWithTime = 20;
// on any update tick, e.g. Entity#onUpdate()
--decreasesWithTime;
That's basically all hunger is doing under the hood, but there is obviously more logic involved for determining when it should decrease (it actually uses two values, one for hunger and one for saturation, and hunger only starts to deplete when saturation is down).
The exact logic will depend on your system, and that's for you to design. I suggest you use your IDE to browse around the vanilla code, checking all the places that hunger is involved. You will learn a lot just by doing that.
That's basically all hunger is doing under the hood, but there is obviously more logic involved for determining when it should decrease (it actually uses two values, one for hunger and one for saturation, and hunger only starts to deplete when saturation is down).
The exact logic will depend on your system, and that's for you to design. I suggest you use your IDE to browse around the vanilla code, checking all the places that hunger is involved. You will learn a lot just by doing that.
That's basically all hunger is doing under the hood, but there is obviously more logic involved for determining when it should decrease (it actually uses two values, one for hunger and one for saturation, and hunger only starts to deplete when saturation is down).
The exact logic will depend on your system, and that's for you to design. I suggest you use your IDE to browse around the vanilla code, checking all the places that hunger is involved. You will learn a lot just by doing that.
Well, I have made something like a logic but some things does not work, can you help me with it? EventHndlerClass:
public class NeedsEventHandler {
int decreaseWithTime = 0;
@SubscribeEvent
public void onEntityConstructing(EntityConstructing event) {
if (event.entity instanceof EntityPlayer && WaterPlayerProperty.get((EntityPlayer) event.entity) == null)
WaterPlayerProperty.register((EntityPlayer) event.entity);
}
@SubscribeEvent
public void onLivingUpdate(LivingUpdateEvent event) {
if (event.entity instanceof EntityPlayer) {
WaterPlayerProperty props = WaterPlayerProperty.get((EntityPlayer) event.entity);
EnumDifficulty enumdifficulty = event.entity.worldObj.getDifficulty();
decreaseWithTime++;
if (decreaseWithTime >= 80 && enumdifficulty != EnumDifficulty.PEACEFUL) {
if (props.currentWater != 0) {
props.currentWater--;
}
if (props.currentWater < 1) {
System.out.println("NEEDS TO TAKE DAMAGE");
event.entity.attackEntityFrom(NeedsMod.Dehidrate, 1F);
}
if (event.entity.isDead) {
props.replenishWater();
}
decreaseWithTime = 0;
}
}
}
}
Everything works. Except taking damage. The timer works, decreasing property works and event check where it sysout's that player needs to take damage works, but player isn't getting damage, only sometimes, it may work once but does not work second time. I have own damage source, but that isn't causing it, because i have tested it with built in ones and does the same thing. Why could it happen(btw sorry for bad english)
You'll need to print out all of the values to see why it happens only once - this is usually a symptom of poor logic in one of your conditional statements.
Another point: I would check the world difficulty as an outer condition - if it's peaceful, none of that logic should be happening (or should it? does the player's thirst increase even on peaceful?). E.g.:
@SubscribeEvent
public void onLivingUpdate(LivingUpdateEvent event) {
if (event.entity instanceof EntityPlayer && event.entity.worldObj.getDifficulty() != EnumDifficulty.PEACEFUL) {
// rest of your thirst logic here
}
}
Furthermore, if you do not plan on implementing thirst for entities other than players, you should use the PlayerTickEvent rather than the LivingUpdateEvent - the former ticks only for players, the latter for every single living entity in the world, which is very likely a needless waste of CPU cycles, however fast they may perform.
Thanks for the tutorial. Everything starts up without errors:) I was confused as to how to save data if anything changed. ie. increase max mana. Also, how would I obtain the variables within this class? I have another method, outside of the class, that will use those variables.
Thanks a bunch!
Rollback Post to RevisionRollBack
I complicate easy problems, come begging for help, and leave with a simple solution.
Thanks for the tutorial. Everything starts up without errors:) I was confused as to how to save data if anything changed. ie. increase max mana. Also, how would I obtain the variables within this class? I have another method, outside of the class, that will use those variables.
Thanks a bunch!
You shouldn't have to do anything other than correctly implement your read and write to NBT methods within IExtendedEntityProperties. You should never be calling those manually - the are called automatically when needed.
For accessing your IEEP instance, you have to fetch the properties for the entity instance you want with whatever unique identifier you gave it, and cast it back to your class:
// Basic syntax:
IExtendedEntityProperties props = entity.getExtendedProperties("SomeUniqueIdentifier");
// but you'll want to cast to your class so you can use it:
YourIEEPClass data = (YourIEEPClass) entity.getExtendedProperties("YourIEEPClassUniqueIdentifier");
// now you can access your class methods and fields for the instance specific to the entity:
data.doSomething();
data.someValue = 42;
It's kind of cumbersome, which is why I add a static getter method to all of my IEEP classes, which you can see in the tutorial.
-
View User Profile
-
View Posts
-
Send Message
Curse PremiumThe IEEP is vanilla. My packets work for other things, so I'm assuming that's not what's null.
I used this tutorial before and got it working but lost the code months ago so I'm guessing something's changed?
PubliCraft & Fandomcraft
-
View User Profile
-
View Posts
-
Send Message
Curse PremiumNothing's changed, and IExtendedEntityProperties is NOT vanilla - it's added by Forge so modders can add additional properties to vanilla entities (such as players). You'll need to show some code.
-
View User Profile
-
View Posts
-
Send Message
Curse PremiumWhat do I need to show?
PubliCraft & Fandomcraft
-
View User Profile
-
View Posts
-
Send Message
Curse PremiumDepends - did you determine what, exactly, is causing your render overlay method to return before it finishes? You have a couple of 'return' statements in there, and I asked you some questions a few posts back that you have yet to answer.
Once you can answer those questions, you will know what part of your code is failing and thus which part to post (you'll probably even be able to figure out how to fix it at that point).
Don't know how to figure out the answers to those questions? Use your IDEs debugger or, if you don't know how (and can't Google it), put System.out.println statements in your render overlay method that print out the values of each variable in question.
-
View User Profile
-
View Posts
-
Send Message
Curse PremiumI can't print out any variables because 'props' is null. I don't understand why. I've tried using both packets and the datawatcher.
Am I just missing some stupid little thing?
public class ExtendedPlayer implements IExtendedEntityProperties
{
public final static String EXT_PROP_NAME = "ExtendedPlayer";
public static final int SANITYWATCHER = 20;
private final EntityPlayer player;
private int maxSanity;
public ExtendedPlayer(EntityPlayer player)
{
this.player = player;
this.maxSanity = 50;
this.player.getDataWatcher().addObject(SANITYWATCHER, this.maxSanity);
}
public static final void register(EntityPlayer player)
{
player.registerExtendedProperties(ExtendedPlayer.EXT_PROP_NAME, new ExtendedPlayer(player));
}
public static final ExtendedPlayer get(EntityPlayer player)
{
return (ExtendedPlayer) player.getExtendedProperties(EXT_PROP_NAME);
}
@Override
public void saveNBTData(NBTTagCompound compound)
{
NBTTagCompound properties = new NBTTagCompound();
properties.setInteger("CurrentSanity", this.player.getDataWatcher().getWatchableObjectInt(SANITYWATCHER));
properties.setInteger("MaxSanity", this.maxSanity);
compound.setTag(EXT_PROP_NAME, properties);
}
@Override
public void loadNBTData(NBTTagCompound compound)
{
NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME);
this.player.getDataWatcher().updateObject(SANITYWATCHER, properties.getInteger("CurrentSanity"));
this.maxSanity = properties.getInteger("MaxSanity");
}
@Override
public void init(Entity entity, World world)
{
}
public final boolean consumeSanity(int amount)
{
int sanity = this.player.getDataWatcher().getWatchableObjectInt(SANITYWATCHER);
boolean sufficient = amount <= sanity;
sanity -= (amount < sanity ? amount : sanity);
this.player.getDataWatcher().updateObject(SANITYWATCHER, sanity);
return sufficient;
}
public final void replenishSanity()
{
this.player.getDataWatcher().updateObject(SANITYWATCHER, this.maxSanity);
}
public final int getCurrentSanity()
{
return this.player.getDataWatcher().getWatchableObjectInt(SANITYWATCHER);
}
public final int getMaxSanity()
{
return maxSanity;
}
public void addCurrentSanity(int amount)
{
int currentSanity = this.player.getDataWatcher().getWatchableObjectInt(SANITYWATCHER);
this.player.getDataWatcher().updateObject(SANITYWATCHER, currentSanity + (amount < this.maxSanity ? amount : this.maxSanity));
}
public void setMaxSanity(int amount)
{
this.maxSanity = (amount > 0 ? amount : 0);
this.sync();
}
public void addMaxSanity(int amount)
{
this.maxSanity = this.maxSanity + (amount);
this.sync();
}
public final void sync()
{
ByteArrayOutputStream bos = new ByteArrayOutputStream(8);
DataOutputStream outputStream = new DataOutputStream(bos);
try
{
outputStream.writeInt(this.maxSanity);
}
catch (Exception ex)
{
ex.printStackTrace();
}
if (!player.worldObj.isRemote) {
EntityPlayerMP player1 = (EntityPlayerMP) player;
PacketDispatcher.sendTo(new MessageSanity(), player1);
}
}
private static String getSaveKey(EntityPlayer player)
{
return player.getCommandSenderName() + ":" + EXT_PROP_NAME;
}
public static void saveProxyData(EntityPlayer player)
{
ExtendedPlayer playerData = ExtendedPlayer.get(player);
NBTTagCompound savedData = new NBTTagCompound();
playerData.saveNBTData(savedData);
ServerProxy.storeEntityData(getSaveKey(player), savedData);
}
public static void loadProxyData(EntityPlayer player)
{
ExtendedPlayer playerData = ExtendedPlayer.get(player);
NBTTagCompound savedData = ServerProxy.getEntityData(getSaveKey(player));
if(savedData != null)
{
playerData.loadNBTData(savedData);
}
playerData.sync();
}
}
PubliCraft & Fandomcraft
-
View User Profile
-
View Posts
-
Send Message
Curse PremiumLet me re-iterate here:
I.e., if ExtendedPlayer.get(player) returns null, the problem is very likely that you did not register your IEEP during EntityConstructing event. Given that, I would need to see your event handler code and registration (registration is usually done in the main mod file during one of the FML events).
-
View User Profile
-
View Posts
-
Send Message
Curse PremiumOkay so, no, that wasn't registered...
Now the problem IS my packets, the one thing I apparently still can't get right.
I'm getting this NullPointerException:
readerIndex(0) + length(4) exceeds writerIndex(1): UnpooledHeapByteBuf(ridx: 0, widx: 1, cap: 256)
MissApocalypse joined the game
[14:11:11] [Server thread/ERROR] [FML]: FMLIndexedMessageCodec exception caught
io.netty.handler.codec.EncoderException: java.lang.IndexOutOfBoundsException: readerIndex(0) + length(4) exceeds writerIndex(1): UnpooledHeapByteBuf(ridx: 0, widx: 1, cap: 256)
at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:107) ~[MessageToMessageEncoder.class:?]
at io.netty.handler.codec.MessageToMessageCodec.write(MessageToMessageCodec.java:116) ~[MessageToMessageCodec.class:?]
at io.netty.channel.DefaultChannelHandlerContext.invokeWrite(DefaultChannelHandlerContext.java:644) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.write(DefaultChannelHandlerContext.java:698) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.writeAndFlush(DefaultChannelHandlerContext.java:688) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.writeAndFlush(DefaultChannelHandlerContext.java:717) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelPipeline.writeAndFlush(DefaultChannelPipeline.java:893) ~[DefaultChannelPipeline.class:?]
at io.netty.channel.AbstractChannel.writeAndFlush(AbstractChannel.java:239) ~[AbstractChannel.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper.sendToServer(SimpleNetworkWrapper.java:236) [SimpleNetworkWrapper.class:?]
at apocalypse.fandomcraft.player.ExtendedPlayer.sync(ExtendedPlayer.java:129) [ExtendedPlayer.class:?]
at apocalypse.fandomcraft.player.ExtendedPlayerEvents.onEntityJoinWorld(ExtendedPlayerEvents.java:56) [ExtendedPlayerEvents.class:?]
at cpw.mods.fml.common.eventhandler.ASMEventHandler_20_ExtendedPlayerEvents_onEntityJoinWorld_EntityJoinWorldEvent.invoke(.dynamic) [?:?]
at cpw.mods.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:54) [ASMEventHandler.class:?]
at cpw.mods.fml.common.eventhandler.EventBus.post(EventBus.java:138) [EventBus.class:?]
at net.minecraft.world.World.spawnEntityInWorld(World.java:1503) [World.class:?]
at net.minecraft.server.management.ServerConfigurationManager.playerLoggedIn(ServerConfigurationManager.java:309) [ServerConfigurationManager.class:?]
at net.minecraft.server.management.ServerConfigurationManager.initializeConnectionToPlayer(ServerConfigurationManager.java:170) [ServerConfigurationManager.class:?]
at cpw.mods.fml.common.network.handshake.NetworkDispatcher.completeServerSideConnection(NetworkDispatcher.java:173) [NetworkDispatcher.class:?]
at cpw.mods.fml.common.network.handshake.NetworkDispatcher.completeHandshake(NetworkDispatcher.java:446) [NetworkDispatcher.class:?]
at cpw.mods.fml.common.network.internal.HandshakeCompletionHandler.channelRead0(HandshakeCompletionHandler.java:17) [HandshakeCompletionHandler.class:?]
at cpw.mods.fml.common.network.internal.HandshakeCompletionHandler.channelRead0(HandshakeCompletionHandler.java:11) [HandshakeCompletionHandler.class:?]
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:98) [SimpleChannelInboundHandler.class:?]
at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323) [DefaultChannelHandlerContext.class:?]
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103) [MessageToMessageDecoder.class:?]
at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111) [MessageToMessageCodec.class:?]
at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785) [DefaultChannelPipeline.class:?]
at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:169) [EmbeddedChannel.class:?]
at cpw.mods.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:86) [FMLProxyPacket.class:?]
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:241) [NetworkManager.class:?]
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182) [NetworkSystem.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:726) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:614) [MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) [IntegratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:485) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:752) [MinecraftServer$2.class:?]
Caused by: java.lang.IndexOutOfBoundsException: readerIndex(0) + length(4) exceeds writerIndex(1): UnpooledHeapByteBuf(ridx: 0, widx: 1, cap: 256)
at io.netty.buffer.AbstractByteBuf.checkReadableBytes(AbstractByteBuf.java:1160) ~[AbstractByteBuf.class:?]
at io.netty.buffer.AbstractByteBuf.readInt(AbstractByteBuf.java:611) ~[AbstractByteBuf.class:?]
at apocalypse.fandomcraft.network.altpacket.APacketSanity$SanityMesage.toBytes(APacketSanity.java:50) ~[APacketSanity$SanityMesage.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleIndexedCodec.encodeInto(SimpleIndexedCodec.java:11) ~[SimpleIndexedCodec.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleIndexedCodec.encodeInto(SimpleIndexedCodec.java:7) ~[SimpleIndexedCodec.class:?]
at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.encode(FMLIndexedMessageToMessageCodec.java:51) ~[FMLIndexedMessageToMessageCodec.class:?]
at io.netty.handler.codec.MessageToMessageCodec$1.encode(MessageToMessageCodec.java:67) ~[MessageToMessageCodec$1.class:?]
at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:89) ~[MessageToMessageEncoder.class:?]
... 37 more
[14:11:11] [Server thread/ERROR] [FML]: SimpleChannelHandlerWrapper exception
io.netty.handler.codec.EncoderException: java.lang.IndexOutOfBoundsException: readerIndex(0) + length(4) exceeds writerIndex(1): UnpooledHeapByteBuf(ridx: 0, widx: 1, cap: 256)
at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:107) ~[MessageToMessageEncoder.class:?]
at io.netty.handler.codec.MessageToMessageCodec.write(MessageToMessageCodec.java:116) ~[MessageToMessageCodec.class:?]
at io.netty.channel.DefaultChannelHandlerContext.invokeWrite(DefaultChannelHandlerContext.java:644) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.write(DefaultChannelHandlerContext.java:698) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.writeAndFlush(DefaultChannelHandlerContext.java:688) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.writeAndFlush(DefaultChannelHandlerContext.java:717) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelPipeline.writeAndFlush(DefaultChannelPipeline.java:893) ~[DefaultChannelPipeline.class:?]
at io.netty.channel.AbstractChannel.writeAndFlush(AbstractChannel.java:239) ~[AbstractChannel.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper.sendToServer(SimpleNetworkWrapper.java:236) [SimpleNetworkWrapper.class:?]
at apocalypse.fandomcraft.player.ExtendedPlayer.sync(ExtendedPlayer.java:129) [ExtendedPlayer.class:?]
at apocalypse.fandomcraft.player.ExtendedPlayerEvents.onEntityJoinWorld(ExtendedPlayerEvents.java:56) [ExtendedPlayerEvents.class:?]
at cpw.mods.fml.common.eventhandler.ASMEventHandler_20_ExtendedPlayerEvents_onEntityJoinWorld_EntityJoinWorldEvent.invoke(.dynamic) [?:?]
at cpw.mods.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:54) [ASMEventHandler.class:?]
at cpw.mods.fml.common.eventhandler.EventBus.post(EventBus.java:138) [EventBus.class:?]
at net.minecraft.world.World.spawnEntityInWorld(World.java:1503) [World.class:?]
at net.minecraft.server.management.ServerConfigurationManager.playerLoggedIn(ServerConfigurationManager.java:309) [ServerConfigurationManager.class:?]
at net.minecraft.server.management.ServerConfigurationManager.initializeConnectionToPlayer(ServerConfigurationManager.java:170) [ServerConfigurationManager.class:?]
at cpw.mods.fml.common.network.handshake.NetworkDispatcher.completeServerSideConnection(NetworkDispatcher.java:173) [NetworkDispatcher.class:?]
at cpw.mods.fml.common.network.handshake.NetworkDispatcher.completeHandshake(NetworkDispatcher.java:446) [NetworkDispatcher.class:?]
at cpw.mods.fml.common.network.internal.HandshakeCompletionHandler.channelRead0(HandshakeCompletionHandler.java:17) [HandshakeCompletionHandler.class:?]
at cpw.mods.fml.common.network.internal.HandshakeCompletionHandler.channelRead0(HandshakeCompletionHandler.java:11) [HandshakeCompletionHandler.class:?]
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:98) [SimpleChannelInboundHandler.class:?]
at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323) [DefaultChannelHandlerContext.class:?]
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103) [MessageToMessageDecoder.class:?]
at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111) [MessageToMessageCodec.class:?]
at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785) [DefaultChannelPipeline.class:?]
at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:169) [EmbeddedChannel.class:?]
at cpw.mods.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:86) [FMLProxyPacket.class:?]
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:241) [NetworkManager.class:?]
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182) [NetworkSystem.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:726) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:614) [MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) [IntegratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:485) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:752) [MinecraftServer$2.class:?]
Caused by: java.lang.IndexOutOfBoundsException: readerIndex(0) + length(4) exceeds writerIndex(1): UnpooledHeapByteBuf(ridx: 0, widx: 1, cap: 256)
at io.netty.buffer.AbstractByteBuf.checkReadableBytes(AbstractByteBuf.java:1160) ~[AbstractByteBuf.class:?]
at io.netty.buffer.AbstractByteBuf.readInt(AbstractByteBuf.java:611) ~[AbstractByteBuf.class:?]
at apocalypse.fandomcraft.network.altpacket.APacketSanity$SanityMesage.toBytes(APacketSanity.java:50) ~[APacketSanity$SanityMesage.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleIndexedCodec.encodeInto(SimpleIndexedCodec.java:11) ~[SimpleIndexedCodec.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleIndexedCodec.encodeInto(SimpleIndexedCodec.java:7) ~[SimpleIndexedCodec.class:?]
at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.encode(FMLIndexedMessageToMessageCodec.java:51) ~[FMLIndexedMessageToMessageCodec.class:?]
at io.netty.handler.codec.MessageToMessageCodec$1.encode(MessageToMessageCodec.java:67) ~[MessageToMessageCodec$1.class:?]
at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:89) ~[MessageToMessageEncoder.class:?]
... 37 more
Packet Handler
public class APacketHandler
{
public static SimpleNetworkWrapper net;
public static void initPackets()
{
net = NetworkRegistry.INSTANCE.newSimpleChannel(Main.modid.toUpperCase());
registerMessage(APacketSanity.class, SanityMesage.class);
}
private static int nextPacketId = 0;
private static void registerMessage(Class packet, Class message)
{
net.registerMessage(packet, message, nextPacketId, Side.CLIENT);
net.registerMessage(packet, message, nextPacketId, Side.SERVER);
nextPacketId++;
}
}
Packet
public class APacketSanity implements IMessageHandler<SanityMesage, IMessage>
{
public int id, currentSanity, maxSanity;
@Override
public IMessage onMessage(SanityMesage message, MessageContext ctx)
{
this.currentSanity = message.currentSanity;
this.maxSanity = message.maxSanity;
return message;
}
public static class SanityMesage implements IMessage
{
public int id, currentSanity, maxSanity;
public SanityMesage(){}
public SanityMesage(int id, EntityPlayer player)
{
ExtendedPlayer props = ExtendedPlayer.get(player);
this.id = id;
this.currentSanity = props.getCurrentSanity();
this.maxSanity = props.getMaxSanity();
}
@Override
public void fromBytes(ByteBuf buf)
{
buf.writeInt(id);
buf.writeInt(currentSanity);
buf.writeInt(maxSanity);
}
@Override
public void toBytes(ByteBuf buf)
{
id=buf.readInt();
currentSanity=buf.readInt();
maxSanity=buf.readInt();
}
}
}
PubliCraft & Fandomcraft
This doesn't work for Minecraft Forge 1.7.10. It doesn't have all those class files you imported.
-
View User Profile
-
View Posts
-
Send Message
Curse PremiumNever mind my last post. It was a stupid fix in my packet.
It works now, but unfortunately my sanity bar is also somehow displaying over the armour bar as well as in the corner....
This is the Gui
@SideOnly(Side.CLIENT)
public class SanityBar extends Gui
{
private Minecraft mc;
private static final ResourceLocation texturepath = new ResourceLocation(Main.modid + ":" + "textures/gui/sanity_bar.png");
public SanityBar(Minecraft mc)
{
super();
this.mc = mc;
}
@SubscribeEvent(priority = EventPriority.NORMAL)
public void onRenderExperienceBar(RenderGameOverlayEvent.Post event)
{
Tools.printcon("Rendering sanity");
ExtendedPlayer props = ExtendedPlayer.get(this.mc.thePlayer);
if (props == null)
{
Tools.printcon("This is null!");
return;
}
int xPos = 2;
int yPos = 2;
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
GL11.glDisable(GL11.GL_LIGHTING);
this.mc.getTextureManager().bindTexture(texturepath);
this.drawTexturedModalRect(xPos, yPos, 0, 0, 60, 8);
int sanitybarwidth = (int)(((float) props.getCurrentSanity() / props.getMaxSanity()) * 58);
Tools.printcon("Rendered sanity");
this.drawTexturedModalRect(xPos + 1, yPos + 1, 0, 8, sanitybarwidth, 6);
}
}
Any ideas how that happened?
PubliCraft & Fandomcraft
-
View User Profile
-
View Posts
-
Send Message
Curse PremiumWhat class files? Are you talking about the online demo? If so, you have to choose the 1.7.10 branch.
Yep - you are rendering your bar during EVERY single overlay, of which there are many; that is why in my example I return if the event's element type is not the XP bar - you only want to render ONCE.
Doesn't really matter which element you choose to do your rendering during, but do choose one.
-
View User Profile
-
View Posts
-
Send Message
Curse PremiumI got all of it working except they still reset on death and world load. Not sure where that's going wrong...
{
public final static String EXT_PROP_NAME = "ExtendedPlayer";
public static final int SANITYWATCHER = 20;
public static final int MONEYWATCHER = 21;
public static final int FUELWATCHER = 22;
private final EntityPlayer player;
private int maxSanity;
private int maxFuel;
public ExtendedPlayer(EntityPlayer player)
{
this.player = player;
this.maxSanity = 50;
this.maxFuel = 50;
this.player.getDataWatcher().addObject(SANITYWATCHER, this.maxSanity);
this.player.getDataWatcher().addObject(FUELWATCHER, this.maxFuel);
this.player.getDataWatcher().addObject(MONEYWATCHER, 0);
}
public static final void register(EntityPlayer player)
{
player.registerExtendedProperties(ExtendedPlayer.EXT_PROP_NAME, new ExtendedPlayer(player));
}
public static final ExtendedPlayer get(EntityPlayer player)
{
return (ExtendedPlayer) player.getExtendedProperties(EXT_PROP_NAME);
}
@Override
public void saveNBTData(NBTTagCompound compound)
{
NBTTagCompound properties = new NBTTagCompound();
properties.setInteger("CurrentSanity", this.player.getDataWatcher().getWatchableObjectInt(SANITYWATCHER));
properties.setInteger("MaxSanity", this.maxSanity);
properties.setInteger("CurrentFuel", this.player.getDataWatcher().getWatchableObjectInt(FUELWATCHER));
properties.setInteger("MaxFuel", this.maxFuel);
properties.setInteger("Money", this.player.getDataWatcher().getWatchableObjectInt(MONEYWATCHER));
compound.setTag(EXT_PROP_NAME, properties);
}
@Override
public void loadNBTData(NBTTagCompound compound)
{
NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME);
this.player.getDataWatcher().updateObject(SANITYWATCHER, properties.getInteger("CurrentSanity"));
this.maxSanity = properties.getInteger("MaxSanity");
this.player.getDataWatcher().updateObject(FUELWATCHER, properties.getInteger("CurrentFuel"));
this.maxSanity = properties.getInteger("MaxFuel");
this.player.getDataWatcher().updateObject(MONEYWATCHER, properties.getInteger("Money"));
}
@Override
public void init(Entity entity, World world)
{
}
public final boolean consumeSanity(int amount)
{
int sanity = this.player.getDataWatcher().getWatchableObjectInt(SANITYWATCHER);
boolean sufficient = amount <= sanity;
sanity -= (amount < sanity ? amount : sanity);
this.player.getDataWatcher().updateObject(SANITYWATCHER, sanity);
return sufficient;
}
public final boolean consumeFuel(int amount)
{
int fuel = this.player.getDataWatcher().getWatchableObjectInt(FUELWATCHER);
boolean sufficient = amount <= fuel;
fuel -= (amount < fuel ? amount : fuel);
this.player.getDataWatcher().updateObject(FUELWATCHER, fuel);
return sufficient;
}
public final void replenishSanity()
{
this.player.getDataWatcher().updateObject(SANITYWATCHER, this.maxSanity);
}
public final void replenishFuel()
{
this.player.getDataWatcher().updateObject(FUELWATCHER, this.maxFuel);
}
public final int getCurrentSanity()
{
return this.player.getDataWatcher().getWatchableObjectInt(SANITYWATCHER);
}
public final int getCurrentFuel()
{
return this.player.getDataWatcher().getWatchableObjectInt(FUELWATCHER);
}
public final int getMoney()
{
return this.player.getDataWatcher().getWatchableObjectInt(MONEYWATCHER);
}
public final int getMaxSanity()
{
return maxSanity;
}
public final int getMaxFuel()
{
return maxFuel;
}
public void addCurrentSanity(int amount)
{
int currentSanity = this.player.getDataWatcher().getWatchableObjectInt(SANITYWATCHER);
if((currentSanity + amount) < maxSanity)
{
Tools.println("Sanity is at " + currentSanity + "/" + maxSanity + ".");
this.player.getDataWatcher().updateObject(SANITYWATCHER, currentSanity + amount);
Tools.println("Restored " + amount + " sanity.");
Tools.println("Sanity is now at " + currentSanity + "/" + maxSanity + ".");
}
else
{
this.player.getDataWatcher().updateObject(SANITYWATCHER, currentSanity = maxSanity);
}
}
public void addCurrentFuel(int amount)
{
int currentFuel = this.player.getDataWatcher().getWatchableObjectInt(FUELWATCHER);
if((currentFuel + amount) < maxFuel)
{
Tools.println("Fuel is at " + currentFuel + "/" + maxFuel + ".");
this.player.getDataWatcher().updateObject(FUELWATCHER, currentFuel + amount);
Tools.println("Restored " + amount + " fuel.");
Tools.println("Fuel is now at " + currentFuel + "/" + maxFuel + ".");
}
else
{
this.player.getDataWatcher().updateObject(FUELWATCHER, currentFuel = maxFuel);
}
}
public void setMaxSanity(int amount)
{
this.maxSanity = (amount > 0 ? amount : 0);
this.sync();
}
public void addMaxSanity(int amount)
{
this.maxSanity = this.maxSanity + (amount);
this.sync();
}
public void setMaxFuel(int amount)
{
this.maxFuel = (amount > 0 ? amount : 0);
this.sync();
}
public void addMaxFuel(int amount)
{
this.maxFuel = this.maxFuel + (amount);
this.sync();
}
public void addMoney(int amount)
{
int money = this.player.getDataWatcher().getWatchableObjectInt(MONEYWATCHER);
this.player.getDataWatcher().updateObject(MONEYWATCHER, money + amount);
}
public void spendMoney(int amount)
{
int money = this.player.getDataWatcher().getWatchableObjectInt(MONEYWATCHER);
this.player.getDataWatcher().updateObject(MONEYWATCHER, money - amount);
}
public final void sync()
{
ByteArrayOutputStream bos1 = new ByteArrayOutputStream(8);
DataOutputStream outputStream1 = new DataOutputStream(bos1);
ByteArrayOutputStream bos2 = new ByteArrayOutputStream(8);
DataOutputStream outputStream2 = new DataOutputStream(bos2);
try
{
outputStream1.writeInt(this.maxSanity);
outputStream2.writeInt(this.maxFuel);
}
catch (Exception ex)
{
ex.printStackTrace();
}
if (!player.worldObj.isRemote)
{
// Sending packet to server
IMessage msg1 = new SanityMesage(500, player);
APacketHandler.net.sendToServer(msg1);
IMessage msg2 = new SanityMesage(501, player);
APacketHandler.net.sendToServer(msg2);
// Sending packet to client
/*
if (player instanceof EntityPlayerMP)
{
IMessage msg = new SanityMesage(800, false);
APacketHandler.net.sendTo(msg, (EntityPlayerMP)player);
}
*/
}
}
private static String getSaveKey(EntityPlayer player)
{
return player.getCommandSenderName() + ":" + EXT_PROP_NAME;
}
public static void saveProxyData(EntityPlayer player)
{
ExtendedPlayer playerData = ExtendedPlayer.get(player);
NBTTagCompound savedData = new NBTTagCompound();
playerData.saveNBTData(savedData);
ServerProxy.storeEntityData(getSaveKey(player), savedData);
}
public static void loadProxyData(EntityPlayer player)
{
ExtendedPlayer playerData = ExtendedPlayer.get(player);
NBTTagCompound savedData = ServerProxy.getEntityData(getSaveKey(player));
if(savedData != null)
{
playerData.loadNBTData(savedData);
}
playerData.sync();
}
}
public void onEntityJoinWorld(EntityJoinWorldEvent event)
{
if (!event.entity.worldObj.isRemote && event.entity instanceof EntityPlayer)
{
NBTTagCompound playerData = ServerProxy.getEntityData(((EntityPlayer) event.entity).getCommandSenderName());
if (playerData != null)
{
((ExtendedPlayer)(event.entity.getExtendedProperties(ExtendedPlayer.EXT_PROP_NAME))).loadNBTData(playerData);
}
((ExtendedPlayer)(event.entity.getExtendedProperties(ExtendedPlayer.EXT_PROP_NAME))).sync();
ServerProxy.storeEntityData(((EntityPlayer) event.entity).getCommandSenderName() + ExtendedPlayer.EXT_PROP_NAME, playerData);
}
}
@SubscribeEvent
public void onLivingDeathEvent(LivingDeathEvent event)
{
if (!event.entity.worldObj.isRemote && event.entity instanceof EntityPlayer)
{
NBTTagCompound playerData = new NBTTagCompound();
((ExtendedPlayer)(event.entity.getExtendedProperties(ExtendedPlayer.EXT_PROP_NAME))).saveNBTData(playerData);
ServerProxy.storeEntityData(((EntityPlayer) event.entity).getCommandSenderName() + ExtendedPlayer.EXT_PROP_NAME, playerData);
ExtendedPlayer.saveProxyData((EntityPlayer) event.entity);
}
}
public static void storeEntityData(String name, NBTTagCompound compound)
{
extendedEntityData.put(name, compound);
}
public static NBTTagCompound getEntityData(String name)
{
return extendedEntityData.remove(name);
}
PubliCraft & Fandomcraft
-
View User Profile
-
View Posts
-
Send Message
Curse Premium@Ms_Raven - why are you still using the old Proxy.save/loadData method? You need to subscribe to PlayerEvent.Clone and copy the data there, as I showed several pages back and in the online repository.
coolAlias do you know how to make property which decreases like hunger does? by itself, from time to time.
-
View User Profile
-
View Posts
-
Send Message
Curse PremiumSure: That's basically all hunger is doing under the hood, but there is obviously more logic involved for determining when it should decrease (it actually uses two values, one for hunger and one for saturation, and hunger only starts to deplete when saturation is down).
The exact logic will depend on your system, and that's for you to design. I suggest you use your IDE to browse around the vanilla code, checking all the places that hunger is involved. You will learn a lot just by doing that.
Thanks! That should do! Great tutorials btw.
Well, I have made something like a logic but some things does not work, can you help me with it? EventHndlerClass:
public class NeedsEventHandler {
int decreaseWithTime = 0;
@SubscribeEvent
public void onEntityConstructing(EntityConstructing event) {
if (event.entity instanceof EntityPlayer && WaterPlayerProperty.get((EntityPlayer) event.entity) == null)
WaterPlayerProperty.register((EntityPlayer) event.entity);
}
@SubscribeEvent
public void onLivingUpdate(LivingUpdateEvent event) {
if (event.entity instanceof EntityPlayer) {
WaterPlayerProperty props = WaterPlayerProperty.get((EntityPlayer) event.entity);
EnumDifficulty enumdifficulty = event.entity.worldObj.getDifficulty();
decreaseWithTime++;
if (decreaseWithTime >= 80 && enumdifficulty != EnumDifficulty.PEACEFUL) {
if (props.currentWater != 0) {
props.currentWater--;
}
if (props.currentWater < 1) {
System.out.println("NEEDS TO TAKE DAMAGE");
event.entity.attackEntityFrom(NeedsMod.Dehidrate, 1F);
}
System.out.println("[currentWater]" + props.getCurrentWater());
if (event.entity.isDead) {
props.replenishWater();
}
decreaseWithTime = 0;
}
}
}
}
Everything works. Except taking damage. The timer works, decreasing property works and event check where it sysout's that player needs to take damage works, but player isn't getting damage, only sometimes, it may work once but does not work second time. I have own damage source, but that isn't causing it, because i have tested it with built in ones and does the same thing. Why could it happen(btw sorry for bad english)
-
View User Profile
-
View Posts
-
Send Message
Curse PremiumAnother point: I would check the world difficulty as an outer condition - if it's peaceful, none of that logic should be happening (or should it? does the player's thirst increase even on peaceful?). E.g.:
@SubscribeEvent public void onLivingUpdate(LivingUpdateEvent event) {Furthermore, if you do not plan on implementing thirst for entities other than players, you should use the PlayerTickEvent rather than the LivingUpdateEvent - the former ticks only for players, the latter for every single living entity in the world, which is very likely a needless waste of CPU cycles, however fast they may perform.if (event.entity instanceof EntityPlayer && event.entity.worldObj.getDifficulty() != EnumDifficulty.PEACEFUL) {
// rest of your thirst logic here
}
}
-
View User Profile
-
View Posts
-
Send Message
Curse PremiumThanks for the tutorial. Everything starts up without errors:) I was confused as to how to save data if anything changed. ie. increase max mana. Also, how would I obtain the variables within this class? I have another method, outside of the class, that will use those variables.
Thanks a bunch!
I complicate easy problems, come begging for help, and leave with a simple solution.

-
View User Profile
-
View Posts
-
Send Message
Curse PremiumYou shouldn't have to do anything other than correctly implement your read and write to NBT methods within IExtendedEntityProperties. You should never be calling those manually - the are called automatically when needed.
For accessing your IEEP instance, you have to fetch the properties for the entity instance you want with whatever unique identifier you gave it, and cast it back to your class:
// Basic syntax: IExtendedEntityProperties props = entity.getExtendedProperties("SomeUniqueIdentifier");It's kind of cumbersome, which is why I add a static getter method to all of my IEEP classes, which you can see in the tutorial.// but you'll want to cast to your class so you can use it: YourIEEPClass data = (YourIEEPClass) entity.getExtendedProperties("YourIEEPClassUniqueIdentifier");
// now you can access your class methods and fields for the instance specific to the entity: data.doSomething(); data.someValue = 42;
I'm little confused about the IExtendedEntityProperties, or we can say the mechanic of minecraft's properties.
Does it run over the ram when minecraft normal running and save as NBT when minecraft closing?
Or running in some other ways?
Also about the packet, is that means after you register a packet, the minecraft will manage when the packet should be sent?
If a mod have a function changing the exp of player, do we need a extra packet to syn the exp again?