Fixed a dupe issue with crafting stations

This commit is contained in:
Jules 2024-05-20 22:53:28 -07:00
parent 159031c8ec
commit 263a9335a1
3 changed files with 26 additions and 28 deletions

View File

@ -6,13 +6,13 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Iterator;
import java.util.Set;
import java.util.List;
public class CheckedIngredient {
@NotNull
private final Ingredient ingredient;
@Nullable
private final Set<PlayerIngredient> found;
private final List<PlayerIngredient> found;
private final boolean isHad;
/**
@ -25,7 +25,7 @@ public class CheckedIngredient {
* @param found The corresponding ingredient found in the player's
* ingredient
*/
public CheckedIngredient(@NotNull Ingredient ingredient, @Nullable Set<PlayerIngredient> found) {
public CheckedIngredient(@NotNull Ingredient ingredient, @NotNull List<PlayerIngredient> found) {
this.ingredient = ingredient;
this.found = found;
this.isHad = getTotalAmount() >= ingredient.getAmount();
@ -84,7 +84,7 @@ public class CheckedIngredient {
}
@Nullable
public Set<PlayerIngredient> getFound() {
public List<PlayerIngredient> getFound() {
return found;
}

View File

@ -13,13 +13,13 @@ import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class IngredientInventory {
private final Map<String, Set<PlayerIngredient>> ingredients = new HashMap<>();
private final Map<String, List<PlayerIngredient>> ingredients = new HashMap<>();
/**
* Loads all the possible crafting station ingredients from a player's inventory
@ -52,23 +52,14 @@ public class IngredientInventory {
* @param ingredient The type of the ingredient added
*/
public void addIngredient(NBTItem item, IngredientType ingredient) {
String key = ingredient.getId();
// Add to existing set
if (ingredients.containsKey(key))
ingredients.get(key).add(ingredient.readPlayerIngredient(item));
// Initialize
else {
Set<PlayerIngredient> ingredients = new HashSet<>();
ingredients.add(ingredient.readPlayerIngredient(item));
this.ingredients.put(key, ingredients);
}
final String key = ingredient.getId();
final List<PlayerIngredient> ingredients = this.ingredients.computeIfAbsent(key, ignored -> new ArrayList<>());
ingredients.add(ingredient.readPlayerIngredient(item));
}
@Nullable
public CheckedIngredient findMatching(@NotNull Ingredient ingredient) {
Set<PlayerIngredient> found = new HashSet<>();
List<PlayerIngredient> found = new ArrayList<>();
if (!ingredients.containsKey(ingredient.getId()))
return new CheckedIngredient(ingredient, found);

View File

@ -29,6 +29,8 @@ import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.UUID;
@ -101,11 +103,11 @@ public class CraftingStationView extends PluginInventory {
public void run() {
/*
* easier than caching a boolean and changing its state when
* Easier than caching a boolean and changing its state when
* closing or opening inventories which is glitchy when just
* updating them.
*/
if (inv.getViewers().size() < 1) {
if (inv.getViewers().isEmpty()) {
cancel();
return;
}
@ -162,19 +164,23 @@ public class CraftingStationView extends PluginInventory {
return;
}
NBTItem item = MythicLib.plugin.getVersion().getWrapper().getNBTItem(event.getCurrentItem());
final NBTItem item = MythicLib.plugin.getVersion().getWrapper().getNBTItem(event.getCurrentItem());
String tag = item.getString("recipeId");
if (!tag.isEmpty()) {
CheckedRecipe recipe = getRecipe(tag);
if (event.isRightClick())
new CraftingStationPreview(this, recipe).open();
// First re-update the player's inventory
// to avoid duplication glitches
updateData();
final CheckedRecipe recipe = getRecipe(tag);
if (event.isRightClick()) new CraftingStationPreview(this, recipe).open();
else {
processRecipe(recipe);
open();
}
}
if (!(tag = item.getString("queueId")).isEmpty()) {
else if (!(tag = item.getString("queueId")).isEmpty()) {
QueueItem recipeInfo = playerData.getCrafting().getQueue(station).getCraft(UUID.fromString(tag));
CraftingRecipe recipe = recipeInfo.getRecipe();
@ -264,7 +270,8 @@ public class CraftingStationView extends PluginInventory {
}
}
private CheckedRecipe getRecipe(String id) {
@Nullable
private CheckedRecipe getRecipe(@NotNull String id) {
for (CheckedRecipe info : recipes)
if (info.getRecipe().getId().equals(id))
return info;