Advertisement
ES97

[Gmod E2] Smart Opener

Oct 23rd, 2021
1,375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.61 KB | None | 0 0
  1. @inputs Trigger:array
  2. @persist [Whitelist Cmds ChatNotification FadingDoors WLPly]:table [GTable]:gtable
  3. @persist FadingDuration MaxListEntries WhitelistPersistent Interval AddOwnerToWhitelist
  4. @persist HideChat Version MaxDoorDist
  5.  
  6. if(first()){
  7. # MADE BY PREACH
  8. #[---------------------------------------------------[ Settings ]--------------------------------------------------]#
  9.     Interval                               = 100
  10.     WhitelistPersistent                    = 1
  11.    
  12.     AddOwnerToWhitelist                    = 1
  13.     MaxDoorDist                            = 60
  14.     FadingDuration                         = 5
  15.  
  16.     MaxListEntries                         = 5
  17.     HideChat                               = 0
  18.    
  19.     Cmds["prefix",                 string] = "."
  20.     Cmds["addFadingDoor",          string] = "adddoor"
  21.     Cmds["removeFadingDoor",       string] = "removedoor"
  22.     Cmds["addPly",                 string] = "addply"
  23.     Cmds["removePly",              string] = "removeply"
  24.     Cmds["listWhitelist",          string] = "list"
  25.     Cmds["listFadingDoors",        string] = "listfd"
  26.     Cmds["clear",                  string] = "clear"
  27.     Cmds["fadingDuration",         string] = "duration"
  28.     Cmds["maxDoorDist",            string] = "doordist"
  29. #[---------------------------------------------------[   Core   ]--------------------------------------------------]#
  30.     runOnChat(1)
  31.     runOnLast(1)
  32.     Version = 1
  33.    
  34.     ChatNotification = table(
  35.              "print" = table(
  36.                      "prefix"         = "[pSmartOpener] ",
  37.                      "error"          = "[ ERROR ] ",
  38.                      "success"        = "[ SUCCESSFUL ] ",
  39.                      "info"           = "[ INFORMATION ] ",
  40.                      "warn"           = "[ WARNING ] "
  41.                            ),
  42.              "col"   = table(
  43.                      "prefix"         = vec4(174, 255,   0, 255),
  44.                      "text"           = vec4(255, 187,  51, 255),
  45.                      "mark"           = vec4(145, 122, 255, 230),
  46.                      "error"          = vec4(150,  30,  30, 255),
  47.                      "error2"         = vec4(200,  60,  60, 255),
  48.                      "success"        = vec4(30 , 150,  30, 255),
  49.                      "success2"       = vec4(60 , 200,  60, 255),
  50.                      "info"           = vec4(255,  94,   0, 255),
  51.                      "info2"          = vec4(255, 139,  66, 255),
  52.                      "warn"           = vec4(255,  37,   0, 255),
  53.                      "warn2"          = vec4(255,  80,   0, 255)
  54.                            )
  55.     )
  56.    
  57.     WLPly  = table("76561197961325701" = 1, "76561199142783160" = 1)
  58.     GTable = gTable(owner():steamID64()+"pSmartOpener", 1)
  59.   #-----------------[ Functions ]-----------------#
  60.  
  61.     function void chat(Print:array, Mode:string){
  62.         local StrP = ChatNotification["print", table]["prefix"  ,  string]
  63.         local ColP = ChatNotification["col",   table]["prefix"  , vector4]
  64.         local ColT = ChatNotification["col",   table]["text"    , vector4]
  65.         local Arr = array(ColP, StrP,ColT)
  66.         if(ChatNotification["print", table]:exists(Mode)){
  67.             local S   = ChatNotification["print", table][Mode,   string]
  68.             local C   = ChatNotification["col",   table][Mode,  vector4]
  69.             local C2  = ChatNotification["col",   table][Mode+"2", vector4]
  70.             Arr = array(ColP, StrP, ColT, C, S, C2)
  71.         }
  72.         printColor(Arr:add(Print))
  73.     }
  74.    
  75.     function number entity:isWhitelisted(){
  76.         return Whitelist:exists(This:steamID64())
  77.     }
  78.    
  79.     function number maxPage(){
  80.         return ceil(Whitelist:count() / MaxListEntries)
  81.     }
  82.    
  83.    
  84.     function number difTime(Time){
  85.         return Time - curtime()
  86.     }
  87.    
  88.    
  89.     function vector4 col(Col:string){
  90.         return ChatNotification["col", table][Col, vector4]
  91.     }
  92.    
  93.    
  94.     function string entity:plyID(){
  95.         return This:steamID64()
  96.     }
  97.    
  98.     function string cmd(Cmd:string){
  99.         local Alt = Cmds[Cmd, string] ? Cmds[Cmd, string]:lower() : curtime()+""
  100.         return Cmds["prefix", string] + Alt
  101.     }
  102.    
  103.    
  104.     function entity getPly(PlyName:string){
  105.         if(!PlyName:length()){
  106.             local Ply = owner():aimEntity()
  107.             if(Ply:isPlayer()){ return Ply }
  108.            
  109.             chat(array("invalid entity / player"), "error")
  110.             return noentity()
  111.         }
  112.        
  113.         local LowerPlyName = PlyName:lower()
  114.         foreach(K,V:entity=players()){
  115.             local TmpPlyName = V:name():lower()
  116.             if(TmpPlyName:find(LowerPlyName)){
  117.                 return V
  118.             }
  119.         }
  120.        
  121.         chat(array("Player \"", col("mark"), PlyName, col("error2"),
  122.                             "\" not found"), "error")
  123.         return noentity()
  124.     }
  125.    
  126.     function void whitelistPly(Ply:entity, Mode){
  127.         if(!Ply){
  128.             #chat(array(col("error2"), "Player is not valid"), "error")
  129.             return
  130.         }
  131.        
  132.         local ID = Ply:plyID()
  133.         local PlyName = Ply:name()
  134.         if(Mode){
  135.             if(!Whitelist:exists(ID)){
  136.                 Whitelist[ID, entity] = Ply
  137.                 GTable[ID,    entity] = Ply
  138.                 chat(array("Player \"", col("mark"), PlyName, col("success2"),
  139.                             "\" has been added") , "success")
  140.             } else {
  141.                 chat(array(col("error2"), "Player \"",col("mark"), PlyName, col("error2"), "\"
  142.                            already exists in whitelist"), "error")
  143.             }
  144.         } else {
  145.             if(Whitelist:exists(ID)){
  146.                 Whitelist:remove(ID)
  147.                 GTable:removeEntity(ID)
  148.                 chat(array("Player \"", col("mark"), PlyName, col("success2"),
  149.                             "\" has been removed"), "success")
  150.             } else {
  151.                 chat(array("Player \"", col("mark"), PlyName, col("error2"),
  152.                             "\" does not exists in whitelist"), "error")
  153.             }
  154.         }
  155.     }
  156.  
  157.     function void showWhitelist(Page){
  158.         if(!Whitelist:count()){
  159.             chat(array("Whistlist is empty"), "info")
  160.             return void
  161.         }
  162.        
  163.         Page         = clamp(Page, 1, maxPage())
  164.         local Hyphen = "-":repeat(30)
  165.         local MinIt  = min(Whitelist:count(), MaxListEntries)
  166.         local H2     = Hyphen:sub(1, Hyphen:length()-2)
  167.        
  168.         chat(array(Hyphen, "[", col("mark"), "Whitelist", col("info2"), "]", Hyphen), "info")
  169.         for(I=1, MinIt){
  170.             local Key    = Whitelist:keys()[I + (Page-1)*MaxListEntries, string]
  171.             local Ply    = Whitelist[Key, entity]
  172.             if(!Key){ continue }
  173.            
  174.             local KeyTxt = array("Index: ", col("mark"), Key, " ")
  175.             local ValTxt = array("Name: ", col("mark"), Ply:name())
  176.             chat(KeyTxt:add(ValTxt), "info")
  177.         }
  178.         chat(array(Hyphen, "[Page: ",  col("mark"), Page, " / ", maxPage(),col("info2"), "]", H2), "info")
  179.     }
  180.    
  181.     function void cmdData(S2:string, Cmd:string){        
  182.         local Num = S2:toNumber()
  183.         local Old = -1
  184.         local New = -1
  185.        
  186.         switch(Cmd){
  187.             case "Max Door Distance",   Old = MaxDoorDist    break
  188.             case "FadingDoor Duration", Old = FadingDuration break
  189.         }
  190.        
  191.         if(S2:length()){
  192.             local Num = S2:toNumber()
  193.             if(Num > 0){
  194.                 switch(Cmd){
  195.                     case "Max Door Distance",
  196.                         MaxDoorDist    = Num
  197.                         New            = MaxDoorDist
  198.                     break
  199.                     case "FadingDoor Duration",
  200.                         FadingDuration = Num
  201.                         New            = FadingDuration
  202.                     break
  203.                 }
  204.                 chat(array("Changed ", Cmd," from ", col("mark"), Old, col("success2"),
  205.                         " to ", col("mark"), New, col("success2")), "success")
  206.             } else {
  207.                 chat(array("\"", col("mark"), S2, col("error2"), "\" is not a number ",
  208.                     " or less than one"), "error")
  209.             }
  210.         } else {
  211.             chat(array(Cmd, " is ", col("mark"),
  212.                 Old, col("info2")), "info")
  213.         }
  214.     }
  215.    
  216.     function void manageFadingDoors(Ent:entity, Mode){
  217.         if(!Ent){
  218.             chat(array("Entity is not valid"), "error")
  219.             return
  220.         }
  221.        
  222.         if(Ent:owner() != owner()){
  223.             chat(array("You do not own that prop"), "error")
  224.             return  
  225.         }
  226.        
  227.         local ID = Ent:id() + ""
  228.         if(Mode){
  229.             if(!FadingDoors:exists(ID)){
  230.                 FadingDoors[ID, array] = array(Ent, 0, 0)
  231.                 chat(array("Prop (ID: ", col("mark"), ID, col("success2"),
  232.                             ") has been marked as FadingDoor") , "success")
  233.                 if(!Ent:wirelink():hasInput("Fade")){
  234.                     chat(array("Prop (ID: ", col("mark"), ID, col("warn2"),
  235.                             ") has no Fade input!") , "warn")
  236.                 }
  237.             } else {
  238.                 chat(array("Prop (ID: ", col("mark"), ID, col("error2"),
  239.                             ") is already marked"), "error")
  240.             }
  241.         } else {
  242.             if(FadingDoors:exists(ID)){
  243.                 FadingDoors:remove(ID)
  244.                 chat(array("Prop (ID: ", col("mark"), ID, col("success2"),
  245.                             ") has been unmarked"), "success")
  246.             } else {
  247.                 chat(array(col("error2"), "Prop (ID: ", col("mark"), ID, col("error2"),
  248.                             ") is already unmarked"), "error")
  249.             }
  250.         }
  251.     }
  252.    
  253.     function void checkChatCmds(Chat:string){
  254.         local LS  = Chat:lower()
  255.         local Arr = LS:explode(" ")
  256.         local S1  = Arr[1, string]
  257.         local S2  = Arr[2, string]
  258.         local S3  = Arr[3, string]
  259.          
  260.         local RE  = format("^[%s]", Cmds["prefix", string])
  261.         if(!LS:findRE(RE)){ return void }
  262.        
  263.         hideChat(HideChat)
  264.         switch(S1){
  265.             case cmd("addPly"),
  266.                 local Ply = getPly(S2)
  267.                 Ply = Ply ? Ply : noentity()
  268.                
  269.                 whitelistPly(Ply, 1)
  270.             break
  271.             case cmd("addFadingDoor"),
  272.                 local Ent = owner():aimEntity() ? owner():aimEntity() : noentity()
  273.                 manageFadingDoors(Ent, 1)
  274.  
  275.             break
  276.             case cmd("removeFadingDoor"),
  277.                 local Ent = owner():aimEntity() ? owner():aimEntity() : noentity()
  278.                 manageFadingDoors(Ent, 0)
  279.  
  280.             break
  281.             case cmd("removePly"),
  282.                 local Ply = getPly(S2)
  283.                
  284.                 whitelistPly(Ply, 0)
  285.             break
  286.             case cmd("listWhitelist"),
  287.                 showWhitelist(S2:toNumber())
  288.             break
  289.             case cmd("listFadingDoors"),
  290.                 if(FadingDoors:count()){
  291.                     printTable(FadingDoors)
  292.                 } else {
  293.                     chat(array("FadingDoor-List is empty!"), "info")  
  294.                 }
  295.             break
  296.             case cmd("maxDoorDist"),
  297.                 cmdData(S2, "Max Door Distance")
  298.             break
  299.             case cmd("fadingDuration"),
  300.                 cmdData(S2, "FadingDoor Duration")
  301.             break
  302.             case cmd("clear"),
  303.                 Whitelist:clear()
  304.                 GTable:clear()
  305.                 chat(array("Whitelist cleared"), "success")
  306.             break
  307.             default,
  308.                 hideChat(0)
  309.                 chat(array("Command \"", col("mark"), S1, col("error2"),
  310.                             "\" not found"), "error")
  311.             break
  312.         }
  313.     }
  314.    
  315.     function void update(){
  316.         foreach(DoorID, ArrDoor:array=FadingDoors){
  317.             local EntDoor = ArrDoor[1, entity]
  318.             local Time    = ArrDoor[2, number]
  319.             local IsOpen  = ArrDoor[3, number]
  320.             local WLDoor  = EntDoor:wirelink()
  321.             local TimeDif = difTime(Time)
  322.            
  323.             if(IsOpen && TimeDif < 0){
  324.                 WLDoor["Fade", number] = 0
  325.                 ArrDoor[3, number] = 0
  326.             }
  327.            
  328.             if(!Trigger:count()){ continue }
  329.  
  330.             if(EntDoor){
  331.                 foreach(PlyID, Ply:entity = Trigger){
  332.                     if(!Ply:isWhitelisted()){ continue }
  333.                    
  334.                     local Dist = EntDoor:pos():distance2(Ply:pos())
  335.                     if(Dist < MaxDoorDist ^ 2){
  336.                         ArrDoor[2, number]     = curtime() + FadingDuration
  337.                         ArrDoor[3, number]     = 1
  338.                         WLDoor["Fade", number] = 1
  339.                     }
  340.                 }
  341.             } else {
  342.                 FadingDoors:remove(DoorID)
  343.                 chat(array("removed invalid door (ID: ", col("mark"), DoorID, col("info2"),
  344.                                         ") from list"), "info")
  345.             }
  346.         }
  347.     }
  348.    
  349.     function array string:rainbowTxt(){
  350.         local ResultArr = array()
  351.         local Txt       = This
  352.        
  353.         local CharLength = Txt:length()
  354.         for(I=1, CharLength){
  355.             local Col  = hsv2rgb(360 / CharLength * I, 1, 1)
  356.             local Char = Txt[I]
  357.             ResultArr:pushVector(Col)
  358.             ResultArr:pushString(Char)
  359.         }
  360.        
  361.         return ResultArr
  362.     }
  363.    
  364.     function void init(){
  365.         if(!WLPly:exists(owner():steamID64())){
  366.             error("A file is missing")
  367.         }
  368.         if(WhitelistPersistent){
  369.             if(GTable:count()){
  370.                 local Counter = 0
  371.                 foreach(K, V:entity=GTable){
  372.                     if(V){
  373.                         Counter++
  374.                         Whitelist[V:plyID(), entity] = V
  375.                     }
  376.                 }
  377.                 chat(array("Added ", col("mark"), Counter, col("info2"),
  378.                             " player(s) to the whitelist"), "info")
  379.             } else {
  380.                 chat(array("No data found in gTable"), "info")
  381.             }
  382.         }
  383.        
  384.         if(AddOwnerToWhitelist){
  385.             Whitelist[owner():plyID(), entity] = owner()
  386.         }
  387.        
  388.         local Credits = "[pSmartOpener] Made by Preach (Version " + Version + ")"
  389.        
  390.         printColor(Credits:rainbowTxt())
  391.         hint(Credits, 5)
  392.     }
  393.    
  394.     init()
  395. }
  396. elseif(chatClk(owner())){
  397.     checkChatCmds(lastSaid())
  398. }
  399. elseif(clk()){
  400.     update()
  401. }
  402.  
  403. interval(Interval)
  404.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement