Skip to content

Commit

Permalink
Populate the other sections
Browse files Browse the repository at this point in the history
  • Loading branch information
paulyhedral committed Nov 21, 2024
1 parent e641259 commit f43c12e
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@

public class CraftQueueOverlay {

static int TITLE_COLOR = 0x99999999;
static int SECTION_COLOR = 0xcccccccc;
static int TEXT_COLOR = 0xffffffff;
static int MESSAGE_COLOR = 0x66666666;
static int SECTION_X_OFFSET = 4;
static int SECTION_TITLE_Y_OFFSET = 18;
static int SECTION_TITLE_Y_OFFSET = 20;
static int ITEM_NAME_X_OFFSET = 22;
static int LINE_HEIGHT = 8;
static int MAX_STRING_LENGTH = 16;

static int LINE_HEIGHT = 16;
static int TEXT_HEIGHT = 12;
static int MAX_STRING_LENGTH = 40;

public static final IIngameOverlay CRAFT_QUEUE = (gui, poseStack, partialTicks, width, height) -> {
CraftTracker.LOGGER.trace("CRAFT_QUEUE");
Expand All @@ -40,7 +43,7 @@ public class CraftQueueOverlay {

GuiComponent.drawCenteredString(poseStack, gui.getFont(),
new TranslatableComponent(Constants.TRANSLATION_KEY_GUI_CRAFTLIST_TITLE),
(x + olWidth - 8) / 2, y + 6, TEXT_COLOR);
(x + olWidth - 8) / 2, y + 6, TITLE_COLOR);

var mgr = CraftingQueueManager.INSTANCE;
var products = mgr.getEndProducts();
Expand All @@ -49,48 +52,119 @@ public class CraftQueueOverlay {
if(products.isEmpty()) { // TODO
GuiComponent.drawCenteredString(poseStack, gui.getFont(),
new TranslatableComponent(Constants.TRANSLATION_KEY_GUI_CRAFTLIST_EMPTY),
(x + olWidth - 8) / 2, (y + olHeight - 6) / 2, TEXT_COLOR);
(x + olWidth - 8) / 2, (y + olHeight - 6) / 2, MESSAGE_COLOR);
return;
}

// end products
int yPos = y + SECTION_TITLE_Y_OFFSET;
CraftTracker.LOGGER.debug("yPos (initial): {}", yPos);

// SECTION: end products

// title
GuiComponent.drawString(poseStack, gui.getFont(),
new TranslatableComponent(Constants.TRANSLATION_KEY_GUI_CRAFTLIST_SECTION_PRODUCTS),
x + SECTION_X_OFFSET, y + SECTION_TITLE_Y_OFFSET, TEXT_COLOR);

int yPos = y + (LINE_HEIGHT * 2) + 2;
x + SECTION_X_OFFSET, yPos, SECTION_COLOR);
yPos += TEXT_HEIGHT + 2;
CraftTracker.LOGGER.debug("yPos (after product title): {}", yPos);

// items
for(var p : products) {
var index = products.indexOf(p);
for(int i = 0; i < products.size(); i++) {
var p = products.get(i);
// var index = products.indexOf(p);

var item = ForgeRegistries.ITEMS.getValue(p.getItemId());
// var image = item.getDefaultInstance().getTooltipImage();

yPos += (index * LINE_HEIGHT);

var stack = item.getDefaultInstance();
stack.setCount(p.getQuantity());
// TODO: count overlay on icon is amount produced by recipe
// stack.setCount(stack.getCount());
var drawable = CTPlugin.jeiRuntime.getJeiHelpers().getGuiHelper()
.createDrawableIngredient(VanillaTypes.ITEM_STACK, stack);
drawable.draw(poseStack, x + SECTION_X_OFFSET, yPos);
// GuiComponent.blit(poseStack, 2, index * 16, 0, 16, 16, image);
GuiComponent.drawString(poseStack, gui.getFont(), item.getDescription().getString(MAX_STRING_LENGTH), x + ITEM_NAME_X_OFFSET, yPos + 2, TEXT_COLOR);
var text = String.format("%s (x%d)", item.getDescription().getString(MAX_STRING_LENGTH), p.getQuantity());
GuiComponent.drawString(poseStack, gui.getFont(), text, x + ITEM_NAME_X_OFFSET, yPos + 4, TEXT_COLOR);

yPos += LINE_HEIGHT + 2;
CraftTracker.LOGGER.debug("yPos (product item {}): {}", i, yPos);
}

// intermediates
// TODO: title
// TODO: items
// SECTION: intermediates
if(!mgr.getIntermediates().isEmpty()) {
yPos += (TEXT_HEIGHT * 2);
CraftTracker.LOGGER.debug("yPos (before intermediates title): {}", yPos);

// title
GuiComponent.drawString(poseStack, gui.getFont(),
new TranslatableComponent(Constants.TRANSLATION_KEY_GUI_CRAFTLIST_SECTION_INTERMEDIATES),
x + SECTION_X_OFFSET, yPos, SECTION_COLOR);
yPos += TEXT_HEIGHT + 2;
CraftTracker.LOGGER.debug("yPos (after intermediates title): {}", yPos);

// items
for(int i = 0; i < mgr.getIntermediates().size(); i++) {
var inter = mgr.getIntermediates().get(i);
// var index = mgr.getIntermediates().indexOf(i);

var item = ForgeRegistries.ITEMS.getValue(inter.getItemId());
var stack = item.getDefaultInstance();
stack.setCount(inter.getQuantity());
var drawable = CTPlugin.jeiRuntime.getJeiHelpers().getGuiHelper()
.createDrawableIngredient(VanillaTypes.ITEM_STACK, stack);
drawable.draw(poseStack, x + SECTION_X_OFFSET, yPos);
GuiComponent.drawString(poseStack, gui.getFont(), item.getDescription().getString(MAX_STRING_LENGTH), x + ITEM_NAME_X_OFFSET, yPos + 4, TEXT_COLOR);

yPos += LINE_HEIGHT + 2;
CraftTracker.LOGGER.debug("yPos (intermediates item {}): {}", i, yPos);
}
}

// raw materials
// TODO: title
// TODO: items
// SECTION: raw materials
if(!mgr.getRawMaterials().isEmpty()) {
yPos += (TEXT_HEIGHT * 2);
CraftTracker.LOGGER.debug("yPos (before materials title): {}", yPos);

// title
GuiComponent.drawString(poseStack, gui.getFont(),
new TranslatableComponent(Constants.TRANSLATION_KEY_GUI_CRAFTLIST_SECTION_MATERIALS),
x + SECTION_X_OFFSET, yPos, SECTION_COLOR);
yPos += TEXT_HEIGHT + 2;
CraftTracker.LOGGER.debug("yPos (after materials title): {}", yPos);

// items
for(int i = 0; i < mgr.getRawMaterials().size(); i++) {
var m = mgr.getRawMaterials().get(i);
// var index = mgr.getRawMaterials().indexOf(m);

var item = ForgeRegistries.ITEMS.getValue(m.getItemId());
var stack = item.getDefaultInstance();
stack.setCount(m.getQuantity());
var drawable = CTPlugin.jeiRuntime.getJeiHelpers().getGuiHelper()
.createDrawableIngredient(VanillaTypes.ITEM_STACK, stack);
drawable.draw(poseStack, x + SECTION_X_OFFSET, yPos);
GuiComponent.drawString(poseStack, gui.getFont(), item.getDescription().getString(MAX_STRING_LENGTH), x + ITEM_NAME_X_OFFSET, yPos + 4, TEXT_COLOR);

yPos += LINE_HEIGHT + 2;
CraftTracker.LOGGER.debug("yPos (materials item {}): {}", i, yPos);
}
}

// SECTION: fuel

if(!mgr.getFuel().isEmpty()) {
yPos += (TEXT_HEIGHT * 2);
CraftTracker.LOGGER.debug("yPos (before fuel title): {}", yPos);

// fuel
// TODO: title
// TODO: items
// title
GuiComponent.drawString(poseStack, gui.getFont(),
new TranslatableComponent(Constants.TRANSLATION_KEY_GUI_CRAFTLIST_SECTION_FUEL),
x + SECTION_X_OFFSET, yPos, SECTION_COLOR);
yPos += TEXT_HEIGHT + 2;
CraftTracker.LOGGER.debug("yPos: {}", yPos);

// items
for(var f : mgr.getFuel()) {

}
}
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,28 @@
public class ShoppingListOverlay {

public static final IIngameOverlay SHOPPING_LIST = (gui, poseStack, partialTicks, width, height) -> {
if(ConfigHandler.CLIENT.SHOPPING_LIST_OVERLAY_HIDE_EMPTY.get() /* TODO: || user wants it to display */) {
// return;
}

var x = ConfigHandler.CLIENT.SHOPPING_LIST_OVERLAY_X.get();
if(x == 0) {
x = ConfigHandler.CLIENT.CRAFT_QUEUE_OVERLAY_X.get();
}
var y = ConfigHandler.CLIENT.SHOPPING_LIST_OVERLAY_Y.get();
if(y == 0) {
y = ConfigHandler.CLIENT.CRAFT_QUEUE_OVERLAY_Y.get() + ConfigHandler.CLIENT.CRAFT_QUEUE_OVERLAY_HEIGHT.get() + 10;
}
var olWidth = Math.min((ConfigHandler.CLIENT.SHOPPING_LIST_OVERLAY_X.get() + ConfigHandler.CLIENT.SHOPPING_LIST_OVERLAY_WIDTH.get()), width - 10);
var olHeight = Math.min((ConfigHandler.CLIENT.SHOPPING_LIST_OVERLAY_Y.get() + ConfigHandler.CLIENT.SHOPPING_LIST_OVERLAY_HEIGHT.get()), height - 10);

GuiComponent.fill(poseStack, x, y, olWidth, olHeight, 0x1f1f1f1f);
GuiComponent.fill(poseStack, x + 2, y + 2, olWidth - 2, olHeight - 2, 0x5f5f5f5f);

GuiComponent.drawCenteredString(poseStack, gui.getFont(),
new TranslatableComponent(Constants.TRANSLATION_KEY_GUI_SHOPPINGLIST_TITLE),
(x + olWidth - 8) / 2, y + 6, 0xffffffff);
// if(ConfigHandler.CLIENT.SHOPPING_LIST_OVERLAY_HIDE_EMPTY.get() /* TODO: || user wants it to display */) {
//// return;
// }
//
// var x = ConfigHandler.CLIENT.SHOPPING_LIST_OVERLAY_X.get();
// if(x == 0) {
// x = ConfigHandler.CLIENT.CRAFT_QUEUE_OVERLAY_X.get();
// }
// var y = ConfigHandler.CLIENT.SHOPPING_LIST_OVERLAY_Y.get();
// if(y == 0) {
// y = ConfigHandler.CLIENT.CRAFT_QUEUE_OVERLAY_Y.get() + ConfigHandler.CLIENT.CRAFT_QUEUE_OVERLAY_HEIGHT.get() + 10;
// }
// var olWidth = Math.min((ConfigHandler.CLIENT.SHOPPING_LIST_OVERLAY_X.get() + ConfigHandler.CLIENT.SHOPPING_LIST_OVERLAY_WIDTH.get()), width - 10);
// var olHeight = Math.min((ConfigHandler.CLIENT.SHOPPING_LIST_OVERLAY_Y.get() + ConfigHandler.CLIENT.SHOPPING_LIST_OVERLAY_HEIGHT.get()), height - 10);
//
// GuiComponent.fill(poseStack, x, y, olWidth, olHeight, 0x1f1f1f1f);
// GuiComponent.fill(poseStack, x + 2, y + 2, olWidth - 2, olHeight - 2, 0x5f5f5f5f);
//
// GuiComponent.drawCenteredString(poseStack, gui.getFont(),
// new TranslatableComponent(Constants.TRANSLATION_KEY_GUI_SHOPPINGLIST_TITLE),
// (x + olWidth - 8) / 2, y + 6, 0xffffffff);
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.sweetrpg.crafttracker.CraftTracker;
import com.sweetrpg.crafttracker.common.lib.Constants;
import com.sweetrpg.crafttracker.common.manager.CraftingQueueManager;
import net.minecraft.world.entity.Entity;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.player.PlayerEvent.PlayerLoggedInEvent;
Expand Down Expand Up @@ -33,7 +34,7 @@ public class EventHandler {
//
@SubscribeEvent
public void onEntitySpawn(final EntityJoinWorldEvent event) {
CraftTracker.LOGGER.debug("EventHandler#onEntitySpawn: {}", event);
CraftTracker.LOGGER.trace("EventHandler#onEntitySpawn: {}", event);

Entity entity = event.getEntity();

Expand All @@ -46,7 +47,7 @@ public void onEntitySpawn(final EntityJoinWorldEvent event) {
public void playerLoggedIn(final PlayerLoggedInEvent event) {
CraftTracker.LOGGER.debug("EventHandler#playerLoggedIn: {}", event);

// CraftingQueueManager.get(event.getPlayer().level);
CraftingQueueManager.INSTANCE.load(event.getPlayer());
}

// @SubscribeEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class Constants {
public static final String TRANSLATION_KEY_GUI_CRAFTLIST_SECTION_PRODUCTS = "crafttracker.screen.craft_list.section.products";
public static final String TRANSLATION_KEY_GUI_CRAFTLIST_SECTION_INTERMEDIATES = "crafttracker.screen.craft_list.section.intermediates";
public static final String TRANSLATION_KEY_GUI_CRAFTLIST_SECTION_MATERIALS = "crafttracker.screen.craft_list.section.materials";
public static final String TRANSLATION_KEY_GUI_CRAFTLIST_SECTION_FUEL = "crafttracker.screen.craft_list.section.fuel";
public static final String TRANSLATION_KEY_GUI_SHOPPINGLIST_TITLE = "crafttracker.screen.shopping_list.title";
public static final String TRANSLATION_KEY_BINDINGS_CATEGORY_TITLE = "key.categories.crafttracker";
public static final String TRANSLATION_KEY_BINDINGS_ADDTOQUEUE_TITLE = "key.addToQueue";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.sweetrpg.crafttracker.common.manager;

import com.sweetrpg.crafttracker.CraftTracker;
import com.sweetrpg.crafttracker.common.addon.jei.CTPlugin;
import com.sweetrpg.crafttracker.common.model.CraftingQueueProduct;
import com.sweetrpg.crafttracker.common.util.RecipeUtil;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -57,9 +58,8 @@ public void load(Player player) {

}

//// this.storage = new CraftingQueueStorage();
/// / this.storage = new CraftingQueueStorage();
// }

public List<ProductItem> getEndProducts() {
return endProducts.entrySet()
.stream()
Expand Down Expand Up @@ -135,36 +135,77 @@ public void removeProduct(Player player, ResourceLocation itemId, int quantity)
computeAll();
}

/**
* Compute all the intermediate items, raw materials, and fuel needed to make the recipes
*/
public void computeAll() {
CraftTracker.LOGGER.debug("CraftingQueueManager#computeAll");

Map<ResourceLocation, Integer> intermediateProducts = new HashMap<>();
Map<ResourceLocation, Integer> rawMaterials = new HashMap<>();

var rm = CTPlugin.jeiRuntime.getRecipeManager();

// RecipeManager rm = new RecipeManager(ICondition.IContext.EMPTY);
this.intermediateProducts.clear();
this.rawMaterials.clear();
this.fuel.clear();

this.endProducts.forEach((k, v) -> {

// rm.byKey(k).ifPresentOrElse(r -> {
// var ingredients = r.getIngredients();
//
// },
// () -> {
// // should not have gotten here, since #addProduct should have filtered out the item
// // since it had to ingredients
// CraftTracker.LOGGER.warn("#computeAll: no recipe found for {}", k);
// });

this.computeProduct(v);
});

this.intermediateProducts = intermediateProducts;
this.rawMaterials = rawMaterials;
}

public void computeIntermediates() {
public void computeProduct(CraftingQueueProduct product) {
CraftTracker.LOGGER.debug("CraftingQueueManager#computeProduct: {}", product);

var index = Math.min(product.getIndex(), product.getRecipes().size());
var recipe = product.getRecipes().get(index);

this.computeRecipe(recipe, product.getQuantity());
}

public void computeRecipe(Recipe recipe, int recipeQuantity) {
CraftTracker.LOGGER.debug("CraftingQueueManager#computeRecipe: {}", recipe);

var ingredients = recipe.getIngredients();
CraftTracker.LOGGER.debug("ingredients: {}", ingredients);

ingredients.stream()
.filter((i) -> i instanceof Ingredient)
.map((i) -> Ingredient.class.cast(i))
.forEach((i) -> {
CraftTracker.LOGGER.debug("i: {}", i);

if(i instanceof Ingredient ingredient) {
CraftTracker.LOGGER.debug("ingredient: {}", ingredient);

for(var item : ingredient.getItems()) {
CraftTracker.LOGGER.debug("item: {}", item);
var id = item.getItem().getRegistryName();
CraftTracker.LOGGER.debug("id: {}", id);
var subRecipes = RecipeUtil.getRecipesFor(id);
CraftTracker.LOGGER.debug("subRecipes: {}", subRecipes);
if(subRecipes.isEmpty()) {
// no recipes for this ingredient, so it's a raw material
this.rawMaterials.compute(id, (itemId, quantity) -> {
if(quantity == null) {
return item.getCount() * recipeQuantity;
}

return quantity + (item.getCount() * recipeQuantity);
});
}
else {
// intermediate
this.intermediateProducts.compute(id, (itemId, quantity) -> {
if(quantity == null) {
return item.getCount();
}

return quantity + item.getCount();
});

this.computeRecipe(subRecipes.get(0), recipeQuantity);
}
}
}
});
}

public static class ProductItem {
Expand Down
Loading

0 comments on commit f43c12e

Please sign in to comment.