teh_black_d00m
-
Posts
9 -
Joined
-
Last visited
Posts posted by teh_black_d00m
-
-
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
Spoilerpublic 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?
-
Okay I watched this tutorial
Spoilerand 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)
Spoilerpublic 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?
-
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
-
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
-
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
Spoilerif(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
Spoilerpublic 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
-
Works perfectly, thanks!
-
I'm making a Capability that requires I store data in a float array and it looks like NBT Tag compound only has a method for integer arrays. Is there some way around this or do I need to use an integer array instead?
-
I thought I could use ItemSword.GetAttackDamage() to get the weapon's attack damage but that just gives the base value. How can I get the value with all damage modifiers applied?
Using Minecraft#player to get Player NBT Data Not Working
in Modder Support
Posted
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)?