1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

Move code to superclass and call method in other machines

This commit is contained in:
EpicPlayerA10 2021-07-03 21:36:15 +02:00
parent 1058c15abe
commit 0128d57af0
5 changed files with 28 additions and 41 deletions

View File

@ -14,7 +14,7 @@ import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.block.data.type.Dispenser; import org.bukkit.block.Dispenser;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -165,6 +165,29 @@ public abstract class MultiBlockMachine extends SlimefunItem implements NotPlace
} }
} }
/**
* This method handles a output {@link ItemStack} from the {@link MultiBlockMachine} which has a crafting delay
*
* @param outputItem
* A crafted {@link ItemStack} from {@link MultiBlockMachine}
* @param dispenser
* Our {@link Dispenser} from {@link MultiBlockMachine}
*
*/
protected void handleCraftedItem(ItemStack outputItem, Dispenser dispenser) {
Inventory dispInv = dispenser.getInventory();
Inventory outputInv = findOutputInventory(outputItem, dispenser.getBlock(), dispInv);
if (outputInv != null) {
outputInv.addItem(outputItem);
} else if (InvUtils.fits(dispInv, outputItem)) {
dispInv.addItem(outputItem);
} else {
// fallback
dispenser.getWorld().dropItemNaturally(dispenser.getLocation(), outputItem);
}
}
private static @Nonnull Material[] convertItemStacksToMaterial(@Nonnull ItemStack[] items) { private static @Nonnull Material[] convertItemStacksToMaterial(@Nonnull ItemStack[] items) {
List<Material> materials = new ArrayList<>(); List<Material> materials = new ArrayList<>();

View File

@ -91,17 +91,8 @@ public class ArmorForge extends AbstractCraftingTable {
if (current < 3) { if (current < 3) {
p.getWorld().playSound(p.getLocation(), Sound.BLOCK_ANVIL_USE, 1F, 2F); p.getWorld().playSound(p.getLocation(), Sound.BLOCK_ANVIL_USE, 1F, 2F);
} else { } else {
Inventory dispInv = dispenser.getInventory();
Inventory outputInv2 = findOutputInventory(output, dispenser.getBlock(), dispInv);
p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F); p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F);
if (outputInv2 != null) { handleCraftedItem(output, dispenser);
outputInv2.addItem(output);
} else if (InvUtils.fits(dispInv, output)) {
dispInv.addItem(output);
} else {
// fallback
dispenser.getWorld().dropItemNaturally(dispenser.getLocation(), output);
}
} }
}, j * 20L); }, j * 20L);
} }

View File

@ -96,17 +96,8 @@ public class Compressor extends MultiBlockMachine {
if (j < 3) { if (j < 3) {
p.getWorld().playSound(p.getLocation(), j == 1 ? Sound.BLOCK_PISTON_CONTRACT : Sound.BLOCK_PISTON_EXTEND, 1F, j == 0 ? 1F : 2F); p.getWorld().playSound(p.getLocation(), j == 1 ? Sound.BLOCK_PISTON_CONTRACT : Sound.BLOCK_PISTON_EXTEND, 1F, j == 0 ? 1F : 2F);
} else { } else {
Inventory dispInv = dispenser.getInventory();
Inventory outputInv = findOutputInventory(output, dispenser.getBlock(), dispInv);
p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F); p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F);
if (outputInv != null) { handleCraftedItem(output, dispenser);
outputInv.addItem(output);
} else if (InvUtils.fits(dispInv, output)) {
dispInv.addItem(output);
} else {
// fallback
dispenser.getWorld().dropItemNaturally(dispenser.getLocation(), output);
}
} }
}, i * 20L); }, i * 20L);
} }

View File

@ -106,17 +106,8 @@ public class MagicWorkbench extends AbstractCraftingTable {
if (current < 3) { if (current < 3) {
p.getWorld().playSound(b.getLocation(), Sound.BLOCK_WOODEN_BUTTON_CLICK_ON, 1F, 1F); p.getWorld().playSound(b.getLocation(), Sound.BLOCK_WOODEN_BUTTON_CLICK_ON, 1F, 1F);
} else { } else {
Inventory dispInv = dispenser.getInventory();
Inventory outputInv = findOutputInventory(output, dispenser.getBlock(), dispInv);
p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F); p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F);
if (outputInv != null) { handleCraftedItem(output, dispenser);
outputInv.addItem(output);
} else if (InvUtils.fits(dispInv, output)) {
dispInv.addItem(output);
} else {
// fallback
dispenser.getWorld().dropItemNaturally(dispenser.getLocation(), output);
}
} }
}, j * 20L); }, j * 20L);
} }

View File

@ -85,17 +85,8 @@ public class PressureChamber extends MultiBlockMachine {
if (j < 3) { if (j < 3) {
p.getWorld().playSound(b.getLocation(), Sound.ENTITY_TNT_PRIMED, 1F, 1F); p.getWorld().playSound(b.getLocation(), Sound.ENTITY_TNT_PRIMED, 1F, 1F);
} else { } else {
Inventory dispInv = dispenser.getInventory();
Inventory outputInv = findOutputInventory(output, dispenser.getBlock(), dispInv);
p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F); p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F);
if (outputInv != null) { handleCraftedItem(output, dispenser);
outputInv.addItem(output);
} else if (InvUtils.fits(dispInv, output)) {
dispInv.addItem(output);
} else {
// fallback
dispenser.getWorld().dropItemNaturally(dispenser.getLocation(), output);
}
} }
}, i * 20L); }, i * 20L);
} }