PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/spoutcraft/client/SpoutClient.java

https://bitbucket.org/kingbowser/hysteria-spoutcraft
Java | 615 lines | 491 code | 85 blank | 39 comment | 96 complexity | e2393055b95d439dfb60a321070a5151 MD5 | raw file
  1. /*
  2. * This file is part of Hysteria-Spoutcraft.
  3. *
  4. * Copyright (c) 2011 Hysteria Group <http://www.hysteria.us.pn/>
  5. * Hysteria-Spoutcraft is licensed under the GNU Lesser General Public License.
  6. *
  7. * Hysteria-Spoutcraft is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Hysteria-Spoutcraft is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /*
  21. * This file is part of Spoutcraft.
  22. *
  23. * Copyright (c) 2011 Spout LLC <http://www.spout.org/>
  24. * Spoutcraft is licensed under the GNU Lesser General Public License.
  25. *
  26. * Spoutcraft is free software: you can redistribute it and/or modify
  27. * it under the terms of the GNU Lesser General Public License as published by
  28. * the Free Software Foundation, either version 3 of the License, or
  29. * (at your option) any later version.
  30. *
  31. * Spoutcraft is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. * GNU Lesser General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU Lesser General Public License
  37. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  38. */
  39. package org.spoutcraft.client;
  40. import java.io.File;
  41. import java.util.HashMap;
  42. import java.util.Iterator;
  43. import java.util.LinkedList;
  44. import java.util.logging.Logger;
  45. import org.newdawn.slick.util.Log;
  46. import org.newdawn.slick.util.LogSystem;
  47. import net.minecraft.client.Minecraft;
  48. import net.minecraft.src.Entity;
  49. import net.minecraft.src.EntityClientPlayerMP;
  50. import net.minecraft.src.EntityPlayer;
  51. import net.minecraft.src.Packet;
  52. import net.minecraft.src.WorldClient;
  53. import org.spoutcraft.api.Client;
  54. import org.spoutcraft.api.Spoutcraft;
  55. import org.spoutcraft.api.gui.Keyboard;
  56. import org.spoutcraft.api.gui.RenderDelegate;
  57. import org.spoutcraft.api.gui.WidgetManager;
  58. import org.spoutcraft.api.inventory.MaterialManager;
  59. import org.spoutcraft.api.keyboard.KeyBindingManager;
  60. import org.spoutcraft.api.material.MaterialData;
  61. import org.spoutcraft.api.player.BiomeManager;
  62. import org.spoutcraft.api.player.SkyManager;
  63. import org.spoutcraft.api.property.PropertyObject;
  64. import org.spoutcraft.client.config.Configuration;
  65. import org.spoutcraft.client.config.MipMapUtils;
  66. import org.spoutcraft.client.controls.SimpleKeyBindingManager;
  67. import org.spoutcraft.client.entity.CraftEntity;
  68. import org.spoutcraft.client.gui.MCRenderDelegate;
  69. import org.spoutcraft.client.gui.SimpleKeyManager;
  70. import org.spoutcraft.client.gui.SimpleWidgetManager;
  71. import org.spoutcraft.client.gui.minimap.MinimapConfig;
  72. import org.spoutcraft.client.gui.server.ServerManager;
  73. import org.spoutcraft.client.gui.texturepacks.TexturePacksDatabaseModel;
  74. import org.spoutcraft.client.gui.texturepacks.TexturePacksModel;
  75. import org.spoutcraft.client.inventory.SimpleMaterialManager;
  76. import org.spoutcraft.client.io.CRCManager;
  77. import org.spoutcraft.client.io.CustomTextureManager;
  78. import org.spoutcraft.client.io.FileDownloadThread;
  79. import org.spoutcraft.client.io.FileUtil;
  80. import org.spoutcraft.client.packet.CustomPacket;
  81. import org.spoutcraft.client.packet.PacketEntityInformation;
  82. import org.spoutcraft.client.packet.PacketManager;
  83. import org.spoutcraft.client.player.ClientPlayer;
  84. import org.spoutcraft.client.player.SimpleBiomeManager;
  85. import org.spoutcraft.client.player.SimpleSkyManager;
  86. public class SpoutClient extends PropertyObject implements Client {
  87. private static SpoutClient instance = null;
  88. private static final Thread dataMiningThread = new DataMiningThread();
  89. private static final String version = "Unknown Version";
  90. public static final String spoutcraftVersion = "1.5.2";
  91. private final SimpleSkyManager skyManager = new SimpleSkyManager();
  92. private final PacketManager packetManager = new PacketManager();
  93. private final BiomeManager biomeManager = new SimpleBiomeManager();
  94. private final MaterialManager materialManager = new SimpleMaterialManager();
  95. private final RenderDelegate render = new MCRenderDelegate();
  96. private final KeyBindingManager bindingManager = new SimpleKeyBindingManager();
  97. private final Logger log = Logger.getLogger(SpoutClient.class.getName());
  98. private final ServerManager serverManager = new ServerManager();
  99. private long tick = 0;
  100. private long inWorldTicks = 0;
  101. private Thread clipboardThread = null;
  102. private long server = -1L;
  103. public ClientPlayer player = null;
  104. private boolean cheatsky = false;
  105. private boolean forcesky = false;
  106. private boolean showsky = true;
  107. private boolean cheatclearwater = false;
  108. private boolean forceclearwater = false;
  109. private boolean showclearwater = true;
  110. private boolean cheatstars = false;
  111. private boolean forcestars = true;
  112. private boolean showstars = true;
  113. private boolean cheatweather = false;
  114. private boolean forceweather = true;
  115. private boolean showweather = true;
  116. private boolean time = false;
  117. private boolean coords = false;
  118. private boolean entitylabel = false;
  119. private boolean cheatvoidfog = false;
  120. private boolean forcevoidfog = true;
  121. private boolean showvoidfog = true;
  122. private boolean flySpeed = false;
  123. private Mode clientMode = Mode.Menu;
  124. private TexturePacksModel textureModel = new TexturePacksModel();
  125. private TexturePacksDatabaseModel textureDatabaseModel = new TexturePacksDatabaseModel();
  126. private String addonFolder = Minecraft.getMinecraftDir() + File.separator + "addons";
  127. private final WidgetManager widgetManager = new SimpleWidgetManager();
  128. private final HashMap<String, Boolean> permissions = new HashMap<String, Boolean>();
  129. private boolean active = false;
  130. private SpoutClient() {
  131. instance = this;
  132. if (!Thread.currentThread().getName().equals("Minecraft main thread")) {
  133. throw new SecurityException("Main thread name mismatch");
  134. }
  135. //System.setSecurityManager(securityManager);
  136. ((SimpleKeyBindingManager)bindingManager).load();
  137. serverManager.init();
  138. Log.setVerbose(false);
  139. Log.setLogSystem(new SilencedLogSystem());
  140. }
  141. private class SilencedLogSystem implements LogSystem {
  142. @Override
  143. public void debug(String debug) {}
  144. @Override
  145. public void error(Throwable t) {}
  146. @Override
  147. public void error(String error) {}
  148. @Override
  149. public void error(String error, Throwable t) {}
  150. @Override
  151. public void info(String info) {}
  152. @Override
  153. public void warn(String warn) {}
  154. @Override
  155. public void warn(String warn, Throwable t) {}
  156. }
  157. static {
  158. dataMiningThread.start();
  159. Packet.addIdClassMapping(195, true, true, CustomPacket.class);
  160. Configuration.read();
  161. Keyboard.setKeyManager(new SimpleKeyManager());
  162. FileUtil.migrateOldFiles();
  163. }
  164. public static SpoutClient getInstance() {
  165. if (instance == null) {
  166. Spoutcraft.setClient(new SpoutClient());
  167. System.out.println("Starting SpoutClient. Available Memory: " + Runtime.getRuntime().maxMemory() / (1024*1024) + " mb");
  168. }
  169. return instance;
  170. }
  171. public static String getClientVersion() {
  172. return version;
  173. }
  174. public static boolean hasAvailableRAM() {
  175. return Runtime.getRuntime().maxMemory() > 756L;
  176. }
  177. public long getServerVersion() {
  178. return server;
  179. }
  180. public SkyManager getSkyManager() {
  181. return skyManager;
  182. }
  183. public PacketManager getPacketManager() {
  184. return packetManager;
  185. }
  186. public ClientPlayer getActivePlayer() {
  187. return player;
  188. }
  189. public BiomeManager getBiomeManager() {
  190. return biomeManager;
  191. }
  192. public MaterialManager getMaterialManager() {
  193. return materialManager;
  194. }
  195. public net.minecraft.src.World getRawWorld() {
  196. if (getHandle() == null) {
  197. return null;
  198. }
  199. return getHandle().theWorld;
  200. }
  201. public boolean isSkyCheat() {
  202. return cheatsky || !getHandle().isMultiplayerWorld() || !isSpoutEnabled();
  203. }
  204. public boolean isForceSky() {
  205. return forcesky || !getHandle().isMultiplayerWorld() || !isSpoutEnabled();
  206. }
  207. public boolean isShowSky() {
  208. return showsky || !getHandle().isMultiplayerWorld() || !isSpoutEnabled();
  209. }
  210. public boolean isClearWaterCheat() {
  211. return cheatclearwater || !getHandle().isMultiplayerWorld() || !isSpoutEnabled();
  212. }
  213. public boolean isForceClearWater() {
  214. return forceclearwater || !getHandle().isMultiplayerWorld() || !isSpoutEnabled();
  215. }
  216. public boolean isShowClearWater() {
  217. return showclearwater || !getHandle().isMultiplayerWorld() || !isSpoutEnabled();
  218. }
  219. public boolean isStarsCheat() {
  220. return cheatstars || !getHandle().isMultiplayerWorld() || !isSpoutEnabled();
  221. }
  222. public boolean isForceStars() {
  223. return forcestars || !getHandle().isMultiplayerWorld() || !isSpoutEnabled();
  224. }
  225. public boolean isShowStars() {
  226. return showstars || !getHandle().isMultiplayerWorld() || !isSpoutEnabled();
  227. }
  228. public boolean isWeatherCheat() {
  229. return cheatweather || !getHandle().isMultiplayerWorld() || !isSpoutEnabled();
  230. }
  231. public boolean isForceWeather() {
  232. return forceweather || !getHandle().isMultiplayerWorld() || !isSpoutEnabled();
  233. }
  234. public boolean isShowWeather() {
  235. return showweather || !getHandle().isMultiplayerWorld() || !isSpoutEnabled();
  236. }
  237. public boolean isTimeCheat() {
  238. return time || !getHandle().isMultiplayerWorld() || !isSpoutEnabled();
  239. }
  240. public boolean isCoordsCheat() {
  241. return coords || !getHandle().isMultiplayerWorld() || !isSpoutEnabled();
  242. }
  243. public boolean isEntityLabelCheat() {
  244. return entitylabel || !getHandle().isMultiplayerWorld() || !isSpoutEnabled();
  245. }
  246. public boolean isFlySpeedCheat() {
  247. return flySpeed || !getHandle().isMultiplayerWorld() || !isSpoutEnabled();
  248. }
  249. public boolean isVoidFogCheat() {
  250. return cheatvoidfog || !getHandle().isMultiplayerWorld() || !isSpoutEnabled();
  251. }
  252. public boolean isForceVoidFog() {
  253. return forcevoidfog || !getHandle().isMultiplayerWorld() || !isSpoutEnabled();
  254. }
  255. public boolean isShowVoidFog() {
  256. return showvoidfog || !getHandle().isMultiplayerWorld() || !isSpoutEnabled();
  257. }
  258. public void setVisualCheats(boolean tsky, boolean fsky, boolean ssky, boolean tclearwater, boolean fclearwater, boolean sclearwater, boolean tstars, boolean fstars, boolean sstars, boolean tweather, boolean fweather, boolean sweather, boolean ttime, boolean tcoords, boolean tentitylabel, boolean tvoidfog, boolean fvoidfog, boolean svoidfog, boolean tflyspeed) {
  259. this.cheatsky = tsky;
  260. this.forcesky = fsky;
  261. this.showsky = ssky;
  262. this.cheatclearwater = tclearwater;
  263. this.forceclearwater = fclearwater;
  264. this.showclearwater = sclearwater;
  265. this.cheatstars = tstars;
  266. this.forcestars = fstars;
  267. this.showstars = sstars;
  268. this.cheatweather = tweather;
  269. this.forceweather = fweather;
  270. this.showweather = sweather;
  271. this.time = ttime;
  272. this.coords = tcoords;
  273. this.entitylabel = tentitylabel;
  274. this.cheatvoidfog = tvoidfog;
  275. this.forcevoidfog = fvoidfog;
  276. this.showvoidfog = svoidfog;
  277. this.flySpeed = tflyspeed;
  278. if (!isTimeCheat()) {
  279. Configuration.setTime(0);
  280. }
  281. if (cheatweather) {
  282. Configuration.cheatweather = true;
  283. } else {
  284. Configuration.cheatweather = false;
  285. }
  286. if (cheatsky) {
  287. Configuration.cheatsky = true;
  288. } else {
  289. Configuration.cheatsky = false;
  290. }
  291. if (cheatstars) {
  292. Configuration.cheatstars = true;
  293. } else {
  294. Configuration.cheatstars = false;
  295. }
  296. if (cheatvoidfog) {
  297. Configuration.cheatvoidFog = true;
  298. } else {
  299. Configuration.cheatvoidFog = false;
  300. }
  301. if (cheatclearwater) {
  302. Configuration.cheatclearWater = true;
  303. } else {
  304. Configuration.cheatclearWater = false;
  305. }
  306. if (isForceWeather()) {
  307. if (isShowWeather()) {
  308. Configuration.setWeather(true);
  309. } else {
  310. Configuration.setWeather(false);
  311. }
  312. }
  313. if (isForceStars()) {
  314. if (isShowStars()) {
  315. Configuration.setStars(true);
  316. } else {
  317. Configuration.setStars(false);
  318. }
  319. }
  320. if (isForceSky()) {
  321. if (isShowSky()) {
  322. Configuration.setSky(true);
  323. } else {
  324. Configuration.setSky(false);
  325. }
  326. }
  327. if (isForceClearWater()) {
  328. if (isShowClearWater()) {
  329. Configuration.setClearWater(true);
  330. } else {
  331. Configuration.setClearWater(false);
  332. }
  333. }
  334. if (isForceWeather()) {
  335. if (isShowWeather()) {
  336. Configuration.setWeather(true);
  337. } else {
  338. Configuration.setWeather(false);
  339. }
  340. }
  341. if (isForceVoidFog()) {
  342. if (isShowVoidFog()) {
  343. Configuration.setVoidFog(true);
  344. } else {
  345. Configuration.setVoidFog(false);
  346. }
  347. }
  348. }
  349. public boolean isSpoutEnabled() {
  350. return server >= 0;
  351. }
  352. public void setSpoutVersion(long version) {
  353. server = version;
  354. }
  355. public boolean isSpoutActive() {
  356. return active;
  357. }
  358. public void setSpoutActive(boolean active) {
  359. this.active = active;
  360. }
  361. public void onTick() {
  362. tick++;
  363. Configuration.onTick();
  364. getHandle().mcProfiler.startSection("httpdownloads");
  365. FileDownloadThread.getInstance().onTick();
  366. getHandle().mcProfiler.endStartSection("packet_decompression");
  367. PacketDecompressionThread.onTick();
  368. getHandle().mcProfiler.endStartSection("widgets");
  369. player.getMainScreen().onTick();
  370. getHandle().mcProfiler.endStartSection("mipmapping");
  371. MipMapUtils.onTick();
  372. getHandle().mcProfiler.endStartSection("special_effects");
  373. if (Minecraft.theMinecraft.theWorld != null) {
  374. Minecraft.theMinecraft.theWorld.doColorfulStuff();
  375. inWorldTicks++;
  376. }
  377. getHandle().mcProfiler.endStartSection("entity_info");
  378. if (isSpoutEnabled()) {
  379. LinkedList<CraftEntity> processed = new LinkedList<CraftEntity>();
  380. Iterator<Entity> i = Entity.toProcess.iterator();
  381. while (i.hasNext()) {
  382. Entity next = i.next();
  383. if (next.spoutEnty != null) {
  384. processed.add((CraftEntity) next.spoutEnty);
  385. }
  386. }
  387. Entity.toProcess.clear();
  388. if (processed.size() > 0) {
  389. getPacketManager().sendSpoutPacket(new PacketEntityInformation(processed));
  390. }
  391. }
  392. getHandle().mcProfiler.endSection();
  393. }
  394. public long getTick() {
  395. return tick;
  396. }
  397. public long getInWorldTicks() {
  398. return inWorldTicks;
  399. }
  400. public void onWorldExit() {
  401. FileUtil.deleteTempDirectory();
  402. CustomTextureManager.resetTextures();
  403. CRCManager.clear();
  404. if (clipboardThread != null) {
  405. clipboardThread.interrupt();
  406. clipboardThread = null;
  407. }
  408. Minecraft.theMinecraft.sndManager.stopMusic();
  409. PacketDecompressionThread.endThread();
  410. MaterialData.reset();
  411. FileDownloadThread.preCacheCompleted.lazySet(0);
  412. server = -1L;
  413. inWorldTicks = 0L;
  414. MaterialData.reset();
  415. MinimapConfig.getInstance().getServerWaypoints().clear();
  416. }
  417. public void onWorldEnter() {
  418. if (player == null) {
  419. player = ClientPlayer.getInstance();
  420. player.setPlayer(getHandle().thePlayer);
  421. getHandle().thePlayer.spoutEnty = player;
  422. }
  423. if (player.getHandle() instanceof EntityClientPlayerMP && isSpoutEnabled()) {
  424. clipboardThread = new ClipboardThread((EntityClientPlayerMP)player.getHandle());
  425. clipboardThread.start();
  426. } else if (clipboardThread != null) {
  427. clipboardThread.interrupt();
  428. clipboardThread = null;
  429. }
  430. PacketDecompressionThread.startThread();
  431. MipMapUtils.initializeMipMaps();
  432. MipMapUtils.update();
  433. player.getMainScreen().toggleSurvivalHUD(!Minecraft.theMinecraft.playerController.isInCreativeMode());
  434. inWorldTicks = 0L;
  435. MinimapConfig.getInstance().getServerWaypoints().clear();
  436. }
  437. public static Minecraft getHandle() {
  438. return Minecraft.theMinecraft;
  439. }
  440. public EntityPlayer getPlayerFromId(int id) {
  441. if (getHandle().thePlayer.entityId == id) {
  442. return getHandle().thePlayer;
  443. }
  444. WorldClient world = (WorldClient)getHandle().theWorld;
  445. Entity e = world.getEntityByID(id);
  446. if (e instanceof EntityPlayer) {
  447. return (EntityPlayer) e;
  448. }
  449. return null;
  450. }
  451. public Entity getEntityFromId(int id) {
  452. if (getHandle().thePlayer.entityId == id) {
  453. return getHandle().thePlayer;
  454. }
  455. WorldClient world = (WorldClient)getHandle().theWorld;
  456. return world.getEntityByID(id);
  457. }
  458. public Logger getLogger() {
  459. return log;
  460. }
  461. public Mode getMode() {
  462. return clientMode;
  463. }
  464. public void setMode(Mode clientMode) {
  465. this.clientMode = clientMode;
  466. }
  467. public String getName() {
  468. return "Spoutcraft";
  469. }
  470. public RenderDelegate getRenderDelegate() {
  471. return render;
  472. }
  473. public File getUpdateFolder() {
  474. return new File(Minecraft.getMinecraftDir(), "addons" + File.separator + "updates");
  475. }
  476. public String getVersion() {
  477. return version;
  478. }
  479. public KeyBindingManager getKeyBindingManager() {
  480. return bindingManager;
  481. }
  482. public File getAddonFolder() {
  483. return new File(addonFolder);
  484. }
  485. public File getAudioCache() {
  486. return getTemporaryCache();
  487. }
  488. public File getTemporaryCache() {
  489. return FileUtil.getTempDir();
  490. }
  491. public File getTextureCache() {
  492. return getTemporaryCache();
  493. }
  494. public File getTexturePackFolder() {
  495. return FileUtil.getTexturePackDir();
  496. }
  497. public File getSelectedTexturePackZip() {
  498. return FileUtil.getSelectedTexturePackZip();
  499. }
  500. public File getStatsFolder() {
  501. return FileUtil.getStatsDir();
  502. }
  503. public ServerManager getServerManager() {
  504. return instance.serverManager;
  505. }
  506. public TexturePacksModel getTexturePacksModel() {
  507. return textureModel;
  508. }
  509. public TexturePacksDatabaseModel getTexturePacksDatabaseModel() {
  510. return textureDatabaseModel;
  511. }
  512. public WidgetManager getWidgetManager() {
  513. return widgetManager;
  514. }
  515. @Override
  516. public boolean hasPermission(String node) {
  517. Boolean allow = permissions.get(node);
  518. if (allow != null) {
  519. return allow;
  520. } else {
  521. return true;
  522. }
  523. }
  524. public void setPermission(String node, boolean allow) {
  525. permissions.put(node, allow);
  526. }
  527. public void clearPermissions() {
  528. permissions.clear();
  529. }
  530. }