Jump to content

Spawning Entity at player through keybind press


Sear_Heat

Recommended Posts

I am trying to spawn an entity with a key press. The keybind code is ok but the real problem is actually spawning an entity. I do not know how to spawn the fireball at the player, because when I use Minecraft.getMinecraft().theWorld for world and Minecraft.getMinecraft().thePlayer for the paramaters on spawning the fireball, it gives me the error, "Cannot instantiate the type EntityFireball". How should I fix this?

code: http://pastebin.com/qr4DSq2u

Developer of the WIP Rubies mod.

Link to comment
Share on other sites

I don't want to get too involved in this, just spawning an entity. It works fine with my own bow class where I did the same thing without needing any packets or server-side work.

Hi

 

I imagine that your bow class probably worked because the spawning code in your bow was running on the server side, you just didn't know it.  Spawning entities on the client will just lead to trouble.  You need to spawn it on the server, because the server is responsible for keeping track of the game state and synchronising it to all the other clients.  If you spawn on the client (or worse, on both) then you wind up with phantom entities that suddenly disappear, the other clients can't see your entity, and in the worst case can crash your game.

 

You could either use  a custom packet, or you could trigger something that the client transmits to the server (for example - right-clicking with the item you are holding).

(PlayerController MP - see http://greyminecraftcoder.blogspot.com.au/2013/10/user-input.html)

 

-TGG

 

 

Link to comment
Share on other sites

I don't want to get too involved in this, just spawning an entity. It works fine with my own bow class where I did the same thing without needing any packets or server-side work.

 

The reason this is more involved is that you want to do this with a key press -- which happens on the client side.  Spawning the entity has to happen on the server side so somehow you need to send information to the server that indicates that the key was pressed by a certain player and then use that player's position to spawn your entity.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

With custom packets, it is up to you to put information into the payload however you choose.  The packet payloads are passed as ByteBuf type which has methods for writing int, float, String, etc. to the buffer.  The first byte you write is usually a unique int value (sometimes called the discriminator) that you use to identify the packet type (because in most mods you will end up with multple types).  So for example you could have this key press packet type be 1.  In that case you may not need anything further  -- a packet type 1 could represent this particular key press. 

 

Then in your client processing code, you would sendToServer() this packet type when appropriate.

 

The server will then get a network event when it receives the packet, and then you check the channel to confirm it is from your mod (for compatibility otherwise you might mis-process unknown packets) then you start reading back the payload of the received packet in the same order you wrote to it.  In this case you'd just read the first int in the payload, check if it equaled 1 (i.e. it is the packet type expected) and then spawn your entity.

 

Not sure if that makes sense.  The main point is that you need to send the packet by calling the sendToServer() method and you need to pass the method a packet you create that has type that you choose to indicate this event.

 

For example, in this tutorial (http://www.minecraftforum.net/topic/1798625-172sobiohazardouss-forge-keybinding-tutorial/) you can see they have following in their key handling class -- notice the sentToServer() method call:

/**
* KeyInputEvent is in the FML package, so we must register to the FML event bus
*/
@SubscribeEvent
public void onKeyInput(KeyInputEvent event) 
{
    // FMLClientHandler.instance().getClient().inGameHasFocus
    if (!FMLClientHandler.instance().isGUIOpen(GuiChat.class)) 
    {
        if (keys[CUSTOM_INV].isPressed()) 
        {
            TutorialMain.packetPipeline.sendToServer(new OpenGuiPacket(TutorialMain.GUI_CUSTOM_INV));
        }
    }
}

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

You should look at this example code.  It uses simple network wrapper method to do keybindings -- pretty much what you're doing.  Pashimar (the author of that example) is a respected coder so you can trust his stuff.  https://github.com/pahimar/Equivalent-Exchange-3/tree/master/src/main/java/com/pahimar/ee3/network

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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.