Fix various bugs from the iinventory back-migration

This commit is contained in:
Vincent Lee committed 2020-07-06 22:14:09 -05:00
1 parent d5fe3bac16
commit 0339191502
3 files changed
+90 -18

No files matched your search

@@ -17,6 +17,7 @@ import net.minecraft.inventory.container.ContainerType;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.ICraftingRecipe;
import net.minecraft.item.crafting.IRecipeType;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.world.World;
import vazkii.botania.api.internal.VanillaPacketDispatcher;
@@ -31,7 +32,9 @@ import java.util.List;
import java.util.Optional;
public class TileCraftCrate extends TileOpenCrate {
private static final String TAG_CRAFTING_RESULT = "craft_result";
private int signal = 0;
private ItemStack craftResult = ItemStack.EMPTY;
public TileCraftCrate() {
super(ModTiles.CRAFT_CRATE);
@@ -39,7 +42,7 @@ public class TileCraftCrate extends TileOpenCrate {
@Override
protected Inventory createItemHandler() {
return new Inventory(10) {
return new Inventory(9) {
@Override
public int getInventoryStackLimit() {
return 1;
@@ -47,7 +50,7 @@ public class TileCraftCrate extends TileOpenCrate {
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
return slot != 9 && !isLocked(slot);
return !isLocked(slot);
}
};
}
@@ -64,6 +67,18 @@ public class TileCraftCrate extends TileOpenCrate {
return !getPattern().openSlots.get(slot);
}
@Override
public void readPacketNBT(CompoundNBT tag) {
super.readPacketNBT(tag);
craftResult = ItemStack.read(tag.getCompound(TAG_CRAFTING_RESULT));
}
@Override
public void writePacketNBT(CompoundNBT tag) {
super.writePacketNBT(tag);
tag.put(TAG_CRAFTING_RESULT, craftResult.write(new CompoundNBT()));
}
@Override
public void tick() {
if (world.isRemote) {
@@ -111,7 +126,7 @@ public class TileCraftCrate extends TileOpenCrate {
Optional<ICraftingRecipe> matchingRecipe = world.getRecipeManager().getRecipe(IRecipeType.CRAFTING, craft, world);
matchingRecipe.ifPresent(recipe -> {
getItemHandler().setInventorySlotContents(9, recipe.getCraftingResult(craft));
craftResult = recipe.getCraftingResult(craft);
List<ItemStack> remainders = recipe.getRemainingItems(craft);
for (int i = 0; i < craft.getSizeInventory(); i++) {
@@ -128,7 +143,7 @@ public class TileCraftCrate extends TileOpenCrate {
}
boolean isFull() {
for (int i = 0; i < 9; i++) {
for (int i = 0; i < getItemHandler().getSizeInventory(); i++) {
if (!isLocked(i) && getItemHandler().getStackInSlot(i).isEmpty()) {
return false;
}
@@ -145,6 +160,10 @@ public class TileCraftCrate extends TileOpenCrate {
}
getItemHandler().setInventorySlotContents(i, ItemStack.EMPTY);
}
if (!craftResult.isEmpty()) {
eject(craftResult, false);
craftResult = ItemStack.EMPTY;
}
}
@Override
@@ -10,13 +10,24 @@ package vazkii.botania.common.block.tile;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Direction;
import net.minecraft.util.LazyValue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Set;
import java.util.stream.IntStream;
/**
* Version of {@link TileSimpleInventory} where the backing inventory is exposed to automation
*/
public abstract class TileExposedSimpleInventory extends TileSimpleInventory implements IInventory {
public abstract class TileExposedSimpleInventory extends TileSimpleInventory implements ISidedInventory {
private final LazyValue<int[]> slots = new LazyValue<>(() -> IntStream.range(0, getSizeInventory()).toArray());
public TileExposedSimpleInventory(TileEntityType<?> type) {
super(type);
}
@@ -60,4 +71,57 @@ public abstract class TileExposedSimpleInventory extends TileSimpleInventory imp
public void clear() {
getItemHandler().clear();
}
@Override
public int getInventoryStackLimit() {
return getItemHandler().getInventoryStackLimit();
}
@Override
public void openInventory(PlayerEntity player) {
getItemHandler().openInventory(player);
}
@Override
public void closeInventory(PlayerEntity player) {
getItemHandler().closeInventory(player);
}
@Override
public boolean isItemValidForSlot(int index, ItemStack stack) {
return getItemHandler().isItemValidForSlot(index, stack);
}
@Override
public int count(Item item) {
return getItemHandler().count(item);
}
@Override
public boolean hasAny(Set<Item> set) {
return getItemHandler().hasAny(set);
}
@Nonnull
@Override
public int[] getSlotsForFace(@Nonnull Direction side) {
return slots.getValue();
}
@Override
public boolean canInsertItem(int index, @Nonnull ItemStack stack, @Nullable Direction direction) {
if (isItemValidForSlot(index, stack)) {
// Vanilla hoppers do not check the inventory's stack limit, so do so here.
// We don't have to check anything else like stackability because the hopper logic will do it
ItemStack existing = getStackInSlot(index);
return existing.isEmpty() || existing.getCount() + stack.getCount() <= getInventoryStackLimit();
}
return false;
}
@Override
public boolean canExtractItem(int index, @Nonnull ItemStack stack, @Nullable Direction direction) {
return true;
}
}
@@ -30,6 +30,7 @@ import vazkii.botania.common.brew.ModBrews;
import vazkii.botania.common.item.ModItems;
import vazkii.botania.common.item.brew.ItemIncenseStick;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
@@ -179,20 +180,8 @@ public class TileIncensePlate extends TileExposedSimpleInventory implements ISid
}
}
private static final int[] SLOTS = { 0 };
@Override
public int[] getSlotsForFace(Direction side) {
return SLOTS;
}
@Override
public boolean canInsertItem(int index, ItemStack itemStackIn, @Nullable Direction direction) {
return true;
}
@Override
public boolean canExtractItem(int index, ItemStack stack, Direction direction) {
public boolean canExtractItem(int index, @Nonnull ItemStack stack, @Nullable Direction direction) {
return false;
}
}