CerbonXD

Untitled

Nov 21st, 2023
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.35 KB | Source Code | 0 0
  1. package com.cerbon.bosses_of_mass_destruction.entity.util;
  2.  
  3. import com.cerbon.bosses_of_mass_destruction.api.maelstrom.general.event.EventScheduler;
  4. import com.cerbon.bosses_of_mass_destruction.entity.damage.IDamageHandler;
  5. import com.google.errorprone.annotations.ForOverride;
  6. import net.minecraft.client.multiplayer.ClientLevel;
  7. import net.minecraft.nbt.CompoundTag;
  8. import net.minecraft.network.chat.Component;
  9. import net.minecraft.network.syncher.EntityDataAccessor;
  10. import net.minecraft.server.level.ServerBossEvent;
  11. import net.minecraft.server.level.ServerLevel;
  12. import net.minecraft.server.level.ServerPlayer;
  13. import net.minecraft.world.damagesource.DamageSource;
  14. import net.minecraft.world.effect.MobEffectInstance;
  15. import net.minecraft.world.entity.EntityType;
  16. import net.minecraft.world.entity.LivingEntity;
  17. import net.minecraft.world.entity.MoverType;
  18. import net.minecraft.world.entity.PathfinderMob;
  19. import net.minecraft.world.level.Level;
  20. import net.minecraft.world.phys.Vec3;
  21. import org.jetbrains.annotations.NotNull;
  22. import org.jetbrains.annotations.Nullable;
  23. import software.bernie.geckolib.animatable.GeoEntity;
  24. import software.bernie.geckolib.core.animatable.instance.AnimatableInstanceCache;
  25. import software.bernie.geckolib.util.GeckoLibUtil;
  26.  
  27. public abstract class BaseEntity extends PathfinderMob implements GeoEntity {
  28.     private AnimatableInstanceCache animationFactory = null;
  29.     public Vec3 idlePosition = Vec3.ZERO;
  30.     protected ServerBossEvent bossBar = null;
  31.     protected IDamageHandler damageHandler = null;
  32.     protected IEntityEventHandler entityEventHandler = null;
  33.     protected IEntityTick<ClientLevel> clientTick = null;
  34.     protected IEntityTick<ServerLevel> serverTick = null;
  35.     protected IDataAccessorHandler dataAccessorHandler = null;
  36.     protected IMobEffectFilter mobEffectHandler = null;
  37.     protected IMoveHandler moveHandler = null;
  38.     protected INbtHandler nbtHandler = null;
  39.     protected IEntityTick<ClientLevel> deathClientTick = null;
  40.     protected IEntityTick<ServerLevel> deathServerTick = null;
  41.     protected EventScheduler preTickEvents = new EventScheduler();
  42.     protected EventScheduler postTickEvents = new EventScheduler();
  43.  
  44.     public BaseEntity(EntityType<? extends PathfinderMob> entityType, Level level) {
  45.         super(entityType, level);
  46.     }
  47.  
  48.     @Override
  49.     public void tick() {
  50.         preTickEvents.updateEvents();
  51.         if (idlePosition == Vec3.ZERO)
  52.             idlePosition = position();
  53.  
  54.         Level sideLevel = level();
  55.         if (sideLevel.isClientSide() && sideLevel instanceof ClientLevel clientLevel){
  56.             clientTick();
  57.             if (clientTick != null)
  58.                 clientTick.tick(clientLevel);
  59.         }else if (sideLevel instanceof ServerLevel serverLevel){
  60.             serverTick(serverLevel);
  61.             if (serverTick != null)
  62.                 serverTick.tick(serverLevel);
  63.         }
  64.  
  65.         super.tick();
  66.         postTickEvents.updateEvents();
  67.     }
  68.  
  69.     @Override
  70.     protected void tickDeath() {
  71.         Level sideLevel = level();
  72.         if (sideLevel.isClientSide() && sideLevel instanceof ClientLevel clientLevel && deathClientTick != null)
  73.             deathClientTick.tick(clientLevel);
  74.         else if (sideLevel instanceof ServerLevel serverLevel && deathServerTick != null)
  75.             deathServerTick.tick(serverLevel);
  76.         else
  77.             super.tickDeath();
  78.     }
  79.  
  80.     @ForOverride
  81.     public void clientTick(){}
  82.  
  83.     @ForOverride
  84.     public void serverTick(ServerLevel serverLevel){}
  85.  
  86.     @Override
  87.     public void aiStep() {
  88.         super.aiStep();
  89.         getSensing().tick();
  90.     }
  91.  
  92.     @Override
  93.     protected void customServerAiStep() {
  94.         super.customServerAiStep();
  95.         if (bossBar != null)
  96.             bossBar.setProgress(this.getHealth() / this.getMaxHealth());
  97.     }
  98.  
  99.     @Override
  100.     public void load(@NotNull CompoundTag compound) {
  101.         super.load(compound);
  102.         if (hasCustomName() && bossBar != null)
  103.             bossBar.setName(this.getDisplayName());
  104.  
  105.         if (nbtHandler != null)
  106.             nbtHandler.fromTag(compound);
  107.     }
  108.  
  109.     @Override
  110.     public void setCustomName(@Nullable Component name) {
  111.         super.setCustomName(name);
  112.         if (bossBar != null)
  113.             bossBar.setName(this.getDisplayName());
  114.     }
  115.  
  116.     @Override
  117.     public void startSeenByPlayer(@NotNull ServerPlayer serverPlayer) {
  118.         super.startSeenByPlayer(serverPlayer);
  119.         if (bossBar != null)
  120.             bossBar.addPlayer(serverPlayer);
  121.     }
  122.  
  123.     @Override
  124.     public void stopSeenByPlayer(@NotNull ServerPlayer serverPlayer) {
  125.         super.stopSeenByPlayer(serverPlayer);
  126.         if (bossBar != null)
  127.             bossBar.removePlayer(serverPlayer);
  128.     }
  129.  
  130.     @Override
  131.     public void readAdditionalSaveData(@NotNull CompoundTag compound) {
  132.         super.readAdditionalSaveData(compound);
  133.     }
  134.  
  135.     @Override
  136.     public void handleEntityEvent(byte id) {
  137.         if (entityEventHandler != null)
  138.             entityEventHandler.handleEntityEvent(id);
  139.         super.handleEntityEvent(id);
  140.     }
  141.  
  142.     @Override
  143.     public void onSyncedDataUpdated(@NotNull EntityDataAccessor<?> data) {
  144.         super.onSyncedDataUpdated(data);
  145.         if (dataAccessorHandler != null)
  146.             dataAccessorHandler.onSyncedDataUpdated(data);
  147.     }
  148.  
  149.     @Override
  150.     public boolean canBeAffected(@NotNull MobEffectInstance effectInstance) {
  151.         IMobEffectFilter mobEffectHandler1 = mobEffectHandler;
  152.         if (mobEffectHandler1 != null)
  153.             return mobEffectHandler1.canBeAffected(effectInstance);
  154.         return super.canBeAffected(effectInstance);
  155.     }
  156.  
  157.     @Override
  158.     public boolean hurt(@NotNull DamageSource source, float amount) {
  159.         EntityStats stats = new EntityStats(this);
  160.         IDamageHandler handler = damageHandler;
  161.         if (!level().isClientSide() && handler != null)
  162.             handler.beforeDamage(stats, source, amount);
  163.  
  164.         boolean result = handler != null
  165.                 ? handler.shouldDamage(this, source, amount) && super.hurt(source, amount)
  166.                 : super.hurt(source, amount);
  167.  
  168.         if (!level().isClientSide() && handler != null)
  169.             handler.afterDamage(stats, source, amount, result);
  170.  
  171.         return result;
  172.     }
  173.  
  174.     @Override
  175.     public void setTarget(@Nullable LivingEntity target) {
  176.         if (target == null) idlePosition = position();
  177.         super.setTarget(target);
  178.     }
  179.  
  180.     @Override
  181.     public void move(@NotNull MoverType type, @NotNull Vec3 movement) {
  182.         boolean shouldDoDefault = moveHandler != null && moveHandler.canMove(type, movement);
  183.         if (moveHandler == null || shouldDoDefault)
  184.             super.move(type, movement);
  185.     }
  186.  
  187.     @Override
  188.     public @NotNull CompoundTag saveWithoutId(@NotNull CompoundTag compound) {
  189.         CompoundTag superCompound = super.saveWithoutId(compound);
  190.         return nbtHandler != null ? nbtHandler.toTag(superCompound) : superCompound;
  191.     }
  192.  
  193.     public Vec3 safeGetTargetPos(){
  194.         LivingEntity target = getTarget();
  195.         return target == null ? Vec3.ZERO : target.position();
  196.     }
  197.  
  198.     public AnimatableInstanceCache getAnimatableInstanceCache() {
  199.         if (animationFactory == null) {
  200.             animationFactory = GeckoLibUtil.createInstanceCache(this);
  201.         }
  202.         return animationFactory;
  203.     }
  204. }
  205.  
  206.  
Add Comment
Please, Sign In to add comment