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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • So, I have a minecraft world hosted with essential on forge 1.20.1, there's 149 mods, and the forge version is 47.3.0, and it's been like that for a good 2-3 weeks, and all of a sudden, it stopped loading, and I have 0 clue as to why, and this world means A LOT to me, so if anyone out there is able to help, I would GLADLY appreciate it. here's the link if ya wanna help:   [04Dec2024 22:42:24.552] [Worker-ResourceReload-3/ERROR][net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener/]: Couldn't parse data file slabsexpanded:snow_blockblocktoslab from slabsexpanded:recipes/snow_blockblocktoslab.json com.google.gson.JsonParseException: com.google.gson.stream.MalformedJsonException: Expected name at line 16 column 4 path $.result.item at net.minecraft.util.GsonHelper.m_13780_(GsonHelper.java:526) ~[client-1.20.1-20230612.114412-srg.jar%23490!/:?] at net.minecraft.util.GsonHelper.m_263475_(GsonHelper.java:531) ~[client-1.20.1-20230612.114412-srg.jar%23490!/:?] at net.minecraft.util.GsonHelper.m_13776_(GsonHelper.java:581) ~[client-1.20.1-20230612.114412-srg.jar%23490!/:?] at net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener.m_278771_(SimpleJsonResourceReloadListener.java:41) ~[client-1.20.1-20230612.114412-srg.jar%23490!/:?] at net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener.m_5944_(SimpleJsonResourceReloadListener.java:29) ~[client-1.20.1-20230612.114412-srg.jar%23490!/:?] at net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener.m_5944_(SimpleJsonResourceReloadListener.java:17) ~[client-1.20.1-20230612.114412-srg.jar%23490!/:?] at net.minecraft.server.packs.resources.SimplePreparableReloadListener.m_10786_(SimplePreparableReloadListener.java:11) ~[client-1.20.1-20230612.114412-srg.jar%23490!/:?] at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1768) ~[?:?] at java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1760) ~[?:?] at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373) ~[?:?] at java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182) ~[?:?] at java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655) ~[?:?] at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622) ~[?:?] at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165) ~[?:?] Caused by: com.google.gson.stream.MalformedJsonException: Expected name at line 16 column 4 path $.result.item at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1657) ~[gson-2.10.jar%23107!/:?] at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:514) ~[gson-2.10.jar%23107!/:?] at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:422) ~[gson-2.10.jar%23107!/:?] at com.google.gson.internal.bind.TypeAdapters$28.read(TypeAdapters.java:779) ~[gson-2.10.jar%23107!/:?] at com.google.gson.internal.bind.TypeAdapters$28.read(TypeAdapters.java:725) ~[gson-2.10.jar%23107!/:?] at com.google.gson.internal.bind.TypeAdapters$34$1.read(TypeAdapters.java:1007) ~[gson-2.10.jar%23107!/:?] at net.minecraft.util.GsonHelper.m_13780_(GsonHelper.java:524) ~[client-1.20.1-20230612.114412-srg.jar%23490!/:?]
    • Hello! I have been having a problem with Forgematica, Embeddium, Oculus, and create. I wanted to download litematica so I could see which blocks are in my creative mode build, so that I could collect them all in survival. However, litematica is a fabric mod. I found a port called forgematica, which I added (along with it's dependency) to my mods folder. I loaded into a new world, and built a structure. Then, I added a part from the create mod, and the game crashed instantly, with exit code -1. Thanks for any help! Crash Report and mods list: https://pastebin.com/rtzh6LAi
    • Have you found a modder for this vehicle project? Because it will be really hard and I want to know that hero who can create all this
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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