Skip to content

Commit

Permalink
LF (Unix) to CRLF (Windows)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBigEye committed Sep 10, 2021
1 parent 78e3935 commit 5e4dfd5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 88 deletions.
45 changes: 4 additions & 41 deletions src/minicraft/saveload/Load.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
import minicraft.entity.particle.TextParticle;
import minicraft.gfx.Color;
import minicraft.item.ArmorItem;
import minicraft.item.BookItem;
import minicraft.item.Inventory;
import minicraft.item.Item;
import minicraft.item.Items;
Expand Down Expand Up @@ -609,53 +608,17 @@ public void loadInventory(Inventory inventory, List<String> data) {

int count = Integer.parseInt(curData[1]);

if(newItem instanceof StackableItem) {
((StackableItem)newItem).count = count;
if (newItem instanceof StackableItem) {
((StackableItem) newItem).count = count;
inventory.add(newItem);
} else
inventory.add(newItem, count);
} else inventory.add(newItem, count);
} else {
Item toAdd = Items.get(item);
if (toAdd instanceof BookItem) {
if (item.contains(";")) {
try {
// tmpData is used so that loadBook (or more accurately, loadFromFile) doesn't overwrite the other items in the inventory
ArrayList<String> tmpData = new ArrayList<>(data);
String text = loadBook("BookData", Integer.parseInt(item.split(";")[1]));
data = tmpData;

// find our "fake" returns and replace them with "true" ones for the in-game book text
text = text.replace("\\n", "\n");

((BookItem) toAdd).setText(text);
inventory.add(toAdd);
} catch (Exception e) {
// if the data doesn't exist or the index isn't an integer
System.out.println("WARNING: Bad data for book");
}
} else {
// it's not editable, so we can just add it
inventory.add(toAdd);
}
} else {
inventory.add(toAdd);
}
inventory.add(toAdd);
}
}
}

private String loadBook(String filename, int idx) {
String file;
try {
file = loadFromFile(location + filename + extension, false);
} catch (IOException e) {
e.printStackTrace();
return "";
}
// split the file at returns and get the right one
return file.split("\n")[idx];
}

private void loadEntities(String filename) {
LoadingDisplay.setMessage("Entities");
loadFromFile(location + filename + extension);
Expand Down
48 changes: 1 addition & 47 deletions src/minicraft/saveload/Save.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@
import minicraft.entity.mob.boss.AirWizard;
import minicraft.entity.particle.Particle;
import minicraft.entity.particle.TextParticle;
import minicraft.item.BookItem;
import minicraft.item.Inventory;
import minicraft.item.Item;
import minicraft.item.Items;
import minicraft.item.PotionType;
import minicraft.network.MinicraftServer;
import minicraft.screen.LoadingDisplay;
Expand Down Expand Up @@ -90,7 +88,6 @@ public Save(String worldname) {
if (!Game.isValidServer()) { // this must be waited for on a server.
writePlayer("Player", Game.player);
writeInventory("Inventory", Game.player);
writeBooks("BookData", Game.player);
}
writeEntities("Entities");

Expand Down Expand Up @@ -122,7 +119,6 @@ public Save(Player player, boolean writePlayer) {
if(writePlayer) {
writePlayer("Player", player);
writeInventory("Inventory", player);
writeBooks("BookData", player);
}
}

Expand Down Expand Up @@ -278,56 +274,14 @@ private void writeInventory(String filename, Player player) {
}
public static void writeInventory(Player player, List<String> data) {
data.clear();

int numBooks = 0;

if(player.activeItem != null) {
data.add(player.activeItem.getData());
}

Inventory inventory = player.getInventory();

for(int i = 0; i < inventory.invSize(); i++) {
Item item = inventory.get(i);
if (item.equals(Items.get("Editable Book"))) {
data.add(item.getData() + ";" + numBooks);
numBooks++;
} else {
data.add(item.getData());
}
}
}

private void writeBooks(String filename, Player player) {
writeBooks(player, data);
String[] stringData = data.toArray(new String[]{});
try {
writeToFile(location + filename + extension, stringData, false);
} catch (IOException e) {
e.printStackTrace();
}
data.clear();
}
public static void writeBooks(Player player, List<String> data) {
data.clear();

Inventory inventory = player.getInventory();

if (player.activeItem != null && player.activeItem.equals(Items.get("Editable Book"))) {
// make sure to include the book in the player's hand
inventory.add(player.activeItem);
}

for (int i = 0; i < inventory.invSize(); i++) {
// TODO: makes this just check the book's 'editable' property instead of checking for a particular name
if (inventory.get(i).equals(Items.get("Editable Book"))) {
String text = ((BookItem)inventory.get(i)).getText();

// make sure we can save it, since returns are used to separate the books from each other in the file
text = text.replace("\n", "\\n");

data.add(text);
}
data.add(inventory.get(i).getData());
}
}

Expand Down

0 comments on commit 5e4dfd5

Please sign in to comment.