Skip to content

Commit

Permalink
Fix disappearing items with output chest
Browse files Browse the repository at this point in the history
  • Loading branch information
EpicPlayerA10 committed Jun 25, 2021
1 parent 816164e commit 0d58702
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.annotation.ParametersAreNonnullByDefault;

import io.github.thebusybiscuit.cscorelib2.inventory.InvUtils;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
Expand Down Expand Up @@ -44,7 +45,7 @@ public void onInteract(Player p, Block b) {
ItemStack output = RecipeType.getRecipeOutputList(this, inputs.get(i)).clone();

if (SlimefunUtils.canPlayerUseItem(p, output, true)) {
craft(p, output, inv, dispenser);
craft(p, output, inv, disp);
}

return;
Expand All @@ -70,9 +71,9 @@ private boolean isCraftable(Inventory inv, ItemStack[] recipe) {
}

@ParametersAreNonnullByDefault
private void craft(Player p, ItemStack output, Inventory inv, Block dispenser) {
private void craft(Player p, ItemStack output, Inventory inv, Dispenser dispenser) {
Inventory fakeInv = createVirtualInventory(inv);
Inventory outputInv = findOutputInventory(output, dispenser, inv, fakeInv);
Inventory outputInv = findOutputInventory(output, dispenser.getBlock(), inv, fakeInv);

if (outputInv != null) {
for (int j = 0; j < 9; j++) {
Expand All @@ -89,9 +90,16 @@ private void craft(Player p, ItemStack output, Inventory inv, Block dispenser) {
SlimefunPlugin.runSync(() -> {
if (current < 3) {
p.getWorld().playSound(p.getLocation(), Sound.BLOCK_ANVIL_USE, 1F, 2F);
} else {
} else if (InvUtils.fits(outputInv, output)) {
p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F);
outputInv.addItem(output);
} else if (InvUtils.fits(dispenser.getInventory(), output)) {
p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F);
dispenser.getInventory().addItem(output);
} else {
// fallback
p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F);
dispenser.getWorld().dropItemNaturally(dispenser.getLocation(), output);
}
}, j * 20L);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import javax.annotation.ParametersAreNonnullByDefault;

import io.github.thebusybiscuit.cscorelib2.inventory.InvUtils;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
Expand Down Expand Up @@ -72,7 +73,7 @@ public void onInteract(Player p, Block b) {
removing.setAmount(recipeInput.getAmount());
inv.removeItem(removing);

craft(p, output, outputInv);
craft(p, output, outputInv, disp);
} else {
SlimefunPlugin.getLocalization().sendMessage(p, "machines.full-inventory", true);
}
Expand All @@ -86,16 +87,23 @@ public void onInteract(Player p, Block b) {
}
}

private void craft(Player p, ItemStack output, Inventory outputInv) {
private void craft(Player p, ItemStack output, Inventory outputInv, Dispenser dispenser) {
for (int i = 0; i < 4; i++) {
int j = i;

SlimefunPlugin.runSync(() -> {
if (j < 3) {
p.getWorld().playSound(p.getLocation(), j == 1 ? Sound.BLOCK_PISTON_CONTRACT : Sound.BLOCK_PISTON_EXTEND, 1F, j == 0 ? 1F : 2F);
} else {
} else if (InvUtils.fits(outputInv, output)) {
p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F);
outputInv.addItem(output);
} else if (InvUtils.fits(dispenser.getInventory(), output)) {
p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F);
dispenser.getInventory().addItem(output);
} else {
// fallback
p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F);
dispenser.getWorld().dropItemNaturally(dispenser.getLocation(), output);
}
}, i * 20L);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.annotation.ParametersAreNonnullByDefault;

import io.github.thebusybiscuit.cscorelib2.inventory.InvUtils;
import org.bukkit.Effect;
import org.bukkit.Material;
import org.bukkit.Sound;
Expand Down Expand Up @@ -53,7 +54,7 @@ public void onInteract(Player p, Block b) {
ItemStack output = RecipeType.getRecipeOutputList(this, inputs.get(i)).clone();

if (SlimefunUtils.canPlayerUseItem(p, output, true)) {
craft(inv, dispenser, p, b, output);
craft(inv, disp, p, b, output);
}

return;
Expand All @@ -68,9 +69,9 @@ public void onInteract(Player p, Block b) {
}
}

private void craft(Inventory inv, Block dispenser, Player p, Block b, ItemStack output) {
private void craft(Inventory inv, Dispenser dispenser, Player p, Block b, ItemStack output) {
Inventory fakeInv = createVirtualInventory(inv);
Inventory outputInv = findOutputInventory(output, dispenser, inv, fakeInv);
Inventory outputInv = findOutputInventory(output, dispenser.getBlock(), inv, fakeInv);

if (outputInv != null) {
SlimefunItem sfItem = SlimefunItem.getByItem(output);
Expand All @@ -89,13 +90,13 @@ private void craft(Inventory inv, Block dispenser, Player p, Block b, ItemStack
}
}

startAnimation(p, b, outputInv, output);
startAnimation(p, b, outputInv, output, dispenser);
} else {
SlimefunPlugin.getLocalization().sendMessage(p, "machines.full-inventory", true);
}
}

private void startAnimation(Player p, Block b, Inventory inv, ItemStack output) {
private void startAnimation(Player p, Block b, Inventory inv, ItemStack output, Dispenser dispenser) {
for (int j = 0; j < 4; j++) {
int current = j;
SlimefunPlugin.runSync(() -> {
Expand All @@ -104,9 +105,16 @@ private void startAnimation(Player p, Block b, Inventory inv, ItemStack output)

if (current < 3) {
p.getWorld().playSound(b.getLocation(), Sound.BLOCK_WOODEN_BUTTON_CLICK_ON, 1F, 1F);
} else {
} else if (InvUtils.fits(inv, output)) {
p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F);
inv.addItem(output);
} else if (InvUtils.fits(dispenser.getInventory(), output)) {
p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F);
dispenser.getInventory().addItem(output);
} else {
// fallback
p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F);
dispenser.getWorld().dropItemNaturally(dispenser.getLocation(), output);
}
}, j * 20L);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import java.util.stream.Collectors;

import io.github.thebusybiscuit.cscorelib2.inventory.InvUtils;
import org.bukkit.Effect;
import org.bukkit.Material;
import org.bukkit.Sound;
Expand Down Expand Up @@ -54,7 +55,7 @@ public void onInteract(Player p, Block b) {
removing.setAmount(convert.getAmount());
inv.removeItem(removing);

craft(p, b, output, outputInv);
craft(p, b, output, outputInv, disp);
} else {
SlimefunPlugin.getLocalization().sendMessage(p, "machines.full-inventory", true);
}
Expand All @@ -67,7 +68,7 @@ public void onInteract(Player p, Block b) {
}
}

private void craft(Player p, Block b, ItemStack output, Inventory outputInv) {
private void craft(Player p, Block b, ItemStack output, Inventory outputInv, Dispenser dispenser) {
for (int i = 0; i < 4; i++) {
int j = i;

Expand All @@ -79,9 +80,16 @@ private void craft(Player p, Block b, ItemStack output, Inventory outputInv) {

if (j < 3) {
p.getWorld().playSound(b.getLocation(), Sound.ENTITY_TNT_PRIMED, 1F, 1F);
} else {
} else if (InvUtils.fits(outputInv, output)) {
p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F);
outputInv.addItem(output);
} else if (InvUtils.fits(dispenser.getInventory(), output)) {
p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F);
dispenser.getInventory().addItem(output);
} else {
// fallback
p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F);
dispenser.getWorld().dropItemNaturally(dispenser.getLocation(), output);
}
}, i * 20L);
}
Expand Down

0 comments on commit 0d58702

Please sign in to comment.