Posted January 20, 20196 yr I have used a Capability to store a lot of data on to the player and usually Im able to access the data just fine and get the correct values by using the capability's interface (called IStats) . here is an example of using the Capability to get the necessary values that works fine Spoiler if(attacker instanceof EntityPlayer) { EntityPlayer player = (EntityPlayer) attacker; IStats playerStats = player.getCapability(StatsProvider.STATS_CAPABILITY, null); ItemStack weaponstack = player.getHeldItemMainhand(); Item weapon = weaponstack.getItem(); if(weapon instanceof ItemMyWeapons) { float rawDamage = damage; int maxCondition = weaponstack.getMaxDamage(); int condition = weaponstack.getMaxDamage() - weaponstack.getItemDamage(); System.out.print("\nWeapon's damage is " + rawDamage); rawDamage *= 0.5 + 0.01 * playerStats.getStat(StatConstants.STRENGTH); rawDamage *= (float)condition / (float)maxCondition; return round1(rawDamage, 2); } return 0; } else if (attacker instanceof EntityMob) { float rawDamage = damage; IMobStats mobStats = attacker.getCapability(MobStatsProvider.MOB_STATS_CAPABILITY, null); rawDamage *= 0.5 + 0.01 * mobStats.getStat(StatConstants.STRENGTH); return round1(rawDamage, 2); } return 0; But when I try to do this Minecraft.getMinecraft().displayGuiScreen(new CharacterSheetGUI(Minecraft.getMinecraft().player)); once the GUI is displayed its giving me incorrect numbers. Here is the GUI code if it helps Spoiler public class CharacterSheetGUI extends GuiScreen{ ResourceLocation texture = new ResourceLocation(Reference.MOD_ID, "textures/gui/vanillabook.png"); int guiHeight = 192; int guiWidth = 192; GuiButton button1; GuiButton button2; GuiButton button3; EntityPlayer player; int page = 0; final int BUTTON1 = 0; final int BUTTON2 = 1; final int BUTTON3 = 2; public CharacterSheetGUI(EntityPlayer player) { this.player = player; } Minecraft mc = Minecraft.getMinecraft(); @Override public void drawScreen(int mouseX, int mouseY, float partialTicks) { drawDefaultBackground(); Minecraft.getMinecraft().renderEngine.bindTexture(texture); int centerX =(width / 2) - guiWidth / 2; int centerY = (height /2) - guiHeight / 2; drawTexturedModalRect(centerX, centerY-20, 0, 0, guiWidth, guiHeight); IStats playerStats = player.getCapability(StatsProvider.STATS_CAPABILITY, null); ISkills playerSkills = player.getCapability(SkillsProvider.SKILLS_CAPABILITY, null); if(page == 0) { fontRenderer.drawString("Attributes ", centerX+40, centerY-5, 0x000000); fontRenderer.drawString("Strength: " + playerStats.getStat(StatConstants.STRENGTH), centerX+40, centerY+10, 0x000000); fontRenderer.drawString("Endurance: " + playerStats.getStat(StatConstants.ENDURANCE), centerX+40, centerY+20, 0x000000); fontRenderer.drawString("Speed: " + playerStats.getStat(StatConstants.SPEED), centerX+40, centerY+30, 0x000000); fontRenderer.drawString("Agility: " + playerStats.getStat(StatConstants.AGILITY), centerX+40, centerY+40, 0x000000); fontRenderer.drawString("Intelligence: " + playerStats.getStat(StatConstants.INTELLIGENCE), centerX+40, centerY+50, 0x000000); fontRenderer.drawString("Luck: " + playerStats.getStat(StatConstants.LUCK), centerX+40, centerY+60, 0x000000); fontRenderer.drawString("Major Skills", centerX+40, centerY+75, 0x000000); fontRenderer.drawString("Long Blade " + playerSkills.getStat(StatConstants.LONG_BLADE), centerX+40, centerY+90, 0x000000); fontRenderer.drawString("Short Blade " + playerSkills.getStat(StatConstants.SHORT_BLADE), centerX+40, centerY+100, 0x000000); fontRenderer.drawString("Axe " + playerSkills.getStat(StatConstants.AXE), centerX+40, centerY+110, 0x000000); fontRenderer.drawString("Spear " + playerSkills.getStat(StatConstants.SPEAR), centerX+40, centerY+120, 0x000000); fontRenderer.drawString("Heavy Armor " + playerSkills.getStat(StatConstants.HEAVY_ARMOR), centerX+40, centerY+130, 0x000000); } if(page == 1) { fontRenderer.drawString("Minor Skills ", centerX+40, centerY-5, 0x000000); fontRenderer.drawString("Strength: " + playerStats.getStat(StatConstants.STRENGTH), centerX+40, centerY+10, 0x000000); fontRenderer.drawString("Endurance: " + playerStats.getStat(StatConstants.ENDURANCE), centerX+40, centerY+20, 0x000000); fontRenderer.drawString("Speed: " + playerStats.getStat(StatConstants.SPEED), centerX+40, centerY+30, 0x000000); fontRenderer.drawString("Agility: " + playerStats.getStat(StatConstants.AGILITY), centerX+40, centerY+40, 0x000000); fontRenderer.drawString("Intelligence: " + playerStats.getStat(StatConstants.INTELLIGENCE), centerX+40, centerY+50, 0x000000); fontRenderer.drawString("Miscelleaneous Skills", centerX+40, centerY+75, 0x000000); fontRenderer.drawString("Long Blade " + playerSkills.getStat(StatConstants.LONG_BLADE), centerX+40, centerY+90, 0x000000); fontRenderer.drawString("Short Blade " + playerSkills.getStat(StatConstants.SHORT_BLADE), centerX+40, centerY+100, 0x000000); fontRenderer.drawString("Axe " + playerSkills.getStat(StatConstants.AXE), centerX+40, centerY+110, 0x000000); fontRenderer.drawString("Spear " + playerSkills.getStat(StatConstants.SPEAR), centerX+40, centerY+120, 0x000000); fontRenderer.drawString("Heavy Armor " + playerSkills.getStat(StatConstants.HEAVY_ARMOR), centerX+40, centerY+130, 0x000000); } super.drawScreen(mouseX, mouseY, partialTicks); } @Override public void initGui() { buttonList.clear(); int centerX =(width / 2) - guiWidth / 2; int centerY = (height /2) - guiHeight / 2; buttonList.add(button1 = new GuiButton(BUTTON1, centerX+40, centerY +170, 100, 20, "Done")); buttonList.add(button2 = new GuiButton(BUTTON2, 0, 0, 50, 20, "Next")); buttonList.add(button3 = new GuiButton(BUTTON3, 0, 30, 50, 20, "Previous")); button3.enabled = false; super.initGui(); } public void updateButtons() { if(page == 1) { button2.enabled = false; button3.enabled = true; } if(page == 0) { button3.enabled = false; button2.enabled = true; } } @Override protected void actionPerformed(GuiButton button) throws IOException { switch (button.id) { case BUTTON1: Minecraft.getMinecraft().displayGuiScreen(null); break; case BUTTON2: if(page == 0) page++; break; case BUTTON3: if(page == 1) page--; break; } updateButtons(); super.actionPerformed(button); } @Override public boolean doesGuiPauseGame() { return true; } For example this line fontRenderer.drawString("Long Blade " + playerSkills.getStat(StatConstants.LONG_BLADE), centerX+40, centerY+90, 0x000000); will display Long Blade 0 (which is the value it is initialized to in the default implementation of IStats) instead of the actual value that's supposed to be there. How can I make make sure I am getting the correct data from the player? The mod is only meant to be single player if that changes anything Edited January 20, 20196 yr by teh_black_d00m
January 20, 20196 yr 46 minutes ago, teh_black_d00m said: fontRenderer.drawString("Long Blade " + playerSkills.getStat(StatConstants.LONG_BLADE), centerX+40, centerY+90, 0x000000); Where are you setting those StatConstants.LONG_BLADE? maybe you are calling the drawString before the actual setting.
January 20, 20196 yr Are you syncing the values to the client? By default capability data is not synchronised between the server and client. About Me Spoiler My Discord - Cadiboo#8887 My Website - Cadiboo.github.io My Mods - Cadiboo.github.io/projects My Tutorials - Cadiboo.github.io/tutorials Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support. When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible. Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)
January 20, 20196 yr Author 2 hours ago, Cadiboo said: Are you syncing the values to the client? By default capability data is not synchronised between the server and client. Probably not because I don't know how to do that haha. For each field in the default implementation of IStats I've done this public class FatigueStorage implements IStorage<IStats> { @Override public NBTBase writeNBT(Capability<IStats> capability, IStats instance, EnumFacing side) { final NBTTagCompound tag = new NBTTagCompound(); tag.setFloat("fatigue", instance.getFatigue()); return tag; } @Override public void readNBT(Capability<IStats> capability, IStats instance, EnumFacing side, NBTBase nbt) { final NBTTagCompound tag = (NBTTagCompound) nbt; instance.setFatigue(tag.getFloat("fatigue")); } } Is that not enough? Does the data still need to be synced even if the mod is only multiplayer? EDIT: I mean the EXACT OPPOSITE. its singleplayer only Edited January 20, 20196 yr by teh_black_d00m typo
January 20, 20196 yr Author 2 hours ago, De Joker said: Where are you setting those StatConstants.LONG_BLADE? maybe you are calling the drawString before the actual setting. Those are all static final variables that are set in the StatConstants class. They are just used as indexes for the arrays holding the actual data
January 20, 20196 yr You need to make a packet to sync your capability data, I would give an example, but all my implementations are horrible, a quick forum search should yield some good results. The basics of it are - have a (logical sever side) packet that implements IMessage and IMessageHandler - register that packet for the logical server in your network manager - send the packet when you need to sync data An example of a network manager can be found here, and it is instantiated here About Me Spoiler My Discord - Cadiboo#8887 My Website - Cadiboo.github.io My Mods - Cadiboo.github.io/projects My Tutorials - Cadiboo.github.io/tutorials Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support. When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible. Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)
January 20, 20196 yr Author Okay I watched this tutorial Spoiler and i think I i mostly understand how to send packets but I'm still not really sure when I'm supposed to send one. I also dont know why when I try to display my custom NBT data using a Gui its not synced. Here is a class that draws some bars displaying the player's health and fatigue(capability I made) Spoiler public class GUIHandler { @SubscribeEvent public void onRenderHealthBar(Pre event) { if (event.getType().equals(RenderGameOverlayEvent.ElementType.HEALTH)) { event.setCanceled(true); // Cancels the rendering of the HEALTH bar. Take a look at all the different ElementType's } if (event.getType().equals(RenderGameOverlayEvent.ElementType.ARMOR)) { event.setCanceled(true); } } private final ResourceLocation bar = new ResourceLocation(Reference.MOD_ID, "textures/gui/hpbar.png"); private final ResourceLocation fatigueBar = new ResourceLocation(Reference.MOD_ID, "textures/gui/fatiguebar.png"); private final int textureWidth = 102; private final int textureHeight = 8; private final int barWidth = 100; private final int barHeight = 6; @SubscribeEvent public void onRenderGui(RenderGameOverlayEvent.Post event) { if (event.getType() != ElementType.EXPERIENCE) return; Minecraft mc = Minecraft.getMinecraft(); //render health bar mc.renderEngine.bindTexture(bar); float healthRatio = mc.player.getHealth() / mc.player.getMaxHealth(); int currentWidth = (int)(barWidth * healthRatio); int maxHealthInt = (int) mc.player.getMaxHealth(); int currentHealthInt = (int) mc.player.getHealth(); String maxHealth = String.valueOf(maxHealthInt); String currentHealth = String.valueOf(currentHealthInt); //int posX = event.getResolution().getScaledWidth() / 2 +10; //int posY = event.getResolution().getScaledHeight() - 48; mc.ingameGUI.drawTexturedModalRect(5, 220, 0, 0, textureWidth, textureHeight); mc.ingameGUI.drawTexturedModalRect(6, 220, 1, textureHeight, currentWidth, textureHeight); //render fatigue bar IStats fatigueValue = mc.player.getCapability(StatsProvider.STATS_CAPABILITY, null); mc.renderEngine.bindTexture(fatigueBar); float fatigueRatio = fatigueValue.getFatigue() / fatigueValue.getMaxFatigue(); int currentWidthF = (int)(barWidth * fatigueRatio); int maxFatigueInt = (int) fatigueValue.getMaxFatigue(); int currentFatigueInt = (int) fatigueValue.getFatigue(); String maxFatigue = String.valueOf(maxFatigueInt); String currentFatigue = String.valueOf(currentFatigueInt); mc.ingameGUI.drawTexturedModalRect(5, 235, 0, 0, textureWidth, textureHeight); mc.ingameGUI.drawTexturedModalRect(6, 235, 1, textureHeight, currentWidthF, textureHeight); mc.ingameGUI.drawString(mc.fontRenderer, currentFatigue + "/" + maxFatigue, 45, 235, Integer.parseInt("FFAA00", 16)); mc.ingameGUI.drawString(mc.fontRenderer, currentHealth + "/" + maxHealth, 45, 220, Integer.parseInt("FFAA00", 16)); } } Here is code that restores the player's fatigue Spoiler /* //Handles the return rate of fatigue every tick EntityPlayer player = (EntityPlayer) event.getEntity(); if(player.getCapability(StatsProvider.STATS_CAPABILITY, null).getFatigue() < player.getCapability(StatsProvider.STATS_CAPABILITY, null).getMaxFatigue()) { float fFatigueReturnBase = 0.25F; float fFatigueReturnMult = 0.02F; float x = fFatigueReturnBase + fFatigueReturnMult * player.getCapability(StatsProvider.STATS_CAPABILITY, null).getStat(StatConstants.ENDURANCE); if (x + player.getCapability(StatsProvider.STATS_CAPABILITY, null).getFatigue() > player.getCapability(StatsProvider.STATS_CAPABILITY, null).getMaxFatigue()) player.getCapability(StatsProvider.STATS_CAPABILITY, null).setFatigue(player.getCapability(StatsProvider.STATS_CAPABILITY, null).getMaxFatigue()); else player.getCapability(StatsProvider.STATS_CAPABILITY, null).fill(x); } */ I put that code in a packet and I know that its working like its supposed to (restoring the fatigue) but for some reason the fatigue bar is not being draw correctly to show that its working. It actually was working before when I wasn't using a packet. Any idea why this is happening?
January 20, 20196 yr Author 6 minutes ago, diesieben07 said: I have no idea what you mean by "putting that code in a packet". You can't put "code in a packet". I mean to say that I moved that code I posted earlier so that its here now Spoiler public class MessageRestoreFatigue extends MessageBase<MessageRestoreFatigue>{ @Override public void fromBytes(ByteBuf buf) { // TODO Auto-generated method stub } @Override public void toBytes(ByteBuf buf) { // TODO Auto-generated method stub } @Override public void handleClientSide(MessageRestoreFatigue message, EntityPlayer player) { // TODO Auto-generated method stub } @Override public void handleServerSide(MessageRestoreFatigue message, EntityPlayer player) { // do whatever you need to do if(player.getCapability(StatsProvider.STATS_CAPABILITY, null).getFatigue() < player.getCapability(StatsProvider.STATS_CAPABILITY, null).getMaxFatigue()) { float fFatigueReturnBase = 0.25F; float fFatigueReturnMult = 0.02F; float x = fFatigueReturnBase + fFatigueReturnMult * player.getCapability(StatsProvider.STATS_CAPABILITY, null).getStat(StatConstants.ENDURANCE); if (x + player.getCapability(StatsProvider.STATS_CAPABILITY, null).getFatigue() > player.getCapability(StatsProvider.STATS_CAPABILITY, null).getMaxFatigue()) player.getCapability(StatsProvider.STATS_CAPABILITY, null).setFatigue(player.getCapability(StatsProvider.STATS_CAPABILITY, null).getMaxFatigue()); else player.getCapability(StatsProvider.STATS_CAPABILITY, null).fill(x); System.out.println("Fatigue is being restored"); } } } And then I did this Spoiler @SubscribeEvent public void updateFatigue(LivingUpdateEvent event) { if(!(event.getEntity() instanceof EntityPlayer)) return; /* //Handles the return rate of fatigue every tick EntityPlayer player = (EntityPlayer) event.getEntity(); if(player.getCapability(StatsProvider.STATS_CAPABILITY, null).getFatigue() < player.getCapability(StatsProvider.STATS_CAPABILITY, null).getMaxFatigue()) { float fFatigueReturnBase = 0.25F; float fFatigueReturnMult = 0.02F; float x = fFatigueReturnBase + fFatigueReturnMult * player.getCapability(StatsProvider.STATS_CAPABILITY, null).getStat(StatConstants.ENDURANCE); if (x + player.getCapability(StatsProvider.STATS_CAPABILITY, null).getFatigue() > player.getCapability(StatsProvider.STATS_CAPABILITY, null).getMaxFatigue()) player.getCapability(StatsProvider.STATS_CAPABILITY, null).setFatigue(player.getCapability(StatsProvider.STATS_CAPABILITY, null).getMaxFatigue()); else player.getCapability(StatsProvider.STATS_CAPABILITY, null).fill(x); } */ NetworkHandler.sendToServer(new MessageRestoreFatigue()); } I know it worked because it kept printing "Fatigue is being restored". 11 minutes ago, diesieben07 said: For capability data that is always needed on the client (like in your case for the HUD [not GUI!]), you need to send the packet in the following cases: PlayerLoggedInEvent PlayerRespawnEvent PlayerChangedDimensionEvent Whenever the data changes. So am I correct in assuming that I need to send a packet in the above example (because I am changing the amount of fatigue the player has)? And if I send the packets correctly in the cases you mentioned my HUD will display the correct data right?
January 20, 20196 yr Author 12 minutes ago, diesieben07 said: Why did you do this? I did that because I thought that's what you're supposed to do to send a packet (put the code into a class that implements IMessage and IMessageHandler) but maybe I'm still not quite understanding packets correctly. So for the example of restoring the player's fatigue I need to use that MessageRestoreFatigue class to send a packet right? I'm not sure of the correctly terminology here but I think I'm supposed to be sending data between the client and server so they are synced. Should I be using the toBytes and fromBytes methods to just send the data instead of doing what I did in the example (moving all the code into MessageRestoreFatigue)?
January 20, 20196 yr "Packets" are a conversion between runtime data and a binary serialization format. That's all. Look at this example: https://github.com/Draco18s/ReasonableRealism/blob/master/src/main/java/com/draco18s/ores/networking/ToClientMessageOreParticles.java The only "code" that is there is writing to, and reading from, a ByteBuffer. Your MessageRestoreFatigue does fuckall: it writes no data and it reads no data and instead...gets the capability on the client and directly modifies the values there. What you should be doing, is sending a packet from the server to the client containing the fatigue value. Your packet also does that on the networking thread, which is a major no-no. Edited January 20, 20196 yr by Draco18s 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.
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.