Jump to content

[1.7.10]How to I get entityPlayer of my own character?


Recommended Posts

Posted

Hello,

I want to make a mod that when I press on a button my character moves automatically to the place that he's pointing to.

 

For that I guess that I'll use the function getlookvec() to find the displacement vector between my place to the place the I'm pointing to, and then use moveEntity() with my vector to move the character to there.

 

But before I do all of that I must find my entityPlayer, something that I couldn't find a way to do yet.

Any ideas?

 

 

Posted

If you wanted to cheat it without server compatibility, you would use Minecraft.getMinecraft().thePlayer. Otherwise, you need to send a packet to the server with the username, obtained by using Minecraft.getMinecraft().thePlayer.getCommandSenderName(). The packet will locate a player by their username (matching it to the name in the packet) and move the player like that.

 

Here's a code example:

 

Packet Class


public class PacketMovePlayer implements IMessage {

public String username;

public PacketMovePlayer(String username) {

this.username = username;

}

public PacketMovePlayer() {}

@Override
    public void toBytes(ByteBuf buf) { 
        ByteBufUtils.writeUTF8String(buf, username); 
    }

    @Override
    public void fromBytes(ByteBuf buf) { 
        this.username = ByteBufUtils.readUTF8String(buf);
    }

public class Handler implements IMessageHandler<PacketMovePlayer, IMessage> { 

    @Override
    public IMessage onMessage(PacketMovePlayer message, MessageContext ctx) { 
        
    for(Object o : MinecraftServer().getServer().getConfigurationManager().playerEntityList) {

    if(((EntityPlayer)o).getCommandSenderName().equals(message.username)) {

      // move player
      return null;

}

}

        return null;
    }

}

}

 

Mod Class


public static SimpleNetworkWrapper network;

@EventHandler
public void preInit(FMLPreInitializationEvent event) {

// other pre-init stuff
network = NetworkRegistry.INSTANCE.newSimpleChannel("your_mod_id");
network.registerMessage(PacketMovePlayer.Handler.class, PacketMovePlayer.class, 0, Side.CLIENT);

}

 

Teleport Code


YourMod.network.sendToServer(new PacketMovePlayer(Minecraft.getMinecraft().thePlayer.getCommandSenderName()));

 

Hope that helps! Just a side note, if you are doing the teleport code in an item, just reference the EntityPlayer parameter in your onItemRightClick method!

 

- Romejanic

Romejanic

 

Creator of Witch Hats, Explosive Chickens and Battlefield!

Posted

Thank you very much.

The code that I came out with is:

 

@Mod(modid = WalkMod.MODID, version = WalkMod.VERSION)
public class WalkMod {
    public static final String MODID = "walkMod";
    public static final String VERSION = "1.0";
    EntityPlayer player;
    
    @EventHandler
    public void init(FMLInitializationEvent event)
    {
    	player = Minecraft.getMinecraft().thePlayer;
    }
    
    public void movePlayerWhereHePointsTo(){
    	Vec3 lookVec = player.getLookVec();
    	player.moveEntity(lookVec.xCoord, lookVec.yCoord, lookVec.zCoord);
    }
}

 

My question is, what is the simplest way to trigger "movePlayerWhereHePointsTo" during the game? I tought about pressing a key as I said but it could be something different.

 

Posted

If that is all you came up with then you'll need to learn how to setup forge mod. (Hint: You cant store fields like player globally)

 

As to question:

If you want Client-SIDE Mod that will make player WALK to x,y,z location he is looking at key-clicked you will need to use vanilla pathfiniding and Client-SIDE entity.motionX/Y/Z. Note that when using Client entity motions you need to stay in bounds of normal walk speed (otherwise server-side will warp you back to maximal possible location of you movement).

 

If you want to TELEPORT on Client-SIDE - it's not possible, everything is handled by server.

 

If you want to actually inform server of your actions and make server move your player then follow Romejanic hint and also use pathfinding but server side instead, to actually move player you will also use entity.motionX/Y/Z but you will ahve to execute it on both sides to keep everything smooth (otherwise the client will receive update-position packets from server and it MIGHT look like you are lagging).

 

If you want to just teleport from Server-SIDE, also - send packet and simply set entity position/rotation on server.

 

As to direct question: simplest way to trigger "movePlayerWhereHePointsTo" during the game?

If you would use some custom item to trigger it you wouldn't have to use packets. So that is so-called "simple".

1.7.10 is no longer supported by forge, you are on your own.

Posted

Otherwise, you need to send a packet to the server with the username, obtained by using Minecraft.getMinecraft().thePlayer.getCommandSenderName(). The packet will locate a player by their username (matching it to the name in the packet) and move the player like that.

 

When you receive a packet from a client it gives you the player that sent it.

ctx.getServerHandler().playerEntity

 

 

I am the author of Draconic Evolution

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.