Skip to content

Commit

Permalink
Move tests for item names and recipes into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander01998 committed Jan 25, 2025
1 parent 24bc010 commit 11dc331
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 177 deletions.
49 changes: 49 additions & 0 deletions src/main/java/net/wurstclient/glass/test/ItemNamesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2019-2025 Wurst-Imperium and contributors.
*
* This source code is subject to the terms of the GNU General Public
* License, version 3. If a copy of the GPL was not distributed with this
* file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt
*/
package net.wurstclient.glass.test;

import net.minecraft.client.resource.language.I18n;
import net.minecraft.item.ItemStack;
import net.wurstclient.glass.MoGlassBlocks;

public enum ItemNamesTest
{
;

public static void testItemNamesShowUpCorrectly()
{
System.out.println("Testing item names...");

assertItemName("Glass Slab", new ItemStack(MoGlassBlocks.GLASS_SLAB));
assertItemName("Glass Stairs",
new ItemStack(MoGlassBlocks.GLASS_STAIRS));

assertItemName("Tinted Glass Slab",
new ItemStack(MoGlassBlocks.TINTED_GLASS_SLAB));
assertItemName("Tinted Glass Stairs",
new ItemStack(MoGlassBlocks.TINTED_GLASS_STAIRS));

String[] colors = {"White", "Orange", "Magenta", "Light Blue", "Yellow",
"Lime", "Pink", "Gray", "Light Gray", "Cyan", "Purple", "Blue",
"Brown", "Green", "Red", "Black"};
for(int i = 0; i < colors.length; i++)
{
assertItemName(colors[i] + " Stained Glass Slab",
new ItemStack(MoGlassBlocks.STAINED_GLASS_SLABS.get(i)));
assertItemName(colors[i] + " Stained Glass Stairs",
new ItemStack(MoGlassBlocks.STAINED_GLASS_STAIRS.get(i)));
}
}

private static void assertItemName(String expected, ItemStack stack)
{
if(!expected.equals(I18n.translate(stack.getName().getString())))
throw new RuntimeException("Wrong item name: Expected <" + expected
+ "> but got <" + stack.getName() + ">");
}
}
179 changes: 2 additions & 177 deletions src/main/java/net/wurstclient/glass/test/MoGlassTestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,15 @@
import static net.wurstclient.glass.test.WiModsTestHelper.*;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.spongepowered.asm.mixin.MixinEnvironment;

import net.fabricmc.api.ClientModInitializer;
import net.minecraft.SharedConstants;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.client.gui.screen.AccessibilityOnboardingScreen;
import net.minecraft.client.gui.screen.TitleScreen;
import net.minecraft.client.gui.screen.world.CreateWorldScreen;
import net.minecraft.client.gui.screen.world.SelectWorldScreen;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.CraftingRecipe;
import net.minecraft.recipe.RecipeEntry;
import net.minecraft.recipe.RecipeType;
import net.minecraft.recipe.StonecuttingRecipe;
import net.minecraft.recipe.display.CuttingRecipeDisplay;
import net.minecraft.recipe.input.CraftingRecipeInput;
import net.minecraft.recipe.input.SingleStackRecipeInput;
import net.minecraft.util.DyeColor;
import net.wurstclient.glass.MoGlassBlocks;

public final class MoGlassTestClient implements ClientModInitializer
{
Expand Down Expand Up @@ -130,8 +113,8 @@ private void runTests()
clearChat();

// Test Mo Glass features
testItemNamesShowUpCorrectly();
testRecipesWork();
ItemNamesTest.testItemNamesShowUpCorrectly();
RecipesTest.testRecipesWork();

System.out.println("Opening game menu");
openGameMenu();
Expand All @@ -144,162 +127,4 @@ private void runTests()
System.out.println("Stopping the game");
clickButton("menu.quit");
}

private void testItemNamesShowUpCorrectly()
{
assertItemName("Glass Slab", new ItemStack(MoGlassBlocks.GLASS_SLAB));
assertItemName("Glass Stairs",
new ItemStack(MoGlassBlocks.GLASS_STAIRS));

assertItemName("Tinted Glass Slab",
new ItemStack(MoGlassBlocks.TINTED_GLASS_SLAB));
assertItemName("Tinted Glass Stairs",
new ItemStack(MoGlassBlocks.TINTED_GLASS_STAIRS));

String[] colors = {"White", "Orange", "Magenta", "Light Blue", "Yellow",
"Lime", "Pink", "Gray", "Light Gray", "Cyan", "Purple", "Blue",
"Brown", "Green", "Red", "Black"};
for(int i = 0; i < colors.length; i++)
{
assertItemName(colors[i] + " Stained Glass Slab",
new ItemStack(MoGlassBlocks.STAINED_GLASS_SLABS.get(i)));
assertItemName(colors[i] + " Stained Glass Stairs",
new ItemStack(MoGlassBlocks.STAINED_GLASS_STAIRS.get(i)));
}
}

private void assertItemName(String expected, ItemStack stack)
{
if(!expected.equals(I18n.translate(stack.getName().getString())))
throw new RuntimeException("Wrong item name: Expected <" + expected
+ "> but got <" + stack.getName() + ">");
}

private void testRecipesWork()
{
// normal glass
testRecipesForGlassType(Blocks.GLASS, MoGlassBlocks.GLASS_SLAB,
MoGlassBlocks.GLASS_STAIRS);

// tinted glass
testRecipesForGlassType(Blocks.TINTED_GLASS,
MoGlassBlocks.TINTED_GLASS_SLAB, MoGlassBlocks.TINTED_GLASS_STAIRS);

// stained glass
for(DyeColor color : DyeColor.values())
{
Block block = getStainedGlassBlock(color);
Block slab = MoGlassBlocks.STAINED_GLASS_SLABS.get(color.ordinal());
Block stairs =
MoGlassBlocks.STAINED_GLASS_STAIRS.get(color.ordinal());
testRecipesForGlassType(block, slab, stairs);
}
}

private void testRecipesForGlassType(ItemConvertible input,
ItemConvertible slabOutput, ItemConvertible stairsOutput)
{
// slab crafting
assertCraftingRecipe(new ItemStack[][]{
{new ItemStack(input), new ItemStack(input), new ItemStack(input)}},
new ItemStack(slabOutput, 6));

// stairs crafting
assertCraftingRecipe(new ItemStack[][]{
{new ItemStack(input), null, null},
{new ItemStack(input), new ItemStack(input), null},
{new ItemStack(input), new ItemStack(input), new ItemStack(input)}},
new ItemStack(stairsOutput, 4));

// slab stonecutting
assertStonecuttingRecipe(new ItemStack(input),
new ItemStack(slabOutput, 2));

// stairs stonecutting
assertStonecuttingRecipe(new ItemStack(input),
new ItemStack(stairsOutput, 1));
}

private void assertCraftingRecipe(ItemStack[][] inputGrid,
ItemStack expectedResult)
{
int width = inputGrid[0].length;
int height = inputGrid.length;

ArrayList<ItemStack> stacks = new ArrayList<>();
for(ItemStack[] row : inputGrid)
for(ItemStack item : row)
stacks.add(item != null ? item : ItemStack.EMPTY);

CraftingRecipeInput input =
CraftingRecipeInput.createPositioned(width, height, stacks).input();
Optional<RecipeEntry<CraftingRecipe>> optional =
submitAndGet(mc -> mc.getServer().getRecipeManager()
.getFirstMatch(RecipeType.CRAFTING, input, mc.world));

if(!optional.isPresent())
throw new RuntimeException(
"No crafting recipe found for " + expectedResult);

RecipeEntry<CraftingRecipe> entry = optional.get();
CraftingRecipe recipe = entry.value();
ItemStack result = submitAndGet(
mc -> recipe.craft(input, mc.world.getRegistryManager()));

if(!ItemStack.areEqual(expectedResult, result))
throw new RuntimeException("Wrong crafting result: Expected "
+ expectedResult + " but got " + result);
}

private void assertStonecuttingRecipe(ItemStack input,
ItemStack expectedResult)
{
CuttingRecipeDisplay.Grouping<StonecuttingRecipe> recipeGroups =
submitAndGet(mc -> mc.getServer().getRecipeManager()
.getStonecutterRecipes().filter(input));

List<StonecuttingRecipe> recipes = recipeGroups.entries().stream()
.map(group -> group.recipe().recipe()).filter(Optional::isPresent)
.map(Optional::get).map(entry -> entry.value()).toList();

if(recipes.isEmpty())
throw new RuntimeException(
"No stonecutting recipes found for " + input);

List<ItemStack> results = submitAndGet(mc -> recipes.stream()
.map(recipe -> recipe.craft(new SingleStackRecipeInput(input),
mc.world.getRegistryManager()))
.toList());

if(!results.stream()
.anyMatch(stack -> ItemStack.areEqual(stack, expectedResult)))
throw new RuntimeException("No stonecutting recipe found for "
+ input + " -> " + expectedResult
+ ", only found recipes that result in " + String.join(", ",
results.stream().map(ItemStack::toString).toList()));
}

// As of 1.21.4, vanilla Minecraft doesn't seem to have a method like this.
private Block getStainedGlassBlock(DyeColor color)
{
return switch(color)
{
case WHITE -> Blocks.WHITE_STAINED_GLASS;
case ORANGE -> Blocks.ORANGE_STAINED_GLASS;
case MAGENTA -> Blocks.MAGENTA_STAINED_GLASS;
case LIGHT_BLUE -> Blocks.LIGHT_BLUE_STAINED_GLASS;
case YELLOW -> Blocks.YELLOW_STAINED_GLASS;
case LIME -> Blocks.LIME_STAINED_GLASS;
case PINK -> Blocks.PINK_STAINED_GLASS;
case GRAY -> Blocks.GRAY_STAINED_GLASS;
case LIGHT_GRAY -> Blocks.LIGHT_GRAY_STAINED_GLASS;
case CYAN -> Blocks.CYAN_STAINED_GLASS;
case PURPLE -> Blocks.PURPLE_STAINED_GLASS;
case BLUE -> Blocks.BLUE_STAINED_GLASS;
case BROWN -> Blocks.BROWN_STAINED_GLASS;
case GREEN -> Blocks.GREEN_STAINED_GLASS;
case RED -> Blocks.RED_STAINED_GLASS;
case BLACK -> Blocks.BLACK_STAINED_GLASS;
};
}
}
Loading

0 comments on commit 11dc331

Please sign in to comment.