mcMMO/src/main/java/com/gmail/nossr50/skills/alchemy/AlchemyPotionBrewer.java

258 lines
8.4 KiB
Java
Raw Normal View History

2013-11-16 00:21:00 +01:00
package com.gmail.nossr50.skills.alchemy;
import java.util.ArrayList;
2013-11-16 00:21:00 +01:00
import java.util.List;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
2013-11-16 00:21:00 +01:00
import org.bukkit.block.BrewingStand;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.BrewerInventory;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import com.gmail.nossr50.mcMMO;
2014-02-03 20:52:28 +01:00
import com.gmail.nossr50.config.skills.alchemy.PotionConfig;
2018-12-29 14:24:55 +01:00
import com.gmail.nossr50.datatypes.skills.SubSkill;
2014-03-03 18:27:45 +01:00
import com.gmail.nossr50.datatypes.skills.alchemy.AlchemyPotion;
import com.gmail.nossr50.datatypes.skills.alchemy.PotionStage;
import com.gmail.nossr50.events.fake.FakeBrewEvent;
import com.gmail.nossr50.runnables.player.PlayerUpdateInventoryTask;
2013-11-16 00:21:00 +01:00
import com.gmail.nossr50.runnables.skills.AlchemyBrewCheckTask;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.player.UserManager;
public final class AlchemyPotionBrewer {
public static boolean isValidBrew(Player player, ItemStack[] contents) {
2014-02-11 22:22:57 +01:00
if (!isValidIngredient(player, contents[Alchemy.INGREDIENT_SLOT])) {
return false;
}
2014-02-11 22:22:57 +01:00
for (int i = 0; i < 3; i++) {
2016-03-16 06:39:23 +01:00
if (contents[i] == null || contents[i].getType() != Material.POTION && contents[i].getType() != Material.SPLASH_POTION && contents[i].getType() != Material.LINGERING_POTION) {
2014-02-11 22:22:57 +01:00
continue;
}
if (getChildPotion(PotionConfig.getInstance().getPotion(contents[i]), contents[Alchemy.INGREDIENT_SLOT]) != null) {
2014-02-11 22:22:57 +01:00
return true;
2013-11-16 00:21:00 +01:00
}
}
2013-11-16 00:21:00 +01:00
return false;
}
private static AlchemyPotion getChildPotion(AlchemyPotion potion, ItemStack ingredient) {
if (potion != null) {
return potion.getChild(ingredient);
2013-11-16 00:21:00 +01:00
}
2013-11-16 00:21:00 +01:00
return null;
}
2014-02-11 22:22:57 +01:00
public static boolean isEmpty(ItemStack item) {
2013-11-16 00:21:00 +01:00
return item == null || item.getType() == Material.AIR || item.getAmount() == 0;
}
private static void removeIngredient(BrewerInventory inventory, Player player) {
2014-03-03 14:03:13 +01:00
ItemStack ingredient = inventory.getIngredient() == null ? null : inventory.getIngredient().clone();
2013-11-16 00:21:00 +01:00
if (isEmpty(ingredient) || !isValidIngredient(player, ingredient)) {
return;
2013-11-16 00:21:00 +01:00
}
else if (ingredient.getAmount() <= 1) {
2014-02-11 22:22:57 +01:00
inventory.setIngredient(null);
return;
2013-11-16 00:21:00 +01:00
}
else {
ingredient.setAmount(ingredient.getAmount() - 1);
2014-02-11 22:22:57 +01:00
inventory.setIngredient(ingredient);
return;
2013-11-16 00:21:00 +01:00
}
}
private static boolean hasIngredient(BrewerInventory inventory, Player player) {
ItemStack ingredient = inventory.getIngredient() == null ? null : inventory.getIngredient().clone();
return !isEmpty(ingredient) && isValidIngredient(player, ingredient);
}
2014-02-11 22:22:57 +01:00
public static boolean isValidIngredient(Player player, ItemStack item) {
2013-11-16 00:21:00 +01:00
if (isEmpty(item)) {
return false;
}
2013-11-16 00:21:00 +01:00
for (ItemStack ingredient : getValidIngredients(player)) {
if (item.isSimilar(ingredient)) {
return true;
}
}
2013-11-16 00:21:00 +01:00
return false;
}
private static List<ItemStack> getValidIngredients(Player player) {
2018-12-29 14:24:55 +01:00
return PotionConfig.getInstance().getIngredients((player == null || !Permissions.isSubSkillEnabled(player, SubSkill.ALCHEMY_CONCOCTIONS)) ? 1 : UserManager.getPlayer(player).getAlchemyManager().getTier());
2013-11-16 00:21:00 +01:00
}
public static void finishBrewing(BlockState brewingStand, Player player, boolean forced) {
if (!(brewingStand instanceof BrewingStand)) {
2013-11-16 00:21:00 +01:00
return;
}
BrewerInventory inventory = ((BrewingStand) brewingStand).getInventory();
2013-11-16 00:21:00 +01:00
ItemStack ingredient = inventory.getIngredient() == null ? null : inventory.getIngredient().clone();
if (!hasIngredient(inventory, player)) {
return;
}
List<AlchemyPotion> inputList = new ArrayList<AlchemyPotion>();
2014-02-11 22:22:57 +01:00
for (int i = 0; i < 3; i++) {
ItemStack item = inventory.getItem(i);
2014-02-11 22:22:57 +01:00
2014-02-15 11:18:11 +01:00
if (isEmpty(item) || item.getType() == Material.GLASS_BOTTLE || !PotionConfig.getInstance().isValidPotion(item)) {
2014-02-15 11:17:31 +01:00
continue;
}
AlchemyPotion input = PotionConfig.getInstance().getPotion(item);
AlchemyPotion output = input.getChild(ingredient);
inputList.add(input);
2014-02-15 11:17:31 +01:00
if (output != null) {
inventory.setItem(i, output.toItemStack(item.getAmount()).clone());
}
}
FakeBrewEvent event = new FakeBrewEvent(brewingStand.getBlock(), inventory, ((BrewingStand) brewingStand).getFuelLevel());
mcMMO.p.getServer().getPluginManager().callEvent(event);
if (event.isCancelled() || inputList.isEmpty()) {
return;
}
removeIngredient(inventory, player);
for (AlchemyPotion input : inputList) {
AlchemyPotion output = input.getChild(ingredient);
if (output != null && player != null) {
PotionStage potionStage = PotionStage.getPotionStage(input, output);
if (UserManager.hasPlayerDataKey(player)) {
UserManager.getPlayer(player).getAlchemyManager().handlePotionBrewSuccesses(potionStage, 1);
}
2013-11-16 00:21:00 +01:00
}
}
if (!forced) {
scheduleUpdate(inventory);
2013-11-16 00:21:00 +01:00
}
}
2014-02-27 16:56:21 +01:00
public static boolean transferItems(InventoryView view, int fromSlot, ClickType click) {
2014-02-11 22:22:57 +01:00
boolean success = false;
if (click.isLeftClick()) {
2014-02-27 16:56:21 +01:00
success = transferItems(view, fromSlot);
2014-02-11 22:22:57 +01:00
}
else if (click.isRightClick()) {
2014-02-27 16:56:21 +01:00
success = transferOneItem(view, fromSlot);
2014-02-11 22:22:57 +01:00
}
return success;
}
2014-02-27 16:56:21 +01:00
private static boolean transferOneItem(InventoryView view, int fromSlot) {
2013-11-16 00:21:00 +01:00
ItemStack from = view.getItem(fromSlot).clone();
2014-02-27 16:56:21 +01:00
ItemStack to = view.getItem(Alchemy.INGREDIENT_SLOT).clone();
2014-02-27 16:56:21 +01:00
if (isEmpty(from)) {
2013-11-16 00:21:00 +01:00
return false;
}
2014-02-11 22:22:57 +01:00
boolean emptyTo = isEmpty(to);
int fromAmount = from.getAmount();
if (!emptyTo && fromAmount >= from.getType().getMaxStackSize()) {
2013-11-16 00:21:00 +01:00
return false;
}
2014-02-11 22:22:57 +01:00
else if (emptyTo || from.isSimilar(to)) {
if (emptyTo) {
2013-11-16 00:21:00 +01:00
to = from.clone();
to.setAmount(1);
}
else {
to.setAmount(to.getAmount() + 1);
}
2014-02-11 22:22:57 +01:00
from.setAmount(fromAmount - 1);
view.setItem(Alchemy.INGREDIENT_SLOT, to);
2014-02-27 16:56:21 +01:00
view.setItem(fromSlot, from);
2013-11-16 00:21:00 +01:00
return true;
}
2013-11-16 00:21:00 +01:00
return false;
}
/**
* Transfer items between two ItemStacks, returning the leftover status
*/
2014-02-27 16:56:21 +01:00
private static boolean transferItems(InventoryView view, int fromSlot) {
2014-02-11 22:22:57 +01:00
ItemStack from = view.getItem(fromSlot).clone();
2014-02-27 16:56:21 +01:00
ItemStack to = view.getItem(Alchemy.INGREDIENT_SLOT).clone();
2014-02-11 22:22:57 +01:00
if (isEmpty(from)) {
2013-11-16 00:21:00 +01:00
return false;
}
2014-02-11 22:22:57 +01:00
else if (isEmpty(to)) {
2014-02-27 16:56:21 +01:00
view.setItem(Alchemy.INGREDIENT_SLOT, from);
2013-11-16 00:21:00 +01:00
view.setItem(fromSlot, null);
2013-11-16 00:21:00 +01:00
return true;
}
2014-02-11 22:22:57 +01:00
else if (from.isSimilar(to)) {
int fromAmount = from.getAmount();
int toAmount = to.getAmount();
int maxSize = to.getType().getMaxStackSize();
2014-02-11 22:22:57 +01:00
if (fromAmount + toAmount > maxSize) {
int left = fromAmount + toAmount - maxSize;
to.setAmount(maxSize);
2014-02-27 16:56:21 +01:00
view.setItem(Alchemy.INGREDIENT_SLOT, to);
2013-11-16 00:21:00 +01:00
from.setAmount(left);
view.setItem(fromSlot, from);
2013-11-16 00:21:00 +01:00
return true;
}
2014-02-11 22:22:57 +01:00
to.setAmount(fromAmount + toAmount);
2013-11-16 00:21:00 +01:00
view.setItem(fromSlot, null);
2014-02-27 16:56:21 +01:00
view.setItem(Alchemy.INGREDIENT_SLOT, to);
2013-11-16 00:21:00 +01:00
return true;
}
2014-02-11 22:22:57 +01:00
return false;
2013-11-16 00:21:00 +01:00
}
2014-01-20 22:38:04 +01:00
2014-02-11 22:22:57 +01:00
public static void scheduleCheck(Player player, BrewingStand brewingStand) {
2014-01-20 22:38:04 +01:00
new AlchemyBrewCheckTask(player, brewingStand).runTask(mcMMO.p);
}
2014-02-11 22:22:57 +01:00
public static void scheduleUpdate(Inventory inventory) {
2014-01-20 22:38:04 +01:00
for (HumanEntity humanEntity : inventory.getViewers()) {
if (humanEntity instanceof Player) {
new PlayerUpdateInventoryTask((Player) humanEntity).runTask(mcMMO.p);
}
}
}
2013-11-16 00:21:00 +01:00
}