Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat #1112 #1118

Merged
merged 14 commits into from
Mar 19, 2023
9 changes: 5 additions & 4 deletions LiteLoader/include/llapi/mc/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,13 @@ class Player : public Mob {
* @return True if the player is successfully given the items; otherwise false
*/
LIAPI bool giveItem(ItemStack* item);
LIAPI bool giveItem(string typeName, int amount);
LIAPI bool giveItem(std::string typeName, int amount);
LIAPI bool giveItem(ItemStack* item, int amount);

LIAPI int clearItem(string typeName);
[[deprecated]] LIAPI int clearItem(std::string typeName);
LIAPI unsigned int clearItem(std::string_view typeName, unsigned int clearCount);
LIAPI bool runcmd(const string& cmd);
LIAPI bool transferServer(const string& address, unsigned short port);
LIAPI bool transferServer(const std::string& address, unsigned short port);
LIAPI bool setSidebar(const std::string& title, const std::vector<std::pair<std::string, int>>& data, ObjectiveSortOrder sortOrder);
LIAPI bool removeSidebar();

Expand Down Expand Up @@ -2148,4 +2149,4 @@ class Player : public Mob {

private:

};
};
85 changes: 56 additions & 29 deletions LiteLoader/src/llapi/mc/PlayerAPI.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <bitset>
#include <bitset>
#include "magic_enum/magic_enum.hpp"

#include "llapi/mc/Minecraft.hpp"
Expand Down Expand Up @@ -251,48 +251,75 @@ bool Player::giveItem(string typeName, int amount) {
return true;
}

int Player::clearItem(string typeName) {
int res = 0;

int Player::clearItem(string typeName){
return this->clearItem(typeName, 2^32);
}

unsigned int Player::clearItem(string_view typeName, unsigned int num) {
unsigned int clearedCount = 0;
if (num < 0) {
return 0;
}

auto reduceItemCount = [&typeName, &clearedCount, num](Player* player, ItemStack* item) {
if (item->getTypeName() == typeName) {
auto itemCount = item->getCount();
if (itemCount >= num - clearedCount) {
item->setNull({});
clearedCount += out;
}
else {
item->remove(num - clearedCount);
clearedCount = num;
}
}
}

// Hand
ItemStack* item = getHandSlot();
if (item->getTypeName() == typeName) {
auto out = item->getCount();
item->setNull({});
res += out;
reduceItemCount(this, getHandSlot());
if (clearedCount >= num) { // > case should never happen
refreshInventory();
return clearedCount;
}


// OffHand
item = (ItemStack*)&getOffhandSlot();
if (item->getTypeName() == typeName) {
auto out = item->getCount();
item->setNull({});
res += out;
reduceItemCount(this, (ItemStack*)&getOffhandSlot());
if (clearedCount >= num) { // > case should never happen
refreshInventory();
return clearedCount;
}


// Inventory
Container* container = &getInventory();
auto items = container->getAllSlots();
int size = container->getSize();
for (int i = 0; i < size; ++i) {
if (items[i]->getTypeName() == typeName) {
int cnt = items[i]->getCount();
container->removeItem(i, cnt);
res += cnt;
{
Container* container = &getInventory();
auto items = container->getAllSlots();
auto size = container->getSize();
for (int i = 0; i < size; ++i) {
reduceItemCount(this, items[i]);
if (clearedCount >= num) { // > case should never happen
refreshInventory();
return clearedCount;
}
}
}

// Armor
auto& armor = getArmorContainer();
items = armor.getAllSlots();
size = armor.getSize();
for (int i = 0; i < size; ++i) {
if (items[i]->getTypeName() == typeName) {
int cnt = items[i]->getCount();
armor.removeItem(i, cnt);
res += cnt;
{
Container* armor = &getArmorContainer();
auto items = armor.getAllSlots();
auto size = armor.getSize();
for (int i = 0; i < size; ++i) {
reduceItemCount(this, items[i]);
if (clearedCount >= num) { // > case should never happen
refreshInventory();
return clearedCount;
}
}
}

refreshInventory();
return res;
}
Expand Down
12 changes: 9 additions & 3 deletions ScriptEngine/src/api/PlayerAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2549,13 +2549,19 @@ Local<Value> PlayerClass::giveItem(const Arguments& args) {
Local<Value> PlayerClass::clearItem(const Arguments& args) {
CHECK_ARGS_COUNT(args, 1);
CHECK_ARG_TYPE(args[0], ValueKind::kString);
CHECK_ARG_TYPE(args[1], ValueKind::kNumber);

try {
Player* player = get();
if (!player)
if (!player) {
return Local<Value>();

return Number::newNumber(player->clearItem(args[0].toStr()));
}
if (args.size() == 1) {
return Number::newNumber(player->clearItem(args[0].toStr()));
}
else {
return Number::newNumber(player->clearItem(args[0].toStr(), args[1].asNumber().toInt32()));
}
}
CATCH("Fail in clearItem!");
}
Expand Down