Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package entity;
- import item.moditems;
- import net.minecraft.nbt.CompoundTag;
- import net.minecraft.network.syncher.EntityDataAccessor;
- import net.minecraft.network.syncher.EntityDataSerializers;
- import net.minecraft.network.syncher.SynchedEntityData;
- import net.minecraft.server.level.ServerPlayer;
- import net.minecraft.sounds.SoundEvent;
- import net.minecraft.sounds.SoundEvents;
- import net.minecraft.world.damagesource.DamageSource;
- import net.minecraft.world.entity.Entity;
- import net.minecraft.world.entity.EntityType;
- import net.minecraft.world.entity.LivingEntity;
- import net.minecraft.world.entity.player.Player;
- import net.minecraft.world.entity.projectile.AbstractArrow;
- import net.minecraft.world.item.ItemStack;
- import net.minecraft.world.item.enchantment.EnchantmentHelper;
- import net.minecraft.world.level.Level;
- import net.minecraft.world.phys.EntityHitResult;
- import net.minecraft.world.phys.Vec3;
- import javax.annotation.Nullable;
- public class ThrownNetheriteSpear extends AbstractArrow {
- private static final EntityDataAccessor<Byte> ID_LOYALTY = SynchedEntityData.defineId(ThrownNetheriteSpear.class, EntityDataSerializers.BYTE);
- private static final EntityDataAccessor<Boolean> ID_FOIL = SynchedEntityData.defineId(ThrownNetheriteSpear.class, EntityDataSerializers.BOOLEAN);
- private ItemStack NetheriteSpearItem = new ItemStack(moditems.NETHERITE_SPEAR.get());
- private boolean dealtDamage;
- public int clientSideReturnNetheriteSpearTickCount;
- public ThrownNetheriteSpear(EntityType<? extends ThrownNetheriteSpear> type, Level level) {
- super(type, level);
- }
- public ThrownNetheriteSpear(EntityType<? extends AbstractArrow> type, Level world, LivingEntity entity, ItemStack stack) {
- super(type, entity, world);
- this.NetheriteSpearItem = stack.copy();
- this.entityData.set(ID_LOYALTY, (byte)EnchantmentHelper.getLoyalty(stack));
- this.entityData.set(ID_FOIL, stack.hasFoil());
- }
- public void tick() {
- if (this.inGroundTime > 4) {
- this.dealtDamage = true;
- }
- Entity entity = this.getOwner();
- int i = this.entityData.get(ID_LOYALTY);
- if (i > 0 && (this.dealtDamage || this.isNoPhysics()) && entity != null) {
- if (!this.isAcceptibleReturnOwner()) {
- if (!this.level.isClientSide && this.pickup == AbstractArrow.Pickup.ALLOWED) {
- this.spawnAtLocation(this.getPickupItem(), 0.1F);
- }
- this.discard();
- } else {
- this.setNoPhysics(true);
- Vec3 vec3 = entity.getEyePosition().subtract(this.position());
- this.setPosRaw(this.getX(), this.getY() + vec3.y * 0.015D * (double)i, this.getZ());
- if (this.level.isClientSide) {
- this.yOld = this.getY();
- }
- double d0 = 0.05D * (double)i;
- this.setDeltaMovement(this.getDeltaMovement().scale(0.95D).add(vec3.normalize().scale(d0)));
- if (this.clientSideReturnNetheriteSpearTickCount == 0) {
- this.playSound(SoundEvents.TRIDENT_RETURN, 10.0F, 1.0F);
- }
- ++this.clientSideReturnNetheriteSpearTickCount;
- }
- }
- super.tick();
- }
- private boolean isAcceptibleReturnOwner() {
- Entity entity = this.getOwner();
- if (entity != null && entity.isAlive()) {
- return !(entity instanceof ServerPlayer) || !entity.isSpectator();
- } else {
- return false;
- }
- }
- public boolean isFoil() {
- return this.entityData.get(ID_FOIL);
- }
- @Nullable
- protected EntityHitResult findHitEntity(Vec3 p_37575_, Vec3 p_37576_) {
- return this.dealtDamage ? null : super.findHitEntity(p_37575_, p_37576_);
- }
- protected void onHitEntity(EntityHitResult p_37573_) {
- Entity entity = p_37573_.getEntity();
- float f = 8.0F;
- if (entity instanceof LivingEntity livingentity) {
- f += EnchantmentHelper.getDamageBonus(this.NetheriteSpearItem, livingentity.getMobType());
- }
- Entity entity1 = this.getOwner();
- DamageSource damagesource = this.damageSources().trident(this, (Entity)(entity1 == null ? this : entity1));
- this.dealtDamage = true;
- SoundEvent soundevent = SoundEvents.TRIDENT_HIT;
- if (entity.hurt(damagesource, f)) {
- if (entity.getType() == EntityType.ENDERMAN) {
- return;
- }
- if (entity instanceof LivingEntity) {
- LivingEntity livingentity1 = (LivingEntity)entity;
- if (entity1 instanceof LivingEntity) {
- EnchantmentHelper.doPostHurtEffects(livingentity1, entity1);
- EnchantmentHelper.doPostDamageEffects((LivingEntity)entity1, livingentity1);
- }
- this.doPostHurtEffects(livingentity1);
- }
- }
- this.setDeltaMovement(this.getDeltaMovement().multiply(-0.01D, -0.1D, -0.01D));
- this.playSound(soundevent, 1.0F, 1.0F);
- }
- protected boolean tryPickup(Player player) {
- return super.tryPickup(player) || this.isNoPhysics() && this.ownedBy(player) && player.getInventory().add(this.getPickupItem());
- }
- protected void defineSynchedData() {
- super.defineSynchedData();
- this.entityData.define(ID_LOYALTY, (byte)0);
- this.entityData.define(ID_FOIL, false);
- }
- protected ItemStack getPickupItem() {
- return this.NetheriteSpearItem.copy();
- }
- protected SoundEvent getDefaultHitGroundSoundEvent() {
- return SoundEvents.TRIDENT_HIT_GROUND;
- }
- public void playerTouch(Player p_37580_) {
- if (this.ownedBy(p_37580_) || this.getOwner() == null) {
- super.playerTouch(p_37580_);
- }
- }
- public void readAdditionalSaveData(CompoundTag p_37578_) {
- super.readAdditionalSaveData(p_37578_);
- if (p_37578_.contains("Netherite Spear", 10)) {
- this.NetheriteSpearItem = ItemStack.of(p_37578_.getCompound("Netherite Spear"));
- }
- this.dealtDamage = p_37578_.getBoolean("DealtDamage");
- this.entityData.set(ID_LOYALTY, (byte)EnchantmentHelper.getLoyalty(this.NetheriteSpearItem));
- }
- public void addAdditionalSaveData(CompoundTag p_37582_) {
- super.addAdditionalSaveData(p_37582_);
- p_37582_.put("Netherite Spear", this.NetheriteSpearItem.save(new CompoundTag()));
- p_37582_.putBoolean("DealtDamage", this.dealtDamage);
- }
- protected float getWaterInertia() {
- return 0.99F;
- }
- public boolean shouldRender(double x, double y, double z) {
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment