Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using PlayerIOClient;
- using Yonom.EE;
- // ey look, a cod -Melody
- namespace Melody.BlockHandler
- {
- public class BlockWorld
- {
- public int[,,] blockArray = new int[2,0,0];
- public List<Block> blockList = new List<Block>();
- private static int worldX = 25, worldY = 25;
- public class Block {
- public int x { get; set; }
- public int y { get; set; }
- public int layer { get; set; }
- public int id { get; set; }
- public int placer { get; set; } // Warning, will send -1 if unknown placer.
- public string type { get; set; } // Warning, wil send "null" if init, clear or reset
- public object[] args { get; set; }
- }
- //
- public Block GetBlock(int x, int y, int layer) {
- foreach(Block b in blockList)
- if(b.x == x && b.y == y && b.layer == layer)
- return b;
- return null;
- }
- public List<Block> GetBlocksById(int id) {
- List<Block> a = new List<Block>();
- foreach(Block b in blockList)
- if(b.id == id)
- a.Add(b);
- return a;
- }
- public List<Block> GetBlocksByLayer(int layer) {
- List<Block> a = new List<Block>();
- foreach(Block b in blockList)
- if(b.layer == layer)
- a.Add(b);
- return a;
- }
- public List<Block> GetBlocksByX(int x) {
- List<Block> a = new List<Block>();
- foreach(Block b in blockList)
- if(b.x == x)
- a.Add(b);
- return a;
- }
- public List<Block> GetBlocksByY(int y) {
- List<Block> a = new List<Block>();
- foreach(Block b in blockList)
- if(b.y == y)
- a.Add(b);
- return a;
- }
- public List<Block> GetBlocksByRegion(int startx, int endx, int starty, int endy, int layer = 0) {
- List<Block> a = new List<Block>();
- foreach(Block b in blockList)
- if((b.x >= startx && b.x <= endx) && (b.y >= starty && b.y <= endy) && b.layer == layer)
- a.Add(b);
- return a;
- }
- public List<Block> GetBlocksByPlacer(int placerID) {
- List<Block> a = new List<Block>();
- foreach(Block b in blockList)
- if(b.placer == placerID)
- a.Add(b);
- return a;
- }
- public void Reset() {
- blockArray = new int[2,worldX,worldY];
- blockList = new List<Block>();
- }
- public void HandleMessage(Message e) {
- switch(e.Type) {
- case "init": {
- worldX = e.GetInt(18);
- worldY = e.GetInt(19);
- var worldD = new int[2,worldX,worldY];
- var chunks = InitParse.Parse(e);
- foreach(DataChunk chunk in chunks) {
- foreach(Point pos in chunk.Locations) {
- worldD[chunk.Layer, pos.X, pos.Y] = (int) chunk.Type;
- Block t = new Block();
- t.x = pos.X; t.y = pos.Y; t.layer = chunk.Layer; t.args = chunk.Args; t.placer = -1; t.type = "null";
- t.id = (int) chunk.Type;
- blockList.Add(t);
- }
- }
- blockArray = worldD;
- }
- break;
- case "clear": {
- var worldD = new int[2,worldX,worldY];
- var worldL = new List<Block>();
- var chunks = InitParse.HandleClear(e, worldX, worldY);
- foreach(DataChunk chunk in chunks) {
- foreach(Point pos in chunk.Locations) {
- worldD[chunk.Layer, pos.X, pos.Y] = (int) chunk.Type;
- worldL.Add(new Block() {
- x = pos.X,
- y = pos.Y,
- layer = chunk.Layer,
- id = (int) chunk.Type,
- args = chunk.Args,
- type = "null",
- placer = -1
- });
- }
- }
- blockList = worldL;
- blockArray = worldD;
- }
- break;
- case "reset": {
- var worldD = new int[2,worldX,worldY];
- var blockL = new List<Block>();
- var chunks = InitParse.Parse(e);
- foreach(DataChunk chunk in chunks) {
- foreach(Point pos in chunk.Locations) {
- worldD[chunk.Layer, pos.X, pos.Y] = (int) chunk.Type;
- blockL.Add(new Block() {
- x = pos.X,
- y = pos.Y,
- layer = chunk.Layer,
- id = (int) chunk.Type,
- args = chunk.Args,
- placer = -1,
- type = "null"
- });
- }
- }
- blockArray = worldD;
- blockList = blockL;
- }
- break;
- case "b":
- case "bc":
- case "br":
- case "bs":
- case "lb":
- case "pt":
- case "ts":
- case "wp": {
- int layer = 0, x = 0, y = 0, id = 0, u = -1;
- string type = e.Type;
- object[] args = {};
- #region Type Adding
- if(e.Type == "b") {
- layer = e.GetInt(0);
- x = e.GetInt(1U);
- y = e.GetInt(2U);
- id = e.GetInt(3U);
- u = e.GetInt(4U);
- }
- else if(e.Type == "bc") {
- layer = 0;
- x = e.GetInt(0U);
- y = e.GetInt(1U);
- id = e.GetInt(2U);
- args = new object[] {e.GetUInt(3)};
- u = e.GetInt(4U);
- }
- else if(e.Type == "br") {
- layer = e.GetInt(4U);
- x = e.GetInt(0U);
- y = e.GetInt(1U);
- id = e.GetInt(2U);
- args = new object[] {e.GetUInt(3)};
- u = e.GetInt(5U);
- }
- else if(e.Type == "bs") {
- layer = 0;
- x = e.GetInt(0U);
- y = e.GetInt(1U);
- id = e.GetInt(2U);
- args = new object[] {e.GetInt(3)};
- u = e.GetInt(4U);
- }
- else if(e.Type == "lb") {
- layer = 0;
- x = e.GetInt(0U);
- y = e.GetInt(1U);
- id = e.GetInt(2);
- args = new object[] {e.GetString(3), e.GetString(4)};
- u = e.GetInt(5U);
- }
- else if(e.Type == "pt") {
- layer = 0;
- x = e.GetInt(0U);
- y = e.GetInt(1U);
- id = e.GetInt(2U);
- args = new object[] {e.GetUInt(3), e.GetUInt(4), e.GetUInt(5)};
- u = e.GetInt(6);
- }
- else if(e.Type == "ts") {
- layer = 0;
- x = e.GetInt(0U);
- y = e.GetInt(1U);
- id = e.GetInt(2U);
- args = new object[] {e.GetString(3), e.GetUInt(4)};
- u = e.GetInt(5U);
- }
- else if(e.Type == "wp") {
- layer = 0;
- x = e.GetInt(0U);
- y = e.GetInt(1U);
- id = e.GetInt(2U);
- args = new object[] {e.GetString(3)};
- u = e.GetInt(4U);
- }
- #endregion
- #region Add to list
- blockArray[layer, x, y] = id;
- List<Block> temp = new List<Block>();
- temp.AddRange(blockList);
- if(id == 0) {
- foreach(Block b in temp)
- if(b.x == x && b.y == y && b.layer == layer)
- blockList.Remove(b);
- } else {
- foreach(Block b in temp) {
- if(b.x == x && b.y == y && b.layer == layer && b.id != id) {
- blockList.Remove(b);
- blockList.Add(new Block() {
- x = x,
- y = y,
- id = id,
- layer = layer,
- args = args,
- placer = u,
- type = e.Type
- });
- }
- }
- }
- #endregion
- }
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement