mirror of
https://github.com/SikongJueluo/kubejs-utils.git
synced 2026-01-11 08:47:50 +08:00
refactor(C4): adapt to new functions and new ForgeEvent usage
This commit is contained in:
@@ -1,3 +1,12 @@
|
|||||||
|
/** @type {JClass_<$TickEvent$PlayerTickEvent_>} */
|
||||||
|
const $TickEvent$PlayerTickEvent = Java.loadClass(
|
||||||
|
"net.minecraftforge.event.TickEvent$PlayerTickEvent",
|
||||||
|
);
|
||||||
|
|
||||||
|
const $ServerStartedEvent = Java.loadClass(
|
||||||
|
"net.minecraftforge.event.server.ServerStartedEvent",
|
||||||
|
);
|
||||||
|
|
||||||
const C4_EXPLOSION_TIME = 3 * 20; // 3 seconds in ticks
|
const C4_EXPLOSION_TIME = 3 * 20; // 3 seconds in ticks
|
||||||
const C4_EXPLOSION_POWER = 128; // Explosion power (TNT is 4)
|
const C4_EXPLOSION_POWER = 128; // Explosion power (TNT is 4)
|
||||||
const C4_USE_TIME = 5 * 20; // 5 seconds in ticks
|
const C4_USE_TIME = 5 * 20; // 5 seconds in ticks
|
||||||
@@ -36,12 +45,11 @@ function isApproximatelyEqual(a, b, tolerance) {
|
|||||||
* @param {Internal.Player} player
|
* @param {Internal.Player} player
|
||||||
* @returns {{x: number, y: number, z: number}}
|
* @returns {{x: number, y: number, z: number}}
|
||||||
*/
|
*/
|
||||||
function getBlockUnderPlayer(player) {
|
function getFeetBlockPosition(player) {
|
||||||
const playerPos = player.position();
|
|
||||||
return {
|
return {
|
||||||
x: Math.floor(playerPos.x()),
|
x: Math.floor(player.x),
|
||||||
y: Math.floor(playerPos.y()) - 1,
|
y: Math.floor(player.y) - 1,
|
||||||
z: Math.floor(playerPos.z()),
|
z: Math.floor(player.z),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,17 +60,16 @@ function getBlockUnderPlayer(player) {
|
|||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
function shouldActivateC4(itemstack, level, player) {
|
function shouldActivateC4(itemstack, level, player) {
|
||||||
const blockUnder = getBlockUnderPlayer(player);
|
const blockUnder = getFeetBlockPosition(player);
|
||||||
const block = level.getBlock(blockUnder.x, blockUnder.y, blockUnder.z);
|
const block = level.getBlock(blockUnder.x, blockUnder.y, blockUnder.z);
|
||||||
|
|
||||||
const lookAngle = player.lookAngle;
|
const lookAngle = player.lookAngle;
|
||||||
const playerPos = player.position();
|
|
||||||
const lastPlayerInfo = lastPlayerInfoMap[player.uuid.toString()];
|
const lastPlayerInfo = lastPlayerInfoMap[player.uuid.toString()];
|
||||||
|
|
||||||
if (lastPlayerInfo === undefined) return false;
|
if (lastPlayerInfo === undefined) return false;
|
||||||
|
|
||||||
// Check if player moved (using block position for stability)
|
// Check if player moved (using block position for stability)
|
||||||
const currentBlockPos = getBlockUnderPlayer(player);
|
const currentBlockPos = getFeetBlockPosition(player);
|
||||||
const isBlockPosChanged =
|
const isBlockPosChanged =
|
||||||
currentBlockPos.x !== lastPlayerInfo.blockPos.x ||
|
currentBlockPos.x !== lastPlayerInfo.blockPos.x ||
|
||||||
currentBlockPos.y !== lastPlayerInfo.blockPos.y ||
|
currentBlockPos.y !== lastPlayerInfo.blockPos.y ||
|
||||||
@@ -70,36 +77,24 @@ function shouldActivateC4(itemstack, level, player) {
|
|||||||
|
|
||||||
// Check if player moved within the same block (with tolerance)
|
// Check if player moved within the same block (with tolerance)
|
||||||
const isPosChanged =
|
const isPosChanged =
|
||||||
!isApproximatelyEqual(
|
!isApproximatelyEqual(player.x, lastPlayerInfo.pos.x, POS_TOLERANCE) ||
|
||||||
playerPos.x(),
|
!isApproximatelyEqual(player.y, lastPlayerInfo.pos.y, POS_TOLERANCE) ||
|
||||||
lastPlayerInfo.pos.x,
|
!isApproximatelyEqual(player.z, lastPlayerInfo.pos.z, POS_TOLERANCE);
|
||||||
POS_TOLERANCE,
|
|
||||||
) ||
|
|
||||||
!isApproximatelyEqual(
|
|
||||||
playerPos.y(),
|
|
||||||
lastPlayerInfo.pos.y,
|
|
||||||
POS_TOLERANCE,
|
|
||||||
) ||
|
|
||||||
!isApproximatelyEqual(
|
|
||||||
playerPos.z(),
|
|
||||||
lastPlayerInfo.pos.z,
|
|
||||||
POS_TOLERANCE,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Check if player rotated view (with tolerance)
|
// Check if player rotated view (with tolerance)
|
||||||
const isAngleChanged =
|
const isAngleChanged =
|
||||||
!isApproximatelyEqual(
|
!isApproximatelyEqual(
|
||||||
lookAngle.x(),
|
lookAngle.get("x"),
|
||||||
lastPlayerInfo.angle.x,
|
lastPlayerInfo.angle.x,
|
||||||
ANGLE_TOLERANCE,
|
ANGLE_TOLERANCE,
|
||||||
) ||
|
) ||
|
||||||
!isApproximatelyEqual(
|
!isApproximatelyEqual(
|
||||||
lookAngle.y(),
|
lookAngle.get("y"),
|
||||||
lastPlayerInfo.angle.y,
|
lastPlayerInfo.angle.y,
|
||||||
ANGLE_TOLERANCE,
|
ANGLE_TOLERANCE,
|
||||||
) ||
|
) ||
|
||||||
!isApproximatelyEqual(
|
!isApproximatelyEqual(
|
||||||
lookAngle.z(),
|
lookAngle.get("z"),
|
||||||
lastPlayerInfo.angle.z,
|
lastPlayerInfo.angle.z,
|
||||||
ANGLE_TOLERANCE,
|
ANGLE_TOLERANCE,
|
||||||
);
|
);
|
||||||
@@ -120,7 +115,7 @@ function shouldActivateC4(itemstack, level, player) {
|
|||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
function shouldStartUseC4(player, level) {
|
function shouldStartUseC4(player, level) {
|
||||||
const blockUnder = getBlockUnderPlayer(player);
|
const blockUnder = getFeetBlockPosition(player);
|
||||||
const block = level.getBlock(blockUnder.x, blockUnder.y, blockUnder.z);
|
const block = level.getBlock(blockUnder.x, blockUnder.y, blockUnder.z);
|
||||||
|
|
||||||
if (block.id !== "kubejs:c4_target") {
|
if (block.id !== "kubejs:c4_target") {
|
||||||
@@ -132,18 +127,17 @@ function shouldStartUseC4(player, level) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const playerPos = player.position();
|
|
||||||
const lookAngle = player.lookAngle;
|
const lookAngle = player.lookAngle;
|
||||||
lastPlayerInfoMap[playerUuid] = {
|
lastPlayerInfoMap[playerUuid] = {
|
||||||
angle: {
|
angle: {
|
||||||
x: lookAngle.x(),
|
x: lookAngle.get("x"),
|
||||||
y: lookAngle.y(),
|
y: lookAngle.get("y"),
|
||||||
z: lookAngle.z(),
|
z: lookAngle.get("z"),
|
||||||
},
|
},
|
||||||
pos: {
|
pos: {
|
||||||
x: playerPos.x(),
|
x: player.x,
|
||||||
y: playerPos.y(),
|
y: player.y,
|
||||||
z: playerPos.z(),
|
z: player.z,
|
||||||
},
|
},
|
||||||
blockPos: blockUnder,
|
blockPos: blockUnder,
|
||||||
};
|
};
|
||||||
@@ -180,7 +174,7 @@ StartupEvents.registry("item", (event) => {
|
|||||||
.create("c4_item")
|
.create("c4_item")
|
||||||
.unstackable()
|
.unstackable()
|
||||||
.useAnimation("eat")
|
.useAnimation("eat")
|
||||||
.useDuration((_itemStack) => C4_USE_TIME) // 5 Seconds
|
.useDuration((_itemStack) => C4_USE_TIME)
|
||||||
.use((level, player, _hand) => {
|
.use((level, player, _hand) => {
|
||||||
if (!shouldStartUseC4(player, level)) return false;
|
if (!shouldStartUseC4(player, level)) return false;
|
||||||
|
|
||||||
@@ -259,9 +253,7 @@ ClientEvents.init(() => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Send data to the server when the key is pressed
|
// Send data to the server when the key is pressed
|
||||||
ForgeEvents.onEvent(
|
ForgeEvents.onEvent($TickEvent$PlayerTickEvent, (event) => {
|
||||||
"net.minecraftforge.event.TickEvent$PlayerTickEvent",
|
|
||||||
(event) => {
|
|
||||||
if (operationKeyMapping === undefined) {
|
if (operationKeyMapping === undefined) {
|
||||||
console.warn("Not in client platform");
|
console.warn("Not in client platform");
|
||||||
return;
|
return;
|
||||||
@@ -280,8 +272,7 @@ ForgeEvents.onEvent(
|
|||||||
console.warn("EventBus is not available");
|
console.warn("EventBus is not available");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
});
|
||||||
);
|
|
||||||
|
|
||||||
// ==================== Server Side Logic ====================
|
// ==================== Server Side Logic ====================
|
||||||
|
|
||||||
@@ -289,13 +280,13 @@ ForgeEvents.onEvent(
|
|||||||
* @param {{player: Internal.Player}} event
|
* @param {{player: Internal.Player}} event
|
||||||
*/
|
*/
|
||||||
function handleC4UseStarted(event) {
|
function handleC4UseStarted(event) {
|
||||||
const server = Utils.server;
|
const server = Utils.getServer();
|
||||||
if (server === null) {
|
if (server === null) {
|
||||||
console.error("C4 Handler: Server is not available");
|
console.error("C4 Handler: Server is not available");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const player = server.getPlayer(event.player.uuid);
|
const player = server.getPlayerList().getPlayer(event.player.uuid);
|
||||||
const level = player.level;
|
const level = player.level;
|
||||||
|
|
||||||
const startTime = level.levelData.gameTime;
|
const startTime = level.levelData.gameTime;
|
||||||
@@ -304,7 +295,13 @@ function handleC4UseStarted(event) {
|
|||||||
server.scheduleRepeatingInTicks(2, (event) => {
|
server.scheduleRepeatingInTicks(2, (event) => {
|
||||||
const itemstack = player.getMainHandItem();
|
const itemstack = player.getMainHandItem();
|
||||||
|
|
||||||
if (!shouldActivateC4(itemstack, player.level, player)) {
|
if (
|
||||||
|
!shouldActivateC4(
|
||||||
|
itemstack,
|
||||||
|
player.level,
|
||||||
|
/** @type {any} */ (player),
|
||||||
|
)
|
||||||
|
) {
|
||||||
player.stopUsingItem();
|
player.stopUsingItem();
|
||||||
player.addItemCooldown(originalItemstack.item, 20);
|
player.addItemCooldown(originalItemstack.item, 20);
|
||||||
originalItemstack.releaseUsing(
|
originalItemstack.releaseUsing(
|
||||||
@@ -352,11 +349,10 @@ function handleC4Activated(event) {
|
|||||||
const { level, player, explosionTime, explosionPower } = event;
|
const { level, player, explosionTime, explosionPower } = event;
|
||||||
|
|
||||||
// Place C4 at player's feet
|
// Place C4 at player's feet
|
||||||
const playerPos = player.position();
|
|
||||||
const c4BlockPos = {
|
const c4BlockPos = {
|
||||||
x: Math.floor(playerPos.x()),
|
x: Math.floor(player.x),
|
||||||
y: Math.floor(playerPos.y()),
|
y: Math.floor(player.y),
|
||||||
z: Math.floor(playerPos.z()),
|
z: Math.floor(player.z),
|
||||||
};
|
};
|
||||||
const newBlock = level.getBlock(c4BlockPos.x, c4BlockPos.y, c4BlockPos.z);
|
const newBlock = level.getBlock(c4BlockPos.x, c4BlockPos.y, c4BlockPos.z);
|
||||||
newBlock.set(/** @type {any} */ ("kubejs:c4"));
|
newBlock.set(/** @type {any} */ ("kubejs:c4"));
|
||||||
@@ -393,9 +389,7 @@ function handleC4Activated(event) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
ForgeEvents.onEvent(
|
ForgeEvents.onEvent($ServerStartedEvent, (event) => {
|
||||||
"net.minecraftforge.event.server.ServerStartedEvent",
|
|
||||||
(event) => {
|
|
||||||
/**
|
/**
|
||||||
* WARNING: Must Do!!!
|
* WARNING: Must Do!!!
|
||||||
* Because Kubejs scheduler is not stable
|
* Because Kubejs scheduler is not stable
|
||||||
@@ -417,5 +411,4 @@ ForgeEvents.onEvent(
|
|||||||
eventBus.register("C4Activated", handleC4Activated);
|
eventBus.register("C4Activated", handleC4Activated);
|
||||||
eventBus.register("C4UseStarted", handleC4UseStarted);
|
eventBus.register("C4UseStarted", handleC4UseStarted);
|
||||||
console.log("C4 Handler: Registered C4Activated event handler");
|
console.log("C4 Handler: Registered C4Activated event handler");
|
||||||
},
|
});
|
||||||
);
|
|
||||||
|
|||||||
@@ -2,12 +2,19 @@
|
|||||||
"$schema": "https://www.schemastore.org/jsconfig.json",
|
"$schema": "https://www.schemastore.org/jsconfig.json",
|
||||||
"extends": "../../jsconfig.json",
|
"extends": "../../jsconfig.json",
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"typeRoots": ["../../types/probe/startup/"]
|
"typeRoots": ["../../types/probe/startup/"],
|
||||||
|
"paths": {
|
||||||
|
"packages/*": [
|
||||||
|
"../../types/probe/startup/packages/*",
|
||||||
|
"../../types/probe/shared/*"
|
||||||
|
]
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"include": [
|
"include": ["./**/*", "../../types/*.d.ts", "../../types/probe/**/*.d.ts"],
|
||||||
"./**/*",
|
"exclude": [
|
||||||
"../../types/*.d.ts",
|
"../../types/probe/client/global/*.d.ts",
|
||||||
"../../types/probe/shared/*.d.ts",
|
"../../types/probe/server/global/*.d.ts",
|
||||||
"../../types/probe/startup/**/*.d.ts"
|
"../../types/probe/client/**/*.d.ts",
|
||||||
|
"../../types/probe/server/**/*.d.ts"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user