Skip to content

Commit

Permalink
fix: fix Player::getBlockFromViewVector #214
Browse files Browse the repository at this point in the history
feat(tests): add PlayerTests
  • Loading branch information
ShrBox committed Jan 29, 2025
1 parent 63fc467 commit 2f6e4a4
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 30 deletions.
52 changes: 29 additions & 23 deletions src/legacy/api/EntityAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1362,7 +1362,7 @@ Local<Value> EntityClass::getEntityFromViewVector(const Arguments& args) {
}
HitResult result = actor->traceRay(maxDistance, true, false);
Actor* entity = result.getEntity();
if (entity) {
if (result.mType != HitResultType::NoHit && entity) {
return EntityClass::newEntity(entity);
}
return Local<Value>();
Expand Down Expand Up @@ -1411,15 +1411,17 @@ Local<Value> EntityClass::getBlockFromViewVector(const Arguments& args) {
return true;
}
);

if (res.mType == HitResultType::NoHit) {
return Local<Value>();
}
BlockPos bp;
if (includeLiquid && res.mIsHitLiquid) {
bp = res.mLiquidPos;
} else {
bp = res.mBlock;
}
Block const& bl = actor->getDimensionBlockSource().getBlock(bp);
if (bl.isEmpty()) {
if (bl.isAir() || bl.isEmpty()) {
return Local<Value>();
}
return BlockClass::newBlock(bl, bp, actor->getDimensionId());
Expand Down Expand Up @@ -1804,16 +1806,18 @@ Local<Value> McClass::explode(const Arguments& args) {
bool isDestroy = args[beginIndex + 2].asBoolean().value();
bool isFire = args[beginIndex + 3].asBoolean().value();

return Boolean::newBoolean(ll::service::getLevel()->explode(
ll::service::getLevel()->getDimension(pos.dim)->getBlockSourceFromMainChunkSource(),
source.value_or(nullptr),
pos.getVec3(),
radius,
isFire,
isDestroy,
FLT_MAX,
false
));
return Boolean::newBoolean(
ll::service::getLevel()->explode(
ll::service::getLevel()->getDimension(pos.dim)->getBlockSourceFromMainChunkSource(),
source.value_or(nullptr),
pos.getVec3(),
radius,
isFire,
isDestroy,
FLT_MAX,
false
)
);
} else {
CHECK_ARG_TYPE(args[beginIndex + 1], ValueKind::kNumber);
CHECK_ARG_TYPE(args[beginIndex + 2], ValueKind::kNumber);
Expand All @@ -1825,16 +1829,18 @@ Local<Value> McClass::explode(const Arguments& args) {
bool isDestroy = args[beginIndex + 3].asBoolean().value();
bool isFire = args[beginIndex + 4].asBoolean().value();

return Boolean::newBoolean(ll::service::getLevel()->explode(
ll::service::getLevel()->getDimension(pos.dim)->getBlockSourceFromMainChunkSource(),
source.value_or(nullptr),
pos.getVec3(),
radius,
isFire,
isDestroy,
maxResistance,
false
));
return Boolean::newBoolean(
ll::service::getLevel()->explode(
ll::service::getLevel()->getDimension(pos.dim)->getBlockSourceFromMainChunkSource(),
source.value_or(nullptr),
pos.getVec3(),
radius,
isFire,
isDestroy,
maxResistance,
false
)
);
}
}
CATCH("Fail in Explode!");
Expand Down
8 changes: 5 additions & 3 deletions src/legacy/api/PlayerAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3120,7 +3120,7 @@ Local<Value> PlayerClass::getEntityFromViewVector(const Arguments& args) {
}
HitResult result = player->traceRay(maxDistance, true, false);
Actor* entity = result.getEntity();
if (entity) {
if (result.mType != HitResultType::NoHit && entity) {
return EntityClass::newEntity(entity);
}
return Local<Value>();
Expand Down Expand Up @@ -3169,15 +3169,17 @@ Local<Value> PlayerClass::getBlockFromViewVector(const Arguments& args) {
return true;
}
);

if (res.mType == HitResultType::NoHit) {
return Local<Value>();
}
BlockPos bp;
if (includeLiquid && res.mIsHitLiquid) {
bp = res.mLiquidPos;
} else {
bp = res.mBlock;
}
Block const& bl = player->getDimensionBlockSource().getBlock(bp);
if (bl.isEmpty()) {
if (bl.isAir() || bl.isEmpty()) {
return Local<Value>();
}
return BlockClass::newBlock(bl, bp, player->getDimensionBlockSource());
Expand Down
26 changes: 26 additions & 0 deletions src/tests/LSETests/PlayerTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export function TestPlayer(players) {
for (let player of players) {
GetFromViewVector(player)
}
}

function GetFromViewVector(player) {
let en = player.getEntityFromViewVector(5.25)
if (en) {
logger.info(`Entity: ${en.name}`)
let bl = player.getBlockFromViewVector(true, false, player.distanceTo(en), false)
if (bl) {
logger.info(`Block: ${bl.name} ${bl.pos}`)
} else {
logger.info(`No block found`)
}
} else {
logger.info(`No entity found`)
let bl = player.getBlockFromViewVector(true, false, 5.25, false)
if (bl) {
logger.info(`Block: ${bl.name} ${bl.pos}`)
} else {
logger.info(`No block found`)
}
}
}
2 changes: 1 addition & 1 deletion src/tests/LSETests/ScriptTests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function loggerTest(players) {
export function TestLogger(players) {
for (let player of players) {
logger.setPlayer(player)
}
Expand Down
10 changes: 7 additions & 3 deletions src/tests/LSETests/main.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import {RegisterEvents, events, triggeredEvents} from './plugins/LSETests/EventTests.js';
import {loggerTest} from './plugins/LSETests/ScriptTests.js';
import {TestLogger} from './plugins/LSETests/ScriptTests.js';
import {TestPlayer} from './plugins/LSETests/PlayerTests.js';

RegisterEvents();

mc.listen('onServerStarted', () => {
let cmd = mc.newCommand('lsetests', "LegacyScriptEngine tests", PermType.Console);
cmd.setEnum('testOption', ['logger', 'events']);
cmd.setEnum('testOption', ['logger', 'events', 'player']);
cmd.mandatory('testOption', ParamType.Enum, 'testOption');
cmd.optional('player', ParamType.Player);
cmd.overload('testOption', 'player');
cmd.setCallback((cmd, origin, output, results) => {
switch (results.testOption) {
case 'logger':
loggerTest(results.player);
TestLogger(results.player);
break;
case 'events':
const
notTriggeredEvents = events.filter(event => !triggeredEvents.has(event));
logger.info(`Events not triggered: ${notTriggeredEvents.join(', ')}`);
break;
case 'player':
TestPlayer(results.player);
break;
default:
logger.error(`Invalid test option ${results.testOption}`);
break;
Expand Down

0 comments on commit 2f6e4a4

Please sign in to comment.