Fix several crashes, added Multiplayer menu

This commit is contained in:
PeytonPlayz595 2023-12-19 22:25:00 -05:00
parent acab40399f
commit 0a9e4ea3f3
14 changed files with 345 additions and 440 deletions

View File

@ -7,7 +7,6 @@ import com.mojang.minecraft.gui.*;
import com.mojang.minecraft.item.Arrow; import com.mojang.minecraft.item.Arrow;
import com.mojang.minecraft.item.Item; import com.mojang.minecraft.item.Item;
import com.mojang.minecraft.level.Level; import com.mojang.minecraft.level.Level;
import com.mojang.minecraft.level.LevelIO;
import com.mojang.minecraft.level.generator.LevelGenerator; import com.mojang.minecraft.level.generator.LevelGenerator;
import com.mojang.minecraft.level.liquid.LiquidType; import com.mojang.minecraft.level.liquid.LiquidType;
import com.mojang.minecraft.level.tile.Block; import com.mojang.minecraft.level.tile.Block;
@ -41,8 +40,9 @@ import java.io.*;
import java.nio.IntBuffer; import java.nio.IntBuffer;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.zip.GZIPInputStream;
public final class Minecraft implements Runnable { public final class Minecraft {
public GameMode gamemode = new SurvivalGameMode(this); public GameMode gamemode = new SurvivalGameMode(this);
public int width; public int width;
@ -60,7 +60,6 @@ public final class Minecraft implements Runnable {
public GuiScreen currentScreen = null; public GuiScreen currentScreen = null;
public ProgressBarDisplay progressBar = new ProgressBarDisplay(this); public ProgressBarDisplay progressBar = new ProgressBarDisplay(this);
public Renderer renderer = new Renderer(this); public Renderer renderer = new Renderer(this);
public LevelIO levelIo;
private int ticks; private int ticks;
private int blockHitTime; private int blockHitTime;
public String levelName; public String levelName;
@ -82,7 +81,6 @@ public final class Minecraft implements Runnable {
public Minecraft() { public Minecraft() {
this.levelIo = new LevelIO(this.progressBar);
this.ticks = 0; this.ticks = 0;
this.blockHitTime = 0; this.blockHitTime = 0;
this.levelName = null; this.levelName = null;
@ -96,7 +94,6 @@ public final class Minecraft implements Runnable {
this.hasMouse = false; this.hasMouse = false;
this.lastClick = 0; this.lastClick = 0;
this.raining = false; this.raining = false;
new SleepForeverThread(this);
this.width = GL11.getCanvasWidth(); this.width = GL11.getCanvasWidth();
this.height = GL11.getCanvasHeight(); this.height = GL11.getCanvasHeight();
} }
@ -107,14 +104,16 @@ public final class Minecraft implements Runnable {
this.currentScreen.onClose(); this.currentScreen.onClose();
} }
if(var1 == null && this.player.health <= 0) { if(var1 == null && level != null && player != null && this.player.health <= 0) {
var1 = new GameOverScreen(); var1 = new GameOverScreen();
} }
this.currentScreen = (GuiScreen)var1; this.currentScreen = (GuiScreen)var1;
if(var1 != null) { if(var1 != null) {
if(this.hasMouse) { if(this.hasMouse) {
this.player.releaseAllKeys(); if(level != null) {
this.player.releaseAllKeys();
}
this.hasMouse = false; this.hasMouse = false;
GL11.mouseSetGrabbed(false); GL11.mouseSetGrabbed(false);
} }
@ -168,18 +167,7 @@ public final class Minecraft implements Runnable {
Item.initModels(); Item.initModels();
Mob.modelCache = new ModelManager(); Mob.modelCache = new ModelManager();
GL11.glViewport(0, 0, this.width, this.height); GL11.glViewport(0, 0, this.width, this.height);
if(this.server != null) { this.setCurrentScreen(new MainMenu());
Level var85;
(var85 = new Level()).setData(8, 8, 8, new byte[512]);
this.setLevel(var85, false);
} else {
Level level1 = new LevelUtils().load();
if(level1 == null) {
this.generateLevel(1);
} else {
this.setLevel(level1, true);
}
}
this.particleManager = new ParticleManager(this.level); this.particleManager = new ParticleManager(this.level);
checkGLError("Post startup"); checkGLError("Post startup");
this.hud = new HUDScreen(this, this.width, this.height); this.hud = new HUDScreen(this, this.width, this.height);
@ -899,7 +887,7 @@ public final class Minecraft implements Runnable {
} }
public final void grabMouse() { public final void grabMouse() {
if(!GL11.isFocused()) { if(!GL11.isFocused() || level == null) {
return; return;
} }
if (GL11.isPointerLocked2()) { if (GL11.isPointerLocked2()) {
@ -924,7 +912,7 @@ public final class Minecraft implements Runnable {
} }
public final void pause() { public final void pause() {
if(this.currentScreen == null) { if(this.currentScreen == null && level != null) {
this.setCurrentScreen(new PauseScreen()); this.setCurrentScreen(new PauseScreen());
} }
} }
@ -1068,10 +1056,10 @@ public final class Minecraft implements Runnable {
NetworkManager var20 = this.networkManager; NetworkManager var20 = this.networkManager;
if(this.networkManager.successful) { if(this.networkManager.successful) {
NetworkHandler var18 = var20.netHandler; NetworkHandler var18 = var20.netHandler;
if(GL11.connectionOpen()) { if(var18.channel.connectionOpen()) {
try { try {
NetworkHandler var22 = var20.netHandler; NetworkHandler var22 = var20.netHandler;
var20.netHandler.read(var22.in); var20.netHandler.channel.read(var22.in);
var4 = 0; var4 = 0;
while(var22.in.position() > 0 && var4++ != 100) { while(var22.in.position() > 0 && var4++ != 100) {
@ -1115,8 +1103,18 @@ public final class Minecraft implements Runnable {
} catch (IOException var14) { } catch (IOException var14) {
var14.printStackTrace(); var14.printStackTrace();
} }
byte[] var51 = LevelIO.decompress(new ByteArrayInputStream(var42.levelData.toByteArray())); byte[] var51;
try {
DataInputStream var3;
byte[] var1 = new byte[(var3 = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(var42.levelData.toByteArray())))).readInt()];
var3.readFully(var1);
var3.close();
var51 = var1;
} catch (Exception var2) {
throw new RuntimeException(var2);
}
var42.levelData = null; var42.levelData = null;
short var55 = ((Short)var7[0]).shortValue(); short var55 = ((Short)var7[0]).shortValue();
short var63 = ((Short)var7[1]).shortValue(); short var63 = ((Short)var7[1]).shortValue();
@ -1264,7 +1262,7 @@ public final class Minecraft implements Runnable {
} }
} }
if(!GL11.connectionOpen()) { if(!var22.channel.connectionOpen()) {
break; break;
} }
@ -1273,7 +1271,7 @@ public final class Minecraft implements Runnable {
if(var22.out.position() > 0) { if(var22.out.position() > 0) {
var22.out.flip(); var22.out.flip();
var22.write(var22.out); var22.channel.write(var22.out);
var22.out.compact(); var22.out.compact();
} }
} catch (Exception var15) { } catch (Exception var15) {
@ -1352,7 +1350,7 @@ public final class Minecraft implements Runnable {
} }
while(GL11.keysNext()) { while(GL11.keysNext()) {
this.player.setKey(GL11.getEventKey(), GL11.getEventKeyState()); this.player.setKey(GL11.getEventKey(), GL11.getEventKeyState());
if(GL11.getEventKeyState()) { if(GL11.getEventKeyState()) {
if(this.currentScreen != null) { if(this.currentScreen != null) {
this.currentScreen.keyboardEvent(); this.currentScreen.keyboardEvent();
@ -1507,7 +1505,9 @@ public final class Minecraft implements Runnable {
this.particleManager.tick(); this.particleManager.tick();
} }
this.player.arrows = 1; if(player != null) {
this.player.arrows = 1;
}
} }
@ -1515,12 +1515,12 @@ public final class Minecraft implements Runnable {
private void levelSave() { private void levelSave() {
if(this.level == null) { if(this.level == null) {
ticksUntilSave = this.hud.ticks + 600; ticksUntilSave = this.ticks + 600;
} }
if(this.hud.ticks >= this.ticksUntilSave) { if(this.ticks >= this.ticksUntilSave) {
new LevelUtils().save(); new LevelUtils().save();
ticksUntilSave = this.hud.ticks + 600; ticksUntilSave = this.ticks + 600;
} }
} }

View File

@ -10,8 +10,6 @@ public final class SessionData {
public String username; public String username;
public String sessionId; public String sessionId;
public String mppass; public String mppass;
public boolean haspaid;
public SessionData(String var1, String var2) { public SessionData(String var1, String var2) {
this.username = var1; this.username = var1;

View File

@ -1,27 +0,0 @@
package com.mojang.minecraft;
public class SleepForeverThread extends Thread
{
public SleepForeverThread(Minecraft minecraft)
{
setDaemon(true);
start();
}
@Override
public void run()
{
while(true)
{
try {
while(true)
{
Thread.sleep(2147483647L);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

View File

@ -33,13 +33,16 @@ public final class HUDScreen extends Screen {
this.width = var2 * 240 / var3; this.width = var2 * 240 / var3;
this.height = var3 * 240 / var3; this.height = var3 * 240 / var3;
if(firstTimeLaunch) { if(firstTimeLaunch && Minecraft.getMinecraft().level != null) {
mc.setCurrentScreen(new PauseScreen()); mc.setCurrentScreen(new PauseScreen());
firstTimeLaunch = false; firstTimeLaunch = false;
} }
} }
public final void render(float var1, boolean var2, int var3, int var4) { public final void render(float var1, boolean var2, int var3, int var4) {
if(Minecraft.getMinecraft().level == null) {
return;
}
FontRenderer var5 = this.mc.fontRenderer; FontRenderer var5 = this.mc.fontRenderer;
this.mc.renderer.enableGuiMode(); this.mc.renderer.enableGuiMode();
new TextureLocation("/gui/gui.png").bindTexture(); new TextureLocation("/gui/gui.png").bindTexture();

View File

@ -0,0 +1,54 @@
package com.mojang.minecraft.gui;
import org.lwjgl.opengl.GL11;
import com.mojang.minecraft.Minecraft;
import com.mojang.minecraft.level.Level;
import com.mojang.minecraft.render.TextureLocation;
import net.PeytonPlayz585.level.LevelUtils;
import net.lax1dude.eaglercraft.adapter.Tessellator;
public class MainMenu extends GuiScreen {
public final void onOpen() {
this.buttons.clear();
this.buttons.add(new Button(1, this.width / 2 - 100, this.height / 4 + 48, "Singleplayer"));
this.buttons.add(new Button(2, this.width / 2 - 100, this.height / 4 + 72, "Multiplayer"));
}
protected final void onButtonClick(Button var1) {
if(var1.id == 1) {
Level level1 = new LevelUtils().load();
if(level1 == null) {
Minecraft.getMinecraft().setCurrentScreen(null);
this.minecraft.generateLevel(1);
this.minecraft.player.releaseAllKeys();
} else {
Minecraft.getMinecraft().setCurrentScreen(null);
this.minecraft.setLevel(level1, true);
this.minecraft.player.releaseAllKeys();
}
} else if(var1.id == 2) {
this.minecraft.setCurrentScreen(new MultiplayerMenu());
}
}
public final void render(int var1, int var2) {
int var4 = this.minecraft.width * 240 / this.minecraft.height;
int var5 = this.minecraft.height * 240 / this.minecraft.height;
GL11.glClear(16640);
Tessellator tessellator = Tessellator.instance;
int var7 = new TextureLocation("/dirt.png").bindTexture();
float var10 = 32.0F;
tessellator.startDrawing(7);
tessellator.setColorOpaque_I(4210752);
tessellator.addVertexWithUV(0.0F, (float)var5, 0.0F, 0.0F, (float)var5 / var10);
tessellator.addVertexWithUV((float)var4, (float)var5, 0.0F, (float)var4 / var10, (float)var5 / var10);
tessellator.addVertexWithUV((float)var4, 0.0F, 0.0F, (float)var4 / var10, 0.0F);
tessellator.addVertexWithUV(0.0F, 0.0F, 0.0F, 0.0F, 0.0F);
tessellator.draw();
super.render(var1, var2);
}
}

View File

@ -0,0 +1,137 @@
package com.mojang.minecraft.gui;
import org.lwjgl.opengl.GL11;
import com.mojang.minecraft.Minecraft;
import com.mojang.minecraft.SessionData;
import com.mojang.minecraft.level.Level;
import com.mojang.minecraft.net.NetworkManager;
import com.mojang.minecraft.render.TextureLocation;
import net.PeytonPlayz585.level.LevelUtils;
import net.lax1dude.eaglercraft.adapter.Tessellator;
public class MultiplayerMenu extends GuiScreen {
boolean textBox1Active = false;
boolean textBox2Active = false;
private int counter = 0;
String server = "";
String username = "";
Button connect;
public final void onOpen() {
GL11.enableRepeatEvents(true);
this.buttons.clear();
this.buttons.add(connect = new Button(0, this.width / 2 - 100, this.height / 4 + 96 + 12, "Connect"));
this.buttons.add(new Button(1, this.width / 2 - 100, this.height / 4 + 120 + 12, "Cancel"));
connect.active = false;
}
public final void tick() {
++this.counter;
}
protected final void onButtonClick(Button var1) {
if(var1.id == 0 && var1.active) {
GL11.enableRepeatEvents(false);
minecraft.session = new SessionData(this.username, "mcpass");
Level var85;
(var85 = new Level()).setData(8, 8, 8, new byte[512]);
minecraft.setLevel(var85, false);
minecraft.networkManager = new NetworkManager(minecraft, this.server, minecraft.session.username, minecraft.session.mppass);
} else if(var1.id == 1) {
GL11.enableRepeatEvents(false);
minecraft.setCurrentScreen(new MainMenu());
}
}
protected void onMouseClick(int var1, int var2, int var3) {
if(var3 == 0) {
if(var1 >= this.width / 2 - 100 && var1 < (this.width / 2 - 100) + 200 && var2 >= this.height / 4 - 10 + 50 + 18 && var2 < (this.height / 4 - 10 + 50 + 18) + 20) {
GL11.enableRepeatEvents(true);
textBox1Active = true;
textBox2Active = false;
} else if(var1 >= this.width / 2 - 100 && var1 < (this.width / 2 - 100) + 200 && var2 >= this.height / 4 - 10 + 50 - 20 && var2 < (this.height / 4 - 10 + 50 - 20) + 20) {
GL11.enableRepeatEvents(true);
textBox2Active = true;
textBox1Active = false;
} else {
GL11.enableRepeatEvents(false);
textBox1Active = false;
textBox2Active = false;
}
}
super.onMouseClick(var1, var2, var3);
}
protected final void onKeyPress(char var1, int var2) {
if(textBox1Active) {
if(var2 == 14 && this.server.length() > 0) {
this.server = this.server.substring(0, this.server.length() - 1);
}
if("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ,.:-_\'*!\\\"#%/()=+?[]{}<>@|$;".indexOf(var1) >= 0 && this.server.length() < 32) {
this.server = this.server + var1;
}
if(server.length() > 0 && username.length() > 0) {
connect.active = true;
} else {
connect.active = false;
}
} else if(textBox2Active) {
if(var2 == 14 && this.username.length() > 0) {
this.username = this.username.substring(0, this.username.length() - 1);
}
if("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ,.:-_\'*!\\\"#%/()=+?[]{}<>@|$;".indexOf(var1) >= 0 && this.username.length() < 32) {
this.username = this.username + var1;
}
if(server.length() > 0 && username.length() > 0) {
connect.active = true;
} else {
connect.active = false;
}
}
}
public final void render(int var1, int var2) {
int var4 = this.minecraft.width * 240 / this.minecraft.height;
int var5 = this.minecraft.height * 240 / this.minecraft.height;
GL11.glClear(16640);
Tessellator tessellator = Tessellator.instance;
int var7 = new TextureLocation("/dirt.png").bindTexture();
float var10 = 32.0F;
tessellator.startDrawing(7);
tessellator.setColorOpaque_I(4210752);
tessellator.addVertexWithUV(0.0F, (float)var5, 0.0F, 0.0F, (float)var5 / var10);
tessellator.addVertexWithUV((float)var4, (float)var5, 0.0F, (float)var4 / var10, (float)var5 / var10);
tessellator.addVertexWithUV((float)var4, 0.0F, 0.0F, (float)var4 / var10, 0.0F);
tessellator.addVertexWithUV(0.0F, 0.0F, 0.0F, 0.0F, 0.0F);
tessellator.draw();
//username
drawBox((this.width / 2 - 100) - 1, (this.height / 4 - 10 + 50 - 20) - 1, (this.width / 2 - 100) + 200 + 1, (this.height / 4 - 10 + 50 - 20) + 20 + 1, -6250336);
drawBox(this.width / 2 - 100, this.height / 4 - 10 + 50 - 20, (this.width / 2 - 100) + 200, (this.height / 4 - 10 + 50 - 20) + 20, -16777216);
this.drawString(this.fontRenderer, "Username:", this.width / 2 - 100, this.height / 4 - 10 + 50 - 30, 10526880);
//server IP
drawBox((this.width / 2 - 100) - 1, (this.height / 4 - 10 + 50 + 18) - 1, (this.width / 2 - 100) + 200 + 1, (this.height / 4 - 10 + 50 + 18) + 20 + 1, -6250336);
drawBox(this.width / 2 - 100, this.height / 4 - 10 + 50 + 18, (this.width / 2 - 100) + 200, (this.height / 4 - 10 + 50 + 18) + 20, -16777216);
this.drawString(this.fontRenderer, "Server address:", this.width / 2 - 100, this.height / 4 - 10 + 50 + 8, 10526880);
if(textBox1Active) {
boolean e = this.counter / 6 % 2 == 0;
this.drawString(this.fontRenderer, server + (e ? "_" : ""), (this.width / 2 - 100) + 4, (this.height / 4 - 10 + 50 + 18) + (20 - 8) / 2, 14737632);
} else {
this.drawString(this.fontRenderer, server, (this.width / 2 - 100) + 4, (this.height / 4 - 10 + 50 + 18) + (20 - 8) / 2, 14737632);
}
if(textBox2Active) {
boolean e = this.counter / 6 % 2 == 0;
this.drawString(this.fontRenderer, username + (e ? "_" : ""), (this.width / 2 - 100) + 4, (this.height / 4 - 10 + 50 - 20) + (20 - 8) / 2, 14737632);
} else {
this.drawString(this.fontRenderer, username, (this.width / 2 - 100) + 4, (this.height / 4 - 10 + 50 - 20) + (20 - 8) / 2, 14737632);
}
super.render(var1, var2);
}
}

View File

@ -1,258 +0,0 @@
package com.mojang.minecraft.level;
import com.mojang.minecraft.ProgressBarDisplay;
import com.mojang.minecraft.level.Level;
import com.mojang.minecraft.level.LevelObjectInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public final class LevelIO {
private ProgressBarDisplay progressBar;
public LevelIO(ProgressBarDisplay var1) {
this.progressBar = var1;
}
public final boolean save(Level var1, File var2) {
try {
FileOutputStream var5 = new FileOutputStream(var2);
save(var1, (OutputStream)var5);
var5.close();
return true;
} catch (Exception var4) {
var4.printStackTrace();
if(this.progressBar != null) {
this.progressBar.setText("Failed!");
}
try {
Thread.sleep(1000L);
} catch (InterruptedException var3) {
;
}
return false;
}
}
public final Level load(File var1) {
try {
FileInputStream var5 = new FileInputStream(var1);
Level var2 = this.load((InputStream)var5);
var5.close();
return var2;
} catch (Exception var4) {
var4.printStackTrace();
if(this.progressBar != null) {
this.progressBar.setText("Failed!");
}
try {
Thread.sleep(1000L);
} catch (InterruptedException var3) {
;
}
return null;
}
}
public final boolean saveOnline(Level var1, String var2, String var3, String var4, String var5, int var6) {
if(var4 == null) {
var4 = "";
}
if(this.progressBar != null && this.progressBar != null) {
this.progressBar.setTitle("Saving level");
}
try {
if(this.progressBar != null && this.progressBar != null) {
this.progressBar.setText("Compressing..");
}
ByteArrayOutputStream var7 = new ByteArrayOutputStream();
save(var1, (OutputStream)var7);
var7.close();
byte[] var10 = var7.toByteArray();
if(this.progressBar != null && this.progressBar != null) {
this.progressBar.setText("Connecting..");
}
HttpURLConnection var12;
(var12 = (HttpURLConnection)(new URL("http://" + var2 + "/level/save.html")).openConnection()).setDoInput(true);
var12.setDoOutput(true);
var12.setRequestMethod("POST");
DataOutputStream var13;
(var13 = new DataOutputStream(var12.getOutputStream())).writeUTF(var3);
var13.writeUTF(var4);
var13.writeUTF(var5);
var13.writeByte(var6);
var13.writeInt(var10.length);
if(this.progressBar != null) {
this.progressBar.setText("Saving..");
}
var13.write(var10);
var13.close();
BufferedReader var11;
if(!(var11 = new BufferedReader(new InputStreamReader(var12.getInputStream()))).readLine().equalsIgnoreCase("ok")) {
if(this.progressBar != null) {
this.progressBar.setText("Failed: " + var11.readLine());
}
var11.close();
Thread.sleep(1000L);
return false;
} else {
var11.close();
return true;
}
} catch (Exception var9) {
var9.printStackTrace();
if(this.progressBar != null) {
this.progressBar.setText("Failed!");
}
try {
Thread.sleep(1000L);
} catch (InterruptedException var8) {
;
}
return false;
}
}
public final Level loadOnline(String var1, String var2, int var3) {
if(this.progressBar != null) {
this.progressBar.setTitle("Loading level");
}
try {
if(this.progressBar != null) {
this.progressBar.setText("Connecting..");
}
HttpURLConnection var6;
(var6 = (HttpURLConnection)(new URL("http://" + var1 + "/level/load.html?id=" + var3 + "&user=" + var2)).openConnection()).setDoInput(true);
if(this.progressBar != null) {
this.progressBar.setText("Loading..");
}
DataInputStream var7;
if((var7 = new DataInputStream(var6.getInputStream())).readUTF().equalsIgnoreCase("ok")) {
return this.load((InputStream)var7);
} else {
if(this.progressBar != null) {
this.progressBar.setText("Failed: " + var7.readUTF());
}
var7.close();
Thread.sleep(1000L);
return null;
}
} catch (Exception var5) {
var5.printStackTrace();
if(this.progressBar != null) {
this.progressBar.setText("Failed!");
}
try {
Thread.sleep(3000L);
} catch (InterruptedException var4) {
;
}
return null;
}
}
public final Level load(InputStream var1) {
if(this.progressBar != null) {
this.progressBar.setTitle("Loading level");
}
if(this.progressBar != null) {
this.progressBar.setText("Reading..");
}
try {
DataInputStream var10;
if((var10 = new DataInputStream(new GZIPInputStream(var1))).readInt() != 656127880) {
return null;
} else {
byte var12;
if((var12 = var10.readByte()) > 2) {
return null;
} else if(var12 <= 1) {
String var14 = var10.readUTF();
String var15 = var10.readUTF();
long var3 = var10.readLong();
short var5 = var10.readShort();
short var6 = var10.readShort();
short var7 = var10.readShort();
byte[] var8 = new byte[var5 * var6 * var7];
var10.readFully(var8);
var10.close();
Level var11;
(var11 = new Level()).setData(var5, var7, var6, var8);
var11.name = var14;
var11.creator = var15;
var11.createTime = var3;
return var11;
} else {
Level var2;
LevelObjectInputStream var13;
(var2 = (Level)(var13 = new LevelObjectInputStream(var10)).readObject()).initTransient();
var13.close();
return var2;
}
}
} catch (Exception var9) {
var9.printStackTrace();
System.out.println("Failed to load level: " + var9.toString());
return null;
}
}
public static void save(Level var0, OutputStream var1) {
try {
DataOutputStream var3;
(var3 = new DataOutputStream(new GZIPOutputStream(var1))).writeInt(656127880);
var3.writeByte(2);
ObjectOutputStream var4;
(var4 = new ObjectOutputStream(var3)).writeObject(var0);
var4.close();
} catch (Exception var2) {
var2.printStackTrace();
}
}
public static byte[] decompress(InputStream var0) {
try {
DataInputStream var3;
byte[] var1 = new byte[(var3 = new DataInputStream(new GZIPInputStream(var0))).readInt()];
var3.readFully(var1);
var3.close();
return var1;
} catch (Exception var2) {
throw new RuntimeException(var2);
}
}
}

View File

@ -21,7 +21,7 @@ public class NetworkManager
players = new HashMap<Byte, NetworkPlayer>(); players = new HashMap<Byte, NetworkPlayer>();
new ServerConnectThread(this, server, username, key, minecraft).start(); new ServerConnect(this, server, username, key, minecraft).connect();
} }
public ByteArrayOutputStream levelData; public ByteArrayOutputStream levelData;
@ -53,7 +53,7 @@ public class NetworkManager
public boolean isConnected() public boolean isConnected()
{ {
return netHandler != null && GL11.connectionOpen(); return netHandler != null && netHandler.channel.connectionOpen();
} }
public List getPlayers() public List getPlayers()

View File

@ -4,9 +4,9 @@ import com.mojang.minecraft.Minecraft;
import com.mojang.minecraft.gui.ErrorScreen; import com.mojang.minecraft.gui.ErrorScreen;
import com.mojang.net.NetworkHandler; import com.mojang.net.NetworkHandler;
public class ServerConnectThread extends Thread public class ServerConnect
{ {
public ServerConnectThread(NetworkManager networkManager, String server, String username, String key, Minecraft minecraft) { public ServerConnect(NetworkManager networkManager, String server, String username, String key, Minecraft minecraft) {
super(); super();
netManager = networkManager; netManager = networkManager;
@ -19,8 +19,7 @@ public class ServerConnectThread extends Thread
this.minecraft = minecraft; this.minecraft = minecraft;
} }
@Override public void connect()
public void run()
{ {
try { try {
netManager.netHandler = new NetworkHandler(server); netManager.netHandler = new NetworkHandler(server);

View File

@ -6,6 +6,8 @@ import com.mojang.minecraft.gui.ErrorScreen;
import com.mojang.minecraft.net.NetworkManager; import com.mojang.minecraft.net.NetworkManager;
import com.mojang.minecraft.net.PacketType; import com.mojang.minecraft.net.PacketType;
import net.PeytonPlayz585.websocket.WebSocketChannel;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Arrays; import java.util.Arrays;
@ -20,6 +22,7 @@ public final class NetworkHandler {
public NetworkManager netManager; public NetworkManager netManager;
private boolean unused = false; private boolean unused = false;
private byte[] stringBytes = new byte[64]; private byte[] stringBytes = new byte[64];
public WebSocketChannel channel;
public NetworkHandler(String var1) { public NetworkHandler(String var1) {
@ -43,18 +46,14 @@ public final class NetworkHandler {
Minecraft.getMinecraft().setCurrentScreen(new ErrorScreen(":(", "Invalid URI protocol!")); Minecraft.getMinecraft().setCurrentScreen(new ErrorScreen(":(", "Invalid URI protocol!"));
} }
if(!GL11.startConnection(address)) { channel = new WebSocketChannel(address);
if(!GL11.startConnection(address)) {
Minecraft.getMinecraft().setCurrentScreen(new ErrorScreen(address, "Failed to connect to server!"));
}
}
} }
public final void close() { public final void close() {
try { try {
if(this.out.position() > 0) { if(this.out.position() > 0) {
this.out.flip(); this.out.flip();
this.write(this.out); this.channel.write(this.out);
this.out.compact(); this.out.compact();
} }
} catch (Exception var2) { } catch (Exception var2) {
@ -62,14 +61,14 @@ public final class NetworkHandler {
} }
try { try {
GL11.endConnection(); channel.endConnection();
} catch (Exception var1) { } catch (Exception var1) {
; ;
} }
} }
public final void send(PacketType var1, Object ... var2) { public final void send(PacketType var1, Object ... var2) {
if(GL11.connectionOpen()) { if(channel.connectionOpen()) {
this.out.put(var1.opcode); this.out.put(var1.opcode);
for(int var3 = 0; var3 < var2.length; ++var3) { for(int var3 = 0; var3 < var2.length; ++var3) {
@ -77,7 +76,7 @@ public final class NetworkHandler {
Object var4 = var2[var3]; Object var4 = var2[var3];
Class<?> var5 = var10001; Class<?> var5 = var10001;
NetworkHandler var6 = this; NetworkHandler var6 = this;
if(GL11.connectionOpen()) { if(channel.connectionOpen()) {
try { try {
if(var5 == Long.TYPE) { if(var5 == Long.TYPE) {
var6.out.putLong(((Long)var4).longValue()); var6.out.putLong(((Long)var4).longValue());
@ -129,7 +128,7 @@ public final class NetworkHandler {
} }
public Object readObject(Class var1) { public Object readObject(Class var1) {
if(!GL11.connectionOpen()) { if(!channel.connectionOpen()) {
return null; return null;
} else { } else {
try { try {
@ -162,49 +161,6 @@ public final class NetworkHandler {
} }
} }
public void read(ByteBuffer buf) {
int bytesRead = 0;
while (bytesRead < buf.capacity()) {
if (!GL11.receivedBuffers.isEmpty()) {
ByteBuffer receivedBuffer = GL11.receivedBuffers.peek();
int remainingBytes = buf.capacity() - bytesRead;
int bytesToRead = Math.min(receivedBuffer.remaining(), remainingBytes);
receivedBuffer.get(buf.array(), bytesRead, bytesToRead);
bytesRead += bytesToRead;
if (receivedBuffer.remaining() == 0) {
GL11.receivedBuffers.poll();
}
} else {
break;
}
}
}
private static ByteBuffer writeBuffer;
public void write(ByteBuffer buf) {
if (writeBuffer == null) {
writeBuffer = ByteBuffer.allocate(buf.capacity());
}
int bytesToWrite = Math.min(buf.remaining(), writeBuffer.remaining());
writeBuffer.put(buf.array(), buf.position(), bytesToWrite);
buf.position(buf.position() + bytesToWrite);
if (writeBuffer.remaining() == 0 || buf.remaining() == 0) {
writeBuffer.flip();
byte[] data = new byte[writeBuffer.remaining()];
writeBuffer.get(data);
GL11.writePacket(data);
writeBuffer.clear();
}
if (buf.remaining() > 0) {
write(buf);
}
}
@JSBody(params = { }, script = "return window.location.href;") @JSBody(params = { }, script = "return window.location.href;")
private static native String getLocationString(); private static native String getLocationString();

View File

@ -109,7 +109,7 @@ public class LevelUtils {
public void save() { public void save() {
if(Minecraft.getMinecraft().networkManager != null) { if(Minecraft.getMinecraft().server != null || Minecraft.getMinecraft().networkManager != null || Minecraft.getMinecraft().level == null) {
return; return;
} }

View File

@ -0,0 +1,102 @@
package net.PeytonPlayz585.websocket;
import org.teavm.jso.JSBody;
import org.teavm.jso.ajax.*;
import org.teavm.jso.dom.events.MessageEvent;
import org.teavm.jso.typedarrays.ArrayBuffer;
import org.teavm.jso.typedarrays.DataView;
import org.teavm.jso.typedarrays.Int8Array;
import org.teavm.jso.websocket.WebSocket;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.Queue;
public class WebSocketChannel {
private final WebSocket socket;
private final Queue<MessageEvent> messageQueue;
private final ByteBuffer readBuffer;
private boolean reading;
private boolean isConnected = false;
public WebSocketChannel(String url) {
socket = WebSocket.create(url);
messageQueue = new LinkedList<>();
readBuffer = ByteBuffer.allocate(4096);
reading = false;
socket.onMessage(event -> {
isConnected = true;
messageQueue.offer(event);
});
socket.onOpen(event -> {
isConnected = true;
messageQueue.clear();
});
socket.onClose((event) -> {
isConnected = false;
messageQueue.clear();
});
socket.onError((event) -> {
isConnected = false;
socket.close();
});
}
public void read(ByteBuffer buffer) {
if (reading || messageQueue.isEmpty()) {
return;
}
MessageEvent event = messageQueue.peek();
readData(buffer, event);
if (!event.isBubbles() && !event.isCancelable()) {
messageQueue.poll();
}
}
private void readData(ByteBuffer buffer, MessageEvent event) {
DataView data = DataView.create(event.getDataAsArray());
int length = Math.min(buffer.remaining(), data.getByteLength());
for (int i = 0; i < length; i++) {
buffer.put((byte) data.getInt8(i));
}
if (reading) {
return;
}
if (buffer.hasRemaining()) {
readBuffer.clear();
readBuffer.put(buffer);
reading = true;
} else {
buffer.flip();
}
}
@JSBody(params = { "sock", "buffer" }, script = "sock.send(buffer);")
private static native void nativeBinarySend(WebSocket sock, ArrayBuffer buffer);
public void write(ByteBuffer buffer) {
if (socket.getReadyState() == 3) {
return;
}
byte[] data = new byte[buffer.remaining()];
buffer.get(data);
Int8Array array = Int8Array.create(data.length);
for (int i = 0; i < data.length; i++) {
array.set(i, data[i]);
}
nativeBinarySend(socket, array.getBuffer());
}
public boolean connectionOpen() {
return isConnected && socket.getReadyState() != 3;
}
public void endConnection() {
socket.close();
}
}

File diff suppressed because one or more lines are too long

View File

@ -1201,61 +1201,6 @@ public class EaglerAdapterImpl2 {
@JSBody(params = { "name", "cvs" }, script = "var a=document.createElement(\"a\");a.href=cvs.toDataURL(\"image/png\");a.download=name;a.click();") @JSBody(params = { "name", "cvs" }, script = "var a=document.createElement(\"a\");a.href=cvs.toDataURL(\"image/png\");a.download=name;a.click();")
private static native void saveScreenshot(String name, HTMLCanvasElement cvs); private static native void saveScreenshot(String name, HTMLCanvasElement cvs);
private static WebSocket webSocket;
private static boolean isConnected = false;
public static java.util.Queue<ByteBuffer> receivedBuffers = new java.util.ArrayDeque<ByteBuffer>();
public static boolean startConnection(String serverUrl) {
webSocket = WebSocket.create(serverUrl);
webSocket.onOpen(new EventListener<MessageEvent>() {
@Override
public void handleEvent(MessageEvent evt) {
isConnected = true;
}
});
webSocket.onMessage(new EventListener<MessageEvent>() {
@Override
public void handleEvent(MessageEvent evt) {
Uint8Array a = Uint8Array.create(evt.getDataAsArray());
byte[] b = new byte[a.getByteLength()];
for(int i = 0; i < b.length; ++i) {
b[i] = (byte) (a.get(i) & 0xFF);
}
ByteBuffer buffer = ByteBuffer.wrap(b);
receivedBuffers.add(buffer);
}
});
webSocket.onClose(new EventListener<CloseEvent>() {
@Override
public void handleEvent(CloseEvent event) {
isConnected = false;
}
});
return isConnected;
}
public static void endConnection() {
webSocket.close();
}
public static final boolean connectionOpen() {
return isConnected;
}
@JSBody(params = { "sock", "buffer" }, script = "sock.send(buffer);")
private static native void nativeBinarySend(WebSocket sock, ArrayBuffer buffer);
public static final void writePacket(byte[] packet) {
if(webSocket != null && isConnected) {
Uint8Array arr = Uint8Array.create(packet.length);
arr.set(packet);
nativeBinarySend(webSocket, arr.getBuffer());
}
}
public static final byte[] loadLocalStorage(String key) { public static final byte[] loadLocalStorage(String key) {
String s = win.getLocalStorage().getItem("_eaglercraft_beta."+key); String s = win.getLocalStorage().getItem("_eaglercraft_beta."+key);
if(s != null) { if(s != null) {