Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
修复获取计分板问题
Browse files Browse the repository at this point in the history
修复表单回调指针错误问题
修复Player.removeItem缺失问题
修复NBT.asString缺失问题
新增Player.__repr__函数
将Player.tell改为Player.sendText
  • Loading branch information
twoone3 committed Jul 1, 2022
1 parent dceac85 commit 5a9442f
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 283 deletions.
15 changes: 8 additions & 7 deletions API/CommandAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,14 @@ bool CommandClass::addOverload(vector<string>&& args) {
bool CommandClass::setCallback(const py::function& cb) {
if (thiz == nullptr)
return false;
thiz->setCallback([cb](const DynamicCommand& command, const CommandOrigin& origin,
CommandOutput& output, std::unordered_map<string, DynamicCommand::Result>& results) {
PY_TRY;
py::gil_scoped_acquire gil_;
cb(CommandOriginClass(const_cast<CommandOrigin*>(&origin)), results);
PY_CATCH;
});
thiz->setCallback(
[cb](const DynamicCommand& command, const CommandOrigin& origin,
CommandOutput& output, std::unordered_map<string, DynamicCommand::Result>& results) {
PY_TRY;
py::gil_scoped_acquire gil_;
cb(CommandOriginClass(const_cast<CommandOrigin*>(&origin)), results);
PY_CATCH;
});
return true;
}

Expand Down
7 changes: 3 additions & 4 deletions API/EventAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "McAPI.h"

#include "BlockAPI.h"
#include "GuiAPI.h"
#include "CommandAPI.h"
#include "ContainerAPI.h"
#include "ItemAPI.h"
Expand All @@ -21,6 +20,8 @@
#include <MC/VanillaBlocks.hpp>
#include <MC/ItemStack.hpp>

#include <MC/AvailableCommandsPacket.hpp>

using namespace std;

class Callbacker {
Expand All @@ -29,14 +30,12 @@ class Callbacker {
//事件回调
bool callback() {
bool pass = true;
arg_.inc_ref();
arg_.inc_ref(); // TODO: 为什么需要增加引用计数?
for (auto& cb : listeners[type_]) {
PY_TRY;
py::gil_scoped_acquire gil_;
pass = bool(cb(arg_));
PY_CATCH;

// TODO: 为什么需要增加引用计数?
}
return pass;
}
Expand Down
122 changes: 20 additions & 102 deletions API/ItemAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,32 @@ ItemClass::ItemClass() : thiz(ItemStack::create()) {}
ItemClass::ItemClass(ItemStack* p) : thiz(p) {}

string ItemClass::getName() {
if (!thiz)
throw std::invalid_argument("invalid item");
return thiz->getName();
}

string ItemClass::getType() {
if (!thiz)
throw std::invalid_argument("invalid item");
return thiz->getTypeName();
}

short ItemClass::getId() {
if (!thiz)
throw std::invalid_argument("invalid item");
return thiz->getId();
}

int ItemClass::getCount() {
if (!thiz)
throw std::invalid_argument("invalid item");
return thiz->getCount();
}

int ItemClass::getAux() {
if (!thiz)
throw std::invalid_argument("invalid item");
return thiz->getAux();
}

Expand All @@ -42,129 +52,37 @@ ItemClass ItemClass::clone() {
}

bool ItemClass::isNull() {
if (!thiz)
throw std::invalid_argument("invalid item");
return thiz->isNull();
}

bool ItemClass::setNull() {
if (!thiz)
throw std::invalid_argument("invalid item");
thiz->setNull();
return true;
}

bool ItemClass::setAux(short aux) {
if (!thiz)
throw std::invalid_argument("invalid item");
thiz->setAuxValue(aux);
return true;
}

bool ItemClass::setLore(const vector<string>& lores) {
if (!thiz)
throw std::invalid_argument("invalid item");
return thiz->setLore(lores);
}

NbtClass ItemClass::getNbt() {
if (!thiz)
throw std::invalid_argument("invalid item");
return thiz->getNbt();
}

bool ItemClass::setNbt(const NbtClass& nbt) {
return thiz->setNbt(nbt.thiz->asCompoundTag());
}

// auto McClass::newItem() {
// CHECK_ARGS_COUNT(args, 1);
//
// try {
// if (args[0].isString()) {
// // name & count
// if (args.size() >= 2 && args[1].isNumber()) {
// string type = args[0].toStr();
// int cnt = args[1].toInt();
//
// ItemStack* item = ItemStack::create(type, cnt);
// if (!item)
// return {}; // Null
// else
// return ItemClass::newItem(item);
// }
// else {
// LOG_TOO_FEW_ARGS();
// return {};
// }
// }
// else {
// CompoundTag* nbt = (CompoundTag*)NbtClass::extract(args[0]);
// if (nbt) {
// ItemStack* item = ItemStack::create(nbt->clone());
// if (!item)
// return {}; // Null
// else
// return ItemClass::newItem(item);
// }
// else {
// LOG_WRONG_ARG_TYPE();
// return {};
// }
// }
// }
// CATCH("Fail in NewItem!");
// }

// auto McClass::spawnItem() {
// CHECK_ARGS_COUNT(args, 2);
//
// try {
// FloatVec4 pos;
// if (args.size() == 2) {
// if (IsInstanceOf<IntPos>(args[1])) {
// // IntPos
// IntPos* posObj = IntPos::extractPos(args[1]);
// if (posObj->dim < 0)
// return false;
// else {
// pos.x = posObj->x;
// pos.y = posObj->y;
// pos.z = posObj->z;
// pos.dim = posObj->dim;
// }
// }
// else if (IsInstanceOf<FloatPos>(args[1])) {
// // FloatPos
// FloatPos* posObj = FloatPos::extractPos(args[1]);
// if (posObj->dim < 0)
// return false;
// else {
// pos = *posObj;
// }
// }
// else {
// LOG_WRONG_ARG_TYPE();
// return {};
// }
// }
// else if (args.size() == 5) {
// // Number Pos
// CHECK_ARG_TYPE(args[1], ValueKind::kNumber);
// CHECK_ARG_TYPE(args[2], ValueKind::kNumber);
// CHECK_ARG_TYPE(args[3], ValueKind::kNumber);
// CHECK_ARG_TYPE(args[4], ValueKind::kNumber);
// pos = {args[1].asNumber().toFloat(), args[2].asNumber().toFloat(), args[3].asNumber().toFloat(), args[4].toInt()};
// }
// else {
// LOG_WRONG_ARGS_COUNT();
// return {};
// }
//
//
// ItemStack* it = ItemClass::extract(args[0]);
// if (it) {
// // By Item
// Actor* entity = Level::spawnItem(pos.getVec3(), pos.dim, it);
// if (!entity)
// return {}; // Null
// else
// return EntityClass::newEntity(entity);
// }
// else {
// LOG_WRONG_ARG_TYPE();
// return {};
// }
// }
// CATCH("Fail in SpawnItem!");
// }
8 changes: 4 additions & 4 deletions API/LoggerAPI.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <Global.hpp>
#include "LoggerAPI.h"
#include "PlayerAPI.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
//#include <iostream>
//#include <fstream>
//#include <sstream>
//#include <string>
#include <MC/Player.hpp>

LoggerClass::LoggerClass(const string& title) : thiz(title) {}
Expand Down
33 changes: 16 additions & 17 deletions API/McAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ void registerCommand(const string& name, const string& desc, const py::function&
using ParamType = DynamicCommand::ParameterType;
auto command = DynamicCommand::createCommand(name, desc, perm);
command->addOverload();
command->setCallback([cb](DynamicCommand const& command, CommandOrigin const& origin,
CommandOutput& output, std::unordered_map<std::string, DynamicCommand::Result>& results) {
PY_TRY;
py::gil_scoped_acquire gil_;
cb(EntityClass((Player*)origin.getPlayer()), results);
PY_CATCH;
});
command->setCallback(
[cb](DynamicCommand const& command, CommandOrigin const& origin,
CommandOutput& output, std::unordered_map<std::string, DynamicCommand::Result>& results) {
PY_TRY;
py::gil_scoped_acquire gil_;
cb(EntityClass((Player*)origin.getPlayer()), results);
PY_CATCH;
});
DynamicCommand::setup(std::move(command));
}

Expand Down Expand Up @@ -169,16 +170,10 @@ ObjectiveClass getDisplayObjective(const string& slot) {
return (Objective*)res;
}
ObjectiveClass clearDisplayObjective(const string& slot) {
auto res = Global<Scoreboard>->clearDisplayObjective(slot);
if (!res)
return nullptr;
return res;
return Global<Scoreboard>->clearDisplayObjective(slot);
}
ObjectiveClass getScoreObjective(const string& name) {
auto res = Global<Scoreboard>->getObjective(name);
if (!res)
return nullptr;
return res;
return Global<Scoreboard>->getObjective(name);
}
ObjectiveClass newScoreObjective(const string& name, const string& display) {
return Scoreboard::newObjective(name, display);
Expand All @@ -190,7 +185,11 @@ bool removeScoreObjective(const string& name) {
Global<Scoreboard>->removeObjective(obj);
return true;
}
vector<const Objective*> getAllScoreObjectives() {
return Global<Scoreboard>->getObjectives();
vector<ObjectiveClass> getAllScoreObjectives() {
vector<ObjectiveClass> res;
for (auto x:Global<Scoreboard>->getObjectives()) {
res.push_back(ObjectiveClass(const_cast<Objective*>(x)));
}
return res;
}
} // namespace mc
2 changes: 1 addition & 1 deletion API/McAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ ObjectiveClass clearDisplayObjective(const string& slot);
ObjectiveClass getScoreObjective(const string& name);
ObjectiveClass newScoreObjective(const string& name, const string& display);
bool removeScoreObjective(const string& name);
vector<const Objective*> getAllScoreObjectives();
vector<ObjectiveClass> getAllScoreObjectives();
}
Loading

0 comments on commit 5a9442f

Please sign in to comment.