Skip to content

Commit

Permalink
fix electric kitchen not returning empty items, seeds dropping in cre…
Browse files Browse the repository at this point in the history
…ative
  • Loading branch information
SchnTgaiSpock committed Oct 7, 2024
1 parent be9deb0 commit d5b01af
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 29 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.schntgaispock.gastronomicon</groupId>
<artifactId>Gastronomicon</artifactId>
<version>1.1.2</version>
<version>1.1.3</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand Down Expand Up @@ -122,7 +122,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.annotation.ParametersAreNonnullByDefault;

import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.enchantments.Enchantment;
Expand Down Expand Up @@ -98,6 +99,11 @@ public static LootTable<ItemStack> getDrops(@Nonnull EntityType dropFrom) {

@EventHandler
public void onBlockBreak(BlockBreakEvent e) {
final Player p = e.getPlayer();
if (p.getGameMode() != GameMode.SURVIVAL) {
return;
}

if (worldsDisabledIn.contains(e.getBlock().getWorld().getName())) {
return;
}
Expand All @@ -110,10 +116,10 @@ public void onBlockBreak(BlockBreakEvent e) {
if (drops == null)
return;

if (!Slimefun.getProtectionManager().hasPermission(e.getPlayer(), e.getBlock(), Interaction.BREAK_BLOCK))
if (!Slimefun.getProtectionManager().hasPermission(p, e.getBlock(), Interaction.BREAK_BLOCK))
return;

final ItemStack weapon = e.getPlayer().getInventory().getItemInMainHand();
final ItemStack weapon = p.getInventory().getItemInMainHand();
final int fortune = weapon == null ? 0
: weapon.getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS);
if (NumberUtil.flip(getDropChance(b.getType()) * (1 + fortune * 0.5))) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.schntgaispock.gastronomicon.core.slimefun.items.workstations.automatic;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -164,7 +165,7 @@ protected void constructMenu(BlockMenuPreset preset) {
draw(preset, GastroStacks.MENU_OUTPUT_BORDER, OUTPUT_BORDER);
draw(preset, GastroStacks.MENU_NO_ANDROID, STATUS_SLOT);

for (int i : getOutputSlots()) { // From AContainer
for (final int i : getOutputSlots()) { // From AContainer
preset.addMenuClickHandler(i, new AdvancedMenuClickHandler() {
@Override
public boolean onClick(Player p, int slot, ItemStack cursor, ClickAction action) {
Expand Down Expand Up @@ -248,14 +249,22 @@ protected MachineRecipe findNextRecipe(BlockMenu menu) {
hashRecipePair.second(found);
}

final MachineRecipe newRecipe = new MachineRecipe(60 / getSpeed(), found.entries().stream().filter(pair -> {
return pair.first() != null;
}).map(pair -> {
final ArrayList<ItemStack> inputs = new ArrayList<>();
final ArrayList<ItemStack> outputs = new ArrayList<>();
outputs.add(recipe.getOutputs()[0]);

for (final var pair : found.entries()) {
if (pair.first() == null) continue;

final ItemStack input = menu.getItemInSlot(pair.first());
final ItemStack clone = input.asQuantity(pair.second());
input.subtract(pair.second());
return clone;
}).toArray(ItemStack[]::new), new ItemStack[] { recipe.getOutputs()[0] });
inputs.add(clone);
ItemUtil.consumeItem(input, pair.second(), true).ifPresent(mat -> {
outputs.add(new ItemStack(mat));
});
}

final MachineRecipe newRecipe = new MachineRecipe(60 / getSpeed(), inputs.toArray(ItemStack[]::new), outputs.toArray(ItemStack[]::new));

return newRecipe;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import io.github.schntgaispock.gastronomicon.util.item.ItemUtil;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType;
import io.github.thebusybiscuit.slimefun4.libraries.dough.items.ItemUtils;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -201,6 +200,23 @@ protected void onNewInstance(BlockMenu menu, Block b) {
toReturn = Arrays.copyOfRange(recipeOutputs, 1, recipeOutputs.length);
}

// Subtract inputs
Arrays.stream(getInputSlots()).forEach(s -> {
final ItemStack i = menu.getItemInSlot(s);
if (i != null)
ItemUtil.consumeItem(i, 1, true).ifPresent(mat -> {
player.getOpenInventory().getTopInventory().setItem(s, new ItemStack(mat));
});
});
for (final int containerSlot : getContainerSlots()) {
final ItemStack i = menu.getItemInSlot(containerSlot);
if (i != null && hashRecipePair.second() != null
&& hashRecipePair.second().getInputs().getContainer().matches(i)) {
i.subtract();
break;
}
}

// Place the result in the inventory
final Inventory inv = player.getOpenInventory().getTopInventory();

Expand All @@ -219,21 +235,6 @@ protected void onNewInstance(BlockMenu menu, Block b) {
}
}
}

// Subtract inputs
Arrays.stream(getInputSlots()).forEach(s -> {
final ItemStack i = menu.getItemInSlot(s);
if (i != null)
ItemUtils.consumeItem(i, 1, true);
});
for (final int containerSlot : getContainerSlots()) {
final ItemStack i = menu.getItemInSlot(containerSlot);
if (i != null && hashRecipePair.second() != null
&& hashRecipePair.second().getInputs().getContainer().matches(i)) {
i.subtract();
break;
}
}
ItemUtil.giveItems(player, toReturn);
return false;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.schntgaispock.gastronomicon.util.item;

import java.util.Optional;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

Expand All @@ -17,7 +19,7 @@ public class ItemUtil {

public static int hashIgnoreAmount(ItemStack stack) {
if (stack == null) return 0;

int hash = 1;

hash = hash * 31 + stack.getType().hashCode();
Expand Down Expand Up @@ -61,7 +63,7 @@ public static Material getPlacedBlock(@Nonnull Material seed) {
}

@ParametersAreNonnullByDefault
public static void giveItems(Player player, ItemStack[] items) {
public static void giveItems(Player player, ItemStack... items) {
player.getInventory().addItem(items).forEach((__, item) -> {
player.getWorld().dropItemNaturally(player.getLocation(), item);
});
Expand All @@ -82,4 +84,40 @@ public static String getPotionName(PotionEffectType type) {
};
}

/**
* Copied from Slimefun's ItemUtils, this version returns the empty container
* version of the item instead of replacing it
*
* @param item
* @param amount
* @param replaceConsumables
*/
public static Optional<Material> consumeItem(@Nonnull ItemStack item, int amount, boolean replaceConsumables) {
if (item.getType() != Material.AIR && item.getAmount() > 0) {
if (replaceConsumables) {
switch (item.getType()) {
case POTION:
case DRAGON_BREATH:
case HONEY_BOTTLE:
item.setAmount(0);
return Optional.of(Material.GLASS_BOTTLE);
case WATER_BUCKET:
case LAVA_BUCKET:
case MILK_BUCKET:
item.setAmount(0);
return Optional.of(Material.BUCKET);
default:
break;
}
}

if (item.getAmount() <= amount) {
item.setAmount(0);
} else {
item.setAmount(item.getAmount() - amount);
}
}
return Optional.empty();
}

}

0 comments on commit d5b01af

Please sign in to comment.