2018-10-19 18:32:20 +02:00
|
|
|
package de.ellpeck.naturesaura.items.tools;
|
|
|
|
|
2018-10-20 21:19:08 +02:00
|
|
|
import de.ellpeck.naturesaura.Helper;
|
2019-11-04 19:08:49 +01:00
|
|
|
import de.ellpeck.naturesaura.NaturesAura;
|
2018-10-20 00:54:33 +02:00
|
|
|
import de.ellpeck.naturesaura.items.ModItems;
|
2018-10-19 18:32:20 +02:00
|
|
|
import de.ellpeck.naturesaura.reg.IModItem;
|
|
|
|
import de.ellpeck.naturesaura.reg.IModelProvider;
|
2020-01-22 01:32:26 +01:00
|
|
|
import de.ellpeck.naturesaura.reg.ModRegistry;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.block.BlockState;
|
|
|
|
import net.minecraft.block.Blocks;
|
2019-11-04 19:08:49 +01:00
|
|
|
import net.minecraft.block.material.Material;
|
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.item.IItemTier;
|
2018-10-20 00:54:33 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2019-11-04 19:08:49 +01:00
|
|
|
import net.minecraft.item.ItemUseContext;
|
|
|
|
import net.minecraft.item.ShovelItem;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
|
|
|
import net.minecraft.util.ActionResultType;
|
|
|
|
import net.minecraft.util.Direction;
|
2018-10-20 00:54:33 +02:00
|
|
|
import net.minecraft.util.SoundCategory;
|
2019-11-04 19:08:49 +01:00
|
|
|
import net.minecraft.util.SoundEvents;
|
2018-10-20 00:54:33 +02:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.world.World;
|
2018-10-20 21:19:08 +02:00
|
|
|
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
2018-10-19 18:32:20 +02:00
|
|
|
|
2018-10-20 21:19:08 +02:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2020-01-26 01:41:49 +01:00
|
|
|
public class ItemShovel extends ShovelItem implements IModItem, IModelProvider {
|
2018-10-19 18:32:20 +02:00
|
|
|
private final String baseName;
|
|
|
|
|
2020-01-26 01:41:49 +01:00
|
|
|
public ItemShovel(String baseName, IItemTier material, float damage, float speed) {
|
2019-11-04 19:08:49 +01:00
|
|
|
super(material, damage, speed, new Properties().group(NaturesAura.CREATIVE_TAB));
|
2018-10-19 18:32:20 +02:00
|
|
|
this.baseName = baseName;
|
2020-01-22 01:32:26 +01:00
|
|
|
ModRegistry.add(this);
|
2018-10-19 18:32:20 +02:00
|
|
|
}
|
|
|
|
|
2018-10-20 00:54:33 +02:00
|
|
|
@Override
|
2019-11-04 19:08:49 +01:00
|
|
|
public ActionResultType onItemUse(ItemUseContext context) {
|
2020-01-23 19:20:47 +01:00
|
|
|
if (this == ModItems.INFUSED_IRON_SHOVEL) {
|
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);
|
2018-10-20 22:40:14 +02:00
|
|
|
int damage = 0;
|
2019-11-04 19:08:49 +01:00
|
|
|
if (state.getBlock() == Blocks.DIRT) {
|
|
|
|
if (world.getBlockState(pos.up()).getMaterial() == Material.AIR) {
|
|
|
|
world.setBlockState(pos, Blocks.GRASS.getDefaultState());
|
2018-10-20 23:23:04 +02:00
|
|
|
damage = 5;
|
2018-10-20 22:40:14 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int range = player.isSneaking() ? 0 : 1;
|
|
|
|
for (int x = -range; x <= range; x++) {
|
|
|
|
for (int y = -range; y <= range; y++) {
|
|
|
|
BlockPos actualPos = pos.add(x, 0, y);
|
2019-11-04 19:08:49 +01:00
|
|
|
Direction facing = context.getFace();
|
2018-10-20 22:40:14 +02:00
|
|
|
if (player.canPlayerEdit(actualPos.offset(facing), facing, stack)) {
|
2019-10-20 22:30:49 +02:00
|
|
|
if (facing != Direction.DOWN
|
2019-11-04 19:08:49 +01:00
|
|
|
&& world.getBlockState(actualPos.up()).getMaterial() == Material.AIR
|
|
|
|
&& world.getBlockState(actualPos).getBlock() == Blocks.GRASS) {
|
|
|
|
if (!world.isRemote) {
|
|
|
|
world.setBlockState(actualPos, Blocks.GRASS_PATH.getDefaultState(), 11);
|
2018-10-20 22:40:14 +02:00
|
|
|
}
|
|
|
|
damage = 1;
|
2018-10-20 00:54:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 22:40:14 +02:00
|
|
|
if (damage > 0) {
|
2019-11-04 19:08:49 +01:00
|
|
|
world.playSound(player, pos, SoundEvents.ITEM_SHOVEL_FLATTEN, SoundCategory.BLOCKS, 1.0F, 1.0F);
|
2020-01-26 00:43:12 +01:00
|
|
|
stack.damageItem(damage, player, playerEntity -> playerEntity.sendBreakAnimation(context.getHand()));
|
2019-10-20 22:30:49 +02:00
|
|
|
return ActionResultType.SUCCESS;
|
2018-10-20 00:54:33 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-20 22:30:49 +02:00
|
|
|
return ActionResultType.PASS;
|
2018-10-20 00:54:33 +02:00
|
|
|
}
|
|
|
|
|
2018-10-19 18:32:20 +02:00
|
|
|
@Override
|
|
|
|
public String getBaseName() {
|
|
|
|
return this.baseName;
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-01-23 19:20:47 +01:00
|
|
|
if (this == ModItems.INFUSED_IRON_SHOVEL)
|
2018-12-01 18:56:05 +01:00
|
|
|
return Helper.makeRechargeProvider(stack, true);
|
2018-10-20 21:19:08 +02:00
|
|
|
else return null;
|
|
|
|
}
|
2018-10-19 18:32:20 +02:00
|
|
|
}
|