mirror of
https://github.com/SikongJueluo/kubejs-utils.git
synced 2026-01-29 08:17:49 +08:00
feat(c4): improve C4 timer and add type definitions
This commit is contained in:
18
src/client_scripts/Test.js
Normal file
18
src/client_scripts/Test.js
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
let isFirstTime = true;
|
||||||
|
ClientEvents.tick((event) => {
|
||||||
|
if (!isFirstTime) return;
|
||||||
|
|
||||||
|
const player = event.player;
|
||||||
|
const lookAngle = player.lookAngle;
|
||||||
|
console.log(`Player Pos: ${player.x}, ${player.y}, ${player.z}`);
|
||||||
|
// console.log(
|
||||||
|
// `LookAngle(property): ${lookAngle.x}, ${lookAngle.y}, ${lookAngle.z}`,
|
||||||
|
// );
|
||||||
|
console.log(
|
||||||
|
`LookAngle(method): ${lookAngle.x()}, ${lookAngle.y()}, ${lookAngle.z()}`,
|
||||||
|
);
|
||||||
|
console.log(
|
||||||
|
`LookAngle(get method): ${lookAngle.get("x")}, ${lookAngle.get("y")}, ${lookAngle.get("z")}`,
|
||||||
|
);
|
||||||
|
isFirstTime = false;
|
||||||
|
});
|
||||||
1
src/server_scripts/C4.js
Normal file
1
src/server_scripts/C4.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
BlockEvents.broken((event) => {});
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
/** @type {JClass_<$TickEvent$PlayerTickEvent_>} */
|
|
||||||
const $TickEvent$PlayerTickEvent = Java.loadClass(
|
const $TickEvent$PlayerTickEvent = Java.loadClass(
|
||||||
"net.minecraftforge.event.TickEvent$PlayerTickEvent",
|
"net.minecraftforge.event.TickEvent$PlayerTickEvent",
|
||||||
);
|
);
|
||||||
@@ -7,7 +6,7 @@ const $ServerStartedEvent = Java.loadClass(
|
|||||||
"net.minecraftforge.event.server.ServerStartedEvent",
|
"net.minecraftforge.event.server.ServerStartedEvent",
|
||||||
);
|
);
|
||||||
|
|
||||||
const C4_EXPLOSION_TIME = 3 * 20; // 3 seconds in ticks
|
const C4_EXPLOSION_TIME = 7 * 20; // 7 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
|
||||||
|
|
||||||
@@ -29,6 +28,12 @@ let operationKeyMapping;
|
|||||||
*/
|
*/
|
||||||
const lastPlayerInfoMap = {};
|
const lastPlayerInfoMap = {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {{[key:string]: boolean}}
|
||||||
|
*/
|
||||||
|
const toExplosionC4Map = {};
|
||||||
|
global["toExplosionC4Map"] = toExplosionC4Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function to compare floating point numbers with tolerance
|
* Helper function to compare floating point numbers with tolerance
|
||||||
* @param {number} a
|
* @param {number} a
|
||||||
@@ -253,10 +258,18 @@ ClientEvents.init(() => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Send data to the server when the key is pressed
|
// Send data to the server when the key is pressed
|
||||||
ForgeEvents.onEvent($TickEvent$PlayerTickEvent, (event) => {
|
ForgeEvents.onEvent(
|
||||||
|
// @ts-ignore
|
||||||
|
$TickEvent$PlayerTickEvent,
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {Internal.TickEvent$PlayerTickEvent} event
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
(event) => {
|
||||||
if (operationKeyMapping === undefined) {
|
if (operationKeyMapping === undefined) {
|
||||||
console.warn("Not in client platform");
|
console.warn("Not in client platform");
|
||||||
return;
|
return event;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (operationKeyMapping.consumeClick()) {
|
while (operationKeyMapping.consumeClick()) {
|
||||||
@@ -272,7 +285,8 @@ ForgeEvents.onEvent($TickEvent$PlayerTickEvent, (event) => {
|
|||||||
console.warn("EventBus is not available");
|
console.warn("EventBus is not available");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
},
|
||||||
|
);
|
||||||
|
|
||||||
// ==================== Server Side Logic ====================
|
// ==================== Server Side Logic ====================
|
||||||
|
|
||||||
@@ -357,15 +371,26 @@ function handleC4Activated(event) {
|
|||||||
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"));
|
||||||
|
|
||||||
|
// Add record
|
||||||
|
const newBlockPosString = newBlock.pos.toShortString();
|
||||||
|
toExplosionC4Map[newBlockPosString] = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: It should use reschedule to replace several schedules
|
* TODO: It should use reschedule to replace several schedules
|
||||||
* But reschedule not work at current time.
|
* But reschedule not work at current time.
|
||||||
* Relative Issue: https://github.com/KubeJS-Mods/KubeJS/issues/763
|
* Relative Issue: https://github.com/KubeJS-Mods/KubeJS/issues/763
|
||||||
*/
|
*/
|
||||||
for (let i = 0; i < explosionTime; i += 20) {
|
let remainingSeconds = explosionTime / 20;
|
||||||
server.scheduleInTicks(i, (scheduledEvent) => {
|
server.scheduleRepeatingInTicks(20, (scheduledEvent) => {
|
||||||
const remainingSeconds =
|
// Assert C4 exsiting
|
||||||
(explosionTime - scheduledEvent.timer) / 20;
|
if (toExplosionC4Map[newBlockPosString] === null) return;
|
||||||
|
|
||||||
|
remainingSeconds -= 1;
|
||||||
|
if (remainingSeconds <= 0) {
|
||||||
|
scheduledEvent.clear();
|
||||||
|
return scheduledEvent;
|
||||||
|
}
|
||||||
|
|
||||||
server.players.forEach((p) => {
|
server.players.forEach((p) => {
|
||||||
p.tell(
|
p.tell(
|
||||||
/** @type {any} */ (
|
/** @type {any} */ (
|
||||||
@@ -374,10 +399,12 @@ function handleC4Activated(event) {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
// Create explosion after countdown
|
// Create explosion after countdown
|
||||||
server.scheduleInTicks(explosionTime, (_) => {
|
server.scheduleInTicks(explosionTime, (_) => {
|
||||||
|
// Assert C4 exsiting
|
||||||
|
if (toExplosionC4Map[newBlockPosString] === null) return;
|
||||||
|
|
||||||
level.explode(
|
level.explode(
|
||||||
/** @type {any} */ (null),
|
/** @type {any} */ (null),
|
||||||
c4BlockPos.x + 0.5,
|
c4BlockPos.x + 0.5,
|
||||||
@@ -389,7 +416,15 @@ function handleC4Activated(event) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
ForgeEvents.onEvent($ServerStartedEvent, (event) => {
|
ForgeEvents.onEvent(
|
||||||
|
//@ts-ignore
|
||||||
|
$ServerStartedEvent,
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {Internal.ServerStartedEvent} event
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
(event) => {
|
||||||
/**
|
/**
|
||||||
* WARNING: Must Do!!!
|
* WARNING: Must Do!!!
|
||||||
* Because Kubejs scheduler is not stable
|
* Because Kubejs scheduler is not stable
|
||||||
@@ -405,10 +440,11 @@ ForgeEvents.onEvent($ServerStartedEvent, (event) => {
|
|||||||
|
|
||||||
if (eventBus === null) {
|
if (eventBus === null) {
|
||||||
console.error("C4 Handler: eventBus is not available");
|
console.error("C4 Handler: eventBus is not available");
|
||||||
return;
|
return event;
|
||||||
}
|
}
|
||||||
|
|
||||||
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");
|
||||||
});
|
},
|
||||||
|
);
|
||||||
|
|||||||
16
types/C4.d.ts
vendored
Normal file
16
types/C4.d.ts
vendored
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
/**
|
||||||
|
* Event data for C4 use started events.
|
||||||
|
*/
|
||||||
|
interface C4UseStartedEvent {
|
||||||
|
player: Internal.Player;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event data for C4 activation events.
|
||||||
|
*/
|
||||||
|
interface C4ActivatedEvent {
|
||||||
|
level: Internal.Level;
|
||||||
|
player: Internal.Player;
|
||||||
|
explosionTime: number;
|
||||||
|
explosionPower: number;
|
||||||
|
}
|
||||||
17
types/EventBus.d.ts
vendored
17
types/EventBus.d.ts
vendored
@@ -1,20 +1,3 @@
|
|||||||
/**
|
|
||||||
* Event data for C4 use started events.
|
|
||||||
*/
|
|
||||||
interface C4UseStartedEvent {
|
|
||||||
player: Internal.Player;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Event data for C4 activation events.
|
|
||||||
*/
|
|
||||||
interface C4ActivatedEvent {
|
|
||||||
level: Internal.Level;
|
|
||||||
player: Internal.Player;
|
|
||||||
explosionTime: number;
|
|
||||||
explosionPower: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mapping of event names to their corresponding event types.
|
* Mapping of event names to their corresponding event types.
|
||||||
*/
|
*/
|
||||||
|
|||||||
8
types/global.d.ts
vendored
Normal file
8
types/global.d.ts
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
export {}; // Mark the file as a module, do not remove unless there are other import/exports!
|
||||||
|
// Override the global type
|
||||||
|
declare global {
|
||||||
|
export type ProbeJS$$ResolvedGlobal = {
|
||||||
|
eventBus: EventBus;
|
||||||
|
toExplosionC4Map: { [key: string]: boolean };
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user