NaturesAura/src/main/java/de/ellpeck/naturesaura/items/ItemAuraCache.java

133 lines
5.8 KiB
Java
Raw Normal View History

2018-10-20 21:19:08 +02:00
package de.ellpeck.naturesaura.items;
2021-12-15 16:24:53 +01:00
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Vector3f;
import de.ellpeck.naturesaura.Helper;
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;
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;
2021-12-15 16:24:53 +01:00
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.block.model.ItemTransforms;
2020-01-29 19:04:33 +01:00
import net.minecraft.client.renderer.texture.OverlayTexture;
2021-12-15 16:24:53 +01:00
import net.minecraft.core.Direction;
import net.minecraft.core.NonNullList;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2021-12-15 16:24:53 +01:00
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.level.Level;
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) {
2021-12-15 16:24:53 +01:00
super(name, new Properties().stacksTo(1));
2019-03-18 21:19:06 +01:00
this.capacity = capacity;
2018-10-20 21:19:08 +02:00
}
@Override
2021-12-04 15:40:09 +01:00
public void inventoryTick(ItemStack stackIn, Level levelIn, Entity entityIn, int itemSlot, boolean isSelected) {
2021-12-15 16:24:53 +01:00
if (!levelIn.isClientSide && entityIn instanceof Player player) {
if (player.isCrouching() && stackIn.getCapability(NaturesAuraAPI.capAuraContainer).isPresent()) {
2021-12-15 16:30:22 +01:00
var container = stackIn.getCapability(NaturesAuraAPI.capAuraContainer).orElse(null);
2019-11-04 19:08:49 +01:00
if (container.getStoredAura() <= 0) {
2018-12-01 18:56:05 +01:00
return;
2019-11-04 19:08:49 +01:00
}
2021-12-15 16:30:22 +01:00
for (var i = 0; i < player.getInventory().getContainerSize(); i++) {
var stack = player.getInventory().getItem(i);
var recharge = stack.getCapability(NaturesAuraAPI.capAuraRecharge).orElse(null);
2020-01-25 19:18:45 +01:00
if (recharge != null) {
2021-12-15 16:24:53 +01:00
if (recharge.rechargeFromContainer(container, itemSlot, i, player.getInventory().selected == i))
2020-01-25 19:18:45 +01:00
break;
2021-12-15 16:24:53 +01:00
} else if (EnchantmentHelper.getItemEnchantmentLevel(ModEnchantments.AURA_MENDING, stack) > 0) {
2021-12-15 16:30:22 +01:00
var mainSize = player.getInventory().items.size();
var isArmor = i >= mainSize && i < mainSize + player.getInventory().armor.size();
2021-12-15 16:24:53 +01:00
if ((isArmor || player.getInventory().selected == i) && Helper.rechargeAuraItem(stack, container, 1000))
2018-12-01 18:56:05 +01:00
break;
}
2018-10-20 21:19:08 +02:00
}
}
}
}
@Override
2021-12-15 16:24:53 +01:00
public void fillItemCategory(CreativeModeTab tab, NonNullList<ItemStack> items) {
if (this.allowdedIn(tab)) {
2018-10-20 21:19:08 +02:00
items.add(new ItemStack(this));
2021-12-15 16:30:22 +01:00
var 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
2021-12-15 16:24:53 +01:00
public boolean isBarVisible(ItemStack stack) {
2018-10-20 21:19:08 +02:00
return true;
}
@Override
public int getBarWidth(ItemStack stack) {
2019-11-04 19:08:49 +01:00
if (stack.getCapability(NaturesAuraAPI.capAuraContainer).isPresent()) {
2021-12-15 16:30:22 +01:00
var container = stack.getCapability(NaturesAuraAPI.capAuraContainer).orElse(null);
return Math.round((container.getStoredAura() / (float) container.getMaxAura()) * 13);
2018-10-20 21:19:08 +02:00
}
2019-11-04 19:08:49 +01:00
return 0;
2018-10-20 21:19:08 +02:00
}
@Override
public int getBarColor(ItemStack stack) {
var cap = stack.getCapability(NaturesAuraAPI.capAuraContainer).orElse(null);
return cap != null ? cap.getAuraColor() : super.getBarColor(stack);
}
2018-10-20 21:19:08 +02:00
@Nullable
@Override
2021-12-04 15:40:09 +01:00
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundTag 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
}
}
};
}
@Override
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
2021-12-15 16:24:53 +01:00
public void render(ItemStack stack, Player player, RenderType type, PoseStack matrices, MultiBufferSource buffer, int packedLight, boolean isHolding) {
if (type == RenderType.BODY && !isHolding) {
2021-12-15 16:30:22 +01:00
var chest = !player.getInventory().armor.get(EquipmentSlot.CHEST.getIndex()).isEmpty();
var legs = !player.getInventory().armor.get(EquipmentSlot.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);
2021-12-15 16:24:53 +01:00
matrices.mulPose(Vector3f.XP.rotationDegrees(180F));
Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemTransforms.TransformType.GROUND, packedLight, OverlayTexture.NO_OVERLAY, matrices, buffer, 0);
2018-10-21 14:44:16 +02:00
}
}
2018-10-20 21:19:08 +02:00
}