Skip to content

Commit

Permalink
Merge pull request #37 from rowan-vr/35-fix-crafting-multiple-using-s…
Browse files Browse the repository at this point in the history
…hift

Fixed multiple crafting counted as one
  • Loading branch information
rowan-vr authored Jul 28, 2022
2 parents f25ec09 + ff40d8e commit d9d0415
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.inventory.CraftingInventory;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -21,8 +24,37 @@ public void onBlockPlace(final CraftItemEvent event) {

@Override protected void onProgress(final CraftItemEvent event, String value, final String path) {
val player = event.getView().getPlayer();

int recipeAmount = event.getRecipe().getResult().getAmount();

switch (event.getClick()) {
case NUMBER_KEY:
if (event.getWhoClicked().getInventory().getItem(event.getHotbarButton()) != null)
recipeAmount = 0;
break;
case DROP:
case CONTROL_DROP:
ItemStack cursor = event.getCursor();
if (cursor == null || cursor.getType() != Material.AIR)
recipeAmount = 0;
break;
case SHIFT_RIGHT:
case SHIFT_LEFT:
if (recipeAmount == 0)
break;

int maxCraftable = getMaxCraftAmount(event.getInventory());
int capacity = fits(event.getRecipe().getResult(), event.getView().getBottomInventory());
if (capacity < maxCraftable)
maxCraftable = ((capacity + recipeAmount - 1) / recipeAmount) * recipeAmount;

recipeAmount = maxCraftable;
break;
default:
}

if (value == null || value.equalsIgnoreCase("any")) {
progression(1, path, player.getUniqueId());
progression(recipeAmount, path, player.getUniqueId());
} else {
boolean not = false;
if (value.startsWith("!")) {
Expand All @@ -34,8 +66,35 @@ public void onBlockPlace(final CraftItemEvent event) {
for (final String materialString : materialStrings)
materials.add(Material.getMaterial(materialString.toUpperCase()));
if ((materials.contains(event.getRecipe().getResult().getType()) && !not) || (!materials.contains(event.getRecipe().getResult().getType()) && not)) {
progression(1, path, player.getUniqueId());
progression(recipeAmount, path, player.getUniqueId());
}
}
}

private static int getMaxCraftAmount(CraftingInventory inventory) {
if (inventory.getResult() == null)
return 0;

int resultCount = inventory.getResult().getAmount();
int materialCount = Integer.MAX_VALUE;

for (ItemStack item : inventory.getMatrix())
if (item != null && item.getAmount() < materialCount)
materialCount = item.getAmount();

return resultCount * materialCount;
}

private static int fits(ItemStack item, Inventory inventory) {
ItemStack[] contents = inventory.getContents();
int result = 0;

for (ItemStack is : contents)
if (is == null)
result += item.getMaxStackSize();
else if (is.isSimilar(item))
result += Math.max(item.getMaxStackSize() - is.getAmount(), 0);

return result;
}
}
2 changes: 1 addition & 1 deletion core/src/main/resources/advancement-trees/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ options:
item: OAK_SAPLING
gui_location: auto
minecraft-gui-display: true
minecraft-gui-background: "textures/block/dirt"
minecraft-gui-background: "textures/block/anvil.png"
advancements:
requirementadvancement:
type: BlockBreak
Expand Down

0 comments on commit d9d0415

Please sign in to comment.