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

134 lines
4.8 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.item;
2020-01-27 11:05:22 +01:00
2022-12-19 00:12:49 +01:00
import java.util.List;
2020-04-26 10:10:37 +02:00
import com.minelittlepony.unicopia.UTags;
2022-03-26 20:34:15 +01:00
import com.minelittlepony.unicopia.Unicopia;
2021-08-19 00:49:02 +02:00
import com.minelittlepony.unicopia.advancement.UCriteria;
2023-06-02 21:20:30 +02:00
import com.minelittlepony.unicopia.entity.Living;
import com.minelittlepony.unicopia.entity.damage.UDamageTypes;
2022-03-26 20:34:15 +01:00
import com.minelittlepony.unicopia.entity.player.Pony;
2022-12-19 00:12:49 +01:00
import com.minelittlepony.unicopia.item.group.MultiItem;
2022-09-30 12:43:07 +02:00
import com.minelittlepony.unicopia.item.toxin.*;
import com.minelittlepony.unicopia.particle.ParticleUtils;
import com.minelittlepony.unicopia.particle.UParticles;
2022-10-01 18:20:53 +02:00
import com.minelittlepony.unicopia.util.TraceHelper;
2022-12-19 00:12:49 +01:00
import com.minelittlepony.unicopia.util.RegistryUtils;
2022-03-26 20:34:15 +01:00
2020-01-27 11:05:22 +01:00
import net.minecraft.entity.Entity;
2020-06-26 11:44:47 +02:00
import net.minecraft.entity.EntityType;
2020-01-27 11:05:22 +01:00
import net.minecraft.entity.LightningEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.mob.CreeperEntity;
import net.minecraft.entity.passive.PigEntity;
import net.minecraft.entity.passive.VillagerEntity;
import net.minecraft.entity.player.PlayerEntity;
2022-09-30 12:43:07 +02:00
import net.minecraft.item.*;
2021-08-19 00:49:02 +02:00
import net.minecraft.server.network.ServerPlayerEntity;
2020-01-27 11:05:22 +01:00
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
2020-01-27 11:05:22 +01:00
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.Vec3d;
2020-01-27 11:05:22 +01:00
import net.minecraft.util.Hand;
import net.minecraft.util.Rarity;
import net.minecraft.util.TypedActionResult;
2022-12-18 22:07:24 +01:00
import net.minecraft.registry.Registries;
2020-01-27 11:05:22 +01:00
import net.minecraft.world.World;
import net.minecraft.world.event.GameEvent;
2020-01-27 11:05:22 +01:00
2022-12-19 00:12:49 +01:00
public class ZapAppleItem extends Item implements ChameleonItem, ToxicHolder, MultiItem {
public ZapAppleItem(Settings settings) {
super(settings);
2020-01-27 11:05:22 +01:00
}
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
2020-09-27 20:07:55 +02:00
ItemStack stack = player.getStackInHand(hand);
2022-10-01 18:20:53 +02:00
return TraceHelper.findEntity(player, 5, 1, e -> canFeedTo(stack, e))
.map(entity -> onFedTo(stack, player, entity))
.orElseGet(() -> super.use(world, player, hand));
2020-01-27 11:05:22 +01:00
}
@Override
public ItemStack finishUsing(ItemStack stack, World w, LivingEntity player) {
stack = super.finishUsing(stack, w, player);
2023-06-02 21:20:30 +02:00
player.damage(Living.living(player).damageOf(UDamageTypes.ZAP_APPLE), 120);
2020-01-27 11:05:22 +01:00
if (w instanceof ServerWorld) {
2020-06-26 11:44:47 +02:00
LightningEntity lightning = EntityType.LIGHTNING_BOLT.create(w);
lightning.refreshPositionAfterTeleport(player.getX(), player.getY(), player.getZ());
2020-06-26 11:44:47 +02:00
player.onStruckByLightning((ServerWorld)w, lightning);
2021-08-19 00:49:02 +02:00
if (player instanceof PlayerEntity) {
2023-06-02 21:20:30 +02:00
UCriteria.EAT_TRICK_APPLE.trigger(player);
2021-08-19 00:49:02 +02:00
}
2020-01-27 11:05:22 +01:00
}
player.emitGameEvent(GameEvent.LIGHTNING_STRIKE);
ParticleUtils.spawnParticle(w, UParticles.LIGHTNING_BOLT, player.getPos(), Vec3d.ZERO);
2020-01-27 11:05:22 +01:00
return stack;
}
public boolean canFeedTo(ItemStack stack, Entity e) {
return e instanceof VillagerEntity
|| e instanceof CreeperEntity
|| e instanceof PigEntity;
}
public TypedActionResult<ItemStack> onFedTo(ItemStack stack, PlayerEntity player, Entity e) {
2020-06-26 11:44:47 +02:00
LightningEntity lightning = EntityType.LIGHTNING_BOLT.create(e.world);
2021-08-19 00:49:02 +02:00
lightning.refreshPositionAfterTeleport(e.getX(), e.getY(), e.getZ());
lightning.setCosmetic(true);
if (player instanceof ServerPlayerEntity) {
lightning.setChanneler((ServerPlayerEntity)player);
}
2020-06-26 11:44:47 +02:00
if (e.world instanceof ServerWorld) {
e.onStruckByLightning((ServerWorld)e.world, lightning);
2021-08-19 00:49:02 +02:00
UCriteria.FEED_TRICK_APPLE.trigger(player);
}
2021-08-19 00:49:02 +02:00
player.world.spawnEntity(lightning);
2020-01-27 11:05:22 +01:00
2021-08-04 15:38:03 +02:00
if (!player.getAbilities().creativeMode) {
2020-01-27 11:05:22 +01:00
stack.decrement(1);
}
return new TypedActionResult<>(ActionResult.SUCCESS, stack);
}
@Override
2022-12-19 00:12:49 +01:00
public List<ItemStack> getDefaultStacks() {
return Unicopia.SIDE.getPony().map(Pony::asWorld)
2022-12-19 00:12:49 +01:00
.stream()
.flatMap(world -> RegistryUtils.valuesForTag(world, UTags.APPLES))
.filter(a -> a != this).map(item -> {
ItemStack stack = new ItemStack(this);
stack.getOrCreateNbt().putString("appearance", Registries.ITEM.getId(item).toString());
return stack;
}).toList();
2020-01-27 11:05:22 +01:00
}
@Override
public Text getName(ItemStack stack) {
return hasAppearance(stack) ? getAppearanceStack(stack).getName() : super.getName(stack);
2020-01-27 11:05:22 +01:00
}
@Override
2022-09-30 12:43:07 +02:00
public Toxic getToxic(ItemStack stack) {
return hasAppearance(stack) ? Toxics.SEVERE_INNERT : Toxics.FORAGE_EDIBLE;
2020-01-27 11:05:22 +01:00
}
@Override
public Rarity getRarity(ItemStack stack) {
if (hasAppearance(stack)) {
2020-01-27 11:05:22 +01:00
return Rarity.EPIC;
}
return Rarity.RARE;
}
}