Skip to content

Commit

Permalink
Merge pull request #12 from AnvilloyDevStudio/plus-pr-618
Browse files Browse the repository at this point in the history
Separate HistoricLoad from LegacyLoad
  • Loading branch information
BenCheung0422 authored Jan 20, 2025
2 parents 4100f74 + e024dd5 commit 5291bed
Show file tree
Hide file tree
Showing 7 changed files with 1,636 additions and 121 deletions.
4 changes: 3 additions & 1 deletion src/client/java/minicraft/core/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ public static void resetGame(boolean keepPlayer) {
* This method is used to create a brand new world, or to load an existing one from a file.
* For the loading screen updates to work, it it assumed that *this* is called by a thread *other* than the one rendering the current *menu*.
**/
public static void initWorld() { // This is a full reset; everything.
public static void initWorld()
throws Load.BackupCreationFailedException, Load.WorldLoadingFailedException,
Load.UserPromptCancelledException { // This is a full reset; everything.
Logging.WORLD.debug("Resetting world...");

PlayerDeathDisplay.shouldRespawn = false;
Expand Down
1,238 changes: 1,238 additions & 0 deletions src/client/java/minicraft/saveload/HistoricLoad.java

Large diffs are not rendered by default.

112 changes: 27 additions & 85 deletions src/client/java/minicraft/saveload/LegacyLoad.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import minicraft.level.tile.Tiles;
import minicraft.screen.LoadingDisplay;
import minicraft.util.Logging;
import org.jetbrains.annotations.Nullable;
import org.tinylog.Logger;

import java.io.BufferedReader;
Expand All @@ -58,17 +59,10 @@ public class LegacyLoad {
ArrayList<String> extradata;

public boolean hasloadedbigworldalready;
Version currentVer, worldVer;
boolean oldSave = false;

Game game = null;

@Nullable Version worldVer = null;
private DeathChest deathChest;

{
currentVer = Game.VERSION;
worldVer = null;

data = new ArrayList<>();
extradata = new ArrayList<>();
hasloadedbigworldalready = false;
Expand All @@ -77,13 +71,6 @@ public class LegacyLoad {
public LegacyLoad(String worldname) {
location += "/saves/" + worldname + "/";

File testFile = new File(location + "KeyPrefs" + extension);
if (!testFile.exists()) {
worldVer = new Version("1.8");
oldSave = true;
} else
testFile.delete(); // We don't care about it anymore anyway.

// This is used in loadInventory().

loadGame("Game"); // More of the version will be determined here
Expand Down Expand Up @@ -181,37 +168,18 @@ protected void updateUnlocks(File file) {

public void loadGame(String filename) {
loadFromFile(location + filename + extension);
boolean hasVersion = data.get(0).contains(".");
if (hasVersion) {
worldVer = new Version(data.get(0)); // Gets the world version
Updater.setTime(Integer.parseInt(data.get(1)));
Updater.gameTime = 65000; // Prevents time cheating.

if (worldVer.compareTo(new Version("1.9.2")) < 0) {
Settings.set("autosave", Boolean.parseBoolean(data.get(3)));
Settings.set("sound", Boolean.parseBoolean(data.get(4)));
if (worldVer.compareTo(new Version("1.9.2-dev2")) >= 0)
AirWizard.beaten = Boolean.parseBoolean(data.get(5));
} else { // This is 1.9.2 official or after
Settings.setIdx("diff", Integer.parseInt(data.get(3)));
AirWizard.beaten = Boolean.parseBoolean(data.get(4));
}
} else {
if (data.size() == 5) {
worldVer = new Version("1.9");
Updater.setTime(Integer.parseInt(data.get(0)));
Settings.set("autosave", Boolean.parseBoolean(data.get(3)));
Settings.set("sound", Boolean.parseBoolean(data.get(4)));
} else { // version == 1.8?
if (!oldSave) {
Logging.SAVELOAD.warn("UNEXPECTED WORLD VERSION");
worldVer = new Version("1.8.1");
}
// For backwards compatibility
Updater.tickCount = Integer.parseInt(data.get(0));
playerac = Integer.parseInt(data.get(3));
Settings.set("autosave", false);
}
worldVer = new Version(data.get(0)); // Gets the world version
Updater.setTime(Integer.parseInt(data.get(1)));
Updater.gameTime = 65000; // Prevents time cheating.

if (worldVer.compareTo(new Version("1.9.2")) < 0) {
Settings.set("autosave", Boolean.parseBoolean(data.get(3)));
Settings.set("sound", Boolean.parseBoolean(data.get(4)));
if (worldVer.compareTo(new Version("1.9.2-dev2")) >= 0)
AirWizard.beaten = Boolean.parseBoolean(data.get(5));
} else { // This is 1.9.2 official or after
Settings.setIdx("diff", Integer.parseInt(data.get(3)));
AirWizard.beaten = Boolean.parseBoolean(data.get(4));
}
}

Expand Down Expand Up @@ -252,21 +220,14 @@ public void loadPlayer(String filename, Player player) {
player.armor = Integer.parseInt(data.get(5));

String modedata;
if (!oldSave) {
if (data.size() >= 14) {
if (worldVer == null) worldVer = new Version("1.9.1-pre1");
player.armorDamageBuffer = Integer.parseInt(data.get(13));
player.curArmor = (ArmorItem) Items.get(data.get(14));
} else player.armor = 0;
if (data.size() >= 14) {
if (worldVer == null) worldVer = new Version("1.9.1-pre1");
player.armorDamageBuffer = Integer.parseInt(data.get(13));
player.curArmor = (ArmorItem) Items.get(data.get(14));
} else player.armor = 0;

Game.currentLevel = Integer.parseInt(data.get(8));
modedata = data.get(9);

} else {
// Old, 1.8 save.
Game.currentLevel = Integer.parseInt(data.get(7));
modedata = data.get(8);
}
Game.currentLevel = Integer.parseInt(data.get(8));
modedata = data.get(9);

player.setScore(Integer.parseInt(data.get(6)));
World.levels[Game.currentLevel].add(player);
Expand All @@ -284,25 +245,18 @@ public void loadPlayer(String filename, Player player) {
Settings.setIdx("mode", mode);

boolean hasEffects;
int potionIdx = 10;
if (oldSave) {
hasEffects = data.size() > 10 && data.get(data.size() - 2).contains("PotionEffects[");
potionIdx = data.size() - 2;
} else
hasEffects = !data.get(10).equals("PotionEffects[]"); // Newer save
hasEffects = !data.get(10).equals("PotionEffects[]"); // Newer save

if (hasEffects) {
String[] effects = data.get(potionIdx).replace("PotionEffects[", "").replace("]", "").split(":");

for (int i = 0; i < effects.length; i++) {
String[] effect = effects[i].split(";");
String[] effects = data.get(10).replace("PotionEffects[", "").replace("]", "").split(":");
for (String s : effects) {
String[] effect = s.split(";");
String pName = effect[0];
if (oldSave) pName = pName.replace("P.", "Potion");
PotionItem.applyPotion(player, Enum.valueOf(PotionType.class, pName), Integer.parseInt(effect[1]));
}
}

String colors = data.get(oldSave ? data.size() - 1 : 11).replace("[", "").replace("]", "");
String colors = data.get(11).replace("[", "").replace("]", "");
String[] color = colors.split(";");
player.shirtColor = Integer.parseInt(color[0] + color[1] + color[2]);
}
Expand All @@ -312,9 +266,7 @@ public void loadInventory(String filename, Inventory inventory) {
loadFromFile(location + filename + extension);
inventory.clearInv();

for (int i = 0; i < data.size(); i++) {
String item = data.get(i);
if (i == 0 && oldSave && item.contains(";")) item = item.replace(";0", ";1");
for (String item : data) {
loadItemToInventory(item, inventory);
}

Expand All @@ -329,15 +281,13 @@ public void loadItemToInventory(String item, Inventory inventory) {
if (item.contains(";")) {
String[] curData = item.split(";");
String itemName = curData[0];
if (oldSave) itemName = subOldName(itemName);

//System.out.println("Item to fetch: " + itemName + "; count=" + curData[1]);
Item newItem = Items.get(itemName);
int count = Integer.parseInt(curData[1]);
for (int i = 0; i < count; i++)
loadItem(inventory, newItem);
} else {
if (oldSave) item = subOldName(item);
Item toAdd = Items.get(item);
loadItem(inventory, toAdd);
}
Expand All @@ -349,14 +299,6 @@ private void loadItem(Inventory inventory, Item item) {
}
}

private String subOldName(String oldName) {
//System.out.println("Old name: " + oldName);
String newName = oldName.replace("P.", "Potion").replace("Fish Rod", "Fishing Rod").replace("bed", "Bed");
newName = Load.subOldName(newName, worldVer);
//System.out.println("New name: " + newName);
return newName;
}

public void loadEntities(String filename, Player player) {
loadFromFile(location + filename + extension);

Expand Down
Loading

0 comments on commit 5291bed

Please sign in to comment.