Skip to content

Commit

Permalink
Add treasure disks
Browse files Browse the repository at this point in the history
  • Loading branch information
SquidDev committed Aug 15, 2024
1 parent 2b9d7c7 commit 026643c
Showing 11 changed files with 435 additions and 327 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -10,6 +10,8 @@ jobs:
steps:
- name: 📥 Clone repository
uses: actions/checkout@v4
with:
submodules: recursive

- name: 📥 Setup Node
uses: actions/setup-node@v4
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "vendor/treasure-disks"]
path = vendor/treasure-disks
url = https://github.com/cc-tweaked/treasure-disks.git
586 changes: 268 additions & 318 deletions package-lock.json

Large diffs are not rendered by default.

27 changes: 20 additions & 7 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { For, Show, createMemo, createSignal, type Accessor, type Component, type JSX } from 'solid-js';
import { Suspense, Switch, Match, For, Show, createMemo, createResource, createSignal, type Accessor, type Component, type JSX } from 'solid-js';

import styles from "./App.module.css";

import { ToggleButton } from './components/Button';
import Download from './components/Download';
import { Feature, type FeatureProps } from './components/Feature';
import { PackOutput, Version, makeModId, versions, type PackItem } from './datapack';
import { turtleFlags } from './datapack/overlay';
import turtleFlags from './datapack/overlay';
import treasure from "./datapack/treasure";
import tools from './datapack/tools';


@@ -40,17 +41,20 @@ const App: Component = () => {
const [mcVersion, setMcVersion] = createSignal(Version.MC_1_20_1);

const enabledTools = createPackItems(tools, mcVersion);
const enabledTweaks = createPackItems([turtleFlags], mcVersion);
const enabledTweaks = createPackItems([turtleFlags, treasure], mcVersion);
const allFeatures = () => [...enabledTools(), ...enabledTweaks()];

const createPack = createMemo(() => {
const [createPack] = createResource(async () => {
console.log("id")
const id = packId();
const pack = new PackOutput(mcVersion(), packName(), id === "" ? undefined : id);
const futures: (void | Promise<void>)[] = [];
for (const feature of allFeatures()) {
if (feature.checked()) feature.process(pack);
if (feature.checked()) futures.push(feature.process(pack));
}
await Promise.all(futures);
return pack;
});
}, v => v);

return <>
<Section title="Pack Details">
@@ -81,7 +85,16 @@ const App: Component = () => {
<FeatureSection title="Turtle Tools" features={enabledTools()} />
<FeatureSection title="Tweaks" features={enabledTweaks()} />
<Section title="Download">
<Download pack={createPack()} />
<Suspense fallback={<p>Loading...</p>}>
<Switch>
<Match when={createPack.error}>
<p>An error occurred (<code>{createPack.error}</code>)</p>
</Match>
<Match when={createPack()}>
<Download pack={createPack()!!} />
</Match>
</Switch>
</Suspense>
</Section>
</>;
};
Binary file added src/assets/treasure.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/datapack/index.ts
Original file line number Diff line number Diff line change
@@ -185,5 +185,5 @@ export type PackItem = {
enabled?: (version: Version) => boolean;

/** Process this datapack. */
process: (datapack: PackOutput) => void;
process: (datapack: PackOutput) => (void | Promise<void>);
};
4 changes: 3 additions & 1 deletion src/datapack/overlay.ts
Original file line number Diff line number Diff line change
@@ -99,7 +99,7 @@ const makeOverlays = (overlays: Overlay[]) => (output: PackOutput): void => {
for (const overlay of overlays) addOverlay(output, overlay);
}

export const turtleFlags: PackItem = {
const turtleFlags: PackItem = {
name: "More Turtle Flags",
description: "Add extra flags for turtles to hold.",
icon,
@@ -146,3 +146,5 @@ export const turtleFlags: PackItem = {
},
]),
};

export default turtleFlags;
61 changes: 61 additions & 0 deletions src/datapack/treasure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Version, type PackItem } from ".";

import icon from "../assets/treasure.png";

export type TreasureDisk = {
author: string,
name: string,
colour: number,
files: Record<string, string>,
}

let diskCache: TreasureDisk[] | null = null;

const treasure: PackItem = {
name: "Treasure Disks",
description: "Add all of ComputerCraft's old treasure disks.",
icon,
iconAlt: "A blue ComputerCraft floppy disk.",
process: async output => {
// We store the disks locally to avoid awaits on later actions, which allows the
// render to be instant.
const disks = diskCache ?? (diskCache = (await import("virtual:treasure-disks")).default);

const pool = [];
for (const { author, name, colour, files } of disks) {
for (const [file, contents] of Object.entries(files)) {
output.data("computercraft", `lua/treasure/${author}/${name}/${file}`, contents)
}

const title = `${name} by ${author}`;
const path = `${author}/${name}`;

pool.push({
type: "minecraft:item",
name: "computercraft:treasure_disk",
functions: [
output.version >= Version.MC_1_20_6 ? {
function: "minecraft:set_components",
components: {
"computercraft:treasure_disk": { name: title, path },
"minecraft:dyed_color": { rgb: colour, show_in_tooltip: false }
}
} : {
function: "minecraft:set_nbt",
tag: JSON.stringify({ "Title": title, "SubPath": path, "Colour": colour })
}
],
})
}

output.data("computercraft", output.version >= Version.MC_1_21 ? "loot_table/treasure_disk.json" : "loot_tables/treasure_disk.json", {
pools: [{
name: "main",
rolls: 1,
entries: pool,
}]
});
},
};

export default treasure;
5 changes: 5 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -2,3 +2,8 @@ declare module "*?base64" {
const src: string
export default src
}

declare module "virtual:treasure-disks" {
const contents: import("./datapack/treasure").TreasureDisk[];
export default contents;
}
1 change: 1 addition & 0 deletions vendor/treasure-disks
Submodule treasure-disks added at 60d1b0
71 changes: 71 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { defineConfig, type Plugin } from "vite";
import fs from "fs/promises";
import path from "path";
import solidPlugin from "vite-plugin-solid";
import { createHash } from "crypto";

const b64Loader: Plugin = {
name: 'b64-loader',
@@ -13,11 +15,80 @@ const b64Loader: Plugin = {
}
};

/**
* Provides a virtual module that contains the contents of all our treasure disks.
*/
const treasureLoader: Plugin = (() => {
const virtualModuleId = "virtual:treasure-disks"
const resolvedVirtualModuleId = '\0' + virtualModuleId

const colours = [
0x111111, // Black
0xcc4c4c, // Red
0x57A64E, // Green
0x7f664c, // Brown
0x3366cc, // Blue
0xb266e5, // Purple
0x4c99b2, // Cyan
0x999999, // Light_grey
0x4c4c4c, // Grey
0xf2b2cc, // Pink
0x7fcc19, // Lime
0xdede6c, // Yellow
0x99b2f2, // Light_blue
0xe57fd8, // Magenta
0xf2b233, // Orange
0xf0f0f0, // White]
]

return {
name: "treasure",
resolveId: (id: string) => id === virtualModuleId ? resolvedVirtualModuleId : undefined,
async load(id: string) {
if (id !== resolvedVirtualModuleId) return undefined;

const treasureRoot = "vendor/treasure-disks";
const root = "data/computercraft/lua/treasure";

const disks: import("./src/datapack/treasure").TreasureDisk[] = [];

// Read all programs from the treasure-disks repo
for (const author of await fs.readdir(path.join(treasureRoot, root))) {
if (author === "deprecated") continue;

for (const name of await fs.readdir(path.join(treasureRoot, root, author))) {
// Generate a colour from the author + name.
const hash = createHash("md5");
hash.update(`${author}/${name}`);
const digest = hash.digest();
const colour = colours[(digest[0] >> 4) & 0xf];

// Walk the file tree to find the contents.
const files: Record<string, string> = {};
const programDir = path.join(treasureRoot, root, author, name);
for (const child of await fs.readdir(programDir, { recursive: true, withFileTypes: true })) {
if (!child.isFile()) continue;

const childPath = path.join(child.parentPath, child.name);
files[path.relative(programDir, childPath)] = await fs.readFile(childPath, { encoding: "utf-8" });
}

disks.push({ author, name, colour, files });
}
}

// Then save as one massive JS file.
return `export default ${JSON.stringify(disks)};`
},
}
})();

export default defineConfig({
base: "",
plugins: [
solidPlugin(),
b64Loader,
treasureLoader,
],
server: {
port: 3000,

0 comments on commit 026643c

Please sign in to comment.