| 1 | package net.minecraft.world.entity.projectile.arrow;
|
| 2 |
|
| 3 | import net.minecraft.core.component.DataComponents;
|
| 4 | import net.minecraft.core.particles.ColorParticleOption;
|
| 5 | import net.minecraft.core.particles.ParticleTypes;
|
| 6 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 7 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 8 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 9 | import net.minecraft.world.effect.MobEffectInstance;
|
| 10 | import net.minecraft.world.entity.Entity;
|
| 11 | import net.minecraft.world.entity.EntityEvent;
|
| 12 | import net.minecraft.world.entity.EntityType;
|
| 13 | import net.minecraft.world.entity.EntityTypes;
|
| 14 | import net.minecraft.world.entity.LivingEntity;
|
| 15 | import net.minecraft.world.item.ItemStack;
|
| 16 | import net.minecraft.world.item.Items;
|
| 17 | import net.minecraft.world.item.alchemy.PotionContents;
|
| 18 | import net.minecraft.world.level.Level;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 |
|
| 21 | public class Arrow extends AbstractArrow {
|
| 22 | private static final int EXPOSED_POTION_DECAY_TIME = 600;
|
| 23 | private static final int NO_EFFECT_COLOR = -1;
|
| 24 | private static final EntityDataAccessor<Integer> ID_EFFECT_COLOR = SynchedEntityData.defineId(Arrow.class, EntityDataSerializers.INT);
|
| 25 | private static final byte EVENT_POTION_PUFF = 0;
|
| 26 |
|
| 27 | public Arrow(final EntityType<? extends Arrow> type, final Level level) {
|
| 28 | super(type, level);
|
| 29 | }
|
| 30 |
|
| 31 | public Arrow(final Level level, final double x, final double y, final double z, final ItemStack pickupItemStack, final @Nullable ItemStack firedFromWeapon) {
|
| 32 | super(EntityTypes.ARROW, x, y, z, level, pickupItemStack, firedFromWeapon);
|
| 33 | this.updateColor();
|
| 34 | }
|
| 35 |
|
| 36 | public Arrow(final Level level, final LivingEntity owner, final ItemStack pickupItemStack, final @Nullable ItemStack firedFromWeapon) {
|
| 37 | super(EntityTypes.ARROW, owner, level, pickupItemStack, firedFromWeapon);
|
| 38 | this.updateColor();
|
| 39 | }
|
| 40 |
|
| 41 | private PotionContents getPotionContents() {
|
| 42 | return this.getPickupItemStackOrigin().getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY);
|
| 43 | }
|
| 44 |
|
| 45 | private float getPotionDurationScale() {
|
| 46 | return this.getPickupItemStackOrigin().getOrDefault(DataComponents.POTION_DURATION_SCALE, 1.0F);
|
| 47 | }
|
| 48 |
|
| 49 | private void setPotionContents(final PotionContents potionContents) {
|
| 50 | this.getPickupItemStackOrigin().set(DataComponents.POTION_CONTENTS, potionContents);
|
| 51 | this.updateColor();
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | protected void setPickupItemStack(final ItemStack itemStack) {
|
| 56 | super.setPickupItemStack(itemStack);
|
| 57 | this.updateColor();
|
| 58 | }
|
| 59 |
|
| 60 | private void updateColor() {
|
| 61 | PotionContents potionContents = this.getPotionContents();
|
| 62 | this.entityData.set(ID_EFFECT_COLOR, potionContents.equals(PotionContents.EMPTY) ? -1 : potionContents.getColor());
|
| 63 | }
|
| 64 |
|
| 65 | public void addEffect(final MobEffectInstance effect) {
|
| 66 | this.setPotionContents(this.getPotionContents().withEffectAdded(effect));
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 71 | super.defineSynchedData(entityData);
|
| 72 | entityData.define(ID_EFFECT_COLOR, -1);
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public void tick() {
|
| 77 | super.tick();
|
| 78 | if (this.level().isClientSide()) {
|
| 79 | if (this.isInGround()) {
|
| 80 | if (this.inGroundTime % 5 == 0) {
|
| 81 | this.makeParticle(1);
|
| 82 | }
|
| 83 | } else {
|
| 84 | this.makeParticle(2);
|
| 85 | }
|
| 86 | } else if (this.isInGround() && this.inGroundTime != 0 && !this.getPotionContents().equals(PotionContents.EMPTY) && this.inGroundTime >= 600) {
|
| 87 | this.level().broadcastEntityEvent(this, (byte)0);
|
| 88 | this.setPickupItemStack(new ItemStack(Items.ARROW));
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | private void makeParticle(final int amount) {
|
| 93 | int colorValue = this.getColor();
|
| 94 | if (colorValue != -1 && amount > 0) {
|
| 95 | for (int i = 0; i < amount; i++) {
|
| 96 | this.level()
|
| 97 | .addParticle(
|
| 98 | ColorParticleOption.create(ParticleTypes.ENTITY_EFFECT, colorValue), this.getRandomX(0.5), this.getRandomY(), this.getRandomZ(0.5), 0.0, 0.0, 0.0
|
| 99 | );
|
| 100 | }
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | public int getColor() {
|
| 105 | return this.entityData.get(ID_EFFECT_COLOR);
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | protected void doPostHurtEffects(final LivingEntity mob) {
|
| 110 | super.doPostHurtEffects(mob);
|
| 111 | Entity effectSource = this.getEffectSource();
|
| 112 | PotionContents potionContents = this.getPotionContents();
|
| 113 | float durationScale = this.getPotionDurationScale();
|
| 114 | potionContents.forEachEffect(effect -> mob.addEffect(effect, effectSource), durationScale);
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | protected ItemStack getDefaultPickupItem() {
|
| 119 | return new ItemStack(Items.ARROW);
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | public void handleEntityEvent(final @EntityEvent.Value byte id) {
|
| 124 | if (id == 0) {
|
| 125 | int colorValue = this.getColor();
|
| 126 | if (colorValue != -1) {
|
| 127 | float red = (colorValue >> 16 & 0xFF) / 255.0F;
|
| 128 | float green = (colorValue >> 8 & 0xFF) / 255.0F;
|
| 129 | float blue = (colorValue >> 0 & 0xFF) / 255.0F;
|
| 130 |
|
| 131 | for (int i = 0; i < 20; i++) {
|
| 132 | this.level()
|
| 133 | .addParticle(
|
| 134 | ColorParticleOption.create(ParticleTypes.ENTITY_EFFECT, red, green, blue), this.getRandomX(0.5), this.getRandomY(), this.getRandomZ(0.5), 0.0, 0.0, 0.0
|
| 135 | );
|
| 136 | }
|
| 137 | }
|
| 138 | } else {
|
| 139 | super.handleEntityEvent(id);
|
| 140 | }
|
| 141 | }
|
| 142 | }
|
| 143 |
|