CrushedPixel

Untitled

Mar 5th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.77 KB | None | 0 0
  1. package eu.crushedpixel.sponge.packetgate.examples.listener;
  2.  
  3. import eu.crushedpixel.sponge.packetgate.api.data.PacketConnection;
  4. import eu.crushedpixel.sponge.packetgate.api.event.PacketEvent;
  5. import eu.crushedpixel.sponge.packetgate.api.listener.PacketListenerAdapter;
  6. import eu.crushedpixel.sponge.packetgate.plugin.PluginPacketGate;
  7. import net.minecraft.network.play.client.CPacketChatMessage;
  8. import net.minecraft.network.play.server.SPacketChat;
  9. import net.minecraft.util.text.TextComponentString;
  10. import org.slf4j.Logger;
  11.  
  12. /**
  13.  * An example PacketListener that filters all swear words sent
  14.  * by clients until it is globally disabled.
  15.  *
  16.  * This also replaces all messages sent out to clients containing
  17.  * swear words, e.g. plugin messages the server owner has no control over.
  18.  *
  19.  * This is obviously just an example, and disabling the filter
  20.  * should be handled by a command that requires permissions
  21.  * in a real-world scenario.
  22.  */
  23. public class SwearWordListener extends PacketListenerAdapter {
  24.  
  25.     private static final String[] FORBIDDEN_PHRASES = { "shit", "fuck", "ez", "rekt" };
  26.  
  27.     private final Logger logger;
  28.  
  29.     public SwearWordListener(Logger logger) {
  30.         this.logger = logger;
  31.     }
  32.  
  33.     @Override
  34.     public Class[] listensTo() {
  35.         return new Class[] {
  36.                 CPacketChatMessage.class,
  37.                 SPacketChat.class
  38.         };
  39.     }
  40.  
  41.     @Override
  42.     public void onPacketRead(PacketEvent event, PacketConnection connection) {
  43.         // we can cast without a check here, because this Listener only
  44.         // receives events for incoming CPacketChatMessage packets
  45.         CPacketChatMessage packet = (CPacketChatMessage)event.getPacket();
  46.  
  47.         // check if the chat message contains swear words
  48.         boolean swearing = false;
  49.         for (String phrase : FORBIDDEN_PHRASES) {
  50.             if (packet.getMessage().contains(phrase)) {
  51.                 swearing = true;
  52.                 break;
  53.             }
  54.         }
  55.  
  56.         if (swearing) {
  57.             // cancel the event so the packet is never processed by the server
  58.             event.setCancelled(true);
  59.  
  60.             // send a nice message to the connection that sent this packet
  61.             connection.sendPacket(new SPacketChat(new TextComponentString("Please don't swear!")));
  62.  
  63.         } else if (packet.getMessage().equalsIgnoreCase("Disable swear filter")) {
  64.             event.setCancelled(true);
  65.             connection.sendPacket(new SPacketChat(new TextComponentString("Swear filter has been globally disabled.")));
  66.  
  67.             // unregister this listener to globally disable it
  68.             PluginPacketGate.getPacketGate().unregisterListener(this);
  69.         }
  70.     }
  71.  
  72.     @Override
  73.     public void onPacketWrite(PacketEvent event, PacketConnection connection) {
  74.         // we can cast without a check here, because this Listener only
  75.         // receives events for outgoing SPacketChat packets
  76.         SPacketChat packet = (SPacketChat)event.getPacket();
  77.  
  78.         // Note that SPacketChat.chatComponent has been made
  79.         // publicly accessible using access transformers
  80.         // - you may have to do this for your plugin as well
  81.         String text = packet.chatComponent.getUnformattedText();
  82.  
  83.         boolean swearing = false;
  84.         for (String phrase : FORBIDDEN_PHRASES) {
  85.             if (text.contains(phrase)) {
  86.                 swearing = true;
  87.                 break;
  88.             }
  89.         }
  90.  
  91.         if (swearing) {
  92.             // replace the outgoing packet with a censored chat packet
  93.             // instead of cancelling the event
  94.             event.setPacket(new SPacketChat(new TextComponentString("<Censored Plugin Message>")));
  95.  
  96.             logger.info("An outgoing swear word has been intercepted!", text);
  97.         }
  98.     }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment