/src/main/java/org/terasology/logic/world/WorldInfo.java

http://github.com/MovingBlocks/Terasology · Java · 96 lines · 61 code · 14 blank · 21 comment · 8 complexity · a8b5744d76686db86f9d7d7cf1568318 MD5 · raw file

  1. /*
  2. * Copyright 2012 Benjamin Glatzel <benjamin.glatzel@me.com>
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.terasology.logic.world;
  17. import com.google.gson.GsonBuilder;
  18. import java.io.File;
  19. import java.io.FileReader;
  20. import java.io.FileWriter;
  21. import java.io.IOException;
  22. /**
  23. * Summary information on a world.
  24. *
  25. * @author Immortius
  26. */
  27. public class WorldInfo {
  28. private String title = "";
  29. private String seed = "";
  30. private long time = 0;
  31. public static final String DEFAULT_FILE_NAME = "WorldManifest.json";
  32. public WorldInfo() {
  33. }
  34. public WorldInfo(String title, String seed, long time) {
  35. if (title != null) {
  36. this.title = title;
  37. }
  38. if (seed != null) {
  39. this.seed = seed;
  40. }
  41. this.time = time;
  42. }
  43. public static void save(File toFile, WorldInfo worldInfo) throws IOException {
  44. FileWriter writer = new FileWriter(toFile);
  45. try {
  46. new GsonBuilder().setPrettyPrinting().create().toJson(worldInfo, writer);
  47. } finally {
  48. // JAVA7: better closing support
  49. writer.close();
  50. }
  51. }
  52. public static WorldInfo load(File fromFile) throws IOException {
  53. FileReader reader = new FileReader(fromFile);
  54. try {
  55. return new GsonBuilder().create().fromJson(reader, WorldInfo.class);
  56. } finally {
  57. reader.close();
  58. }
  59. }
  60. public String getTitle() {
  61. return title;
  62. }
  63. public void setTitle(String title) {
  64. if (title != null) {
  65. this.title = title;
  66. }
  67. }
  68. public String getSeed() {
  69. return seed;
  70. }
  71. public void setSeed(String seed) {
  72. if (seed != null) {
  73. this.seed = seed;
  74. }
  75. }
  76. public long getTime() {
  77. return time;
  78. }
  79. public void setTime(long time) {
  80. this.time = time;
  81. }
  82. }