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.30-preview.21 #59

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: 58 additions & 56 deletions dye-brush/dye-brush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

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

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

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

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

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 }],
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 }],
]);

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

type DyeBrushSession = IPlayerUISession<DyeBrushStorage>;
Expand All @@ -151,22 +154,23 @@ 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',
});

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

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

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

Expand All @@ -217,11 +219,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(props.size / 2);
const sizeHalf = Math.floor(brushSize / 2);
let fromOffset = Vector3Utils.scale(relativeDirection, -sizeHalf);
const toOffset = Vector3Utils.scale(relativeDirection, props.size - 1);
const toOffset = Vector3Utils.scale(relativeDirection, brushSize - 1);

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

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

pane.hide();

return props;
}

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

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

Expand Down
Binary file modified editor-samples.mceditoraddon
Binary file not shown.
1 change: 1 addition & 0 deletions farm-generator/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ sample.farmgenerator.pane.length=Length
sample.farmgenerator.pane.width=Width
sample.farmgenerator.pane.fence=Fence
sample.farmgenerator.pane.irrigation=Add Irrigation
sample.farmgenerator.pane.irrigation.tooltip=Adds water blocks in the farm to irrigate crops.
sample.farmgenerator.pane.crops.title=Crops
sample.farmgenerator.pane.crops.wheat=Wheat
sample.farmgenerator.pane.crops.potato=Potato
Expand Down
Loading