-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
44 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,52 @@ | ||
import { | ||
world, | ||
system, | ||
Player, | ||
ItemStack, | ||
MinecraftItemTypes, | ||
MinecraftEntityTypes, | ||
DynamicPropertiesDefinition | ||
DynamicPropertiesDefinition, | ||
PlayerBreakBlockAfterEventSignal | ||
} from '@minecraft/server' | ||
import { MinecraftItemTypes } from '@minecraft/vanilla-data' | ||
import { getRandomProbability } from '@mcbe-mods/utils' | ||
import type { ItemType } from '@minecraft/server' | ||
|
||
type MinecraftItemTypesKeys = keyof typeof MinecraftItemTypes | ||
/* ---------- Versions Adaptation ---------- */ | ||
|
||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
const afterEvents = ((world.afterEvents as any).playerBreakBlock || | ||
(world.afterEvents as any).blockBreak) as PlayerBreakBlockAfterEventSignal | ||
/* eslint-enable @typescript-eslint/no-explicit-any */ | ||
|
||
/* ---------- Versions Adaptation ---------- */ | ||
|
||
const itemTypes = Object.values(MinecraftItemTypes) | ||
const PROB = 'prob' | ||
|
||
world.afterEvents.worldInitialize.subscribe((e) => { | ||
const def = new DynamicPropertiesDefinition() | ||
def.defineNumber(PROB, 1) | ||
e.propertyRegistry.registerEntityTypeDynamicProperties(def, MinecraftEntityTypes.player) | ||
e.propertyRegistry.registerWorldDynamicProperties(def) | ||
}) | ||
|
||
system.afterEvents.scriptEventReceive.subscribe((e) => { | ||
const { id, message } = e | ||
if (id === 'serendipity:prob') { | ||
const prob = +message | ||
if (isNaN(prob)) return | ||
world.getAllPlayers().forEach((player) => { | ||
player.setDynamicProperty(PROB, prob) | ||
}) | ||
const prob = +message | ||
if (isNaN(prob)) return | ||
|
||
const player = e.sourceEntity as Player | ||
if (id === 'serendipity:prob' && player?.isOp()) { | ||
world.setDynamicProperty(PROB, prob) | ||
} | ||
}) | ||
|
||
world.afterEvents.blockBreak.subscribe(async (e) => { | ||
afterEvents.subscribe(async (e) => { | ||
const { dimension, block, player } = e | ||
const prob = +(player.getDynamicProperty(PROB) || 1) | ||
if (isNaN(prob)) return | ||
const prob = (world.getDynamicProperty(PROB) as number) || 1 | ||
const is = getRandomProbability(prob) | ||
if (!is) return | ||
|
||
player.playSound('random.orb', { pitch: 0.5, volume: 0.5 }) | ||
const itemTypes = Object.keys(MinecraftItemTypes) as MinecraftItemTypesKeys[] | ||
const randomIndex = Math.floor(Math.random() * itemTypes.length) | ||
const randomKey = itemTypes[randomIndex] | ||
const randomItemName = itemTypes[randomIndex] | ||
|
||
const item = MinecraftItemTypes[randomKey] as ItemType | ||
dimension.spawnItem(new ItemStack(item), block.location) | ||
dimension.spawnItem(new ItemStack(randomItemName), block.location) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
pack.name=Serendipity v1.0.0 | ||
pack.name=Serendipity v1.0.1 | ||
pack.description=When you break a block, there is a 1/100 chance to randomly drop an item from the game. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
pack.name=意外发现 v1.0.0 | ||
pack.name=意外发现 v1.0.1 | ||
pack.description=打碎方块时,有 1/100 的几率随机掉落游戏中的物品。 |