Unicopia/src/main/java/com/minelittlepony/unicopia/toxin/ToxicBlockItem.java

99 lines
3.3 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.toxin;
2020-01-27 11:05:22 +01:00
import java.util.List;
import javax.annotation.Nullable;
import com.minelittlepony.unicopia.Race;
2020-04-15 18:12:00 +02:00
import com.minelittlepony.unicopia.entity.player.Pony;
2020-04-15 19:06:45 +02:00
import com.minelittlepony.unicopia.item.UEffects;
2020-01-27 11:05:22 +01:00
2020-01-27 13:47:14 +01:00
import net.minecraft.block.Block;
2020-01-27 11:05:22 +01:00
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.player.PlayerEntity;
2020-01-27 13:47:14 +01:00
import net.minecraft.item.BlockItem;
import net.minecraft.item.FoodComponent;
2020-01-27 11:05:22 +01:00
import net.minecraft.item.Item;
2020-01-27 13:47:14 +01:00
import net.minecraft.item.ItemGroup;
2020-01-27 11:05:22 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.UseAction;
import net.minecraft.world.World;
2020-01-27 13:47:14 +01:00
public class ToxicBlockItem extends BlockItem implements Toxic, Toxin {
2020-01-27 11:05:22 +01:00
2020-01-27 13:47:14 +01:00
private final UseAction action;
2020-01-27 11:05:22 +01:00
private final Toxicity toxicity;
2020-01-27 13:47:14 +01:00
public ToxicBlockItem(Block block, Item.Settings settings, int hunger, float saturation, UseAction action, Toxicity toxicity) {
super(block, settings
.group(ItemGroup.FOOD)
.food(new FoodComponent.Builder()
.hunger(hunger)
.saturationModifier(saturation)
.build()));
2020-01-27 11:05:22 +01:00
this.toxicity = toxicity;
2020-01-27 13:47:14 +01:00
this.action = action;
2020-01-27 11:05:22 +01:00
}
@Override
2020-01-27 13:47:14 +01:00
public Toxicity getToxicity(ItemStack stack) {
2020-01-27 11:05:22 +01:00
return toxicity;
}
@Override
public UseAction getUseAction(ItemStack stack) {
2020-01-27 13:47:14 +01:00
return action;
2020-01-27 11:05:22 +01:00
}
@Override
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {
2020-01-27 13:47:14 +01:00
tooltip.add(getToxicity(stack).getTooltip());
2020-01-27 11:05:22 +01:00
}
@Override
public ItemStack finishUsing(ItemStack stack, World world, LivingEntity entity) {
super.finishUsing(stack, world, entity);
if (entity instanceof PlayerEntity) {
2020-04-15 18:12:00 +02:00
Race race = Pony.of((PlayerEntity)entity).getSpecies();
2020-01-27 13:47:14 +01:00
Toxicity toxicity = (race.isDefault() || race == Race.CHANGELING) ? Toxicity.LETHAL : getToxicity(stack);
2020-01-27 11:05:22 +01:00
addSecondaryEffects((PlayerEntity)entity, toxicity, stack);
}
return new ItemStack(getRecipeRemainder());
}
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
2020-04-15 18:12:00 +02:00
Race race = Pony.of(player).getSpecies();
2020-01-27 11:05:22 +01:00
if (race.isDefault() || race == Race.CHANGELING) {
return new TypedActionResult<>(ActionResult.FAIL, player.getStackInHand(hand));
}
return super.use(world, player, hand);
}
@Override
public void addSecondaryEffects(PlayerEntity player, Toxicity toxicity, ItemStack stack) {
if (toxicity.toxicWhenRaw()) {
2020-04-22 16:28:20 +02:00
player.addStatusEffect(toxicity.getPoisonEffect());
2020-01-27 11:05:22 +01:00
}
if (toxicity.isLethal()) {
2020-04-22 16:28:20 +02:00
player.addStatusEffect(new StatusEffectInstance(UEffects.FOOD_POISONING, 300, 7, false, false));
2020-01-27 11:05:22 +01:00
} else if (toxicity.toxicWhenCooked()) {
2020-04-22 16:28:20 +02:00
player.addStatusEffect(new StatusEffectInstance(StatusEffects.NAUSEA, 3, 1, false, false));
2020-01-27 11:05:22 +01:00
}
}
}