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

Commit

Permalink
1.9.4更新,修复部分监听器无效的问题,新增onCmdBlockExecute事件
Browse files Browse the repository at this point in the history
  • Loading branch information
twoone3l committed Feb 1, 2022
1 parent 7fd73b9 commit 8bfe0d2
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 10 deletions.
1 change: 1 addition & 0 deletions BDSpyrunner.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ copy $(OutputPath)$(ProjectName).pdb ..\BDS\plugins\$(ProjectName).pdb
<ClInclude Include="mod\CPython.h" />
<ClInclude Include="mod\PyItemStack.h" />
<ClInclude Include="mod\PyBlockInstance.h" />
<ClInclude Include="mod\Version.h" />
</ItemGroup>
<ItemGroup>
<Library Include="lib\python37.lib" />
Expand Down
3 changes: 3 additions & 0 deletions BDSpyrunner.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
<ClInclude Include="mod\PyBlockInstance.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="mod\Version.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Library Include="lib\python37.lib">
Expand Down
11 changes: 8 additions & 3 deletions mod/Event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ class Callbacker {
PyGILGuard gil_;
};

#define EVENT_BEGIN(evt) if(!evt::hasListener())evt::subscribe([code](evt e){Callbacker h(code); h.insert("Event",magic_enum::enum_name(code))
#define EVENT_BEGIN(evt) evt::subscribe([code](evt e){Callbacker h(code); h.insert("Event",magic_enum::enum_name(code))
#define EVENT_INSERT(key) h.insert(#key, e.m##key)
#define EVENT_INSERT2(key,value) h.insert(#key, value)
#define EVENT_END return h.callback();})

void EnableEventListener(EventCode code, PyObject* func) {
void EnableEventListener(EventCode code) {
using namespace Event;
switch (code) {
case EventCode::onPreJoin:
Expand Down Expand Up @@ -315,6 +315,12 @@ void EnableEventListener(EventCode code, PyObject* func) {
EVENT_END;
break;
case EventCode::onCmdBlockExecute:
EVENT_BEGIN(CmdBlockExecuteEvent);
EVENT_INSERT(BlockInstance);
EVENT_INSERT(Command);
EVENT_INSERT(IsMinecart);
EVENT_INSERT(Minecart);
EVENT_END;
break;
case EventCode::onRedStoneUpdate:
break;
Expand Down Expand Up @@ -416,5 +422,4 @@ void EnableEventListener(EventCode code, PyObject* func) {
default:
break;
}
g_callback_functions[code].push_back(func);
}
5 changes: 2 additions & 3 deletions mod/Event.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#pragma once
#include <EventAPI.h>
#include "CPython.h"
#include "magic_enum.hpp"

enum class EventCode : int {
enum class EventCode {
onPreJoin, onJoin, onLeft, onPlayerCmd, onChat, onPlayerDie,
onRespawn, onChangeDim, onJump, onSneak, onAttack, onEat, onMove, onChangeSprinting, onSpawnProjectile,
onFireworkShootWithCrossbow, onSetArmor, onRide, onStepOnPressurePlate,
Expand All @@ -18,4 +17,4 @@ enum class EventCode : int {
onMoneyAdd, onMoneyReduce, onMoneyTrans, onMoneySet, onConsumeTotem, onEffectChanged,
};

void EnableEventListener(EventCode e, PyObject* func);
void EnableEventListener(EventCode e);
3 changes: 2 additions & 1 deletion mod/Main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Module.h"
#include "NBT.h"
#include "Version.h"

#define PLUGIN_PATH "plugins\\py\\"

Expand All @@ -17,7 +18,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) {
//Return FALSE to fail DLL load.
LL::registerPlugin(
"BDSpyrunner", "For .py plugins' loading",
LL::Version(1, 9, 3, LL::Version::Release),
LL::Version(PYR_VERSION_MAJOR, PYR_VERSION_MINOR, PYR_VERSION_MICRO, LL::Version::Release),
{ { "Author", "twoone3" } }
);
break;
Expand Down
10 changes: 9 additions & 1 deletion mod/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ static PyObject* setListener(PyObject*, PyObject* args) {
auto event_code = magic_enum::enum_cast<EventCode>(event_name);
if (!event_code.has_value())
Py_RETURN_ERROR_FORMAT("Invalid Listener key words %s", event_name);
EnableEventListener(event_code.value(), func);
//在全局回调表中查找code
auto funcs = g_callback_functions.find(event_code.value());
//如果是第一次设置则订阅监听器
if (funcs == g_callback_functions.end()) {
g_callback_functions[event_code.value()];
EnableEventListener(event_code.value());
}
//添加回调函数
funcs->second.push_back(func);
Py_RETURN_NONE;
}
//设置指令说明
Expand Down
2 changes: 0 additions & 2 deletions mod/Tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@

using json_t = nlohmann::detail::value_t;

constexpr const char* PYR_VERSION = "v1.9.3";

//字符串转JSON,本插件采用 https://json.nlohmann.me 的JSON库
fifo_json CompoundTagToJson(std::string_view str);

Expand Down
5 changes: 5 additions & 0 deletions mod/Version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once
constexpr unsigned PYR_VERSION_MAJOR = 1;
constexpr unsigned PYR_VERSION_MINOR = 9;
constexpr unsigned PYR_VERSION_MICRO = 4;
constexpr const char* PYR_VERSION = "v1.9.4";

0 comments on commit 8bfe0d2

Please sign in to comment.