Skip to content

Commit

Permalink
Update how crafting queue data is stored
Browse files Browse the repository at this point in the history
  • Loading branch information
paulyhedral committed Nov 19, 2024
1 parent e24519b commit 25f286e
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,100 @@

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.storage.CraftingQueueStorage;
import com.sweetrpg.crafttracker.common.util.RecipeUtil;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.crafting.CraftingRecipe;
import net.minecraft.world.level.Level;
import org.antlr.v4.misc.OrderedHashMap;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class CraftingQueueManager {

public static CraftingQueueManager INSTANCE = new CraftingQueueManager();

private Map<ResourceLocation, Integer> endProducts = new OrderedHashMap<>();
private Map<ResourceLocation, CraftingQueueProduct> endProducts = new OrderedHashMap<>();
private Map<ResourceLocation, Integer> intermediateProducts = new OrderedHashMap<>();
private Map<ResourceLocation, Integer> rawMaterials = new HashMap<>();

private CraftingQueueStorage storage;
// private CraftingQueueStorage storage;

public CraftingQueueManager() {
this.storage = new CraftingQueueStorage();
// this.storage = new CraftingQueueStorage();
}

public List<QueueItem> getEndProducts() {
return endProducts.entrySet()
.stream()
.map((e) -> new QueueItem(e.getKey(), e.getValue()))
.collect(Collectors.toUnmodifiableList());
}
// public List<QueueItem> getEndProducts() {
// return endProducts.entrySet()
// .stream()
// .map((e) -> new QueueItem(e.getKey(), e.getValue()))
// .collect(Collectors.toUnmodifiableList());
// }
//
// public List<QueueItem> getIntermediates() {
// return intermediateProducts.entrySet()
// .stream()
// .map((e) -> new QueueItem(e.getKey(), e.getValue()))
// .collect(Collectors.toUnmodifiableList());
// }
//
// public List<QueueItem> getRawMaterials() {
// return rawMaterials.entrySet()
// .stream()
// .map((e) -> new QueueItem(e.getKey(), e.getValue()))
// .collect(Collectors.toUnmodifiableList());
// }

public List<QueueItem> getIntermediates() {
return intermediateProducts.entrySet()
.stream()
.map((e) -> new QueueItem(e.getKey(), e.getValue()))
.collect(Collectors.toUnmodifiableList());
}
public void addProduct(Level level, ResourceLocation itemId, int quantity) {
CraftTracker.LOGGER.debug("CraftingQueueManager#addProduct: {}, quantity: {}", itemId, quantity);

public List<QueueItem> getRawMaterials() {
return rawMaterials.entrySet()
.stream()
.map((e) -> new QueueItem(e.getKey(), e.getValue()))
.collect(Collectors.toUnmodifiableList());
}
var recipes = RecipeUtil.getRecipesFor(itemId);

public void addProduct(ResourceLocation itemId, int quantity) {
CraftTracker.LOGGER.debug("#addProduct: {}, quantity: {}", itemId, quantity);
if(recipes.size() > 0) {
CraftTracker.LOGGER.debug("recipes: {}", recipes);

var rm = CTPlugin.jeiRuntime.getRecipeManager();
var product = new CraftingQueueProduct(itemId, recipes, quantity);
endProducts.compute(itemId, (rl, p) -> p == null ? product :
new CraftingQueueProduct(p.getItemId(), p.getRecipes(), p.getQuantity() + quantity));

rm.createRecipeCategoryLookup().get()
.peek(c -> CraftTracker.LOGGER.debug("category: {}", c))
.map(c -> c.getRecipeType())
.peek(t -> CraftTracker.LOGGER.debug("type: {}", t))
.flatMap(t -> rm.createRecipeLookup(t).get())
.peek(r -> CraftTracker.LOGGER.debug("recipe: {}", r))
.filter(r -> r instanceof CraftingRecipe)
.map(r -> CraftingRecipe.class.cast(r))
.peek(r -> CraftTracker.LOGGER.debug("CraftingRecipe: {}", r.getId()))
.filter(cr -> cr.getId().equals(itemId))
.peek(cr -> CraftTracker.LOGGER.debug("{}: {}", itemId, cr))
.findFirst()
.ifPresentOrElse(r -> {
CraftTracker.LOGGER.debug("r: {}", r);
endProducts.compute(itemId, (k, v) -> v == null ? quantity : v + quantity);
},
() -> {
CraftTracker.LOGGER.warn("No recipe found for {}", itemId);
});
CraftingQueueStorage.get(level).putData(itemId, quantity);

computeAll();
computeAll();
}
else {
CraftTracker.LOGGER.info("Not adding {} to queue, since there are no recipes for it.", itemId);
}
}

public void removeProduct(ResourceLocation itemId, int quantity) {
public void removeProduct(Level level, ResourceLocation itemId, int quantity) {
CraftTracker.LOGGER.debug("CraftingQueueManager#removeProduct: {}, quantity: {}", itemId, quantity);

var product = this.endProducts.get(itemId);
if(product == null) {
CraftTracker.LOGGER.info("No product found in queue for {}", itemId);
return;
}

// if we would remove more than what's left in the queue, remove it entirely
int newQuantity = product.getQuantity() - quantity;
if(newQuantity < 1) {
CraftTracker.LOGGER.info("Removing item from queue storage: {}", itemId);
CraftingQueueStorage.get(level).removeData(itemId);
}
else {
CraftTracker.LOGGER.info("Adjusting quantity of item in queue storage to {}: {}", quantity, itemId);
CraftingQueueStorage.get(level).putData(itemId, newQuantity);
}

computeAll();
}

public void computeAll() {
CraftTracker.LOGGER.debug("CraftingQueueManager#computeAll");

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.sweetrpg.crafttracker.common.model;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.crafting.Recipe;

import java.util.List;

public class CraftingQueueProduct {

ResourceLocation itemId;
List<Recipe> recipes;
int quantity;

public CraftingQueueProduct(ResourceLocation itemId, List<Recipe> recipes, int quantity) {
this.itemId = itemId;
this.recipes = recipes;
this.quantity = quantity;
}

public ResourceLocation getItemId() {
return itemId;
}

public void setItemId(ResourceLocation itemId) {
this.itemId = itemId;
}

public List<Recipe> getRecipes() {
return recipes;
}

public void setRecipes(List<Recipe> recipes) {
this.recipes = recipes;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
import com.sweetrpg.crafttracker.common.manager.CraftingQueueManager;
import com.sweetrpg.crafttracker.common.network.IPacket;
import com.sweetrpg.crafttracker.common.network.packet.data.AddToQueueData;
import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.ingredients.ITypedIngredient;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.network.NetworkEvent.Context;

import java.io.IOException;
import java.util.function.Supplier;

public class AddToQueuePacket implements IPacket<AddToQueueData> {
Expand All @@ -35,10 +32,10 @@ public final void handle(AddToQueueData data, Supplier<Context> ctx) {

ctx.get().enqueueWork(() -> {
LogicalSide side = ctx.get().getDirection().getReceptionSide();
if (side.isClient()) {
if(side.isClient()) {

}
else if (side.isServer()) {
else if(side.isServer()) {
CTPlugin.jeiRuntime.getIngredientListOverlay().getIngredientUnderMouse()
.ifPresent(ingredient -> {
CraftTracker.LOGGER.debug("AddToQueuePacket#handle: type {}", ingredient.getType());
Expand All @@ -48,9 +45,7 @@ else if (side.isServer()) {
ResourceLocation res = itemStack.getItem().getRegistryName();
CraftTracker.LOGGER.debug("AddToQueuePacket#handle: res {}", res);

CraftingQueueManager.INSTANCE.addProduct(res, 1);


CraftingQueueManager.INSTANCE.addProduct(ctx.get().getSender().level, res, 1);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,90 +3,75 @@
import com.sweetrpg.crafttracker.CraftTracker;
import com.sweetrpg.crafttracker.common.util.NBTUtil;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.phys.Vec3;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
* This class represents an entry in the crafting queue's end products list.
*/
public class CraftingQueueData {

private final CraftingQueueStorage storage;
private final UUID uuid;
private @Nullable UUID ownerId;
private Vec3 position;

private Map<ResourceLocation, Integer> endProducts;
private ResourceLocation itemId;
private int quantity;

protected CraftingQueueData(CraftingQueueStorage storage, UUID uuid) {
protected CraftingQueueData(CraftingQueueStorage storage) {
this.storage = storage;
this.uuid = uuid;
this.endProducts = new HashMap<>();
}

public void addItem(ResourceLocation itemId, int quantity) {
CraftTracker.LOGGER.debug("CraftingQueueData#addItem: {}, quantity {}", itemId, quantity);

this.endProducts.compute(itemId, (k, v) -> (v == null ? 0 : v) + quantity);

this.storage.setDirty();
protected CraftingQueueData(CraftingQueueStorage storage, ResourceLocation itemId, int quantity) {
this.storage = storage;
this.itemId = itemId;
this.quantity = quantity;
}

public void removeItem(ResourceLocation itemId, int quantity) {
CraftTracker.LOGGER.debug("CraftingQueueData#removeItem: {}, quantity {}", itemId, quantity);

this.endProducts.computeIfPresent(itemId, (k, v) -> {
if(v - quantity < 1) {
return null;
}

return v - quantity;
});

this.storage.setDirty();
}
// public void addItem(ResourceLocation itemId, int quantity) {
// CraftTracker.LOGGER.debug("CraftingQueueData#addItem: {}, quantity {}", itemId, quantity);
//
// this.endProducts.compute(itemId, (k, v) -> (v == null ? 0 : v) + quantity);
//
// this.storage.setDirty();
// }
//
// public void removeItem(ResourceLocation itemId, int quantity) {
// CraftTracker.LOGGER.debug("CraftingQueueData#removeItem: {}, quantity {}", itemId, quantity);
//
// this.endProducts.computeIfPresent(itemId, (k, v) -> {
// if(v - quantity < 1) {
// return null;
// }
//
// return v - quantity;
// });
//
// this.storage.setDirty();
// }

public void read(CompoundTag compound) {
CraftTracker.LOGGER.debug("CraftingQueueData#read");

this.ownerId = NBTUtil.getUniqueId(compound, "ownerId");
this.position = NBTUtil.getVector3d(compound);
this.endProducts.clear();

if(compound.contains("products", Tag.TAG_COMPOUND)) {
ListTag products = compound.getList("products", Tag.TAG_COMPOUND);
for(Tag p : products) {
if(p instanceof CompoundTag product) {
String itemId = product.getString("item_id");
ResourceLocation res = ResourceLocation.tryParse(itemId);
int quantity = product.getInt("quantity");
this.endProducts.put(res, quantity);
}
}
}
this.itemId = NBTUtil.getResourceLocation(compound, Keys.ITEM_ID);
this.quantity = compound.getInt(Keys.QUANTITY);
}

public CompoundTag write(CompoundTag compound) {
CraftTracker.LOGGER.debug("CraftingQueueData#write");

NBTUtil.putUniqueId(compound, "ownerId", this.ownerId);
NBTUtil.putVector3d(compound, this.position);
NBTUtil.putResourceLocation(compound, Keys.ITEM_ID, this.itemId);
compound.putInt(Keys.QUANTITY, this.quantity);

ListTag list = new ListTag();
for(Map.Entry<ResourceLocation, Integer> product : this.endProducts.entrySet()) {
CompoundTag tag = new CompoundTag();
return compound;
}

NBTUtil.putResourceLocation(tag, "item_id", product.getKey());
tag.putInt("item_id", product.getValue());
public ResourceLocation getItemId() {
return itemId;
}

list.add(tag);
}
compound.put("products", list);
public int getQuantity() {
return quantity;
}

return compound;
static class Keys {
static String ITEM_ID = "item_id";
static String QUANTITY = "quantity";
}
}
Loading

0 comments on commit 25f286e

Please sign in to comment.