Jump to content

Entity wont spawn when created in GUIScreen


shucke

Recommended Posts

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  ;)

Link to comment
Share on other sites

 

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).

 

 

Link to comment
Share on other sites

???

 

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)

Link to comment
Share on other sites

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 :P

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

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

It worked Thank you ver much :D

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.

Link to comment
Share on other sites

You have to use this to send something to the server:

PacketDispatcher.sendPacketToServer(packet)

 

You don't need a player instance for this.

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

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.

Link to comment
Share on other sites

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
}

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

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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