Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates for Minecraft release 1.21.20 #61

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 56 additions & 58 deletions dye-brush/dye-brush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import {
ActionTypes,
ColorPickerPropertyItemVariant,
bindDataSource,
ColorPickerVariant,
CursorTargetMode,
IDropdownItem,
IModalTool,
IObservable,
IPlayerUISession,
makeObservable,
ModalToolLifecycleEventPayload,
MouseActionType,
MouseInputType,
Expand All @@ -32,7 +31,7 @@ import {
import { Vector3Utils, VECTOR3_UP } from '@minecraft/math';

// Color identifiers expected by EntityColorComponent
enum EntityColor {
enum BrushColor {
White = 0,
Orange = 1,
Magenta = 2,
Expand Down Expand Up @@ -94,9 +93,9 @@ export function getRotationCorrectedDirectionVector(rotationY: number, realDirec
}

// Calculate nearest entity color to an RGBA color
function findClosestColor(targetColor: RGBA, colorPalette: Map<EntityColor, RGB>): EntityColor {
function findClosestColor(targetColor: RGBA, colorPalette: Map<BrushColor, RGB>): BrushColor {
let minDistance = Number.MAX_VALUE;
let closestColor: EntityColor = EntityColor.White;
let closestColor: BrushColor = BrushColor.White;

colorPalette.forEach((paletteColor, color) => {
const distance = Math.sqrt(
Expand All @@ -113,31 +112,29 @@ function findClosestColor(targetColor: RGBA, colorPalette: Map<EntityColor, RGB>
return closestColor;
}

const colorPalette = new Map<EntityColor, RGB>([
[EntityColor.White, { red: 1, green: 1, blue: 1 }],
[EntityColor.Orange, { red: 0.95, green: 0.459, blue: 0 }],
[EntityColor.Magenta, { red: 0.94, green: 0, blue: 0.9 }],
[EntityColor.LightBlue, { red: 0, green: 0.85, blue: 0.95 }],
[EntityColor.Yellow, { red: 0.85, green: 0.95, blue: 0 }],
[EntityColor.LightGreen, { red: 0, green: 0.95, blue: 0.6 }],
[EntityColor.Pink, { red: 0.9, green: 0.65, blue: 0.85 }],
[EntityColor.Gray, { red: 0.6, green: 0.6, blue: 0.6 }],
[EntityColor.Silver, { red: 0.75, green: 0.75, blue: 0.75 }],
[EntityColor.Cyan, { red: 0, green: 0.9, blue: 0.9 }],
[EntityColor.Purple, { red: 0.45, green: 0, blue: 0.9 }],
[EntityColor.Blue, { red: 0, green: 0, blue: 1 }],
[EntityColor.Brown, { red: 0.8, green: 0.5, blue: 0.1 }],
[EntityColor.Green, { red: 0, green: 1, blue: 0 }],
[EntityColor.Red, { red: 1, green: 0, blue: 0 }],
[EntityColor.Black, { red: 0, green: 0, blue: 0 }],
const colorPalette = new Map<BrushColor, RGB>([
[BrushColor.White, { red: 1, green: 1, blue: 1 }],
[BrushColor.Orange, { red: 0.95, green: 0.459, blue: 0 }],
[BrushColor.Magenta, { red: 0.94, green: 0, blue: 0.9 }],
[BrushColor.LightBlue, { red: 0, green: 0.85, blue: 0.95 }],
[BrushColor.Yellow, { red: 0.85, green: 0.95, blue: 0 }],
[BrushColor.LightGreen, { red: 0, green: 0.95, blue: 0.6 }],
[BrushColor.Pink, { red: 0.9, green: 0.65, blue: 0.85 }],
[BrushColor.Gray, { red: 0.6, green: 0.6, blue: 0.6 }],
[BrushColor.Silver, { red: 0.75, green: 0.75, blue: 0.75 }],
[BrushColor.Cyan, { red: 0, green: 0.9, blue: 0.9 }],
[BrushColor.Purple, { red: 0.45, green: 0, blue: 0.9 }],
[BrushColor.Blue, { red: 0, green: 0, blue: 1 }],
[BrushColor.Brown, { red: 0.8, green: 0.5, blue: 0.1 }],
[BrushColor.Green, { red: 0, green: 1, blue: 0 }],
[BrushColor.Red, { red: 1, green: 0, blue: 0 }],
[BrushColor.Black, { red: 0, green: 0, blue: 0 }],
]);

interface DyeBrushStorage {
previewSelection: Selection;
lastVolumePlaced?: BoundingBox;
currentColor: EntityColor;
brushColor: IObservable<RGBA>;
brushSize: number;
currentColor: BrushColor;
}

type DyeBrushSession = IPlayerUISession<DyeBrushStorage>;
Expand All @@ -154,23 +151,22 @@ function onColorUpdated(newColor: RGBA, uiSession: DyeBrushSession) {
}

function addDyeBrushPane(uiSession: DyeBrushSession, tool: IModalTool) {
if (!uiSession.scratchStorage) {
throw Error('UI Session storage should exist');
}
const brushColor = uiSession.scratchStorage.brushColor;
const brushSize = uiSession.scratchStorage.brushSize;

const pane = uiSession.createPropertyPane({
title: 'sample.dyeBrush.pane.title',
});

const entityBrush = makeObservable(EntityColor.White);
// Here is the binding created.
const props = bindDataSource(pane, {
entityBrush: BrushColor.White,
color: { red: 1, green: 1, blue: 1, alpha: 0.5 },
size: 4,
});

onColorUpdated(brushColor.value, uiSession);
onColorUpdated(props.color, uiSession);

pane.addDropdown(entityBrush, {
pane.addDropdown(props, 'entityBrush', {
title: 'Brush',
entries: Object.values(EntityColor).reduce<IDropdownItem[]>((list, dye, index) => {
dropdownItems: Object.values(BrushColor).reduce<IDropdownItem[]>((list, dye, index) => {
if (typeof dye === 'string') {
list.push({
label: dye,
Expand All @@ -179,22 +175,24 @@ function addDyeBrushPane(uiSession: DyeBrushSession, tool: IModalTool) {
}
return list;
}, []),
onChange: (newVal: number) => {
if (newVal in EntityColor) {
onChange: (_obj, _property, _oldValue, newValue: object) => {
const newVal = newValue as unknown as BrushColor;
if (newVal in BrushColor) {
const foundColor = colorPalette.get(newVal);
if (foundColor) {
brushColor.set({ ...foundColor, alpha: brushColor.value.alpha });
props.color = { ...foundColor, alpha: props.color.alpha };
}
onColorUpdated(brushColor.value, uiSession);
onColorUpdated(props.color, uiSession);
}
},
});

pane.addColorPicker(brushColor, {
variant: ColorPickerPropertyItemVariant.Expanded,
onChange: (color: RGBA) => {
entityBrush.set(findClosestColor(color, colorPalette));
onColorUpdated(brushColor.value, uiSession);
pane.addColorPicker(props, 'color', {
variant: ColorPickerVariant.Expanded,
onChange: (_obj, _property, _oldValue, newValue: object) => {
const color = newValue as unknown as RGBA;
props.entityBrush = findClosestColor(color, colorPalette);
onColorUpdated(props.color, uiSession);
},
});

Expand All @@ -219,11 +217,11 @@ function addDyeBrushPane(uiSession: DyeBrushSession, tool: IModalTool) {
const directionForward = getRotationCorrectedDirectionVector(rotationY, Direction.South);
const relativeDirection = Vector3Utils.add(Vector3Utils.add(directionRight, directionForward), VECTOR3_UP);

const sizeHalf = Math.floor(brushSize / 2);
const sizeHalf = Math.floor(props.size / 2);
let fromOffset = Vector3Utils.scale(relativeDirection, -sizeHalf);
const toOffset = Vector3Utils.scale(relativeDirection, brushSize - 1);
const toOffset = Vector3Utils.scale(relativeDirection, props.size - 1);

const isEven = brushSize % 2 === 0;
const isEven = props.size % 2 === 0;
if (isEven) {
fromOffset = Vector3Utils.add(fromOffset, VECTOR3_UP);
}
Expand Down Expand Up @@ -269,7 +267,7 @@ function addDyeBrushPane(uiSession: DyeBrushSession, tool: IModalTool) {
for (const entity of entities) {
const colorComp = entity.getComponent('minecraft:color') as EntityColorComponent;
if (colorComp) {
colorComp.value = entityBrush.value;
colorComp.value = props.entityBrush;
}
}
}
Expand All @@ -296,28 +294,30 @@ function addDyeBrushPane(uiSession: DyeBrushSession, tool: IModalTool) {
onExecute: (_, mouseProps: MouseProps) => {
if (mouseProps.mouseAction === MouseActionType.Wheel) {
if (mouseProps.inputType === MouseInputType.WheelOut) {
if (entityBrush.value > 0) {
entityBrush.set(entityBrush.value - 1);
if (props.entityBrush > 0) {
props.entityBrush--;
}
} else if (mouseProps.inputType === MouseInputType.WheelIn) {
if (entityBrush.value < 15) {
entityBrush.set(entityBrush.value + 1);
if (props.entityBrush < 15) {
props.entityBrush++;
}
}
onColorUpdated(brushColor.value, uiSession);
onColorUpdated(props.color, uiSession);
}
},
});
tool.registerMouseWheelBinding(executeBrushSizeAction);

tool.onModalToolActivation.subscribe((evt: ModalToolLifecycleEventPayload) => {
if (evt.isActiveTool) {
onColorUpdated(brushColor.value, uiSession);
onColorUpdated(props.color, uiSession);
}
uiSession.scratchStorage?.previewSelection?.clear();
});

pane.hide();

return props;
}

export function addDyeBrushTool(uiSession: DyeBrushSession) {
Expand All @@ -342,9 +342,7 @@ export function registerDyeBrushExtension() {

const storage: DyeBrushStorage = {
previewSelection: previewSelection,
currentColor: EntityColor.White,
brushColor: makeObservable<RGBA>({ red: 1, green: 1, blue: 1, alpha: 0.5 }),
brushSize: 4,
currentColor: BrushColor.White,
};
uiSession.scratchStorage = storage;

Expand Down
Binary file modified editor-samples.mceditoraddon
Binary file not shown.
71 changes: 40 additions & 31 deletions farm-generator/farm-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ import {
MouseInputType,
MouseProps,
Ray,
bindDataSource,
makeObservable,
registerEditorExtension,
} from '@minecraft/server-editor';
import { Player, Vector3 } from '@minecraft/server';
import { MinecraftBlockTypes, MinecraftEntityTypes } from '@minecraft/vanilla-data';

type SettingsType = {
farmWidth: number;
farmLength: number;
fenceType: number;
};

type CommonSettingsType = {
farmWidth: IObservable<number>;
farmLength: IObservable<number>;
irrigation: IObservable<boolean>;
fenceType: IObservable<number>;
};

type CropSettingsType = {
Expand Down Expand Up @@ -78,6 +82,7 @@ const buildFarm = (
possibleAnimals: MinecraftEntityTypes[],
possibleCrops: string[],
player: Player,
settings: SettingsType,
commonSettings: CommonSettingsType
) => {
let didPlaceAnimal = false;
Expand All @@ -100,7 +105,7 @@ const buildFarm = (
block?.setType(MinecraftBlockTypes.Farmland);
}
if (isBorder) {
blockAbove?.setType(fenceTypeToBlockType(commonSettings.fenceType.value));
blockAbove?.setType(fenceTypeToBlockType(settings.fenceType));
} else if (possibleAnimals.length > 0 && getRandomInt(5) === 5) {
const animal = getRandomInt(possibleAnimals.length - 1);
const entityType = possibleAnimals[animal];
Expand Down Expand Up @@ -134,18 +139,21 @@ function addFarmGeneratorSettingsPane(uiSession: IPlayerUISession, tool: IModalT
const windowPane = uiSession.createPropertyPane({
title: 'sample.farmgenerator.pane.title',
});
const cropPane = windowPane.createSubPane({
const cropPane = windowPane.createPropertyPane({
title: 'sample.farmgenerator.pane.crops.title',
});
const animalPane = windowPane.createSubPane({
const animalPane = windowPane.createPropertyPane({
title: 'sample.farmgenerator.pane.animals.title',
});

const settings: SettingsType = bindDataSource(windowPane, {
farmWidth: 10,
farmLength: 10,
fenceType: 0, // oak fence
});

const commonSettings: CommonSettingsType = {
farmWidth: makeObservable(10),
farmLength: makeObservable(10),
irrigation: makeObservable(false),
fenceType: makeObservable(0),
};

const cropSettings: CropSettingsType = {
Expand Down Expand Up @@ -206,47 +214,47 @@ function addFarmGeneratorSettingsPane(uiSession: IPlayerUISession, tool: IModalT
}
let x = 1;
let z = 1;
let length = commonSettings.farmLength.value;
let width = commonSettings.farmWidth.value;
let length = settings.farmLength;
let width = settings.farmWidth;
if (Math.round(player.getViewDirection().z) === -1) {
targetCorner = {
x: targetCorner.x + (commonSettings.farmWidth.value / 2 - 1),
x: targetCorner.x + (settings.farmWidth / 2 - 1),
y: targetCorner.y,
z: targetCorner.z - (commonSettings.farmLength.value / 2 - 1),
z: targetCorner.z - (settings.farmLength / 2 - 1),
};
uiSession.log.info('Facing north');
x = -1;
} else if (Math.round(player.getViewDirection().x) === 1) {
targetCorner = {
x: targetCorner.x + (commonSettings.farmWidth.value / 2 - 1),
x: targetCorner.x + (settings.farmWidth / 2 - 1),
y: targetCorner.y,
z: targetCorner.z + (commonSettings.farmLength.value / 2 - 1),
z: targetCorner.z + (settings.farmLength / 2 - 1),
};
uiSession.log.info('Facing east');
length = commonSettings.farmWidth.value;
width = commonSettings.farmLength.value;
length = settings.farmWidth;
width = settings.farmLength;
x = -1;
z = -1;
}
if (Math.round(player.getViewDirection().z) === 1) {
targetCorner = {
x: targetCorner.x - (commonSettings.farmWidth.value / 2 - 1),
x: targetCorner.x - (settings.farmWidth / 2 - 1),
y: targetCorner.y,
z: targetCorner.z + (commonSettings.farmLength.value / 2 - 1),
z: targetCorner.z + (settings.farmLength / 2 - 1),
};
uiSession.log.info('Facing south');
z = -1;
} else if (Math.round(player.getViewDirection().x) === -1) {
targetCorner = {
x: targetCorner.x - (commonSettings.farmWidth.value / 2 - 1),
x: targetCorner.x - (settings.farmWidth / 2 - 1),
y: targetCorner.y,
z: targetCorner.z - (commonSettings.farmLength.value / 2 - 1),
z: targetCorner.z - (settings.farmLength / 2 - 1),
};
uiSession.log.info('Facing west');
length = commonSettings.farmWidth.value;
width = commonSettings.farmLength.value;
length = settings.farmWidth;
width = settings.farmLength;
}
buildFarm(targetCorner, x, z, length, width, possibleAnimals, possibleCrops, player, commonSettings);
buildFarm(targetCorner, x, z, length, width, possibleAnimals, possibleCrops, player, settings, commonSettings);
};

// Create an action that will be executed on left mouse click
Expand Down Expand Up @@ -281,23 +289,22 @@ function addFarmGeneratorSettingsPane(uiSession: IPlayerUISession, tool: IModalT
);

tool.registerMouseButtonBinding(executeMouseAction);

windowPane.addNumber(commonSettings.farmLength, {
windowPane.addNumber(settings, 'farmLength', {
title: 'sample.farmgenerator.pane.length',
min: 2,
max: 20,
isInteger: true,
showSlider: false,
});
windowPane.addNumber(commonSettings.farmWidth, {
windowPane.addNumber(settings, 'farmWidth', {
title: 'sample.farmgenerator.pane.width',
min: 2,
max: 20,
isInteger: true,
showSlider: false,
});
windowPane.addDropdown(commonSettings.fenceType, {
windowPane.addDropdown(settings, 'fenceType', {
title: 'sample.farmgenerator.pane.fence',
enable: true,
entries: [
dropdownItems: [
{
label: 'Oak',
value: 0,
Expand Down Expand Up @@ -367,6 +374,8 @@ function addFarmGeneratorSettingsPane(uiSession: IPlayerUISession, tool: IModalT
});

tool.bindPropertyPane(windowPane);

return settings;
}

/**
Expand Down
Loading