Jump to content

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


TheiTay

Recommended Posts

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?

 

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Please share a link to your crash report on https://paste.ee, as explained in the FAQ. Dumping it directly into the thread often triggers the anti-spam and makes it hard to read due to word wrapping
    • Hi all, I'm running a 1.20.1 Minecraft Forge server with a hosting website and something seems to have gone wrong. It started as an occasional crash that has now become extremely frequent and I don't know what exactly is causing it. I already tried disabling watchdog but the server crashes anyway. Oddly it only occasionally generates a crash report after a crash occurs. Based on viewing the server console's research usage it seems that the CPU spikes astronomically high (100% usage), especially when chunks are being loaded or when a new person joins the server. I downloaded Spark to create a profile (https://spark.lucko.me/bBlWe7ou29) but I lack experience with this mod and don't understand what I'm looking at/how this should help me diagnose the issue. I am using a lot of mods so I'm guessing there is some sort of issue there or a corrupted chunk (although MCRF found no chunk errors). We actually don't experience any lag until the crash occurs, so it's some sort of sudden spike in resource usage. Based on the crash report I've already tried removing Canary and Modernfix but there was no change. Thanks for any help you can provide. Specs:  4 cores x 4200MHz AMD Ryzen 9 5950X 4GB RAM Crash report:  
    • will test that once i get home, thanks
    • I am getting this exception (I am new and just trying to create an item) Caused by: java.io.FileNotFoundException: abrasmagics:models/item/ingot_test.json at net.minecraft.client.resources.SimpleReloadableResourceManager.getResource(SimpleReloadableResourceManager.java:69) ~[SimpleReloadableResourceManager.class:?] at net.minecraft.client.renderer.block.model.ModelBakery.loadModel(ModelBakery.java:334) ~[ModelBakery.class:?] at net.minecraftforge.client.model.ModelLoader.access$1400(ModelLoader.java:115) ~[ModelLoader.class:?] at net.minecraftforge.client.model.ModelLoader$VanillaLoader.loadModel(ModelLoader.java:861) ~[ModelLoader$VanillaLoader.class:?] at net.minecraftforge.client.model.ModelLoaderRegistry.getModel(ModelLoaderRegistry.java:157) ~[ModelLoaderRegistry.class:?]   However the directory is set up correctly and the file does exist   idk how to upload images so here is a treeview of the directories:   src ├───main │ ├───java │ │ └───net │ │ └───abrasmods │ │ └───abrasmagics │ │ ├───init │ │ ├───items │ │ └───utils │ │ ├───handlers │ │ ├───misc │ │ └───proxy │ └───resources │ └───assets │ └───abrasmagics │ ├───models │ │ ├───block │ │ └───item │ └───textures └───test ├───java └───resources   the file ingot_test.json is under models/item. The directory seems fine and the file does exist so anyone plz help.   i am using intellij and is coding for 1.12.2
  • Topics

×
×
  • Create New...

Important Information

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