Jump to content

TheMCJavaFre4k

Members
  • Posts

    84
  • Joined

  • Last visited

Everything posted by TheMCJavaFre4k

  1. Yea - its just something I've forgotten - Ive got around 2 and a half years of java experience under my belt but haven't been doing it much lately Ive just tried that and now the item doesn't even appear at all her is my code now: package net.PartyMod.Common; import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; import net.PartyMod.GUI.TileEntityRecordingUnit; import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.network.INetworkManager; import net.minecraft.network.packet.Packet250CustomPayload; import net.minecraft.tileentity.TileEntity; import cpw.mods.fml.common.network.IPacketHandler; import cpw.mods.fml.common.network.Player; public class PacketHandler implements IPacketHandler { public int discNo; public int PosX, PosY, PosZ; TileEntityRecordingUnit te = new TileEntityRecordingUnit(); @Override public void onPacketData(INetworkManager manager, Packet250CustomPayload par1, Player par2) { if (par1.channel.equals("PartyMod1")) { this.handlePacketData(par1, (EntityPlayer) par2); } } private void handlePacketData(Packet250CustomPayload par1, EntityPlayer player) { DataInputStream In = new DataInputStream(new ByteArrayInputStream(par1.data)); try { PosX = In.readInt(); PosY = In.readInt(); PosZ = In.readInt(); discNo = In.readInt(); } catch (IOException e) { e.printStackTrace(); return; } TileEntity tile = player.worldObj.getBlockTileEntity(PosX, PosY, PosZ); ItemStack e = ((TileEntityRecordingUnit) tile).getStackInSlot(0); System.out.print("Step 1"); if (tile instanceof TileEntityRecordingUnit) { System.out.print("Step 2"); ((TileEntityRecordingUnit) tile).setInventorySlotContents(0, new ItemStack(Item.appleGold, 1, 0));; ((TileEntityRecordingUnit) tile).onInventoryChanged(); } } } Ive renamed the tile entity to my decided name of the block
  2. Ok i can see where i am probably going wrong. Where you have the Player player in the method params - when i try to access the world object it only comes up with basic class things like" wait(), equals(), getClass(), notify() etc...; so instead I've been using the Minecraft.getMinecraft.theWorld but that probably gets it on the client? This is my code: package net.PartyMod.Common; import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; import net.PartyMod.GUI.TileEntityRecordingUnit; import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.network.INetworkManager; import net.minecraft.network.packet.Packet250CustomPayload; import net.minecraft.tileentity.TileEntity; import cpw.mods.fml.common.network.IPacketHandler; import cpw.mods.fml.common.network.Player; public class PacketHandler implements IPacketHandler { public int number; public int PosX, PosY, PosZ; TileEntityReplacer te = new TileEntityReplacer(); @Override public void onPacketData(INetworkManager manager, Packet250CustomPayload par1, Player par2) { if (par1.channel.equals("UniqueModID")) { this.handlePacketData(par1); } } private void handlePacketData(Packet250CustomPayload par1) { DataInputStream In = new DataInputStream(new ByteArrayInputStream(par1.data)); try { PosX = In.readInt(); PosY = In.readInt(); PosZ = In.readInt(); number = In.readInt(); } catch (IOException e) { e.printStackTrace(); return; } TileEntity tile = Minecraft.getMinecraft().theWorld.getBlockTileEntity(PosX, PosY, PosZ); ItemStack e = ((TileEntityReplacer) tile).getStackInSlot(0); if (tile instanceof TileEntityReplacer) { ((TileEntityReplacer) tile).setInventorySlotContents(0, new ItemStack(Item.appleGold, 1, 0));; ((TileEntityReplacerUnit) tile).onInventoryChanged(); } } } The number that is received is going to be used later to determine which button was pressed and what item to give. And this is how i create and send the packet: public void constructPacket(TileEntity te, int Number){ int PosX = te.xCoord; int PosY = te.yCoord; int PosZ = te.zCoord; ByteArrayOutputStream bos = new ByteArrayOutputStream(; DataOutputStream os = new DataOutputStream(bos); try { os.writeInt(PosX); os.writeInt(PosY); os.writeInt(PosZ); os.writeInt(buttonnumber); } catch (Exception ex) { ex.printStackTrace(); } Packet250CustomPayload packet = new Packet250CustomPayload(); packet.channel = "UniqueModID"; packet.data = bos.toByteArray(); packet.length = bos.size(); PacketDispatcher.sendPacketToServer(packet); } If i am correct and the problem is with getting the world object thru Minecraft.getMinecraft().theWorld Then how would i get the world if using the Player param doesn't work?
  3. i don't think that the packet handler is actually creating the item on the server side even though it is done in my packet handler
  4. Ok I've got the other one now but get and error if i have PacketDispatcher.sendPacketToServer(Packet packet) with the two packets but if i just have one it works like the others where i cannot actually pick up the item
  5. so do i need this code at all: Side side = FMLCommonHandler.instance().getEffectiveSide(); if (side == Side.SERVER) { // We are on the server side. EntityPlayerMP player = (EntityPlayerMP) playerEntity; } else if (side == Side.CLIENT) { EntityClientPlayerMP player = (EntityClientPlayerMP) playerEntity; // We are on the client side. } else { // We have an errornous state! } And i if i don't use that i can just use Minecraft.getMinecraft().thePlayer.sendQueue.addToSendQueue(packet) because its the guy class which is on the client side?
  6. Ok - ive had another read over it and seem to have missed out on the: Side side = FMLCommonHandler.instance().getEffectiveSide(); if (side == Side.SERVER) { // We are on the server side. EntityPlayerMP player = (EntityPlayerMP) playerEntity; } else if (side == Side.CLIENT) { EntityClientPlayerMP player = (EntityClientPlayerMP) playerEntity; // We are on the client side. } else { // We have an errornous state! } . But since this is in a custom methods to create and send the packet i don't have a playerEntity parameter. - Also if i can add this do i use: player.sendQueue.addToSendQueue(packet); or PacketDispatcher.sendPacketToServer(Packet packet); or can i use either. This is just really confusing and i just can't get it to work.
  7. I've added this: if(!Minecraft.getMinecraft().theWorld.isRemote){ System.out.print("On The Server"); } To my makeReplcement Method an it doesn't print anything. - How would i make it so it is running on the server and not the client side. Because I've been trying many things but I'm still having sync issues that creates the item in slot but when i try to take it it disappears(I'm guessing that these are client /server sync issues).
  8. How do i check if the packet handler is making the itemstack on the server side?
  9. all i am doing is sending the packet like the tutorial on the wiki says - I'm hoping that is to the server. Packet handlers are still a very new thing to me and i don't fully understand what side they are on or not so forgive me if i repeat my self If you wondering this is the tut i used http://www.minecraftforge.net/wiki/Packet_Handling
  10. Ok so I've made the appropriate changes and now it creates the item in slot but i think i have sync issues now as it just disappears when i try to pick it up. hers my code in my packet handler that creates the item: public void makeReplacement(int numberID){ TileEntity te = Minecraft.getMinecraft().thePlayer.worldObj.getBlockTileEntity(PosX, PosY, PosZ); if (te instanceof TileEntityReplacer) { ((TileEntityReplacer) te).setInventorySlotContents(0, new ItemStack(Item.appleGold));; } } I will be using the int numberID in later usage of the Gui
  11. Well this is what i have in my guy handler: package net.mod.GUI; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import cpw.mods.fml.common.network.IGuiHandler; public class GuiHandler implements IGuiHandler { //returns an instance of the Container you made earlier @Override public Object getServerGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) { TileEntity tileEntity = world.getBlockTileEntity(x, y, z); if(tileEntity instanceof TileEntityReplacer){ return new ContainerReplacer(player.inventory, (TileEntityReplacer) tileEntity); } return null; } //returns an instance of the Gui you made earlier @Override public Object getClientGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) { TileEntity tileEntity = world.getBlockTileEntity(x, y, z); if(tileEntity instanceof TileEntityReplacer){ return new GuiReplacer(player.inventory, (TileEntityReplacer) tileEntity); } return null; } } If you can spot the problem with all my latest code that would be great - I had it all working back in 1.4.7 but i guess a fair few things have changed since then
  12. Yea - I've got it opening from a block but couldn't think of any other way of getting the x, y, z coords of the tile entity without using those variables. How else would i do this? - do i send the packets that contain the x y z from say the block instead when clicked on?
  13. Ok so now I'm sending the x y z coords of the tile entity by using this in my Gui class: public TileEntityReplacer tile = new TileEntityReplacer(); public int PosX = tile.xCoord; public int PosY = tile.yCoord; public int PosZ = tile.zCoord; And when button clicked i use: ByteArrayOutputStream bos = new ByteArrayOutputStream(; DataOutputStream os = new DataOutputStream(bos); try { os.writeInt(PosX); os.writeInt(PosY); os.writeInt(PosZ); os.writeInt(number); } catch (Exception ex) { ex.printStackTrace(); } Packet250CustomPayload packet = new Packet250CustomPayload(); packet.channel = "UniqueModID"; packet.data = bos.toByteArray(); packet.length = bos.size(); Minecraft.getMinecraft().thePlayer.sendQueue.addToSendQueue(packet); And finally i handle this by this: package net.mod.Common; import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; import net.mod.GUI.TileEntityReplacer; import net.minecraft.client.Minecraft; import net.minecraft.item.ItemStack; import net.minecraft.network.INetworkManager; import net.minecraft.network.packet.Packet250CustomPayload; import net.minecraft.tileentity.TileEntity; import cpw.mods.fml.common.network.IPacketHandler; import cpw.mods.fml.common.network.Player; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; public class PacketHandler implements IPacketHandler { public int number; public int PosX, PosY, PosZ; TileEntityReplacer te = new TileEntityReplacer(); @Override public void onPacketData(INetworkManager manager, Packet250CustomPayload par1, Player par2) { if (par1.channel.equals("UniqueModID")) { handleRandom(par1); } } private void handleRandom(Packet250CustomPayload par1) { DataInputStream In = new DataInputStream(new ByteArrayInputStream(par1.data)); try { PosX = In.readInt(); PosY = In.readInt(); PosZ = In.readInt(); number = In.readInt(); } catch (IOException e) { e.printStackTrace(); return; } this.makeReplacement(number); System.out.println(number); } public void makeRecord(int numberID){ TileEntity te = Minecraft.getMinecraft().thePlayer.worldObj.getBlockTileEntity(PosX, PosY, PosZ); if (te instanceof TileEntityReplacer) { ((TileEntityReplacer) te).setInventorySlotContents(0, new ItemStack(Common.ItemCustom.itemID + number, 1, 0));; } } } It still doesn't work - even if i say try to use an apple in the item stack it still won't set the slot contents
  14. So for that code you use that instead of the Packet250CustomPayLoad? Once i get all the data sending i can still use the te.setInventorySlotContents(0, new ItemStack(Common.ItemCustom.itemID + numberID, 1, 0)); to set the contents? Also can all this be done using the NBTTagCompounds already in the tile entity class? - Forgot to ask that
  15. Ok - So does this mean i have to send like 4 more ints - one for x, y, z and tile entity id? if so - how would i do that with packets - is it just the same thing but 4 times over? I have actually seen somewhere that you can use the Dimension Manager to send coords and stuff but i don't think it worked real well And basically I've added like 10 items or so from 4000 to 4010(They are all the same Item class just different ids) and I've got ten buttons so i just thought if i use that itemCustom + whatever number button was pressed and sent through the packet it would select the right item.
  16. Ok so I'm finally getting somewhere - Ive got the buttons when pressed set an int to a certain value which is then sent and received by my packethandler class. In there i have it read the int and run a method where that int is a parameter. In there i have it setTileInventoryContents with a new ItemStack . When i create the item stack i have it create the item with an id of 4000 + the int that was sent through the packet. Im not quite sure what I'm doing wrong but nothing shows up in the slot. - Ive added printOuts to check if the packet was being sent and the right int was being sent and that is all fine. And suggestions? Code in packet handler: package net.mod.Common; import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; import net.minecraft.item.ItemStack; import net.minecraft.network.INetworkManager; import net.minecraft.network.packet.Packet250CustomPayload; import cpw.mods.fml.common.network.IPacketHandler; import cpw.mods.fml.common.network.Player; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; public class PacketHandler implements IPacketHandler { public int number; TileEntityReplacer te = new TileEntityReplacer(); @Override public void onPacketData(INetworkManager manager, Packet250CustomPayload par1, Player par2) { if (par1.channel.equals("UniqueModID")) { handleRandom(par1); } } private void handleRandom(Packet250CustomPayload par1) { DataInputStream In = new DataInputStream(new ByteArrayInputStream(par1.data)); try { number = In.readInt(); } catch (IOException e) { e.printStackTrace(); return; } this.makeReplacement(number); System.out.println(number); } public void makeReplacement(int numberID){ te.setInventorySlotContents(1, new ItemStack(Common.ItemCustom.itemID + numberID, 1, 0)); } } Ive read that you can't just add a @SideOnly(Side.SERVER) in front as it stuffs things up and i have no idea what it does any way Im hopping this time it IS something I've just missed.
  17. How do you make a new itemstack on the server side because i can just send the itemstack back to the tile entity through packets? - I think this way will be the easiest now
  18. So I've made a new PacketHandler class as follows: package net.mod.Common; import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; import net.minecraft.network.INetworkManager; import net.minecraft.network.packet.Packet250CustomPayload; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.network.IPacketHandler; import cpw.mods.fml.common.network.Player; public class PacketHandler implements IPacketHandler { @Override public void onPacketData(INetworkManager manager, Packet250CustomPayload par1, Player par2) { if (par1.channel.equals("UniqueModID")) { handleRandom(par1); } } private void handleRandom(Packet250CustomPayload par1) { DataInputStream In = new DataInputStream(new ByteArrayInputStream(par1.data)); } } and I've registered it in my common class using: @NetworkMod(clientSideRequired = true, serverSideRequired = true, channels={"UniqueModID"}, packetHandler = PacketHandler.class) Would i just send the id of the button - have a switch statement that creates a new item stack based on which button is pressed then use a side(side.SERVER)and run that method in the tile entity? - I don't think it will work but should i try it ?
  19. So with this do i need to make my packet handler again or is all the needed things in mine craft already? - And you put this method in my Gui class when i click on the button or in the tile entity class in the setIventorySlotContents method? I just don't seem to get PacketHandlers.
  20. Ok - ive had a look through the packet.class but haven't come across anything, I thought i had the sufficient NBTtag methods - ill keep looking through the code but as of i can see theres not much that is helpful to me. - I could be just skipping over something obvious but I'm really fed up with this now - been trying to get this working for about a week now
  21. Sorry about the code not being formatted properly but the code button doesn't seem to be working for me atm Ive tried that - I doesn't seem to work. Ive put the slot id which is 1 - i think and the new ItemStack(the id of the item, 1, 0) but it doesn't create the item in the slot - thats y i thought it could be a packet handler job. This is what i have so far - Gui class with button: switch(b.id){ case 2:{ tile.setInventorySlotContents(1, new ItemStack(Item.appleGold, 1, 0)); break; } And then my tile entity class as a whole: package net.mod.gui; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.tileentity.TileEntity; public class TileEntityReplacer extends TileEntity implements IInventory { private ItemStack[] inv; public TileEntityReplacer() { inv = new ItemStack[9]; } @Override public void closeChest() {} @Override public ItemStack decrStackSize(int i, int j) { ItemStack stack = getStackInSlot(i); if (stack != null) { if (stack.stackSize <= j) { setInventorySlotContents(i, null); } else { stack = stack.splitStack(j); if (stack.stackSize == 0) { setInventorySlotContents(i,null); } } } return stack; } @Override public String getInvName() { return "Replacer"; } @Override public int getInventoryStackLimit() { return 1; } @Override public int getSizeInventory() { return inv.length; } @Override public ItemStack getStackInSlot(int i) { return inv[i]; } @Override public ItemStack getStackInSlotOnClosing(int i) { ItemStack stack = getStackInSlot(i); if (stack != null) { setInventorySlotContents(i, null); } return stack; } @Override public boolean isInvNameLocalized() { // TODO Auto-generated method stub return false; } @Override public boolean isItemValidForSlot(int i, ItemStack itemstack) { // TODO Auto-generated method stub return false; } @Override public boolean isUseableByPlayer(EntityPlayer entityplayer) { return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) == this && entityplayer.getDistanceSq(xCoord+0.5, yCoord+0.5, zCoord+0.5) < 64; } @Override public void openChest() {} @Override public void setInventorySlotContents(int i, ItemStack itemstack) { inv[i] = itemstack; if (itemstack != null && itemstack.stackSize >= getInventoryStackLimit()) { itemstack.stackSize = getInventoryStackLimit(); } } @Override public void readFromNBT(NBTTagCompound tagCompound) { super.readFromNBT(tagCompound); NBTTagList tagList = tagCompound.getTagList("Inventory"); for (int i = 0; i < tagList.tagCount(); i++) { NBTTagCompound tag = (NBTTagCompound) tagList.tagAt(i); byte slot = tag.getByte("Slot"); if (slot >= 0 && slot < inv.length) { inv[slot] = ItemStack.loadItemStackFromNBT(tag); } } } @Override public void writeToNBT(NBTTagCompound tagCompound) { super.writeToNBT(tagCompound); NBTTagList itemList = new NBTTagList(); for (int i = 0; i < inv.length; i++) { ItemStack stack = inv[i]; if (stack != null) { NBTTagCompound tag = new NBTTagCompound(); tag.setByte("Slot", (byte)i); stack.writeToNBT(tag); itemList.appendTag(tag); } } tagCompound.setTag("Inventory", itemList); } } and my container which in guihandler is called on server side rather than the gui: package net.mod.Gui; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; public class ContainerReplacer extends Container { protected TileEntityReplacer tileEntity; public ContainerReplacer (InventoryPlayer inventoryPlayer, TileEntityReplacer te){ tileEntity = te; addSlotToContainer(new Slot(tileEntity, 0, 44 , 35)); //commonly used vanilla code that adds the player's inventory bindPlayerInventory(inventoryPlayer); } @Override public boolean canInteractWith(EntityPlayer player) { return tileEntity.isUseableByPlayer(player); } protected void bindPlayerInventory(InventoryPlayer inventoryPlayer) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 9; j++) { addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18)); } } for (int i = 0; i < 9; i++) { addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142)); } } @Override public ItemStack transferStackInSlot(EntityPlayer player, int slot) { ItemStack stack = null; Slot slotObject = (Slot) inventorySlots.get(slot); //null checks and checks if the item can be stacked (maxStackSize > 1) if (slotObject != null && slotObject.getHasStack()) { ItemStack stackInSlot = slotObject.getStack(); stack = stackInSlot.copy(); //merges the item into player inventory since its in the tileEntity if (slot < 9) { if (!this.mergeItemStack(stackInSlot, 0, 35, true)) { return null; } } //places it into the tileEntity is possible since its in the player inventory else if (!this.mergeItemStack(stackInSlot, 0, 9, false)) { return null; } if (stackInSlot.stackSize == 0) { slotObject.putStack(null); } else { slotObject.onSlotChanged(); } if (stackInSlot.stackSize == stack.stackSize) { return null; } slotObject.onPickupFromSlot(player, stackInSlot); } return stack; } }
  22. So i need to create a new itemstack or remove an item stack in a slot in a custom gui but it either doesn't do anything at all or it crashes. I have not dealt with packet handlers and i am almost certain that is it. I have tried following the tut on the wiki for packet handling and tried implementing it into my problem but it didn't change anything. I still think that might be the problem. The whole setup includes a button on which clicked will either created a new item or delete the item. I can post the code if needed but its almost identical to the one on the wiki. Any suggestions or packet handling code for this type of problem will be appreciated
  23. Atm its hard to say whats wrong as you have not posted your full code but have you tried changing the serverSide = false to true - Don't think it will make a difference but thats the only thing i can say atm
  24. Try Removing the ".setCreativeTab()" from where you register your item and add this into your item class "this.setCreativeTab(CreativeTabs.tabMisc) Not sure if it will make a difference but it might. Also if you want a 2D texture - i like the method of using this in your item class: public void registerIcons(IconRegister ir) { itemIcon = ir.registerIcon([ModId Of YourMod] + ":" + this.getUnlocalizedName().substring(5)); } So in your case this will look in the folder - assets/[YourModid]/textures/items/wolveMask.png Hope This Helped Also on a side note - you wouldn't know how to make a 3d item render face where the head faces?
×
×
  • Create New...

Important Information

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