Skip to content

Commit

Permalink
Fix storage crate issues (might fix #146)
Browse files Browse the repository at this point in the history
- Fixed checkPileConnections
- Crate pile .dat doesn't store crate ID anymore (redundant, filename
already contains it)
- Uncommented old cold in charge of clearing crate pile collection on
world unload (save is not called after unload anymore)
  • Loading branch information
copyboy committed Dec 30, 2013
1 parent a789207 commit 344c2c9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ public static CratePileCollection getCollection(World world) {
return collection;
}

private void removeCollectionFromMap() {
collectionMap.remove(dimension);
}

/** Gets a crate pile from the collection, creates/loads it if necessary. */
public CratePileData getCratePile(int id) {
CratePileData pileData;
Expand Down Expand Up @@ -78,11 +74,11 @@ public void removeCratePile(CratePileData pileData) {
dirtyPiles.remove(pileData);
getSaveFile(pileData.id).delete();
if (pileDataMap.size() <= 0)
removeCollectionFromMap();
collectionMap.remove(dimension);
}

/** Saves the pile data to file. */
// Gets saved to <world>[/<dimension>]/crates/<id>.dat in uncompressed NBT.
// Gets saved to <world>[/<dimension>]/data/crates/<id>.dat in uncompressed NBT.
public void save(CratePileData pileData) {
try {
File file = getSaveFile(pileData.id);
Expand All @@ -106,7 +102,7 @@ public CratePileData load(int id) {
File file = getSaveFile(id);
if (!file.exists()) return null;
NBTTagCompound root = CompressedStreamTools.read(file);
return CratePileData.fromCompound(this, root.getCompoundTag("data"));
return CratePileData.fromCompound(this, id, root.getCompoundTag("data"));
} catch (Exception e) {
BetterStorage.log.warning("Error loading CratePileData: " + e);
e.printStackTrace();
Expand Down Expand Up @@ -143,10 +139,8 @@ public static void saveAll(World world) {
/** Called when the world unloads, removes the
* crate pile connection from the collection map. */
public static void unload(World world) {
// Unload is called before save. This breaks saving.
// Disabling this until there is something better isn't too bad.
//int dimension = world.provider.dimensionId;
//collectionMap.remove(dimension);
int dimension = world.provider.dimensionId;
collectionMap.remove(dimension);
}

/** Called every tick if the world is loaded. */
Expand Down
10 changes: 6 additions & 4 deletions src/net/mcft/copy/betterstorage/block/crate/CratePileData.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.mcft.copy.betterstorage.api.ICrateWatcher;
import net.mcft.copy.betterstorage.inventory.InventoryCrateBlockView;
import net.mcft.copy.betterstorage.misc.ItemIdentifier;
import net.mcft.copy.betterstorage.misc.Region;
import net.mcft.copy.betterstorage.utils.RandomUtils;
import net.mcft.copy.betterstorage.utils.StackUtils;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -54,6 +55,9 @@ public class CratePileData implements Iterable<ItemStack> {
/** Returns the number of slots free. Negative if there's any overflow. */
public int getFreeSlots() { return getCapacity() - getOccupiedSlots(); }

/** Returns the region / bounds this crate pile takes up. */
public Region getRegion() { return map.region; }

public int getCenterX() { return (map.region.minX + map.region.maxX) / 2; }
public int getCenterY() { return (map.region.minY + map.region.maxY) / 2; }
public int getCenterZ() { return (map.region.minZ + map.region.maxZ) / 2; }
Expand Down Expand Up @@ -360,7 +364,6 @@ private void markDirty() {

public NBTTagCompound toCompound() {
NBTTagCompound compound = new NBTTagCompound("");
compound.setInteger("id", id);
compound.setShort("numCrates", (short)getNumCrates());
NBTTagList stacks = new NBTTagList("stacks");
for (ItemStack stack : this) {
Expand All @@ -378,10 +381,9 @@ public NBTTagCompound toCompound() {
return compound;
}

public static CratePileData fromCompound(CratePileCollection collection, NBTTagCompound compound) {
int cratePileId = compound.getInteger("id");
public static CratePileData fromCompound(CratePileCollection collection, int crateId, NBTTagCompound compound) {
int numCrates = compound.getShort("numCrates");
CratePileData pileData = new CratePileData(collection, cratePileId, numCrates);
CratePileData pileData = new CratePileData(collection, crateId, numCrates);
NBTTagList stacks = compound.getTagList("stacks");
for (int j = 0; j < stacks.tagCount(); j++) {
NBTTagCompound stackCompound = (NBTTagCompound)stacks.tagAt(j);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private void checkPileConnections(CratePileData data) {
}
// If there's still some crates left and this is a
// base crate, see which crates are still connected.
if ((data.getNumCrates() > 0) && !WorldUtils.is(worldObj, x, y - 1, z, Blocks.crate)) {
if ((data.getNumCrates() > 0) && (y == data.getRegion().minY)) {
List<HashSet<TileEntityCrate>> crateSets =
new ArrayList<HashSet<TileEntityCrate>>();
int checkedChecks = 0;
Expand Down

0 comments on commit 344c2c9

Please sign in to comment.