Unicopia/src/main/java/com/minelittlepony/unicopia/item/SpearItem.java

102 lines
2.8 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.item;
2019-04-09 13:45:36 +02:00
import javax.annotation.Nullable;
import com.minelittlepony.unicopia.entity.SpearEntity;
2020-04-15 18:12:00 +02:00
import com.minelittlepony.unicopia.magic.Caster;
import com.minelittlepony.unicopia.util.projectile.AdvancedProjectile;
import com.minelittlepony.unicopia.util.projectile.TossableItem;
2019-04-09 13:45:36 +02:00
2020-01-16 16:46:24 +01:00
import net.minecraft.block.BlockState;
2020-01-27 11:05:22 +01:00
import net.minecraft.entity.EquipmentSlot;
2020-01-16 12:35:46 +01:00
import net.minecraft.entity.LivingEntity;
2020-01-16 16:46:24 +01:00
import net.minecraft.entity.player.PlayerEntity;
2019-04-09 13:45:36 +02:00
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
2020-01-27 11:05:22 +01:00
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.UseAction;
2019-04-09 13:45:36 +02:00
import net.minecraft.util.math.BlockPos;
2020-01-16 16:46:24 +01:00
import net.minecraft.util.math.Position;
2019-04-09 13:45:36 +02:00
import net.minecraft.world.World;
2020-04-15 18:12:00 +02:00
public class SpearItem extends Item implements TossableItem {
2019-04-09 13:45:36 +02:00
2020-01-27 11:05:22 +01:00
public SpearItem(Settings settings) {
super(settings);
2019-08-13 09:16:22 +02:00
}
@Override
2020-01-27 11:05:22 +01:00
public UseAction getUseAction(ItemStack stack) {
return UseAction.BOW;
2019-08-13 09:16:22 +02:00
}
@Override
2020-01-27 11:05:22 +01:00
public int getMaxUseTime(ItemStack stack) {
2019-08-13 09:16:22 +02:00
return 440;
2019-04-09 13:45:36 +02:00
}
@Override
2020-01-27 11:05:22 +01:00
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
2020-01-16 12:35:46 +01:00
if (!world.isClient) {
ItemStack itemstack = player.getStackInHand(hand);
2019-08-13 09:16:22 +02:00
if (canBeThrown(itemstack)) {
2020-01-27 11:05:22 +01:00
player.swingHand(hand);
2019-04-09 13:45:36 +02:00
2020-01-27 11:05:22 +01:00
return new TypedActionResult<>(ActionResult.SUCCESS, itemstack);
2019-08-13 09:16:22 +02:00
}
2019-04-09 13:45:36 +02:00
}
2020-01-27 11:05:22 +01:00
return super.use(world, player, hand);
2019-04-09 13:45:36 +02:00
}
2019-08-13 09:16:22 +02:00
@Override
2020-01-27 11:05:22 +01:00
public void onStoppedUsing(ItemStack itemstack, World world, LivingEntity entity, int timeLeft) {
2020-01-16 16:46:24 +01:00
if (entity instanceof PlayerEntity) {
2019-08-13 09:16:22 +02:00
2020-01-27 11:05:22 +01:00
int i = getMaxUseTime(itemstack) - timeLeft;
2019-08-13 09:16:22 +02:00
if (i > 10) {
if (canBeThrown(itemstack)) {
2020-01-27 11:05:22 +01:00
itemstack.damage(1, entity, p -> p.sendEquipmentBreakStatus(EquipmentSlot.MAINHAND));
2020-01-16 16:46:24 +01:00
toss(world, itemstack, (PlayerEntity)entity);
2019-08-13 09:16:22 +02:00
}
}
}
}
2019-04-09 13:45:36 +02:00
@Override
public boolean isEnchantable(ItemStack stack) {
return true;
}
public boolean canApplyAtEnchantingTable(ItemStack stack, net.minecraft.enchantment.Enchantment enchantment) {
switch (enchantment.type) {
case WEAPON:
case BOW:
return true;
default: return false;
}
}
@Nullable
@Override
2020-04-15 18:12:00 +02:00
public AdvancedProjectile createProjectile(World world, PlayerEntity player) {
2020-01-27 11:05:22 +01:00
return new SpearEntity(world, player);
2019-04-09 13:45:36 +02:00
}
@Nullable
@Override
2020-04-15 18:12:00 +02:00
public AdvancedProjectile createProjectile(World world, Position pos) {
2019-04-09 13:45:36 +02:00
return null;
}
@Override
2020-04-15 18:12:00 +02:00
public void onImpact(Caster<?> caster, BlockPos pos, BlockState state) {
2019-04-09 13:45:36 +02:00
}
}