ModNetHandlerPlayServer.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package me.km.playerbank;
  2. import me.km.KajetansMod;
  3. import me.km.events.PlayerLeaveMessageEvent;
  4. import me.km.snuviscript.ScriptEvents;
  5. import net.minecraft.entity.player.EntityPlayer;
  6. import net.minecraft.entity.player.EntityPlayerMP;
  7. import net.minecraft.network.NetHandlerPlayServer;
  8. import net.minecraft.network.NetworkManager;
  9. import net.minecraft.network.play.client.CPacketAnimation;
  10. import net.minecraft.network.play.client.CPacketChatMessage;
  11. import net.minecraft.network.play.server.SPacketChat;
  12. import net.minecraft.server.MinecraftServer;
  13. import net.minecraft.util.text.ITextComponent;
  14. import net.minecraft.util.text.TextComponentString;
  15. import net.minecraft.util.text.TextComponentTranslation;
  16. import net.minecraft.util.text.TextFormatting;
  17. import net.minecraftforge.common.MinecraftForge;
  18. import org.apache.logging.log4j.LogManager;
  19. import org.apache.logging.log4j.Logger;
  20. public class ModNetHandlerPlayServer extends NetHandlerPlayServer
  21. {
  22. private static final Logger LOGGER = LogManager.getLogger();
  23. private final MinecraftServer serverController;
  24. private final ScriptEvents scriptEvents;
  25. public ModNetHandlerPlayServer(MinecraftServer server, NetworkManager nm, EntityPlayerMP p)
  26. {
  27. super(server, nm, p);
  28. serverController = server;
  29. scriptEvents = KajetansMod.scripts.getEvent(ScriptEvents.class);
  30. }
  31. @Override
  32. public void processChatMessage(CPacketChatMessage packetIn)
  33. {
  34. if(packetIn.getMessage().startsWith("/"))
  35. {
  36. String s = packetIn.getMessage();
  37. int i = s.indexOf(" ");
  38. String command;
  39. String left;
  40. if(i == -1)
  41. {
  42. command = s.substring(1).toLowerCase();
  43. left = "";
  44. }
  45. else
  46. {
  47. command = s.substring(1, i).toLowerCase();
  48. left = s.substring(i);
  49. }
  50. if(KajetansMod.scripts.isRegisteredScriptCommand(command))
  51. {
  52. if (this.player.getChatVisibility() == EntityPlayer.EnumChatVisibility.HIDDEN)
  53. {
  54. TextComponentTranslation trans = new TextComponentTranslation("chat.cannotSend", new Object[0]);
  55. trans.getStyle().setColor(TextFormatting.RED);
  56. this.sendPacket(new SPacketChat(trans));
  57. }
  58. else
  59. {
  60. this.player.markPlayerActive();
  61. scriptEvents.onCustomCommand(this.player, command, left);
  62. }
  63. }
  64. else
  65. {
  66. super.processChatMessage(new CPacketChatMessage("/" + command + left));
  67. }
  68. return;
  69. }
  70. super.processChatMessage(packetIn);
  71. }
  72. @Override
  73. public void onDisconnect(ITextComponent reason)
  74. {
  75. LOGGER.info("{} lost connection: {}", new Object[] {this.player.getName(), reason});
  76. this.serverController.refreshStatusNextTick();
  77. // Custom Leave Message - Start
  78. PlayerLeaveMessageEvent event = new PlayerLeaveMessageEvent(this.player, "No message was set.");
  79. if(!MinecraftForge.EVENT_BUS.post(event))
  80. {
  81. TextComponentString text = new TextComponentString(event.getMessage());
  82. this.serverController.getPlayerList().sendMessage(text);
  83. }
  84. // Custom Leave Message - End
  85. this.player.mountEntityAndWakeUp();
  86. this.serverController.getPlayerList().playerLoggedOut(this.player);
  87. if (this.serverController.isSinglePlayer() && this.player.getName().equals(this.serverController.getServerOwner()))
  88. {
  89. LOGGER.info("Stopping singleplayer server as player logged out");
  90. this.serverController.initiateShutdown();
  91. }
  92. }
  93. @Override
  94. public void handleAnimation(CPacketAnimation packetIn)
  95. {
  96. super.handleAnimation(packetIn);
  97. scriptEvents.onArmSwing(this.player, packetIn.getHand());
  98. }
  99. }