Skip to content
This repository has been archived by the owner on Dec 26, 2021. It is now read-only.

Player place + break functions #460

Merged
merged 1 commit into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions Horion/Scripting/Functions/LocalPlayerFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,79 @@ JsValueRef CALLBACK LocalPlayerFunctions::getInventory(JsValueRef callee, bool i

return inventoryArr;
}

JsValueRef CALLBACK LocalPlayerFunctions::breakBlock(JsValueRef callee, bool isConstructCall, JsValueRef* arguments, unsigned short argumentCount, void* callbackState) {
auto ent = EntityFunctions::getEntityFromValue(arguments[0]);
if (ent == nullptr) {
ENTITY_INVALID;
}

const auto plr = reinterpret_cast<C_LocalPlayer*>(ent);

auto vecOpt = Vector3Functions::getVec3FromArguments(&arguments[1], argumentCount - 1);
if (!vecOpt.has_value()) {
THROW(L"Invalid vector!");
}

vec3_ti pos = vecOpt.value();

g_Data.getCGameMode()->destroyBlock(&pos, 1);
return chakra.trueValue();
}

JsValueRef CALLBACK LocalPlayerFunctions::placeBlock(JsValueRef callee, bool isConstructCall, JsValueRef* arguments, unsigned short argumentCount, void* callbackState) {
auto ent = EntityFunctions::getEntityFromValue(arguments[0]);
if (ent == nullptr) {
ENTITY_INVALID;
}

const auto plr = reinterpret_cast<C_LocalPlayer*>(ent);

auto vecOpt = Vector3Functions::getVec3FromArguments(&arguments[1], argumentCount - 1);
if (!vecOpt.has_value()) {
THROW(L"Invalid vector!");
}

vec3_ti pos = vecOpt.value();

g_Data.getCGameMode()->buildBlock(&pos, 1);
return chakra.trueValue();
}

JsValueRef CALLBACK LocalPlayerFunctions::breakBlockRelativeToPlr(JsValueRef callee, bool isConstructCall, JsValueRef* arguments, unsigned short argumentCount, void* callbackState) {
auto ent = EntityFunctions::getEntityFromValue(arguments[0]);
if (ent == nullptr) {
ENTITY_INVALID;
}

const auto plr = reinterpret_cast<C_LocalPlayer*>(ent);

auto vecOpt = Vector3Functions::getVec3FromArguments(&arguments[1], argumentCount - 1);
if (!vecOpt.has_value()) {
THROW(L"Invalid vector!");
}

vec3_ti pos = vecOpt.value().add(*plr->getPos());

g_Data.getCGameMode()->destroyBlock(&pos, 1);
return chakra.trueValue();
}

JsValueRef CALLBACK LocalPlayerFunctions::placeBlockRelativeToPlr(JsValueRef callee, bool isConstructCall, JsValueRef* arguments, unsigned short argumentCount, void* callbackState) {
auto ent = EntityFunctions::getEntityFromValue(arguments[0]);
if (ent == nullptr) {
ENTITY_INVALID;
}

const auto plr = reinterpret_cast<C_LocalPlayer*>(ent);

auto vecOpt = Vector3Functions::getVec3FromArguments(&arguments[1], argumentCount - 1);
if (!vecOpt.has_value()) {
THROW(L"Invalid vector!");
}

vec3_ti pos = vecOpt.value().add(*plr->getPos());

g_Data.getCGameMode()->buildBlock(&pos, 1);
return chakra.trueValue();
}
4 changes: 4 additions & 0 deletions Horion/Scripting/Functions/LocalPlayerFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ class LocalPlayerFunctions {
DECL_FUN(setIsOnGround);
DECL_FUN(getInventory);
DECL_FUN(getInventorySlot);
DECL_FUN(breakBlock);
DECL_FUN(placeBlock);
DECL_FUN(breakBlockRelativeToPlr);
DECL_FUN(placeBlockRelativeToPlr);
};
5 changes: 5 additions & 0 deletions Horion/Scripting/ScriptManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ void ScriptManager::prepareLocalPlayerPrototype(JsValueRef proto, ContextObjects
chakra.defineFunction(proto, L"setIsOnGround", LocalPlayerFunctions::setIsOnGround, objs);
chakra.defineFunction(proto, L"getInventory", LocalPlayerFunctions::getInventory, objs);
chakra.defineFunction(proto, L"getInventorySlot", LocalPlayerFunctions::getInventorySlot, objs);

chakra.defineFunction(proto, L"placeBlock", LocalPlayerFunctions::placeBlock, objs);
chakra.defineFunction(proto, L"placeBlockRelative", LocalPlayerFunctions::placeBlockRelativeToPlr, objs);
chakra.defineFunction(proto, L"breakBlock", LocalPlayerFunctions::breakBlock, objs);
chakra.defineFunction(proto, L"breakBlockRelative", LocalPlayerFunctions::breakBlockRelativeToPlr, objs);
}

void ScriptManager::prepareGameFunctions(JsValueRef global, ContextObjects* objs) {
Expand Down