Refactor getAccesoriesInventory to use vanilla interface

This commit is contained in:
Vincent Lee committed 2020-07-06 20:38:44 -05:00
1 parent b631775c32
commit 9269aeb73d
10 files changed
+153 -88

No files matched your search

@@ -12,6 +12,8 @@ import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.*;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.util.*;
@@ -215,8 +217,8 @@ public interface BotaniaAPI {
return 0;
}
default IItemHandlerModifiable getAccessoriesInventory(PlayerEntity player) {
return new EmptyHandler();
default IInventory getAccessoriesInventory(PlayerEntity player) {
return new Inventory(0);
}
/**
@@ -20,6 +20,7 @@ import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Direction;
import net.minecraft.util.Util;
@@ -30,11 +31,6 @@ import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.util.math.vector.Matrix4f;
import net.minecraft.world.World;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.wrapper.CombinedInvWrapper;
import vazkii.botania.api.BotaniaAPI;
import vazkii.botania.api.item.IWireframeCoordinateListProvider;
@@ -85,34 +81,33 @@ public final class BoundTileRenderer {
}
}
LazyOptional<IItemHandler> mainInvCap = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, Direction.UP);
mainInvCap.ifPresent(mainInv -> {
IItemHandlerModifiable acc = BotaniaAPI.instance().getAccessoriesInventory(player);
IItemHandler joined = acc != null ? new CombinedInvWrapper((IItemHandlerModifiable) mainInv, acc) : mainInv;
for (int i = 0; i < joined.getSlots(); i++) {
ItemStack stackInSlot = joined.getStackInSlot(i);
if (!stackInSlot.isEmpty() && stackInSlot.getItem() instanceof IWireframeCoordinateListProvider) {
IWireframeCoordinateListProvider provider = (IWireframeCoordinateListProvider) stackInSlot.getItem();
List<BlockPos> coordsList = provider.getWireframesToDraw(player, stackInSlot);
for (BlockPos coords : coordsList) {
renderBlockOutlineAt(ms, LINE_BUFFERS, coords, color);
}
BlockPos coords = provider.getSourceWireframe(player, stackInSlot);
if (coords != null && coords.getY() > -1) {
renderBlockOutlineAt(ms, LINE_BUFFERS, coords, color, true);
}
}
}
});
renderWireframeProviders(player.inventory, player, ms, color);
renderWireframeProviders(BotaniaAPI.instance().getAccessoriesInventory(player), player, ms, color);
ms.pop();
RenderSystem.disableDepthTest();
LINE_BUFFERS.finish();
}
private static void renderWireframeProviders(IInventory inv, PlayerEntity player, MatrixStack ms, int color) {
for (int i = 0; i < inv.getSizeInventory(); i++) {
ItemStack stackInSlot = inv.getStackInSlot(i);
if (!stackInSlot.isEmpty() && stackInSlot.getItem() instanceof IWireframeCoordinateListProvider) {
IWireframeCoordinateListProvider provider = (IWireframeCoordinateListProvider) stackInSlot.getItem();
List<BlockPos> coordsList = provider.getWireframesToDraw(player, stackInSlot);
for (BlockPos coords : coordsList) {
renderBlockOutlineAt(ms, LINE_BUFFERS, coords, color);
}
BlockPos coords = provider.getSourceWireframe(player, stackInSlot);
if (coords != null && coords.getY() > -1) {
renderBlockOutlineAt(ms, LINE_BUFFERS, coords, color, true);
}
}
}
}
private static void renderBlockOutlineAt(MatrixStack ms, IRenderTypeBuffer buffers, BlockPos pos, int color) {
renderBlockOutlineAt(ms, buffers, pos, color, false);
}
@@ -18,6 +18,7 @@ import net.minecraft.client.gui.AbstractGui;
import net.minecraft.client.gui.screen.ChatScreen;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.profiler.IProfiler;
@@ -31,9 +32,6 @@ import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.wrapper.EmptyHandler;
import org.lwjgl.opengl.GL11;
@@ -173,18 +171,15 @@ public final class HUDHandler {
boolean anyRequest = false;
boolean creative = false;
IItemHandler mainInv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElse(EmptyHandler.INSTANCE);
IItemHandler accInv = BotaniaAPI.instance().getAccessoriesInventory(player);
IInventory mainInv = player.inventory;
IInventory accInv = BotaniaAPI.instance().getAccessoriesInventory(player);
int invSize = mainInv.getSlots();
int size = invSize;
if (accInv != null) {
size += accInv.getSlots();
}
int invSize = mainInv.getSizeInventory();
int size = invSize + accInv.getSizeInventory();
for (int i = 0; i < size; i++) {
boolean useAccessories = i >= invSize;
IItemHandler inv = useAccessories ? accInv : mainInv;
IInventory inv = useAccessories ? accInv : mainInv;
ItemStack stack = inv.getStackInSlot(i - (useAccessories ? invSize : 0));
if (!stack.isEmpty()) {
@@ -12,6 +12,7 @@ import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.DyeColor;
import net.minecraft.item.DyeItem;
import net.minecraft.item.ItemStack;
@@ -105,8 +106,8 @@ public class EntitySpark extends EntitySparkBase implements ISparkEntity {
stacks.addAll(player.inventory.mainInventory);
stacks.addAll(player.inventory.armorInventory);
IItemHandler inv = BotaniaAPI.instance().getAccessoriesInventory(player);
for (int i = 0; i < inv.getSlots(); i++) {
IInventory inv = BotaniaAPI.instance().getAccessoriesInventory(player);
for (int i = 0; i < inv.getSizeInventory(); i++) {
stacks.add(inv.getStackInSlot(i));
}
@@ -12,6 +12,7 @@ import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.*;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.util.*;
@@ -19,7 +20,6 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.wrapper.EmptyHandler;
import vazkii.botania.api.BotaniaAPI;
@@ -36,6 +36,7 @@ import vazkii.botania.common.core.handler.ConfigHandler;
import vazkii.botania.common.core.handler.EquipmentHandler;
import vazkii.botania.common.core.handler.ManaNetworkHandler;
import vazkii.botania.common.integration.corporea.WrappedIInventory;
import vazkii.botania.common.item.CapWrapper;
import vazkii.botania.common.item.ModItems;
import vazkii.botania.common.item.relic.ItemLokiRing;
import vazkii.botania.common.lib.LibMisc;
@@ -236,8 +237,8 @@ public class BotaniaAPIImpl implements BotaniaAPI {
}
@Override
public IItemHandlerModifiable getAccessoriesInventory(PlayerEntity player) {
return EquipmentHandler.getAllWorn(player).orElseGet(EmptyHandler::new);
public IInventory getAccessoriesInventory(PlayerEntity player) {
return new CapWrapper(EquipmentHandler.getAllWorn(player).orElseGet(EmptyHandler::new));
}
@Override
@@ -11,6 +11,7 @@ package vazkii.botania.common.impl.mana;
import com.google.common.collect.Iterables;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.items.CapabilityItemHandler;
@@ -54,14 +55,11 @@ public class ManaItemHandlerImpl implements ManaItemHandler {
return Collections.emptyList();
}
IItemHandler acc = BotaniaAPI.instance().getAccessoriesInventory(player);
if (acc == null) {
return Collections.emptyList();
}
IInventory acc = BotaniaAPI.instance().getAccessoriesInventory(player);
List<ItemStack> toReturn = new ArrayList<>(acc.getSlots());
List<ItemStack> toReturn = new ArrayList<>(acc.getSizeInventory());
for (int slot = 0; slot < acc.getSlots(); slot++) {
for (int slot = 0; slot < acc.getSizeInventory(); slot++) {
ItemStack stackInSlot = acc.getStackInSlot(slot);
if (!stackInSlot.isEmpty() && stackInSlot.getItem() instanceof IManaItem) {
@@ -0,0 +1,77 @@
/*
* This class is distributed as part of the Botania Mod.
* Get the Source Code in github:
* https://github.com/Vazkii/Botania
*
* Botania is Open Source and distributed under the
* Botania License: http://botaniamod.net/license.php
*/
package vazkii.botania.common.item;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.IItemHandlerModifiable;
public class CapWrapper implements IInventory {
private final IItemHandler handler;
public CapWrapper(IItemHandler handler) {
this.handler = handler;
}
@Override
public int getSizeInventory() {
return handler.getSlots();
}
@Override
public boolean isEmpty() {
for (int i = 0; i < getSizeInventory(); i++) {
if (!getStackInSlot(i).isEmpty()) {
return false;
}
}
return true;
}
@Override
public ItemStack getStackInSlot(int index) {
return handler.getStackInSlot(index);
}
@Override
public ItemStack decrStackSize(int index, int count) {
return handler.extractItem(index, count, false);
}
@Override
public ItemStack removeStackFromSlot(int index) {
return handler.extractItem(index, Integer.MAX_VALUE, false);
}
@Override
public void setInventorySlotContents(int index, ItemStack stack) {
if (handler instanceof IItemHandlerModifiable) {
((IItemHandlerModifiable) handler).setStackInSlot(index, stack);
}
}
@Override
public void markDirty() {
}
@Override
public boolean isUsableByPlayer(PlayerEntity player) {
return false;
}
@Override
public void clear() {
for (int i = 0; i < getSizeInventory(); i++) {
removeStackFromSlot(i);
}
}
}
@@ -86,13 +86,13 @@ public class ItemEnderHand extends Item implements IManaUsingItem, IBlockProvide
return false;
}
ItemStack istack = ItemExchangeRod.removeFromInventory(player, new InvWrapper(player.getInventoryEnderChest()), stack, block, false);
ItemStack istack = ItemExchangeRod.removeFromInventory(player, player.getInventoryEnderChest(), stack, block, false);
if (!istack.isEmpty()) {
boolean mana = ManaItemHandler.instance().requestManaExact(stack, player, COST_PROVIDE, false);
if (mana) {
if (doit) {
ManaItemHandler.instance().requestManaExact(stack, player, COST_PROVIDE, true);
ItemExchangeRod.removeFromInventory(player, new InvWrapper(player.getInventoryEnderChest()), stack, block, true);
ItemExchangeRod.removeFromInventory(player, player.getInventoryEnderChest(), stack, block, true);
}
return true;
@@ -108,7 +108,7 @@ public class ItemEnderHand extends Item implements IManaUsingItem, IBlockProvide
return 0;
}
return ItemExchangeRod.getInventoryItemCount(player, new InvWrapper(player.getInventoryEnderChest()), stack, block);
return ItemExchangeRod.getInventoryItemCount(player, player.getInventoryEnderChest(), stack, block);
}
}
@@ -37,17 +37,16 @@ import net.minecraft.util.math.BlockPos;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.wrapper.InvWrapper;
import net.minecraftforge.items.wrapper.EmptyHandler;
import vazkii.botania.api.BotaniaAPI;
import vazkii.botania.client.core.handler.MiscellaneousIcons;
import vazkii.botania.client.fx.WispParticleData;
import vazkii.botania.common.Botania;
import vazkii.botania.common.core.handler.EquipmentHandler;
import vazkii.botania.common.core.helper.ItemNBTHelper;
import vazkii.botania.common.item.CapWrapper;
import java.util.List;
@@ -140,11 +139,10 @@ public class ItemItemFinder extends ItemBauble {
entIdBuilder.add(item.getEntityId());
}
} else if (e instanceof PlayerEntity) {
PlayerEntity player_ = (PlayerEntity) e;
LazyOptional<IItemHandler> playerInv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY);
LazyOptional<IItemHandlerModifiable> binv = EquipmentHandler.getAllWorn(player);
if (scanInventory(playerInv, pstack) || scanInventory(binv, pstack)) {
entIdBuilder.add(player_.getEntityId());
PlayerEntity targetPlayer = (PlayerEntity) e;
IInventory binv = BotaniaAPI.instance().getAccessoriesInventory(targetPlayer);
if (scanInventory(targetPlayer.inventory, pstack) || scanInventory(binv, pstack)) {
entIdBuilder.add(targetPlayer.getEntityId());
}
} else if (e instanceof AbstractVillagerEntity) {
@@ -157,12 +155,13 @@ public class ItemItemFinder extends ItemBauble {
}
}
} else if (e.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).isPresent()) {
if (scanInventory(e.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY), pstack)) {
IItemHandler cap = e.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElse(EmptyHandler.INSTANCE);
if (scanInventory(new CapWrapper(cap), pstack)) {
entIdBuilder.add(e.getEntityId());
}
} else if (e instanceof IInventory) {
IInventory inv = (IInventory) e;
if (scanInventory(LazyOptional.of(() -> new InvWrapper(inv)), pstack)) {
if (scanInventory(inv, pstack)) {
entIdBuilder.add(e.getEntityId());
}
}
@@ -176,7 +175,8 @@ public class ItemItemFinder extends ItemBauble {
if (tile != null) {
boolean foundCap = false;
for (Direction e : Direction.values()) {
if (scanInventory(tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, e), pstack)) {
IItemHandler cap = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, e).orElse(EmptyHandler.INSTANCE);
if (scanInventory(new CapWrapper(cap), pstack)) {
blockPosBuilder.add(LongNBT.valueOf(pos_.toLong()));
foundCap = true;
break;
@@ -184,7 +184,7 @@ public class ItemItemFinder extends ItemBauble {
}
if (!foundCap && tile instanceof IInventory) {
IInventory inv = (IInventory) tile;
if (scanInventory(LazyOptional.of(() -> new InvWrapper(inv)), pstack)) {
if (scanInventory(inv, pstack)) {
blockPosBuilder.add(LongNBT.valueOf(pos_.toLong()));
}
}
@@ -198,16 +198,12 @@ public class ItemItemFinder extends ItemBauble {
return stack1.isItemEqual(stack2) && ItemStack.areItemStackTagsEqual(stack1, stack2);
}
private boolean scanInventory(LazyOptional<? extends IItemHandler> optInv, ItemStack pstack) {
return optInv.map(inv -> scanInventory(inv, pstack)).orElse(false);
}
private boolean scanInventory(IItemHandler inv, ItemStack pstack) {
private boolean scanInventory(IInventory inv, ItemStack pstack) {
if (pstack.isEmpty()) {
return false;
}
for (int l = 0; l < inv.getSlots(); l++) {
for (int l = 0; l < inv.getSizeInventory(); l++) {
ItemStack istack = inv.getStackInSlot(l);
if (!istack.isEmpty() && equalStacks(istack, pstack)) {
return true;
@@ -15,6 +15,7 @@ import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
@@ -232,13 +233,9 @@ public class ItemExchangeRod extends Item implements IManaUsingItem, IWireframeC
return !getState(stack).isAir();
}
public static ItemStack removeFromInventory(PlayerEntity player, LazyOptional<IItemHandler> inv, ItemStack stack, Block block, boolean doit) {
return inv.map(handler -> removeFromInventory(player, handler, stack, block, doit)).orElse(ItemStack.EMPTY);
}
public static ItemStack removeFromInventory(PlayerEntity player, IItemHandler inv, ItemStack stack, Block block, boolean doit) {
public static ItemStack removeFromInventory(PlayerEntity player, IInventory inv, ItemStack stack, Block block, boolean doit) {
List<ItemStack> providers = new ArrayList<>();
for (int i = inv.getSlots() - 1; i >= 0; i--) {
for (int i = inv.getSizeInventory() - 1; i >= 0; i--) {
ItemStack invStack = inv.getStackInSlot(i);
if (invStack.isEmpty()) {
continue;
@@ -246,7 +243,14 @@ public class ItemExchangeRod extends Item implements IManaUsingItem, IWireframeC
Item item = invStack.getItem();
if (item == block.asItem()) {
return inv.extractItem(i, 1, !doit);
ItemStack ret;
if (doit) {
ret = inv.decrStackSize(i, 1);
} else {
ret = invStack.copy();
ret.setCount(1);
}
return ret;
}
if (item instanceof IBlockProvider) {
@@ -271,7 +275,7 @@ public class ItemExchangeRod extends Item implements IManaUsingItem, IWireframeC
ItemStack outStack = removeFromInventory(player, BotaniaAPI.instance().getAccessoriesInventory(player), stack, block, doit);
if (outStack.isEmpty()) {
outStack = removeFromInventory(player, player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY), stack, block, doit);
outStack = removeFromInventory(player, player.inventory, stack, block, doit);
}
return outStack;
}
@@ -286,7 +290,7 @@ public class ItemExchangeRod extends Item implements IManaUsingItem, IWireframeC
return -1;
}
int count = getInventoryItemCount(player, player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY), stack, block);
int count = getInventoryItemCount(player, player.inventory, stack, block);
if (count == -1) {
return -1;
}
@@ -294,17 +298,13 @@ public class ItemExchangeRod extends Item implements IManaUsingItem, IWireframeC
return count + baubleCount;
}
public static int getInventoryItemCount(PlayerEntity player, LazyOptional<IItemHandler> inv, ItemStack stack, Block block) {
return inv.map(handler -> getInventoryItemCount(player, handler, stack, block)).orElse(0);
}
public static int getInventoryItemCount(PlayerEntity player, IItemHandler inv, ItemStack stack, Block block) {
public static int getInventoryItemCount(PlayerEntity player, IInventory inv, ItemStack stack, Block block) {
if (player.abilities.isCreativeMode) {
return -1;
}
int count = 0;
for (int i = 0; i < inv.getSlots(); i++) {
for (int i = 0; i < inv.getSizeInventory(); i++) {
ItemStack invStack = inv.getStackInSlot(i);
if (invStack.isEmpty()) {
continue;