Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mystanica.dreamlands.common.entity.misc;
- import com.mojang.authlib.GameProfile;
- import net.minecraft.core.BlockPos;
- import net.minecraft.core.NonNullList;
- import net.minecraft.nbt.CompoundTag;
- import net.minecraft.nbt.ListTag;
- import net.minecraft.nbt.NbtOps;
- import net.minecraft.nbt.NbtUtils;
- import net.minecraft.network.syncher.EntityDataAccessor;
- import net.minecraft.network.syncher.EntityDataSerializers;
- import net.minecraft.network.syncher.SynchedEntityData;
- import net.minecraft.server.level.ServerPlayer;
- import net.minecraft.sounds.SoundEvent;
- import net.minecraft.sounds.SoundEvents;
- import net.minecraft.tags.DamageTypeTags;
- import net.minecraft.util.Mth;
- import net.minecraft.world.damagesource.DamageSource;
- import net.minecraft.world.effect.MobEffects;
- import net.minecraft.world.entity.*;
- import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
- import net.minecraft.world.entity.ai.attributes.Attributes;
- import net.minecraft.world.entity.item.ItemEntity;
- import net.minecraft.world.entity.player.Abilities;
- import net.minecraft.world.entity.player.Inventory;
- import net.minecraft.world.entity.player.Player;
- import net.minecraft.world.food.FoodData;
- import net.minecraft.world.inventory.AbstractContainerMenu;
- import net.minecraft.world.inventory.InventoryMenu;
- import net.minecraft.world.inventory.PlayerEnderChestContainer;
- import net.minecraft.world.item.ItemStack;
- import net.minecraft.world.item.component.ResolvableProfile;
- import net.minecraft.world.level.Level;
- import java.util.Collections;
- import java.util.Optional;
- public class SleepingPlayer extends LivingEntity {
- private static final EntityDataAccessor<CompoundTag> DATA_PROFILE = SynchedEntityData.defineId(SleepingPlayer.class, EntityDataSerializers.COMPOUND_TAG);
- private GameProfile playerProfile;
- private Optional<BlockPos> sleepPos;
- private FoodData foodData = new FoodData();
- private final Abilities abilities = new Abilities();
- private final NonNullList<ItemStack> items = NonNullList.withSize(36, ItemStack.EMPTY);
- private final NonNullList<ItemStack> armor = NonNullList.withSize(4, ItemStack.EMPTY);
- private final NonNullList<ItemStack> offhand = NonNullList.withSize(1, ItemStack.EMPTY);
- private int selectedSlot = 0;
- public SleepingPlayer(EntityType<? extends LivingEntity> type, Level level) {
- super(type, level);
- }
- @Override
- protected void defineSynchedData(SynchedEntityData.Builder builder) {
- super.defineSynchedData(builder);
- }
- public static AttributeSupplier.Builder createAttributes() {
- return Mob.createMobAttributes().add(Attributes.MAX_HEALTH, 20.0D).add(Attributes.MOVEMENT_SPEED, 0.0D);
- }
- @Override
- public void addAdditionalSaveData(CompoundTag tag) {
- super.addAdditionalSaveData(tag);
- if (this.playerProfile != null) {
- CompoundTag profileTag = (CompoundTag) ResolvableProfile.CODEC
- .encodeStart(NbtOps.INSTANCE, new ResolvableProfile(this.playerProfile))
- .result()
- .orElse(new CompoundTag());
- tag.put("Profile", profileTag);
- }
- if (this.sleepPos.isPresent()) {
- tag.put("SleepPos", NbtUtils.writeBlockPos(this.sleepPos.get()));
- }
- ListTag inventory = new ListTag();
- for (int i = 0; i < this.items.size(); i++) {
- if (!this.items.get(i).isEmpty()) {
- CompoundTag tag1 = new CompoundTag();
- tag1.putByte("Slot", (byte)i);
- inventory.add(this.items.get(i).save(this.registryAccess(), tag1));
- }
- }
- for (int j = 0; j < this.armor.size(); j++) {
- if (!this.armor.get(j).isEmpty()) {
- CompoundTag tag1 = new CompoundTag();
- tag1.putByte("Slot", (byte)(j + 100));
- inventory.add(this.armor.get(j).save(this.registryAccess(), tag1));
- }
- }
- for (int k = 0; k < this.offhand.size(); k++) {
- if (!this.offhand.get(k).isEmpty()) {
- CompoundTag tag1 = new CompoundTag();
- tag1.putByte("Slot", (byte)(k + 150));
- inventory.add(this.offhand.get(k).save(this.registryAccess(), tag1));
- }
- }
- tag.put("Inventory", inventory);
- }
- @Override
- public void readAdditionalSaveData(CompoundTag tag) {
- super.readAdditionalSaveData(tag);
- if (tag.contains("Profile")) {
- CompoundTag profileTag = tag.getCompound("Profile");
- this.playerProfile = ResolvableProfile.CODEC
- .parse(NbtOps.INSTANCE, profileTag)
- .result()
- .map(ResolvableProfile::gameProfile)
- .orElse(null);
- if (this.playerProfile != null) {
- this.entityData.set(DATA_PROFILE, profileTag);
- }
- }
- if (tag.contains("SleepPos")) {
- this.sleepPos = NbtUtils.readBlockPos(tag.getCompound("SleepPos"), "SleepPos");
- }
- this.foodData.readAdditionalSaveData(tag);
- this.abilities.loadSaveData(tag);
- this.items.clear();
- this.armor.clear();
- this.offhand.clear();
- ListTag inventory = tag.getList("Inventory", 10);
- for (int i = 0; i < inventory.size(); i++) {
- CompoundTag tag1 = inventory.getCompound(i);
- int j = tag1.getByte("Slot") & 255;
- ItemStack stack = ItemStack.parse(this.registryAccess(), tag1).orElse(ItemStack.EMPTY);
- if (j >= 0 && j < this.items.size()) {
- this.items.set(j, stack);
- } else if (j >= 100 && j < this.armor.size() + 100) {
- this.armor.set(j - 100, stack);
- } else if (j >= 150 && j < this.offhand.size() + 150) {
- this.offhand.set(j - 150, stack);
- }
- }
- }
- public GameProfile getGameProfile() {
- if (this.playerProfile == null) {
- CompoundTag tag = this.entityData.get(DATA_PROFILE);
- if (!tag.isEmpty()) {
- this.playerProfile = ResolvableProfile.CODEC
- .parse(NbtOps.INSTANCE, tag)
- .result()
- .map(ResolvableProfile::gameProfile)
- .orElse(null);
- }
- }
- return this.playerProfile;
- }
- public void setGameProfile(GameProfile profile) {
- this.playerProfile = profile;
- CompoundTag profileTag = (CompoundTag) ResolvableProfile.CODEC
- .encodeStart(NbtOps.INSTANCE, new ResolvableProfile(profile))
- .result()
- .orElse(new CompoundTag());
- this.entityData.set(DATA_PROFILE, profileTag);
- }
- public Optional<BlockPos> getSleepPos() {
- return this.sleepPos;
- }
- public NonNullList<ItemStack> getItems() {
- return this.items;
- }
- public NonNullList<ItemStack> getArmor() {
- return this.armor;
- }
- public NonNullList<ItemStack> getOffhand() {
- return this.offhand;
- }
- @Override
- public void tick() {
- super.tick();
- if (!this.isSleeping() && this.sleepPos.isPresent()) {
- this.startSleeping(this.sleepPos.get());
- }
- }
- @Override
- public boolean hurt(DamageSource source, float amount) {
- if (this.isInvulnerableTo(source)) {
- return false;
- } else if (this.level().isClientSide) {
- return false;
- } else if (this.isDeadOrDying()) {
- return false;
- } else if (source.is(DamageTypeTags.IS_FIRE) && this.hasEffect(MobEffects.FIRE_RESISTANCE)) {
- return false;
- } else {
- this.noActionTime = 0;
- boolean flag1 = true;
- if ((float)this.invulnerableTime > 10.0F && !source.is(DamageTypeTags.BYPASSES_COOLDOWN)) {
- if (amount <= this.lastHurt) {
- return false;
- }
- this.actuallyHurt(source, amount - this.lastHurt);
- this.lastHurt = amount;
- flag1 = false;
- } else {
- this.lastHurt = amount;
- this.invulnerableTime = 20;
- this.actuallyHurt(source, amount);
- this.hurtDuration = 10;
- this.hurtTime = this.hurtDuration;
- }
- if (source.is(DamageTypeTags.DAMAGES_HELMET) && !this.getItemBySlot(EquipmentSlot.HEAD).isEmpty()) {
- this.hurtHelmet(source, amount);
- amount *= 0.75F;
- }
- if (source.getEntity() != null) {
- if (source.getEntity() instanceof LivingEntity entity) {
- if (!source.is(DamageTypeTags.NO_ANGER)) {
- this.setLastHurtByMob(entity);
- }
- }
- if (source.getEntity() instanceof Player player) {
- this.lastHurtByPlayerTime = 100;
- this.lastHurtByPlayer = player;
- } else if (source.getEntity() instanceof TamableAnimal animal) {
- if (animal.isTame()) {
- this.lastHurtByPlayerTime = 100;
- if (animal.getOwner() instanceof Player playerOwner) {
- this.lastHurtByPlayer = playerOwner;
- } else {
- this.lastHurtByPlayer = null;
- }
- }
- }
- }
- if (flag1) {
- this.level().broadcastDamageEvent(this, source);
- if (!source.is(DamageTypeTags.NO_IMPACT) && (amount > 0.0F)) {
- this.markHurt();
- }
- }
- if (this.isDeadOrDying()) {
- if (flag1 && this.getDeathSound() != null) {
- this.playSound(this.getDeathSound(), this.getSoundVolume(), this.getVoicePitch());
- }
- this.die(source);
- } else if (flag1) {
- this.playHurtSound(source);
- }
- return amount > 0.0F;
- }
- }
- @Override
- protected SoundEvent getHurtSound(DamageSource source) {
- return source.type().effects().sound();
- }
- @Override
- protected SoundEvent getDeathSound() {
- return SoundEvents.PLAYER_DEATH;
- }
- @Override
- public boolean isInvulnerable() {
- return false;
- }
- @Override
- public boolean isPushable() {
- return false;
- }
- @Override
- public HumanoidArm getMainArm() {
- return HumanoidArm.RIGHT;
- }
- @Override
- public Iterable<ItemStack> getArmorSlots() {
- return Collections.emptyList();
- }
- @Override
- public ItemStack getItemBySlot(EquipmentSlot slot) {
- if (slot == EquipmentSlot.MAINHAND) {
- return this.items.get(this.selectedSlot);
- } else if (slot == EquipmentSlot.OFFHAND) {
- return this.offhand.getFirst();
- } else {
- return slot.getType() == EquipmentSlot.Type.HUMANOID_ARMOR ? this.armor.get(slot.getIndex()) : ItemStack.EMPTY;
- }
- }
- @Override
- public void setItemSlot(EquipmentSlot slot, ItemStack stack) {
- if (slot == EquipmentSlot.MAINHAND) {
- this.items.set(this.selectedSlot, stack.copy());
- } else if (slot == EquipmentSlot.OFFHAND) {
- this.offhand.set(0, stack.copy());
- } else if (slot.getType() == EquipmentSlot.Type.HUMANOID_ARMOR) {
- this.armor.set(slot.getIndex(), stack.copy());
- }
- }
- public ItemStack getSelected() {
- return this.items.get(this.selectedSlot);
- }
- public void setSelectedSlot(int slot) {
- if (slot >= 0 && slot < 9) {
- this.selectedSlot = slot;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment