#include "stdafx.h" #include "com.mojang.nbt.h" #include "net.minecraft.world.h" #include "net.minecraft.world.level.h" #include "net.minecraft.world.entity.player.h" #include "net.minecraft.world.item.h" #include "net.minecraft.world.level.tile.h" #include "ShulkerBoxTileEntity.h" #include "ContainerOpenPacket.h" #include "SoundTypes.h" #include "Facing.h" #include "net.minecraft.world.entity.h" #include "net.minecraft.world.phys.h" const float ShulkerBoxTileEntity::LID_HEIGHT_FACTOR = 0.5f; const float ShulkerBoxTileEntity::LID_ROTATION_DEGREES = 270.0f; ShulkerBoxTileEntity::ShulkerBoxTileEntity(int color) : TileEntity() { items = new ItemInstanceArray(CONTAINER_SIZE); name = L""; this->color = color; openCount = 0; animationStatus = ANIMATION_CLOSED; progress = 0.0f; progressOld = 0.0f; cleared = false; destroyedByCreativePlayer = false; } ShulkerBoxTileEntity::~ShulkerBoxTileEntity() { delete[] items->data; delete items; } int ShulkerBoxTileEntity::getColor() { if (color < 0 && hasLevel()) { Tile *tile = getTile(); if (tile != nullptr && tile->id >= Tile::white_shulker_box_Id && tile->id <= Tile::black_shulker_box_Id) { color = tile->id - Tile::white_shulker_box_Id; } } return color < 0 ? 10 : color; } int ShulkerBoxTileEntity::getAnimationStatus() { return animationStatus; } float ShulkerBoxTileEntity::getProgress(float a) { return progressOld + (progress - progressOld) * a; } bool ShulkerBoxTileEntity::isEmpty() { for (unsigned int i = 0; i < items->length; i++) { if (items->data[i] != nullptr) return false; } return true; } bool ShulkerBoxTileEntity::isCleared() { return cleared; } void ShulkerBoxTileEntity::setDestroyedByCreativePlayer(bool value) { destroyedByCreativePlayer = value; } bool ShulkerBoxTileEntity::shouldDrop() { return !destroyedByCreativePlayer || !isEmpty() || hasCustomName(); } bool ShulkerBoxTileEntity::isShulkerBoxItem(shared_ptr item) { return item != nullptr && item->id >= Tile::white_shulker_box_Id && item->id <= Tile::black_shulker_box_Id; } int ShulkerBoxTileEntity::getContainerType() { return ContainerOpenPacket::SHULKER_BOX; } unsigned int ShulkerBoxTileEntity::getContainerSize() { return CONTAINER_SIZE; } shared_ptr ShulkerBoxTileEntity::getItem(unsigned int slot) { return items->data[slot]; } shared_ptr ShulkerBoxTileEntity::removeItem(unsigned int slot, int count) { if (items->data[slot] != nullptr) { if (items->data[slot]->count <= count) { shared_ptr item = items->data[slot]; items->data[slot] = nullptr; setChanged(); if (item->count <= 0) return nullptr; return item; } shared_ptr i = items->data[slot]->remove(count); if (items->data[slot]->count == 0) items->data[slot] = nullptr; setChanged(); if (i->count <= 0) return nullptr; return i; } return nullptr; } shared_ptr ShulkerBoxTileEntity::removeItemNoUpdate(int slot) { if (items->data[slot] != nullptr) { shared_ptr item = items->data[slot]; items->data[slot] = nullptr; return item; } return nullptr; } void ShulkerBoxTileEntity::setItem(unsigned int slot, shared_ptr item) { items->data[slot] = item; if (item != nullptr && item->count > getMaxStackSize()) item->count = getMaxStackSize(); setChanged(); } wstring ShulkerBoxTileEntity::getName() { return hasCustomName() ? name : app.GetString(IDS_TILE_SHULKER_BOX); } wstring ShulkerBoxTileEntity::getCustomName() { return hasCustomName() ? name : L""; } bool ShulkerBoxTileEntity::hasCustomName() { return !name.empty(); } void ShulkerBoxTileEntity::setCustomName(const wstring &name) { this->name = name; } int ShulkerBoxTileEntity::getMaxStackSize() const { return Container::LARGE_MAX_STACK_SIZE; } void ShulkerBoxTileEntity::setChanged() { TileEntity::setChanged(); } bool ShulkerBoxTileEntity::stillValid(shared_ptr player) { if (level == nullptr) return true; if (level->getTileEntity(x, y, z) != shared_from_this()) return false; if (player->distanceToSqr(x + 0.5, y + 0.5, z + 0.5) > 8 * 8) return false; return true; } void ShulkerBoxTileEntity::startOpen() { if (level == nullptr || getTile() == nullptr) return; if (openCount < 0) openCount = 0; openCount++; level->tileEvent(x, y, z, getTile()->id, EVENT_SET_OPEN_COUNT, openCount); if (openCount == 1) { level->playSound(x + 0.5, y + 0.5, z + 0.5, eSoundType_BLOCK_SHULKER_BOX_OPEN, 0.5f, level->random->nextFloat() * 0.1f + 0.9f); } } void ShulkerBoxTileEntity::stopOpen() { if (level == nullptr || getTile() == nullptr) return; openCount--; level->tileEvent(x, y, z, getTile()->id, EVENT_SET_OPEN_COUNT, openCount); if (openCount <= 0) { level->playSound(x + 0.5, y + 0.5, z + 0.5, eSoundType_BLOCK_SHULKER_BOX_CLOSE, 0.5f, level->random->nextFloat() * 0.1f + 0.9f); } } bool ShulkerBoxTileEntity::canPlaceItem(int slot, shared_ptr item) { return !isShulkerBoxItem(item); } void ShulkerBoxTileEntity::clearContent() { cleared = true; for (unsigned int i = 0; i < items->length; i++) { items->data[i] = nullptr; } } void ShulkerBoxTileEntity::saveContents(CompoundTag *base) { ListTag *listTag = new ListTag; for (unsigned int i = 0; i < items->length; i++) { if (items->data[i] != nullptr) { CompoundTag *tag = new CompoundTag(); tag->putByte(L"Slot", static_cast(i)); items->data[i]->save(tag); listTag->add(tag); } } base->put(L"Items", listTag); if (hasCustomName()) base->putString(L"CustomName", name); } void ShulkerBoxTileEntity::loadContents(CompoundTag *base) { for (unsigned int i = 0; i < items->length; i++) { items->data[i] = nullptr; } if (base->contains(L"Items")) { ListTag *inventoryList = (ListTag *) base->getList(L"Items"); for (int i = 0; i < inventoryList->size(); i++) { CompoundTag *tag = inventoryList->get(i); unsigned int slot = tag->getByte(L"Slot") & 0xff; if (slot < items->length) { (*items)[slot] = ItemInstance::fromTag(tag); } } } if (base->contains(L"CustomName")) name = base->getString(L"CustomName"); } void ShulkerBoxTileEntity::load(CompoundTag *base) { TileEntity::load(base); loadContents(base); } void ShulkerBoxTileEntity::save(CompoundTag *base) { TileEntity::save(base); saveContents(base); } void ShulkerBoxTileEntity::updateAnimation() { progressOld = progress; switch (animationStatus) { case ANIMATION_CLOSED: progress = 0.0f; break; case ANIMATION_OPENING: progress += 0.1f; if (progress >= 1.0f) { moveCollidedEntities(); animationStatus = ANIMATION_OPENED; progress = 1.0f; } break; case ANIMATION_CLOSING: progress -= 0.1f; if (progress <= 0.0f) { animationStatus = ANIMATION_CLOSED; progress = 0.0f; } break; case ANIMATION_OPENED: progress = 1.0f; break; } } void ShulkerBoxTileEntity::tick() { updateAnimation(); if (animationStatus == ANIMATION_OPENING || animationStatus == ANIMATION_CLOSING) moveCollidedEntities(); } void ShulkerBoxTileEntity::moveCollidedEntities() { if (level == nullptr) return; Tile *tile = Tile::tiles[level->getTile(x, y, z)]; if (dynamic_cast(tile) == nullptr) return; int facing = level->getData(x, y, z) % 6; int sx = Facing::STEP_X[facing]; int sy = Facing::STEP_Y[facing]; int sz = Facing::STEP_Z[facing]; float grow = 0.5f * getProgress(1.0f); double x0 = x, y0 = y, z0 = z, x1 = x + 1.0, y1 = y + 1.0, z1 = z + 1.0; if (sx > 0) { x0 = x + 1.0; x1 = x + 1.0 + grow; } else if (sx < 0) { x1 = x; x0 = x - grow; } if (sy > 0) { y0 = y + 1.0; y1 = y + 1.0 + grow; } else if (sy < 0) { y1 = y; y0 = y - grow; } if (sz > 0) { z0 = z + 1.0; z1 = z + 1.0 + grow; } else if (sz < 0) { z1 = z; z0 = z - grow; } if (x1 - x0 <= 0.0 || y1 - y0 <= 0.0 || z1 - z0 <= 0.0) return; AABB *box = AABB::newTemp(x0, y0, z0, x1, y1, z1); vector > *entities = level->getEntities(nullptr, box); if (entities == nullptr) return; for (size_t i = 0; i < entities->size(); ++i) { shared_ptr entity = entities->at(i); if (entity == nullptr || entity->bb == nullptr) continue; double dx = 0.0, dy = 0.0, dz = 0.0; if (sx > 0) dx = box->x1 - entity->bb->x0 + 0.01; else if (sx < 0) dx = -(entity->bb->x1 - box->x0 + 0.01); if (sy > 0) dy = box->y1 - entity->bb->y0 + 0.01; else if (sy < 0) dy = -(entity->bb->y1 - box->y0 + 0.01); if (sz > 0) dz = box->z1 - entity->bb->z0 + 0.01; else if (sz < 0) dz = -(entity->bb->z1 - box->z0 + 0.01); entity->move(dx, dy, dz); } } bool ShulkerBoxTileEntity::triggerEvent(int b0, int b1) { if (b0 == EVENT_SET_OPEN_COUNT) { openCount = b1; if (b1 == 0) { animationStatus = ANIMATION_CLOSING; } else if (b1 == 1) { animationStatus = ANIMATION_OPENING; } return true; } return TileEntity::triggerEvent(b0, b1); } shared_ptr ShulkerBoxTileEntity::clone() { shared_ptr result = std::make_shared(color); TileEntity::clone(result); for (unsigned int i = 0; i < items->length; i++) { if (items->data[i] != nullptr) { result->items->data[i] = ItemInstance::clone(items->data[i]); } } result->name = name; return result; }