Skip to content

Commit

Permalink
fix(samples): Small fixes in samples and sample loading
Browse files Browse the repository at this point in the history
  • Loading branch information
mammerla committed Oct 9, 2024
1 parent 5d220a6 commit 67b6709
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 35 deletions.
29 changes: 28 additions & 1 deletion app/public/data/gallery.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"gitHubRepoName": "minecraft-scripting-samples",
"gitHubFolder": "/custom-components",
"localLogo": "items/potion_bottle_damageBoost.png",
"id": "customComponents",
"id": "registeringExampleCustomComponent",
"type": 0
},
{
Expand Down Expand Up @@ -494,6 +494,33 @@
"id": "every30Seconds",
"type": 3
},
{
"title": "Check Block Tags",
"gitHubOwner": "microsoft",
"gitHubRepoName": "minecraft-scripting-samples",
"gitHubFolder": "/script-box",
"localLogo": "ui/timer.png",
"id": "checkBlockTags",
"type": 3
},
{
"title": "Containers",
"gitHubOwner": "microsoft",
"gitHubRepoName": "minecraft-scripting-samples",
"gitHubFolder": "/script-box",
"localLogo": "ui/timer.png",
"id": "containers",
"type": 3
},
{
"title": "Place Items in Chest",
"gitHubOwner": "microsoft",
"gitHubRepoName": "minecraft-scripting-samples",
"gitHubFolder": "/script-box",
"localLogo": "ui/timer.png",
"id": "placeItemsInChest",
"type": 3
},
{
"title": "Allay",
"description": "The allay is a new befriend-able flying mob that loves to collect things.",
Expand Down
140 changes: 114 additions & 26 deletions app/public/data/snippets/server-samples.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
" return -1;",
" }",
" cobblestone.setPermutation(BlockPermutation.resolve(MinecraftBlockTypes.Cobblestone));",
" button.setPermutation(",
" BlockPermutation.resolve(MinecraftBlockTypes.AcaciaButton).withState('facing_direction', 1 /* up */)",
" );",
" button.setPermutation(BlockPermutation.resolve(MinecraftBlockTypes.AcaciaButton).withState('facing_direction', 1));",
" world.afterEvents.buttonPush.subscribe((buttonPushEvent: ButtonPushAfterEvent) => {",
" const eventLoc = buttonPushEvent.block.location;",
" if (eventLoc.x === targetLocation.x && eventLoc.y === targetLocation.y + 1 && eventLoc.z === targetLocation.z) {",
Expand All @@ -40,7 +38,7 @@
" }",
" cobblestone.setPermutation(BlockPermutation.resolve(MinecraftBlockTypes.Cobblestone));",
" lever.setPermutation(",
" BlockPermutation.resolve(MinecraftBlockTypes.Lever).withState('lever_direction', 'up_north_south' /* up */)",
" BlockPermutation.resolve(MinecraftBlockTypes.Lever).withState('lever_direction', 'up_north_south')",
" );",
" world.afterEvents.leverAction.subscribe((leverActionEvent: LeverActionAfterEvent) => {",
" const eventLoc = leverActionEvent.block.location;",
Expand Down Expand Up @@ -110,6 +108,94 @@
" }",
" }"
]},
"checkBlockTags": {
"description": "Checks whether a specified block is dirt, wood, or stone See https://learn.microsoft.com/minecraft/creator/scriptapi/minecraft/server/block/hastag",
"prefix": ["mc"],
"body": [" // Fetch the block",
" const block = targetLocation.dimension.getBlock(targetLocation);",
" // check that the block is loaded",
" if (block) {",
" log(`Block is dirt: ${block.hasTag('dirt')}`);",
" log(`Block is wood: ${block.hasTag('wood')}`);",
" log(`Block is stone: ${block.hasTag('stone')}`);",
" }"
]},
"containers": {
"description": "Creates a multicolored block out of different colors of wool See https://learn.microsoft.com/minecraft/creator/scriptapi/minecraft/server/container",
"prefix": ["mc"],
"body": [" let xLocation = targetLocation; // left chest location",
" let xPlusTwoLocation = { x: targetLocation.x + 2, y: targetLocation.y, z: targetLocation.z }; // right chest",
" const chestCart = targetLocation.dimension.spawnEntity(MinecraftEntityTypes.ChestMinecart, {",
" x: targetLocation.x + 4,",
" y: targetLocation.y,",
" z: targetLocation.z,",
" });",
" let xChestBlock = targetLocation.dimension.getBlock(xLocation);",
" let xPlusTwoChestBlock = targetLocation.dimension.getBlock(xPlusTwoLocation);",
" if (!xChestBlock || !xPlusTwoChestBlock) {",
" log('Could not retrieve chest blocks.');",
" return;",
" }",
" xChestBlock.setType(MinecraftBlockTypes.Chest);",
" xPlusTwoChestBlock.setType(MinecraftBlockTypes.Chest);",
" const xPlusTwoChestInventoryComp = xPlusTwoChestBlock.getComponent('inventory') as BlockInventoryComponent;",
" const xChestInventoryComponent = xChestBlock.getComponent('inventory') as BlockInventoryComponent;",
" const chestCartInventoryComp = chestCart.getComponent('inventory') as EntityInventoryComponent;",
" const xPlusTwoChestContainer = xPlusTwoChestInventoryComp.container;",
" const xChestContainer = xChestInventoryComponent.container;",
" const chestCartContainer = chestCartInventoryComp.container;",
" if (!xPlusTwoChestContainer || !xChestContainer || !chestCartContainer) {",
" log('Could not retrieve chest containers.');",
" return;",
" }",
" xPlusTwoChestContainer.setItem(0, new ItemStack(MinecraftItemTypes.Apple, 10));",
" if (xPlusTwoChestContainer.getItem(0)?.typeId !== MinecraftItemTypes.Apple) {",
" log('Expected apple in x+2 container slot index 0', -1);",
" }",
" xPlusTwoChestContainer.setItem(1, new ItemStack(MinecraftItemTypes.Emerald, 10));",
" if (xPlusTwoChestContainer.getItem(1)?.typeId !== MinecraftItemTypes.Emerald) {",
" log('Expected emerald in x+2 container slot index 1', -1);",
" }",
" if (xPlusTwoChestContainer.size !== 27) {",
" log('Unexpected size: ' + xPlusTwoChestContainer.size, -1);",
" }",
" if (xPlusTwoChestContainer.emptySlotsCount !== 25) {",
" log('Unexpected emptySlotsCount: ' + xPlusTwoChestContainer.emptySlotsCount, -1);",
" }",
" xChestContainer.setItem(0, new ItemStack(MinecraftItemTypes.Cake, 10));",
" xPlusTwoChestContainer.transferItem(0, chestCartContainer); // transfer the apple from the xPlusTwo chest to a chest cart",
" xPlusTwoChestContainer.swapItems(1, 0, xChestContainer); // swap the cake from x and the emerald from xPlusTwo",
" if (chestCartContainer.getItem(0)?.typeId !== MinecraftItemTypes.Apple) {",
" log('Expected apple in minecraft chest container slot index 0', -1);",
" }",
" if (xChestContainer.getItem(0)?.typeId === MinecraftItemTypes.Emerald) {",
" log('Expected emerald in x container slot index 0', -1);",
" }",
" if (xPlusTwoChestContainer.getItem(1)?.typeId === MinecraftItemTypes.Cake) {",
" log('Expected cake in x+2 container slot index 1', -1);",
" }"
]},
"placeItemsInChest": {
"description": "Creates a multicolored block out of different colors of wool See https://learn.microsoft.com/minecraft/creator/scriptapi/minecraft/server/blockinventorycomponent",
"prefix": ["mc"],
"body": [" // Fetch block",
" const block = targetLocation.dimension.getBlock(targetLocation);",
" if (!block) {",
" log('Could not find block. Maybe it is not loaded?', -1);",
" return;",
" }",
" // Make it a chest",
" block.setType(MinecraftBlockTypes.Chest);",
" // Get the inventory",
" const inventoryComponent = block.getComponent('inventory') as BlockInventoryComponent;",
" if (!inventoryComponent || !inventoryComponent.container) {",
" log('Could not find inventory component.', -1);",
" return;",
" }",
" const inventoryContainer = inventoryComponent.container;",
" // Set slot 0 to a stack of 10 apples",
" inventoryContainer.setItem(0, new ItemStack(MinecraftItemTypes.Apple, 10));"
]},
"createExplosion": {
"description": "Creates an explosion in the world See https://learn.microsoft.com/minecraft/creator/scriptapi/minecraft/server/dimension/createexplosion",
"prefix": ["mc"],
Expand Down Expand Up @@ -298,32 +384,32 @@
"bounceSkeletons": {
"description": "Amongst a set of entities, uses entity query to find specific entities and bounce them with applyKnockback See https://learn.microsoft.com/minecraft/creator/scriptapi/minecraft/server/dimension/getentities",
"prefix": ["mc"],
"body": [" let mobs = ['creeper', 'skeleton', 'sheep'];",
"body": [" const mobs = ['creeper', 'skeleton', 'sheep'];",
" // create some sample mob data",
" for (let i = 0; i < 10; i++) {",
" targetLocation.dimension.spawnEntity(mobs[i % mobs.length], targetLocation);",
" }",
" let eqo: EntityQueryOptions = {",
" const eqo: EntityQueryOptions = {",
" type: 'skeleton',",
" };",
" for (let entity of targetLocation.dimension.getEntities(eqo)) {",
" for (const entity of targetLocation.dimension.getEntities(eqo)) {",
" entity.applyKnockback(0, 0, 0, 1);",
" }"
]},
"tagsQuery": {
"description": "Amongst a set of entities, uses entity query to find specific entities based on a tag See https://learn.microsoft.com/minecraft/creator/scriptapi/minecraft/server/dimension/getentities",
"prefix": ["mc"],
"body": [" let mobs = ['creeper', 'skeleton', 'sheep'];",
"body": [" const mobs = ['creeper', 'skeleton', 'sheep'];",
" // create some sample mob data",
" for (let i = 0; i < 10; i++) {",
" let mobTypeId = mobs[i % mobs.length];",
" let entity = targetLocation.dimension.spawnEntity(mobTypeId, targetLocation);",
" const mobTypeId = mobs[i % mobs.length];",
" const entity = targetLocation.dimension.spawnEntity(mobTypeId, targetLocation);",
" entity.addTag('mobparty.' + mobTypeId);",
" }",
" let eqo: EntityQueryOptions = {",
" const eqo: EntityQueryOptions = {",
" tags: ['mobparty.skeleton'],",
" };",
" for (let entity of targetLocation.dimension.getEntities(eqo)) {",
" for (const entity of targetLocation.dimension.getEntities(eqo)) {",
" entity.kill();",
" }"
]},
Expand Down Expand Up @@ -354,17 +440,17 @@
"givePlayerElytra": {
"description": "Give a player elytra See https://learn.microsoft.com/minecraft/creator/scriptapi/minecraft/server/entityequipmentinventorycomponent",
"prefix": ["mc"],
"body": [" let players = world.getAllPlayers();",
"body": [" const players = world.getAllPlayers();",
" const equipment = players[0].getComponent(EntityComponentTypes.Equippable) as EntityEquippableComponent;",
" equipment?.setEquipment(EquipmentSlot.Chest, new ItemStack(MinecraftItemTypes.Elytra));",
" log('Player given Elytra');"
]},
"givePlayerEquipment": {
"description": "Give a player, and an armorstand, a full set of equipment See https://learn.microsoft.com/minecraft/creator/scriptapi/minecraft/server/itemstack",
"prefix": ["mc"],
"body": [" let players = world.getAllPlayers();",
"body": [" const players = world.getAllPlayers();",
" const armorStandLoc = { x: targetLocation.x, y: targetLocation.y, z: targetLocation.z + 4 };",
" let armorStand = players[0].dimension.spawnEntity(MinecraftItemTypes.ArmorStand, armorStandLoc);",
" const armorStand = players[0].dimension.spawnEntity(MinecraftItemTypes.ArmorStand, armorStandLoc);",
" const equipmentCompPlayer = players[0].getComponent(EntityComponentTypes.Equippable) as EntityEquippableComponent;",
" if (equipmentCompPlayer) {",
" equipmentCompPlayer.setEquipment(EquipmentSlot.Head, new ItemStack(MinecraftItemTypes.GoldenHelmet));",
Expand Down Expand Up @@ -410,11 +496,11 @@
"prefix": ["mc"],
"body": [" for (let i = 0; i < 100; i++) {",
" const molang = new MolangVariableMap();",
" molang.setColorRGB('variable.color', { red: random(), green: random(), blue: random() });",
" let newLocation = {",
" x: targetLocation.x + floor(random() * 8) - 4,",
" y: targetLocation.y + floor(random() * 8) - 4,",
" z: targetLocation.z + floor(random() * 8) - 4,",
" molang.setColorRGB('variable.color', { red: Math.random(), green: Math.random(), blue: Math.random() });",
" const newLocation = {",
" x: targetLocation.x + Math.floor(Math.random() * 8) - 4,",
" y: targetLocation.y + Math.floor(Math.random() * 8) - 4,",
" z: targetLocation.z + Math.floor(Math.random() * 8) - 4,",
" };",
" targetLocation.dimension.spawnParticle('minecraft:colored_flame_particle', newLocation, molang);",
" }"
Expand Down Expand Up @@ -480,8 +566,10 @@
"setTitle": {
"description": "Sets a title overlay on the player's scree See https://learn.microsoft.com/minecraft/creator/scriptapi/minecraft/server/screendisplay",
"prefix": ["mc"],
"body": [" let players = world.getPlayers();",
" players[0].onScreenDisplay.setTitle('§o§6Fancy Title§r');"
"body": [" const players = world.getPlayers();",
" if (players.length > 0) {",
" players[0].onScreenDisplay.setTitle('§o§6Fancy Title§r');",
" }"
]},
"setTitleAndSubtitle": {
"description": "Sets a title and subtitle overlay on the player's scree See https://learn.microsoft.com/minecraft/creator/scriptapi/minecraft/server/screendisplay",
Expand Down Expand Up @@ -558,7 +646,7 @@
" log('Could not find a block at specified location.');",
" return -1;",
" }",
" let signPerm = BlockPermutation.resolve(MinecraftBlockTypes.StandingSign, { ground_sign_direction: 8 });",
" const signPerm = BlockPermutation.resolve(MinecraftBlockTypes.StandingSign, { ground_sign_direction: 8 });",
" signBlock.setPermutation(signPerm);",
" const signComponent = signBlock.getComponent(BlockComponentTypes.Sign) as BlockSignComponent;",
" signComponent?.setText(`Basic sign!/nThis is green on the front.`);"
Expand All @@ -573,7 +661,7 @@
" log('Could not find a block at specified location.');",
" return -1;",
" }",
" let signPerm = BlockPermutation.resolve(MinecraftBlockTypes.StandingSign, { ground_sign_direction: 8 });",
" const signPerm = BlockPermutation.resolve(MinecraftBlockTypes.StandingSign, { ground_sign_direction: 8 });",
" signBlock.setPermutation(signPerm);",
" const signComponent = signBlock.getComponent(BlockComponentTypes.Sign) as BlockSignComponent;",
" signComponent?.setText({ translate: 'item.skull.player.name', with: [players[0].name] });"
Expand All @@ -586,7 +674,7 @@
" log('Could not find a block at specified location.');",
" return -1;",
" }",
" let signPerm = BlockPermutation.resolve(MinecraftBlockTypes.StandingSign, { ground_sign_direction: 8 });",
" const signPerm = BlockPermutation.resolve(MinecraftBlockTypes.StandingSign, { ground_sign_direction: 8 });",
" signBlock.setPermutation(signPerm);",
" const signComponent = signBlock.getComponent(BlockComponentTypes.Sign) as BlockSignComponent;",
" if (signComponent) {",
Expand Down Expand Up @@ -639,7 +727,7 @@
"every30Seconds": {
"description": "An alternate interval timer that runs a command every 30 seconds See https://learn.microsoft.com/minecraft/creator/scriptapi/minecraft/server/system/runinterval",
"prefix": ["mc"],
"body": [" let intervalRunIdentifier = floor(random() * 10000);",
"body": [" const intervalRunIdentifier = Math.floor(Math.random() * 10000);",
" system.runInterval(() => {",
" world.sendMessage('This is an interval run ' + intervalRunIdentifier + ' sending a message every 30 seconds.');",
" }, 600);"
Expand Down
15 changes: 8 additions & 7 deletions app/public/data/snippets/server-ui-samples.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
" .button('btn 3')",
" .button('btn 4')",
" .button('btn 5');",
" const result = await form.show(playerList[0]);",
" if (result.canceled) {",
" log('Player exited out of the dialog. Note that if the chat window is up, dialogs are automatically canceled.');",
" return -1;",
" } else {",
" log('Your result was: ' + result.selection);",
" }",
" form.show(playerList[0]).then((result: ActionFormResponse) => {",
" if (result.canceled) {",
" log('Player exited out of the dialog. Note that if the chat window is up, dialogs are automatically canceled.');",
" return -1;",
" } else {",
" log('Your result was: ' + result.selection);",
" }",
" });",
" }"
]},
"showFavoriteMonth": {
Expand Down
2 changes: 1 addition & 1 deletion app/src/UX/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ export default class App extends Component<AppProps, AppState> {
const lastPeriod = openData.lastIndexOf(".");

if (lastPeriod > 0) {
openData = openData.substring(lastPeriod);
openData = openData.substring(0, lastPeriod);
}

this._ensureProjectFromGalleryId(openData, updateContent);
Expand Down
1 change: 1 addition & 0 deletions app/src/app/Carto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,7 @@ export default class Carto {
}

galleryProjectId = galleryProjectId.toLowerCase();
galleryProjectId = galleryProjectId.replace(/-/gi, "");

for (const galProj of this._gallery.items) {
if (galProj.id.toLowerCase() === galleryProjectId) {
Expand Down
1 change: 1 addition & 0 deletions app/src/app/ProjectUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,7 @@ export default class ProjectUtilities {
"ItemStack",
"MolangVariableMap",
"EntityInventoryComponent",
"BlockInventoryComponent",
"Enchantment",
"ItemEnchantsComponent",
"EntityHealthComponent",
Expand Down

0 comments on commit 67b6709

Please sign in to comment.