Posted February 16, 201510 yr So I am new to packets and extended properties and I was wondering if anyone could help me with them. As far as I know, it works... kinda? but the GUI doesn't open when I click my button. Here is my abstract packet class: package com.feudalcraft.network; import net.minecraft.entity.player.EntityPlayer; import com.feudalcraft.main.MainRegistry; import cpw.mods.fml.common.network.simpleimpl.IMessage; import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; import cpw.mods.fml.common.network.simpleimpl.MessageContext; import cpw.mods.fml.relauncher.Side; public abstract class FeudalPacket<REQ extends IMessage> implements IMessage, IMessageHandler<REQ, REQ> { public REQ onMessage(REQ message, MessageContext ctx) { if (ctx.side == Side.SERVER) { handleServerSide(message, ctx.getServerHandler().playerEntity); } else { handleClientSide(message, MainRegistry.proxy.getPlayer()); } return null; } public abstract void handleClientSide(REQ paramREQ, EntityPlayer paramEntityPlayer); public abstract void handleServerSide(REQ paramREQ, EntityPlayer paramEntityPlayer); } Here is my packet handler: package com.feudalcraft.network; import net.minecraft.entity.player.EntityPlayerMP; import com.feudalcraft.lib.Strings; import com.feudalcraft.network.play.client.OpenGuiPacket; import com.feudalcraft.network.play.client.SyncPlayerPropsPacket; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.network.simpleimpl.IMessage; import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper; import cpw.mods.fml.relauncher.Side; public class FeudalPacketHandler { public static final SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel(Strings.PACKET_CHANNEL); private static int discriminant; public static void init() { INSTANCE.registerMessage(OpenGuiPacket.class, OpenGuiPacket.class, discriminant++, Side.SERVER); INSTANCE.registerMessage(OpenGuiPacket.class, OpenGuiPacket.class, discriminant++, Side.CLIENT); INSTANCE.registerMessage(SyncPlayerPropsPacket.class, SyncPlayerPropsPacket.class, discriminant++, Side.SERVER); INSTANCE.registerMessage(SyncPlayerPropsPacket.class, SyncPlayerPropsPacket.class, discriminant++, Side.CLIENT); } public static void sendToAll(IMessage message) { INSTANCE.sendToAll(message); } public static void sendTo(IMessage message, EntityPlayerMP player) { INSTANCE.sendTo(message, player); } public static void sendToAllAround(IMessage message, NetworkRegistry.TargetPoint point) { INSTANCE.sendToAllAround(message, point); } public static void sendToDimension(IMessage message, int dimensionId) { INSTANCE.sendToDimension(message, dimensionId); } public static void sendToServer(IMessage message) { INSTANCE.sendToServer(message); } } Here is my class that extends IExtendedEntityProperties: package com.feudalcraft.client.entity.player; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.World; import net.minecraftforge.common.IExtendedEntityProperties; import com.feudalcraft.main.CommonProxy; import com.feudalcraft.main.MainRegistry; import com.feudalcraft.network.play.client.SyncPlayerPropsPacket; public class ExtendedPlayer implements IExtendedEntityProperties { public final static String EXTENDED_ENTITY_PLAYER = "ExtendedEntityPlayer"; public EntityPlayer player; public CustomPlayerInventory inventory = new CustomPlayerInventory(player); public ExtendedPlayer(EntityPlayer player) { this.player = player; } public static final void register(EntityPlayer player) { player.registerExtendedProperties(ExtendedPlayer.EXTENDED_ENTITY_PLAYER, new ExtendedPlayer(player)); } /** * Returns ExtendedPlayer properties for player * This method is for convenience only. */ public static final ExtendedPlayer get(EntityPlayer player) { return (ExtendedPlayer) player.getExtendedProperties(EXTENDED_ENTITY_PLAYER); } /** * Called when the entity that this class is attached to is saved. * Any custom entity data that needs saving should be saved here. * @param compound The compound to save to. */ @Override public void saveNBTData(NBTTagCompound compound) { NBTTagCompound properties = new NBTTagCompound(); compound.setTag(EXTENDED_ENTITY_PLAYER, properties); this.inventory.writeToNBT(properties); } /** * Called when the entity that this class is attached to is loaded. * In order to hook into this, you will need to subscribe to the EntityConstructing event. * Otherwise, you will need to initialize manually. * @param compound The compound to load from. */ @Override public void loadNBTData(NBTTagCompound compound) { NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXTENDED_ENTITY_PLAYER); this.inventory.readFromNBT(properties); } /** * Used to initialize the extended properties with the entity that this is attached to, as well * as the world object. * Called automatically if you register with the EntityConstructing event. * May be called multiple times if the extended properties is moved over to a new entity. * Such as when a player switches dimension {Minecraft re-creates the player entity} * @param entity The entity that this extended properties is attached to * @param world The world in which the entity exists */ @Override public void init(Entity entity, World world) {} private static final String getSaveKey(EntityPlayer player) { return player.getCommandSenderName() + ":" + EXTENDED_ENTITY_PLAYER; } public static final void loadProxyData(EntityPlayer player) { ExtendedPlayer playerData = ExtendedPlayer.get(player); NBTTagCompound savedData = CommonProxy.getEntityData(getSaveKey(player)); if (savedData != null) { playerData.loadNBTData(savedData); } MainRegistry.packetFeudal.sendTo(new SyncPlayerPropsPacket(player), (EntityPlayerMP) player); } } My EntityConstructing class: package com.feudalcraft.handler; import net.minecraft.entity.player.EntityPlayer; import net.minecraftforge.event.entity.EntityEvent.EntityConstructing; import com.feudalcraft.client.entity.player.ExtendedPlayer; import cpw.mods.fml.common.eventhandler.SubscribeEvent; public class FeudalEventHandler { @SubscribeEvent public void onEntityConstructing(EntityConstructing event) { if (event.entity instanceof EntityPlayer && ExtendedPlayer.get((EntityPlayer) event.entity) == null) ExtendedPlayer.register((EntityPlayer) event.entity); if (event.entity instanceof EntityPlayer && event.entity.getExtendedProperties(ExtendedPlayer.EXTENDED_ENTITY_PLAYER) == null) event.entity.registerExtendedProperties(ExtendedPlayer.EXTENDED_ENTITY_PLAYER, new ExtendedPlayer((EntityPlayer) event.entity)); } } My OpenGuiPacket: package com.feudalcraft.network.play.client; import io.netty.buffer.ByteBuf; import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.inventory.Container; import com.feudalcraft.main.MainRegistry; import com.feudalcraft.network.FeudalPacket; import com.feudalcraft.network.FeudalPacketHandler; public class OpenGuiPacket extends FeudalPacket<OpenGuiPacket> { int containerId; int x; int y; int z; public OpenGuiPacket(){} public OpenGuiPacket(int containerId) { this.containerId = containerId; } @Override public void fromBytes(ByteBuf buf) { this.containerId = buf.readInt(); this.x = buf.readInt(); this.y = buf.readInt(); this.z = buf.readInt(); } @Override public void toBytes(ByteBuf buf) { buf.writeInt(this.containerId); buf.writeInt(this.x); buf.writeInt(this.y); buf.writeInt(this.z); } @Override public void handleClientSide(OpenGuiPacket message, EntityPlayer entity) { System.out.println("WORKING...?"); Minecraft.getMinecraft().thePlayer.openContainer.windowId = message.containerId; } @Override public void handleServerSide(OpenGuiPacket message, EntityPlayer player) { if ((player instanceof EntityPlayerMP)) { EntityPlayerMP entityPlayer = (EntityPlayerMP)player; Container container = (Container)MainRegistry.guiHandler.getServerGuiElement(message.containerId, entityPlayer, entityPlayer.worldObj, (int)entityPlayer.posZ, (int)entityPlayer.posY, (int)entityPlayer.posZ); if (container != null) { entityPlayer.getNextWindowId(); entityPlayer.closeContainer(); int windowID = entityPlayer.currentWindowId; entityPlayer.openContainer = container; entityPlayer.openContainer.windowId = windowID; FeudalPacketHandler.sendTo(new OpenGuiPacket(windowID), entityPlayer); } } } } My SyncPlayerPropsPacket: package com.feudalcraft.network.play.client; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; import com.feudalcraft.client.entity.player.ExtendedPlayer; import com.feudalcraft.network.FeudalPacket; import cpw.mods.fml.common.network.ByteBufUtils; import cpw.mods.fml.common.network.simpleimpl.IMessage; public class SyncPlayerPropsPacket extends FeudalPacket<SyncPlayerPropsPacket> { private NBTTagCompound data; public SyncPlayerPropsPacket(){} public SyncPlayerPropsPacket(EntityPlayer player) { data = new NBTTagCompound(); ExtendedPlayer.get(player).saveNBTData(data); } @Override public void fromBytes(ByteBuf buffer) { ByteBufUtils.writeTag(buffer, data); } @Override public void toBytes(ByteBuf buffer) { data = ByteBufUtils.readTag(buffer); } @Override public void handleClientSide(SyncPlayerPropsPacket paramREQ, EntityPlayer player) { ExtendedPlayer.get(player).loadNBTData(data); } @Override public void handleServerSide(SyncPlayerPropsPacket paramREQ, EntityPlayer paramEntityPlayer) {} } Custom Inventory Container: package com.feudalcraft.container; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.InventoryCraftResult; import net.minecraft.inventory.InventoryCrafting; import net.minecraft.inventory.Slot; import net.minecraft.inventory.SlotCrafting; import net.minecraft.item.ItemArmor; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.CraftingManager; import net.minecraft.util.IIcon; import com.feudalcraft.client.entity.player.CustomPlayerInventory; import com.feudalcraft.client.entity.player.ExtendedPlayer; import com.feudalcraft.client.entity.player.SlotAccessory; import com.feudalcraft.client.entity.player.SlotArmor; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; public class ContainerCustomInventory extends Container { /** * Avoid magic numbers! This will greatly reduce the chance of you making * errors in 'transferStackInSlot' method */ private static final int ARMOR_START = CustomPlayerInventory.INV_SIZE, ARMOR_END = ARMOR_START + 3, INV_START = ARMOR_END + 1, INV_END = INV_START + 26, HOTBAR_START = INV_END + 1, HOTBAR_END = HOTBAR_START + 8; /** The crafting matrix inventory. */ public InventoryCrafting craftMatrix = new InventoryCrafting(this, 2, 2); public IInventory craftResult = new InventoryCraftResult(); /** Determines if inventory manipulation should be handled. */ public boolean isLocalWorld; private final EntityPlayer thePlayer; public ContainerCustomInventory(ExtendedPlayer playerExtended, EntityPlayer player) { this.thePlayer = player; } public ContainerCustomInventory(InventoryPlayer inventoryplayer, boolean b, EntityPlayer entityplayer,CustomPlayerInventory inventoryCustom) { this.isLocalWorld = b; this.thePlayer = entityplayer; this.addSlotToContainer(new SlotCrafting(inventoryplayer.player, this.craftMatrix, this.craftResult, 0, 125, 8 )); this.addSlotToContainer(new SlotAccessory(inventoryCustom, 0, null, 80, 8 )); this.addSlotToContainer(new SlotAccessory(inventoryCustom, 1, null, 80, 26)); this.addSlotToContainer(new SlotAccessory(inventoryCustom, 2, null, 80, 44)); this.addSlotToContainer(new SlotAccessory(inventoryCustom, 3, null, 80, 62)); int i; int j; for (i = 0; i < 2; ++i) { for (j = 0; j < 2; ++j) { this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 2, 88 + j * 18, 26 + i * 18)); } } for (i = 0; i < 4; ++i) { this.addSlotToContainer(new SlotArmor(entityplayer, inventoryplayer, inventoryplayer.getSizeInventory() - 1 - i, 8, 8 + i * 18, i)); } for (i = 0; i < 4; ++i) { final int k = i; this.addSlotToContainer(new Slot(inventoryplayer, inventoryplayer.getSizeInventory() - 1 - i, 8, 8 + i * 18) { /** * Returns the maximum stack size for a given slot (usually the same as getInventoryStackLimit(), but 1 * in the case of armor slots) */ public int getSlotStackLimit() { return 1; } /** * Check if the stack is a valid item for this slot. Always true beside for the armor slots. */ public boolean isItemValid(ItemStack p_75214_1_) { if (p_75214_1_ == null) return false; return p_75214_1_.getItem().isValidArmor(p_75214_1_, k, thePlayer); } /** * Returns the icon index on items.png that is used as background image of the slot. */ @SideOnly(Side.CLIENT) public IIcon getBackgroundIconIndex() { return ItemArmor.func_94602_b(k); } }); } for (i = 0; i < 3; ++i) { for (j = 0; j < 9; ++j) { this.addSlotToContainer(new Slot(inventoryplayer, j + (i + 1) * 9, 8 + j * 18, 84 + i * 18)); } } for (i = 0; i < 9; ++i) { this.addSlotToContainer(new Slot(inventoryplayer, i, 8 + i * 18, 142)); } this.onCraftMatrixChanged(this.craftMatrix); } /** * Callback for when the crafting matrix is changed. */ public void onCraftMatrixChanged(IInventory p_75130_1_) { this.craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.thePlayer.worldObj)); } /** * Called when the container is closed. */ public void onContainerClosed(EntityPlayer p_75134_1_) { super.onContainerClosed(p_75134_1_); for (int i = 0; i < 4; ++i) { ItemStack itemstack = this.craftMatrix.getStackInSlotOnClosing(i); if (itemstack != null) { p_75134_1_.dropPlayerItemWithRandomChoice(itemstack, false); } } this.craftResult.setInventorySlotContents(0, (ItemStack)null); } public boolean canInteractWith(EntityPlayer p_75145_1_) { return true; } /** * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that. */ public ItemStack transferStackInSlot(EntityPlayer p_82846_1_, int p_82846_2_) { ItemStack itemstack = null; Slot slot = (Slot)this.inventorySlots.get(p_82846_2_); if (slot != null && slot.getHasStack()) { ItemStack itemstack1 = slot.getStack(); itemstack = itemstack1.copy(); if (p_82846_2_ == 0) { if (!this.mergeItemStack(itemstack1, 9, 45, true)) { return null; } slot.onSlotChange(itemstack1, itemstack); } else if (p_82846_2_ >= 1 && p_82846_2_ < 5) { if (!this.mergeItemStack(itemstack1, 9, 45, false)) { return null; } } else if (p_82846_2_ >= 5 && p_82846_2_ < 9) { if (!this.mergeItemStack(itemstack1, 9, 45, false)) { return null; } } else if (itemstack.getItem() instanceof ItemArmor && !((Slot)this.inventorySlots.get(5 + ((ItemArmor)itemstack.getItem()).armorType)).getHasStack()) { int j = 5 + ((ItemArmor)itemstack.getItem()).armorType; if (!this.mergeItemStack(itemstack1, j, j + 1, false)) { return null; } } else if (p_82846_2_ >= 9 && p_82846_2_ < 36) { if (!this.mergeItemStack(itemstack1, 36, 45, false)) { return null; } } else if (p_82846_2_ >= 36 && p_82846_2_ < 45) { if (!this.mergeItemStack(itemstack1, 9, 36, false)) { return null; } } else if (!this.mergeItemStack(itemstack1, 9, 45, false)) { return null; } if (itemstack1.stackSize == 0) { slot.putStack((ItemStack)null); } else { slot.onSlotChanged(); } if (itemstack1.stackSize == itemstack.stackSize) { return null; } slot.onPickupFromSlot(p_82846_1_, itemstack1); } return itemstack; } public boolean func_94530_a(ItemStack p_94530_1_, Slot p_94530_2_) { return p_94530_2_.inventory != this.craftResult && super.func_94530_a(p_94530_1_, p_94530_2_); } } Custom Inventory Gui: package com.feudalcraft.gui; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL12; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.gui.inventory.GuiContainerCreative; import net.minecraft.client.renderer.OpenGlHelper; import net.minecraft.client.renderer.RenderHelper; import net.minecraft.client.renderer.entity.RenderManager; import net.minecraft.client.resources.I18n; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.util.ResourceLocation; import com.feudalcraft.client.entity.player.ContainerCustomPlayerInventory; import com.feudalcraft.client.entity.player.CustomPlayerInventory; import com.feudalcraft.client.entity.player.ExtendedPlayer; import com.feudalcraft.container.ContainerCustomInventory; import com.feudalcraft.lib.Strings; import cpw.mods.fml.relauncher.*; @SideOnly(Side.CLIENT) public class GuiCustomInventory extends GuiContainer { /** x size of the inventory window in pixels. Defined as float, passed as int */ private float xSizeFloat; /** y size of the inventory window in pixels. Defined as float, passed as int. */ private float ySizeFloat; /** Could use IInventory type to be more generic, but this way will save an import... */ private CustomPlayerInventory customInventory = new CustomPlayerInventory(); public static final ResourceLocation inventory = new ResourceLocation(Strings.MOD_ID + ":textures/gui/inventory.png"); public GuiCustomInventory(ExtendedPlayer openingPlayer) { super(new ContainerCustomInventory(openingPlayer, openingPlayer.player)); this.allowUserInput = true; } public GuiCustomInventory(EntityPlayer player, InventoryPlayer inventoryPlayer, CustomPlayerInventory inventoryCustom) { super(new ContainerCustomPlayerInventory(player, inventoryPlayer, inventoryCustom)); this.customInventory = inventoryCustom; this.allowUserInput = true; System.out.println("This is in GuiCustomInventory"); } /** * Draw the foreground layer for the GuiContainer (everything in front of the items) */ protected void drawGuiContainerForegroundLayer(int i, int j) { String s = this.customInventory.hasCustomInventoryName() ? this.customInventory.getInventoryName() : I18n.format(this.customInventory.getInventoryName()); this.fontRendererObj.drawString(s, this.xSize - this.fontRendererObj.getStringWidth(s), 12, 4210752); this.fontRendererObj.drawString(I18n.format("container.inventory"), 80, this.ySize - 96, 4210752); this.fontRendererObj.drawString(I18n.format("container.crafting", new Object[0]), 120, 2, 4210752); } /** * Draws the screen and all the components in it. */ public void drawScreen(int i, int j, float f) { super.drawScreen(i, j, f); this.xSizeFloat = (float)i; this.ySizeFloat = (float)j; } /** * Draw the background layer for the GuiContainer (everything behind the items) */ protected void drawGuiContainerBackgroundLayer(float f, int i, int j) { GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); this.mc.getTextureManager().bindTexture(inventory); int k = this.guiLeft; int l = this.guiTop; this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize); func_147046_a(k + 51, l + 75, 30, (float)(k + 51) - this.xSizeFloat, (float)(l + 75 - 50) - this.ySizeFloat, this.mc.thePlayer); } public static void func_147046_a(int x, int y, int z, float f, float f1, EntityLivingBase entitylivingbase) { GL11.glEnable(GL11.GL_COLOR_MATERIAL); GL11.glPushMatrix(); GL11.glTranslatef((float)x, (float)y, 50.0F); GL11.glScalef((float)(-z), (float)z, (float)z); GL11.glRotatef(180.0F, 0.0F, 0.0F, 1.0F); float f2 = entitylivingbase.renderYawOffset; float f3 = entitylivingbase.rotationYaw; float f4 = entitylivingbase.rotationPitch; float f5 = entitylivingbase.prevRotationYawHead; float f6 = entitylivingbase.rotationYawHead; GL11.glRotatef(135.0F, 0.0F, 1.0F, 0.0F); RenderHelper.enableStandardItemLighting(); GL11.glRotatef(-135.0F, 0.0F, 1.0F, 0.0F); GL11.glRotatef(-((float)Math.atan((double)(f1 / 40.0F))) * 20.0F, 1.0F, 0.0F, 0.0F); entitylivingbase.renderYawOffset = (float)Math.atan((double)(f / 40.0F)) * 20.0F; entitylivingbase.rotationYaw = (float)Math.atan((double)(f / 40.0F)) * 40.0F; entitylivingbase.rotationPitch = -((float)Math.atan((double)(f1 / 40.0F))) * 20.0F; entitylivingbase.rotationYawHead = entitylivingbase.rotationYaw; entitylivingbase.prevRotationYawHead = entitylivingbase.rotationYaw; GL11.glTranslatef(0.0F, entitylivingbase.yOffset, 0.0F); RenderManager.instance.playerViewY = 180.0F; RenderManager.instance.renderEntityWithPosYaw(entitylivingbase, 0.0D, 0.0D, 0.0D, 0.0F, 1.0F); entitylivingbase.renderYawOffset = f2; entitylivingbase.rotationYaw = f3; entitylivingbase.rotationPitch = f4; entitylivingbase.prevRotationYawHead = f5; entitylivingbase.rotationYawHead = f6; GL11.glPopMatrix(); RenderHelper.disableStandardItemLighting(); GL11.glDisable(GL12.GL_RESCALE_NORMAL); OpenGlHelper.setActiveTexture(OpenGlHelper.lightmapTexUnit); GL11.glDisable(GL11.GL_TEXTURE_2D); OpenGlHelper.setActiveTexture(OpenGlHelper.defaultTexUnit); } }
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.