easyPlace feature-complete
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { system, world } from '@minecraft/server';
|
import { GameMode, system, world } from '@minecraft/server';
|
||||||
import { Raycaster } from '../classes/Raycaster';
|
import { Raycaster } from '../classes/Raycaster';
|
||||||
|
import { fetchMatchingItemSlot } from '../utils';
|
||||||
|
|
||||||
class BlockInfo {
|
class BlockInfo {
|
||||||
static shownToLastTick = new Set();
|
static shownToLastTick = new Set();
|
||||||
@@ -20,23 +21,35 @@ class BlockInfo {
|
|||||||
}
|
}
|
||||||
if (!block)
|
if (!block)
|
||||||
return;
|
return;
|
||||||
player.onScreenDisplay.setActionBar({ text: this.getFormattedBlockInfo(block.permutation) });
|
player.onScreenDisplay.setActionBar({ text: this.getFormattedBlockInfo(player, block.permutation) });
|
||||||
this.shownToLastTick.add(player.id);
|
this.shownToLastTick.add(player.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
static getFormattedBlockInfo(block) {
|
static getFormattedBlockInfo(player, block) {
|
||||||
const header = 'Structure:\n'
|
return 'Structure:' + this.getSupplyMessage(player, block) + '\n' + this.getBlockMessage(block);
|
||||||
|
}
|
||||||
|
|
||||||
|
static getBlockMessage(block) {
|
||||||
if (!block)
|
if (!block)
|
||||||
return header + '§7Unknown';
|
return '§7Unknown';
|
||||||
const states = block.getAllStates();
|
const states = block.getAllStates();
|
||||||
if (Object.keys(states).length === 0)
|
if (Object.keys(states).length === 0)
|
||||||
return header + `§a${block.type.id}`;
|
return `§a${block.type.id}`;
|
||||||
return header + `§a${block.type.id}\n§7${this.getFormattedStates(states)}`;
|
else
|
||||||
|
return `§a${block.type.id}\n§7${this.getFormattedStates(states)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
static getFormattedStates(states) {
|
static getFormattedStates(states) {
|
||||||
return Object.entries(states).map(([key, value]) => `§7${key}: §3${value}`).join('\n');
|
return Object.entries(states).map(([key, value]) => `§7${key}: §3${value}`).join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static getSupplyMessage(player, block) {
|
||||||
|
const itemStack = fetchMatchingItemSlot(player, block.getItemStack()?.typeId);
|
||||||
|
const isInSurvival = player.getGameMode() === GameMode.survival;
|
||||||
|
if (!itemStack && isInSurvival)
|
||||||
|
return ' §c[No Supply]';
|
||||||
|
return '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
system.runInterval(() => BlockInfo.onTick());
|
system.runInterval(() => BlockInfo.onTick());
|
||||||
+10
-16
@@ -4,6 +4,7 @@ import { BlockPermutation, EntityComponentTypes, GameMode, ItemStack, system, wo
|
|||||||
import { structureCollection } from '../classes/StructureCollection';
|
import { structureCollection } from '../classes/StructureCollection';
|
||||||
import { bannedBlocks, bannedToValidBlockMap, whitelistedBlockStates, resetToBlockStates, bannedDimensionBlocks, specialItemPlacementConversions,
|
import { bannedBlocks, bannedToValidBlockMap, whitelistedBlockStates, resetToBlockStates, bannedDimensionBlocks, specialItemPlacementConversions,
|
||||||
blockIdToItemStackMap } from '../data';
|
blockIdToItemStackMap } from '../data';
|
||||||
|
import { fetchMatchingItemSlot } from '../utils';
|
||||||
|
|
||||||
const ACTION_SLOT = 35;
|
const ACTION_SLOT = 35;
|
||||||
|
|
||||||
@@ -16,7 +17,7 @@ const easyPlace = new Rule({
|
|||||||
extension.addRule(easyPlace);
|
extension.addRule(easyPlace);
|
||||||
|
|
||||||
function onPlayerPlaceBlock(event) {
|
function onPlayerPlaceBlock(event) {
|
||||||
const { player, block } = event;
|
const { player, block, permutationBeingPlaced } = event;
|
||||||
if (!player || !block || !hasActionItemInCorrectSlot(player)) return;
|
if (!player || !block || !hasActionItemInCorrectSlot(player)) return;
|
||||||
const structureBlock = structureCollection.fetchStructureBlock(block.dimension.id, block.location);
|
const structureBlock = structureCollection.fetchStructureBlock(block.dimension.id, block.location);
|
||||||
if (!structureBlock)
|
if (!structureBlock)
|
||||||
@@ -33,8 +34,8 @@ function hasActionItemInCorrectSlot(player) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function tryPlaceBlock(event, player, block, structureBlock) {
|
function tryPlaceBlock(event, player, block, structureBlock) {
|
||||||
if (isBannedBlock(player, structureBlock))
|
if (shouldPreventAction(player, structureBlock))
|
||||||
preventAction(event, player);
|
return preventAction(event, player);
|
||||||
structureBlock = tryConvertBannedToValidBlock(structureBlock);
|
structureBlock = tryConvertBannedToValidBlock(structureBlock);
|
||||||
if (player.getGameMode() === GameMode.creative) {
|
if (player.getGameMode() === GameMode.creative) {
|
||||||
placeBlock(block, structureBlock);
|
placeBlock(block, structureBlock);
|
||||||
@@ -44,6 +45,10 @@ function tryPlaceBlock(event, player, block, structureBlock) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function shouldPreventAction(player, structureBlock) {
|
||||||
|
return isBannedBlock(player, structureBlock);
|
||||||
|
}
|
||||||
|
|
||||||
function preventAction(event, player) {
|
function preventAction(event, player) {
|
||||||
event.cancel = true;
|
event.cancel = true;
|
||||||
system.run(() => {
|
system.run(() => {
|
||||||
@@ -92,6 +97,8 @@ function tryPlaceBlockSurvival(event, player, block, structureBlock) {
|
|||||||
if (itemSlotToUse) {
|
if (itemSlotToUse) {
|
||||||
event.cancel = true;
|
event.cancel = true;
|
||||||
placeBlock(block, structureBlock, itemSlotToUse);
|
placeBlock(block, structureBlock, itemSlotToUse);
|
||||||
|
} else {
|
||||||
|
preventAction(event, player);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,19 +108,6 @@ function getPlaceableItemStack(structureBlock) {
|
|||||||
return newItemId ? new ItemStack(newItemId) : structureBlock.getItemStack();
|
return newItemId ? new ItemStack(newItemId) : structureBlock.getItemStack();
|
||||||
}
|
}
|
||||||
|
|
||||||
function fetchMatchingItemSlot(player, itemToMatchId) {
|
|
||||||
if (!itemToMatchId)
|
|
||||||
return void 0;
|
|
||||||
const inventory = player.getComponent(EntityComponentTypes.Inventory)?.container;
|
|
||||||
if (!inventory)
|
|
||||||
return void 0;
|
|
||||||
for (let index = 0; index < inventory.size; index++) {
|
|
||||||
const itemSlot = inventory.getSlot(index);
|
|
||||||
if (itemSlot.hasItem() && itemSlot?.typeId === itemToMatchId)
|
|
||||||
return itemSlot;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function placeBlock(block, structureBlock, itemSlot) {
|
function placeBlock(block, structureBlock, itemSlot) {
|
||||||
system.run(() => {
|
system.run(() => {
|
||||||
if (itemSlot) {
|
if (itemSlot) {
|
||||||
|
|||||||
+15
-2
@@ -1,4 +1,4 @@
|
|||||||
import { system } from '@minecraft/server';
|
import { system, EntityComponentTypes } from '@minecraft/server';
|
||||||
import { FormCancelationReason } from '@minecraft/server-ui';
|
import { FormCancelationReason } from '@minecraft/server-ui';
|
||||||
|
|
||||||
export async function forceShow(player, form, timeout = Infinity) {
|
export async function forceShow(player, form, timeout = Infinity) {
|
||||||
@@ -11,4 +11,17 @@ export async function forceShow(player, form, timeout = Infinity) {
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
throw new Error("Menu timed out.");
|
throw new Error("Menu timed out.");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export function fetchMatchingItemSlot(entity, itemToMatchId) {
|
||||||
|
if (!itemToMatchId)
|
||||||
|
return void 0;
|
||||||
|
const inventory = entity.getComponent(EntityComponentTypes.Inventory)?.container;
|
||||||
|
if (!inventory)
|
||||||
|
return void 0;
|
||||||
|
for (let index = 0; index < inventory.size; index++) {
|
||||||
|
const itemSlot = inventory.getSlot(index);
|
||||||
|
if (itemSlot.hasItem() && itemSlot?.typeId === itemToMatchId)
|
||||||
|
return itemSlot;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user