vova7865 Posted January 11, 2018 Share Posted January 11, 2018 I have this code: package com.vova7865.essencecraft; import java.util.UUID; import com.mojang.authlib.GameProfile; import net.minecraft.entity.EntityLiving; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.server.MinecraftServer; import net.minecraft.world.World; import net.minecraft.world.WorldServer; import net.minecraftforge.common.DimensionManager; import net.minecraftforge.common.util.FakePlayer; import net.minecraftforge.event.entity.living.LivingSpawnEvent; import net.minecraftforge.fml.common.Mod.EventBusSubscriber; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; @EventBusSubscriber public class EventHandler { @SubscribeEvent public void livingSpawn(LivingSpawnEvent event) { if(event.getEntityLiving().toString().contains("Guardian")) { EntityLiving living = (EntityLiving) event.getEntityLiving(); World world = event.getWorld(); WorldServer serverWorld = DimensionManager.getWorld(0); GameProfile gameProfile = new GameProfile(UUID.randomUUID(), "FakePlayer"); FakePlayer fakePlayer = new FakePlayer(serverWorld, gameProfile); MinecraftServer minecraftServer = fakePlayer.mcServer; String playerNames[] = minecraftServer.getOnlinePlayerNames(); String firstPlayerName = playerNames[0]; EntityPlayer player = world.getPlayerEntityByName(firstPlayerName); double x = player.posX; double y = player.posY; double z = player.posZ; living.attemptTeleport(x, y, z) } } } So when the guardians spawn, they teleport to me. They do, but the same guardians are teleported to me multiple times, so I think that LivingSpawnEvent is fired not only when they spawn. Anyone knows how can I do some code only when the mob spawns? Quote Link to comment Share on other sites More sharing options...
Draco18s Posted January 11, 2018 Share Posted January 11, 2018 The LivingSpawnEvent has sub-events IIRC. Quote Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given. Link to comment Share on other sites More sharing options...
vova7865 Posted January 11, 2018 Author Share Posted January 11, 2018 So how can I subscribe to them? Quote Link to comment Share on other sites More sharing options...
Draco18s Posted January 11, 2018 Share Posted January 11, 2018 livingSpawn(LivingSpawnEvent.NameOfSubEvent event) Quote Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given. Link to comment Share on other sites More sharing options...
vova7865 Posted January 11, 2018 Author Share Posted January 11, 2018 Thanks! Another question: do ElderGuardians fire this event when they spawn in the monuments? Quote Link to comment Share on other sites More sharing options...
Matryoshika Posted January 11, 2018 Share Posted January 11, 2018 (edited) Mobs spawned by the world & mobs spawned by spawners eventually pass through LivingSpawnEvent#checkSpawn before actually getting spawned. And yes, all children of LivingSpawnEvent will be given to you unless a specific one is stated. Btw, your whole "create a fake-player just to get the server" is quite redundant. Just call World::getMinecraftServer and be done with it. Creating FakePlayers is dangerous if you don't also make sure to remove them when the world needs to unload, or it's not gonna get unloaded, because the "player" is still in there. Edited January 11, 2018 by Matryoshika Quote Also previously known as eAndPi. "Pi, is there a station coming up where we can board your train of thought?" -Kronnn Published Mods: Underworld Handy links: Vic_'s Forge events Own WIP Tutorials. Link to comment Share on other sites More sharing options...
Recommended Posts
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.