Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
JaylyDev committed Jun 18, 2023
1 parent 43bf610 commit e49738b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
28 changes: 16 additions & 12 deletions scripts/cps-counter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@
// Project: https://github.com/JaylyDev/ScriptAPI
import { Player, world } from "@minecraft/server";

declare module "@minecraft/server" {
interface ClickInfo { timestamp: number }
interface Player {
clicks?: ClickInfo[];
}
interface ClickInfo {
readonly timestamp: number
};

// Using map because typescript doesn't support prototype property declaration properly
const clicks = new Map<Player, ClickInfo[]>();

world.afterEvents.entityHitBlock.subscribe(function ({ damagingEntity }) {
if (damagingEntity instanceof Player) {
damagingEntity.clicks ||= [];
damagingEntity.clicks.push({ timestamp: Date.now() });
const clickInfo = { timestamp: Date.now() };
const playerClicks = clicks.get(damagingEntity) || [];
playerClicks.push(clickInfo);
clicks.set(damagingEntity, playerClicks);
}
});

world.afterEvents.entityHitEntity.subscribe(function ({ damagingEntity }) {
if (damagingEntity instanceof Player) {
damagingEntity.clicks ||= [];
damagingEntity.clicks.push({ timestamp: Date.now() });
const clickInfo = { timestamp: Date.now() };
const playerClicks = clicks.get(damagingEntity) || [];
playerClicks.push(clickInfo);
clicks.set(damagingEntity, playerClicks);
}
});

Expand All @@ -31,9 +35,9 @@ world.afterEvents.entityHitEntity.subscribe(function ({ damagingEntity }) {
*/
export function getPlayerCPS(player: Player) {
const currentTime = Date.now();
player.clicks ||= [];
const recentClicks = player.clicks.filter(({ timestamp }) => currentTime - 1000 < timestamp);
player.clicks = recentClicks;
const playerClicks = clicks.get(player) || [];
const recentClicks = playerClicks.filter(({ timestamp }) => currentTime - 1000 < timestamp);
clicks.set(player, recentClicks);

return recentClicks.length;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Server from "@minecraft/server";
import * as Editor from "@minecraft/server-editor";
import { MinecraftEffectTypes } from "@minecraft/vanilla-data";
/**
* @param {import("@minecraft/server-editor").IPlayerUISession} uiSession
*/
Expand All @@ -13,7 +14,7 @@ export const Start = (uiSession) => {
uiSession.actionManager.createAction(
{
actionType: Editor.ActionTypes.NoArgsAction,
onExecute: () => uiSession.extensionContext.player.addEffect( Server.MinecraftEffectTypes.nightVision, 20000000, { amplifier: 1, showParticles: false } ),
onExecute: () => uiSession.extensionContext.player.addEffect( MinecraftEffectTypes.NightVision, 20000000, { amplifier: 1, showParticles: false } ),
},
),
);
Expand All @@ -23,7 +24,7 @@ export const Start = (uiSession) => {
uiSession.actionManager.createAction(
{
actionType: Editor.ActionTypes.NoArgsAction,
onExecute: () => uiSession.extensionContext.player.removeEffect( Server.MinecraftEffectTypes.nightVision ),
onExecute: () => uiSession.extensionContext.player.removeEffect( MinecraftEffectTypes.NightVision ),
},
),
);
Expand Down
6 changes: 3 additions & 3 deletions scripts/editor-fullbright/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Author: Jayly <https://github.com/JaylyDev>
// Project: https://github.com/JaylyDev/ScriptAPI
import { ActionTypes, EditorInputContext, IMenu, InputModifier, IPlayerUISession, KeyboardKey } from "@minecraft/server-editor";
import { MinecraftEffectTypes } from "@minecraft/server";
import { MinecraftEffectTypes } from "@minecraft/vanilla-data";

/**
* Class to enable toggles for full bright in menu in editor mode
Expand All @@ -14,13 +14,13 @@ export class FullbrightToggle {
const enableAction = uiSession.actionManager.createAction({
actionType: ActionTypes.NoArgsAction,
onExecute: () => {
player.addEffect(MinecraftEffectTypes.nightVision, 20000000, { amplifier: 1, showParticles: false });
player.addEffect(MinecraftEffectTypes.NightVision, 20000000, { amplifier: 1, showParticles: false });
},
});
const disableAction = uiSession.actionManager.createAction({
actionType: ActionTypes.NoArgsAction,
onExecute: () => {
player.runCommand("effect @s " + MinecraftEffectTypes.nightVision.getName() + " 0");
player.removeEffect(MinecraftEffectTypes.NightVision);
},
});
// Add actions to menu
Expand Down
7 changes: 3 additions & 4 deletions scripts/get-effects/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
// Project: https://github.com/JaylyDev/ScriptAPI

import {
world,
MinecraftEffectTypes,
/* Typings only */
Entity,
Player,
Effect,
EffectType
EffectType,
EffectTypes
} from "@minecraft/server";

/**
Expand All @@ -22,7 +21,7 @@ import {
*/
export function getEffects(entity) {
const effectList = [];
for (const eff of Object.values(MinecraftEffectTypes)) {
for (const eff of EffectTypes.getAll()) {
const effect = entity.getEffect(eff);
if (!effect) continue;
effectList.push({
Expand Down
2 changes: 1 addition & 1 deletion scripts/get-scores/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function getScores (target) {
let targetScoreboard = {};

if (!(target.scoreboardIdentity instanceof ScoreboardIdentity)) return targetScoreboard;
for (const objective of objectives) targetScoreboard[objective.id] = objective.getScores().find((score) => score.participant.type !== ScoreboardIdentityType.fakePlayer ? score.participant.getEntity() === target : false)?.score;
for (const objective of objectives) targetScoreboard[objective.id] = objective.getScores().find((score) => score.participant.type !== ScoreboardIdentityType.FakePlayer ? score.participant.getEntity() === target : false)?.score;
return targetScoreboard;
};

Expand Down

0 comments on commit e49738b

Please sign in to comment.