shucke Posted October 14, 2012 Share Posted October 14, 2012 Once again i am having troubles with the multiplayer GUI's. The gui opens and all but when i tried to spawn an entity from my guiScreen it doesnt spawn the entity on the servers side. i registered my gui hadler and all but it think it have something to do with my getServerGuiElement. @Override public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { return null; } @Override public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { TileEntity te = world.getBlockTileEntity(x, y, z); if (te != null){ switch(ID){ case 0: return new GuiVendingMachine((TileEntityVendingMachine) te, x, y, z, world); } } return null; } it worked for the container to open return a new container but i dont know what to return at the servers side. btw this is the code for openening my gui: FMLNetworkHandler.openGui(par5EntityPlayer, BaseMineConomy.instance, 0, par1World, par2, par3, par4); Thanks in advance Quote http://www.minecraftforum.net/topic/1937703-162smpforge-pet-mastery-hatch-level-battle/ Link to comment Share on other sites More sharing options...
shadowmage4513 Posted October 15, 2012 Share Posted October 15, 2012 This is because everything that happens in a non-container GUI happens client-side only. You will need to enact some sort of packets/packethandlers--and when the client pressses the button/does the action to spawn the entity--send a packet to the server and have the server do the spawning. You will need to send whatever information you need from the GUI, as the server has no idea you even have GUI open (it doesn't deal with GUIs--they are client-side only). Quote Link to comment Share on other sites More sharing options...
shucke Posted October 16, 2012 Author Share Posted October 16, 2012 Alright so now i send a packet to the server whenever a button is pressed. but the item that have to be spawned is defined in the gui. so i have to send the item id as integer to the server. but how can i see what itemid is belongs to what item? Quote http://www.minecraftforum.net/topic/1937703-162smpforge-pet-mastery-hatch-level-battle/ Link to comment Share on other sites More sharing options...
shadowmage4513 Posted October 16, 2012 Share Posted October 16, 2012 Are you trying to spawn an entity, or do something with items? Item ID's have nothing to do with entities. If you are trying to give your player an item, that is something completely seperate, and should probably be handled in a container. If you are tyring to have your vending machine spit out an EntityItem, that again..is something seperate (kind of related). In that case, find the code that spawns entityItems, and give it your itemID that you want to have spawned. "but how can i see what itemid is belongs to what item?" ? You pull them from the item in-game. Something like Item.silk.shiftedIndex will get you the itemID of a particular item--use that itemID when spawning an EntityItem (look for the EntityItem spawning source code in the game source--it is there) Quote Link to comment Share on other sites More sharing options...
shucke Posted October 16, 2012 Author Share Posted October 16, 2012 I am trying to let the vending machine spit out an item. but since it needs to be handled trough packets i sended the x,y,z coordiates to the server but the item that needs to be spawned is defined in the gui and since you cant send items trough packets i send the itemID to the server. and from the dispenser source code i found: EntityItem var12 = new EntityItem(par0World, par6, par8 - 0.3D, par10, par1ItemStack); par0World.spawnEntityInWorld(var12); so im trying to spawn an item in the world with this code but instead of an itemid you need the Item type to spawn the item. So my question: is there a way to convert the itemID to an Item type. sorry if i was a little vague in my last post but english isnt my first language Quote http://www.minecraftforum.net/topic/1937703-162smpforge-pet-mastery-hatch-level-battle/ Link to comment Share on other sites More sharing options...
shadowmage4513 Posted October 17, 2012 Share Posted October 17, 2012 No worries, I had kind of inferred you were trying to spit out items. basically, you need to construct a new ItemStack to feed the entityItem, which is as simple as EntityItem var12 = new EntityItem(par0World, par6, par8 - 0.3D, par10, new ItemStack(itemID, quantity)); replace itemID with the shiftedIndex for the item you are trying to spawn. replace quantity with the quantity you want dispensed. Hope that helps EDIT--Sorry, apparently I gave you the wrong constructor, and it should be new ItemStack(Item, quantity). There is a three int constructor (itemID, qty, dmg). You can get the item directly by calling the static Item that you declared / that is declared in minecraft i.e. Item.silk, or Your_Mod_Item_Loader._your_item_name Quote Link to comment Share on other sites More sharing options...
shucke Posted October 17, 2012 Author Share Posted October 17, 2012 I can't make an new ItemStack from an int and int. i need to define an item as the first variable. unless i am missing something obvious here... Quote http://www.minecraftforum.net/topic/1937703-162smpforge-pet-mastery-hatch-level-battle/ Link to comment Share on other sites More sharing options...
SanAndreaP Posted October 17, 2012 Share Posted October 17, 2012 You can also make an ItemStack out of 3 integers, the 3rd integer would be the damage value, which you can set to 0. new ItemStack(itemID, quantity, damage) Quote Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! | mah twitter This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works. Link to comment Share on other sites More sharing options...
shucke Posted October 17, 2012 Author Share Posted October 17, 2012 It worked Thank you ver much the item spawns now but i cant pick it up. i think the packet i send is somehow only send to the client instead of the server. i used this code to send my packet: EntityClientPlayerMP playerr = (EntityClientPlayerMP) player; playerr.sendQueue.addToSendQueue(packet);[/Code] and this to spawn the item in(Inside my PacketHandler.class) [Code]World world = Minecraft.getMinecraft().theWorld; EntityItem entityitem = new EntityItem(world, x, y, z, new ItemStack(i, 1, 0)); world.spawnEntityInWorld(entityitem); [/Code] but in my packet handler when i do the !world.isRemote check it doesnt return false which should be if you are on a server. i followed the tutorial on packages on the forge wiki: http://www.minecraftforge.net/wiki/Tutorials/Packet_Handling#Building_the_Packet so the code is about the same. Quote http://www.minecraftforum.net/topic/1937703-162smpforge-pet-mastery-hatch-level-battle/ Link to comment Share on other sites More sharing options...
SanAndreaP Posted October 17, 2012 Share Posted October 17, 2012 You have to use this to send something to the server: PacketDispatcher.sendPacketToServer(packet) You don't need a player instance for this. Quote Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! | mah twitter This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works. Link to comment Share on other sites More sharing options...
shucke Posted October 17, 2012 Author Share Posted October 17, 2012 i changed: [codeEntityClientPlayerMP playerr = (EntityClientPlayerMP) player; playerr.sendQueue.addToSendQueue(packet);][/code] to: PacketDispatcher.sendPacketToServer(packet);[/Code] and it still doesnt work. maybe i'm missing something obvious: here is my code: Base Class: [code]package mineconomy.common; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.SidedProxy; import cpw.mods.fml.common.Mod.Init; import cpw.mods.fml.common.Mod.Instance; import cpw.mods.fml.common.Mod.PreInit; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.network.NetworkMod; import cpw.mods.fml.common.network.NetworkMod.SidedPacketHandler; import cpw.mods.fml.common.network.NetworkRegistry; @Mod(modid = "MineConomy", name = "Mine Conomy", version = "0.1") @NetworkMod( clientSideRequired = true, serverSideRequired = false, versionBounds = "[0.1]", channels = {"MineConomy"}, packetHandler = PacketHandler.class /*clientPacketHandlerSpec = @SidedPacketHandler(channels = {"MineConomyClient"}, packetHandler = ClientPacketHandler.class), serverPacketHandlerSpec = @SidedPacketHandler(channels = {"MineConomyServer"}, packetHandler = ServerPacketHandler.class)*/) public class BaseMineConomy { @SidedProxy(clientSide = "mineconomy.client.ClientProxy", serverSide = "mineconomy.common.CommonProxy") public static CommonProxy proxy; @Instance public static BaseMineConomy instance; @Init public void Load(FMLInitializationEvent evt){ proxy.registerRenderInformation(); NetworkRegistry.instance().registerGuiHandler(instance, proxy); } @PreInit public void PreLoad(FMLInitializationEvent evt){ } } Gui: private void BuildPacketItemSpawn(int x, int y, int z, int i) { int id = 0; //Defining the amount of bytes that has to be sent ByteArrayOutputStream bos = new ByteArrayOutputStream(24); DataOutputStream outputStream = new DataOutputStream(bos); try{ outputStream.writeInt(id); outputStream.writeInt(x); outputStream.writeInt(y); outputStream.writeInt(z); outputStream.writeInt(i); }catch(Exception ex){ ex.printStackTrace(); } Packet250CustomPayload packet = new Packet250CustomPayload(); packet.channel = "MineConomy"; packet.data = bos.toByteArray(); packet.length = bos.size(); PacketDispatcher.sendPacketToServer(packet); }[/Code] PacketHandler: [Code]package mineconomy.common; import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; import net.minecraft.client.Minecraft; import net.minecraft.src.Block; import net.minecraft.src.EntityItem; import net.minecraft.src.Item; import net.minecraft.src.ItemStack; import net.minecraft.src.NetworkManager; import net.minecraft.src.Packet250CustomPayload; import net.minecraft.src.World; import cpw.mods.fml.common.network.IPacketHandler; import cpw.mods.fml.common.network.Player; public class PacketHandler implements IPacketHandler{ @Override public void onPacketData(NetworkManager manager, Packet250CustomPayload packet, Player player) { if(packet.channel.equals("MineConomy")){ handlePacket(packet, player); } } private void handlePacket(Packet250CustomPayload packet, Player player) { DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data)); int id; try{id = inputStream.readInt();}catch(IOException ex){ex.printStackTrace();return;} switch(id){ case 0: int x; int y; int z; int i; try { x = inputStream.readInt(); y = inputStream.readInt(); z = inputStream.readInt(); i = inputStream.readInt(); } catch (IOException e) { e.printStackTrace(); return; } World world = Minecraft.getMinecraft().theWorld; TileEntityVendingMachine tile = (TileEntityVendingMachine)world.getBlockTileEntity(x,y,z); Item item = tile.Items[i-1]; EntityItem entityitem = new EntityItem(world, x, y, z, new ItemStack(item, 1)); entityitem.delayBeforeCanPickup = 10; entityitem.motionY += 10; world.spawnEntityInWorld(entityitem); break; } } }[/Code] CommonProxy: [Code]@Override public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { TileEntity te = world.getBlockTileEntity(x, y, z); if (te != null){ switch(ID){ case 1: return new ContainerVendingMachine(player.inventory, (TileEntityVendingMachine)te); } } return null; } @Override public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { TileEntity te = world.getBlockTileEntity(x, y, z); if (te != null){ switch(ID){ case 0: return new GuiVendingMachine((TileEntityVendingMachine) te, x, y, z, world, player); case 1: return new GuiVendingMachineConfig(player.inventory,(TileEntityVendingMachine)te); } } return null; }[/Code] I have no clue what is wrong with the code and why it wont send the packet to the server. Quote http://www.minecraftforum.net/topic/1937703-162smpforge-pet-mastery-hatch-level-battle/ Link to comment Share on other sites More sharing options...
SanAndreaP Posted October 17, 2012 Share Posted October 17, 2012 Your PacketHandler has a client reference: World world = Minecraft.getMinecraft().theWorld; Don't do that! Use this code instead: World world = ((EntityPlayer)player).worldObj; and then check if it's not remote, like this: if(world.isRemote) break; or: if(!world.isRemote) { // spawn entity code } Quote Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! | mah twitter This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works. Link to comment Share on other sites More sharing options...
Recommended Posts
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.