Skip to content

Commit 294a405

Browse files
committed
Fix container items (?)
1 parent b1effac commit 294a405

File tree

3 files changed

+21
-75
lines changed

3 files changed

+21
-75
lines changed

src/main/java/io/github/cadiboo/examplemod/tileentity/ElectricFurnaceTileEntity.java

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import net.minecraft.entity.player.PlayerInventory;
1212
import net.minecraft.inventory.IInventory;
1313
import net.minecraft.inventory.Inventory;
14-
import net.minecraft.inventory.InventoryHelper;
1514
import net.minecraft.inventory.container.Container;
1615
import net.minecraft.inventory.container.INamedContainerProvider;
1716
import net.minecraft.item.ItemStack;
@@ -53,7 +52,6 @@ public class ElectricFurnaceTileEntity extends TileEntity implements ITickableTi
5352
private static final String SMELT_TIME_LEFT_TAG = "smeltTimeLeft";
5453
private static final String MAX_SMELT_TIME_TAG = "maxSmeltTime";
5554
private static final String ENERGY_TAG = "energy";
56-
5755
public final ItemStackHandler inventory = new ItemStackHandler(2) {
5856
@Override
5957
public boolean isItemValid(final int slot, @Nonnull final ItemStack stack) {
@@ -148,7 +146,7 @@ public void tick() {
148146

149147
// Smelting code
150148

151-
final ItemStack input = inventory.getStackInSlot(INPUT_SLOT);
149+
final ItemStack input = inventory.getStackInSlot(INPUT_SLOT).copy();
152150
final ItemStack result = getResult(input).orElse(ItemStack.EMPTY);
153151

154152
if (!result.isEmpty() && isInput(input)) {
@@ -168,14 +166,12 @@ public void tick() {
168166
--smeltTimeLeft;
169167
if (smeltTimeLeft == 0) {
170168
inventory.insertItem(OUTPUT_SLOT, result, false);
171-
if (input.hasContainerItem()) {
172-
final ItemStack containerStack = input.getContainerItem();
173-
input.shrink(1); // Shrink now to make space in the slot.
174-
insertOrDropStack(INPUT_SLOT, containerStack);
175-
} else {
169+
if (input.hasContainerItem())
170+
inventory.setStackInSlot(INPUT_SLOT, input.getContainerItem());
171+
else {
176172
input.shrink(1);
173+
inventory.setStackInSlot(INPUT_SLOT, input); // Update the data
177174
}
178-
inventory.setStackInSlot(INPUT_SLOT, input); // Update the data
179175
smeltTimeLeft = -1; // Set to -1 so we smelt the next stack on the next tick
180176
}
181177
}
@@ -206,20 +202,6 @@ public void tick() {
206202

207203
}
208204

209-
/**
210-
* Tries to insert the stack into the given slot or drops the stack on the ground if it can't insert it.
211-
*
212-
* @param slot The slot to try to insert the container item into
213-
* @param stack The stack to try to insert
214-
*/
215-
private void insertOrDropStack(final int slot, final ItemStack stack) {
216-
final boolean canInsertContainerItemIntoSlot = inventory.insertItem(slot, stack, true).isEmpty();
217-
if (canInsertContainerItemIntoSlot)
218-
inventory.insertItem(slot, stack, false);
219-
else // Drop the stack if we can't insert it
220-
InventoryHelper.spawnItemStack(world, pos.getX(), pos.getY(), pos.getZ(), stack);
221-
}
222-
223205
/**
224206
* Mimics the code in {@link AbstractFurnaceTileEntity#func_214005_h()}
225207
*

src/main/java/io/github/cadiboo/examplemod/tileentity/HeatCollectorTileEntity.java

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import net.minecraft.entity.player.PlayerEntity;
1313
import net.minecraft.entity.player.PlayerInventory;
1414
import net.minecraft.fluid.IFluidState;
15-
import net.minecraft.inventory.InventoryHelper;
1615
import net.minecraft.inventory.container.Container;
1716
import net.minecraft.inventory.container.INamedContainerProvider;
1817
import net.minecraft.item.ItemStack;
@@ -53,7 +52,7 @@ public class HeatCollectorTileEntity extends TileEntity implements ITickableTile
5352
public final ItemStackHandler inventory = new ItemStackHandler(1) {
5453
@Override
5554
public boolean isItemValid(final int slot, @Nonnull final ItemStack stack) {
56-
return !stack.isEmpty() && FurnaceTileEntity.isFuel(stack);
55+
return FurnaceTileEntity.isFuel(stack);
5756
}
5857

5958
@Override
@@ -86,20 +85,18 @@ public void tick() {
8685
final BlockPos pos = this.pos;
8786
final SettableEnergyStorage energy = this.energy;
8887

89-
final ItemStack fuelStack = this.inventory.getStackInSlot(FUEL_SLOT);
88+
final ItemStack fuelStack = this.inventory.getStackInSlot(FUEL_SLOT).copy();
9089
if (!fuelStack.isEmpty()) {
9190
int energyToReceive = ForgeHooks.getBurnTime(fuelStack);
9291
// Only use the stack if we can receive 100% of the energy from it
9392
if (energy.receiveEnergy(energyToReceive, true) == energyToReceive) {
9493
energy.receiveEnergy(energyToReceive, false);
95-
if (fuelStack.hasContainerItem()) {
96-
final ItemStack containerStack = fuelStack.getContainerItem();
97-
fuelStack.shrink(1); // Shrink now to make space in the slot.
98-
insertOrDropStack(FUEL_SLOT, containerStack);
99-
} else {
94+
if (fuelStack.hasContainerItem())
95+
inventory.setStackInSlot(FUEL_SLOT, fuelStack.getContainerItem());
96+
else {
10097
fuelStack.shrink(1);
98+
inventory.setStackInSlot(FUEL_SLOT, fuelStack); // Update the data
10199
}
102-
inventory.setStackInSlot(FUEL_SLOT, fuelStack); // Update the data
103100
}
104101
}
105102

@@ -167,20 +164,6 @@ else if (blockState.getBlock() == Blocks.CAMPFIRE)
167164

168165
}
169166

170-
/**
171-
* Tries to insert the stack into the given slot or drops the stack on the ground if it can't insert it.
172-
*
173-
* @param slot The slot to try to insert the container item into
174-
* @param stack The stack to try to insert
175-
*/
176-
private void insertOrDropStack(final int slot, final ItemStack stack) {
177-
final boolean canInsertContainerItemIntoSlot = inventory.insertItem(slot, stack, true).isEmpty();
178-
if (canInsertContainerItemIntoSlot)
179-
inventory.insertItem(slot, stack, false);
180-
else // Drop the stack if we can't insert it
181-
InventoryHelper.spawnItemStack(world, pos.getX(), pos.getY(), pos.getZ(), stack);
182-
}
183-
184167
@Nonnull
185168
@Override
186169
public <T> LazyOptional<T> getCapability(@Nonnull final Capability<T> cap, @Nullable final Direction side) {

src/main/java/io/github/cadiboo/examplemod/tileentity/ModFurnaceTileEntity.java

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import net.minecraft.entity.player.PlayerInventory;
1111
import net.minecraft.inventory.IInventory;
1212
import net.minecraft.inventory.Inventory;
13-
import net.minecraft.inventory.InventoryHelper;
1413
import net.minecraft.inventory.container.Container;
1514
import net.minecraft.inventory.container.INamedContainerProvider;
1615
import net.minecraft.item.ItemStack;
@@ -157,7 +156,7 @@ public void tick() {
157156

158157
// Smelting code
159158

160-
final ItemStack input = inventory.getStackInSlot(INPUT_SLOT);
159+
final ItemStack input = inventory.getStackInSlot(INPUT_SLOT).copy();
161160
final ItemStack result = getResult(input).orElse(ItemStack.EMPTY);
162161

163162
if (!result.isEmpty() && isInput(input)) {
@@ -173,14 +172,12 @@ public void tick() {
173172
--smeltTimeLeft;
174173
if (smeltTimeLeft == 0) {
175174
inventory.insertItem(OUTPUT_SLOT, result, false);
176-
if (input.hasContainerItem()) {
177-
final ItemStack containerStack = input.getContainerItem();
178-
input.shrink(1); // Shrink now to make space in the slot.
179-
insertOrDropStack(FUEL_SLOT, containerStack);
180-
} else {
175+
if (input.hasContainerItem())
176+
inventory.setStackInSlot(INPUT_SLOT, input.getContainerItem());
177+
else {
181178
input.shrink(1);
179+
inventory.setStackInSlot(INPUT_SLOT, input); // Update the data
182180
}
183-
inventory.setStackInSlot(INPUT_SLOT, input); // Update the data
184181
smeltTimeLeft = -1; // Set to -1 so we smelt the next stack on the next tick
185182
}
186183
}
@@ -212,20 +209,6 @@ public void tick() {
212209

213210
}
214211

215-
/**
216-
* Tries to insert the stack into the given slot or drops the stack on the ground if it can't insert it.
217-
*
218-
* @param slot The slot to try to insert the container item into
219-
* @param stack The stack to try to insert
220-
*/
221-
private void insertOrDropStack(final int slot, final ItemStack stack) {
222-
final boolean canInsertContainerItemIntoSlot = inventory.insertItem(slot, stack, true).isEmpty();
223-
if (canInsertContainerItemIntoSlot)
224-
inventory.insertItem(slot, stack, false);
225-
else // Drop the stack if we can't insert it
226-
InventoryHelper.spawnItemStack(world, pos.getX(), pos.getY(), pos.getZ(), stack);
227-
}
228-
229212
/**
230213
* Mimics the code in {@link AbstractFurnaceTileEntity#func_214005_h()}
231214
*
@@ -242,19 +225,17 @@ private short getSmeltTime(final ItemStack input) {
242225
* @return If the fuel was burnt
243226
*/
244227
private boolean burnFuel() {
245-
final ItemStack fuelStack = inventory.getStackInSlot(FUEL_SLOT);
228+
final ItemStack fuelStack = inventory.getStackInSlot(FUEL_SLOT).copy();
246229
if (!fuelStack.isEmpty()) {
247230
final int burnTime = ForgeHooks.getBurnTime(fuelStack);
248231
if (burnTime > 0) {
249232
fuelBurnTimeLeft = maxFuelBurnTime = ((short) burnTime);
250-
if (fuelStack.hasContainerItem()) {
251-
final ItemStack containerStack = fuelStack.getContainerItem();
252-
fuelStack.shrink(1); // Shrink now to make space in the slot.
253-
insertOrDropStack(FUEL_SLOT, containerStack);
254-
} else {
233+
if (fuelStack.hasContainerItem())
234+
inventory.setStackInSlot(FUEL_SLOT, fuelStack.getContainerItem());
235+
else {
255236
fuelStack.shrink(1);
237+
inventory.setStackInSlot(FUEL_SLOT, fuelStack); // Update the data
256238
}
257-
inventory.setStackInSlot(FUEL_SLOT, fuelStack); // Update the data
258239
return true;
259240
}
260241
}

0 commit comments

Comments
 (0)