mirror of
https://github.com/GTNewHorizons/Botania.git
synced 2026-11-18 11:19:19 +00:00
Fix crafting halo crash and cleanup inventory utils, closes #385
This commit is contained in:
3 files changed
+33
-280
No files matched your search
@@ -46,57 +46,6 @@ import java.util.stream.StreamSupport;
|
||||
// Some methods from OpenBlocksLib: https://github.com/OpenMods/OpenModsLib
|
||||
public class InventoryHelper {
|
||||
|
||||
protected static boolean areMergeCandidates(ItemStack source, ItemStack target) {
|
||||
return source.isItemEqual(target) && ItemStack.areItemStackTagsEqual(source, target) && target.stackSize < target.getMaxStackSize();
|
||||
}
|
||||
|
||||
public static int testInventoryInsertion(IInventory inventory, ItemStack item, EnumFacing side) {
|
||||
if(item == null || item.stackSize == 0)
|
||||
return 0;
|
||||
item = item.copy();
|
||||
|
||||
if(inventory == null)
|
||||
return 0;
|
||||
|
||||
inventory.getSizeInventory();
|
||||
|
||||
int itemSizeCounter = item.stackSize;
|
||||
int[] availableSlots;
|
||||
|
||||
if(inventory instanceof ISidedInventory)
|
||||
availableSlots = ((ISidedInventory) inventory).getSlotsForFace(side);
|
||||
else {
|
||||
availableSlots = buildSlotsForLinearInventory(inventory);
|
||||
}
|
||||
|
||||
for(int i : availableSlots) {
|
||||
if(itemSizeCounter <= 0)
|
||||
break;
|
||||
|
||||
if (!inventory.isItemValidForSlot(i, item))
|
||||
continue;
|
||||
|
||||
if(side != null && inventory instanceof ISidedInventory)
|
||||
if(!((ISidedInventory)inventory).canInsertItem(i, item, side))
|
||||
continue;
|
||||
|
||||
ItemStack inventorySlot = inventory.getStackInSlot(i);
|
||||
if(inventorySlot == null)
|
||||
itemSizeCounter -= Math.min(Math.min(itemSizeCounter, inventory.getInventoryStackLimit()), item.getMaxStackSize());
|
||||
else if(areMergeCandidates(item, inventorySlot)) {
|
||||
int space = Math.min(inventory.getInventoryStackLimit(), inventorySlot.getMaxStackSize()) - inventorySlot.stackSize;
|
||||
itemSizeCounter -= Math.min(itemSizeCounter, space);
|
||||
}
|
||||
}
|
||||
|
||||
if(itemSizeCounter != item.stackSize) {
|
||||
itemSizeCounter = Math.max(itemSizeCounter, 0);
|
||||
return item.stackSize - itemSizeCounter;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static int[] buildSlotsForLinearInventory(IInventory inv) {
|
||||
int[] slots = new int[inv.getSizeInventory()];
|
||||
for (int i = 0; i < slots.length; i++)
|
||||
@@ -133,213 +82,6 @@ public class InventoryHelper {
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static Iterable<ItemStack> asIterable(final IItemHandler itemHandler) {
|
||||
return new Iterable<ItemStack>() {
|
||||
@Override
|
||||
public Iterator<ItemStack> iterator() {
|
||||
return new Iterator<ItemStack>() {
|
||||
int index = 0;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return index < itemHandler.getSlots();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack next() {
|
||||
return itemHandler.getStackInSlot(index++);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Spliterator<ItemStack> spliterator() {
|
||||
return Spliterators.spliterator(iterator(), itemHandler.getSlots(), Spliterator.ORDERED);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static Stream<ItemStack> streamNonnull(IItemHandler itemHandler) {
|
||||
return StreamSupport.stream(asIterable(itemHandler).spliterator(), false).filter(s -> s != null);
|
||||
}
|
||||
|
||||
public static class GenericInventory implements IInventory {
|
||||
|
||||
protected final String inventoryTitle;
|
||||
protected int slotsCount;
|
||||
protected ItemStack[] inventoryContents;
|
||||
protected final boolean isInvNameLocalized;
|
||||
|
||||
public GenericInventory(String name, boolean isInvNameLocalized, int size) {
|
||||
this.isInvNameLocalized = isInvNameLocalized;
|
||||
slotsCount = size;
|
||||
inventoryTitle = name;
|
||||
inventoryContents = new ItemStack[size];
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack decrStackSize(int par1, int par2) {
|
||||
if(inventoryContents[par1] != null) {
|
||||
ItemStack itemstack;
|
||||
|
||||
if(inventoryContents[par1].stackSize <= par2) {
|
||||
itemstack = inventoryContents[par1];
|
||||
inventoryContents[par1] = null;
|
||||
return itemstack;
|
||||
}
|
||||
|
||||
itemstack = inventoryContents[par1].splitStack(par2);
|
||||
if(inventoryContents[par1].stackSize == 0)
|
||||
inventoryContents[par1] = null;
|
||||
|
||||
return itemstack;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInventoryStackLimit() {
|
||||
return 64;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSizeInventory() {
|
||||
return slotsCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getStackInSlot(int i) {
|
||||
return inventoryContents[i];
|
||||
}
|
||||
|
||||
public ItemStack getStackInSlot(Enum<?> i) {
|
||||
return getStackInSlot(i.ordinal());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack removeStackFromSlot(int i) {
|
||||
if(i >= inventoryContents.length)
|
||||
return null;
|
||||
|
||||
if(inventoryContents[i] != null) {
|
||||
ItemStack itemstack = inventoryContents[i];
|
||||
inventoryContents[i] = null;
|
||||
return itemstack;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isItem(int slot, Item item) {
|
||||
return inventoryContents[slot] != null && inventoryContents[slot].getItem() == item;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isItemValidForSlot(int i, @Nonnull ItemStack itemstack) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getField(int id) { return 0; }
|
||||
|
||||
@Override
|
||||
public void setField(int id, int value) { }
|
||||
|
||||
@Override
|
||||
public int getFieldCount() { return 0; }
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
Arrays.fill(inventoryContents, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUseableByPlayer(@Nonnull EntityPlayer entityplayer) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void clearAndSetSlotCount(int amount) {
|
||||
slotsCount = amount;
|
||||
inventoryContents = new ItemStack[amount];
|
||||
}
|
||||
|
||||
public void readFromNBT(NBTTagCompound tag) {
|
||||
if(tag.hasKey("size"))
|
||||
slotsCount = tag.getInteger("size");
|
||||
|
||||
NBTTagList nbttaglist = tag.getTagList("Items", 10);
|
||||
inventoryContents = new ItemStack[slotsCount];
|
||||
for(int i = 0; i < nbttaglist.tagCount(); i++) {
|
||||
NBTTagCompound stacktag = nbttaglist.getCompoundTagAt(i);
|
||||
int j = stacktag.getByte("Slot");
|
||||
if(j >= 0 && j < inventoryContents.length)
|
||||
inventoryContents[j] = ItemStack.loadItemStackFromNBT(stacktag);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInventorySlotContents(int i, ItemStack itemstack) {
|
||||
inventoryContents[i] = itemstack;
|
||||
|
||||
if(itemstack != null && itemstack.stackSize > getInventoryStackLimit())
|
||||
itemstack.stackSize = getInventoryStackLimit();
|
||||
}
|
||||
|
||||
public void writeToNBT(NBTTagCompound tag) {
|
||||
tag.setInteger("size", getSizeInventory());
|
||||
NBTTagList nbttaglist = new NBTTagList();
|
||||
for(int i = 0; i < inventoryContents.length; i++) {
|
||||
if(inventoryContents[i] != null) {
|
||||
NBTTagCompound stacktag = new NBTTagCompound();
|
||||
stacktag.setByte("Slot", (byte)i);
|
||||
inventoryContents[i].writeToNBT(stacktag);
|
||||
nbttaglist.appendTag(stacktag);
|
||||
}
|
||||
}
|
||||
tag.setTag("Items", nbttaglist);
|
||||
}
|
||||
|
||||
public void copyFrom(IInventory inventory) {
|
||||
for(int i = 0; i < inventory.getSizeInventory(); i++)
|
||||
if(i < getSizeInventory()) {
|
||||
ItemStack stack = inventory.getStackInSlot(i);
|
||||
if(stack != null)
|
||||
setInventorySlotContents(i, stack.copy());
|
||||
else setInventorySlotContents(i, null);
|
||||
}
|
||||
}
|
||||
|
||||
public List<ItemStack> contents() {
|
||||
return Arrays.asList(inventoryContents);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String getName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCustomName() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ITextComponent getDisplayName() {
|
||||
return new TextComponentString(getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markDirty() { }
|
||||
|
||||
@Override
|
||||
public void openInventory(@Nonnull EntityPlayer player) { }
|
||||
|
||||
@Override
|
||||
public void closeInventory(@Nonnull EntityPlayer player) { }
|
||||
}
|
||||
|
||||
public static void dropInventory(TileSimpleInventory inv, World world, IBlockState state, BlockPos pos) {
|
||||
if(inv != null) {
|
||||
for(int j1 = 0; j1 < inv.getSizeInventory(); ++j1) {
|
||||
|
||||
@@ -16,6 +16,7 @@ import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
import vazkii.botania.client.lib.LibResources;
|
||||
import vazkii.botania.common.lib.LibItemNames;
|
||||
|
||||
@@ -33,7 +34,7 @@ public class ItemAutocraftingHalo extends ItemCraftingHalo {
|
||||
|
||||
if(entity instanceof EntityPlayer && !equipped) {
|
||||
EntityPlayer player = (EntityPlayer) entity;
|
||||
IInventory inv = getFakeInv(player);
|
||||
IItemHandler inv = getFakeInv(player);
|
||||
|
||||
for(int i = 1; i < SEGMENTS; i++)
|
||||
tryCraft(player, stack, i, false, inv, false);
|
||||
|
||||
@@ -15,6 +15,7 @@ import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.Gui;
|
||||
import net.minecraft.client.gui.ScaledResolution;
|
||||
import net.minecraft.client.renderer.GlStateManager;
|
||||
import net.minecraft.client.renderer.RenderHelper;
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
import net.minecraft.client.renderer.block.model.ItemCameraTransforms;
|
||||
import net.minecraft.client.renderer.texture.TextureMap;
|
||||
@@ -34,6 +35,7 @@ import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.stats.Achievement;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.minecraft.util.EnumActionResult;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.EnumHand;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
@@ -47,7 +49,10 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.gameevent.PlayerEvent.ItemCraftedEvent;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
import net.minecraftforge.items.CapabilityItemHandler;
|
||||
import net.minecraftforge.items.ItemHandlerHelper;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
import net.minecraftforge.items.ItemStackHandler;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import vazkii.botania.client.core.handler.ClientMethodHandles;
|
||||
import vazkii.botania.client.core.handler.ClientTickHandler;
|
||||
@@ -56,8 +61,6 @@ import vazkii.botania.client.lib.LibResources;
|
||||
import vazkii.botania.common.Botania;
|
||||
import vazkii.botania.common.achievement.ICraftAchievement;
|
||||
import vazkii.botania.common.achievement.ModAchievements;
|
||||
import vazkii.botania.common.core.helper.InventoryHelper;
|
||||
import vazkii.botania.common.core.helper.InventoryHelper.GenericInventory;
|
||||
import vazkii.botania.common.core.helper.ItemNBTHelper;
|
||||
import vazkii.botania.common.core.helper.PlayerHelper;
|
||||
import vazkii.botania.common.core.helper.Vector3;
|
||||
@@ -106,10 +109,15 @@ public class ItemCraftingHalo extends ItemMod implements ICraftAchievement {
|
||||
return ActionResult.newResult(EnumActionResult.SUCCESS, stack);
|
||||
}
|
||||
|
||||
public static IInventory getFakeInv(EntityPlayer player) {
|
||||
GenericInventory tempInv = new GenericInventory("temp", false, player.inventory.getSizeInventory() - 4);
|
||||
tempInv.copyFrom(player.inventory);
|
||||
return tempInv;
|
||||
public static IItemHandler getFakeInv(EntityPlayer player) {
|
||||
ItemStackHandler ret = new ItemStackHandler(player.inventory.mainInventory.length);
|
||||
for(int i = 0; i < player.inventory.mainInventory.length; i++) {
|
||||
ItemStack stack = player.inventory.mainInventory[i];
|
||||
if(stack != null)
|
||||
ret.setStackInSlot(i, player.inventory.mainInventory[i].copy());
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -134,7 +142,7 @@ public class ItemCraftingHalo extends ItemMod implements ICraftAchievement {
|
||||
}
|
||||
}
|
||||
|
||||
void tryCraft(EntityPlayer player, ItemStack stack, int slot, boolean particles, IInventory inv, boolean validate) {
|
||||
void tryCraft(EntityPlayer player, ItemStack stack, int slot, boolean particles, IItemHandler inv, boolean validate) {
|
||||
ItemStack itemForPos = getItemForSlot(stack, slot);
|
||||
if(itemForPos == null)
|
||||
return;
|
||||
@@ -143,7 +151,7 @@ public class ItemCraftingHalo extends ItemMod implements ICraftAchievement {
|
||||
if(validate)
|
||||
recipe = validateRecipe(player, stack, recipe, slot);
|
||||
|
||||
if(canCraft(player, recipe, inv))
|
||||
if(canCraft(recipe, inv))
|
||||
doCraft(player, recipe, particles);
|
||||
}
|
||||
|
||||
@@ -166,18 +174,18 @@ public class ItemCraftingHalo extends ItemMod implements ICraftAchievement {
|
||||
return recipe;
|
||||
}
|
||||
|
||||
private static boolean canCraft(EntityPlayer player, ItemStack[] recipe, IInventory inv) {
|
||||
private static boolean canCraft(ItemStack[] recipe, IItemHandler inv) {
|
||||
if(recipe == null)
|
||||
return false;
|
||||
|
||||
if(recipe[9].stackSize != InventoryHelper.testInventoryInsertion(inv, recipe[9], null))
|
||||
if(ItemHandlerHelper.insertItemStacked(inv, recipe[9], true) != null)
|
||||
return false;
|
||||
|
||||
return consumeRecipeIngredients(recipe, inv, null);
|
||||
}
|
||||
|
||||
private static void doCraft(EntityPlayer player, ItemStack[] recipe, boolean particles) {
|
||||
consumeRecipeIngredients(recipe, player.inventory, player);
|
||||
consumeRecipeIngredients(recipe, player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP), player);
|
||||
if(!player.inventory.addItemStackToInventory(recipe[9]))
|
||||
player.dropItem(recipe[9], false);
|
||||
|
||||
@@ -191,7 +199,7 @@ public class ItemCraftingHalo extends ItemMod implements ICraftAchievement {
|
||||
Botania.proxy.wispFX(player.worldObj, centerVector.x, centerVector.y, centerVector.z, 1F, 0F, 1F, 0.2F + 0.2F * (float) Math.random(), ((float) Math.random() - 0.5F) * m, ((float) Math.random() - 0.5F) * m, ((float) Math.random() - 0.5F) * m);
|
||||
}
|
||||
|
||||
private static boolean consumeRecipeIngredients(ItemStack[] recipe, IInventory inv, EntityPlayer player) {
|
||||
private static boolean consumeRecipeIngredients(ItemStack[] recipe, IItemHandler inv, EntityPlayer player) {
|
||||
for(int i = 0; i < 9; i++) {
|
||||
ItemStack ingredient = recipe[i];
|
||||
if(ingredient != null && !consumeFromInventory(ingredient, inv, player))
|
||||
@@ -201,8 +209,8 @@ public class ItemCraftingHalo extends ItemMod implements ICraftAchievement {
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean consumeFromInventory(ItemStack stack, IInventory inv, EntityPlayer player) {
|
||||
for(int i = 0; i < inv.getSizeInventory(); i++) {
|
||||
private static boolean consumeFromInventory(ItemStack stack, IItemHandler inv, EntityPlayer player) {
|
||||
for(int i = 0; i < inv.getSlots(); i++) {
|
||||
ItemStack stackAt = inv.getStackInSlot(i);
|
||||
if(stackAt != null && stack.isItemEqual(stackAt) && ItemStack.areItemStackTagsEqual(stack, stackAt)) {
|
||||
boolean consume = true;
|
||||
@@ -212,15 +220,15 @@ public class ItemCraftingHalo extends ItemMod implements ICraftAchievement {
|
||||
if(container == stackAt)
|
||||
consume = false;
|
||||
else {
|
||||
ItemHandlerHelper.giveItemToPlayer(player, container);
|
||||
if(player == null)
|
||||
ItemHandlerHelper.insertItem(inv, container, false);
|
||||
else
|
||||
ItemHandlerHelper.giveItemToPlayer(player, container);
|
||||
}
|
||||
}
|
||||
|
||||
if(consume) {
|
||||
stackAt.stackSize--;
|
||||
if(stackAt.stackSize == 0)
|
||||
inv.setInventorySlotContents(i, null);
|
||||
}
|
||||
if(consume)
|
||||
inv.extractItem(i, 1, false);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -460,6 +468,7 @@ public class ItemCraftingHalo extends ItemMod implements ICraftAchievement {
|
||||
ItemStack slotStack = getItemForSlot(stack, seg);
|
||||
if(slotStack != null) {
|
||||
mc.renderEngine.bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);
|
||||
RenderHelper.enableStandardItemLighting();
|
||||
float scale = seg == 0 ? 0.9F : 0.8F;
|
||||
GlStateManager.scale(scale, scale, scale);
|
||||
GlStateManager.rotate(180F, 0F, 1F, 0F);
|
||||
@@ -467,6 +476,7 @@ public class ItemCraftingHalo extends ItemMod implements ICraftAchievement {
|
||||
|
||||
GlStateManager.rotate(90.0F, 0.0F, 1.0F, 0.0F);
|
||||
Minecraft.getMinecraft().getRenderItem().renderItem(slotStack, ItemCameraTransforms.TransformType.GUI);
|
||||
RenderHelper.disableStandardItemLighting();
|
||||
}
|
||||
GlStateManager.popMatrix();
|
||||
|
||||
@@ -587,7 +597,7 @@ public class ItemCraftingHalo extends ItemMod implements ICraftAchievement {
|
||||
}
|
||||
|
||||
int yoff = 110;
|
||||
if(setRecipe && !canCraft(player, recipe, getFakeInv(player))) {
|
||||
if(setRecipe && !canCraft(recipe, getFakeInv(player))) {
|
||||
String warning = TextFormatting.RED + I18n.format("botaniamisc.cantCraft");
|
||||
mc.fontRendererObj.drawStringWithShadow(warning, resolution.getScaledWidth() / 2 - mc.fontRendererObj.getStringWidth(warning) / 2, resolution.getScaledHeight() / 2 - yoff, 0xFFFFFF);
|
||||
yoff += 12;
|
||||
|
||||
Reference in new issue
Block a user