Advertisement
Scouter456

Untitled

Jul 31st, 2023
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.14 KB | None | 0 0
  1. public class EntityWorldSpawnable extends LivingEntity implements IAnimatable {
  2.  
  3.     private static final EntityDataAccessor<Integer> VARIANT = SynchedEntityData.defineId(EntityWorldSpawnable.class, EntityDataSerializers.INT);
  4.     private static final EntityDataAccessor<Float> WIDTH_SCALE = SynchedEntityData.defineId(EntityWorldSpawnable.class, EntityDataSerializers.FLOAT);
  5.     private static final EntityDataAccessor<Float> HEIGHT_SCALE = SynchedEntityData.defineId(EntityWorldSpawnable.class, EntityDataSerializers.FLOAT);
  6.     public static final ResourceLocation MAMMOTH_LOOT = prefix("entities/iceberg/mammoth");
  7.     public static final ResourceLocation SMILODON_LOOT = prefix("entities/iceberg/smilodon");
  8.     public static final ResourceLocation ERYON_LOOT = prefix("entities/iceberg/eryon");
  9.     private AnimationFactory factory = GeckoLibUtil.createFactory(this);
  10.     public static final Logger LOGGER = LogManager.getLogger();
  11.     public EntityWorldSpawnable(EntityType<? extends LivingEntity> p_27557_, Level p_27558_) {
  12.         super(p_27557_, p_27558_);
  13.         this.refreshDimensions();
  14.     }
  15.  
  16.     public static AttributeSupplier.Builder bakeAttributes() {
  17.         return Mob.createMobAttributes()
  18.                 //HEALTH
  19.                 .add(Attributes.MAX_HEALTH, 10.0D)
  20.                 //SPEED
  21.                 .add(Attributes.MOVEMENT_SPEED, 0D);
  22.     }
  23.  
  24.  
  25.     @Override
  26.     public InteractionResult interact(Player pPlayer, InteractionHand pHand) {
  27.         ItemStack itemStack = pPlayer.getItemInHand(pHand);
  28.         if(itemStack.isEmpty())
  29.         {
  30.             itemDrop();
  31.         }
  32.         return super.interact(pPlayer, pHand);
  33.     }
  34.  
  35.     public void setRandomVariant()
  36.     {
  37.         RandomSource randomSource = this.level.random;
  38.         float nr = randomSource.nextFloat();
  39.         if(nr < 0.3F){
  40.             mammothVariant();
  41.             return;
  42.         } else{
  43.             smilodonVariant();
  44.             return;
  45.         }
  46.         //if(nr >= 0.3 && nr < 0.5)
  47.         //{
  48.         //
  49.         //}
  50.  
  51.     }
  52.  
  53.     public ResourceLocation getDeadLootTable(){
  54.         if(getVariant() == 1){
  55.             return SMILODON_LOOT;
  56.         }
  57.         if(getVariant() == 2)
  58.         {
  59.             return MAMMOTH_LOOT;
  60.         }
  61.         return ERYON_LOOT;
  62.     }
  63.  
  64.  
  65.     public void itemDrop(){
  66.         ItemStack drop = getSoundForDrop();
  67.         if (!drop.isEmpty() && !level.isClientSide) {
  68.             this.spawnAtLocation(drop, 1);
  69.         }
  70.     }
  71.  
  72.     protected ItemStack getSoundForDrop(){
  73.         ItemStack lootItem = getItemFromLootTable();
  74.         if(lootItem.getItem() == UPItems.FROZEN_FOSSIL.get() || lootItem.getItem() == UPItems.SMILODON_EMBRYO.get()){
  75.             this.playSound(SoundEvents.AMETHYST_BLOCK_CHIME, 1,1);
  76.         } else {
  77.             this.playSound(SoundEvents.BOOK_PAGE_TURN,1,1);
  78.         }
  79.         return lootItem;
  80.     }
  81.  
  82.     public ItemStack getItemFromLootTable() {
  83.         if(this.level.getServer() == null){
  84.             return ItemStack.EMPTY;
  85.         }
  86.         LootTable loottable = this.level.getServer().getLootTables().get(this.getDeadLootTable());
  87.         LootContext.Builder lootcontext$builder = this.createLootContext(false, DamageSource.GENERIC);
  88.         for (ItemStack itemstack : loottable.getRandomItems(lootcontext$builder.create(LootContextParamSets.ENTITY))) {
  89.             return itemstack;
  90.         }
  91.         return ItemStack.EMPTY;
  92.     }
  93.  
  94.     private void mammothVariant(){
  95.         setVariant(1);
  96.         setWidthScale(5.0F);
  97.         setHeightScale(5.0F);
  98.     }
  99.  
  100.     private void smilodonVariant(){
  101.         setVariant(2);
  102.         setWidthScale(2.0F);
  103.         setHeightScale(2.0F);
  104.     }
  105.  
  106.  
  107.     public void onSyncedDataUpdated(EntityDataAccessor<?> pKey) {
  108.         if (HEIGHT_SCALE.equals(pKey) || WIDTH_SCALE.equals(pKey)) {
  109.             this.refreshDimensions();
  110.         }
  111.         super.onSyncedDataUpdated(pKey);
  112.     }
  113.  
  114.     @Override
  115.     protected void defineSynchedData() {
  116.         super.defineSynchedData();
  117.         this.entityData.define(VARIANT, 0);
  118.         this.entityData.define(HEIGHT_SCALE, 0F);
  119.         this.entityData.define(WIDTH_SCALE, 0F);
  120.     }
  121.  
  122.     @Override
  123.     public void readAdditionalSaveData(CompoundTag compound) {
  124.         super.readAdditionalSaveData(compound);
  125.         this.setVariant(compound.getInt("variant"));
  126.         this.setHeightScale(Math.min(compound.getFloat("scaleHeight"), 0F));
  127.         this.setWidthScale(Math.min(compound.getFloat("scaleWdith"), 0F));
  128.  
  129.  
  130.     }
  131.  
  132.     @Override
  133.     public void addAdditionalSaveData(CompoundTag compound) {
  134.         super.addAdditionalSaveData(compound);
  135.         compound.putInt("variant", this.getVariant());
  136.         compound.putFloat("scaleHeight", this.getHeightScale());
  137.         compound.putFloat("scaleWdith", this.getWidthScale());
  138.     }
  139.  
  140.  
  141.     public int getVariant() {
  142.         return this.entityData.get(VARIANT);
  143.     }
  144.  
  145.     public void setVariant(int variant) {
  146.         this.entityData.set(VARIANT, variant);
  147.     }
  148.  
  149.     public float getHeightScale() {
  150.         return this.entityData.get(HEIGHT_SCALE);
  151.     }
  152.  
  153.     public void setHeightScale(float scale) {
  154.         this.entityData.set(HEIGHT_SCALE, scale);
  155.     }
  156.  
  157.     public float getWidthScale() {
  158.         return this.entityData.get(WIDTH_SCALE);
  159.     }
  160.  
  161.     public void setWidthScale(float scale) {
  162.         this.entityData.set(WIDTH_SCALE, scale);
  163.     }
  164.  
  165.     public EntityDimensions getDimensions(Pose pPose) {
  166.         return super.getDimensions(pPose).scale(getWidthScale(), getHeightScale());
  167.     }
  168.  
  169.     @Override
  170.     public Iterable<ItemStack> getArmorSlots() {
  171.         return ImmutableList.of();
  172.     }
  173.  
  174.     @Override
  175.     public ItemStack getItemBySlot(EquipmentSlot p_21127_) {
  176.         return ItemStack.EMPTY;
  177.     }
  178.  
  179.     @Override
  180.     public void setItemSlot(EquipmentSlot p_21036_, ItemStack p_21037_) {
  181.     }
  182.  
  183.     @Override
  184.     protected void onInsideBlock(BlockState p_20005_) {
  185.     }
  186.  
  187.  
  188.     @Override
  189.     public boolean canCollideWith(Entity entity) {
  190.         return false;
  191.     }
  192.  
  193.     @Override
  194.     public boolean canBeCollidedWith() {
  195.         return true;
  196.     }
  197.  
  198.     @Override
  199.     public boolean isPushable() {
  200.         return false;
  201.     }
  202.  
  203.     @Override
  204.     public boolean hurt(DamageSource source, float damage) {
  205.         if(source == DamageSource.IN_WALL || source == DamageSource.GENERIC)
  206.         {
  207.             return false;
  208.         }
  209.  
  210.         return super.hurt(source, damage);
  211.     }
  212.  
  213.     @Override
  214.     public HumanoidArm getMainArm() {
  215.         return HumanoidArm.RIGHT;
  216.     }
  217.  
  218.     @Override
  219.     public boolean attackable() {
  220.         return false;
  221.     }
  222.  
  223.     @Override
  224.     public Packet<?> getAddEntityPacket() {
  225.         return NetworkHooks.getEntitySpawningPacket(this);
  226.     }
  227.  
  228.     private <E extends IAnimatable> PlayState predicate(AnimationEvent<E> event) {
  229.         return PlayState.CONTINUE;
  230.     }
  231.  
  232.  
  233.     @Override
  234.     public void registerControllers(AnimationData data) {
  235.         data.addAnimationController(new AnimationController<>(this, "controller", 10, this::predicate));
  236.     }
  237.  
  238.     @Override
  239.     public AnimationFactory getFactory() {
  240.         return factory;
  241.     }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement