package me.km.events; import com.mojang.authlib.GameProfile; import io.netty.buffer.Unpooled; import java.util.UUID; import me.km.KajetansMod; import me.km.api.GlobalText; import me.km.api.Utils; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.NetHandlerPlayServer; import net.minecraft.network.NetworkManager; import net.minecraft.network.PacketBuffer; import net.minecraft.network.play.server.SPacketCustomPayload; import net.minecraft.network.play.server.SPacketEntityEffect; import net.minecraft.network.play.server.SPacketHeldItemChange; import net.minecraft.network.play.server.SPacketJoinGame; import net.minecraft.network.play.server.SPacketPlayerAbilities; import net.minecraft.network.play.server.SPacketServerDifficulty; import net.minecraft.scoreboard.ServerScoreboard; import net.minecraft.server.dedicated.DedicatedPlayerList; import net.minecraft.server.dedicated.DedicatedServer; import net.minecraft.server.management.PlayerProfileCache; import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextComponentTranslation; import net.minecraft.world.World; import net.minecraft.world.WorldServer; import net.minecraft.world.chunk.storage.AnvilChunkLoader; import net.minecraft.world.storage.WorldInfo; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @SideOnly(Side.SERVER) public class ModDedicatedPlayerList extends DedicatedPlayerList { private final DedicatedServer mcServer; private static final Logger LOG = LogManager.getLogger(); public ModDedicatedPlayerList(DedicatedServer server) { super(server); this.mcServer = server; } // copied from net.minecraft.server.management.PlayerList private void setPlayerGameTypeBasedOnOther(EntityPlayerMP target, EntityPlayerMP source, World worldIn) { /*if (source != null) { target.interactionManager.setGameType(source.interactionManager.getGameType()); } else if (this.gameType != null) { target.interactionManager.setGameType(this.gameType); }*/ target.interactionManager.initializeGameType(worldIn.getWorldInfo().getGameType()); } // copied from net.minecraft.server.management.PlayerList @Override public void initializeConnectionToPlayer(NetworkManager netManager, EntityPlayerMP playerIn, NetHandlerPlayServer nethandlerplayserver) { GameProfile gameprofile = playerIn.getGameProfile(); PlayerProfileCache playerprofilecache = this.mcServer.getPlayerProfileCache(); GameProfile gameprofile1 = playerprofilecache.getProfileByUUID(gameprofile.getId()); String s = gameprofile1 == null ? gameprofile.getName() : gameprofile1.getName(); playerprofilecache.addEntry(gameprofile); NBTTagCompound nbttagcompound = this.readPlayerDataFromFile(playerIn); playerIn.setWorld(this.mcServer.worldServerForDimension(playerIn.dimension)); World playerWorld = this.mcServer.worldServerForDimension(playerIn.dimension); if (playerWorld == null) { playerIn.dimension = 0; playerWorld = this.mcServer.worldServerForDimension(0); BlockPos spawnPoint = playerWorld.provider.getRandomizedSpawnPoint(); playerIn.setPosition(spawnPoint.getX(), spawnPoint.getY(), spawnPoint.getZ()); } playerIn.setWorld(playerWorld); playerIn.interactionManager.setWorld((WorldServer)playerIn.world); String s1 = "local"; if (netManager.getRemoteAddress() != null) { s1 = netManager.getRemoteAddress().toString(); } LOG.info("{}[{}] logged in with entity id {} at ({}, {}, {})", new Object[] {playerIn.getName(), s1, playerIn.getEntityId(), playerIn.posX, playerIn.posY, playerIn.posZ}); WorldServer worldserver = this.mcServer.worldServerForDimension(playerIn.dimension); WorldInfo worldinfo = worldserver.getWorldInfo(); this.setPlayerGameTypeBasedOnOther(playerIn, null, worldserver); playerIn.connection = nethandlerplayserver; nethandlerplayserver.sendPacket(new SPacketJoinGame(playerIn.getEntityId(), playerIn.interactionManager.getGameType(), worldinfo.isHardcoreModeEnabled(), worldserver.provider.getDimension(), worldserver.getDifficulty(), this.getMaxPlayers(), worldinfo.getTerrainType(), worldserver.getGameRules().getBoolean("reducedDebugInfo"))); nethandlerplayserver.sendPacket(new SPacketCustomPayload("MC|Brand", (new PacketBuffer(Unpooled.buffer())).writeString(this.getServerInstance().getServerModName()))); nethandlerplayserver.sendPacket(new SPacketServerDifficulty(worldinfo.getDifficulty(), worldinfo.isDifficultyLocked())); nethandlerplayserver.sendPacket(new SPacketPlayerAbilities(playerIn.capabilities)); nethandlerplayserver.sendPacket(new SPacketHeldItemChange(playerIn.inventory.currentItem)); this.updatePermissionLevel(playerIn); playerIn.getStatFile().markAllDirty(); playerIn.getStatFile().sendAchievements(playerIn); this.sendScoreboard((ServerScoreboard)worldserver.getScoreboard(), playerIn); this.mcServer.refreshStatusNextTick(); // Custom Join Message - Start //TextComponentTranslation textcomponenttranslation; PlayerJoinMessageEvent event; if (playerIn.getName().equalsIgnoreCase(s)) { //textcomponenttranslation = new TextComponentTranslation("multiplayer.player.joined", new Object[] {playerIn.getDisplayName()}); event = new PlayerJoinMessageEvent(playerIn, false, "No message was set."); } else { //textcomponenttranslation = new TextComponentTranslation("multiplayer.player.joined.renamed", new Object[] {playerIn.getDisplayName(), s}); event = new PlayerJoinMessageEvent(playerIn, true, "No message was set."); } //textcomponenttranslation.getStyle().setColor(TextFormatting.YELLOW); //this.sendMessage(textcomponenttranslation); if(!MinecraftForge.EVENT_BUS.post(event)) { this.sendMessage(new TextComponentString(event.getMessage())); } // Custom Join Message - End this.playerLoggedIn(playerIn); nethandlerplayserver.setPlayerLocation(playerIn.posX, playerIn.posY, playerIn.posZ, playerIn.rotationYaw, playerIn.rotationPitch); this.updateTimeAndWeatherForPlayer(playerIn, worldserver); if (!this.mcServer.getResourcePackUrl().isEmpty()) { playerIn.loadResourcePack(this.mcServer.getResourcePackUrl(), this.mcServer.getResourcePackHash()); } playerIn.getActivePotionEffects().stream().forEach((potioneffect) -> { nethandlerplayserver.sendPacket(new SPacketEntityEffect(playerIn.getEntityId(), potioneffect)); }); if (nbttagcompound != null && nbttagcompound.hasKey("RootVehicle", 10)) { NBTTagCompound nbttagcompound1 = nbttagcompound.getCompoundTag("RootVehicle"); Entity entity1 = AnvilChunkLoader.readWorldEntity(nbttagcompound1.getCompoundTag("Entity"), worldserver, true); if (entity1 != null) { UUID uuid = nbttagcompound1.getUniqueId("Attach"); if (entity1.getUniqueID().equals(uuid)) { playerIn.startRiding(entity1, true); } else { for (Entity entity : entity1.getRecursivePassengers()) { if (entity.getUniqueID().equals(uuid)) { playerIn.startRiding(entity, true); break; } } } if (!playerIn.isRiding()) { LOG.warn("Couldn\'t reattach entity to player"); worldserver.removeEntityDangerously(entity1); entity1.getRecursivePassengers().stream().forEach((entity2) -> { worldserver.removeEntityDangerously(entity2); }); } } } playerIn.addSelfToInternalCraftingInventory(); net.minecraftforge.fml.common.FMLCommonHandler.instance().firePlayerLoggedIn(playerIn); } // workaround handler for leave message @Override public void sendMessage(ITextComponent component) { // Custom Leave Message - Start if(component instanceof TextComponentTranslation) { TextComponentTranslation trans = (TextComponentTranslation) component; switch(trans.getKey()) { case "multiplayer.player.left": { // trying to get a player Object[] o = trans.getFormatArgs(); if(o.length >= 1) { EntityPlayer p = Utils.getPlayerByDisplayName(o[0].toString()); if(p == null) { KajetansMod.error.sendToConsole(GlobalText.shouldNotHappen()); return; } PlayerLeaveMessageEvent event = new PlayerLeaveMessageEvent(p, "No message was set.");; if(!MinecraftForge.EVENT_BUS.post(event)) { this.sendMessage(new TextComponentString(event.getMessage()), true); } } else { KajetansMod.error.sendToConsole(GlobalText.shouldNotHappen()); } return; } } } // Custom Leave Message - End this.sendMessage(component, true); } }