jayhillx

SleepingPlayer

May 5th, 2026
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.98 KB | None | 0 0
  1. package com.mystanica.dreamlands.common.entity.misc;
  2.  
  3. import com.mojang.authlib.GameProfile;
  4. import net.minecraft.core.BlockPos;
  5. import net.minecraft.core.NonNullList;
  6. import net.minecraft.nbt.CompoundTag;
  7. import net.minecraft.nbt.ListTag;
  8. import net.minecraft.nbt.NbtOps;
  9. import net.minecraft.nbt.NbtUtils;
  10. import net.minecraft.network.syncher.EntityDataAccessor;
  11. import net.minecraft.network.syncher.EntityDataSerializers;
  12. import net.minecraft.network.syncher.SynchedEntityData;
  13. import net.minecraft.server.level.ServerPlayer;
  14. import net.minecraft.sounds.SoundEvent;
  15. import net.minecraft.sounds.SoundEvents;
  16. import net.minecraft.tags.DamageTypeTags;
  17. import net.minecraft.util.Mth;
  18. import net.minecraft.world.damagesource.DamageSource;
  19. import net.minecraft.world.effect.MobEffects;
  20. import net.minecraft.world.entity.*;
  21. import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
  22. import net.minecraft.world.entity.ai.attributes.Attributes;
  23. import net.minecraft.world.entity.item.ItemEntity;
  24. import net.minecraft.world.entity.player.Abilities;
  25. import net.minecraft.world.entity.player.Inventory;
  26. import net.minecraft.world.entity.player.Player;
  27. import net.minecraft.world.food.FoodData;
  28. import net.minecraft.world.inventory.AbstractContainerMenu;
  29. import net.minecraft.world.inventory.InventoryMenu;
  30. import net.minecraft.world.inventory.PlayerEnderChestContainer;
  31. import net.minecraft.world.item.ItemStack;
  32. import net.minecraft.world.item.component.ResolvableProfile;
  33. import net.minecraft.world.level.Level;
  34.  
  35. import java.util.Collections;
  36. import java.util.Optional;
  37.  
  38. public class SleepingPlayer extends LivingEntity {
  39.     private static final EntityDataAccessor<CompoundTag> DATA_PROFILE = SynchedEntityData.defineId(SleepingPlayer.class, EntityDataSerializers.COMPOUND_TAG);
  40.  
  41.     private GameProfile playerProfile;
  42.  
  43.     private Optional<BlockPos> sleepPos;
  44.  
  45.     private FoodData foodData = new FoodData();
  46.  
  47.     private final Abilities abilities = new Abilities();
  48.  
  49.     private final NonNullList<ItemStack> items = NonNullList.withSize(36, ItemStack.EMPTY);
  50.     private final NonNullList<ItemStack> armor = NonNullList.withSize(4, ItemStack.EMPTY);
  51.     private final NonNullList<ItemStack> offhand = NonNullList.withSize(1, ItemStack.EMPTY);
  52.     private int selectedSlot = 0;
  53.  
  54.     public SleepingPlayer(EntityType<? extends LivingEntity> type, Level level) {
  55.         super(type, level);
  56.     }
  57.  
  58.     @Override
  59.     protected void defineSynchedData(SynchedEntityData.Builder builder) {
  60.         super.defineSynchedData(builder);
  61.     }
  62.  
  63.     public static AttributeSupplier.Builder createAttributes() {
  64.         return Mob.createMobAttributes().add(Attributes.MAX_HEALTH, 20.0D).add(Attributes.MOVEMENT_SPEED, 0.0D);
  65.     }
  66.  
  67.     @Override
  68.     public void addAdditionalSaveData(CompoundTag tag) {
  69.         super.addAdditionalSaveData(tag);
  70.         if (this.playerProfile != null) {
  71.             CompoundTag profileTag = (CompoundTag) ResolvableProfile.CODEC
  72.                     .encodeStart(NbtOps.INSTANCE, new ResolvableProfile(this.playerProfile))
  73.                     .result()
  74.                     .orElse(new CompoundTag());
  75.  
  76.             tag.put("Profile", profileTag);
  77.         }
  78.  
  79.         if (this.sleepPos.isPresent()) {
  80.             tag.put("SleepPos", NbtUtils.writeBlockPos(this.sleepPos.get()));
  81.         }
  82.  
  83.         ListTag inventory = new ListTag();
  84.         for (int i = 0; i < this.items.size(); i++) {
  85.             if (!this.items.get(i).isEmpty()) {
  86.                 CompoundTag tag1 = new CompoundTag();
  87.                 tag1.putByte("Slot", (byte)i);
  88.                 inventory.add(this.items.get(i).save(this.registryAccess(), tag1));
  89.             }
  90.         }
  91.  
  92.         for (int j = 0; j < this.armor.size(); j++) {
  93.             if (!this.armor.get(j).isEmpty()) {
  94.                 CompoundTag tag1 = new CompoundTag();
  95.                 tag1.putByte("Slot", (byte)(j + 100));
  96.                 inventory.add(this.armor.get(j).save(this.registryAccess(), tag1));
  97.             }
  98.         }
  99.  
  100.         for (int k = 0; k < this.offhand.size(); k++) {
  101.             if (!this.offhand.get(k).isEmpty()) {
  102.                 CompoundTag tag1 = new CompoundTag();
  103.                 tag1.putByte("Slot", (byte)(k + 150));
  104.                 inventory.add(this.offhand.get(k).save(this.registryAccess(), tag1));
  105.             }
  106.         }
  107.         tag.put("Inventory", inventory);
  108.     }
  109.  
  110.     @Override
  111.     public void readAdditionalSaveData(CompoundTag tag) {
  112.         super.readAdditionalSaveData(tag);
  113.         if (tag.contains("Profile")) {
  114.             CompoundTag profileTag = tag.getCompound("Profile");
  115.  
  116.             this.playerProfile = ResolvableProfile.CODEC
  117.                     .parse(NbtOps.INSTANCE, profileTag)
  118.                     .result()
  119.                     .map(ResolvableProfile::gameProfile)
  120.                     .orElse(null);
  121.  
  122.             if (this.playerProfile != null) {
  123.                 this.entityData.set(DATA_PROFILE, profileTag);
  124.             }
  125.         }
  126.  
  127.         if (tag.contains("SleepPos")) {
  128.             this.sleepPos = NbtUtils.readBlockPos(tag.getCompound("SleepPos"), "SleepPos");
  129.         }
  130.  
  131.         this.foodData.readAdditionalSaveData(tag);
  132.         this.abilities.loadSaveData(tag);
  133.  
  134.         this.items.clear();
  135.         this.armor.clear();
  136.         this.offhand.clear();
  137.         ListTag inventory = tag.getList("Inventory", 10);
  138.         for (int i = 0; i < inventory.size(); i++) {
  139.             CompoundTag tag1 = inventory.getCompound(i);
  140.             int j = tag1.getByte("Slot") & 255;
  141.             ItemStack stack = ItemStack.parse(this.registryAccess(), tag1).orElse(ItemStack.EMPTY);
  142.             if (j >= 0 && j < this.items.size()) {
  143.                 this.items.set(j, stack);
  144.             } else if (j >= 100 && j < this.armor.size() + 100) {
  145.                 this.armor.set(j - 100, stack);
  146.             } else if (j >= 150 && j < this.offhand.size() + 150) {
  147.                 this.offhand.set(j - 150, stack);
  148.             }
  149.         }
  150.     }
  151.  
  152.     public GameProfile getGameProfile() {
  153.         if (this.playerProfile == null) {
  154.             CompoundTag tag = this.entityData.get(DATA_PROFILE);
  155.             if (!tag.isEmpty()) {
  156.                 this.playerProfile = ResolvableProfile.CODEC
  157.                         .parse(NbtOps.INSTANCE, tag)
  158.                         .result()
  159.                         .map(ResolvableProfile::gameProfile)
  160.                         .orElse(null);
  161.             }
  162.         }
  163.         return this.playerProfile;
  164.     }
  165.  
  166.     public void setGameProfile(GameProfile profile) {
  167.         this.playerProfile = profile;
  168.  
  169.         CompoundTag profileTag = (CompoundTag) ResolvableProfile.CODEC
  170.                 .encodeStart(NbtOps.INSTANCE, new ResolvableProfile(profile))
  171.                 .result()
  172.                 .orElse(new CompoundTag());
  173.         this.entityData.set(DATA_PROFILE, profileTag);
  174.     }
  175.  
  176.     public Optional<BlockPos> getSleepPos() {
  177.         return this.sleepPos;
  178.     }
  179.  
  180.     public NonNullList<ItemStack> getItems() {
  181.         return this.items;
  182.     }
  183.  
  184.     public NonNullList<ItemStack> getArmor() {
  185.         return this.armor;
  186.     }
  187.  
  188.     public NonNullList<ItemStack> getOffhand() {
  189.         return this.offhand;
  190.     }
  191.  
  192.     @Override
  193.     public void tick() {
  194.         super.tick();
  195.         if (!this.isSleeping() && this.sleepPos.isPresent()) {
  196.             this.startSleeping(this.sleepPos.get());
  197.         }
  198.     }
  199.  
  200.     @Override
  201.     public boolean hurt(DamageSource source, float amount) {
  202.         if (this.isInvulnerableTo(source)) {
  203.             return false;
  204.         } else if (this.level().isClientSide) {
  205.             return false;
  206.         } else if (this.isDeadOrDying()) {
  207.             return false;
  208.         } else if (source.is(DamageTypeTags.IS_FIRE) && this.hasEffect(MobEffects.FIRE_RESISTANCE)) {
  209.             return false;
  210.         } else {
  211.             this.noActionTime = 0;
  212.             boolean flag1 = true;
  213.             if ((float)this.invulnerableTime > 10.0F && !source.is(DamageTypeTags.BYPASSES_COOLDOWN)) {
  214.                 if (amount <= this.lastHurt) {
  215.                     return false;
  216.                 }
  217.  
  218.                 this.actuallyHurt(source, amount - this.lastHurt);
  219.                 this.lastHurt = amount;
  220.                 flag1 = false;
  221.             } else {
  222.                 this.lastHurt = amount;
  223.                 this.invulnerableTime = 20;
  224.                 this.actuallyHurt(source, amount);
  225.                 this.hurtDuration = 10;
  226.                 this.hurtTime = this.hurtDuration;
  227.             }
  228.  
  229.             if (source.is(DamageTypeTags.DAMAGES_HELMET) && !this.getItemBySlot(EquipmentSlot.HEAD).isEmpty()) {
  230.                 this.hurtHelmet(source, amount);
  231.                 amount *= 0.75F;
  232.             }
  233.  
  234.             if (source.getEntity() != null) {
  235.                 if (source.getEntity() instanceof LivingEntity entity) {
  236.                     if (!source.is(DamageTypeTags.NO_ANGER)) {
  237.                         this.setLastHurtByMob(entity);
  238.                     }
  239.                 }
  240.  
  241.                 if (source.getEntity() instanceof Player player) {
  242.                     this.lastHurtByPlayerTime = 100;
  243.                     this.lastHurtByPlayer = player;
  244.                 } else if (source.getEntity() instanceof TamableAnimal animal) {
  245.                     if (animal.isTame()) {
  246.                         this.lastHurtByPlayerTime = 100;
  247.                         if (animal.getOwner() instanceof Player playerOwner) {
  248.                             this.lastHurtByPlayer = playerOwner;
  249.                         } else {
  250.                             this.lastHurtByPlayer = null;
  251.                         }
  252.                     }
  253.                 }
  254.             }
  255.  
  256.             if (flag1) {
  257.                 this.level().broadcastDamageEvent(this, source);
  258.                 if (!source.is(DamageTypeTags.NO_IMPACT) && (amount > 0.0F)) {
  259.                     this.markHurt();
  260.                 }
  261.             }
  262.  
  263.             if (this.isDeadOrDying()) {
  264.                 if (flag1 && this.getDeathSound() != null) {
  265.                     this.playSound(this.getDeathSound(), this.getSoundVolume(), this.getVoicePitch());
  266.                 }
  267.  
  268.                 this.die(source);
  269.             } else if (flag1) {
  270.                 this.playHurtSound(source);
  271.             }
  272.             return amount > 0.0F;
  273.         }
  274.     }
  275.  
  276.     @Override
  277.     protected SoundEvent getHurtSound(DamageSource source) {
  278.         return source.type().effects().sound();
  279.     }
  280.  
  281.     @Override
  282.     protected SoundEvent getDeathSound() {
  283.         return SoundEvents.PLAYER_DEATH;
  284.     }
  285.  
  286.     @Override
  287.     public boolean isInvulnerable() {
  288.         return false;
  289.     }
  290.  
  291.     @Override
  292.     public boolean isPushable() {
  293.         return false;
  294.     }
  295.  
  296.     @Override
  297.     public HumanoidArm getMainArm() {
  298.         return HumanoidArm.RIGHT;
  299.     }
  300.  
  301.     @Override
  302.     public Iterable<ItemStack> getArmorSlots() {
  303.         return Collections.emptyList();
  304.     }
  305.  
  306.     @Override
  307.     public ItemStack getItemBySlot(EquipmentSlot slot) {
  308.         if (slot == EquipmentSlot.MAINHAND) {
  309.             return this.items.get(this.selectedSlot);
  310.         } else if (slot == EquipmentSlot.OFFHAND) {
  311.             return this.offhand.getFirst();
  312.         } else {
  313.             return slot.getType() == EquipmentSlot.Type.HUMANOID_ARMOR ? this.armor.get(slot.getIndex()) : ItemStack.EMPTY;
  314.         }
  315.     }
  316.  
  317.     @Override
  318.     public void setItemSlot(EquipmentSlot slot, ItemStack stack) {
  319.         if (slot == EquipmentSlot.MAINHAND) {
  320.             this.items.set(this.selectedSlot, stack.copy());
  321.         } else if (slot == EquipmentSlot.OFFHAND) {
  322.             this.offhand.set(0, stack.copy());
  323.         } else if (slot.getType() == EquipmentSlot.Type.HUMANOID_ARMOR) {
  324.             this.armor.set(slot.getIndex(), stack.copy());
  325.         }
  326.     }
  327.  
  328.     public ItemStack getSelected() {
  329.         return this.items.get(this.selectedSlot);
  330.     }
  331.  
  332.     public void setSelectedSlot(int slot) {
  333.         if (slot >= 0 && slot < 9) {
  334.             this.selectedSlot = slot;
  335.         }
  336.     }
  337.  
  338. }
Advertisement
Add Comment
Please, Sign In to add comment