NaturesAura/src/main/java/de/ellpeck/naturesaura/items/tools/ItemPickaxe.java

101 lines
3.9 KiB
Java
Raw Normal View History

2019-11-04 19:08:49 +01:00
package de.ellpeck.naturesaura.items.tools;
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.misc.IWorldData;
2020-01-29 00:40:28 +01:00
import de.ellpeck.naturesaura.data.ItemModelGenerator;
2019-11-04 19:08:49 +01:00
import de.ellpeck.naturesaura.items.ModItems;
import de.ellpeck.naturesaura.misc.WorldData;
2020-01-29 00:40:28 +01:00
import de.ellpeck.naturesaura.reg.ICustomItemModel;
2019-11-04 19:08:49 +01:00
import de.ellpeck.naturesaura.reg.IModItem;
2020-01-22 01:32:26 +01:00
import de.ellpeck.naturesaura.reg.ModRegistry;
2019-11-04 19:08:49 +01:00
import net.minecraft.block.BlockState;
2020-05-13 17:57:31 +02:00
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.ItemEntity;
2019-11-04 19:08:49 +01:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.IItemTier;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.item.PickaxeItem;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
2020-05-13 17:57:31 +02:00
import net.minecraft.util.math.AxisAlignedBB;
2019-11-04 19:08:49 +01:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import javax.annotation.Nullable;
2020-01-29 00:40:28 +01:00
public class ItemPickaxe extends PickaxeItem implements IModItem, ICustomItemModel {
2019-11-04 19:08:49 +01:00
private final String baseName;
2020-01-26 01:41:49 +01:00
public ItemPickaxe(String baseName, IItemTier material, int damage, float speed) {
2019-11-04 19:08:49 +01:00
super(material, damage, speed, new Properties().group(NaturesAura.CREATIVE_TAB));
this.baseName = baseName;
2020-01-22 01:32:26 +01:00
ModRegistry.add(this);
2019-11-04 19:08:49 +01:00
}
@Override
public String getBaseName() {
return this.baseName;
}
@Override
public ActionResultType onItemUse(ItemUseContext context) {
2020-01-23 19:20:47 +01:00
if (this == ModItems.INFUSED_IRON_PICKAXE) {
2019-11-04 19:08:49 +01:00
PlayerEntity player = context.getPlayer();
World world = context.getWorld();
BlockPos pos = context.getPos();
ItemStack stack = player.getHeldItem(context.getHand());
BlockState state = world.getBlockState(pos);
BlockState result = NaturesAuraAPI.BOTANIST_PICKAXE_CONVERSIONS.get(state);
if (result != null) {
if (!world.isRemote) {
2019-11-04 19:08:49 +01:00
world.setBlockState(pos, result);
WorldData data = (WorldData) IWorldData.getWorldData(world);
2020-03-14 03:10:56 +01:00
data.addMossStone(pos);
}
2019-11-04 19:08:49 +01:00
world.playSound(player, pos, SoundEvents.BLOCK_STONE_PLACE, SoundCategory.BLOCKS, 1.0F, 1.0F);
stack.damageItem(15, player, playerEntity -> playerEntity.sendBreakAnimation(context.getHand()));
2019-11-04 19:08:49 +01:00
return ActionResultType.SUCCESS;
}
}
return ActionResultType.PASS;
}
2020-05-13 17:57:31 +02:00
@Override
public void inventoryTick(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
if (this == ModItems.SKY_PICKAXE) {
if (!(entityIn instanceof PlayerEntity))
return;
if (!isSelected || worldIn.isRemote)
return;
AxisAlignedBB bounds = new AxisAlignedBB(entityIn.getPosition()).grow(3.5F);
for (ItemEntity item : worldIn.getEntitiesWithinAABB(ItemEntity.class, bounds)) {
// only pick up freshly dropped items
if (item.getAge() >= 5)
continue;
item.setPickupDelay(0);
item.onCollideWithPlayer((PlayerEntity) entityIn);
}
}
}
2019-11-04 19:08:49 +01:00
@Nullable
@Override
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundNBT nbt) {
2020-05-13 15:52:46 +02:00
return Helper.makeRechargeProvider(stack, true);
2019-11-04 19:08:49 +01:00
}
2020-01-29 00:40:28 +01:00
@Override
public void generateCustomItemModel(ItemModelGenerator generator) {
generator.withExistingParent(this.getBaseName(), "item/handheld").texture("layer0", "item/" + this.getBaseName());
}
2019-11-04 19:08:49 +01:00
}