ModDedicatedPlayerList.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package me.km.events;
  2. import com.mojang.authlib.GameProfile;
  3. import io.netty.buffer.Unpooled;
  4. import java.util.UUID;
  5. import me.km.KajetansMod;
  6. import me.km.api.GlobalText;
  7. import me.km.api.Utils;
  8. import net.minecraft.entity.Entity;
  9. import net.minecraft.entity.player.EntityPlayer;
  10. import net.minecraft.entity.player.EntityPlayerMP;
  11. import net.minecraft.nbt.NBTTagCompound;
  12. import net.minecraft.network.NetHandlerPlayServer;
  13. import net.minecraft.network.NetworkManager;
  14. import net.minecraft.network.PacketBuffer;
  15. import net.minecraft.network.play.server.SPacketCustomPayload;
  16. import net.minecraft.network.play.server.SPacketEntityEffect;
  17. import net.minecraft.network.play.server.SPacketHeldItemChange;
  18. import net.minecraft.network.play.server.SPacketJoinGame;
  19. import net.minecraft.network.play.server.SPacketPlayerAbilities;
  20. import net.minecraft.network.play.server.SPacketServerDifficulty;
  21. import net.minecraft.scoreboard.ServerScoreboard;
  22. import net.minecraft.server.dedicated.DedicatedPlayerList;
  23. import net.minecraft.server.dedicated.DedicatedServer;
  24. import net.minecraft.server.management.PlayerProfileCache;
  25. import net.minecraft.util.math.BlockPos;
  26. import net.minecraft.util.text.ITextComponent;
  27. import net.minecraft.util.text.TextComponentString;
  28. import net.minecraft.util.text.TextComponentTranslation;
  29. import net.minecraft.world.World;
  30. import net.minecraft.world.WorldServer;
  31. import net.minecraft.world.chunk.storage.AnvilChunkLoader;
  32. import net.minecraft.world.storage.WorldInfo;
  33. import net.minecraftforge.common.MinecraftForge;
  34. import net.minecraftforge.fml.relauncher.Side;
  35. import net.minecraftforge.fml.relauncher.SideOnly;
  36. import org.apache.logging.log4j.LogManager;
  37. import org.apache.logging.log4j.Logger;
  38. @SideOnly(Side.SERVER)
  39. public class ModDedicatedPlayerList extends DedicatedPlayerList
  40. {
  41. private final DedicatedServer mcServer;
  42. private static final Logger LOG = LogManager.getLogger();
  43. public ModDedicatedPlayerList(DedicatedServer server)
  44. {
  45. super(server);
  46. this.mcServer = server;
  47. }
  48. // copied from net.minecraft.server.management.PlayerList
  49. private void setPlayerGameTypeBasedOnOther(EntityPlayerMP target, EntityPlayerMP source, World worldIn)
  50. {
  51. /*if (source != null)
  52. {
  53. target.interactionManager.setGameType(source.interactionManager.getGameType());
  54. }
  55. else if (this.gameType != null)
  56. {
  57. target.interactionManager.setGameType(this.gameType);
  58. }*/
  59. target.interactionManager.initializeGameType(worldIn.getWorldInfo().getGameType());
  60. }
  61. // copied from net.minecraft.server.management.PlayerList
  62. @Override
  63. public void initializeConnectionToPlayer(NetworkManager netManager, EntityPlayerMP playerIn, NetHandlerPlayServer nethandlerplayserver)
  64. {
  65. GameProfile gameprofile = playerIn.getGameProfile();
  66. PlayerProfileCache playerprofilecache = this.mcServer.getPlayerProfileCache();
  67. GameProfile gameprofile1 = playerprofilecache.getProfileByUUID(gameprofile.getId());
  68. String s = gameprofile1 == null ? gameprofile.getName() : gameprofile1.getName();
  69. playerprofilecache.addEntry(gameprofile);
  70. NBTTagCompound nbttagcompound = this.readPlayerDataFromFile(playerIn);
  71. playerIn.setWorld(this.mcServer.worldServerForDimension(playerIn.dimension));
  72. World playerWorld = this.mcServer.worldServerForDimension(playerIn.dimension);
  73. if (playerWorld == null)
  74. {
  75. playerIn.dimension = 0;
  76. playerWorld = this.mcServer.worldServerForDimension(0);
  77. BlockPos spawnPoint = playerWorld.provider.getRandomizedSpawnPoint();
  78. playerIn.setPosition(spawnPoint.getX(), spawnPoint.getY(), spawnPoint.getZ());
  79. }
  80. playerIn.setWorld(playerWorld);
  81. playerIn.interactionManager.setWorld((WorldServer)playerIn.world);
  82. String s1 = "local";
  83. if (netManager.getRemoteAddress() != null)
  84. {
  85. s1 = netManager.getRemoteAddress().toString();
  86. }
  87. LOG.info("{}[{}] logged in with entity id {} at ({}, {}, {})", new Object[] {playerIn.getName(), s1, playerIn.getEntityId(), playerIn.posX, playerIn.posY, playerIn.posZ});
  88. WorldServer worldserver = this.mcServer.worldServerForDimension(playerIn.dimension);
  89. WorldInfo worldinfo = worldserver.getWorldInfo();
  90. this.setPlayerGameTypeBasedOnOther(playerIn, null, worldserver);
  91. playerIn.connection = nethandlerplayserver;
  92. nethandlerplayserver.sendPacket(new SPacketJoinGame(playerIn.getEntityId(), playerIn.interactionManager.getGameType(), worldinfo.isHardcoreModeEnabled(), worldserver.provider.getDimension(), worldserver.getDifficulty(), this.getMaxPlayers(), worldinfo.getTerrainType(), worldserver.getGameRules().getBoolean("reducedDebugInfo")));
  93. nethandlerplayserver.sendPacket(new SPacketCustomPayload("MC|Brand", (new PacketBuffer(Unpooled.buffer())).writeString(this.getServerInstance().getServerModName())));
  94. nethandlerplayserver.sendPacket(new SPacketServerDifficulty(worldinfo.getDifficulty(), worldinfo.isDifficultyLocked()));
  95. nethandlerplayserver.sendPacket(new SPacketPlayerAbilities(playerIn.capabilities));
  96. nethandlerplayserver.sendPacket(new SPacketHeldItemChange(playerIn.inventory.currentItem));
  97. this.updatePermissionLevel(playerIn);
  98. playerIn.getStatFile().markAllDirty();
  99. playerIn.getStatFile().sendAchievements(playerIn);
  100. this.sendScoreboard((ServerScoreboard)worldserver.getScoreboard(), playerIn);
  101. this.mcServer.refreshStatusNextTick();
  102. // Custom Join Message - Start
  103. //TextComponentTranslation textcomponenttranslation;
  104. PlayerJoinMessageEvent event;
  105. if (playerIn.getName().equalsIgnoreCase(s))
  106. {
  107. //textcomponenttranslation = new TextComponentTranslation("multiplayer.player.joined", new Object[] {playerIn.getDisplayName()});
  108. event = new PlayerJoinMessageEvent(playerIn, false, "No message was set.");
  109. }
  110. else
  111. {
  112. //textcomponenttranslation = new TextComponentTranslation("multiplayer.player.joined.renamed", new Object[] {playerIn.getDisplayName(), s});
  113. event = new PlayerJoinMessageEvent(playerIn, true, "No message was set.");
  114. }
  115. //textcomponenttranslation.getStyle().setColor(TextFormatting.YELLOW);
  116. //this.sendMessage(textcomponenttranslation);
  117. if(!MinecraftForge.EVENT_BUS.post(event))
  118. {
  119. this.sendMessage(new TextComponentString(event.getMessage()));
  120. }
  121. // Custom Join Message - End
  122. this.playerLoggedIn(playerIn);
  123. nethandlerplayserver.setPlayerLocation(playerIn.posX, playerIn.posY, playerIn.posZ, playerIn.rotationYaw, playerIn.rotationPitch);
  124. this.updateTimeAndWeatherForPlayer(playerIn, worldserver);
  125. if (!this.mcServer.getResourcePackUrl().isEmpty())
  126. {
  127. playerIn.loadResourcePack(this.mcServer.getResourcePackUrl(), this.mcServer.getResourcePackHash());
  128. }
  129. playerIn.getActivePotionEffects().stream().forEach((potioneffect) ->
  130. {
  131. nethandlerplayserver.sendPacket(new SPacketEntityEffect(playerIn.getEntityId(), potioneffect));
  132. });
  133. if (nbttagcompound != null && nbttagcompound.hasKey("RootVehicle", 10))
  134. {
  135. NBTTagCompound nbttagcompound1 = nbttagcompound.getCompoundTag("RootVehicle");
  136. Entity entity1 = AnvilChunkLoader.readWorldEntity(nbttagcompound1.getCompoundTag("Entity"), worldserver, true);
  137. if (entity1 != null)
  138. {
  139. UUID uuid = nbttagcompound1.getUniqueId("Attach");
  140. if (entity1.getUniqueID().equals(uuid))
  141. {
  142. playerIn.startRiding(entity1, true);
  143. }
  144. else
  145. {
  146. for (Entity entity : entity1.getRecursivePassengers())
  147. {
  148. if (entity.getUniqueID().equals(uuid))
  149. {
  150. playerIn.startRiding(entity, true);
  151. break;
  152. }
  153. }
  154. }
  155. if (!playerIn.isRiding())
  156. {
  157. LOG.warn("Couldn\'t reattach entity to player");
  158. worldserver.removeEntityDangerously(entity1);
  159. entity1.getRecursivePassengers().stream().forEach((entity2) ->
  160. {
  161. worldserver.removeEntityDangerously(entity2);
  162. });
  163. }
  164. }
  165. }
  166. playerIn.addSelfToInternalCraftingInventory();
  167. net.minecraftforge.fml.common.FMLCommonHandler.instance().firePlayerLoggedIn(playerIn);
  168. }
  169. // workaround handler for leave message
  170. @Override
  171. public void sendMessage(ITextComponent component)
  172. {
  173. // Custom Leave Message - Start
  174. if(component instanceof TextComponentTranslation)
  175. {
  176. TextComponentTranslation trans = (TextComponentTranslation) component;
  177. switch(trans.getKey())
  178. {
  179. case "multiplayer.player.left":
  180. {
  181. // trying to get a player
  182. Object[] o = trans.getFormatArgs();
  183. if(o.length >= 1)
  184. {
  185. EntityPlayer p = Utils.getPlayerByDisplayName(o[0].toString());
  186. if(p == null)
  187. {
  188. KajetansMod.error.sendToConsole(GlobalText.shouldNotHappen());
  189. return;
  190. }
  191. PlayerLeaveMessageEvent event = new PlayerLeaveMessageEvent(p, "No message was set.");;
  192. if(!MinecraftForge.EVENT_BUS.post(event))
  193. {
  194. this.sendMessage(new TextComponentString(event.getMessage()), true);
  195. }
  196. }
  197. else
  198. {
  199. KajetansMod.error.sendToConsole(GlobalText.shouldNotHappen());
  200. }
  201. return;
  202. }
  203. }
  204. }
  205. // Custom Leave Message - End
  206. this.sendMessage(component, true);
  207. }
  208. }