Skip to content

Commit

Permalink
massive inventory code rework.
Browse files Browse the repository at this point in the history
all inventory code, including that of tenders and trains, is now handled by the Freight class to improve overall code consistency, simplify a significant number of things, and improve some crashes related to add-ons
  • Loading branch information
EternalBlueFlame committed Sep 27, 2024
1 parent 17094a2 commit 871e500
Show file tree
Hide file tree
Showing 83 changed files with 127 additions and 3,852 deletions.
6 changes: 3 additions & 3 deletions src/main/java/train/client/gui/GuiLoco2.java
Original file line number Diff line number Diff line change
Expand Up @@ -402,15 +402,15 @@ protected void drawGuiContainerBackgroundLayer(float f, int t, int g) {
drawTexturedModalRect(j + 10, (k + 36 + 13) - l, 178, 12 - l, 14, l + 2);
}
} else {
for (int i1 = loco.numCargoSlots; i1 < 5; i1++) {
for (int i1 = loco instanceof ElectricTrain?5:3; i1 < 5; i1++) {
drawTexturedModalRect(j + 79 + 18 * i1, k + 17, 190, 0, 18, 18);
}

for (int j1 = loco.numCargoSlots1; j1 < 5; j1++) {
for (int j1 = loco instanceof ElectricTrain?5:3; j1 < 5; j1++) {
drawTexturedModalRect(j + 79 + 18 * j1, k + 35, 190, 0, 18, 18);
}

for (int k1 = loco.numCargoSlots2; k1 < 5; k1++) {
for (int k1 = loco instanceof ElectricTrain?5:3; k1 < 5; k1++) {
drawTexturedModalRect(j + 79 + 18 * k1, k + 53, 190, 0, 18, 18);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/train/common/api/AbstractTrains.java
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ public String transportFuelType(){
/**defines the size of the inventory row by row, not counting any special slots like for fuel.
* end result number of slots is this times 9. plus any crafting/fuel slots
* may not return null*/
public int getInventoryRows(){return Math.max((int)(getSpec().getCargoCapacity()*0.1111111111112),1);}
public int getInventoryRows(){return 0;}

/**defines the capacity of the fluidTank tank.
* each value defibes another tank.
Expand Down
39 changes: 19 additions & 20 deletions src/main/java/train/common/api/DieselTrain.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

public abstract class DieselTrain extends Locomotive implements IFluidHandler {

public int fuelSlot = 1;
private int maxTank = 7 * 1000;
private int update = 8;
private StandardTank theTank;
Expand Down Expand Up @@ -45,23 +44,24 @@ private DieselTrain(World world, FluidStack filter, String[] multiFilter) {
this.theTank = LiquidManager.getInstance().new FilteredTank(maxTank, multiFilter);
}
dataWatcher.addObject(4, 0);
numCargoSlots = 3;
numCargoSlots1 = 3;
numCargoSlots2 = 3;
inventorySize = numCargoSlots + numCargoSlots2 + numCargoSlots1 + fuelSlot;
this.dataWatcher.addObject(23, 0);
this.dataWatcher.addObject(27, 0);
this.dataWatcher.addObject(5, "");
}
public DieselTrain(World world, double d, double d1, double d2) {
super(world, d, d1, d2);
}

@Override
public int getSizeInventory() {
return 10+(getInventoryRows()*9);
}

@Override
public void onUpdate() {
super.onUpdate();
if (!worldObj.isRemote) {
if (theTank.getFluidAmount() != this.dataWatcher.getWatchableObjectInt(23)){
this.dataWatcher.updateObject(23, theTank.getFluidAmount());
if (theTank.getFluidAmount() != this.dataWatcher.getWatchableObjectInt(27)){
this.dataWatcher.updateObject(27, theTank.getFluidAmount());
fuelTrain = theTank.getFluidAmount();
this.dataWatcher.updateObject(4, theTank.getFluid()!=null?theTank.getFluid().getFluidID():0);
this.dataWatcher.updateObject(5, theTank.getFluid()!=null?theTank.getFluid().getUnlocalizedName():"");
Expand All @@ -72,11 +72,12 @@ public void onUpdate() {
motionZ *= 0.94;
}
}
checkInvent(cargoItems[0]);
}
}

public int getDiesel() {
return getFuel()==0?(this.dataWatcher.getWatchableObjectInt(23)):getFuel();
return getFuel()==0?(this.dataWatcher.getWatchableObjectInt(27)):getFuel();
}
public String getLiquidName(){ return this.dataWatcher.getWatchableObjectString(5);}

Expand All @@ -92,38 +93,36 @@ public StandardTank getTank() {
protected void writeEntityToNBT(NBTTagCompound nbttagcompound) {
super.writeEntityToNBT(nbttagcompound);
this.theTank.writeToNBT(nbttagcompound);
nbttagcompound.setBoolean("canBeAdjusted", canBeAdjusted);
}

@Override
protected void readEntityFromNBT(NBTTagCompound nbttagcompound) {
super.readEntityFromNBT(nbttagcompound);
this.theTank.readFromNBT(nbttagcompound);
canBeAdjusted = nbttagcompound.getBoolean("canBeAdjusted");
}

public int getCartTankCapacity() {
return maxTank;
}

private void placeInInvent(ItemStack itemstack1) {
for (int i = 1; i < locoInvent.length; i++) {
if (locoInvent[i] == null) {
locoInvent[i] = itemstack1;
for (int i = 1; i < cargoItems.length; i++) {
if (cargoItems[i] == null) {
cargoItems[i] = itemstack1;
return;
}
else if (locoInvent[i] != null && locoInvent[i].getItem() == itemstack1.getItem() && itemstack1.isStackable() && (!itemstack1.getHasSubtypes() || locoInvent[i].getItemDamage() == itemstack1.getItemDamage()) && ItemStack.areItemStackTagsEqual(locoInvent[i], itemstack1)) {
int var9 = locoInvent[i].stackSize + itemstack1.stackSize;
else if (cargoItems[i] != null && cargoItems[i].getItem() == itemstack1.getItem() && itemstack1.isStackable() && (!itemstack1.getHasSubtypes() || cargoItems[i].getItemDamage() == itemstack1.getItemDamage()) && ItemStack.areItemStackTagsEqual(cargoItems[i], itemstack1)) {
int var9 = cargoItems[i].stackSize + itemstack1.stackSize;
if (var9 <= itemstack1.getMaxStackSize()) {
locoInvent[i].stackSize = var9;
cargoItems[i].stackSize = var9;

}
else if (locoInvent[i].stackSize < itemstack1.getMaxStackSize()) {
locoInvent[i].stackSize += 1;
else if (cargoItems[i].stackSize < itemstack1.getMaxStackSize()) {
cargoItems[i].stackSize += 1;
}
return;
}
else if (i == locoInvent.length - 1) {
else if (i == cargoItems.length - 1) {
entityDropItem(itemstack1,1);
return;
}
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/train/common/api/ElectricTrain.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ public abstract class ElectricTrain extends Locomotive {
public ElectricTrain(World world) {
super(world);
fuelTrain = 0;

numCargoSlots = 5;
numCargoSlots1 = 5;
numCargoSlots2 = 5;
inventorySize = numCargoSlots + numCargoSlots2 + numCargoSlots1 + 1;
locoInvent = new ItemStack[inventorySize];
//hasUranium = false;
//reduceExplosionChance = 1000;
Ignite = false;
Expand All @@ -36,6 +30,12 @@ public ElectricTrain(World world) {
public ElectricTrain(World world, double x, double y, double z){
super(world,x,y,z);
}

@Override//todo:why was this so much?
public int getSizeInventory() {
return 16+(getInventoryRows()*9);
}

@Override
public void onUpdate() {
super.onUpdate();
Expand All @@ -52,9 +52,9 @@ public void onUpdate() {
if(!this.canCheckInvent)return;

/* if the loco has fuel */
if (getFuel() < maxEnergy && locoInvent[0] != null)
if (getFuel() < maxEnergy && cargoItems[0] != null)
{
Item item = locoInvent[0].getItem();
Item item = cargoItems[0].getItem();
if (item == Items.redstone && ((getFuel() + 2000) <= maxEnergy))
{
fuelTrain += 2000;
Expand All @@ -67,7 +67,7 @@ public void onUpdate() {
}else if (item instanceof IEnergyContainerItem)
{
int draw = MathHelper.floor_double(Math.min(200, maxEnergy - getFuel()) * 0.1); // amount of energy to attempt to draw this tick
fuelTrain += ((IEnergyContainerItem) item).extractEnergy(locoInvent[0], draw, false) * 10;
fuelTrain += ((IEnergyContainerItem) item).extractEnergy(cargoItems[0], draw, false) * 10;
}
/*else if ((PluginIndustrialCraft.getItems().containsKey(PluginIndustrialCraft.getNames()[4]) && PluginIndustrialCraft.getItems().containsKey(PluginIndustrialCraft.getNames()[3])) && (item == PluginIndustrialCraft.getItems().get(PluginIndustrialCraft.getNames()[4]).getItem())) {
Expand Down
41 changes: 25 additions & 16 deletions src/main/java/train/common/api/Freight.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
public abstract class Freight extends EntityRollingStock implements IInventory {
public ItemStack[] cargoItems;
protected double itemInsideCount = 0;
private int slotsFilled = 0;
public int slotsFilled = 0;

public Freight(World world) {
super(world);
dataWatcher.addObject(22, 0);
cargoItems = new ItemStack[getInventoryRows()*9];
cargoItems = new ItemStack[getSizeInventory()];
}
public Freight(World world, double d, double d1, double d2) {
super(world, d, d1, d2);
cargoItems = new ItemStack[getInventoryRows()*9];
cargoItems = new ItemStack[getSizeInventory()];
}

@Override
Expand Down Expand Up @@ -130,7 +130,7 @@ public String getInventoryName() {
}

@Override
public int getSizeInventory(){return getInventoryRows()*9;}
public int getSizeInventory(){return getSpec()==null?getInventoryRows()*9:getSpec().getCargoCapacity();}

/**
* Returns true if this cart is a storage cart Some carts may have inventories but not be storage carts and some carts without inventories may be storage carts.
Expand Down Expand Up @@ -188,27 +188,36 @@ public int getAmmountOfCargo() {
@Override
protected void readEntityFromNBT(NBTTagCompound nbttagcompound) {
super.readEntityFromNBT(nbttagcompound);
ItemStack[] cargoItemsCount;
NBTTagList nbttaglist = nbttagcompound.getTagList("Items", Constants.NBT.TAG_COMPOUND);
cargoItemsCount = new ItemStack[getSizeInventory()];

slotsFilled = 0;
NBTTagList nbttaglist = nbttagcompound.getTagList("Items", Constants.NBT.TAG_COMPOUND);
cargoItems = new ItemStack[getSizeInventory()];
for (int i = 0; i < nbttaglist.tagCount(); i++) {
NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
int j = nbttagcompound1.getByte("Slot") & 0xff;
if (j < cargoItemsCount.length) {
cargoItemsCount[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
if (j >= 0 && j < cargoItems.length) {
cargoItems[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
if(cargoItems[j]!=null){
slotsFilled++;
}
}
}
this.dataWatcher.updateObject(22, slotsFilled);
}

slotsFilled = 0;

for (int i = 0; i < getSizeInventory(); i++) {
ItemStack itemstack = cargoItemsCount[i];
if (itemstack != null) {
slotsFilled++;
@Override
protected void writeEntityToNBT(NBTTagCompound nbttagcompound) {
super.writeEntityToNBT(nbttagcompound);
NBTTagList nbttaglist = new NBTTagList();
for (int i = 0; i < cargoItems.length; i++) {
if (cargoItems[i] != null) {
NBTTagCompound nbttagcompound1 = new NBTTagCompound();
nbttagcompound1.setByte("Slot", (byte) i);
cargoItems[i].writeToNBT(nbttagcompound1);
nbttaglist.appendTag(nbttagcompound1);
}
}
this.dataWatcher.updateObject(22, slotsFilled);
nbttagcompound.setTag("Items", nbttaglist);
}

@Override
Expand Down
Loading

0 comments on commit 871e500

Please sign in to comment.