Advertisement
mathiaseklund

Untitled

Apr 21st, 2023
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.54 KB | None | 0 0
  1. package me.mathiaseklund.itemization.entities.custom;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.Location;
  5. import org.bukkit.craftbukkit.v1_19_R3.CraftWorld;
  6. import org.bukkit.craftbukkit.v1_19_R3.attribute.CraftAttributeMap;
  7. import org.bukkit.craftbukkit.v1_19_R3.event.CraftEventFactory;
  8. import org.bukkit.craftbukkit.v1_19_R3.inventory.CraftItemStack;
  9. import org.bukkit.event.entity.EntityCombustByEntityEvent;
  10. import org.bukkit.event.entity.EntityShootBowEvent;
  11.  
  12. import me.mathiaseklund.itemization.entities.CustomEntity;
  13. import me.mathiaseklund.itemization.entities.goals.AttackPlayerGoal;
  14. import me.mathiaseklund.itemization.entities.goals.FollowPlayerGoal;
  15. import me.mathiaseklund.itemization.entities.goals.RangedAttackPlayerGoal;
  16. import me.mathiaseklund.itemization.utils.Util;
  17. import net.minecraft.core.BlockPos;
  18. import net.minecraft.core.Holder;
  19. import net.minecraft.nbt.CompoundTag;
  20. import net.minecraft.network.chat.Component;
  21. import net.minecraft.network.syncher.EntityDataAccessor;
  22. import net.minecraft.network.syncher.EntityDataSerializers;
  23. import net.minecraft.network.syncher.SynchedEntityData;
  24. import net.minecraft.server.MinecraftServer;
  25. import net.minecraft.server.level.ServerLevel;
  26. import net.minecraft.sounds.SoundEvent;
  27. import net.minecraft.sounds.SoundEvents;
  28. import net.minecraft.util.Mth;
  29. import net.minecraft.util.RandomSource;
  30. import net.minecraft.world.InteractionHand;
  31. import net.minecraft.world.damagesource.DamageSource;
  32. import net.minecraft.world.entity.Entity;
  33. import net.minecraft.world.entity.EntityType;
  34. import net.minecraft.world.entity.LivingEntity;
  35. import net.minecraft.world.entity.MobType;
  36. import net.minecraft.world.entity.PathfinderMob;
  37. import net.minecraft.world.entity.ai.attributes.Attribute;
  38. import net.minecraft.world.entity.ai.attributes.AttributeInstance;
  39. import net.minecraft.world.entity.ai.attributes.AttributeMap;
  40. import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
  41. import net.minecraft.world.entity.ai.attributes.Attributes;
  42. import net.minecraft.world.entity.animal.Cow;
  43. import net.minecraft.world.entity.monster.RangedAttackMob;
  44. import net.minecraft.world.entity.player.Player;
  45. import net.minecraft.world.entity.projectile.AbstractArrow;
  46. import net.minecraft.world.entity.projectile.ProjectileUtil;
  47. import net.minecraft.world.item.ItemStack;
  48. import net.minecraft.world.item.Items;
  49. import net.minecraft.world.item.enchantment.EnchantmentHelper;
  50. import net.minecraft.world.level.ItemLike;
  51. import net.minecraft.world.level.block.state.BlockState;
  52.  
  53. public class SuperEntity extends PathfinderMob implements RangedAttackMob {
  54.  
  55.     // private static final EntityDataAccessor<Boolean> DATA_BABY_ID =
  56.     // SynchedEntityData.defineId(Cow.class,
  57.     // EntityDataSerializers.BOOLEAN);
  58.  
  59.     // private static final EntityDataAccessor<Integer> DATA_SPECIAL_TYPE_ID =
  60.     // SynchedEntityData.defineId(Cow.class,
  61.     // EntityDataSerializers.INT);
  62.  
  63.     private int inWaterTime;
  64.  
  65.     public int conversionTime;
  66.  
  67.     public CustomEntity ent;
  68.  
  69.     @SuppressWarnings("unused")
  70.     private int lastTick = MinecraftServer.currentTick;
  71.  
  72.     public CraftAttributeMap craftAttributes;
  73.  
  74.     private AttributeMap attributes;
  75.  
  76.     @SuppressWarnings({ "unchecked", "rawtypes" })
  77.     public SuperEntity(Location loc, CustomEntity ent, EntityType<? extends PathfinderMob> entitytypes) {
  78.         super((EntityType) entitytypes, ((CraftWorld) loc.getWorld()).getHandle());
  79.         this.ent = ent;
  80.  
  81.         this.attributes = new AttributeMap(createAttributes().build());
  82.         this.craftAttributes = new CraftAttributeMap(this.attributes);
  83.  
  84.         if (ent.getDisplayName() != null) {
  85.             Component component = Component.literal(ent.getDisplayName());
  86.             this.setCustomName(component);
  87.             this.setCustomNameVisible(true);
  88.         }
  89.         this.setBaby(!ent.isAdult());
  90.  
  91.         this.goalSelector.getAvailableGoals().clear();
  92.         this.targetSelector.getAvailableGoals().clear();
  93.         if (ent.getHelm() != null) {
  94.             this.equipItemIfPossible(CraftItemStack.asNMSCopy(ent.getHelm()));
  95.         }
  96.         if (ent.getChest() != null) {
  97.             this.equipItemIfPossible(CraftItemStack.asNMSCopy(ent.getChest()));
  98.         }
  99.         if (ent.getLegs() != null) {
  100.             this.equipItemIfPossible(CraftItemStack.asNMSCopy(ent.getLegs()));
  101.         }
  102.         if (ent.getBoots() != null) {
  103.             this.equipItemIfPossible(CraftItemStack.asNMSCopy(ent.getBoots()));
  104.         }
  105.         if (ent.getMainHand() != null) {
  106.             this.setItemInHand(InteractionHand.MAIN_HAND, CraftItemStack.asNMSCopy(ent.getMainHand()));
  107.         }
  108.         if (ent.getOffHand() != null) {
  109.             this.setItemInHand(InteractionHand.OFF_HAND, CraftItemStack.asNMSCopy(ent.getOffHand()));
  110.         }
  111.  
  112.         addGoals();
  113.  
  114.     }
  115.  
  116.     protected void addGoals() {
  117.         for (String str : ent.getGoals()) {
  118.             switch (str) {
  119.             case "ranged_players":
  120.                 this.goalSelector.addGoal(1, new RangedAttackPlayerGoal(this, 1, 30, 7));
  121.                 break;
  122.             case "melee_players":
  123.                 this.goalSelector.addGoal(1, new AttackPlayerGoal(this, 1, false));
  124.                 break;
  125.             case "follow_players":
  126.                 this.goalSelector.addGoal(2, new FollowPlayerGoal(this, 1, 1, 15));
  127.                 break;
  128.             default:
  129.                 break;
  130.             }
  131.         }
  132.     }
  133.  
  134.     public AttributeSupplier.Builder createAttributes() {
  135.         // Util.debug("CREATING MOB ATTRIBUTES");
  136.         if (ent != null) {
  137.             // Util.debug("Getting attribute data.");
  138.             return AttributeSupplier.builder().add(Attributes.MOVEMENT_SPEED, ent.getMovementSpeed())
  139.                     .add(Attributes.ATTACK_DAMAGE, ent.getAttackDamage()).add(Attributes.ARMOR, ent.getArmor())
  140.                     .add(Attributes.SPAWN_REINFORCEMENTS_CHANCE, ent.getZombieSpawnReinforcements())
  141.                     .add(Attributes.ARMOR_TOUGHNESS, ent.getArmorToughness())
  142.                     .add(Attributes.ATTACK_KNOCKBACK, ent.getAttackKnockback())
  143.                     .add(Attributes.MAX_HEALTH, ent.getMaxHealth()).add(Attributes.FOLLOW_RANGE, 16)
  144.                     .add(Attributes.KNOCKBACK_RESISTANCE, ent.getKnockbackResistance());
  145.         } else {
  146.             // Util.debug("Creating null attributes");
  147.             return AttributeSupplier.builder().add(Attributes.MOVEMENT_SPEED).add(Attributes.ATTACK_DAMAGE)
  148.                     .add(Attributes.ARMOR).add(Attributes.SPAWN_REINFORCEMENTS_CHANCE).add(Attributes.ARMOR_TOUGHNESS)
  149.                     .add(Attributes.ATTACK_KNOCKBACK).add(Attributes.MAX_HEALTH).add(Attributes.FOLLOW_RANGE)
  150.                     .add(Attributes.KNOCKBACK_RESISTANCE);
  151.         }
  152.     }
  153.  
  154.     protected void defineSynchedData() {
  155.         super.defineSynchedData();
  156.         // getEntityData().define(DATA_BABY_ID, Boolean.valueOf(false));
  157.         // getEntityData().define(DATA_SPECIAL_TYPE_ID, Integer.valueOf(0));
  158.     }
  159.  
  160.     public boolean isBaby() {
  161.         return false;// ((Boolean) getEntityData().get(DATA_BABY_ID)).booleanValue();
  162.     }
  163.  
  164.     public int getExperienceReward() {
  165.         if (isBaby())
  166.             this.xpReward = (int) (this.xpReward * 2.5D);
  167.         return super.getExperienceReward();
  168.     }
  169.  
  170.     public void setBaby(boolean flag) {
  171.         // getEntityData().set(DATA_BABY_ID, Boolean.valueOf(flag));
  172.         if (this.level != null && !this.level.isClientSide) {
  173.             // AttributeInstance attributemodifiable =
  174.             // getAttribute(Attributes.MOVEMENT_SPEED);
  175.             // attributemodifiable.removeModifier(SPEED_MODIFIER_BABY);
  176.             // if (flag)
  177.             // attributemodifiable.addTransientModifier(SPEED_MODIFIER_BABY);
  178.         }
  179.     }
  180.  
  181.     public void onSyncedDataUpdated(EntityDataAccessor<?> datawatcherobject) {
  182.         // if (DATA_BABY_ID.equals(datawatcherobject))
  183.         refreshDimensions();
  184.         super.onSyncedDataUpdated(datawatcherobject);
  185.     }
  186.  
  187.     public void tick() {
  188.  
  189.         super.tick();
  190.         this.lastTick = MinecraftServer.currentTick;
  191.     }
  192.  
  193.     public void aiStep() {
  194.         super.aiStep();
  195.     }
  196.  
  197.     @SuppressWarnings("unused")
  198.     public boolean hurt(DamageSource damagesource, float f) {
  199.         if (!super.hurt(damagesource, f))
  200.             return false;
  201.         if (!(this.level instanceof ServerLevel))
  202.             return false;
  203.         ServerLevel worldserver = (ServerLevel) this.level;
  204.         LivingEntity entityliving = getTarget();
  205.         if (entityliving == null && damagesource.getEntity() instanceof LivingEntity)
  206.             entityliving = (LivingEntity) damagesource.getEntity();
  207.  
  208.         return true;
  209.     }
  210.  
  211.     public boolean doHurtTarget(Entity entity) {
  212.         float f = (float) this.getAttributeValue(Attributes.ATTACK_DAMAGE);
  213.         float f1 = (float) this.getAttributeValue(Attributes.ATTACK_KNOCKBACK);
  214.         if (entity instanceof LivingEntity) {
  215.             f += EnchantmentHelper.getDamageBonus(getMainHandItem(), ((LivingEntity) entity).getMobType());
  216.             f1 += EnchantmentHelper.getKnockbackBonus(this);
  217.         }
  218.         int i = EnchantmentHelper.getFireAspect(this);
  219.         if (i > 0) {
  220.             EntityCombustByEntityEvent combustEvent = new EntityCombustByEntityEvent(getBukkitEntity(),
  221.                     entity.getBukkitEntity(), i * 4);
  222.             Bukkit.getPluginManager().callEvent(combustEvent);
  223.             if (!combustEvent.isCancelled())
  224.                 entity.setSecondsOnFire(combustEvent.getDuration(), false);
  225.         }
  226.         boolean flag = entity.hurt(damageSources().mobAttack(this), f);
  227.         if (flag) {
  228.             if (f1 > 0.0F && entity instanceof LivingEntity) {
  229.                 ((LivingEntity) entity).knockback((f1 * 0.5F), Mth.sin(getYRot() * 0.017453292F),
  230.                         -Mth.cos(getYRot() * 0.017453292F));
  231.                 setDeltaMovement(getDeltaMovement().multiply(0.6D, 1.0D, 0.6D));
  232.             }
  233.             if (entity instanceof Player) {
  234.                 Player entityhuman = (Player) entity;
  235.                 maybeDisableShield(entityhuman, getMainHandItem(),
  236.                         entityhuman.isUsingItem() ? entityhuman.getUseItem() : ItemStack.EMPTY);
  237.             }
  238.             doEnchantDamageEffects(this, entity);
  239.             setLastHurtMob(entity);
  240.         }
  241.         return flag;
  242.     }
  243.  
  244.     private void maybeDisableShield(Player entityhuman, ItemStack itemstack, ItemStack itemstack1) {
  245.         if (!itemstack.isEmpty() && !itemstack1.isEmpty()
  246.                 && itemstack.getItem() instanceof net.minecraft.world.item.AxeItem && itemstack1.is(Items.SHIELD)) {
  247.             float f = 0.25F + EnchantmentHelper.getBlockEfficiency(this) * 0.05F;
  248.             if (this.random.nextFloat() < f) {
  249.                 entityhuman.getCooldowns().addCooldown(Items.SHIELD, 100);
  250.                 this.level.broadcastEntityEvent((Entity) entityhuman, (byte) 30);
  251.             }
  252.         }
  253.     }
  254.  
  255.     public double getAttributeValue(Attribute base) {
  256.         return getAttributes().getValue(base);
  257.     }
  258.  
  259.     public double getAttributeBaseValue(Attribute base) {
  260.         return getAttributes().getBaseValue(base);
  261.     }
  262.  
  263.     public AttributeMap getAttributes() {
  264.         if (attributes == null) {
  265.             attributes = new AttributeMap(createAttributes().build());
  266.         }
  267.         return attributes;
  268.     }
  269.  
  270.     public AttributeInstance getAttribute(Attribute attributebase) {
  271.         if (getAttributes().getInstance(attributebase) == null) {
  272.             Util.debug("AttributeInstance = NULL");
  273.         }
  274.         return this.getAttributes().getInstance(attributebase);
  275.     }
  276.  
  277.     public double getAttributeValue(Holder<Attribute> holder) {
  278.         return this.getAttributeValue((Attribute) holder.value());
  279.     }
  280.  
  281.     public double getAttributeBaseValue(Holder<Attribute> holder) {
  282.         return this.getAttributeBaseValue((Attribute) holder.value());
  283.     }
  284.  
  285.     protected SoundEvent getAmbientSound() {
  286.         return SoundEvents.COW_AMBIENT;
  287.     }
  288.  
  289.     protected SoundEvent getHurtSound(DamageSource damagesource) {
  290.         return SoundEvents.COW_HURT;
  291.     }
  292.  
  293.     public SoundEvent getDeathSound() {
  294.         return SoundEvents.COW_DEATH;
  295.     }
  296.  
  297.     protected SoundEvent getStepSound() {
  298.         return SoundEvents.COW_STEP;
  299.     }
  300.  
  301.     protected void playStepSound(BlockPos blockposition, BlockState iblockdata) {
  302.         playSound(getStepSound(), 0.15F, 1.0F);
  303.     }
  304.  
  305.     public MobType getMobType() {
  306.         return MobType.UNDEFINED;
  307.     }
  308.  
  309.     public void addAdditionalSaveData(CompoundTag nbttagcompound) {
  310.         super.addAdditionalSaveData(nbttagcompound);
  311.         nbttagcompound.putBoolean("IsBaby", isBaby());
  312.         nbttagcompound.putInt("InWaterTime", isInWater() ? this.inWaterTime : -1);
  313.     }
  314.  
  315.     public void readAdditionalSaveData(CompoundTag nbttagcompound) {
  316.         super.readAdditionalSaveData(nbttagcompound);
  317.         setBaby(nbttagcompound.getBoolean("IsBaby"));
  318.         this.inWaterTime = nbttagcompound.getInt("InWaterTime");
  319.  
  320.     }
  321.  
  322.     public boolean wasKilled(ServerLevel worldserver, LivingEntity entityliving) {
  323.         boolean flag = super.wasKilled(worldserver, entityliving);
  324.         return flag;
  325.     }
  326.  
  327.     public boolean canHoldItem(ItemStack itemstack) {
  328.         return (itemstack.is(Items.EGG) && isBaby() && isPassenger()) ? false : super.canHoldItem(itemstack);
  329.     }
  330.  
  331.     public boolean wantsToPickUp(ItemStack itemstack) {
  332.         return itemstack.is(Items.GLOW_INK_SAC) ? false : super.wantsToPickUp(itemstack);
  333.     }
  334.  
  335.     public static boolean getSpawnAsBabyOdds(RandomSource randomsource) {
  336.         return (randomsource.nextFloat() < 0.05F);
  337.     }
  338.  
  339.     public double getMyRidingOffset() {
  340.         return isBaby() ? 0.0D : -0.45D;
  341.     }
  342.  
  343.     protected void dropCustomDeathLoot(DamageSource damagesource, int i, boolean flag) {
  344.         super.dropCustomDeathLoot(damagesource, i, flag);
  345.         // Customize drops based on damagesource.
  346.     }
  347.  
  348.     public void performRangedAttack(LivingEntity entityliving, float f) {
  349.         ItemStack itemstack = new ItemStack((ItemLike) Items.ARROW);
  350.         AbstractArrow entityarrow = getArrow(itemstack, f);
  351.         double d0 = entityliving.getX() - getX();
  352.         double d1 = entityliving.getY(0.3333333333333333D) - entityarrow.getY();
  353.         double d2 = entityliving.getZ() - getZ();
  354.         double d3 = Math.sqrt(d0 * d0 + d2 * d2);
  355.         entityarrow.shoot(d0, d1 + d3 * 0.20000000298023224D, d2, 1.6F, (14 - this.level.getDifficulty().getId() * 4));
  356.         EntityShootBowEvent event = CraftEventFactory.callEntityShootBowEvent((LivingEntity) this, getMainHandItem(),
  357.                 null, (Entity) entityarrow, InteractionHand.MAIN_HAND, 0.8F, true);
  358.         if (event.isCancelled()) {
  359.             event.getProjectile().remove();
  360.             return;
  361.         }
  362.         if (event.getProjectile() == entityarrow.getBukkitEntity())
  363.             this.level.addFreshEntity((Entity) entityarrow);
  364.         playSound(SoundEvents.SKELETON_SHOOT, 1.0F, 1.0F / (getRandom().nextFloat() * 0.4F + 0.8F));
  365.     }
  366.  
  367.     protected AbstractArrow getArrow(ItemStack itemstack, float f) {
  368.         return ProjectileUtil.getMobArrow((LivingEntity) this, itemstack, f);
  369.     }
  370. }
  371.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement