Skip to content

Commit

Permalink
safer getter for all layers, including custom layers in getLayerById
Browse files Browse the repository at this point in the history
  • Loading branch information
IR0NSIGHT committed Dec 1, 2023
1 parent ffbf9dd commit fb6aeb2
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/worldpainterApi/worldpainterApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ export interface Terrain {
getName(): string;
}


const allLayers =(): Layer[] => {
const javaIterator = dimension.getAllLayers(false).iterator();
const myArr = [];
while (javaIterator.hasNext()) {
// Assuming the Java objects have a 'name' property, adapt this to your actual Java class structure
const javaLayer = javaIterator.next();
myArr.push(javaLayer);
}
return myArr;
}

export function getLayerById(layerId: string): Layer | ParsingError {
switch (layerId) {
case 'Frost': // @ts-ignore wp object
Expand Down Expand Up @@ -52,20 +64,25 @@ export function getLayerById(layerId: string): Layer | ParsingError {

default: {
//search for custom layers
const customLayers: Layer[] = dimension.getCustomLayers();
if (!Array.isArray(customLayers))
return { mssg: 'no custom layers found in project: ' + layerId };
//@ts-ignore overload exists, but isnt documented in dimension typestub.
const customLayers: Layer[] = allLayers()

let matched: Layer | undefined = undefined;
customLayers
try {
customLayers
.filter((f) => f != null)
.forEach(function (element) {
if (layerId == element.getName()) {
matched = element;
}
});
} catch (error) {
//ignore, handled below
return { mssg: 'could not find custom layer with name: ' + layerId + "\navailable custom layers:\n" + customLayers + "\nerror:" + error};
}

if (matched == undefined || matched == null) {
return { mssg: 'could not find custom layer with name: ' + layerId };
return { mssg: 'could not find custom layer with name: ' + layerId + "\navailable custom layers:\n" + customLayers.map(l => l.getName()) };
}
return matched;
}
Expand Down

0 comments on commit fb6aeb2

Please sign in to comment.