CodeHz

chat

Sep 6th, 2018
954
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. def eat(string src) {
  3.   auto r := range(src);
  4.   auto ret = "";
  5.   while (!r.empty() && r.front() == ' ') {
  6.     r.pop_front();
  7.   }
  8.   while (!r.empty() && r.front() != ' ') {
  9.     ret += r.front().to_string();
  10.     r.pop_front();
  11.   }
  12.   if (!r.empty()) { r.pop_front(); }
  13.   return [ret, r.join("")];
  14. }
  15.  
  16. def eat(string src, int n) {
  17.   auto r := range(src);
  18.   auto arr = [];
  19.   auto temp = "";
  20.   while (!r.empty() && r.front() == ' ') {
  21.     r.pop_front();
  22.   }
  23.   while (!r.empty() && arr.size() < n) {
  24.     if (r.front() == ' ') {
  25.       arr.push_back(temp);
  26.       temp = "";
  27.       r.pop_front();
  28.       while (!r.empty() && r.front() == ' ') {
  29.         r.pop_front();
  30.       }
  31.       continue;
  32.     }
  33.     temp += r.front().to_string();
  34.     r.pop_front();
  35.   }
  36.   if (!r.empty) {
  37.     arr.push_back(r.join(""));
  38.   }
  39.   return arr;
  40. }
  41.  
  42. auto ChatModDB := ProfileDB
  43.  
  44. WorldDB.exec("CREATE TABLE IF NOT EXISTS chat_log(id INTEGER PRIMARY KEY AUTOINCREMENT, sender_uuid BLOB CHECK(valid_uuid(sender_uuid)), sender_name TEXT, content TEXT);")
  45. global chat_log_stmt = WorldDB.prepare("INSERT INTO chat_log(sender_uuid, sender_name, content) VALUES (?, ?, ?);")
  46. ChatModDB.exec("CREATE TABLE IF NOT EXISTS chat_mod(uuid BLOB PRIMARY KEY CHECK(valid_uuid(uuid)), prefix TEXT, suffix TEXT, tails TEXT) WITHOUT ROWID;")
  47. global chat_mod_query = ChatModDB.prepare("SELECT ifnull(prefix, ''), ifnull(suffix, ''), ifnull(tails, '') FROM chat_mod WHERE uuid=?;")
  48. global chat_mod_update = [
  49.   "prefix": ChatModDB.prepare("INSERT INTO chat_mod(uuid, prefix) VALUES (?, ?) ON CONFLICT(uuid) DO UPDATE SET prefix=excluded.prefix;"),
  50.   "suffix": ChatModDB.prepare("INSERT INTO chat_mod(uuid, suffix) VALUES (?, ?) ON CONFLICT(uuid) DO UPDATE SET suffix=excluded.suffix;"),
  51.   "tails": ChatModDB.prepare("INSERT INTO chat_mod(uuid, tails) VALUES (?, ?) ON CONFLICT(uuid) DO UPDATE SET tails=excluded.tails;")
  52. ];
  53.  
  54. dbus_chat_start(fun(string message) {
  55.   forEachPlayer(fun[message](ServerPlayer n) {
  56.     n.sendMessage("system։ " + message)
  57.   })
  58. })
  59.  
  60. setChatHook(fun(ServerPlayer sp, string message) {
  61.   auto sender = sp.getNameTag();
  62.   Log.info("CHAT", sender + ": " + message);
  63.   dbus_chat_log(sender, message);
  64.   auto uuid = sp.getUUID();
  65.   chat_log_stmt.bind(1, uuid);
  66.   chat_log_stmt.bind(2, sp.getNameTag());
  67.   chat_log_stmt.bind(3, message);
  68.   chat_log_stmt.step();
  69.   chat_log_stmt.reset();
  70.   chat_log_stmt.clear();
  71.  
  72.   auto send;
  73.  
  74.   chat_mod_query.bind(1, uuid);
  75.   switch (chat_mod_query.step()) {
  76.     case (ROW) {
  77.       send = fun[sender, message](n) { n.sendMessage(chat_mod_query.get(0) + sender + chat_mod_query.get(1) + "։ " + message + chat_mod_query.get(2)); };
  78.       break;
  79.     }
  80.     case (DONE) {
  81.       send = fun[sender, message](n) { n.sendMessage(sender + "։ " + message); };
  82.     }
  83.   }
  84.   forEachPlayer(fun[send](ServerPlayer n) {
  85.     send(n);
  86.   });
  87.   chat_mod_query.clear();
  88.   chat_mod_query.reset();
  89. });
  90.  
  91. {
  92.   auto chatOverload = CommandOverload()
  93.   chatOverload.add(Param_target_wildcard("target", false));
  94.   auto modEnum = EnumDef("chat_mod")
  95.   modEnum.add("prefix");
  96.   modEnum.add("suffix");
  97.   modEnum.add("tails");
  98.   chatOverload.add(Param_enum("attr", false, modEnum));
  99.   chatOverload.add(Param_message("text", false));
  100.   defcmd2("setchat", "Set chat option to players", 0, 0, fun(orig, list, str) {
  101.     if (list.size() == 0) {
  102.       return "No players selected";
  103.     }
  104.     auto spd = str.eat();
  105.     auto key := spd[0];
  106.     auto value := spd[1];
  107.     if (chat_mod_update.count(key) == 0) {
  108.       return "Illegal attr"
  109.     }
  110.     auto stmt := chat_mod_update[key];
  111.     auto res = "success";
  112.     for (item : list) {
  113.       stmt.bind(1, item.getUUID());
  114.       stmt.bind(2, value);
  115.       stmt.step();
  116.       stmt.reset();
  117.       stmt.clear();
  118.     }
  119.     return res;
  120.   }).add(chatOverload);
  121. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×