2018-10-20 21:19:08 +02:00
|
|
|
package de.ellpeck.naturesaura.items;
|
|
|
|
|
2020-01-29 19:04:33 +01:00
|
|
|
import com.mojang.blaze3d.matrix.MatrixStack;
|
2018-10-21 14:42:30 +02:00
|
|
|
import de.ellpeck.naturesaura.Helper;
|
2019-11-04 19:08:49 +01:00
|
|
|
import de.ellpeck.naturesaura.NaturesAura;
|
2018-11-12 22:04:40 +01:00
|
|
|
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
|
2018-11-11 13:26:19 +01:00
|
|
|
import de.ellpeck.naturesaura.api.aura.container.IAuraContainer;
|
|
|
|
import de.ellpeck.naturesaura.api.aura.container.ItemAuraContainer;
|
2018-12-01 18:56:05 +01:00
|
|
|
import de.ellpeck.naturesaura.api.aura.item.IAuraRecharge;
|
2018-11-21 20:36:55 +01:00
|
|
|
import de.ellpeck.naturesaura.api.render.ITrinketItem;
|
2020-01-25 19:18:45 +01:00
|
|
|
import de.ellpeck.naturesaura.enchant.ModEnchantments;
|
2020-01-29 19:04:33 +01:00
|
|
|
import net.minecraft.client.Minecraft;
|
|
|
|
import net.minecraft.client.renderer.IRenderTypeBuffer;
|
|
|
|
import net.minecraft.client.renderer.model.ItemCameraTransforms;
|
|
|
|
import net.minecraft.client.renderer.texture.OverlayTexture;
|
2020-01-25 19:18:45 +01:00
|
|
|
import net.minecraft.enchantment.EnchantmentHelper;
|
2018-10-20 21:19:08 +02:00
|
|
|
import net.minecraft.entity.Entity;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.inventory.EquipmentSlotType;
|
2020-01-25 19:18:45 +01:00
|
|
|
import net.minecraft.item.ItemGroup;
|
2018-10-20 21:19:08 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
|
|
|
import net.minecraft.util.Direction;
|
2018-10-20 21:19:08 +02:00
|
|
|
import net.minecraft.util.NonNullList;
|
2020-09-22 03:17:02 +02:00
|
|
|
import net.minecraft.util.math.vector.Vector3f;
|
2018-10-20 21:19:08 +02:00
|
|
|
import net.minecraft.world.World;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
|
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
2020-01-25 19:18:45 +01:00
|
|
|
import net.minecraftforge.common.capabilities.Capability;
|
|
|
|
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
2019-11-04 19:08:49 +01:00
|
|
|
import net.minecraftforge.common.util.LazyOptional;
|
2018-10-20 21:19:08 +02:00
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2020-01-26 01:41:49 +01:00
|
|
|
public class ItemAuraCache extends ItemImpl implements ITrinketItem {
|
2018-10-20 21:19:08 +02:00
|
|
|
|
2019-03-18 21:19:06 +01:00
|
|
|
private final int capacity;
|
|
|
|
|
2020-01-26 01:41:49 +01:00
|
|
|
public ItemAuraCache(String name, int capacity) {
|
2020-02-07 23:50:16 +01:00
|
|
|
super(name, new Properties().maxStackSize(1));
|
2019-03-18 21:19:06 +01:00
|
|
|
this.capacity = capacity;
|
2018-10-20 21:19:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-04 19:08:49 +01:00
|
|
|
public void inventoryTick(ItemStack stackIn, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
|
2019-10-20 22:30:49 +02:00
|
|
|
if (!worldIn.isRemote && entityIn instanceof PlayerEntity) {
|
|
|
|
PlayerEntity player = (PlayerEntity) entityIn;
|
2020-09-22 03:17:02 +02:00
|
|
|
if (player.isSneaking() && stackIn.getCapability(NaturesAuraAPI.capAuraContainer).isPresent()) {
|
2019-11-04 19:08:49 +01:00
|
|
|
IAuraContainer container = stackIn.getCapability(NaturesAuraAPI.capAuraContainer).orElse(null);
|
|
|
|
if (container.getStoredAura() <= 0) {
|
2018-12-01 18:56:05 +01:00
|
|
|
return;
|
2019-11-04 19:08:49 +01:00
|
|
|
}
|
2018-12-01 18:56:05 +01:00
|
|
|
for (int i = 0; i < player.inventory.getSizeInventory(); i++) {
|
|
|
|
ItemStack stack = player.inventory.getStackInSlot(i);
|
2020-01-25 19:18:45 +01:00
|
|
|
IAuraRecharge recharge = stack.getCapability(NaturesAuraAPI.capAuraRecharge).orElse(null);
|
|
|
|
if (recharge != null) {
|
|
|
|
if (recharge.rechargeFromContainer(container, itemSlot, i, player.inventory.currentItem == i))
|
|
|
|
break;
|
|
|
|
} else if (EnchantmentHelper.getEnchantmentLevel(ModEnchantments.AURA_MENDING, stack) > 0) {
|
|
|
|
int mainSize = player.inventory.mainInventory.size();
|
|
|
|
boolean isArmor = i >= mainSize && i < mainSize + player.inventory.armorInventory.size();
|
|
|
|
if ((isArmor || player.inventory.currentItem == i) && Helper.rechargeAuraItem(stack, container, 1000))
|
2018-12-01 18:56:05 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-10-20 21:19:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-04 19:08:49 +01:00
|
|
|
public void fillItemGroup(ItemGroup tab, NonNullList<ItemStack> items) {
|
|
|
|
if (this.isInGroup(tab)) {
|
2018-10-20 21:19:08 +02:00
|
|
|
items.add(new ItemStack(this));
|
|
|
|
|
|
|
|
ItemStack stack = new ItemStack(this);
|
2019-11-04 19:08:49 +01:00
|
|
|
stack.getCapability(NaturesAuraAPI.capAuraContainer).ifPresent(container -> {
|
|
|
|
container.storeAura(container.getMaxAura(), false);
|
|
|
|
items.add(stack);
|
|
|
|
});
|
2018-10-20 21:19:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean showDurabilityBar(ItemStack stack) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getDurabilityForDisplay(ItemStack stack) {
|
2019-11-04 19:08:49 +01:00
|
|
|
if (stack.getCapability(NaturesAuraAPI.capAuraContainer).isPresent()) {
|
|
|
|
IAuraContainer container = stack.getCapability(NaturesAuraAPI.capAuraContainer).orElse(null);
|
2018-10-20 21:19:08 +02:00
|
|
|
return 1 - container.getStoredAura() / (double) container.getMaxAura();
|
|
|
|
}
|
2019-11-04 19:08:49 +01:00
|
|
|
return 0;
|
2018-10-20 21:19:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
2019-10-20 22:30:49 +02:00
|
|
|
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundNBT nbt) {
|
2018-10-20 21:19:08 +02:00
|
|
|
return new ICapabilityProvider() {
|
2020-10-19 21:26:32 +02:00
|
|
|
private final LazyOptional<ItemAuraContainer> container = LazyOptional.of(() -> new ItemAuraContainer(stack, null, ItemAuraCache.this.capacity));
|
2018-10-20 21:19:08 +02:00
|
|
|
|
2019-11-04 19:08:49 +01:00
|
|
|
@Nonnull
|
2018-10-20 21:19:08 +02:00
|
|
|
@Override
|
2019-11-04 19:08:49 +01:00
|
|
|
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction facing) {
|
2018-11-12 22:04:40 +01:00
|
|
|
if (capability == NaturesAuraAPI.capAuraContainer) {
|
2020-10-19 21:26:32 +02:00
|
|
|
return this.container.cast();
|
2018-10-20 21:19:08 +02:00
|
|
|
} else {
|
2019-11-04 19:08:49 +01:00
|
|
|
return LazyOptional.empty();
|
2018-10-20 21:19:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2018-10-21 14:42:30 +02:00
|
|
|
|
|
|
|
@Override
|
2019-10-20 22:30:49 +02:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2020-01-29 19:04:33 +01:00
|
|
|
public void render(ItemStack stack, PlayerEntity player, RenderType type, MatrixStack matrices, IRenderTypeBuffer buffer, int packedLight, boolean isHolding) {
|
2018-11-02 21:10:53 +01:00
|
|
|
if (type == RenderType.BODY && !isHolding) {
|
2019-10-20 22:30:49 +02:00
|
|
|
boolean chest = !player.inventory.armorInventory.get(EquipmentSlotType.CHEST.getIndex()).isEmpty();
|
|
|
|
boolean legs = !player.inventory.armorInventory.get(EquipmentSlotType.LEGS.getIndex()).isEmpty();
|
2020-01-29 19:04:33 +01:00
|
|
|
matrices.translate(-0.15F, 0.65F, chest ? -0.195F : legs ? -0.165F : -0.1475F);
|
|
|
|
matrices.scale(0.5F, 0.5F, 0.5F);
|
|
|
|
matrices.rotate(Vector3f.XP.rotationDegrees(180F));
|
2020-09-22 03:17:02 +02:00
|
|
|
Minecraft.getInstance().getItemRenderer().renderItem(stack, ItemCameraTransforms.TransformType.GROUND, packedLight, OverlayTexture.NO_OVERLAY, matrices, buffer);
|
2018-10-21 14:44:16 +02:00
|
|
|
}
|
2018-10-21 14:42:30 +02:00
|
|
|
}
|
2018-10-20 21:19:08 +02:00
|
|
|
}
|