Skip to content

Commit

Permalink
Merge branch 'beta'
Browse files Browse the repository at this point in the history
准备更新到新的游戏版本
  • Loading branch information
Silvigarabis committed Sep 23, 2023
2 parents b72aebc + cb0a4d2 commit a77b35e
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 19 deletions.
4 changes: 2 additions & 2 deletions install_deps.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
npm i --no-bin-links @minecraft/server@1.4.0-beta.1.20.10-stable
npm i --no-bin-links @minecraft/[email protected].10-stable
npm i --no-bin-links @minecraft/server@1.6.0-beta.1.20.30-preview.24
npm i --no-bin-links @minecraft/[email protected].30-preview.24
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"dependencies": {
"@minecraft/server": "1.4.0-beta.1.20.10-stable",
"@minecraft/server-gametest": "1.0.0-beta.1.20.10-stable"
"@minecraft/server": "^1.6.0-beta.1.20.30-preview.24",
"@minecraft/server-gametest": "^1.0.0-beta.1.20.30-preview.24"
},
"devDependencies": {
"typescript": "^4.9.3"
Expand Down
2 changes: 1 addition & 1 deletion src/yoni/event/GetEventOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export interface EventOptionDefinitions extends MinecraftEventsOptions, InnerEve
}

interface MinecraftEventsOptions {
"Minecraft.EntityRemovedAfterEvent": [Minecraft.EntityRemovedAfterEvent, Minecraft.EntityEventOptions]
"Minecraft.EntityRemoveAfterEvent": [Minecraft.EntityRemoveAfterEvent, Minecraft.EntityEventOptions]
"Minecraft.EntityHurtAfterEvent": [Minecraft.EntityHurtAfterEvent, Minecraft.EntityEventOptions]
"Minecraft.EntityHitEntityAfterEvent": [Minecraft.EntityHitEntityAfterEvent, Minecraft.EntityEventOptions]
"Minecraft.EntityHitBlockAfterEvent": [Minecraft.EntityHitBlockAfterEvent, Minecraft.EntityEventOptions]
Expand Down
15 changes: 11 additions & 4 deletions src/yoni/event/adapting/eventOptions/EntityEventOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,25 @@ registry.extraOptionResolver = (event, options) => {
})();

(function (){
let registry = EventRegistry.getRegistry(Minecraft.EntityRemovedAfterEvent);
let registry = EventRegistry.getRegistry(Minecraft.EntityRemoveAfterEvent);

registry.extraOption = true;
registry.extraOptionResolver = (event, options) => {
if (!options.entities) return true;
if (!options || !(options.entityTypes || options.entities))
return true;

let id = event.removedEntity;

let id = event.removedEntityId;
let typeId = event.typeId;

if (options.entities)
for (const entity of options.entities){
if (entity.id === id) return true;
}

if (options.entityTypes)
if (options.entityTypes.includes(typeId))
return true;

return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/yoni/event/lib/eventRegistration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const NotAEventClassNames = [
const DefinedEventClassMappings = new Map<Function, Function>([
[Minecraft.ItemDefinitionTriggeredAfterEvent, Minecraft.ItemDefinitionAfterEventSignal],
[Minecraft.ItemDefinitionTriggeredBeforeEvent, Minecraft.ItemDefinitionBeforeEventSignal],
[Minecraft.MessageReceiveAfterEvent, Minecraft.IServerMessageAfterEventSignal]
[Minecraft.MessageReceiveAfterEvent, Minecraft.ServerMessageAfterEventSignal]
]);

const EventClasses: [string, Function, Function][] = [];
Expand Down
214 changes: 214 additions & 0 deletions src/yoni/item/ItemUpdater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
import { Minecraft } from "../basis.js";

/**
* 允许开发者调用一连串方法来调整出需要的物品对象。
*/
export class ItemUpdater {
/**
* 所有修改都会直接应用到此物品对象当中。
*/
itemStack: Minecraft.ItemStack;
constructor(itemStack: Minecraft.ItemStack){
this.itemStack = itemStack;
}
setAmount(amount: number): this {
this.itemStack.amount = amount;
return this;
}
setLore(loreList: string[]): this {
this.itemStack.setLore(loreList);
return this;
}
setLockMode(mode: Minecraft.ItemLockMode): this {
this.itemStack.lockMode = mode;
return this;
}
setKeepOnDeath(keepOnDeath: boolean): this {
this.itemStack.keepOnDeath = keepOnDeath;
return this;
}
setNameTag(nameTag?: string): this {
this.itemStack.nameTag = nameTag;
return this;
}
removeNameTag(): this {
this.itemStack.nameTag = undefined;
return this;
}
setCanPlaceOn(blockIdentifiers?: string[]): this {
this.itemStack.setCanPlaceOn(blockIdentifiers);
return this;
}
addCanPlaceOn(blockIdentifier: string): this {
const old = this.itemStack.getCanPlaceOn();
old.push(blockIdentifier);
this.itemStack.setCanPlaceOn(old);
return this;
}
removeCanPlaceOn(blockIdentifier?: string): this {
if (blockIdentifier){
const old = this.itemStack.getCanPlaceOn();
deleteFirst(old, blockIdentifier);
this.itemStack.setCanPlaceOn(old);
} else {
this.itemStack.setCanPlaceOn();
}
return this;
}
setCanDestroy(blockIdentifiers?: string[]): this {
this.itemStack.setCanDestroy(blockIdentifiers);
return this;
}
addCanDestroy(blockIdentifier: string): this {
const old = this.itemStack.getCanDestroy();
old.push(blockIdentifier);
this.itemStack.setCanDestroy(old);
return this;
}
removeCanDestroy(blockIdentifier?: string): this {
if (blockIdentifier){
const old = this.itemStack.getCanDestroy();
deleteFirst(old, blockIdentifier);
this.itemStack.setCanDestroy(old);
} else {
this.itemStack.setCanDestroy();
}
return this;
}
/**
* 为物品增添新的附魔。
* @param newEnchantment 新添加的附魔。
* @param override 如果为真,则替换现有附魔(如果附魔存在);否则,为现有附魔条目提升附魔等级。
* @returns 返回 this
*/
addEnchant(newEnchantment: Enchantment, override?: boolean): this
/**
* 为物品增添新的附魔。
* @param newEnchantment 新添加的附魔类型。
* @param level 新添加的附魔的等级,默认为 1 级。
* @param override 如果为真,则替换现有附魔(如果附魔存在);否则,为现有附魔条目提升附魔等级。
* @returns 返回 this
*/
addEnchant(newEnchantment: string | Minecraft.EnchantmentType, level: number, override?: boolean): this
addEnchant(newEnchantment: unknown, level: unknown, override: unknown = true): this {
let ench: Enchantment;
if (level === true || level === false || level === undefined){
override = (level as boolean) ?? true;
ench = newEnchantment as Enchantment;
} else {
ench = {
type: newEnchantment as (string | Minecraft.EnchantmentType),
level: (level as number) ?? 1
};
}

this.addEnchants([ench], override as boolean);
return this;
}
addEnchants(newEnchantments: Enchantment[], override: boolean = true): this {
const component = this.itemStack.getComponent("minecraft:enchantments") as Minecraft.ItemEnchantsComponent;
const { enchantments: enchantmentList } = component;

if (!override)
newEnchantments = newEnchantments.map(ench => {
let { type, level } = ench;
if (enchantmentList.hasEnchantment(type))
level += (enchantmentList.getEnchantment(type) as Minecraft.Enchantment).level;

return { type, level };
});

for (const ench of newEnchantments){
if (enchantmentList.hasEnchantment(ench.type))
enchantmentList.removeEnchantment(ench.type);

if (ench.level > 0)
enchantmentList.addEnchantment(ItemUpdater.getEnchantment(ench.type, ench.level));
}

component.enchantments = enchantmentList;

return this;
}
/**
* 从物品上移除附魔。
* @param enchantment 要移除的附魔类型,将物品上拥有的附魔的等级与指定附魔条目的等级相减。
* @param completelyMatches 如果为真,则只有在附魔完全相同时才会移除。默认为真。
* @returns 返回 this
*/
removeEnchant(enchantment: Enchantment, completelyMatches?: boolean): this
/**
* 从物品上移除附魔。
* @param enchantmentType 要移除的附魔类型。
* @param level 要移除的附魔的等级,不设置 或 设置为 0 则以已有的附魔等级为此参数的值。
* @param completelyMatches 如果为真,则只有在附魔完全相同时才会移除。默认为真。
* @returns 返回 this
*/
removeEnchant(enchantmentType: string | Minecraft.EnchantmentType, level?: number, completelyMatches?: boolean): this
removeEnchant(enchantment: Enchantment | string | Minecraft.EnchantmentType, level: number | boolean = 0, completelyMatches: unknown = true): this {
let enchantmentType: Minecraft.EnchantmentType | string;
if (level === true || level === false || level === 0){
if (enchantment instanceof Minecraft.EnchantmentType || typeof enchantment === "string"){
enchantmentType = enchantment;
} else {
enchantmentType = (enchantment as Enchantment).type;
completelyMatches = level === 0 ? true : (level as boolean);
level = (enchantment as Enchantment).level;
}
} else {
enchantmentType = enchantment as (string | Minecraft.EnchantmentType);
}

const component = this.itemStack.getComponent("minecraft:enchantments") as Minecraft.ItemEnchantsComponent;
const { enchantments: enchantmentList } = component;

if (enchantmentList.hasEnchantment(enchantmentType)){
const oldEnchantment = enchantmentList.getEnchantment(enchantmentType) as Minecraft.Enchantment;

if (!completelyMatches || (completelyMatches && (level === 0 || oldEnchantment.level === level))){
enchantmentList.removeEnchantment(enchantmentType);

if (level !== 0){
let { type, level: level0 } = oldEnchantment;
level0 -= (level as number);
if (level0 > 0){
enchantmentList.addEnchantment(ItemUpdater.getEnchantment(enchantmentType, level0));
}
}

component.enchantments = enchantmentList;
}
}
return this;
}
static cloneEnchantmentList(list: Minecraft.EnchantmentList): Minecraft.EnchantmentList {
const newList = new Minecraft.EnchantmentList(list.slot);
for (const ench of list)
newList.addEnchantment(ench);
return newList;
}

static getEnchantmentType(ench: string | Minecraft.EnchantmentType): Minecraft.EnchantmentType {
return typeof ench === "string" ? Minecraft.EnchantmentTypes.get(ench) as Minecraft.EnchantmentType: ench;
}

static getEnchantment(enchantmentType: string | Minecraft.EnchantmentType, level: number = 1): Minecraft.Enchantment {
let ench = new Minecraft.Enchantment(enchantmentType);
ench.level = level;
return ench;
}

}

interface Enchantment {
type: Minecraft.EnchantmentType | string
level: number
}

function deleteFirst<T>(array: Array<T>, element: T): Array<T> {
const index = array.indexOf(element);
if (index !== -1)
array.splice(index, 1);

return array;
}
4 changes: 1 addition & 3 deletions src/yoni/remix/entity/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ import { Entity } from "./Entity.js";
import { EntityUtils } from "../../EntityUtils.js";
import { EntityWraps } from "./EntityWraps.js";

const { MinecraftEntityTypes } = Minecraft;

class Player extends Entity {

get vanillaPlayer(): Minecraft.Player {
return this.vanillaEntity as unknown as Minecraft.Player;
}

get entityType(){
return MinecraftEntityTypes.player;
return Minecraft.EntityTypes.get("player");
}

get [Symbol.toStringTag](){
Expand Down
15 changes: 9 additions & 6 deletions src/yoni/scoreboard/Scoreboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export enum ObjectiveSortOrder {
*/
export interface DisplayOptions {
/**
* 显示的记分项。可能不存在。
* 显示的记分项。
*/
objective: Objective | null;
objective: Objective;
/**
* 记分项的项目显示在此位置上时,项目排序的方式。
*/
Expand Down Expand Up @@ -153,7 +153,7 @@ export class Scoreboard {
} else if (Scoreboard.#objectives.has(name)){
Scoreboard.#objectives.delete(name);
}
let vanillaObjective: Minecraft.ScoreboardObjective | null = null;
let vanillaObjective: Minecraft.ScoreboardObjective | undefined | null = null;
try {
vanillaObjective = VanillaScoreboard.getObjective(name);
} catch {
Expand Down Expand Up @@ -233,11 +233,14 @@ export class Scoreboard {
* @param {DisplaySlot|Minecraft.DisplaySlotId} slot - 显示位。
* @returns {DisplayOptions} - 显示位上显示的内容。
*/
static getDisplayAtSlot(slot: DisplaySlot | Minecraft.DisplaySlotId): DisplayOptions {
static getDisplayAtSlot(slot: DisplaySlot | Minecraft.DisplaySlotId): DisplayOptions | undefined {
const vanillaResult = VanillaScoreboard.getObjectiveAtDisplaySlot(slot as Minecraft.DisplaySlotId);
const objective = Scoreboard.tryGetObjective(vanillaResult.objective?.id) || null;
if (!vanillaResult)
return undefined;

const objective = Scoreboard.getObjective(vanillaResult.objective.id);
const result: DisplayOptions = { objective };
if (vanillaResult.sortOrder != null){
if (vanillaResult?.sortOrder != null){
result.sortOrder = vanillaResult.sortOrder as unknown as ObjectiveSortOrder;
}
return result;
Expand Down

0 comments on commit a77b35e

Please sign in to comment.