/src/cpw/mods/fml/common/network/ModListResponsePacket.java

https://bitbucket.org/agaricusb/mcpc-1.4.5 · Java · 131 lines · 112 code · 17 blank · 2 comment · 10 complexity · b49b145948a05e24430202998eef4aa9 MD5 · raw file

  1. package cpw.mods.fml.common.network;
  2. import static cpw.mods.fml.common.network.FMLPacket.Type.MOD_IDENTIFIERS;
  3. import static cpw.mods.fml.common.network.FMLPacket.Type.MOD_LIST_RESPONSE;
  4. import static cpw.mods.fml.common.network.FMLPacket.Type.MOD_MISSING;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Map.Entry;
  8. import java.util.logging.Logger;
  9. import net.minecraft.server.NetHandler;
  10. import net.minecraft.server.NetLoginHandler;
  11. import net.minecraft.server.INetworkManager;
  12. import net.minecraft.server.Packet1Login;
  13. import net.minecraft.server.Packet250CustomPayload;
  14. import cpw.mods.fml.common.FMLLog;
  15. import cpw.mods.fml.common.Loader;
  16. import cpw.mods.fml.common.ModContainer;
  17. import mcpc.com.google.common.collect.Lists;
  18. import mcpc.com.google.common.collect.Maps;
  19. import mcpc.com.google.common.io.ByteArrayDataInput;
  20. import mcpc.com.google.common.io.ByteArrayDataOutput;
  21. import mcpc.com.google.common.io.ByteStreams;
  22. public class ModListResponsePacket extends FMLPacket
  23. {
  24. private Map<String,String> modVersions;
  25. private List<String> missingMods;
  26. public ModListResponsePacket()
  27. {
  28. super(MOD_LIST_RESPONSE);
  29. }
  30. @Override
  31. public byte[] generatePacket(Object... data)
  32. {
  33. Map<String,String> modVersions = (Map<String, String>) data[0];
  34. List<String> missingMods = (List<String>) data[1];
  35. ByteArrayDataOutput dat = ByteStreams.newDataOutput();
  36. dat.writeInt(modVersions.size());
  37. for (Entry<String, String> version : modVersions.entrySet())
  38. {
  39. dat.writeUTF(version.getKey());
  40. dat.writeUTF(version.getValue());
  41. }
  42. dat.writeInt(missingMods.size());
  43. for (String missing : missingMods)
  44. {
  45. dat.writeUTF(missing);
  46. }
  47. return dat.toByteArray();
  48. }
  49. @Override
  50. public FMLPacket consumePacket(byte[] data)
  51. {
  52. ByteArrayDataInput dat = ByteStreams.newDataInput(data);
  53. int versionListSize = dat.readInt();
  54. modVersions = Maps.newHashMapWithExpectedSize(versionListSize);
  55. for (int i = 0; i < versionListSize; i++)
  56. {
  57. String modName = dat.readUTF();
  58. String modVersion = dat.readUTF();
  59. modVersions.put(modName, modVersion);
  60. }
  61. int missingModSize = dat.readInt();
  62. missingMods = Lists.newArrayListWithExpectedSize(missingModSize);
  63. for (int i = 0; i < missingModSize; i++)
  64. {
  65. missingMods.add(dat.readUTF());
  66. }
  67. return this;
  68. }
  69. @Override
  70. public void execute(INetworkManager network, FMLNetworkHandler handler, NetHandler netHandler, String userName)
  71. {
  72. Map<String, ModContainer> indexedModList = Maps.newHashMap(Loader.instance().getIndexedModList());
  73. List<String> missingClientMods = Lists.newArrayList();
  74. List<String> versionIncorrectMods = Lists.newArrayList();
  75. for (String m : missingMods)
  76. {
  77. ModContainer mc = indexedModList.get(m);
  78. NetworkModHandler networkMod = handler.findNetworkModHandler(mc);
  79. if (networkMod.requiresClientSide())
  80. {
  81. missingClientMods.add(m);
  82. }
  83. }
  84. for (Entry<String,String> modVersion : modVersions.entrySet())
  85. {
  86. ModContainer mc = indexedModList.get(modVersion.getKey());
  87. NetworkModHandler networkMod = handler.findNetworkModHandler(mc);
  88. if (!networkMod.acceptVersion(modVersion.getValue()))
  89. {
  90. versionIncorrectMods.add(modVersion.getKey());
  91. }
  92. }
  93. Packet250CustomPayload pkt = new Packet250CustomPayload();
  94. pkt.tag = "FML";
  95. if (missingClientMods.size()>0 || versionIncorrectMods.size() > 0)
  96. {
  97. pkt.data = FMLPacket.makePacket(MOD_MISSING, missingClientMods, versionIncorrectMods);
  98. Logger.getLogger("Minecraft").info(String.format("User %s connection failed: missing %s, bad versions %s", userName, missingClientMods, versionIncorrectMods));
  99. FMLLog.info("User %s connection failed: missing %s, bad versions %s", userName, missingClientMods, versionIncorrectMods);
  100. // Mark this as bad
  101. FMLNetworkHandler.setHandlerState((NetLoginHandler) netHandler, FMLNetworkHandler.MISSING_MODS_OR_VERSIONS);
  102. }
  103. else
  104. {
  105. pkt.data = FMLPacket.makePacket(MOD_IDENTIFIERS, netHandler);
  106. Logger.getLogger("Minecraft").info(String.format("User %s connecting with mods %s", userName, modVersions.keySet()));
  107. FMLLog.info("User %s connecting with mods %s", userName, modVersions.keySet());
  108. }
  109. pkt.length = pkt.data.length;
  110. network.queue(pkt);
  111. // reset the continuation flag - we have completed extra negotiation and the login should complete now
  112. NetLoginHandler.a((NetLoginHandler) netHandler, true);
  113. }
  114. }