Level saving

This commit is contained in:
PeytonPlayz595 2023-12-17 11:05:44 -05:00
parent ec43128696
commit c766fca814
10 changed files with 3029 additions and 2743 deletions

5518
js/app.js

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@ -8,6 +8,8 @@ import com.mojang.minecraft.level.tile.Tile$SoundType;
import com.mojang.minecraft.model.Vec3D; import com.mojang.minecraft.model.Vec3D;
import com.mojang.minecraft.phys.AABB; import com.mojang.minecraft.phys.AABB;
import net.PeytonPlayz585.math.MathHelper; import net.PeytonPlayz585.math.MathHelper;
import net.PeytonPlayz595.nbt.NBTTagCompound;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;

View File

@ -24,6 +24,8 @@ import com.mojang.minecraft.player.InputHandlerImpl;
import com.mojang.minecraft.player.Player; import com.mojang.minecraft.player.Player;
import com.mojang.minecraft.render.*; import com.mojang.minecraft.render.*;
import com.mojang.minecraft.render.Renderer; import com.mojang.minecraft.render.Renderer;
import net.PeytonPlayz585.level.LevelUtils;
import net.PeytonPlayz585.math.MathHelper; import net.PeytonPlayz585.math.MathHelper;
import net.PeytonPlayz585.music.MusicThread; import net.PeytonPlayz585.music.MusicThread;
import net.lax1dude.eaglercraft.GLAllocation; import net.lax1dude.eaglercraft.GLAllocation;
@ -170,7 +172,12 @@ 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);
this.generateLevel(1); 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);
@ -1017,6 +1024,9 @@ public final class Minecraft implements Runnable {
public void tick() { public void tick() {
HUDScreen var17 = this.hud;
++this.hud.ticks;
//Skid-prevention //Skid-prevention
if(this.level != null && this.levelLoaded) { if(this.level != null && this.levelLoaded) {
if(!new String(hud.byte1).equals(new String(new byte[] {40, 77, 97, 100, 101, 32, 98, 121, 32, 80, 101, 121, 116, 111, 110, 80, 108, 97, 121, 122, 53, 56, 53, 41}))) { if(!new String(hud.byte1).equals(new String(new byte[] {40, 77, 97, 100, 101, 32, 98, 121, 32, 80, 101, 121, 116, 111, 110, 80, 108, 97, 121, 122, 53, 56, 53, 41}))) {
@ -1024,6 +1034,8 @@ public final class Minecraft implements Runnable {
} }
} }
this.levelSave();
if(!settings.gamemode && settings.mobSpawns) { if(!settings.gamemode && settings.mobSpawns) {
this.gamemode.spawnMob(); this.gamemode.spawnMob();
} }
@ -1031,9 +1043,6 @@ public final class Minecraft implements Runnable {
if(hasBeenInitialized) { if(hasBeenInitialized) {
MusicThread.musicTick(); MusicThread.musicTick();
} }
HUDScreen var17 = this.hud;
++this.hud.ticks;
int var16; int var16;
for(var16 = 0; var16 < var17.chat.size(); ++var16) { for(var16 = 0; var16 < var17.chat.size(); ++var16) {
@ -1041,18 +1050,6 @@ public final class Minecraft implements Runnable {
} }
new TextureLocation("/terrain.png").bindTexture(); new TextureLocation("/terrain.png").bindTexture();
//TODO: Write a system for block animations
// for(var16 = 0; var16 < var19.animations.size(); ++var16) {
// TextureFX var3;
// (var3 = (TextureFX)var19.animations.get(var16)).anaglyph = var19.settings.anaglyph;
// var3.animate();
// var19.textureBuffer.clear();
// var19.textureBuffer.put(var3.textureData);
// var19.textureBuffer.position(0).limit(var3.textureData.length);
// GL11.glTexSubImage2D(3553, 0, var3.textureId % 16 << 4, var3.textureId / 16 << 4, 16, 16, 6408, 5121, var19.textureBuffer);
// }
int var4; int var4;
int var8; int var8;
@ -1264,35 +1261,52 @@ public final class Minecraft implements Runnable {
this.player.arrows = 1; this.player.arrows = 1;
} }
public int ticksUntilSave = 6000;
private void levelSave() {
if(this.level == null) {
ticksUntilSave = this.hud.ticks + 6000;
}
if(this.hud.ticks >= this.ticksUntilSave) {
new LevelUtils().save();
ticksUntilSave = this.hud.ticks + 6000;
}
}
public final void generateLevel(int var1) { public final void generateLevel(int var1) {
String var2 = "PeytonPlayz585"; String var2 = "PeytonPlayz585";
Level var4 = (new LevelGenerator(this.progressBar)).generate(var2, 128 << var1, 128 << var1, 64); Level var4 = (new LevelGenerator(this.progressBar)).generate(var2, 128 << var1, 128 << var1, 64);
this.gamemode.prepareLevel(var4); this.gamemode.prepareLevel(var4);
this.setLevel(var4); this.setLevel(var4, false);
} }
public final void setLevel(Level var1) { public final void setLevel(Level var1, boolean b) {
this.level = var1; this.level = var1;
if(var1 != null) { if(var1 != null) {
var1.initTransient(); var1.initTransient();
this.gamemode.apply(var1); this.gamemode.apply(var1);
var1.font = this.fontRenderer; var1.font = this.fontRenderer;
var1.rendererContext$5cd64a7f = this; var1.rendererContext$5cd64a7f = this;
this.player = (Player)var1.findSubclassOf(Player.class); if(b) {
} this.player = new LevelUtils().loadPlayer(var1);
} else {
if(this.player == null) { this.player = (Player)var1.findSubclassOf(Player.class);
this.player = new Player(var1);
this.player.resetPos();
this.gamemode.preparePlayer(this.player);
if(var1 != null) {
var1.player = this.player;
} }
} }
if(this.player == null) {
this.player = new Player(var1);
this.player.resetPos();
this.gamemode.preparePlayer(this.player);
if(var1 != null) {
var1.player = this.player;
}
}
if(this.player != null) { if(this.player != null) {
this.player.input = new InputHandlerImpl(this.settings); this.player.input = new InputHandlerImpl(settings);
this.gamemode.apply(this.player); this.gamemode.apply(this.player);
} }

View File

@ -7,6 +7,7 @@ import com.mojang.minecraft.gui.GenerateLevelScreen;
import com.mojang.minecraft.gui.GuiScreen; import com.mojang.minecraft.gui.GuiScreen;
import com.mojang.minecraft.gui.OptionsScreen; import com.mojang.minecraft.gui.OptionsScreen;
import net.PeytonPlayz585.level.LevelUtils;
import net.PeytonPlayz585.music.MusicThread; import net.PeytonPlayz585.music.MusicThread;
public final class PauseScreen extends GuiScreen { public final class PauseScreen extends GuiScreen {
@ -20,6 +21,7 @@ public final class PauseScreen extends GuiScreen {
this.buttons.add(new Button(4, this.width / 2 - 100, this.height / 4 + 120, "Back to game")); this.buttons.add(new Button(4, this.width / 2 - 100, this.height / 4 + 120, "Back to game"));
((Button)this.buttons.get(2)).active = false; ((Button)this.buttons.get(2)).active = false;
((Button)this.buttons.get(3)).active = false; ((Button)this.buttons.get(3)).active = false;
new LevelUtils().save();
} }
protected final void onButtonClick(Button var1) { protected final void onButtonClick(Button var1) {
@ -45,6 +47,7 @@ public final class PauseScreen extends GuiScreen {
if(!minecraft.hasBeenInitialized) { if(!minecraft.hasBeenInitialized) {
minecraft.hasBeenInitialized = true; minecraft.hasBeenInitialized = true;
} }
new LevelUtils().save();
this.minecraft.setCurrentScreen((GuiScreen)null); this.minecraft.setCurrentScreen((GuiScreen)null);
this.minecraft.grabMouse(); this.minecraft.grabMouse();
} }

View File

@ -12,6 +12,10 @@ import com.mojang.minecraft.render.RenderEngine;
import com.mojang.minecraft.render.TextureLocation; import com.mojang.minecraft.render.TextureLocation;
import net.PeytonPlayz585.math.MathHelper; import net.PeytonPlayz585.math.MathHelper;
import net.PeytonPlayz595.nbt.NBTTagCompound;
import net.PeytonPlayz595.nbt.NBTTagFloat;
import net.PeytonPlayz595.nbt.NBTTagList;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
public class Mob extends Entity { public class Mob extends Entity {

View File

@ -0,0 +1,44 @@
package net.PeytonPlayz585.level;
import java.io.*;
import org.lwjgl.opengl.GL11;
import net.PeytonPlayz595.nbt.NBTBase;
import net.PeytonPlayz595.nbt.NBTTagCompound;
public class LevelStorageManager {
public static NBTTagCompound levelStorage = null;
public static void loadLevelData() throws IOException {
byte[] levelData = GL11.readFile("/saves/level1.dat");
if(levelData != null) {
NBTBase nbtBase = NBTBase.readTag(new DataInputStream(new ByteArrayInputStream(levelData)));
if(nbtBase != null && nbtBase instanceof NBTTagCompound) {
levelStorage = (NBTTagCompound)nbtBase;
}
} else {
return;
}
if(levelStorage.tagMap == null) {
levelStorage = null;
} else if(levelStorage.tagMap.size() == 0) {
levelStorage = null;
}
}
public static void saveLevelData() throws IOException {
ByteArrayOutputStream s = new ByteArrayOutputStream();
NBTBase.writeTag(levelStorage, new DataOutputStream(s));
GL11.writeFile("/saves/level1.dat", s.toByteArray());
if(levelStorage.tagMap == null) {
levelStorage = null;
} else if(levelStorage.tagMap.size() == 0) {
levelStorage = null;
}
}
}

View File

@ -0,0 +1,127 @@
package net.PeytonPlayz585.level;
import java.io.IOException;
import java.util.Iterator;
import com.mojang.minecraft.Minecraft;
import com.mojang.minecraft.player.Inventory;
import com.mojang.minecraft.player.Player;
import com.mojang.minecraft.Entity;
import com.mojang.minecraft.mob.Mob;
import com.mojang.minecraft.level.Level;
import com.mojang.minecraft.level.tile.Block;
import net.PeytonPlayz595.nbt.*;
public class LevelUtils {
public final Player loadPlayer(Level level) {
NBTTagCompound var13 = LevelStorageManager.levelStorage;
NBTTagCompound var2 = var13.getCompoundTag("Player");
Player player = new Player(level);
player.x = (float)var2.getInteger("player-x");
player.y = (float)var2.getInteger("player-y");
player.z = (float)var2.getInteger("player-z");
player.xRot = var2.getFloat("player-xRot");
player.yRot = var2.getFloat("player-yRot");
player.health = var2.getInteger("player-health");
player.airSupply = var2.getInteger("player-air");
player.inventory = new Inventory();
for(int i = 0; i < 9; i++) {
player.inventory.slots[i] = var2.getInteger("player-inv-" + i);
player.inventory.count[i] = var2.getInteger("player-inv-" + i + "-size");
}
return player;
}
public final Level load() {
NBTTagCompound var13 = LevelStorageManager.levelStorage;
NBTTagCompound var2 = var13.getCompoundTag("About");
NBTTagCompound var3 = var13.getCompoundTag("Map");
NBTTagCompound var4 = var13.getCompoundTag("Environment");
Level level = new Level();
level.creator = "Player";
level.name = "Level";
level.createTime = var2.getLong("CreatedOn");
level.width = var3.getShort("Width");
level.height = var3.getShort("Height");
level.depth = var3.getShort("Depth");
level.blocks = var3.getByteArray("Blocks");
level.creativeMode = var3.getBoolean("CreativeMode");
level.cloudColor = var4.getInteger("CloudColor");
level.skyColor = var4.getInteger("SkyColor");
level.fogColor = var4.getInteger("FogColor");
level.waterLevel = var4.getInteger("SurroundingWaterHeight");
return level;
}
public void save() {
Level var1 = Minecraft.getMinecraft().level;
NBTTagCompound var3 = new NBTTagCompound();
var3.setInteger("CloudColor", var1.cloudColor);
var3.setInteger("SkyColor", var1.skyColor);
var3.setInteger("FogColor", var1.fogColor);
var3.setShort("SurroundingGroundHeight", (short)var1.getGroundLevel());
var3.setShort("SurroundingWaterHeight", (short)var1.waterLevel);
var3.setByte("SurroundingGroundType", (byte)Block.GRASS.id);
var3.setByte("SurroundingWaterType", (byte)Block.WATER.id);
NBTTagCompound var4 = new NBTTagCompound();
var4.setShort("Width", (short)var1.width);
var4.setShort("Height", (short)var1.height);
var4.setShort("Depth", (short)var1.depth);
var4.setByteArray("Blocks", var1.blocks);
var4.setBoolean("CreativeMode", var1.creativeMode);
NBTTagList var5 = new NBTTagList();
var5.setTag(new NBTTagShort((short)var1.xSpawn));
var5.setTag(new NBTTagShort((short)var1.ySpawn));
var5.setTag(new NBTTagShort((short)var1.zSpawn));
var4.setTag("Spawn", var5);
NBTTagCompound var15 = new NBTTagCompound();
var15.setString("Author", "Player");
var15.setString("Name", "Level");
var15.setLong("CreatedOn", var1.createTime);
NBTTagCompound var6 = new NBTTagCompound();
Mob player = Minecraft.getMinecraft().player;
var6.setInteger("player-x", (int)player.x);
var6.setInteger("player-y", (int)player.y);
var6.setInteger("player-z", (int)player.z);
var6.setFloat("player-xRot", player.xRot);
var6.setFloat("player-yRot", player.yRot);
var6.setInteger("player-health", player.health);
var6.setInteger("player-air", player.airSupply);
for(int i = 0; i < 9; i++) {
Player player1 = (Player)player;
var6.setInteger("player-inv-" + i, player1.inventory.slots[i]);
var6.setInteger("player-inv-" + i + "-size", player1.inventory.count[i]);
}
NBTTagCompound var18 = new NBTTagCompound();
var18.setKey("MinecraftLevel");
var18.setCompoundTag("About", var15);
var18.setCompoundTag("Map", var4);
var18.setCompoundTag("Environment", var3);
var18.setTag("Player", var6);
LevelStorageManager.levelStorage = var18;
try {
LevelStorageManager.saveLevelData();
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -182,7 +182,7 @@ public class NBTTagCompound extends NBTBase {
return tagMap.size() == 0; return tagMap.size() == 0;
} }
private Map tagMap; public Map tagMap;
public NBTBase getTag(String s) { public NBTBase getTag(String s) {
return (NBTBase) tagMap.get(s); return (NBTBase) tagMap.get(s);