|
| 1 | +package org.lins.mmmjjkx.rykenslimefuncustomizer.objects.customs.machine.sf; |
| 2 | + |
| 3 | +import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; |
| 4 | +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; |
| 5 | +import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; |
| 6 | +import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.AutoBrewer; |
| 7 | +import io.github.thebusybiscuit.slimefun4.libraries.dough.inventory.InvUtils; |
| 8 | +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.MachineRecipe; |
| 9 | +import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu; |
| 10 | +import org.bukkit.Material; |
| 11 | +import org.bukkit.inventory.ItemStack; |
| 12 | +import org.bukkit.inventory.meta.PotionMeta; |
| 13 | +import org.bukkit.potion.PotionData; |
| 14 | +import org.bukkit.potion.PotionType; |
| 15 | + |
| 16 | +import javax.annotation.Nonnull; |
| 17 | +import javax.annotation.Nullable; |
| 18 | +import javax.annotation.ParametersAreNonnullByDefault; |
| 19 | +import java.util.EnumMap; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +public class AdvancedAutoBrewer extends AutoBrewer { |
| 23 | + private static final Map<Material, PotionType> potionRecipes = new EnumMap<>(Material.class); |
| 24 | + private static final Map<PotionType, PotionType> fermentations = new EnumMap<>(PotionType.class); |
| 25 | + |
| 26 | + private final int speed; |
| 27 | + |
| 28 | + public AdvancedAutoBrewer(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, int speed) { |
| 29 | + super(itemGroup, item, recipeType, recipe); |
| 30 | + |
| 31 | + this.speed = speed; |
| 32 | + } |
| 33 | + |
| 34 | + @Nullable |
| 35 | + protected MachineRecipe findNextRecipe(BlockMenu menu) { |
| 36 | + ItemStack input1 = menu.getItemInSlot(this.getInputSlots()[0]); |
| 37 | + ItemStack input2 = menu.getItemInSlot(this.getInputSlots()[1]); |
| 38 | + if (input1 != null && input2 != null) { |
| 39 | + if (!this.isPotion(input1.getType()) && !this.isPotion(input2.getType())) { |
| 40 | + return null; |
| 41 | + } else { |
| 42 | + boolean isPotionInFirstSlot = this.isPotion(input1.getType()); |
| 43 | + ItemStack ingredient = isPotionInFirstSlot ? input2 : input1; |
| 44 | + if (ingredient.hasItemMeta()) { |
| 45 | + return null; |
| 46 | + } else { |
| 47 | + ItemStack potionItem = isPotionInFirstSlot ? input1 : input2; |
| 48 | + PotionMeta potion = (PotionMeta)potionItem.getItemMeta(); |
| 49 | + ItemStack output = this.brew(ingredient.getType(), potionItem.getType(), potion); |
| 50 | + if (output == null) { |
| 51 | + return null; |
| 52 | + } else { |
| 53 | + output.setItemMeta(potion); |
| 54 | + if (!InvUtils.fits(menu.toInventory(), output, this.getOutputSlots())) { |
| 55 | + return null; |
| 56 | + } else { |
| 57 | + for(int slot : this.getInputSlots()) { |
| 58 | + menu.consumeItem(slot); |
| 59 | + } |
| 60 | + |
| 61 | + return new MachineRecipe(30 / speed, new ItemStack[]{input1, input2}, new ItemStack[]{output}); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } else { |
| 67 | + return null; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @ParametersAreNonnullByDefault |
| 72 | + @Nullable |
| 73 | + private ItemStack brew(Material input, Material potionType, PotionMeta potion) { |
| 74 | + PotionData data = potion.getBasePotionData(); |
| 75 | + PotionType type = data.getType(); |
| 76 | + if (type == PotionType.WATER) { |
| 77 | + if (input == Material.FERMENTED_SPIDER_EYE) { |
| 78 | + potion.setBasePotionData(new PotionData(PotionType.WEAKNESS, false, false)); |
| 79 | + return new ItemStack(potionType); |
| 80 | + } |
| 81 | + |
| 82 | + if (input == Material.NETHER_WART) { |
| 83 | + potion.setBasePotionData(new PotionData(PotionType.AWKWARD, false, false)); |
| 84 | + return new ItemStack(potionType); |
| 85 | + } |
| 86 | + |
| 87 | + if (potionType == Material.POTION && input == Material.GUNPOWDER) { |
| 88 | + return new ItemStack(Material.SPLASH_POTION); |
| 89 | + } |
| 90 | + |
| 91 | + if (potionType == Material.SPLASH_POTION && input == Material.DRAGON_BREATH) { |
| 92 | + return new ItemStack(Material.LINGERING_POTION); |
| 93 | + } |
| 94 | + } else if (input == Material.FERMENTED_SPIDER_EYE) { |
| 95 | + PotionType fermented = fermentations.get(type); |
| 96 | + if (fermented != null) { |
| 97 | + potion.setBasePotionData(new PotionData(fermented, data.isExtended(), data.isUpgraded())); |
| 98 | + return new ItemStack(potionType); |
| 99 | + } |
| 100 | + } else { |
| 101 | + if (input == Material.REDSTONE && type.isExtendable() && !data.isUpgraded()) { |
| 102 | + potion.setBasePotionData(new PotionData(type, true, false)); |
| 103 | + return new ItemStack(potionType); |
| 104 | + } |
| 105 | + |
| 106 | + if (input == Material.GLOWSTONE_DUST && type.isUpgradeable() && !data.isExtended()) { |
| 107 | + potion.setBasePotionData(new PotionData(type, false, true)); |
| 108 | + return new ItemStack(potionType); |
| 109 | + } |
| 110 | + |
| 111 | + if (type == PotionType.AWKWARD) { |
| 112 | + PotionType potionRecipe = potionRecipes.get(input); |
| 113 | + if (potionRecipe != null) { |
| 114 | + potion.setBasePotionData(new PotionData(potionRecipe, false, false)); |
| 115 | + return new ItemStack(potionType); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + private boolean isPotion(@Nonnull Material mat) { |
| 124 | + return mat == Material.POTION || mat == Material.SPLASH_POTION || mat == Material.LINGERING_POTION; |
| 125 | + } |
| 126 | + |
| 127 | + static { |
| 128 | + potionRecipes.put(Material.SUGAR, PotionType.SPEED); |
| 129 | + potionRecipes.put(Material.RABBIT_FOOT, PotionType.JUMP); |
| 130 | + potionRecipes.put(Material.BLAZE_POWDER, PotionType.STRENGTH); |
| 131 | + potionRecipes.put(Material.GLISTERING_MELON_SLICE, PotionType.INSTANT_HEAL); |
| 132 | + potionRecipes.put(Material.SPIDER_EYE, PotionType.POISON); |
| 133 | + potionRecipes.put(Material.GHAST_TEAR, PotionType.REGEN); |
| 134 | + potionRecipes.put(Material.MAGMA_CREAM, PotionType.FIRE_RESISTANCE); |
| 135 | + potionRecipes.put(Material.PUFFERFISH, PotionType.WATER_BREATHING); |
| 136 | + potionRecipes.put(Material.GOLDEN_CARROT, PotionType.NIGHT_VISION); |
| 137 | + potionRecipes.put(Material.TURTLE_HELMET, PotionType.TURTLE_MASTER); |
| 138 | + potionRecipes.put(Material.PHANTOM_MEMBRANE, PotionType.SLOW_FALLING); |
| 139 | + fermentations.put(PotionType.SPEED, PotionType.SLOWNESS); |
| 140 | + fermentations.put(PotionType.JUMP, PotionType.SLOWNESS); |
| 141 | + fermentations.put(PotionType.INSTANT_HEAL, PotionType.INSTANT_DAMAGE); |
| 142 | + fermentations.put(PotionType.POISON, PotionType.INSTANT_DAMAGE); |
| 143 | + fermentations.put(PotionType.NIGHT_VISION, PotionType.INVISIBILITY); |
| 144 | + } |
| 145 | +} |
0 commit comments