Advertisement
CloudHopper

KAG ChatCommands.as with anti-spam and spawn

Aug 2nd, 2013
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.90 KB | None | 0 0
  1. // Simple chat processing example.
  2. // If the player sends a command, the server does what the command says.
  3. // You can also modify the chat message before it is sent to clients by modifying text_out
  4.  
  5. #include "MakeSeed.as";
  6. #include "MakeCrate.as";
  7. #include "MakeScroll.as";
  8.  
  9. int minTimeBetweenInSeconds = 1;
  10.  
  11. bool onServerProcessChat( CRules@ this, const string& in text_in, string& out text_out, CPlayer@ player )
  12. {
  13.     if (player is null)
  14.         return true;
  15.  
  16.     const bool canSpawn = sv_test || player.isMod();
  17.    
  18.     if (text_in == "!bot" && player.isMod()) // TODO: whoaaa check seclevs
  19.     {
  20.         CPlayer@ bot = AddBot( "Henry" );
  21.         return true;
  22.     }
  23.  
  24.     //spawning things
  25.     CBlob@ blob = player.getBlob();
  26.  
  27.     if (blob is null) {
  28.         return true;
  29.     }
  30.  
  31.     // Check the time since their last message, and see if they should be allowed to send another
  32.     if (blob.exists("message sent"))
  33.     {
  34.         s32 timeDifference = getGameTime() - blob.get_s32("message sent");
  35.         if(timeDifference < (50 * minTimeBetweenInSeconds))
  36.         {
  37.             client_AddToChat( "Please stop spamming", SColor(255, 255, 0,0));
  38.             return false;
  39.         }
  40.         blob.set_s32("message sent", getGameTime());
  41.     }
  42.     else
  43.     {
  44.         blob.set_s32("message sent", getGameTime());
  45.     }
  46.    
  47.     if (text_in == "!tree" && canSpawn)
  48.     {
  49.         server_MakeSeed( blob.getPosition(), "tree_pine", 600, 1, 16 );
  50.     }
  51.     else if (text_in == "!btree" && canSpawn)
  52.     {
  53.         server_MakeSeed( blob.getPosition(), "tree_bushy", 400, 2, 16 );
  54.     }
  55.     else if (text_in == "!flowers" && canSpawn)
  56.     {
  57.         server_CreateBlob( "Entities/Natural/Flowers/Flowers.cfg", blob.getTeamNum(), blob.getPosition() );
  58.     }
  59.     else if (text_in == "!catapult" && canSpawn)
  60.     {
  61.         server_CreateBlob( "Entities/Vehicles/Catapult/Catapult.cfg", blob.getTeamNum(), blob.getPosition() );
  62.     }
  63.     else if (text_in == "!stones" && canSpawn)
  64.     {
  65.         CBlob@ b = server_CreateBlob( "Entities/Materials/MaterialStone.cfg", blob.getTeamNum(), blob.getPosition() );
  66.  
  67.         if (b !is null) {
  68.             b.server_SetQuantity(320);
  69.         }
  70.     }
  71.     else if (text_in == "!arrows" && canSpawn)
  72.     {
  73.         for (int i = 0; i < 3; i++)
  74.         {
  75.             CBlob@ b = server_CreateBlob( "Entities/Materials/MaterialArrows.cfg", blob.getTeamNum(), blob.getPosition() );
  76.  
  77.             if (b !is null) {
  78.                 b.server_SetQuantity(30);
  79.             }
  80.         }
  81.     }
  82.     else if (text_in == "!bombs" && canSpawn)
  83.     {
  84.         //  for (int i = 0; i < 3; i++)
  85.         CBlob@ b = server_CreateBlob( "Entities/Materials/MaterialBombs.cfg", blob.getTeamNum(), blob.getPosition() );
  86.  
  87.         if (b !is null) {
  88.             b.server_SetQuantity(30);
  89.         }
  90.     }
  91.     else if (text_in == "!spawnwater" && canSpawn)
  92.     {
  93.         getMap().server_setFloodWaterWorldspace(blob.getPosition(),true);
  94.     }
  95.     else if (text_in == "!seed")
  96.     {
  97.         // crash prevention
  98.     }
  99.     else if (text_in == "!killme")
  100.     {
  101.         blob.server_Hit( blob, blob.getPosition(), Vec2f(0,0), 4.0f, 0);
  102.     }
  103.     else if (text_in == "!crate" && canSpawn)
  104.     {
  105.         client_AddToChat( "usage: !crate BLOBNAME [DESCRIPTION]", SColor(255, 255, 0,0));
  106.         server_MakeCrate( "", "", 0, blob.getTeamNum(), Vec2f( blob.getPosition().x, blob.getPosition().y - 30.0f ) );
  107.     }
  108.     else if (text_in == "!debug" && player.isMod())
  109.     {
  110.         // print all blobs
  111.         CBlob@[] all;
  112.         getBlobs( @all );
  113.  
  114.         for (u32 i=0; i < all.length; i++)
  115.         {
  116.             CBlob@ blob = all[i];
  117.             print("["+blob.getName()+" " + blob.getNetworkID() + "] ");            
  118.         }
  119.     }
  120.     else if (text_in.substr(0,1) == "!")
  121.     {
  122.         // check if we have tokens
  123.         string[]@ tokens = text_in.split(" ");
  124.  
  125.         if (tokens.length > 1)
  126.         {
  127.             if (tokens[0] == "!crate" && canSpawn)
  128.             {
  129.                 int frame = tokens[1] == "catapult" ? 1 : 0;
  130.                 string description = tokens.length > 2 ? tokens[2] : tokens[1];
  131.                 server_MakeCrate( tokens[1], description, frame, -1, Vec2f( blob.getPosition().x, blob.getPosition().y ) );
  132.             }
  133.             else if (tokens[0] == "!team" && canSpawn)
  134.             {
  135.                 int team = parseInt(tokens[1]);
  136.                 blob.server_setTeamNum(team);
  137.             }
  138.             else if (tokens[0] == "!scroll" && canSpawn)
  139.             {
  140.                 server_MakePredefinedScroll( blob.getPosition(), tokens[1] );
  141.             }
  142.             else if (tokens[0] == "!spawn" && canSpawn) // Allow players to spawn a specified amount of blobs
  143.             {
  144.                 int amount = tokens.length > 2 ? parseInt(tokens[2]) : 1;
  145.                 int count = 0;
  146.                 for (count = 0; count < amount; count++)
  147.                 {
  148.                     server_CreateBlob( tokens[1], -1, blob.getPosition() );
  149.                 }
  150.             }
  151.  
  152.             return true;
  153.         }
  154.  
  155.         // try to spawn an actor with this name !actor
  156.         string name = text_in.substr(1, text_in.size());
  157.  
  158.         if (canSpawn && server_CreateBlob( name, -1, blob.getPosition() ) is null) {
  159.             client_AddToChat( "blob " + text_in + " not found", SColor(255, 255, 0,0));
  160.         }
  161.     }
  162.  
  163.     return true;
  164. }
  165.  
  166. bool onClientProcessChat( CRules@ this, const string& in text_in, string& out text_out, CPlayer@ player )
  167. {
  168.     if (text_in == "!debug" && !getNet().isServer())
  169.     {
  170.         // print all blobs
  171.         CBlob@[] all;
  172.         getBlobs( @all );
  173.  
  174.         for (u32 i=0; i < all.length; i++)
  175.         {
  176.             CBlob@ blob = all[i];
  177.             print("["+blob.getName()+" " + blob.getNetworkID() + "] ");
  178.  
  179.             if (blob.getShape() !is null)
  180.             {
  181.                 CBlob@[] overlapping;      
  182.                 if (blob.getOverlapping( @overlapping ))
  183.                 {
  184.                     for (uint i = 0; i < overlapping.length; i++)
  185.                     {
  186.                         CBlob@ overlap = overlapping[i];   
  187.                         print("       " + overlap.getName() + " " + overlap.isLadder());
  188.                     }
  189.                 }
  190.             }
  191.         }
  192.     }
  193.  
  194.     return true;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement