Advertisement
EESweetieBot

[EE] BlockHandler

Jul 15th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.98 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using PlayerIOClient;
  5. using Yonom.EE;
  6.  
  7. // ey look, a cod -Melody
  8.  
  9. namespace Melody.BlockHandler
  10. {
  11.     public class BlockWorld
  12.     {
  13.         public int[,,] blockArray = new int[2,0,0];
  14.         public List<Block> blockList = new List<Block>();
  15.         private static int worldX = 25, worldY = 25;
  16.         public class Block {
  17.             public int x { get; set; }
  18.             public int y { get; set; }
  19.             public int layer { get; set; }
  20.             public int id { get; set; }
  21.             public int placer { get; set; } // Warning, will send -1 if unknown placer.
  22.             public string type { get; set; } // Warning, wil send "null" if init, clear or reset
  23.             public object[] args { get; set; }
  24.         }
  25.         //
  26.         public Block GetBlock(int x, int y, int layer) {
  27.             foreach(Block b in blockList)
  28.                 if(b.x == x && b.y == y && b.layer == layer)
  29.                     return b;
  30.             return null;
  31.         }
  32.         public List<Block> GetBlocksById(int id) {
  33.             List<Block> a = new List<Block>();
  34.             foreach(Block b in blockList)
  35.                 if(b.id == id)
  36.                     a.Add(b);
  37.             return a;
  38.         }
  39.         public List<Block> GetBlocksByLayer(int layer) {
  40.             List<Block> a = new List<Block>();
  41.             foreach(Block b in blockList)
  42.                 if(b.layer == layer)
  43.                     a.Add(b);
  44.             return a;
  45.         }
  46.         public List<Block> GetBlocksByX(int x) {
  47.             List<Block> a = new List<Block>();
  48.             foreach(Block b in blockList)
  49.                 if(b.x == x)
  50.                     a.Add(b);
  51.             return a;
  52.         }
  53.         public List<Block> GetBlocksByY(int y) {
  54.             List<Block> a = new List<Block>();
  55.             foreach(Block b in blockList)
  56.                 if(b.y == y)
  57.                     a.Add(b);
  58.             return a;
  59.         }
  60.         public List<Block> GetBlocksByRegion(int startx, int endx, int starty, int endy, int layer = 0) {
  61.             List<Block> a = new List<Block>();
  62.             foreach(Block b in blockList)
  63.                 if((b.x >= startx && b.x <= endx) && (b.y >= starty && b.y <= endy) && b.layer == layer)
  64.                     a.Add(b);
  65.             return a;
  66.         }
  67.         public List<Block> GetBlocksByPlacer(int placerID) {
  68.             List<Block> a = new List<Block>();
  69.             foreach(Block b in blockList)
  70.                 if(b.placer == placerID)
  71.                     a.Add(b);
  72.             return a;
  73.         }
  74.         public void Reset() {
  75.             blockArray = new int[2,worldX,worldY];
  76.             blockList = new List<Block>();
  77.         }
  78.         public void HandleMessage(Message e) {
  79.             switch(e.Type) {
  80.                     case "init": {
  81.                         worldX = e.GetInt(18);
  82.                         worldY = e.GetInt(19);
  83.                         var worldD = new int[2,worldX,worldY];
  84.                         var chunks = InitParse.Parse(e);
  85.                         foreach(DataChunk chunk in chunks) {
  86.                             foreach(Point pos in chunk.Locations) {
  87.                                 worldD[chunk.Layer, pos.X, pos.Y] = (int) chunk.Type;
  88.                                 Block t = new Block();
  89.                                 t.x = pos.X; t.y = pos.Y; t.layer = chunk.Layer; t.args = chunk.Args; t.placer = -1; t.type = "null";
  90.                                 t.id = (int) chunk.Type;
  91.                                 blockList.Add(t);
  92.                             }
  93.                         }
  94.                         blockArray = worldD;
  95.                     }
  96.                     break;
  97.                     case "clear": {
  98.                         var worldD = new int[2,worldX,worldY];
  99.                         var worldL = new List<Block>();
  100.                         var chunks = InitParse.HandleClear(e, worldX, worldY);
  101.                         foreach(DataChunk chunk in chunks) {
  102.                             foreach(Point pos in chunk.Locations) {
  103.                                 worldD[chunk.Layer, pos.X, pos.Y] = (int) chunk.Type;
  104.                                 worldL.Add(new Block() {
  105.                                             x = pos.X,
  106.                                             y = pos.Y,
  107.                                             layer = chunk.Layer,
  108.                                             id = (int) chunk.Type,
  109.                                             args = chunk.Args,
  110.                                             type = "null",
  111.                                             placer = -1
  112.                                            });
  113.                             }
  114.                         }
  115.                         blockList = worldL;
  116.                         blockArray = worldD;
  117.                     }
  118.                     break;
  119.                     case "reset": {
  120.                         var worldD = new int[2,worldX,worldY];
  121.                         var blockL = new List<Block>();
  122.                         var chunks = InitParse.Parse(e);
  123.                         foreach(DataChunk chunk in chunks) {
  124.                             foreach(Point pos in chunk.Locations) {
  125.                                 worldD[chunk.Layer, pos.X, pos.Y] = (int) chunk.Type;
  126.                                 blockL.Add(new Block() {
  127.                                             x = pos.X,
  128.                                             y = pos.Y,
  129.                                             layer = chunk.Layer,
  130.                                             id = (int) chunk.Type,
  131.                                             args = chunk.Args,
  132.                                             placer = -1,
  133.                                             type = "null"
  134.                                            });
  135.                             }
  136.                         }
  137.                         blockArray = worldD;
  138.                         blockList = blockL;
  139.                     }
  140.                     break;
  141.                     case "b":
  142.                     case "bc":
  143.                     case "br":
  144.                     case "bs":
  145.                     case "lb":
  146.                     case "pt":
  147.                     case "ts":
  148.                     case "wp": {
  149.                         int layer = 0, x = 0, y = 0, id = 0, u = -1;
  150.                         string type = e.Type;
  151.                         object[] args = {};
  152.                         #region Type Adding
  153.                         if(e.Type == "b") {
  154.                             layer = e.GetInt(0);
  155.                             x = e.GetInt(1U);
  156.                             y = e.GetInt(2U);
  157.                             id = e.GetInt(3U);
  158.                             u = e.GetInt(4U);
  159.                         }
  160.                         else if(e.Type == "bc") {
  161.                             layer = 0;
  162.                             x = e.GetInt(0U);
  163.                             y = e.GetInt(1U);
  164.                             id = e.GetInt(2U);
  165.                             args = new object[] {e.GetUInt(3)};
  166.                             u = e.GetInt(4U);
  167.                         }
  168.                         else if(e.Type == "br") {
  169.                             layer = e.GetInt(4U);
  170.                             x = e.GetInt(0U);
  171.                             y = e.GetInt(1U);
  172.                             id = e.GetInt(2U);
  173.                             args = new object[] {e.GetUInt(3)};
  174.                             u = e.GetInt(5U);
  175.                         }
  176.                         else if(e.Type == "bs") {
  177.                             layer = 0;
  178.                             x = e.GetInt(0U);
  179.                             y = e.GetInt(1U);
  180.                             id = e.GetInt(2U);
  181.                             args = new object[] {e.GetInt(3)};
  182.                             u = e.GetInt(4U);
  183.                         }
  184.                         else if(e.Type == "lb") {
  185.                             layer = 0;
  186.                             x = e.GetInt(0U);
  187.                             y = e.GetInt(1U);
  188.                             id = e.GetInt(2);
  189.                             args = new object[] {e.GetString(3), e.GetString(4)};
  190.                             u = e.GetInt(5U);
  191.                         }
  192.                         else if(e.Type == "pt") {
  193.                             layer = 0;
  194.                             x = e.GetInt(0U);
  195.                             y = e.GetInt(1U);
  196.                             id = e.GetInt(2U);
  197.                             args = new object[] {e.GetUInt(3), e.GetUInt(4), e.GetUInt(5)};
  198.                             u = e.GetInt(6);
  199.                         }
  200.                         else if(e.Type == "ts") {
  201.                             layer = 0;
  202.                             x = e.GetInt(0U);
  203.                             y = e.GetInt(1U);
  204.                             id = e.GetInt(2U);
  205.                             args = new object[] {e.GetString(3), e.GetUInt(4)};
  206.                             u = e.GetInt(5U);
  207.                         }
  208.                         else if(e.Type == "wp") {
  209.                             layer = 0;
  210.                             x = e.GetInt(0U);
  211.                             y = e.GetInt(1U);
  212.                             id = e.GetInt(2U);
  213.                             args = new object[] {e.GetString(3)};
  214.                             u = e.GetInt(4U);
  215.                         }
  216.                         #endregion
  217.                         #region Add to list
  218.                         blockArray[layer, x, y] = id;
  219.                         List<Block> temp = new List<Block>();
  220.                         temp.AddRange(blockList);
  221.                         if(id == 0) {
  222.                             foreach(Block b in temp)
  223.                                 if(b.x == x && b.y == y && b.layer == layer)
  224.                                     blockList.Remove(b);
  225.                         } else {
  226.                             foreach(Block b in temp) {
  227.                                 if(b.x == x && b.y == y && b.layer == layer && b.id != id) {
  228.                                     blockList.Remove(b);
  229.                                     blockList.Add(new Block() {
  230.                                                     x = x,
  231.                                                     y = y,
  232.                                                     id = id,
  233.                                                     layer = layer,
  234.                                                     args = args,
  235.                                                     placer = u,
  236.                                                     type = e.Type
  237.                                                   });
  238.                                 }
  239.                             }
  240.                         }
  241.                         #endregion
  242.                     }
  243.                     break;
  244.             }
  245.         }
  246.     }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement