2020-09-22 15:11:20 +02:00
|
|
|
package com.minelittlepony.unicopia.item;
|
2020-01-27 11:05:22 +01:00
|
|
|
|
|
|
|
|
2020-09-27 20:07:55 +02:00
|
|
|
import java.util.Optional;
|
|
|
|
|
2020-04-26 10:10:37 +02:00
|
|
|
import com.minelittlepony.unicopia.UTags;
|
2021-08-19 00:49:02 +02:00
|
|
|
import com.minelittlepony.unicopia.advancement.UCriteria;
|
2020-09-22 15:11:20 +02:00
|
|
|
import com.minelittlepony.unicopia.item.toxin.Toxicity;
|
2020-04-15 14:22:03 +02:00
|
|
|
import com.minelittlepony.unicopia.util.MagicalDamageSource;
|
2020-09-27 20:07:55 +02:00
|
|
|
import com.minelittlepony.unicopia.util.RayTraceHelper;
|
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;
|
|
|
|
import net.minecraft.item.ItemGroup;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2020-09-27 20:07:55 +02:00
|
|
|
import net.minecraft.predicate.entity.EntityPredicates;
|
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;
|
2021-02-14 22:13:12 +01:00
|
|
|
import net.minecraft.text.Text;
|
2020-01-27 11:05:22 +01:00
|
|
|
import net.minecraft.util.ActionResult;
|
2020-06-26 11:44:47 +02:00
|
|
|
import net.minecraft.util.collection.DefaultedList;
|
2020-01-27 11:05:22 +01:00
|
|
|
import net.minecraft.util.Hand;
|
|
|
|
import net.minecraft.util.Rarity;
|
|
|
|
import net.minecraft.util.TypedActionResult;
|
|
|
|
import net.minecraft.util.registry.Registry;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
2021-02-14 21:15:30 +01:00
|
|
|
public class ZapAppleItem extends AppleItem implements ChameleonItem {
|
2020-01-27 11:05:22 +01:00
|
|
|
|
2020-04-25 22:22:28 +02:00
|
|
|
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);
|
2020-01-27 11:05:22 +01:00
|
|
|
|
2020-09-27 20:07:55 +02:00
|
|
|
Optional<Entity> entity = RayTraceHelper.doTrace(player, 5, 1, EntityPredicates.EXCEPT_SPECTATOR.and(e -> canFeedTo(stack, e))).getEntity();
|
2020-01-27 11:05:22 +01:00
|
|
|
|
2020-09-27 20:07:55 +02:00
|
|
|
if (entity.isPresent()) {
|
|
|
|
return onFedTo(stack, player, entity.get());
|
2020-01-27 11:05:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return super.use(world, player, hand);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack finishUsing(ItemStack stack, World w, LivingEntity player) {
|
|
|
|
stack = super.finishUsing(stack, w, player);
|
|
|
|
|
|
|
|
player.damage(MagicalDamageSource.ZAP_APPLE, 120);
|
|
|
|
|
|
|
|
if (w instanceof ServerWorld) {
|
2020-06-26 11:44:47 +02:00
|
|
|
LightningEntity lightning = EntityType.LIGHTNING_BOLT.create(w);
|
2020-09-22 15:11:20 +02:00
|
|
|
lightning.refreshPositionAfterTeleport(player.getX(), player.getY(), player.getZ());
|
2020-06-26 11:44:47 +02:00
|
|
|
|
|
|
|
w.spawnEntity(lightning);
|
2021-08-19 00:49:02 +02:00
|
|
|
if (player instanceof PlayerEntity) {
|
|
|
|
UCriteria.EAT_TRICK_APPLE.trigger((PlayerEntity)player);
|
|
|
|
}
|
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
|
|
|
|
2020-09-22 15:11:20 +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);
|
2020-09-22 15:11:20 +02:00
|
|
|
}
|
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
|
|
|
|
public void appendStacks(ItemGroup tab, DefaultedList<ItemStack> items) {
|
|
|
|
super.appendStacks(tab, items);
|
|
|
|
if (isIn(tab)) {
|
2020-04-26 10:10:37 +02:00
|
|
|
UTags.APPLES.values().forEach(item -> {
|
|
|
|
if (item != this) {
|
|
|
|
ItemStack stack = new ItemStack(this);
|
2021-12-22 10:15:09 +01:00
|
|
|
stack.getOrCreateNbt().putString("appearance", Registry.ITEM.getId(item).toString());
|
2020-04-26 10:10:37 +02:00
|
|
|
items.add(stack);
|
|
|
|
}
|
2020-04-25 23:22:43 +02:00
|
|
|
});
|
2020-01-27 11:05:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-14 22:13:12 +01:00
|
|
|
public Text getName(ItemStack stack) {
|
|
|
|
return hasAppearance(stack) ? getAppearanceStack(stack).getName() : super.getName(stack);
|
2020-01-27 11:05:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-01-27 13:47:14 +01:00
|
|
|
public Toxicity getToxicity(ItemStack stack) {
|
2021-02-14 22:13:12 +01:00
|
|
|
return hasAppearance(stack) ? Toxicity.SEVERE : Toxicity.SAFE;
|
2020-01-27 11:05:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Rarity getRarity(ItemStack stack) {
|
2021-02-14 22:13:12 +01:00
|
|
|
if (hasAppearance(stack)) {
|
2020-01-27 11:05:22 +01:00
|
|
|
return Rarity.EPIC;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Rarity.RARE;
|
|
|
|
}
|
|
|
|
}
|