Skip to content

Commit

Permalink
Add custom tag for crafting recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Mar 10, 2024
1 parent 9255fa4 commit e9e36a7
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
**/.jekyll-cache
**/.jekyll-metadata
.vscode
.idea
.bundle
Expand Down
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ title: Ladysnake's Website
source: ./public
plugins_dir: ./jekyll_plugins
markdown: kramdown
incremental: true
sass:
style: compressed
exclude: []
Expand Down
156 changes: 156 additions & 0 deletions jekyll_plugins/RecipeTag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
require 'open-uri'
require 'json'

module Ladysnake
class Ingredient
def initialize(data)
@item = data["item"]
end

EMPTY = Ingredient.new(Hash["item", "minecraft:air"])

def item
@item
end

def empty?
@item == "minecraft:air"
end

def to_s
@item
end
end

class ShapedRecipe
def initialize(result, ingredients, width, height)
@result = result
@ingredients = ingredients
@width = width
@height = height
end

def result
@result
end

def ingredients
@ingredients
end

def pad_ingredients
if @width == 3
if @height == 3
# Nothing do do here
return @ingredients
else
# Missing ingredients must be at the end of the list, so we can just append them
return @ingredients + [Ingredient::EMPTY] * (9 - ingredients.size)
end
else
# We have missing ingredients in the middle of the array - need to reconstruct the list as a 3x3
padded = Array.new(9) { Ingredient::EMPTY }
(0...@height).each do |row|
(0...@width).each do |col|
padded[row * 3 + col] = @ingredients[row * @width + col]
end
end
end
end

def width
@width
end

def height
@height
end

def to_s
"Recipe[#{@width}x#{@height}]"
end
end

class RecipeTag < Liquid::Tag
def self.cache
@cache ||= Jekyll::Cache.new("Ladysnake::RecipeTag")
end

def cache
self.class.cache
end

def render(context)
recipe_root = context.registers[:page]["recipe_root"]
recipe_path = @markup.strip
url = recipe_root + recipe_path

recipe = cache.getset(url) do
begin
json_data = URI.open(url) do |io|
io.read
end

data = JSON.parse(json_data)

pattern = data["pattern"]
height = pattern.size

# Check preconditions
if height > 3
raise "Invalid pattern: too many rows, 3 is maximum"
elsif pattern.empty?
raise "Invalid pattern: empty pattern not allowed"
end

width = pattern[0].length
pattern.each { |row|
if row.length > 3
raise "Invalid pattern: too many columns, 3 is maximum"
elsif row.length != width
raise "Invalid pattern: each row must be the same width"
end
}

# Parse the pattern and key into a list of ingredients
ingredients = Array.new(width * height) { Ingredient::EMPTY }
keys = data["key"]
(0...height).each { |y|
row = pattern[y]
(0...row.length).each { |x|
char = row[x]
ingredient = keys[char] ? Ingredient.new(keys[char]) : Ingredient::EMPTY
if ingredient.empty? && char != ' '
raise "Pattern references symbol '#{char}' but it's not defined in the key"
end
ingredients[x + width * y] = ingredient
}
}

# Check postconditions
unused_symbols = keys.keys - pattern.join.chars
unless unused_symbols.empty?
raise "Key defines symbols that aren't used in pattern: #{unused_symbols.join(", ")}"
end

ShapedRecipe.new(data["result"], ingredients, width, height)
rescue StandardError => e
puts "Error loading JSON data: #{e.message}"
nil
end
end

# Check if the data was loaded successfully
if recipe
context["slots"] = recipe.pad_ingredients.map { |i| i.item }
context["result"] = recipe.result["item"]
context["count"] = recipe.result["count"]
Liquid::Template.parse("{% include mc/crafting.liquid slots=slots result=result count=count %}").render(context)
else
puts "Failed to load recipe from #{url}"
"<em>Failed to load recipe #{recipe_path}</em>"
end
end
end
end
Liquid::Template.register_tag('recipe', Ladysnake::RecipeTag)
21 changes: 21 additions & 0 deletions public/_data/minecraft/items.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
name: Iron Ingot
link: https://minecraft.wiki/w/Iron_Ingot
icon: https://minecraft.wiki/images/Invicon_Iron_Ingot.png
"minecraft:leather":
name: Leather
link: https://minecraft.wiki/w/Leather
icon: https://minecraft.wiki/images/Invicon_Leather.png
"minecraft:popped_chorus_fruit":
name: Popped Chorus Fruit
link: https://minecraft.wiki/w/Popped_Chorus_Fruit
icon: https://minecraft.wiki/images/Invicon_Popped_Chorus_Fruit.png
"minecraft:redstone":
name: Redstone Dust
link: https://minecraft.wiki/w/Redstone_Dust
Expand All @@ -31,3 +39,16 @@
name: String
link: https://minecraft.wiki/w/String
icon: https://minecraft.wiki/images/Invicon_String.png
"minecraft:twisting_vines":
name: Twisting Vines
link: https://minecraft.wiki/w/Twisting_Vines
icon: https://minecraft.wiki/images/Invicon_Twisting_Vines.png
"ratsmischief:leather_rat_pouch":
name: Leather Pouch
icon: https://raw.githubusercontent.com/Ladysnake/RATs-Mischief/2.0/src/main/resources/assets/ratsmischief/textures/item/leather_rat_pouch.png
"ratsmischief:purpur_rat_pouch":
name: Purpur Pouch
icon: https://raw.githubusercontent.com/Ladysnake/RATs-Mischief/2.0/src/main/resources/assets/ratsmischief/textures/item/purpur_rat_pouch.png
"ratsmischief:twisted_rat_pouch":
name: Twisted Pouch
icon: https://raw.githubusercontent.com/Ladysnake/RATs-Mischief/2.0/src/main/resources/assets/ratsmischief/textures/item/twisted_rat_pouch.png
6 changes: 5 additions & 1 deletion public/_includes/mc/crafting.liquid
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{%- if include.slots -%}
{%- assign slots = include.slots -%}
{%- else -%}
{%- assign slots = "" | split: ","
| push: include.i1
| push: include.i2
Expand All @@ -9,8 +12,9 @@
| push: include.i8
| push: include.i9
-%}
{%- endif -%}
<div class="mcui crafting-table">
<img class="mcui-background" src="/public/img/wiki-crafting-recipe.png" alt="Crafting recipe" height="198" width="450"/>
<img class="mcui-background" src="/img/wiki-crafting-recipe.png" alt="Crafting recipe" height="198" width="450"/>
<div class="crafting-grid">
{%- for slot in slots -%}
{%- capture alt_prefix-%}Ingredient {{ forloop.index }}: {% endcapture -%}
Expand Down
8 changes: 7 additions & 1 deletion public/_sass/parts/mc.scss
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,25 @@
.mcui {
position: relative;
font-size: 20px;

figure & {
font-size: 26.5px;
}
}

.mcui-background {
position: absolute;
max-width: 100%;
width: auto;
max-height: 100%;
width: 100%;
height: auto;
}

.mcui.crafting-table {
display: flex;
align-items: center;
width: 17em;
height: 7.48em;
justify-content: space-around;
}

Expand Down
Binary file removed public/wiki/rats-mischief/LeatherPouchRecipe.png
Binary file not shown.
Binary file removed public/wiki/rats-mischief/PurpurPouchRecipe.png
Binary file not shown.
Binary file removed public/wiki/rats-mischief/TwistedPouchRecipe.png
Binary file not shown.
12 changes: 6 additions & 6 deletions public/wiki/rats-mischief/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ title: Rat's Mischief
slug: rats-mischief
curse_project: 431787
modrinth: true
recipe_root: https://raw.githubusercontent.com/Ladysnake/RATs-Mischief/2.0/src/main/resources/data/ratsmischief/recipes/
---
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

Expand Down Expand Up @@ -186,12 +187,11 @@ Rat pouches can be used to quickly gather rats back. Using a pouch on a rat will
Pouches can be crafted like bundles with leather, twisted vines and popped chorus fruit, to produce respectively pouches with a capacity of 5, 10 and 20 rats.
Rats stored in Pouches won't age, meaning they will keep status effects, health, their variant and names.

![Leather Pouch Recipe](LeatherPouchRecipe.png)

![Twisted Pouch Recipe](TwistedPouchRecipe.png)

![Purpur Pouch Recipe](PurpurPouchRecipe.png)

<figure>
{% recipe leather_rat_pouch.json %}
{% recipe twisted_rat_pouch.json %}
{% recipe purpur_rat_pouch.json %}
</figure>

#### Mask of Rat

Expand Down

0 comments on commit e9e36a7

Please sign in to comment.