Skip to content

Commit

Permalink
Removed ISidedInventory again \o/ and added onInstalled/`onUninst…
Browse files Browse the repository at this point in the history
…alled` instead! Improved general lifecycle logic of modules a big, adding `onDisposed`! Fixed pipes potentially forgetting their data!
  • Loading branch information
fnuecke committed Dec 14, 2015
1 parent 7472020 commit 06002ea
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 169 deletions.
14 changes: 1 addition & 13 deletions src/main/java/li/cil/tis3d/api/machine/Casing.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package li.cil.tis3d.api.machine;

import li.cil.tis3d.api.module.Module;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
Expand All @@ -11,7 +10,7 @@
* <p>
* This is implemented by the tile entity of TIS-3D casings.
*/
public interface Casing extends ISidedInventory {
public interface Casing {
/**
* The world this casing resides in.
*
Expand Down Expand Up @@ -41,17 +40,6 @@ public interface Casing extends ISidedInventory {
*/
Module getModule(Face face);

/**
* Set the module for the specified face of the casing.
* <p>
* This is automatically called by the casing tile entity when items are
* added or removed, you'll usually not need to call this directly.
*
* @param face the face to install the module on.
* @param module the module to install on the face, or <tt>null</tt> for none.
*/
void setModule(Face face, Module module);

/**
* Get the receiving pipe on the specified port of a module in this casing.
* <p>
Expand Down
54 changes: 49 additions & 5 deletions src/main/java/li/cil/tis3d/api/module/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import li.cil.tis3d.api.machine.Face;
import li.cil.tis3d.api.machine.Port;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
Expand Down Expand Up @@ -36,6 +37,41 @@ public interface Module {
*/
void step();

/**
* Called when the module is being installed into a {@link Casing}.
* <p>
* This is mainly for convenience and having things in one place, you could
* just as well restore state in your {@link ModuleProvider}'s
* {@link ModuleProvider#createModule(ItemStack, Casing, Face)} method.
* <p>
* This is called before the first {@link #onEnabled()}, and also <em>before
* it is actually set in the containing {@link Casing}</em>. Particularly
* this means {@link Casing#getModule(Face)} for the module's {@link Face}
* will return <tt>null</tt> in this callback.
* <p>
* Note that this is only called on the server.
*
* @param stack the item stack the module was created from.
*/
void onInstalled(ItemStack stack);

/**
* Called after the module was uninstalled from a {@link Casing}.
* <p>
* This allows storing any data that should be persisted onto the module's
* item representation. For most modules this will not apply, since they
* are generally stateless / reset state when the computer shuts down.
* <p>
* This is called after the last {@link #onDisabled()} and is equivalent
* to a <tt>dispose</tt> method (i.e. the module will not be used again
* after this).
* <p>
* Note that this is only called on the server.
*
* @param stack the stack representing the module.
*/
void onUninstalled(ItemStack stack);

/**
* Called when the multi-block of casings the module is installed in is
* enabled, or when the module was installed into an enabled casing.
Expand All @@ -52,14 +88,22 @@ public interface Module {
* a controller resets the whole multi-block system.
* <p>
* Note that this is only called on the server.
* <p>
* When this is called due to a controller or casing being disposed, e.g.
* due to a chunk being unloaded, the <tt>isDisposing</tt> parameter will
* be <tt>true</tt>, indicating that the module should avoid world
* interaction in its clean-up logic (to avoid loading the chunk again).
*/
void onDisabled();

/**
* Called when the {@link Casing} housing the module is being disposed,
* e.g. due to a chunk being unloaded.
* <p>
* This is intended for freeing up resources (e.g. allocated texture or
* audio memory). Unlike {@link #onDisabled()} this is only called once
* on a module, at the very end of its life. Avoid world interaction in
* this callback to avoid loading the chunk again.
* <p>
* This is called on the server <em>and</em> the client.
*/
void onDisposed();

/**
* Called from a pipe this module is writing to when the data was read.
* <p>
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/li/cil/tis3d/api/prefab/module/AbstractModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import li.cil.tis3d.api.module.Module;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.MovingObjectPosition;
import net.minecraftforge.fml.relauncher.Side;
Expand Down Expand Up @@ -84,6 +85,14 @@ public Face getFace() {
public void step() {
}

@Override
public void onInstalled(final ItemStack stack) {
}

@Override
public void onUninstalled(final ItemStack stack) {
}

@Override
public void onEnabled() {
}
Expand All @@ -92,6 +101,10 @@ public void onEnabled() {
public void onDisabled() {
}

@Override
public void onDisposed() {
}

@Override
public void onWriteComplete(final Port port) {
}
Expand Down
26 changes: 2 additions & 24 deletions src/main/java/li/cil/tis3d/common/inventory/Inventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
import net.minecraft.util.IChatComponent;
import net.minecraftforge.common.util.Constants;

import java.util.AbstractList;

/**
* Base implementation of an array based inventory.
*/
public class Inventory extends AbstractList<ItemStack> implements IInventory {
public class Inventory implements IInventory {
private static final String TAG_ITEMS = "inventory";

private final String name;
Expand All @@ -32,7 +30,7 @@ public void readFromNBT(final NBTTagCompound nbt) {
final NBTTagList itemList = nbt.getTagList(TAG_ITEMS, Constants.NBT.TAG_COMPOUND);
final int count = Math.min(itemList.tagCount(), items.length);
for (int index = 0; index < count; index++) {
setInventorySlotContents(index, ItemStack.loadItemStackFromNBT(itemList.getCompoundTagAt(index)));
items[index] = ItemStack.loadItemStackFromNBT(itemList.getCompoundTagAt(index));
}
}

Expand Down Expand Up @@ -173,24 +171,4 @@ public int getFieldCount() {
@Override
public void clear() {
}

// --------------------------------------------------------------------- //
// List

@Override
public ItemStack get(final int index) {
return getStackInSlot(index);
}

@Override
public int size() {
return getSizeInventory();
}

@Override
public ItemStack set(final int index, final ItemStack element) {
final ItemStack oldStack = get(index);
setInventorySlotContents(index, element);
return oldStack;
}
}
15 changes: 13 additions & 2 deletions src/main/java/li/cil/tis3d/common/inventory/InventoryCasing.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import li.cil.tis3d.api.ModuleAPI;
import li.cil.tis3d.api.machine.Face;
import li.cil.tis3d.api.module.Module;
import li.cil.tis3d.api.module.ModuleProvider;
import li.cil.tis3d.common.Constants;
import li.cil.tis3d.common.tile.TileEntityCasing;
Expand Down Expand Up @@ -77,11 +78,21 @@ protected void onItemAdded(final int index) {
return;
}

tileEntity.setModule(Face.VALUES[index], provider.createModule(stack, tileEntity, face));
final Module module = provider.createModule(stack, tileEntity, face);
if (module != null && !tileEntity.getCasingWorld().isRemote) {
module.onInstalled(stack);
}
tileEntity.setModule(Face.VALUES[index], module);
}

@Override
protected void onItemRemoved(final int index) {
tileEntity.setModule(Face.VALUES[index], null);
final Face face = Face.VALUES[index];
final Module module = tileEntity.getModule(face);
tileEntity.setModule(face, null);
if (module != null && !tileEntity.getCasingWorld().isRemote) {
module.onUninstalled(getStackInSlot(index));
module.onDisposed();
}
}
}
Loading

0 comments on commit 06002ea

Please sign in to comment.