123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package me.km.playerbank;
- import me.km.KajetansMod;
- import me.km.events.PlayerLeaveMessageEvent;
- import me.km.snuviscript.ScriptEvents;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.entity.player.EntityPlayerMP;
- import net.minecraft.network.NetHandlerPlayServer;
- import net.minecraft.network.NetworkManager;
- import net.minecraft.network.play.client.CPacketAnimation;
- import net.minecraft.network.play.client.CPacketChatMessage;
- import net.minecraft.network.play.server.SPacketChat;
- import net.minecraft.server.MinecraftServer;
- import net.minecraft.util.text.ITextComponent;
- import net.minecraft.util.text.TextComponentString;
- import net.minecraft.util.text.TextComponentTranslation;
- import net.minecraft.util.text.TextFormatting;
- import net.minecraftforge.common.MinecraftForge;
- import org.apache.logging.log4j.LogManager;
- import org.apache.logging.log4j.Logger;
- public class ModNetHandlerPlayServer extends NetHandlerPlayServer
- {
- private static final Logger LOGGER = LogManager.getLogger();
- private final MinecraftServer serverController;
- private final ScriptEvents scriptEvents;
- public ModNetHandlerPlayServer(MinecraftServer server, NetworkManager nm, EntityPlayerMP p)
- {
- super(server, nm, p);
- serverController = server;
- scriptEvents = KajetansMod.scripts.getEvent(ScriptEvents.class);
- }
-
- @Override
- public void processChatMessage(CPacketChatMessage packetIn)
- {
- if(packetIn.getMessage().startsWith("/"))
- {
- String s = packetIn.getMessage();
- int i = s.indexOf(" ");
- String command;
- String left;
- if(i == -1)
- {
- command = s.substring(1).toLowerCase();
- left = "";
- }
- else
- {
- command = s.substring(1, i).toLowerCase();
- left = s.substring(i);
- }
- if(KajetansMod.scripts.isRegisteredScriptCommand(command))
- {
- if (this.player.getChatVisibility() == EntityPlayer.EnumChatVisibility.HIDDEN)
- {
- TextComponentTranslation trans = new TextComponentTranslation("chat.cannotSend", new Object[0]);
- trans.getStyle().setColor(TextFormatting.RED);
- this.sendPacket(new SPacketChat(trans));
- }
- else
- {
- this.player.markPlayerActive();
- scriptEvents.onCustomCommand(this.player, command, left);
- }
- }
- else
- {
- super.processChatMessage(new CPacketChatMessage("/" + command + left));
- }
- return;
- }
- super.processChatMessage(packetIn);
- }
-
- @Override
- public void onDisconnect(ITextComponent reason)
- {
- LOGGER.info("{} lost connection: {}", new Object[] {this.player.getName(), reason});
- this.serverController.refreshStatusNextTick();
- // Custom Leave Message - Start
- PlayerLeaveMessageEvent event = new PlayerLeaveMessageEvent(this.player, "No message was set.");
- if(!MinecraftForge.EVENT_BUS.post(event))
- {
- TextComponentString text = new TextComponentString(event.getMessage());
- this.serverController.getPlayerList().sendMessage(text);
- }
- // Custom Leave Message - End
- this.player.mountEntityAndWakeUp();
- this.serverController.getPlayerList().playerLoggedOut(this.player);
- if (this.serverController.isSinglePlayer() && this.player.getName().equals(this.serverController.getServerOwner()))
- {
- LOGGER.info("Stopping singleplayer server as player logged out");
- this.serverController.initiateShutdown();
- }
- }
- @Override
- public void handleAnimation(CPacketAnimation packetIn)
- {
- super.handleAnimation(packetIn);
- scriptEvents.onArmSwing(this.player, packetIn.getHand());
- }
- }
|