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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • This is a MacOs related issue: https://bugs.mojang.com/browse/MC-118506     Download this lib: https://libraries.minecraft.net/ca/weblite/java-objc-bridge/1.0.0/java-objc-bridge-1.0.0.jar and put it into ~/Library/Application Support/minecraft/libraries/ca/weblite/java-objc-bridge/1.0.0  
    • I use Bisect-Hosting, and I host a relatively modded server.  There is a mod I desperately want to have in a server. https://www.curseforge.com/minecraft/mc-mods/minehoplite This is MineHop. It's a mod that replicates the movement capabilities seen in Source Engine games, such as Half-Life, and such.  https://youtu.be/SbtLo7VbOvk - A video explaining the mod, if anyone is interested.  It is a clientside mod, meaning whoever is using it can change anything about the mod that they want, with no restrictions, even when they join a server with the same mod. They can change it to where they can go infinitely fast, or do some other overpowered thing. I don't want that to happen. So I just want to know if there is some way to force the SERVER'S configuration file, onto whoever is joining.  I'm not very savvy with all this modding stuff. There are two config files: minehop-common.txt, and minehop.txt I don't really know much about how each are different. I just know what the commands relating to acceleration and stuff mean.    
    • My journey into crypto trading began tentatively, with me dipping my toes into the waters by purchasing my first Bitcoin through a seasoned trader. With an initial investment of $5,000, I watched as my investment grew, proving to be both fruitful and lucrative. Encouraged by this success, I decided to increase my investment to $150,000, eager to capitalize on the growing popularity of cryptocurrency, However, as cryptocurrency gained mainstream attention, so too did the number of self-proclaimed "experts" in the field. Suddenly, everyone seemed to be a crypto guru, and more and more people were eager to jump on the bandwagon without fully understanding the intricacies of this complex world. With promises of quick and easy profits, these con artists preyed on the uninformed, luring them into schemes that often ended in disappointment and financial loss. Unfortunately, I fell victim to one such scheme. Seduced by the allure of easy money, I entrusted my hard-earned funds to a dubious trading platform, granting them access to my accounts in the hopes of seeing my investment grow. For a brief period, everything seemed to be going according to plan, with regular withdrawals and promising returns on my investment. However, my hopes were soon dashed when, without warning, communication from the platform ceased, and my Bitcoin holdings vanished into thin air. Feeling helpless and betrayed, I confided in a family member about my predicament. They listened sympathetically and offered a glimmer of hope in the form of a recommendation for Wizard Web Recovery. Intrigued by the possibility of reclaiming what I had lost, I decided to explore this option further. From the moment I reached out to Wizard Web Recovery, I was met with professionalism and empathy. They took the time to understand my situation and reassured me that I was not alone in my plight. With their guidance, I embarked on a journey to reclaim what was rightfully mine. Wizard Web Recovery's expertise and dedication were evident from the start. They meticulously analyzed the details of my case, uncovering crucial evidence that would prove invaluable in our quest for justice. With each step forward, they kept me informed and empowered, instilling in me a newfound sense of hope and determination. Through their tireless efforts and unwavering support, Wizard Web Recovery succeeded in recovering my lost Bitcoin holdings. It was a moment of triumph and relief, knowing that justice had been served and that I could finally put this chapter behind me. In conclusion, My experience with Wizard Web Recovery  was nothing short of transformative. Their professionalism, expertise, and unwavering commitment to their clients set them apart as true leaders in the field of cryptocurrency recovery. I am forever grateful for their assistance and would highly recommend their services to anyone in need of help navigating the treacherous waters of cryptocurrency scams. 
    • Ok so: Two things to note: It got stuck due to my dimension type. It was previously the same as the overworld dimension tpye but after changing it , it didn't freeze during spawn generation. ALSO, APPARENTLY, the way I'm doing things, the game can't have two extremely-rich dimensions or it will make the new chunk generation be veeery VEEERY slow. I'm doing the dimension file genreation all in the data generation step now, so it's all good. Mostly. If anybody has any tips regarding how can i more efficently generate a biome-rich dimension, im all ears.
    • https://mclo.gs/qTo3bUE  
  • Topics

×
×
  • Create New...

Important Information

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