From 254c96c2c0920ac3e3d19b4201191c6a5c35e21c Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Sat, 13 May 2023 00:04:15 -0400 Subject: [PATCH] Add setting level to entity card --- src/handbook/src/ui/widgets/EntityCard.tsx | 60 ++++++++++++++++------ src/handbook/src/utils.ts | 11 ++++ 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/src/handbook/src/ui/widgets/EntityCard.tsx b/src/handbook/src/ui/widgets/EntityCard.tsx index 0ffea5d53..eedf7320d 100644 --- a/src/handbook/src/ui/widgets/EntityCard.tsx +++ b/src/handbook/src/ui/widgets/EntityCard.tsx @@ -1,7 +1,7 @@ import React from "react"; import type { Entity as EntityType, EntityInfo } from "@backend/types"; -import { copyToClipboard, entityIcon } from "@app/utils"; +import { copyToClipboard, entityIcon, notNaN } from "@app/utils"; import "@css/widgets/ObjectCard.scss"; import { connected, spawnEntity } from "@backend/server"; @@ -28,11 +28,16 @@ interface IProps { interface IState { icon: boolean; count: number | string; + level: number | string; + + showingCount: boolean; } const defaultState = { icon: true, - count: 1 + count: 1, + level: 1, + showingCount: true }; class EntityCard extends React.Component { @@ -49,10 +54,20 @@ class EntityCard extends React.Component { * @private */ private updateCount(event: React.ChangeEvent) { - const value = event.target.value; - if (isNaN(parseInt(value)) && value.length > 1) return; + let value = event.target.value; + // Remove non-numeric characters. + value = value.replace(/[^0-9]/g, ""); - this.setState({ count: value }); + let numeric = parseInt(value); + if (isNaN(numeric) && value.length > 1) return; + + // Check if the value should be a level. + if (!this.state.showingCount && numeric > 200) + numeric = 200; + + const updated: any = this.state.showingCount ? + { count: numeric } : { level: numeric }; + this.setState(updated); } /** @@ -63,18 +78,24 @@ class EntityCard extends React.Component { * @private */ private addCount(positive: boolean, multiple: boolean) { - let { count } = this.state; - if (count === "") count = 1; - if (typeof count == "string") count = parseInt(count); - if (count < 1) count = 1; + let value = this.state.showingCount ? + this.state.count : this.state.level; + if (value === "") value = 1; + if (typeof value == "string") value = parseInt(value); + if (value < 1) value = 1; let increment = 1; if (!positive) increment = -1; if (multiple) increment *= 10; - count = Math.max(1, count + increment); + value = Math.max(1, value + increment); + // Check if the value should be a level. + if (!this.state.showingCount && value > 200) + value = 200; - this.setState({ count }); + const updated: any = this.state.showingCount ? + { count: value } : { level: value }; + this.setState(updated); } /** @@ -84,11 +105,12 @@ class EntityCard extends React.Component { private async summonAtPlayer(): Promise { const entity = this.props.entity?.id ?? 21010101; const amount = typeof this.state.count == "string" ? parseInt(this.state.count) : this.state.count; + const level = typeof this.state.level == "string" ? parseInt(this.state.level) : this.state.level; if (connected) { - await spawnEntity(entity, amount, 1); + await spawnEntity(entity, amount, level); } else { - await copyToClipboard(spawn.monster(entity, amount, 1)); + await copyToClipboard(spawn.monster(entity, amount, level)); } } @@ -138,7 +160,8 @@ class EntityCard extends React.Component { { @@ -159,7 +182,14 @@ class EntityCard extends React.Component { - diff --git a/src/handbook/src/utils.ts b/src/handbook/src/utils.ts index 513ee9066..3ec901c41 100644 --- a/src/handbook/src/utils.ts +++ b/src/handbook/src/utils.ts @@ -154,3 +154,14 @@ export async function copyToClipboard(text: string): Promise { export function openUrl(url: string): void { window.open(url, "_blank"); } + +/** + * Checks if a value is NaN. + * Returns an empty string if it is. + * + * @param value The value to check. + */ +export function notNaN(value: number | string): string { + const number = parseInt(value.toString()); + return isNaN(number) ? "" : number.toString(); +}