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.3 #56

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
15 changes: 5 additions & 10 deletions camera-grapple/camera-grapple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
CursorControlMode,
EditorInputContext,
IPlayerUISession,
InputModifier,
KeyboardKey,
registerEditorExtension,
} from '@minecraft/server-editor';
Expand Down Expand Up @@ -112,20 +113,14 @@ export function registerCameraGrapple() {
uiSession.inputManager.registerKeyBinding(
EditorInputContext.GlobalToolMode,
grappleAction,
{ key: KeyboardKey.KEY_G },
{
uniqueId: 'editorSamples:grapple:flyToCursor',
label: 'sample.cameragrapple.keyBinding.flyToCursor',
}
KeyboardKey.KEY_G,
InputModifier.Control | InputModifier.Shift
);
uiSession.inputManager.registerKeyBinding(
EditorInputContext.GlobalToolMode,
frameAction,
{ key: KeyboardKey.KEY_F },
{
uniqueId: 'editorSamples:grapple:flyToSelection',
label: 'sample.cameragrapple.keyBinding.flyToSelection',
}
KeyboardKey.KEY_F,
InputModifier.Control | InputModifier.Shift
);

return [];
Expand Down
4 changes: 1 addition & 3 deletions camera-grapple/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@
##
## Note, trailing spaces will NOT be trimmed. If you want room between the end of the string and the start of a
## comment on the same line, use TABs.
sample.cameragrapple.title=Camera Base Grapple
sample.cameragrapple.keyBinding.flyToCursor=Fly To Cursor
sample.cameragrapple.keyBinding.flyToSelection=Fly To Selection
sample.cameragrapple.title=Camera Base Grapple
146 changes: 67 additions & 79 deletions dye-brush/dye-brush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import {
ActionTypes,
bindDataSource,
ColorPickerVariant,
CursorTargetMode,
IDropdownItem,
IModalTool,
Expand All @@ -12,6 +11,7 @@ import {
MouseActionType,
MouseInputType,
MouseProps,
Ray,
registerEditorExtension,
Selection,
} from '@minecraft/server-editor';
Expand All @@ -26,11 +26,9 @@ import {
EntityColorComponent,
Player,
Vector3,
RGB,
} from '@minecraft/server';
import { Vector3Utils, VECTOR3_UP } from '@minecraft/math';

// Color identifiers expected by EntityColorComponent
enum BrushColor {
White = 0,
Orange = 1,
Expand Down Expand Up @@ -92,43 +90,25 @@ export function getRotationCorrectedDirectionVector(rotationY: number, realDirec
return directionLookup[relativeDirection];
}

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

colorPalette.forEach((paletteColor, color) => {
const distance = Math.sqrt(
Math.pow(targetColor.red - paletteColor.red, 2) +
Math.pow(targetColor.green - paletteColor.green, 2) +
Math.pow(targetColor.blue - paletteColor.blue, 2)
);
if (distance < minDistance) {
minDistance = distance;
closestColor = color;
}
});

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 }],
//#endregion

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

interface DyeBrushStorage {
Expand All @@ -139,63 +119,65 @@ interface DyeBrushStorage {

type DyeBrushSession = IPlayerUISession<DyeBrushStorage>;

function onColorUpdated(newColor: RGBA, uiSession: DyeBrushSession) {
if (uiSession.scratchStorage) {
uiSession.scratchStorage.previewSelection.setFillColor(newColor);
uiSession.scratchStorage.previewSelection.setOutlineColor({ ...newColor, alpha: 1 });
function onColorUpdated(newColor: BrushColor, uiSession: DyeBrushSession) {
const color = colorPalette.get(newColor);
if (color && uiSession.scratchStorage) {
uiSession.scratchStorage.currentColor = newColor;
uiSession.scratchStorage.previewSelection.setFillColor({
red: color.red,
green: color.green,
blue: color.blue,
alpha: 0.01,
});
uiSession.scratchStorage.previewSelection.setOutlineColor({
red: color.red,
green: color.green,
blue: color.blue,
alpha: 1,
});
const cursorProps = uiSession.extensionContext.cursor.getProperties();
cursorProps.outlineColor = { ...newColor, alpha: 1 };
cursorProps.outlineColor = color;
cursorProps.targetMode = CursorTargetMode.Face;
uiSession.extensionContext.cursor.setProperties(cursorProps);
}
}

function addDyeBrushPane(uiSession: DyeBrushSession, tool: IModalTool) {
const pane = uiSession.createPropertyPane({
title: 'sample.dyeBrush.pane.title',
titleStringId: 'sample.dyeBrush.pane.title',
titleAltText: 'Dye Brush',
});

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

onColorUpdated(props.color, uiSession);

pane.addDropdown(props, 'entityBrush', {
title: 'Brush',
pane.addDropdown(props, 'color', {
titleStringId: 'sample.dyebrush.pane.colordropdown.title',
titleAltText: 'Color',
dropdownItems: Object.values(BrushColor).reduce<IDropdownItem[]>((list, dye, index) => {
if (typeof dye === 'string') {
list.push({
label: dye,
displayStringId: dye,
displayAltText: dye,
value: index,
});
}
return list;
}, []),
onChange: (_obj, _property, _oldValue, newValue: object) => {
const newVal = newValue as unknown as BrushColor;
if (newVal in BrushColor) {
const foundColor = colorPalette.get(newVal);
if (foundColor) {
props.color = { ...foundColor, alpha: props.color.alpha };
}
onChange: (_obj: object, _property: string, _oldValue: object, _newValue: object) => {
const newVal = _newValue as unknown as BrushColor;
if (newVal) {
props.color = newVal;
onColorUpdated(props.color, 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);
},
});

tool.bindPropertyPane(pane);

const onExecuteBrush = () => {
Expand Down Expand Up @@ -248,7 +230,7 @@ function addDyeBrushPane(uiSession: DyeBrushSession, tool: IModalTool) {

const mouseButtonAction = uiSession.actionManager.createAction({
actionType: ActionTypes.MouseRayCastAction,
onExecute: (_, mouseProps: MouseProps) => {
onExecute: (mouseRay: Ray, mouseProps: MouseProps) => {
if (uiSession.scratchStorage === undefined) {
uiSession.log.error('Storage was not initialized.');
return;
Expand All @@ -267,7 +249,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 = props.color;
}
}
}
Expand All @@ -280,7 +262,7 @@ function addDyeBrushPane(uiSession: DyeBrushSession, tool: IModalTool) {

const executeBrushRayAction = uiSession.actionManager.createAction({
actionType: ActionTypes.MouseRayCastAction,
onExecute: (_, mouseProps: MouseProps) => {
onExecute: (mouseRay: Ray, mouseProps: MouseProps) => {
if (mouseProps.inputType === MouseInputType.Drag) {
onExecuteBrush();
}
Expand All @@ -291,15 +273,15 @@ function addDyeBrushPane(uiSession: DyeBrushSession, tool: IModalTool) {
// Example for adding mouse wheel
const executeBrushSizeAction = uiSession.actionManager.createAction({
actionType: ActionTypes.MouseRayCastAction,
onExecute: (_, mouseProps: MouseProps) => {
onExecute: (mouseRay: Ray, mouseProps: MouseProps) => {
if (mouseProps.mouseAction === MouseActionType.Wheel) {
if (mouseProps.inputType === MouseInputType.WheelOut) {
if (props.entityBrush > 0) {
props.entityBrush--;
if (props.color > 0) {
props.color--;
}
} else if (mouseProps.inputType === MouseInputType.WheelIn) {
if (props.entityBrush < 15) {
props.entityBrush++;
if (props.color < 15) {
props.color++;
}
}
onColorUpdated(props.color, uiSession);
Expand All @@ -308,10 +290,14 @@ function addDyeBrushPane(uiSession: DyeBrushSession, tool: IModalTool) {
});
tool.registerMouseWheelBinding(executeBrushSizeAction);

tool.onModalToolActivation.subscribe((evt: ModalToolLifecycleEventPayload) => {
if (evt.isActiveTool) {
tool.onModalToolActivation.subscribe((eventData: ModalToolLifecycleEventPayload) => {
if (eventData.isActiveTool) {
pane.show();
onColorUpdated(props.color, uiSession);
} else {
pane.hide();
}

uiSession.scratchStorage?.previewSelection?.clear();
});

Expand All @@ -322,9 +308,11 @@ function addDyeBrushPane(uiSession: DyeBrushSession, tool: IModalTool) {

export function addDyeBrushTool(uiSession: DyeBrushSession) {
const tool = uiSession.toolRail.addTool({
title: 'sample.dyebrush.tool.title',
tooltip: 'sample.dyebrush.tool.tooltip',
displayAltText: 'Dye Brush',
displayStringId: 'sample.dyebrush.tool.title',
icon: 'pack://textures/dye-brush.png',
tooltipAltText: 'Change the color of entity color components (this only works when actors are un-paused)',
tooltipStringId: 'sample.dyebrush.tool.tooltip',
});

return tool;
Expand Down
2 changes: 1 addition & 1 deletion dye-brush/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
## Note, trailing spaces will NOT be trimmed. If you want room between the end of the string and the start of a
## comment on the same line, use TABs.
sample.dyebrush.tool.title=Dye Brush
sample.dyebrush.tool.tooltip=Change the color of entity color components. The updated color is applied when entity simulation resumes.
sample.dyebrush.tool.tooltip=Change the color of entity color components (this only works when actors are un-paused)
sample.dyebrush.pane.colordropdown.title=Color
Binary file modified editor-samples.mceditoraddon
Binary file not shown.
4 changes: 1 addition & 3 deletions farm-generator/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@ sample.farmgenerator.pane.crops.carrot=Carrot
sample.farmgenerator.pane.animals.title=Animals
sample.farmgenerator.pane.animals.cow=Cow
sample.farmgenerator.pane.animals.sheep=Sheep
sample.farmgenerator.pane.animals.pig=Pig
sample.farmgenerator.keyBinding.toggleTool=Toggle Farm Generator Tool
sample.farmgenerator.keyBinding.place=Place Farm At Cursor
sample.farmgenerator.pane.animals.pig=Pig
Loading