问题
Using Forge 1.8.9 in Eclipse (Mars.1 Release (4.5.1)) in local development environment.
The goal is to set a player's location to a predetermined xyz every time they join (or re-join) a world. So if they quit the game, but then come back to the world, they will start in the same location as determined by the code below instead of wherever they left off. Basically, it will work like a lobby, where players start in the same place each time.
The code works using the chat component (e.g. the chat message appears upon joining the game) but I've commented it out, for now. The player simply appears wherever they left off after having left the game from last time around.
Questions are: 1. is the PlayerLoggedInEvent the best event to use, or is there a better event? 2. is setLocationAndAngles the best to use, or should a different set location type event (or move) better?
Thanks in advance. Lots of experience with LAMP stack, but Java and mods are a new interest (obvs). Code is below.
import net.minecraftforge.client.event.RenderPlayerEvent;
//import net.minecraft.util.ChatComponentText;
//import net.minecraft.util.EnumChatFormatting;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent;
//import cpw.mods.fml.common.eventhandler.SubscribeEvent;
public class JoinGameLocation {
@SubscribeEvent
public void SpawningLocation(PlayerLoggedInEvent event){
event.player.setLocationAndAngles(145, 72, 145, 0, 0);
//-----This works when uncommented
//event.player
//.addChatMessage(
// new ChatComponentText(
// EnumChatFormatting.RED + "You joined the game"));
//event.world.setWorldTime(0);
int ticks = 0;
double good_x = 145;
double good_y = 72;
double good_z = 145;
event.player.setLocationAndAngles(good_x, good_y, good_z, 0, 0);
}
}
回答1:
Use the entity join world and the clone event
@SubscribeEvent
public void onClonePlayer(PlayerEvent.Clone event) {
}
@SubscribeEvent
public void onEntityJoinWorld(EntityJoinWorldEvent event) {
if (event.entity != null && event.entity instanceof EntityPlayer && !event.entity.worldObj.isRemote) {
}
}
The clone event is triggered upon tp, dimension change, etc... The join world obviously when the world is joined. I suggest you play around with these.
来源:https://stackoverflow.com/questions/35460996/minecraft-forge-using-correct-join-game-listener-for-setlocationandangles