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
2 changes: 1 addition & 1 deletion LiteLoader/include/llapi/mc/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class Player : public Mob {
LIAPI bool giveItem(string typeName, int amount);
LIAPI bool giveItem(ItemStack* item, int amount);

LIAPI int clearItem(string typeName);
LIAPI int clearItem(string typeName, int num = 2147483647);
ShrBox marked this conversation as resolved.
Show resolved Hide resolved
ShrBox marked this conversation as resolved.
Show resolved Hide resolved
LIAPI bool runcmd(const string& cmd);
LIAPI bool transferServer(const string& address, unsigned short port);
LIAPI bool setSidebar(const std::string& title, const std::vector<std::pair<std::string, int>>& data, ObjectiveSortOrder sortOrder);
Expand Down
62 changes: 52 additions & 10 deletions LiteLoader/src/llapi/mc/PlayerAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,23 +251,45 @@ bool Player::giveItem(string typeName, int amount) {
return true;
}

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

if (num < 0) {
return 0;
}
// Hand
ItemStack* item = getHandSlot();
if (item->getTypeName() == typeName) {
auto out = item->getCount();
item->setNull({});
res += out;
if (out >= num - res) {
item->setNull({});
res += out;
}
else {
item->remove(num - res);
res = num;
}
if (res >= num) {
refreshInventory();
return res;
}
}

// OffHand
item = (ItemStack*)&getOffhandSlot();
if (item->getTypeName() == typeName) {
auto out = item->getCount();
item->setNull({});
res += out;
if (out >= num - res) {
item->setNull({});
res += out;
}
else {
item->remove(num - res);
res = num;
}
if (res >= num) {
refreshInventory();
return res;
}
}

// Inventory
Expand All @@ -277,8 +299,18 @@ int Player::clearItem(string typeName) {
for (int i = 0; i < size; ++i) {
if (items[i]->getTypeName() == typeName) {
int cnt = items[i]->getCount();
container->removeItem(i, cnt);
res += cnt;
if (cnt >= num - res) {
container->removeItem(i, cnt);
res += cnt;
}
else {
container->removeItem(i, num - res);
res = num;
}
if (res >= num) {
refreshInventory();
return res;
}
}
}

Expand All @@ -289,8 +321,18 @@ int Player::clearItem(string typeName) {
for (int i = 0; i < size; ++i) {
if (items[i]->getTypeName() == typeName) {
int cnt = items[i]->getCount();
armor.removeItem(i, cnt);
res += cnt;
if (cnt >= num - res) {
container->removeItem(i, cnt);
res += cnt;
}
else {
container->removeItem(i, num - res);
res = num;
}
if (res >= num) {
refreshInventory();
return res;
}
}
}
refreshInventory();
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