[Class/Lib] Evas - The simple Schematic System

Discussion in 'Resources' started by Yekllurt, Aug 23, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Yekllurt

    Hi Guys,
    this is my newest project wich make's it able to create easy Schematic's(please notice that they don't get saved like the Schematics) like in World Edit.
    This coud be good used in a Minigame Plugin where you want to reset an Arena and if you don't want to use WorldEdit (Please notice that i don't have anything vs WorldEdit)
    You can use this to as an area protection plugin

    Code:java
    1.  
    2. package me.Yekllurt.Main;
    3.  
    4. import java.io.File;
    5. import java.io.IOException;
    6. import java.util.ArrayList;
    7. import java.util.Iterator;
    8. import java.util.List;
    9.  
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.Location;
    12. import org.bukkit.Material;
    13. import org.bukkit.World;
    14. import org.bukkit.block.Block;
    15. import org.bukkit.configuration.file.YamlConfiguration;
    16. import org.bukkit.entity.Player;
    17.  
    18.  
    19. /**
    20. *
    21. * @author Yekllurt
    22. * copyright 2014
    23. */
    24. public class Evas {
    25.  
    26. enum EvasType{
    27. Square;
    28. public static EvasType type;
    29. }
    30.  
    31. private int CordinateX, CordinateY, CordinateZ, CordinateX1, CordinateY1, CordinateZ1, size;
    32. private World CordinateWorld;
    33. private EvasType evasType;
    34. private String name;
    35. private File file;
    36. private YamlConfiguration yml;
    37.  
    38. /**
    39. * @param Create's a new Evas
    40. * @param World world : is the World of the Evas
    41. * @param Integer x : is the x Cordinate, Corner 1 case EvasType.Square else the center x Cordinate of the Circle
    42. * @param Integer y : is the y Cordinate, Corner 1 case EvasType.Square else the center y Cordinate of the Circle
    43. * @param Integer z : is the z Cordinate, Corner 1 case EvasType.Square else the center z Cordinate of the Circle
    44. * @param Integer x1 : is the x Cordinate, Corner 2 case EvasType.Square
    45. * @param Integer y1 : is the y Cordinate, Corner 2 case EvasType.Square
    46. * @param Integer z1 : is the t Cordinate, Corner 2 case EvasType.Square
    47. */
    48. public Evas(World world, int x, int y, int z, int x1, int y1, int z1, EvasType type, String name, String savePath){
    49. this.CordinateWorld = world;
    50. this.name = name;
    51. //this.file = new File("plugins/Evas/Evas", name + ".evas");
    52. this.file = new File(savePath, name + ".evas");
    53. try {
    54. this.file.createNewFile();
    55. this.file.setReadable(false);
    56. this.yml = YamlConfiguration.loadConfiguration(file);
    57. } catch (IOException e) {
    58. e.printStackTrace();
    59. }
    60. switch (type) {
    61. case Square:
    62. if(y > y1){
    63. this.CordinateX = Math.min(x1, x);
    64. this.CordinateY = Math.min(y1, y);
    65. this.CordinateZ = Math.min(z1, z);
    66. this.CordinateX1 = Math.max(x1, x);
    67. this.CordinateY1 = Math.max(y1, y);
    68. this.CordinateZ1 = Math.max(z1, z);
    69. }else{
    70. this.CordinateX = Math.min(x, x1);
    71. this.CordinateY = Math.min(y, y1);
    72. this.CordinateZ = Math.min(z, z1);
    73. this.CordinateX1 = Math.max(x, x1);
    74. this.CordinateY1 = Math.max(y, y1);
    75. this.CordinateZ1 = Math.max(z, z1);
    76. }
    77. break;
    78. }
    79. this.evasType = type;
    80. }
    81.  
    82.  
    83. /**
    84. * Load an Evas throw the name
    85. */
    86. public Evas(String name, String savePath){
    87. //this.file = new File("plugins/Evas/Evas/" + name + ".evas");
    88. this.file = new File(savePath, name + ".evas");
    89. this.yml = YamlConfiguration.loadConfiguration(this.file);
    90. ArrayList<String> importantData = (ArrayList<String>) yml.getList("ImportantData");
    91. this.CordinateWorld = Bukkit.getWorld(importantData.get(0));
    92. this.CordinateX = Integer.valueOf(importantData.get(1));
    93. this.CordinateY = Integer.valueOf(importantData.get(2));
    94. this.CordinateZ = Integer.valueOf(importantData.get(3));
    95. this.CordinateX1 = Integer.valueOf(importantData.get(4));
    96. this.CordinateY1 = Integer.valueOf(importantData.get(5));
    97. this.CordinateZ1 = Integer.valueOf(importantData.get(6));
    98. this.size = Integer.valueOf(importantData.get(7));
    99. }
    100.  
    101. /**
    102. * Get important values from the Evas
    103. */
    104. public int getX(){
    105. return this.CordinateX;
    106. }
    107. public int getY(){
    108. return this.CordinateY;
    109. }
    110. public int getZ(){
    111. return this.CordinateZ;
    112. }
    113. public int getX1(){
    114. return this.CordinateX1;
    115. }
    116. public int getY1(){
    117. return this.CordinateY1;
    118. }
    119. public int getZ1(){
    120. return this.CordinateZ1;
    121. }
    122. public World getWorld(){
    123. return this.CordinateWorld;
    124. }
    125. public String getName(){
    126. return this.name;
    127. }
    128.  
    129. /**
    130. *
    131. * @return Returns the amount of blocks in the Evas
    132. */
    133. public int size(){
    134. this.size = getBlocks().size();
    135. return getBlocks().size();
    136. }
    137.  
    138. /**
    139. * @param Fills the Block wich are Air with the given Material
    140. * @param material The material type
    141. * @param damage The material byte
    142. */
    143. public void fill(Material material, byte damage){
    144. List<Block> blocks = getBlocks();
    145. for(Block block : blocks){
    146. Block locBlock = block.getWorld().getBlockAt(block.getX(), block.getY(), block.getZ());
    147. if(locBlock.getType() == Material.AIR){
    148. locBlock.setType(material);
    149. locBlock.setData(damage);
    150. }
    151. }
    152. }
    153.  
    154. /**
    155. * @param Set's all blocks to a material
    156. * @param material Set the block material
    157. * @param damage Set the block's byte
    158. */
    159. public void set(Material material, byte damage){
    160. List<Block> blocks = getBlocks();
    161. for(Block block : blocks){
    162. Block locBlock = block.getWorld().getBlockAt(block.getX(), block.getY(), block.getZ());
    163. locBlock.setType(material);
    164. locBlock.setData(damage);
    165. }
    166. }
    167.  
    168. /**
    169. * @param Replace blocks throw another block
    170. * @param material Replaced Material
    171. * @param damage Replaced Material byte
    172. * @param toReplace Material where get's replaced
    173. * @param toReplaceByte Material where get's replaced byte
    174. */
    175. public void replace(Material material, byte damage, Material toReplace, byte toReplaceByte){
    176. List<Block> blocks = getBlocks();
    177. for(Block block : blocks){
    178. Block locBlock = block.getWorld().getBlockAt(block.getX(), block.getY(), block.getZ());
    179. if(locBlock.getType() == toReplace && locBlock.getData() == toReplaceByte){
    180. locBlock.setType(material);
    181. locBlock.setData(damage);
    182. }
    183. }
    184. }
    185.  
    186.  
    187. /**
    188. *
    189. * @return Returns a List of all Blocks
    190. */
    191. public List<Block> getBlocks() {
    192. Iterator<Block> block = this.iterator();
    193. List<Block> copy = new ArrayList<Block>();
    194. while (block.hasNext())
    195. copy.add(block.next());
    196. return copy;
    197. }
    198.  
    199. public Iterator<Block> iterator() {
    200. return new EvasIterator(this.CordinateWorld, this.CordinateX, this.CordinateY, this.CordinateZ, this.CordinateX1, this.CordinateY1, this.CordinateZ1);
    201. }
    202.  
    203. public class EvasIterator implements Iterator<Block> {
    204. private World w;
    205. private int baseX, baseY, baseZ;
    206. private int x, y, z;
    207. private int sizeX, sizeY, sizeZ;
    208.  
    209. public EvasIterator(World w, int x1, int y1, int z1, int x2, int y2, int z2) {
    210. this.w = w;
    211. this.baseX = x1;
    212. this.baseY = y1;
    213. this.baseZ = z1;
    214. this.sizeX = Math.abs(x2 - x1) + 1;
    215. this.sizeY = Math.abs(y2 - y1) + 1;
    216. this.sizeZ = Math.abs(z2 - z1) + 1;
    217. this.x = this.y = this.z = 0;
    218. }
    219.  
    220. public boolean hasNext() {
    221. return this.x < this.sizeX && this.y < this.sizeY && this.z < this.sizeZ;
    222. }
    223.  
    224. public Block next() {
    225. Block b = this.w.getBlockAt(this.baseX + this.x, this.baseY + this.y, this.baseZ + this.z);
    226. if (++x >= this.sizeX) {
    227. this.x = 0;
    228. if (++this.y >= this.sizeY) {
    229. this.y = 0;
    230. ++this.z;
    231. }
    232. }
    233. return b;
    234. }
    235.  
    236.  
    237. }
    238.  
    239. /**
    240. * Save the Evas to the file
    241. */
    242. public void save(){
    243. ArrayList<String> importantData = new ArrayList<>();
    244. importantData.add(this.CordinateWorld.getName());
    245. importantData.add(String.valueOf(this.CordinateX));
    246. importantData.add(String.valueOf(this.CordinateY));
    247. importantData.add(String.valueOf(this.CordinateZ));
    248. importantData.add(String.valueOf(this.CordinateX1));
    249. importantData.add(String.valueOf(this.CordinateY1));
    250. importantData.add(String.valueOf(this.CordinateZ1));
    251. importantData.add(String.valueOf(size()));
    252. //String[] importantData = {this.CordinateWorld.getName(), String.valueOf(this.CordinateX), String.valueOf(this.CordinateY), String.valueOf(this.CordinateZ), String.valueOf(this.CordinateX1), String.valueOf(this.CordinateY1), String.valueOf(this.CordinateZ1), String.valueOf(size())};
    253. this.yml.set("ImportantData", importantData);
    254. List<Block> blocks = getBlocks();
    255. int count = 0;
    256. for(Block block : blocks){
    257. List<String> blockString = new ArrayList<>();
    258. blockString.add(String.valueOf(block.getX()));
    259. blockString.add(String.valueOf(block.getY()));
    260. blockString.add(String.valueOf(block.getZ()));
    261. blockString.add(String.valueOf(block.getType()));
    262. blockString.add(String.valueOf(block.getData()));
    263. //blockString = new String[]{block.getWorld().getName(), String.valueOf(block.getX()), String.valueOf(block.getY()), String.valueOf(block.getZ()), String.valueOf(block.getType()), String.valueOf(block.getData())};
    264. count++;
    265. yml.set(String.valueOf(count), blockString);
    266. }
    267. try {
    268. this.yml.save(this.file);
    269. } catch (IOException e) {
    270. e.printStackTrace();
    271. }
    272.  
    273. }
    274.  
    275. /**
    276. * Rests the Evas to the last saving point
    277. */
    278. public void reset(){
    279. ArrayList<String> importantData = (ArrayList<String>) yml.getList("ImportantData");
    280. int size = Integer.valueOf((String) importantData.get(7));
    281. String world = importantData.get(0);
    282. for(int i = 1; i <= size; i++){
    283. ArrayList<String> blockString = (ArrayList<String>) this.yml.getList(String.valueOf(i));
    284. Block b = Bukkit.getWorld(world).getBlockAt(Integer.valueOf(blockString.get(0)), Integer.valueOf(blockString.get(1)), Integer.valueOf(blockString.get(2)));
    285. b.setType(Material.valueOf(blockString.get(3)));
    286. b.setData(Byte.valueOf(blockString.get(4)));
    287. }
    288. }
    289.  
    290.  
    291. /**
    292. * Check if a Evas Schematic exists with that name
    293. * @return
    294. */
    295. public static boolean exists(String name){
    296. File f = new File("plugins/Evas/Evas/");
    297. for(File file : f.listFiles()){
    298. if(file.getName().equals(name + ".evas")){
    299. return true;
    300. }else{
    301. return false;
    302. }
    303. }
    304. return false;
    305. }
    306.  
    307.  
    308. /**
    309. * Get the center Location of the Evas
    310. * @return
    311. */
    312. public Location getCenter() {
    313. int x1 = this.CordinateX + 1;
    314. int y1 = this.CordinateY + 1;
    315. int z1 = this.CordinateZ + 1;
    316. return new Location(this.getWorld(), this.CordinateX1 + (x1 - this.CordinateX1) / 2.0, this.CordinateY1 + (y1 - this.CordinateY1) / 2.0, this.CordinateZ1 + (z1 - this.CordinateZ1) / 2.0);
    317. }
    318.  
    319. /**
    320. * Check if the location is in the Evas
    321. * @return
    322. */
    323. public boolean contains(int x, int y, int z) {
    324. return x >= this.CordinateX && x <= this.CordinateX1 && y >= this.CordinateY && y <= this.CordinateY1 && z >= this.CordinateZ && z <= this.CordinateZ1;
    325. }
    326.  
    327. /**
    328. * Use this in the PlayerMoveEvent to check if the player enter's it
    329. * @param a e.getTo();
    330. * @param b e.getFrom();
    331. * @return
    332. */
    333. public boolean enter(Location a, Location b){
    334. if(this.contains(a.getBlockX(), a.getBlockY(), a.getBlockZ()) && !this.contains(b.getBlockX(), b.getBlockY(), b.getBlockZ())){
    335. return true;
    336. }else{
    337. return false;
    338. }
    339. }
    340.  
    341. /**
    342. * Use this in the PlayerMoveEvent to check if the player leave's it
    343. * @param a e.getTo();
    344. * @param b e.getFrom();
    345. * @return
    346. */
    347. public boolean leave(Location a, Location b){
    348. if(!this.contains(a.getBlockX(), a.getBlockY(), a.getBlockZ()) && this.contains(b.getBlockX(), b.getBlockY(), b.getBlockZ())){
    349. return true;
    350. }else{
    351. return false;
    352. }
    353. }
    354.  
    355. }
    356.  


    The getBlocks() and iterator() methods are from KingFaris11 (https://forums.bukkit.org/threads/protection-region-cuboid-creation.164161/).
    The EvasIterator class is from KingFaris11 (https://forums.bukkit.org/threads/protection-region-cuboid-creation.164161/)

    Example how to use it
    Code:java
    1.  
    2. Evas evas;
    3. //Create a new Evas and save it
    4. evas = new Evas(Bukkit.getWorld("world"), 0, 0, 0, 10, 10, 10, EvasType.Square, "Spawn", "plugins/YourName/Evas");
    5. evas.save();
    6.  
    7. //Load an Evas
    8. evas = new Evas("Spawn", "plugins/YourName/Evas");
    9.  
    10. //Reset the Evas to the last saved location
    11. evas.reset();
    12.  


    If you have ideas write them in the comments.
     
    Phasesaber and ChipDev like this.
  2. Offline

    ChipDev

    Wow. great :D
    Oh crappo. I now have to let you reserve this spot o.o
     
    Phasesaber likes this.
  3. Offline

    Dubehh

    Could this be used to reset (regenerate) an Entire world?
     
  4. Offline

    97WaterPolo

    Dubehh
    It could, but you would need an insane server depending on your world size.
     
  5. Offline

    Yekllurt

    For regenerating a whole World it woud be easyer to delete the old World file and replace it through a copy of the World.
    How it saves it at the moment it can save a maximum of 999999999 Blocks.
    For regenerating an Arena it works out problem or if you have for example a Mine that shoud be resetet.
     
  6. Offline

    Dubehh

    Yekllurt
    Well, I don't exactly use the 'whole' world, just a big plot (400x300-ish).
    How would I (somewhere in the world) mark the land I want to reset. (How would I let the plugin know which part of the world it needs to reset)
     
  7. Offline

    Yekllurt

    Evas evas = evas = new Evas(Bukkit.getWorld("YourWorldName"), x1, y1, z1, x2, y2, z2, EvasType.Square, "NameOfYourEvas", "ThePlaceWhereYouSaveIt");
    evas.save(); //Very Importent when you creat it this save's it

    Evas evas = new Evas("NameOfYourEvas", "ThePlaceWhereYouSaveIt");
    evas.reset(); //Rests you Evas to the last saved point
     
  8. Offline

    Bart

    Code:
    public int size() {
      int i = 0;
      List<Block> blocks = getBlocks();
     
      for(Block block : blocks) {
        i++;
      }
     
      this.size = i;
     
      return i;
    
    Could you not have
    Code:
    public int size() { return getBlocks().size(); }
    Then you won't need to cache the size of the list.
     
  9. Offline

    Yekllurt

    Thanks :)
     
Thread Status:
Not open for further replies.

Share This Page