PageRenderTime 2212ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/oldsrc/hmod/HmodWorldImpl.java

http://github.com/sk89q/craftbook
Java | 292 lines | 194 code | 78 blank | 20 comment | 27 complexity | 89831c792bc1aa003cb2e06f9782431a MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. Craftbook
  3. Copyright (C) 2010 Lymia <lymiahugs@gmail.com>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. import com.sk89q.craftbook.access.*;
  16. import com.sk89q.craftbook.access.MinecartInterface.Type;
  17. import com.sk89q.craftbook.util.BlockVector;
  18. import com.sk89q.craftbook.util.Tuple2;
  19. import java.io.File;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.logging.Logger;
  23. public class HmodWorldImpl extends WorldInterface$ {
  24. /**
  25. * Logger.
  26. */
  27. static final Logger logger = Logger.getLogger("Minecraft.CraftBook");
  28. private final Server server = etc.getServer();
  29. private final CraftBook main;
  30. private final TickDelayer delay = new TickDelayer(this);
  31. private boolean fakeExisting = false;
  32. private int fakeX = 0;
  33. private int fakeY = 0;
  34. private int fakeZ = 0;
  35. private int fakeData = 0;
  36. public HmodWorldImpl(CraftBook main) {
  37. this.main = main;
  38. }
  39. public boolean setId(int x, int y, int z, int t) {
  40. if (server.setBlockAt(t, x, y, z)) {
  41. if (isFakedBlock(x, y, z)) fakeExisting = false;
  42. return true;
  43. }
  44. return false;
  45. }
  46. public int getId(int x, int y, int z) {
  47. return server.getBlockIdAt(x, y, z);
  48. }
  49. public boolean setData(int x, int y, int z, int t) {
  50. if (isFakedBlock(x, y, z)) fakeExisting = false;
  51. return server.setBlockData(x, y, z, t);
  52. }
  53. public int getData(int x, int y, int z) {
  54. if (isFakedBlock(x, y, z)) {
  55. return fakeData;
  56. }
  57. return server.getBlockData(x, y, z);
  58. }
  59. public void dropItem(double x, double y, double z, int type, int count) {
  60. server.dropItem(x, y, z, type, count);
  61. }
  62. public String getName() {
  63. return main.name;
  64. }
  65. public boolean setDataAndUpdate(int x, int y, int z, int t) {
  66. if (server.setBlockData(x, y, z, t)) {
  67. if (isFakedBlock(x, y, z)) fakeExisting = false;
  68. server.updateBlockPhysics(x, y, z, t);
  69. return true;
  70. }
  71. return false;
  72. }
  73. public boolean isPlayerInWorld(String player) {
  74. return main.isPlayerOnline(player);
  75. }
  76. public PlayerInterface getPlayer(String player) {
  77. return main.getPlayer(player);
  78. }
  79. public PlayerInterface matchPlayer(String player) {
  80. return main.matchPlayer(player);
  81. }
  82. public List<PlayerInterface> getPlayerList() {
  83. return main.getPlayerList();
  84. }
  85. public BlockEntity getBlockEntity(int x, int y, int z) {
  86. ComplexBlock b = server.getComplexBlock(x, y, z);
  87. if (b == null) return null;
  88. else if (b instanceof Sign) return new HmodSignImpl(this, new BlockVector(x, y, z), (Sign) b);
  89. else if (b instanceof Chest) return new HmodChestImpl(this, new BlockVector(x, y, z), (Chest) b);
  90. else if (b instanceof DoubleChest) return new HmodChestImpl(this, new BlockVector(x, y, z), (DoubleChest) b);
  91. else return null;
  92. }
  93. public Tuple2<Integer, Integer>[] getLoadedChunks() {
  94. return ChunkFinder.getLoadedChunks(server.getMCServer().e);
  95. }
  96. public long getTime() {
  97. return server.getTime();
  98. }
  99. public void setTime(long time) {
  100. server.setTime(time);
  101. }
  102. public void delayAction(Action a) {
  103. delay.delayAction(a);
  104. }
  105. protected void fakeData(int x, int y, int z, int d) {
  106. fakeExisting = true;
  107. fakeX = x;
  108. fakeY = y;
  109. fakeZ = z;
  110. fakeData = d;
  111. }
  112. protected void destroyFake() {
  113. fakeExisting = false;
  114. }
  115. private boolean isFakedBlock(int x, int y, int z) {
  116. return fakeExisting && fakeX == x && fakeY == y && fakeZ == z;
  117. }
  118. public void sendMessage(String message) {
  119. server.messageAll(message);
  120. }
  121. public int getLightLevel(int x, int y, int z) {
  122. return server.getMCServer().e.j(x, y + 1, z);
  123. }
  124. public String getUniqueIdString() {
  125. return main.name;
  126. }
  127. public File getPath() {
  128. return main.path;
  129. }
  130. public File getToggleAreaPath() {
  131. return main.pathToToggleAreas;
  132. }
  133. public void explode(double x, double y, double z) {
  134. dh tnt = new dh(etc.getMCServer().e);
  135. tnt.a(x, y, z);
  136. tnt.b_();
  137. }
  138. public ArrowInterface shootArrow(double x, double y, double z,
  139. double xComponent, double yComponent, double zComponent,
  140. double speed, double spread) {
  141. fc arrow = new fc(etc.getMCServer().e);
  142. arrow.c(x, y, z, 0, 0);
  143. etc.getMCServer().e.a(arrow);
  144. arrow.a(xComponent, yComponent, zComponent, (float) speed, (float) spread);
  145. return new HmodArrowImpl(arrow, this);
  146. }
  147. public void enqueAction(Runnable r) {
  148. server.addToServerQueue(r);
  149. }
  150. public List<MobInterface> getMobList() {
  151. List<Mob> list = server.getMobList();
  152. List<MobInterface> list2 = new ArrayList<MobInterface>();
  153. for (Mob p : list) { list2.add(new HmodMobImpl(p, this)); }
  154. return list2;
  155. }
  156. public MobInterface spawnMob(double x, double y, double z, String type) {
  157. Mob m = new Mob(type);
  158. m.setX(x);
  159. m.setY(y);
  160. m.setZ(z);
  161. m.spawn();
  162. return new HmodMobImpl(m, this);
  163. }
  164. @Override
  165. public MobInterface spawnMob(double x, double y, double z, String type, String rider) {
  166. Mob m = new Mob(type);
  167. m.setX(x);
  168. m.setY(y);
  169. m.setZ(z);
  170. Mob r = new Mob(rider);
  171. r.setX(x);
  172. r.setY(y);
  173. r.setZ(z);
  174. m.spawn(r);
  175. return new HmodMobImpl(m, this);
  176. }
  177. public List<MinecartInterface> getMinecartList() {
  178. List<MinecartInterface> list = new ArrayList<MinecartInterface>();
  179. for (BaseVehicle v : server.getVehicleEntityList()) {
  180. if (v instanceof Minecart) {
  181. Minecart m = (Minecart) v;
  182. if (m.getStorage() == null)
  183. list.add(new HmodMinecartImpl(m, this));
  184. else
  185. list.add(new HmodStorageMinecartImpl(m, this));
  186. }
  187. }
  188. return list;
  189. }
  190. @Override
  191. public MinecartInterface spawnMinecart(double x, double y, double z, Type type) {
  192. Minecart minecart = null;
  193. switch (type) {
  194. case REGULAR:
  195. minecart = new Minecart(x, y, z, Minecart.Type.Minecart);
  196. break;
  197. case POWERED:
  198. minecart = new Minecart(x, y, z, Minecart.Type.PoweredMinecart);
  199. break;
  200. case STORAGE:
  201. minecart = new Minecart(x, y, z, Minecart.Type.StorageCart);
  202. break;
  203. default:
  204. assert false;
  205. }
  206. return new HmodMinecartImpl(minecart, this);
  207. }
  208. protected void registerTickListener(CraftBook core) {
  209. TickPatch.addTask(TickPatch.wrapRunnable(core, delay));
  210. }
  211. }