Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.67 KB | None | 0 0
  1. //PvP Plugin created by Sirvoid.
  2. using System;
  3. using System.Collections.Generic;
  4. using MCGalaxy;
  5. using MCGalaxy.Events;
  6. using MCGalaxy.Events.PlayerEvents;
  7. using MCGalaxy.Drawing.Ops;
  8. using MCGalaxy.Maths;
  9.  
  10. namespace MCGalaxy
  11. {
  12. public class pvp : Plugin
  13. {
  14. public override string name { get { return "pvp"; } }
  15. public override string website { get { return "https://github.com/Sirvoid"; } }
  16. public override string MCGalaxy_Version { get { return "1.8.9.7"; } }
  17. public override int build { get { return 100; } }
  18. public override string welcome { get { return "PvP Loaded !"; } }
  19. public override string creator { get { return "Sirvoid"; } }
  20. public override bool LoadAtStartup { get { return true; } }
  21.  
  22. ///////////////////Setting///////////////////////
  23. string MaxHp = "20"; //Max players hp.
  24. string PvPaddcmd = "pvpadd"; //Command to add pvp to a map.
  25. string PvPdelcmd = "pvpdel"; //Command to remove pvp of a map.
  26. string PvPsuicmd = "suicide"; //Command to kill yourself.
  27. string PvPsethpcmd = "sethp"; //Command to set player's hp.
  28. string PvPaddsafezone = "addsafezone"; //Command to add a safezone.
  29. string PvPaddweapon = "addweapon"; //Command to add a weapon.
  30. string PvPaddweaponplayer = "giveweapon"; //Command to give a weapon to a player.
  31. string PvPresetweaponplayer = "resetweapon"; //Command to reset weapons of a player.
  32. LevelPermission PvPcmdRank = LevelPermission.Operator; //Min rank allowed for the commands pvpadd/pvpdel/sethp.
  33. bool PvPEconomy = true; //Enable(true) or Disable(false) rewards when killing someone.
  34. int moneystealed = 1; //money stealed when you kill someone.
  35. /////////////////////////////////////////////////
  36.  
  37. int curpid = -1;
  38. List<string> maplist = new List<string>();
  39. string[,] playersinpvp = new string[100, 3];
  40. string[,] weapons = new string[255, 3];
  41.  
  42. public override void Load(bool startup){
  43. Chat.MessageAll("%aPvP plugin loaded ! For more informations , do /help pvp");
  44. loadfromfile();
  45. loadWeapons();
  46. OnPlayerCommandEvent.Register(HandleCommand, Priority.Low);
  47. OnPlayerClickEvent.Register(HandleClick, Priority.Low);
  48. OnJoinedLevelEvent.Register(HandleOnJoinedLevel, Priority.Low);
  49.  
  50. Player[] online = PlayerInfo.Online.Items;
  51. foreach (Player p in online){
  52. if (maplist.Contains(p.level.name)){
  53. for(int i = 0;i<100;i++){
  54. if(playersinpvp[i,0] == null){
  55. playersinpvp[i,0] = p.name;
  56. playersinpvp[i,1] = MaxHp;
  57. playersinpvp[i,2] = "60000";
  58. break;
  59. }
  60. }
  61. p.SendCpeMessage(CpeMessageType.Status1, "%4♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥");
  62. }
  63. }
  64. }
  65.  
  66. public override void Unload(bool shutdown){
  67. Chat.MessageAll("%aPvP plugin unloaded !");
  68. OnPlayerCommandEvent.Unregister(HandleCommand);
  69. OnPlayerClickEvent.Unregister(HandleClick);
  70. OnJoinedLevelEvent.Unregister(HandleOnJoinedLevel);
  71. }
  72.  
  73. void SetHpIndicator(int i,Player pl){
  74. int a = int.Parse(playersinpvp[i,1]);
  75.  
  76. string hpstring = "";
  77. for (int h = 0;h < a;h++){
  78. hpstring = hpstring + "♥";
  79. }
  80.  
  81. pl.SendCpeMessage(CpeMessageType.Status1, "%4"+hpstring);
  82. }
  83.  
  84. void savetofile(){
  85. using (System.IO.StreamWriter maplistwriter =
  86. new System.IO.StreamWriter("./text/pvpmaps.txt")){
  87.  
  88. foreach (String s in maplist){
  89. maplistwriter.WriteLine(s);
  90. }
  91.  
  92. }
  93. }
  94.  
  95. void loadfromfile(){
  96.  
  97. if(System.IO.File.Exists("./text/pvpmaps.txt")){
  98. using (var maplistreader = new System.IO.StreamReader("./text/pvpmaps.txt"))
  99. {
  100. string line;
  101. while ((line = maplistreader.ReadLine()) != null)
  102. {
  103. maplist.Add(line);
  104. }
  105. }
  106. }
  107. }
  108.  
  109.  
  110. void HandleCommand(Player p, string cmd, string args) {
  111. string[] multArgs = args.Split(' ');
  112. //PvP add command
  113. if (cmd == PvPaddcmd){
  114. p.cancelcommand = true;
  115. if (p.Rank >= PvPcmdRank){
  116. if(args != ""){
  117. maplist.Add(args);
  118. Player.Message(p, "The map " + args + " has been added to the PvP map list.");
  119.  
  120. savetofile();
  121.  
  122. Player[] online = PlayerInfo.Online.Items;
  123. foreach (Player pl in online){
  124. if (pl.level.name == args){
  125. for(int i = 0;i<100;i++){
  126. if(playersinpvp[i,0] == null){
  127. playersinpvp[i,0] = pl.name;
  128. playersinpvp[i,1] = MaxHp;
  129. playersinpvp[i,2] = "60000";
  130. break;
  131. }
  132. }
  133. pl.SendCpeMessage(CpeMessageType.Status1, "%4♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥");
  134. }
  135. }
  136.  
  137. }else{
  138. Player.Message(p, "Correct usage: /" + PvPaddcmd + " [map]");
  139. }
  140. }
  141. else{
  142. Player.Message(p, "You are not allowed to use this command !");
  143. }
  144. //PvP del command
  145. } else if (cmd == PvPdelcmd){
  146. p.cancelcommand = true;
  147. if (p.Rank >= PvPcmdRank){
  148. if(args != ""){
  149. maplist.Remove(args);
  150. Player.Message(p, "The map " + args + " has been removed to the PvP map list.");
  151. }else{
  152. Player.Message(p, "Correct usage: /" + PvPdelcmd + " [map]");
  153. }
  154. }
  155. else{
  156. Player.Message(p, "You are not allowed to use this command !");
  157. }
  158. //PvP set hp command
  159. } else if (cmd == PvPsethpcmd){
  160. p.cancelcommand = true;
  161. if (p.Rank >= PvPcmdRank){
  162. if(args != ""){
  163. curpid = -1;
  164. for (int yi = 0;yi<100;yi++){
  165. if(playersinpvp[yi,0] == multArgs[0] + "+"){
  166. curpid = yi;
  167.  
  168. }
  169. }
  170.  
  171. playersinpvp[curpid,1] = multArgs[1];
  172. Player[] online = PlayerInfo.Online.Items;
  173.  
  174. foreach (Player pl in online){
  175. if(pl.truename == multArgs[0]){
  176. SetHpIndicator(curpid,pl);
  177. }
  178. }
  179.  
  180. }else{
  181. Player.Message(p, "Correct usage: /" + PvPsethpcmd + " [Player] [HP]");
  182. }
  183. }else{
  184. Player.Message(p, "You are not allowed to use this command !");
  185. }
  186. //Suicide command
  187. } else if (cmd == PvPsuicmd){
  188. if (maplist.Contains(p.level.name)){
  189. p.cancelcommand = true;
  190. curpid = -1;
  191. for (int yi = 0;yi<100;yi++){
  192. if(playersinpvp[yi,0] == p.name){
  193. curpid = yi;
  194.  
  195. }
  196. }
  197. playersinpvp[curpid,1] = MaxHp;
  198. p.SendPos(Entities.SelfID, new Position(16+(p.level.spawnx*32),32+(p.level.spawny*32),16+(p.level.spawnz*32)), p.Rot);
  199. SetHpIndicator(curpid,p);
  200. Chat.MessageLevel(p.level, p.ColoredName + " %Scommitted suicide.");
  201. }
  202. //add safezone command
  203. } else if (cmd == PvPaddsafezone){
  204.  
  205. p.cancelcommand = true;
  206. if (p.Rank >= PvPcmdRank){
  207. Player.Message(p, "Place or break two blocks to determine the edges.");
  208. p.MakeSelection(2, null, DoSafeZone);
  209. }else{
  210. Player.Message(p, "You are not allowed to use this command !");
  211. }
  212. //add weapon command
  213. } else if (cmd == PvPaddweapon){
  214.  
  215.  
  216. p.cancelcommand = true;
  217. if (p.Rank >= PvPcmdRank){
  218. if(args != ""){
  219. for(int i = 0;i<255;i++){
  220. if(weapons[i,0] == null){
  221. weapons[i,0] = multArgs[0];
  222. weapons[i,1] = multArgs[1];
  223. weapons[i,2] = multArgs[2];
  224. AddWeaponToFile(multArgs[0],multArgs[1],multArgs[2]);
  225. break;
  226. }
  227. }
  228. } else {
  229. Player.Message(p, "Correct usage: /" + PvPaddweapon + " [BlockId] [Damage] [Durability]");
  230. }
  231. }else{
  232. Player.Message(p, "You are not allowed to use this command !");
  233. }
  234.  
  235. } else if (cmd == PvPaddweaponplayer) {
  236. p.cancelcommand = true;
  237. if (p.Rank >= PvPcmdRank){
  238. if(args != ""){
  239. Player[] online = PlayerInfo.Online.Items;
  240. foreach (Player pl in online){
  241. if(pl.truename == multArgs[0]){
  242. AddWeaponToPlayer(multArgs[1],pl,multArgs[2]);
  243. }
  244. }
  245.  
  246. } else {
  247. Player.Message(p, "Correct usage: /" + PvPaddweaponplayer + " [Player] [World] [WeaponName]");
  248. }
  249. }else{
  250. Player.Message(p, "You are not allowed to use this command !");
  251. }
  252. } else if (cmd == PvPresetweaponplayer) {
  253. p.cancelcommand = true;
  254. if (p.Rank >= PvPcmdRank){
  255. if(args != ""){
  256. Player[] online = PlayerInfo.Online.Items;
  257. foreach (Player pl in online){
  258. if(pl.truename == multArgs[0]){
  259. ResetPlayerWeapon(multArgs[1],pl);
  260. }
  261. }
  262.  
  263. } else {
  264. Player.Message(p, "Correct usage: /" + PvPresetweaponplayer + " [Player] [World]");
  265. }
  266. }else{
  267. Player.Message(p, "You are not allowed to use this command !");
  268. }
  269. }
  270. else {return;}
  271. }
  272.  
  273. void AddWeaponToPlayer(string world,Player p,string item){
  274. string filepath = "./text/playersweapons/" + world + "/" + p.truename + ".txt";
  275. System.IO.FileInfo filedir = new System.IO.FileInfo(filepath);
  276. filedir.Directory.Create();
  277. using (System.IO.StreamWriter file =
  278. new System.IO.StreamWriter(filepath, true))
  279. {
  280. file.WriteLine(item);
  281. }
  282. }
  283.  
  284. void ResetPlayerWeapon(string world,Player p){
  285. string filepath = "./text/playersweapons/" + world + "/" + p.truename + ".txt";
  286. if(System.IO.File.Exists(filepath)){
  287. System.IO.File.WriteAllText(filepath,string.Empty);
  288. }
  289. }
  290.  
  291. bool CheckPlayerWeapon(string world,Player p,string item){
  292. string filepath = "./text/playersweapons/" + world + "/" + p.truename + ".txt";
  293. if(System.IO.File.Exists(filepath)){
  294. using (var r = new System.IO.StreamReader(filepath)){
  295. string line;
  296. while ((line = r.ReadLine()) != null){
  297. if(line == item){return true;}
  298. }
  299. }
  300. }
  301. return false;
  302. }
  303.  
  304. string GetWeaponStats(string item){
  305. for(int i = 0;i<255;i++){
  306. if(weapons[i,0] == item){
  307. return weapons[i,0] + " " + weapons[i,1] + " " + weapons[i,2];
  308. }
  309. }
  310. return "0 1 0";
  311. }
  312.  
  313. void AddWeaponToFile(string id,string damage,string durability) {
  314.  
  315. System.IO.FileInfo filedir = new System.IO.FileInfo("./text/weapons.txt");
  316. filedir.Directory.Create();
  317.  
  318. using (System.IO.StreamWriter file =
  319. new System.IO.StreamWriter("./text/weapons.txt", true))
  320. {
  321.  
  322. file.WriteLine(id + ";" + damage + ";" + durability);
  323. }
  324. }
  325.  
  326. void loadWeapons(){
  327.  
  328. if(System.IO.File.Exists("./text/weapons.txt")){
  329. using (var r = new System.IO.StreamReader("./text/weapons.txt")){
  330. string line;
  331. while ((line = r.ReadLine()) != null){
  332. string[] weaponstats = line.Split(';');
  333. for(int i = 0;i<255;i++){
  334. if(weapons[i,0] == null){
  335. weapons[i,0] = weaponstats[0];
  336. weapons[i,1] = weaponstats[1];
  337. weapons[i,2] = weaponstats[2];
  338. break;
  339. }
  340. }
  341.  
  342. }
  343. }
  344. }
  345.  
  346.  
  347.  
  348. }
  349.  
  350. bool DoSafeZone(Player p, Vec3S32[] marks, object state, ExtBlock block) {
  351.  
  352. System.IO.FileInfo filedir = new System.IO.FileInfo("./text/safezones/" + p.level.name + ".txt");
  353. filedir.Directory.Create();
  354.  
  355. using (System.IO.StreamWriter file =
  356. new System.IO.StreamWriter("./text/safezones/" + p.level.name + ".txt", true))
  357. {
  358.  
  359. file.WriteLine(marks.GetValue(0) + ";" + marks.GetValue(1));
  360. }
  361.  
  362. Player.Message(p, "SafeZone added.");
  363. return true;
  364. }
  365.  
  366. bool InSafeZone(Player p,string map){
  367.  
  368. if(System.IO.File.Exists("./text/safezones/" + map + ".txt")){
  369. using (var r = new System.IO.StreamReader("./text/safezones/" + map + ".txt")){
  370. string line;
  371. while ((line = r.ReadLine()) != null){
  372. string[] temp = line.Split(';');
  373. string[] coord1 = temp[0].Split(',');
  374. string[] coord2 = temp[1].Split(',');
  375.  
  376. if((p.Pos.BlockX <= int.Parse(coord1[0]) && p.Pos.BlockX >= int.Parse(coord2[0])) || (p.Pos.BlockX >= int.Parse(coord1[0]) && p.Pos.BlockX <= int.Parse(coord2[0]))){
  377. if((p.Pos.BlockZ <= int.Parse(coord1[2]) && p.Pos.BlockZ >= int.Parse(coord2[2])) || (p.Pos.BlockZ >= int.Parse(coord1[2]) && p.Pos.BlockZ <= int.Parse(coord2[2]))){
  378. if((p.Pos.BlockY <= int.Parse(coord1[1]) && p.Pos.BlockY >= int.Parse(coord2[1])) || (p.Pos.BlockY >= int.Parse(coord1[1]) && p.Pos.BlockY <= int.Parse(coord2[1]))){
  379. return true;
  380. }
  381. }
  382.  
  383. }
  384. }
  385. return false;
  386. }
  387. return false;
  388. }
  389. return false;
  390. }
  391.  
  392.  
  393. void HandleClick(Player p, MouseButton button, MouseAction action, ushort yaw, ushort pitch, byte entity, ushort x, ushort y, ushort z, TargetBlockFace face){
  394. if (button == MouseButton.Left){
  395. if (maplist.Contains(p.level.name)){
  396. curpid = -1;
  397. for(int yi = 0;yi<100;yi++){
  398. if(playersinpvp[yi,0] == p.name){
  399. curpid = yi;
  400. }
  401. }
  402. if(int.Parse(DateTime.Now.Second + "" + DateTime.Now.Millisecond) - int.Parse(playersinpvp[curpid,2])>300 || int.Parse(DateTime.Now.Second+""+DateTime.Now.Millisecond) - int.Parse(playersinpvp[curpid,2])<-300){
  403. Player[] online = PlayerInfo.Online.Items;
  404. foreach (Player pl in online){
  405. if (pl.EntityID == entity){
  406. for(int i = 0;i<100;i++){
  407. if(playersinpvp[i,0] == pl.name){
  408. double dist = (Math.Sqrt(Math.Pow(Math.Abs(p.Pos.X - pl.Pos.X), 2) + Math.Pow(Math.Abs(p.Pos.Y - pl.Pos.Y), 2) + Math.Pow(Math.Abs(p.Pos.Z - pl.Pos.Z), 2)));
  409. if (dist < 150){
  410.  
  411. if(!InSafeZone(p,p.level.name) && !InSafeZone(pl,pl.level.name)){
  412.  
  413. int a = int.Parse(playersinpvp[i,1]);
  414.  
  415. ExtBlock b = p.GetHeldBlock();
  416. string[] weaponstats = GetWeaponStats(b.ExtID + "").Split(' ');
  417. //Player.Message(p, "dmg: " + weaponstats[1] + " id: " + b.ExtID);
  418.  
  419. if(CheckPlayerWeapon(p.level.name,p,p.level.BlockName(b)) || weaponstats[0] == "0"){
  420. playersinpvp[i,1] = (a - int.Parse(weaponstats[1])) + "";
  421.  
  422. Position new_pos;
  423. new_pos.X = pl.Pos.X +(int)((pl.Pos.X-p.Pos.X)/2);
  424. new_pos.Y = pl.Pos.Y;
  425. new_pos.Z = pl.Pos.Z +(int)((pl.Pos.Z-p.Pos.Z)/2);
  426.  
  427. Position new_midpos;
  428. new_midpos.X = pl.Pos.X +(int)((pl.Pos.X-p.Pos.X)/4);
  429. new_midpos.Y = pl.Pos.Y;
  430. new_midpos.Z = pl.Pos.Z +(int)((pl.Pos.Z-p.Pos.Z)/4);
  431.  
  432. if (pl.level.IsAirAt((ushort)new_pos.BlockX, (ushort)new_pos.BlockY, (ushort)new_pos.BlockZ) && pl.level.IsAirAt((ushort)new_pos.BlockX, (ushort)(new_pos.BlockY-1), (ushort)new_pos.BlockZ) &&
  433. pl.level.IsAirAt((ushort)new_midpos.BlockX, (ushort)new_midpos.BlockY, (ushort)new_midpos.BlockZ) && pl.level.IsAirAt((ushort)new_midpos.BlockX, (ushort)(new_midpos.BlockY-1), (ushort)new_midpos.BlockZ)){
  434. pl.SendPos(Entities.SelfID, new Position(new_pos.X, new_pos.Y, new_pos.Z), pl.Rot);
  435. }
  436.  
  437. SetHpIndicator(i,pl);
  438.  
  439. if (a <= 1){
  440.  
  441. string stringweaponused = weaponstats[0] == "0" ? "." : " using " + p.level.BlockName(b) + ".";
  442. Chat.MessageLevel(pl.level, pl.ColoredName + " %Swas slain by " + p.ColoredName + stringweaponused);
  443.  
  444. playersinpvp[i,1] = MaxHp;
  445. pl.SendPos(Entities.SelfID, new Position(16+(p.level.spawnx*32),32+(p.level.spawny*32),16+(p.level.spawnz*32)), pl.Rot);
  446.  
  447. pl.SendCpeMessage(CpeMessageType.Status1, "%4♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥");
  448.  
  449. if(PvPEconomy == true && (p.ip != pl.ip || p.ip == "127.0.0.1")){
  450. if(pl.money > 0){
  451. Player.Message(p, "You stole " + moneystealed + " " + ServerConfig.Currency + " %Sfrom " + pl.ColoredName + "%S.");
  452. Player.Message(pl, p.ColoredName + " %Sstole " + moneystealed + " " + ServerConfig.Currency + " from you.");
  453. p.SetMoney(p.money+moneystealed);
  454. pl.SetMoney(pl.money-moneystealed);
  455.  
  456. MCGalaxy.Games.BountyData bounty= Server.zombie.FindBounty(pl.name);
  457. if (bounty != null){
  458. Server.zombie.Bounties.Remove(bounty);
  459.  
  460. Player setter = PlayerInfo.FindExact(bounty.Origin);
  461.  
  462. if (setter == null) {
  463. Player.Message(p, "Cannot collect the bounty, as the player who set it is offline.");
  464. } else {
  465. p.level.ChatLevel("&c" + p.DisplayName + " %Scollected the bounty of &a" +
  466. bounty.Amount + " %S" + ServerConfig.Currency + " on " + pl.ColoredName + "%S.");
  467. p.SetMoney(p.money + bounty.Amount);
  468. }
  469. }
  470. }
  471. }
  472. }
  473. } else {Player.Message(p, "You don't own this weapon.");}
  474. }
  475. else{
  476. Player.Message(p, "You can't hurt people in a safe zone.");
  477. }
  478. }
  479. playersinpvp[curpid,2] = DateTime.Now.Second + "" + DateTime.Now.Millisecond + "";
  480. }
  481.  
  482. }
  483. }
  484. }
  485. }
  486. }
  487. }
  488. }
  489.  
  490.  
  491.  
  492. void HandleOnJoinedLevel(Player p, Level prevLevel, Level level){
  493.  
  494.  
  495.  
  496. if (maplist.Contains(level.name)){
  497.  
  498. p.SendCpeMessage(CpeMessageType.Status1, "%4♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥");
  499.  
  500. for(int i =0;i<100;i++){
  501. if(playersinpvp[i,0] == p.name) return;
  502. }
  503.  
  504. for(int i = 0;i<100;i++){
  505.  
  506. if(playersinpvp[i,0] == null){
  507. playersinpvp[i,0] = p.name;
  508. playersinpvp[i,1] = MaxHp;
  509. playersinpvp[i,2] = "60000";
  510. return;
  511. }
  512.  
  513. }
  514.  
  515. }
  516.  
  517. if (maplist.Contains(prevLevel.name) && !maplist.Contains(level.name)){
  518.  
  519. p.SendCpeMessage(CpeMessageType.Status1, "");
  520. }
  521. }
  522.  
  523. public override void Help(Player p){
  524. Player.Message(p,"%7-----PvP Plugin by Sirvoid-----");
  525. Player.Message(p,"%7/" + PvPaddcmd + " -To add PvP to a map.");
  526. Player.Message(p,"%7/" + PvPdelcmd + " -To remove PvP of a map.");
  527. Player.Message(p,"%7/" + PvPsethpcmd + " -To set the HP of a player.");
  528. Player.Message(p,"%7/" + PvPaddsafezone + " -To add a safezone.");
  529. Player.Message(p,"%7/" + PvPaddweapon + " -To add a weapon.");
  530. Player.Message(p,"%7/" + PvPaddweaponplayer + " -To give a weapon to a player.");
  531. Player.Message(p,"%7/" + PvPresetweaponplayer + " -To reset weapons of a player.");
  532. Player.Message(p,"%7/" + PvPsuicmd + " -To kill yourself.");
  533. Player.Message(p,"%7---------------------------");
  534. }
  535. }
  536. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement