Advertisement
NovaViper

Untitled

Jun 18th, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-# VARIABLE DECLARATION #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
  2. $currentLocation = Split-Path -parent $PSCommandPath
  3. $ui = (Get-Host).UI.RawUI
  4. $consoleName = "Console Master"
  5. $consoleVersion = '1.0'
  6. $prefix = "$consoleName $consoleVersion"
  7. $quitConsole = $false
  8. $configFileName = "config.json"
  9. $sleepTime = 5
  10. #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-# POWERSHELL CUSTOMIZATION #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
  11. $ui.WindowTitle = "$prefix"
  12. Set-Location $currentLocation
  13. #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-# FUNCTION DECLARATION #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
  14.  
  15. function Setup-Console(){
  16.     Clear-Host
  17.     $ui.WindowTitle = "$prefix+: Server Running (Jar: , MC )"
  18.     Write-Host "Welcome To $consoleName!!"
  19.     Write-Host ''
  20.    
  21.     $jarFile = Check-Jar "Please enter the name of the server's .jar file (w/o the extension)"
  22.     $mcVersion = Check-Version "Please enter the Minecraft version your server is using"
  23.     $ramDataType = Check-RAM-Type
  24.     $maxRAM = Check-Integer "Please enter your maximum amount of RAM (Random Access Memory) to use"
  25.  
  26.     $fullRAM = -Join ($maxRAM, $ramDataType)
  27.     $basicInfo = @{
  28.         Jar_File = "$jarFile"
  29.         Minecraft_Version = "$mcVersion"
  30.         Java_Flags = "-Xmx$fullRAM"
  31.         Server_Options = "-o true"
  32.    
  33.     }
  34.  
  35.     New-Item $configFileName -ItemType "file" | Out-Null
  36.     $basicInfo | ConvertTo-Json | Add-Content $configFileName
  37.  
  38.     Write-Host ""
  39.     Write-Host "Moving to Main Menu..." -BackgroundColor Red
  40.     Sleep -Seconds $sleepTime
  41.     Main-Menu
  42. }
  43.  
  44.  
  45. function Main-Menu() {
  46.  
  47.     Clear-Host
  48.     Write-Host @"
  49. ---------------------------------- Main Menu ----------------------------------
  50.  
  51.                    1. Start Server and Ngrok
  52.                    2. Configurations Menu
  53.                        
  54.                    3. Exit the Console
  55.  
  56. -------------------------------------------------------------------------------
  57. "@
  58.  
  59.     $answer = Read-Host "Please Make a Selection"
  60.     switch($answer){
  61.         '1' {
  62.             Clear-Host
  63.             Start-Server
  64.         }
  65.         '2' {
  66.             Clear-Host
  67.             Settings-Menu
  68.         }
  69.         '3' {
  70.             Exit-Script
  71.         }
  72.         default {
  73.             Invaild-Choice
  74.             Main-Menu
  75.         }
  76.     }
  77. }
  78.  
  79. function Start-Server(){
  80.     $consoleServerPrefix = "["+$consoleName+"]:"
  81.     $ui.WindowTitle = "$prefix+: Server Running (Jar: , MC )"
  82.     Write-Host @"
  83. ------------------------------- SERVER LOG START -------------------------------
  84. $consoleServerPrefix Server Starting!
  85. "@
  86.     #-#-#-#-# Ngrok Checking here #-#-#-#-#
  87.     #Write-Host "$consoleServerPrefix Checking for Ngrok process..."
  88.  
  89.     ##Already Started
  90.     #Write-Host "$consoleServerPrefix Ngrok is Already Running! Skipping Ngrok Startup..."
  91.  
  92.     #Not Started
  93.     #Write-Host "$consoleServerPrefix Ngrok Not Running! Starting Ngrok..."
  94.     #start powershell {ngrok tcp 25565}
  95.     #Write-Host "$consoleServerPrefix Ngrok Started!"
  96.     #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
  97.  
  98.     Write-Host "$consoleServerPrefix Loading Java Arguments..."
  99.     #start-process java -ArgumentList '-Xms1G -Xmx1G -jar C:\Users\novag\Documents\Powershell\forge.jar' -Wait -NoNewWindow
  100.  
  101.     java $INFO.Java_Flags -jar $INFO.Jar_File $INFO.Server_Options
  102.  
  103.  
  104. }
  105.  
  106.  
  107. function Settings-Menu() {
  108.     Clear-Host
  109.     Write-Host @"
  110. -------------------------------- Settings Menu --------------------------------
  111.  
  112.    1. Change Version - Change the saved Minecraft version to match
  113.                        what the jar file uses
  114.    2. RAM Allocation - Set the amount of RAM (Random Access Memory)
  115.                        to reserve to the server [!]
  116.    3. Custom Arguments - Set custom arguments besides the RAM [!!]
  117.    4. Factory Reset - Erase all user defined settings and boot to
  118.                        first-time setup [!!!]
  119.  
  120.    5. Return To Main Menu
  121.  
  122. -------------------------------------------------------------------------------
  123. "@
  124.  
  125.     $answer = Read-Host "Please Make a Selection"
  126.     switch($answer){
  127.         '1' {
  128.             Clear-Host
  129.             Change-Minecraft-Version
  130.         }
  131.         '2' {
  132.             Clear-Host
  133.             Change-RAM
  134.         }
  135.         '3' {
  136.             Clear-Host
  137.             Confirm-Change-Args
  138.         }
  139.         '4' {
  140.             Clear-Host
  141.              Confirm-Factory-Reset
  142.         }
  143.         '5' {Return-Main-Menu}
  144.         default {
  145.             Invaild-Choice
  146.             Settings-Menu
  147.         }
  148.     }
  149. }
  150.  
  151. function Change-Minecraft-Version(){
  152.  
  153. }
  154.  
  155. function Change-RAM(){
  156.     #Write-Host "Please Enter Your Minimum Amount Of RAM To Use Followed By 'M' For Megabytes Or  'G' For Gigabytes"
  157.    
  158.  
  159. }
  160.  
  161. function Confirm-Change-Args(){
  162.  
  163. }
  164.  
  165. function Confirm-Factory-Reset(){
  166.     Write-Host -ForegroundColor Red "Are you SURE you want to reset back to factory default? This process CANNOT be reversed!!"
  167.     $answer = Read-Host "(Y)es or (N)o"
  168.     switch($answer.ToUpper()){
  169.  
  170.         'Y' {
  171.             Clear-Host
  172.             Perform-Factory-Reset
  173.         }
  174.         'N' {
  175.             Clear-Host
  176.             Return-Settings-Menu
  177.         }
  178.         default {
  179.             Invaild-Choice
  180.             Confirm-Factory-Reset
  181.         }
  182.     }
  183. }
  184.  
  185. function Perform-Factory-Reset(){
  186.     Write-Host "Deleting Preferences..." -BackgroundColor Red
  187.     Remove-Item $configFileName
  188.    
  189.     Write-Host "Factory Reset Completed!"
  190.     Write-Host ""
  191.     Write-Host "Returning to First-Time Setup..." -BackgroundColor Red
  192.     Sleep -Seconds $sleepTime
  193. }
  194.  
  195.  
  196. function Exit-Script(){
  197.     Write-Host ""
  198.     Write-Host "Exiting Script..." -BackgroundColor Red
  199.     Start-Sleep -Seconds $sleepTime
  200.     $quitConsole = $true
  201. }
  202.  
  203. function Return-Main-Menu(){
  204.     Write-Host ""
  205.     Write-Host "Returning to Main Menu..." -BackgroundColor Red
  206.     Sleep -Seconds $sleepTime
  207.     Main-Menu
  208. }
  209.  
  210. function Return-Settings-Menu(){
  211.     Write-Host ""
  212.     Write-Host "Returning to Settings Menu..." -BackgroundColor Red
  213.     Sleep -Seconds $sleepTime
  214.     Settings-Menu
  215. }
  216.  
  217. function Invaild-Choice(){
  218.     [Console]::Beep(1000, 100)
  219.     Write-Host "INVALID CHOICE! PLEASE ENTER ONE OF THE CHOICES LISTED!" -ForegroundColor Red
  220.     Sleep -Seconds $sleepTime
  221. }
  222.  
  223. function Check-Jar([string]$promptMessage){
  224.     Do{
  225.         [string]$data = Read-Host -Prompt $promptMessage
  226.         if ((-not $null -eq $data) -and (-not $data -eq '')){
  227.             return $data+'.jar'
  228.             $loop = $false
  229.         }else{
  230.             Write-Output ''
  231.             [Console]::Beep(1000, 100)
  232.             Write-Host "You cannot enter a null/empty name!" -ForegroundColor Red
  233.             Sleep -Seconds $sleepTime
  234.             $loop = $true
  235.         }      
  236.  
  237.     } Until($loop = $false)
  238. }
  239.  
  240. function Check-Version([string]$promptMessage){
  241.     Do{
  242.         $data = Read-Host -Prompt $promptMessage
  243.         $minVer = [version]'1.0.0'
  244.         $maxVer = [version]'999.999.999'
  245.  
  246.         if (($minVer -le $data) -and ($data -le $maxVer) -and ($data.Revision -lt 0)){
  247.             return $data
  248.             $loop = $false
  249.         }else{
  250.             [Console]::Beep(1000, 100)
  251.             Write-Host "The value [$data] MUST be a version (major, minor, build) ranging between $min to $max!" -ForegroundColor Red
  252.             $loop = $true
  253.         }
  254.     }Until($loop = $false)
  255. }
  256.  
  257. function Check-Integer([string]$promptMessage){
  258.     $data = ''
  259.     $min = 1
  260.     $max = [int]::MaxValue
  261.     While(($null -eq $data) -or ($data -eq '') -or ($data -eq '0') -or (-not ($data -notmatch '[^\d]'))){
  262.        
  263.         [int]$data = Read-Host -Prompt $promptMessage
  264.         if(($null -eq $data) -or ($data -eq '') -or ($data -eq '0')){
  265.             Write-Host ''
  266.             [Console]::Beep(1000, 100)
  267.             Write-Host "The value [$data] cannot be null, empty or 0!" -ForegroundColor Red
  268.             Sleep -Seconds $sleepTime
  269.         }elseif(-not ($data -notmatch '[^\d]')){
  270.             Write-Output ''
  271.             [Console]::Beep(1000, 100)
  272.             Write-Host "The value [$data] MUST be an integer!" -ForegroundColor Red
  273.             Sleep -Seconds $sleepTime
  274.         }else{
  275.             return $data
  276.         }
  277.    
  278.     }
  279. }
  280.  
  281. function Check-RAM-Type(){
  282.     $ramType = ''
  283.     $ValidChoices = ('M', 'G')
  284.     While ($ramType -notin $ValidChoices){
  285.         $ramType = (Read-Host -Prompt 'Would you like to use [M]egabytes or [G]igabytes?').ToUpper()
  286.         if ($ramType -notin $ValidChoices)
  287.         {
  288.             Invaild-Choice
  289.             $ramType = ''
  290.         }else{
  291.             return $ramType
  292.         }
  293.     }
  294. }
  295.  
  296. function Load-Variables(){
  297.     $INFO = Get-Content -Path $configFileName -Raw | ConvertFrom-Json
  298. }
  299.  
  300. #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#   BEGIN FUNCTION CALLS   #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
  301.  
  302. Do{
  303.  
  304.     Clear-Host
  305.     #-#-#-#-# Setup Checking here #-#-#-#-#
  306.     If (!(Test-Path $configFileName)) {
  307.         Setup-Console
  308.     }else{
  309.         #Load data from JSON
  310.         Main-Menu
  311.     }
  312.  
  313.    
  314.     #If there are no basic files
  315.  
  316.     #If there are basic files
  317.     #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
  318. } Until ($quitConsole = $true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement