Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.mathiaseklund.itemization.entities.custom;
- import org.bukkit.Bukkit;
- import org.bukkit.Location;
- import org.bukkit.craftbukkit.v1_19_R3.CraftWorld;
- import org.bukkit.craftbukkit.v1_19_R3.attribute.CraftAttributeMap;
- import org.bukkit.craftbukkit.v1_19_R3.event.CraftEventFactory;
- import org.bukkit.craftbukkit.v1_19_R3.inventory.CraftItemStack;
- import org.bukkit.event.entity.EntityCombustByEntityEvent;
- import org.bukkit.event.entity.EntityShootBowEvent;
- import me.mathiaseklund.itemization.entities.CustomEntity;
- import me.mathiaseklund.itemization.entities.goals.AttackPlayerGoal;
- import me.mathiaseklund.itemization.entities.goals.FollowPlayerGoal;
- import me.mathiaseklund.itemization.entities.goals.RangedAttackPlayerGoal;
- import me.mathiaseklund.itemization.utils.Util;
- import net.minecraft.core.BlockPos;
- import net.minecraft.core.Holder;
- import net.minecraft.nbt.CompoundTag;
- import net.minecraft.network.chat.Component;
- import net.minecraft.network.syncher.EntityDataAccessor;
- import net.minecraft.network.syncher.EntityDataSerializers;
- import net.minecraft.network.syncher.SynchedEntityData;
- import net.minecraft.server.MinecraftServer;
- import net.minecraft.server.level.ServerLevel;
- import net.minecraft.sounds.SoundEvent;
- import net.minecraft.sounds.SoundEvents;
- import net.minecraft.util.Mth;
- import net.minecraft.util.RandomSource;
- import net.minecraft.world.InteractionHand;
- 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.MobType;
- import net.minecraft.world.entity.PathfinderMob;
- import net.minecraft.world.entity.ai.attributes.Attribute;
- import net.minecraft.world.entity.ai.attributes.AttributeInstance;
- import net.minecraft.world.entity.ai.attributes.AttributeMap;
- import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
- import net.minecraft.world.entity.ai.attributes.Attributes;
- import net.minecraft.world.entity.animal.Cow;
- import net.minecraft.world.entity.monster.RangedAttackMob;
- import net.minecraft.world.entity.player.Player;
- import net.minecraft.world.entity.projectile.AbstractArrow;
- import net.minecraft.world.entity.projectile.ProjectileUtil;
- import net.minecraft.world.item.ItemStack;
- import net.minecraft.world.item.Items;
- import net.minecraft.world.item.enchantment.EnchantmentHelper;
- import net.minecraft.world.level.ItemLike;
- import net.minecraft.world.level.block.state.BlockState;
- public class SuperEntity extends PathfinderMob implements RangedAttackMob {
- // private static final EntityDataAccessor<Boolean> DATA_BABY_ID =
- // SynchedEntityData.defineId(Cow.class,
- // EntityDataSerializers.BOOLEAN);
- // private static final EntityDataAccessor<Integer> DATA_SPECIAL_TYPE_ID =
- // SynchedEntityData.defineId(Cow.class,
- // EntityDataSerializers.INT);
- private int inWaterTime;
- public int conversionTime;
- public CustomEntity ent;
- @SuppressWarnings("unused")
- private int lastTick = MinecraftServer.currentTick;
- public CraftAttributeMap craftAttributes;
- private AttributeMap attributes;
- @SuppressWarnings({ "unchecked", "rawtypes" })
- public SuperEntity(Location loc, CustomEntity ent, EntityType<? extends PathfinderMob> entitytypes) {
- super((EntityType) entitytypes, ((CraftWorld) loc.getWorld()).getHandle());
- this.ent = ent;
- this.attributes = new AttributeMap(createAttributes().build());
- this.craftAttributes = new CraftAttributeMap(this.attributes);
- if (ent.getDisplayName() != null) {
- Component component = Component.literal(ent.getDisplayName());
- this.setCustomName(component);
- this.setCustomNameVisible(true);
- }
- this.setBaby(!ent.isAdult());
- this.goalSelector.getAvailableGoals().clear();
- this.targetSelector.getAvailableGoals().clear();
- if (ent.getHelm() != null) {
- this.equipItemIfPossible(CraftItemStack.asNMSCopy(ent.getHelm()));
- }
- if (ent.getChest() != null) {
- this.equipItemIfPossible(CraftItemStack.asNMSCopy(ent.getChest()));
- }
- if (ent.getLegs() != null) {
- this.equipItemIfPossible(CraftItemStack.asNMSCopy(ent.getLegs()));
- }
- if (ent.getBoots() != null) {
- this.equipItemIfPossible(CraftItemStack.asNMSCopy(ent.getBoots()));
- }
- if (ent.getMainHand() != null) {
- this.setItemInHand(InteractionHand.MAIN_HAND, CraftItemStack.asNMSCopy(ent.getMainHand()));
- }
- if (ent.getOffHand() != null) {
- this.setItemInHand(InteractionHand.OFF_HAND, CraftItemStack.asNMSCopy(ent.getOffHand()));
- }
- addGoals();
- }
- protected void addGoals() {
- for (String str : ent.getGoals()) {
- switch (str) {
- case "ranged_players":
- this.goalSelector.addGoal(1, new RangedAttackPlayerGoal(this, 1, 30, 7));
- break;
- case "melee_players":
- this.goalSelector.addGoal(1, new AttackPlayerGoal(this, 1, false));
- break;
- case "follow_players":
- this.goalSelector.addGoal(2, new FollowPlayerGoal(this, 1, 1, 15));
- break;
- default:
- break;
- }
- }
- }
- public AttributeSupplier.Builder createAttributes() {
- // Util.debug("CREATING MOB ATTRIBUTES");
- if (ent != null) {
- // Util.debug("Getting attribute data.");
- return AttributeSupplier.builder().add(Attributes.MOVEMENT_SPEED, ent.getMovementSpeed())
- .add(Attributes.ATTACK_DAMAGE, ent.getAttackDamage()).add(Attributes.ARMOR, ent.getArmor())
- .add(Attributes.SPAWN_REINFORCEMENTS_CHANCE, ent.getZombieSpawnReinforcements())
- .add(Attributes.ARMOR_TOUGHNESS, ent.getArmorToughness())
- .add(Attributes.ATTACK_KNOCKBACK, ent.getAttackKnockback())
- .add(Attributes.MAX_HEALTH, ent.getMaxHealth()).add(Attributes.FOLLOW_RANGE, 16)
- .add(Attributes.KNOCKBACK_RESISTANCE, ent.getKnockbackResistance());
- } else {
- // Util.debug("Creating null attributes");
- return AttributeSupplier.builder().add(Attributes.MOVEMENT_SPEED).add(Attributes.ATTACK_DAMAGE)
- .add(Attributes.ARMOR).add(Attributes.SPAWN_REINFORCEMENTS_CHANCE).add(Attributes.ARMOR_TOUGHNESS)
- .add(Attributes.ATTACK_KNOCKBACK).add(Attributes.MAX_HEALTH).add(Attributes.FOLLOW_RANGE)
- .add(Attributes.KNOCKBACK_RESISTANCE);
- }
- }
- protected void defineSynchedData() {
- super.defineSynchedData();
- // getEntityData().define(DATA_BABY_ID, Boolean.valueOf(false));
- // getEntityData().define(DATA_SPECIAL_TYPE_ID, Integer.valueOf(0));
- }
- public boolean isBaby() {
- return false;// ((Boolean) getEntityData().get(DATA_BABY_ID)).booleanValue();
- }
- public int getExperienceReward() {
- if (isBaby())
- this.xpReward = (int) (this.xpReward * 2.5D);
- return super.getExperienceReward();
- }
- public void setBaby(boolean flag) {
- // getEntityData().set(DATA_BABY_ID, Boolean.valueOf(flag));
- if (this.level != null && !this.level.isClientSide) {
- // AttributeInstance attributemodifiable =
- // getAttribute(Attributes.MOVEMENT_SPEED);
- // attributemodifiable.removeModifier(SPEED_MODIFIER_BABY);
- // if (flag)
- // attributemodifiable.addTransientModifier(SPEED_MODIFIER_BABY);
- }
- }
- public void onSyncedDataUpdated(EntityDataAccessor<?> datawatcherobject) {
- // if (DATA_BABY_ID.equals(datawatcherobject))
- refreshDimensions();
- super.onSyncedDataUpdated(datawatcherobject);
- }
- public void tick() {
- super.tick();
- this.lastTick = MinecraftServer.currentTick;
- }
- public void aiStep() {
- super.aiStep();
- }
- @SuppressWarnings("unused")
- public boolean hurt(DamageSource damagesource, float f) {
- if (!super.hurt(damagesource, f))
- return false;
- if (!(this.level instanceof ServerLevel))
- return false;
- ServerLevel worldserver = (ServerLevel) this.level;
- LivingEntity entityliving = getTarget();
- if (entityliving == null && damagesource.getEntity() instanceof LivingEntity)
- entityliving = (LivingEntity) damagesource.getEntity();
- return true;
- }
- public boolean doHurtTarget(Entity entity) {
- float f = (float) this.getAttributeValue(Attributes.ATTACK_DAMAGE);
- float f1 = (float) this.getAttributeValue(Attributes.ATTACK_KNOCKBACK);
- if (entity instanceof LivingEntity) {
- f += EnchantmentHelper.getDamageBonus(getMainHandItem(), ((LivingEntity) entity).getMobType());
- f1 += EnchantmentHelper.getKnockbackBonus(this);
- }
- int i = EnchantmentHelper.getFireAspect(this);
- if (i > 0) {
- EntityCombustByEntityEvent combustEvent = new EntityCombustByEntityEvent(getBukkitEntity(),
- entity.getBukkitEntity(), i * 4);
- Bukkit.getPluginManager().callEvent(combustEvent);
- if (!combustEvent.isCancelled())
- entity.setSecondsOnFire(combustEvent.getDuration(), false);
- }
- boolean flag = entity.hurt(damageSources().mobAttack(this), f);
- if (flag) {
- if (f1 > 0.0F && entity instanceof LivingEntity) {
- ((LivingEntity) entity).knockback((f1 * 0.5F), Mth.sin(getYRot() * 0.017453292F),
- -Mth.cos(getYRot() * 0.017453292F));
- setDeltaMovement(getDeltaMovement().multiply(0.6D, 1.0D, 0.6D));
- }
- if (entity instanceof Player) {
- Player entityhuman = (Player) entity;
- maybeDisableShield(entityhuman, getMainHandItem(),
- entityhuman.isUsingItem() ? entityhuman.getUseItem() : ItemStack.EMPTY);
- }
- doEnchantDamageEffects(this, entity);
- setLastHurtMob(entity);
- }
- return flag;
- }
- private void maybeDisableShield(Player entityhuman, ItemStack itemstack, ItemStack itemstack1) {
- if (!itemstack.isEmpty() && !itemstack1.isEmpty()
- && itemstack.getItem() instanceof net.minecraft.world.item.AxeItem && itemstack1.is(Items.SHIELD)) {
- float f = 0.25F + EnchantmentHelper.getBlockEfficiency(this) * 0.05F;
- if (this.random.nextFloat() < f) {
- entityhuman.getCooldowns().addCooldown(Items.SHIELD, 100);
- this.level.broadcastEntityEvent((Entity) entityhuman, (byte) 30);
- }
- }
- }
- public double getAttributeValue(Attribute base) {
- return getAttributes().getValue(base);
- }
- public double getAttributeBaseValue(Attribute base) {
- return getAttributes().getBaseValue(base);
- }
- public AttributeMap getAttributes() {
- if (attributes == null) {
- attributes = new AttributeMap(createAttributes().build());
- }
- return attributes;
- }
- public AttributeInstance getAttribute(Attribute attributebase) {
- if (getAttributes().getInstance(attributebase) == null) {
- Util.debug("AttributeInstance = NULL");
- }
- return this.getAttributes().getInstance(attributebase);
- }
- public double getAttributeValue(Holder<Attribute> holder) {
- return this.getAttributeValue((Attribute) holder.value());
- }
- public double getAttributeBaseValue(Holder<Attribute> holder) {
- return this.getAttributeBaseValue((Attribute) holder.value());
- }
- protected SoundEvent getAmbientSound() {
- return SoundEvents.COW_AMBIENT;
- }
- protected SoundEvent getHurtSound(DamageSource damagesource) {
- return SoundEvents.COW_HURT;
- }
- public SoundEvent getDeathSound() {
- return SoundEvents.COW_DEATH;
- }
- protected SoundEvent getStepSound() {
- return SoundEvents.COW_STEP;
- }
- protected void playStepSound(BlockPos blockposition, BlockState iblockdata) {
- playSound(getStepSound(), 0.15F, 1.0F);
- }
- public MobType getMobType() {
- return MobType.UNDEFINED;
- }
- public void addAdditionalSaveData(CompoundTag nbttagcompound) {
- super.addAdditionalSaveData(nbttagcompound);
- nbttagcompound.putBoolean("IsBaby", isBaby());
- nbttagcompound.putInt("InWaterTime", isInWater() ? this.inWaterTime : -1);
- }
- public void readAdditionalSaveData(CompoundTag nbttagcompound) {
- super.readAdditionalSaveData(nbttagcompound);
- setBaby(nbttagcompound.getBoolean("IsBaby"));
- this.inWaterTime = nbttagcompound.getInt("InWaterTime");
- }
- public boolean wasKilled(ServerLevel worldserver, LivingEntity entityliving) {
- boolean flag = super.wasKilled(worldserver, entityliving);
- return flag;
- }
- public boolean canHoldItem(ItemStack itemstack) {
- return (itemstack.is(Items.EGG) && isBaby() && isPassenger()) ? false : super.canHoldItem(itemstack);
- }
- public boolean wantsToPickUp(ItemStack itemstack) {
- return itemstack.is(Items.GLOW_INK_SAC) ? false : super.wantsToPickUp(itemstack);
- }
- public static boolean getSpawnAsBabyOdds(RandomSource randomsource) {
- return (randomsource.nextFloat() < 0.05F);
- }
- public double getMyRidingOffset() {
- return isBaby() ? 0.0D : -0.45D;
- }
- protected void dropCustomDeathLoot(DamageSource damagesource, int i, boolean flag) {
- super.dropCustomDeathLoot(damagesource, i, flag);
- // Customize drops based on damagesource.
- }
- public void performRangedAttack(LivingEntity entityliving, float f) {
- ItemStack itemstack = new ItemStack((ItemLike) Items.ARROW);
- AbstractArrow entityarrow = getArrow(itemstack, f);
- double d0 = entityliving.getX() - getX();
- double d1 = entityliving.getY(0.3333333333333333D) - entityarrow.getY();
- double d2 = entityliving.getZ() - getZ();
- double d3 = Math.sqrt(d0 * d0 + d2 * d2);
- entityarrow.shoot(d0, d1 + d3 * 0.20000000298023224D, d2, 1.6F, (14 - this.level.getDifficulty().getId() * 4));
- EntityShootBowEvent event = CraftEventFactory.callEntityShootBowEvent((LivingEntity) this, getMainHandItem(),
- null, (Entity) entityarrow, InteractionHand.MAIN_HAND, 0.8F, true);
- if (event.isCancelled()) {
- event.getProjectile().remove();
- return;
- }
- if (event.getProjectile() == entityarrow.getBukkitEntity())
- this.level.addFreshEntity((Entity) entityarrow);
- playSound(SoundEvents.SKELETON_SHOOT, 1.0F, 1.0F / (getRandom().nextFloat() * 0.4F + 0.8F));
- }
- protected AbstractArrow getArrow(ItemStack itemstack, float f) {
- return ProjectileUtil.getMobArrow((LivingEntity) this, itemstack, f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement