Skip to content

Commit

Permalink
Add grouping strategies, add legacy console sounds, fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
FoundationGames committed Oct 1, 2023
1 parent b670955 commit 9fa6a98
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 21 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ repositories {
}

dependencies {
implementation 'org.jetbrains:annotations:23.0.0'
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import io.github.foundationgames.controllerexpansion.screen.widget.RecipeCatalogWidget;
import io.github.foundationgames.controllerexpansion.util.CEUtil;
import io.github.foundationgames.controllerexpansion.util.SlotPositionRemapper;
import io.github.foundationgames.controllerexpansion.util.crafting.ItemCategory;
import io.github.foundationgames.controllerexpansion.util.menu.ItemCategory;
import net.minecraft.client.gui.navigation.GuiNavigation;
import net.minecraft.client.gui.navigation.GuiNavigationPath;
import net.minecraft.client.gui.navigation.NavigationDirection;
Expand All @@ -23,7 +23,6 @@
import net.minecraft.screen.CraftingScreenHandler;
import net.minecraft.screen.PlayerScreenHandler;
import net.minecraft.screen.slot.Slot;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -79,8 +78,8 @@ protected void init() {

this.addSelectableChild(this.recipeCatalog);

int buttonX = this.x + (this.backgroundWidth - ItemCategory.CATEGORIES.size() * 33) / 2;
for (var category : ItemCategory.CATEGORIES) {
int buttonX = this.x + (this.backgroundWidth - ItemCategory.CRAFTING_CATEGORIES.size() * 33) / 2;
for (var category : ItemCategory.CRAFTING_CATEGORIES) {
this.addDrawableChild(new ItemCategoryButtonWidget(buttonX, this.y - 24, category, this.recipeCatalog));
buttonX += 33;
}
Expand Down Expand Up @@ -138,7 +137,6 @@ private void controllerTick() {
var sounds = this.client.getSoundManager();
if (!this.controllerRequestingCraft && this.controller.getRequestingPress()) {
if (this.recipeCatalog.craft()) {
CEUtil.playUI(sounds, SoundEvents.UI_BUTTON_CLICK.value(), 1);
CEUtil.playUI(sounds, ControllerExpansion.UI_CRAFT, 1);
} else {
CEUtil.playUI(sounds, ControllerExpansion.UI_DENY, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.mojang.blaze3d.systems.RenderSystem;
import io.github.foundationgames.controllerexpansion.screen.ControllerCraftingScreen;
import io.github.foundationgames.controllerexpansion.util.crafting.ItemCategory;
import io.github.foundationgames.controllerexpansion.util.menu.ItemCategory;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder;
import net.minecraft.client.gui.widget.ClickableWidget;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import com.google.common.collect.Lists;
import com.mojang.blaze3d.systems.RenderSystem;
import io.github.foundationgames.controllerexpansion.screen.ControllerCraftingScreen;
import io.github.foundationgames.controllerexpansion.util.crafting.ItemCategory;
import io.github.foundationgames.controllerexpansion.util.menu.GroupingStrategy;
import io.github.foundationgames.controllerexpansion.util.menu.ItemCategory;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.*;
import net.minecraft.client.gui.navigation.NavigationDirection;
Expand All @@ -30,7 +31,6 @@
import org.jetbrains.annotations.Nullable;
import org.lwjgl.glfw.GLFW;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -82,7 +82,15 @@ public void open(ItemCategory category, boolean resetSelect) {
this.screenHandler.populateRecipeFinder(this.recipeMatcher);
this.client.player.getInventory().populateRecipeFinder(this.recipeMatcher);

var results = Lists.newArrayList(this.recipes.getResultsForGroup(RecipeBookGroup.CRAFTING_SEARCH));
var ungrouped = Lists.newArrayList(this.recipes.getResultsForGroup(RecipeBookGroup.CRAFTING_SEARCH));
final var drm = this.client.world.getRegistryManager();

var results = GroupingStrategy.group(ungrouped).stream().map(list -> {
var recipe = new RecipeResultCollection(drm, list);
recipe.initialize(this.recipes);
return recipe;
}).collect(Collectors.toList());

results.forEach(res -> res.computeCraftables(this.recipeMatcher, this.screenHandler.getCraftingWidth(), this.screenHandler.getCraftingHeight(), this.recipes));
results.removeIf(res ->
!res.isInitialized() ||
Expand Down Expand Up @@ -291,9 +299,9 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
}

public void changeCategory(int by) {
int idx = ItemCategory.CATEGORIES.indexOf(this.currentCategory);
idx = MathHelper.floorMod(idx + by, ItemCategory.CATEGORIES.size());
this.open(ItemCategory.CATEGORIES.get(idx), true);
int idx = ItemCategory.CRAFTING_CATEGORIES.indexOf(this.currentCategory);
idx = MathHelper.floorMod(idx + by, ItemCategory.CRAFTING_CATEGORIES.size());
this.open(ItemCategory.CRAFTING_CATEGORIES.get(idx), true);
}

public boolean navigate(NavigationDirection nav) {
Expand Down Expand Up @@ -477,12 +485,15 @@ protected void renderRecipeIcon(MatrixStack matrices, Recipe<?> recipe, int x, i
if (this.allRecipes.stream().anyMatch(re -> re != recipe && re.getOutput(this.results.getRegistryManager()).isItemEqual(output))) {
var ingredients = recipe.getIngredients();
var stacks = ingredients.get((int)((this.time / 60) % ingredients.size())).getMatchingStacks();
var ingredient = stacks[(int)((this.time / 20) % stacks.length)];

matrices.push();
matrices.scale(0.5f, 0.5f, 0.5f);
client.getItemRenderer().renderInGui(matrices, ingredient, (x * 2) - 1, (y * 2));
matrices.pop();
if (stacks.length > 0) {
var ingredient = stacks[(int)((this.time / 20) % stacks.length)];

matrices.push();
matrices.scale(0.5f, 0.5f, 0.5f);
client.getItemRenderer().renderInGui(matrices, ingredient, (x * 2) - 1, (y * 2));
matrices.pop();
}
}

client.getItemRenderer().renderInGui(matrices, output, x, y);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package io.github.foundationgames.controllerexpansion.util.menu;

import com.google.common.collect.ImmutableList;
import net.minecraft.block.*;
import net.minecraft.client.gui.screen.recipebook.RecipeResultCollection;
import net.minecraft.item.ArmorItem;
import net.minecraft.item.BannerPatternItem;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.ToolItem;
import net.minecraft.recipe.Recipe;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.Predicate;

public record GroupingStrategy(Predicate<ItemStack> applies, BiPredicate<ItemStack, ItemStack> shouldGroup) {
// Functional Items
public static final GroupingStrategy TOOL_TYPE = new GroupingStrategy(i -> i.getItem() instanceof ToolItem, GroupingStrategy::sameItemClass);
public static final GroupingStrategy ARMOR_TYPE = new GroupingStrategy(i -> i.getItem() instanceof ArmorItem,
(a, b) -> {
if (a.getItem() instanceof ArmorItem ai && b.getItem() instanceof ArmorItem bi) {
return ai.getType().equals(bi.getType());
}
return false;
});
public static final GroupingStrategy BANNER_PATTERN = new GroupingStrategy(i -> i.getItem() instanceof BannerPatternItem, GroupingStrategy::always);
public static final GroupingStrategy UNPACK_RESOURCE_BLOCK = GroupingStrategy.makeSingleWhitelistGroup(Items.COAL, Items.IRON_INGOT,
Items.COPPER_INGOT, Items.GOLD_INGOT, Items.DIAMOND, Items.LAPIS_LAZULI, Items.EMERALD, Items.NETHERITE_INGOT);
public static final GroupingStrategy UNPACK_RAW_BLOCK = GroupingStrategy.makeSingleWhitelistGroup(Items.RAW_COPPER, Items.RAW_IRON, Items.RAW_GOLD);

// Blocks
public static final GroupingStrategy FURNACES = new GroupingStrategy(i ->
i.getItem() instanceof BlockItem block && block.getBlock() instanceof AbstractFurnaceBlock, GroupingStrategy::always);
public static final GroupingStrategy WORKSTATIONS = new GroupingStrategy(GroupingStrategy::workstation, GroupingStrategy::always);
public static final GroupingStrategy STORAGE = new GroupingStrategy(GroupingStrategy::storage, GroupingStrategy::always);
public static final GroupingStrategy PACK_RESOURCE_BLOCK = GroupingStrategy.makeSingleWhitelistGroup(Items.COAL_BLOCK, Items.IRON_BLOCK,
Items.COPPER_BLOCK, Items.GOLD_BLOCK, Items.DIAMOND_BLOCK, Items.LAPIS_BLOCK, Items.EMERALD_BLOCK, Items.NETHERITE_BLOCK);
public static final GroupingStrategy PACK_RAW_BLOCK = GroupingStrategy.makeSingleWhitelistGroup(Items.RAW_COPPER_BLOCK, Items.RAW_IRON_BLOCK, Items.RAW_GOLD_BLOCK);

public static final List<GroupingStrategy> STRATEGIES = List.of(TOOL_TYPE, ARMOR_TYPE, BANNER_PATTERN, UNPACK_RESOURCE_BLOCK, UNPACK_RAW_BLOCK,
FURNACES, WORKSTATIONS, STORAGE, PACK_RESOURCE_BLOCK, PACK_RAW_BLOCK);


public static GroupingStrategy makeSingleWhitelistGroup(Item ... items) {
final var itemList = ImmutableList.copyOf(items);

return new GroupingStrategy(i -> itemList.contains(i.getItem()), GroupingStrategy::always);
}

public static boolean workstation(ItemStack i) {
if (i.getItem() instanceof BlockItem item) {
var block = item.getBlock();

return block instanceof CraftingTableBlock ||
block instanceof CartographyTableBlock ||
block instanceof LoomBlock ||
block instanceof StonecutterBlock;
}

return false;
}

public static boolean storage(ItemStack i) {
if (i.getItem() instanceof BlockItem item) {
var block = item.getBlock();

return block instanceof ChestBlock ||
block instanceof BarrelBlock ||
block instanceof ShulkerBoxBlock;
}

return false;
}

public static boolean always(ItemStack a, ItemStack b) {
return true;
}

public static boolean sameItemClass(ItemStack a, ItemStack b) {
return a.getItem().getClass().equals(b.getItem().getClass());
}

public static List<List<Recipe<?>>> group(List<RecipeResultCollection> list) {
List<List<Recipe<?>>> groups = new ArrayList<>();

for (var coll : list) {
var recipes = coll.getAllRecipes();
nextInputGroup:
for (var recipe : recipes) {
var drm = coll.getRegistryManager();
var recipeOutput = recipe.getOutput(drm);

for (var strategy : STRATEGIES) {
if (strategy.applies().test(recipe.getOutput(drm))) {
boolean foundGroup = false;
for (var group : groups) {
var groupOutput = group.get(0).getOutput(drm);
if (strategy.applies().test(groupOutput) && strategy.shouldGroup().test(recipeOutput, groupOutput)) {
group.addAll(recipes);
foundGroup = true;
break;
}
}

if (!foundGroup) {
groups.add(new ArrayList<>(recipes));
}

break nextInputGroup;
}
}

groups.add(new ArrayList<>(recipes));
break;
}
}

return groups;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package io.github.foundationgames.controllerexpansion.util.crafting;
package io.github.foundationgames.controllerexpansion.util.menu;

import net.minecraft.block.Blocks;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.*;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.text.Text;

import java.util.List;
Expand All @@ -28,7 +27,7 @@ public record ItemCategory(Predicate<ItemStack> filter, String name, ItemStack..
public static final ItemCategory FOOD_DRINK = new ItemCategory(item -> inAnyOf(item, ItemGroups.FOOD_AND_DRINK),
"food_drink", stack(Items.CAKE), stack(Items.APPLE), stack(Items.BREAD));

public static final List<ItemCategory> CATEGORIES =
public static final List<ItemCategory> CRAFTING_CATEGORIES =
List.of(CONSTRUCTION, NATURE_DECOR, UTILITIES_COMBAT, RESOURCES, REDSTONE, FOOD_DRINK, ALL);

public void drawIcon(MatrixStack matrices, MinecraftClient client, int x, int y) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/assets/controllerexpansion/sounds.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
]
},
"ui.craft": {
"sounds": ["random/pop"]
"sounds": ["random/click_stereo"]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@
"ui.deny": {
"replace": true,
"sounds": ["controllerexpansion:ui/deny"]
},
"ui.craft": {
"replace": true,
"sounds": ["random/pop"]
}
}
Binary file not shown.

0 comments on commit 9fa6a98

Please sign in to comment.