Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
odorajbotoj committed Oct 14, 2024
2 parents 2923035 + 7c7b868 commit e158c74
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 66 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.1.0] - 2024-10-14

### Added

+ Added tryPutIntoContainer
+ Added tryGetFromContainerWithName
+ Added getFirstEmptySlot
+ Added trySwapSlotWithContainer

### Fixed

+ Fixed searchInInvWithName

## [2.0.0] - 2024-10-13

### Added
Expand All @@ -23,5 +36,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
+ Move CoralFans SimulatedPlayer System from [CoralFans](https://github.com/CoralFans-Dev/CoralFans) to here
+ Added Script Arg

[2.1.0]: https://github.com/CoralFans-Dev/CFSP/compare/v2.0.0...v2.1.0
[2.0.0]: https://github.com/CoralFans-Dev/CFSP/compare/v1.0.0...v2.0.0
[1.0.0]: https://github.com/CoralFans-Dev/CFSP/releases/tag/v1.0.0
149 changes: 122 additions & 27 deletions src/cfsp/simplayer/CFSP.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
#include "mc/world/attribute/AttributeInstance.h"
#include "mc/world/item/registry/ItemStack.h"
#include "mc/world/level/BlockPos.h"
#include "mc/world/level/BlockSource.h"
#include "mc/world/level/block/Block.h"
#include "mc/world/level/block/actor/BlockActor.h"
#include "mc/world/level/block/actor/ChestBlockActor.h"
#include "mc/world/level/block/utils/BlockActorType.h"
#include "mc/world/phys/HitResultType.h"
#include <boost/serialization/access.hpp>
#include <boost/serialization/shared_ptr.hpp>
Expand All @@ -42,9 +47,9 @@ namespace coral_fans::cfsp {
class SimPlayerManager {
public:
enum SimPlayerStatus : int {
Offline,
Alive,
Dead,
Offline = 0,
Alive = 1,
Dead = 2,
};
struct SimPlayerInfo {
std::string name;
Expand Down Expand Up @@ -142,42 +147,38 @@ class SimPlayerManager {
}
CFSP_API void swap(Player* player) {
// get data
auto& spInv = simPlayer->getInventory();
const auto spOffhand = simPlayer->getOffhandSlot();
auto& spArmor = ActorEquipment::getArmorContainer(simPlayer->getEntityContext());
auto& pInv = player->getInventory();
const auto pOffhand = player->getOffhandSlot();
auto& pArmor = ActorEquipment::getArmorContainer(player->getEntityContext());
auto spEnder = simPlayer->getEnderChestContainer();
auto pEnder = player->getEnderChestContainer();
auto& spInv = simPlayer->getInventory();
auto& spArmor = ActorEquipment::getArmorContainer(simPlayer->getEntityContext());
auto& pInv = player->getInventory();
const auto pOffhand = player->getOffhandSlot();
auto& pArmor = ActorEquipment::getArmorContainer(player->getEntityContext());
auto spEnder = simPlayer->getEnderChestContainer();
auto pEnder = player->getEnderChestContainer();
// swap offhand
player->setOffhandSlot(spOffhand);
player->setOffhandSlot(simPlayer->getOffhandSlot());
simPlayer->setOffhandSlot(pOffhand);
// swap inv
int spInvSize = spInv.getContainerSize();
if (spInvSize == pInv.getContainerSize())
for (int i = 0; i < spInvSize; ++i) {
const auto spItem = spInv.getItem(i);
const auto pItem = pInv.getItem(i);
spInv.setItem(i, pItem);
spInv.setItem(i, pInv.getItem(i));
pInv.setItem(i, spItem);
}
// swap armor
int spArmorSize = spArmor.getContainerSize();
if (spArmorSize == pArmor.getContainerSize())
for (int i = 0; i < spArmorSize; ++i) {
const auto spItem = spArmor.getItem(i);
const auto pItem = pArmor.getItem(i);
spArmor.setItem(i, pItem);
spArmor.setItem(i, pArmor.getItem(i));
pArmor.setItem(i, spItem);
}
// swap enderchest
int spEnderSize = spEnder->getContainerSize();
if (spEnder.has_value() && pEnder.has_value() && spEnderSize == pEnder->getContainerSize())
for (int i = 0; i < spEnderSize; ++i) {
const auto spItem = spEnder->getItem(i);
const auto pItem = pEnder->getItem(i);
spEnder->setItem(i, pItem);
spEnder->setItem(i, pEnder->getItem(i));
pEnder->setItem(i, spItem);
}
// refresh
Expand All @@ -199,24 +200,19 @@ class SimPlayerManager {
CFSP_API int searchInInvWithId(int id, int start = 0) {
auto& inv = simPlayer->getInventory();
int size = inv.getContainerSize();
for (int i = start; i < size; ++i) {
for (int i = start; i < size; ++i)
if (inv.getItem(i).getId() == id) return i;
}
return -1;
}
CFSP_API int searchInInvWithName(std::string const& itemName, int start = 0) {
auto& inv = simPlayer->getInventory();
int size = inv.getContainerSize();
for (int i = start; i < size; ++i) {
const auto& it = inv.getItem(i);
const auto& customName = it.getCustomName();
if ((customName.empty() ? it.getName() : customName) == itemName) return i;
}
for (int i = start; i < size; ++i)
if (utils::removeMinecraftPrefix(inv.getItem(i).getTypeName()) == itemName) return i;
return -1;
}
CFSP_API bool selectSlot(int slot) {
int maxslot = simPlayer->getInventory().getContainerSize();
if (slot < 0 || slot >= maxslot) return false;
if (slot < 0 || slot >= simPlayer->getInventory().getContainerSize()) return false;
int sel = simPlayer->getSelectedItemSlot();
utils::swapItemInContainer(simPlayer, sel, slot);
return true;
Expand Down Expand Up @@ -278,6 +274,105 @@ class SimPlayerManager {
if (scheduler->isRunning(scriptid)) cancelScript();
scriptid = 0;
}
CFSP_API int getFirstEmptySlot() {
auto& inv = simPlayer->getInventory();
int size = inv.getContainerSize();
for (int i = 0; i < size; ++i)
if (inv.getItem(i) == ItemStack::EMPTY_ITEM) return i;
return -1;
}
enum ContainerOperationErrCode : int {
Success = 0,
NoHit = 1,
NoContainer = 2,
NoBlockActor = 3,
CannotOpen = 4,
SlotOutOfRange = 5,
NoEnoughSpace = 6,
NoItem = 7
};
CFSP_API ContainerOperationErrCode trySwapSlotWithContainer(int invSlot, int targetSlot) {
using errc = ContainerOperationErrCode;
const auto& hit = simPlayer->traceRay(5.25f, false, true);
if (!hit) return errc::NoHit;
auto& blockSource = simPlayer->getDimensionBlockSource();
const auto& block = blockSource.getBlock(hit.mBlockPos);
if (!block.isContainerBlock()) return errc::NoContainer;
auto* blockActor = blockSource.getBlockEntity(hit.mBlockPos);
if (!blockActor) return errc::NoBlockActor;
auto type = blockActor->getType();
if (type == BlockActorType::Chest || type == BlockActorType::ShulkerBox) {
auto* chestBlockActor = reinterpret_cast<ChestBlockActor*>(blockActor);
if (!chestBlockActor->canOpen(blockSource)) return errc::CannotOpen;
}
auto* container = blockActor->getContainer();
if (!container) return errc::NoContainer;
auto& inv = simPlayer->getInventory();
if (invSlot >= inv.getContainerSize() || targetSlot >= container->getContainerSize())
return errc::SlotOutOfRange;
auto item = container->getItem(targetSlot);
container->setItem(targetSlot, inv.getItem(invSlot));
inv.setItem(invSlot, item);
return errc::Success;
}
CFSP_API ContainerOperationErrCode tryPutIntoContainer(int invSlot) {
using errc = ContainerOperationErrCode;
const auto& hit = simPlayer->traceRay(5.25f, false, true);
if (!hit) return errc::NoHit;
auto& blockSource = simPlayer->getDimensionBlockSource();
const auto& block = blockSource.getBlock(hit.mBlockPos);
if (!block.isContainerBlock()) return errc::NoContainer;
auto* blockActor = blockSource.getBlockEntity(hit.mBlockPos);
if (!blockActor) return errc::NoBlockActor;
auto type = blockActor->getType();
if (type == BlockActorType::Chest || type == BlockActorType::ShulkerBox) {
auto* chestBlockActor = reinterpret_cast<ChestBlockActor*>(blockActor);
if (!chestBlockActor->canOpen(blockSource)) return errc::CannotOpen;
}
auto* container = blockActor->getContainer();
if (!container) return errc::NoContainer;
auto& inv = simPlayer->getInventory();
if (invSlot >= inv.getContainerSize()) return errc::SlotOutOfRange;
int size = container->getContainerSize();
for (int i = 0; i < size; ++i) {
if (container->getItem(i) == ItemStack::EMPTY_ITEM) {
container->setItem(i, inv.getItem(invSlot));
inv.setItem(invSlot, ItemStack::EMPTY_ITEM);
return errc::Success;
}
}
return errc::NoEnoughSpace;
}
CFSP_API ContainerOperationErrCode tryGetFromContainerWithName(std::string const& typeName) {
using errc = ContainerOperationErrCode;
const auto& hit = simPlayer->traceRay(5.25f, false, true);
if (!hit) return errc::NoHit;
auto& blockSource = simPlayer->getDimensionBlockSource();
const auto& block = blockSource.getBlock(hit.mBlockPos);
if (!block.isContainerBlock()) return errc::NoContainer;
auto* blockActor = blockSource.getBlockEntity(hit.mBlockPos);
if (!blockActor) return errc::NoBlockActor;
auto type = blockActor->getType();
if (type == BlockActorType::Chest || type == BlockActorType::ShulkerBox) {
auto* chestBlockActor = reinterpret_cast<ChestBlockActor*>(blockActor);
if (!chestBlockActor->canOpen(blockSource)) return errc::CannotOpen;
}
auto* container = blockActor->getContainer();
if (!container) return errc::NoContainer;
auto& inv = simPlayer->getInventory();
if (int emptySlot = getFirstEmptySlot(); emptySlot != -1) {
int size = container->getContainerSize();
for (int i = 0; i < size; ++i) {
const auto& item = container->getItem(i);
if (utils::removeMinecraftPrefix(item.getTypeName()) == typeName) {
inv.setItem(emptySlot, item);
container->setItem(i, ItemStack::EMPTY_ITEM);
return errc::Success;
}
}
return errc::NoItem;
} else return errc::NoEnoughSpace;
}
};

private:
Expand Down
3 changes: 2 additions & 1 deletion src/cfsp/simplayer/luaapi/BlockInfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "cfsp/base/Utils.h"
#include "mc/nbt/CompoundTag.h"
#include "mc/nbt/CompoundTagVariant.h"
#include "mc/nbt/Tag.h"
Expand Down Expand Up @@ -48,7 +49,7 @@ class BlockInfo {
public:
BlockInfo(BlockPos const bp, int d, Block const& bl)
: name(bl.buildDescriptionName()),
type(bl.getTypeName()),
type(utils::removeMinecraftPrefix(bl.getTypeName())),
id(bl.getBlockItemId()),
pos(bp),
dim(d),
Expand Down
3 changes: 2 additions & 1 deletion src/cfsp/simplayer/luaapi/ItemInfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "cfsp/base/Utils.h"
#include "mc/nbt/CompoundTag.h"
#include "mc/nbt/CompoundTagVariant.h"
#include "mc/nbt/Tag.h"
Expand Down Expand Up @@ -53,7 +54,7 @@ class ItemInfo {
public:
ItemInfo(ItemStack const& item)
: name(item.getCustomName().empty() ? item.getName() : item.getCustomName()),
type(item.getTypeName()),
type(utils::removeMinecraftPrefix(item.getTypeName())),
id(item.getId()),
count(item.mCount),
aux(item.getAuxValue()),
Expand Down
Loading

0 comments on commit e158c74

Please sign in to comment.