2013-11-16 00:21:00 +01:00
|
|
|
package com.gmail.nossr50.skills.alchemy;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.bukkit.Material;
|
2014-01-30 20:45:21 +01:00
|
|
|
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;
|
2013-12-31 00:30:58 +01:00
|
|
|
import com.gmail.nossr50.datatypes.skills.SecondaryAbility;
|
2014-03-03 18:27:45 +01:00
|
|
|
import com.gmail.nossr50.datatypes.skills.alchemy.AlchemyPotion;
|
2013-11-16 00:21:00 +01:00
|
|
|
import com.gmail.nossr50.runnables.PlayerUpdateInventoryTask;
|
|
|
|
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])) {
|
2013-12-31 00:30:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-11 22:22:57 +01:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
if (contents[i] == null || contents[i].getType() != Material.POTION) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-12-31 00:30:58 +01:00
|
|
|
|
2014-02-11 22:22:57 +01:00
|
|
|
if (getChildPotion(PotionConfig.getInstance().getPotion(contents[i].getDurability()), contents[Alchemy.INGREDIENT_SLOT]) != null) {
|
|
|
|
return true;
|
2013-11-16 00:21:00 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-31 00:30:58 +01:00
|
|
|
|
2013-11-16 00:21:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static AlchemyPotion getChildPotion(AlchemyPotion potion, ItemStack ingredient) {
|
|
|
|
if (potion != null && potion.getChildDataValue(ingredient) != -1) {
|
2014-02-11 22:22:57 +01:00
|
|
|
return PotionConfig.getInstance().getPotion(potion.getChildDataValue(ingredient));
|
2013-11-16 00:21:00 +01:00
|
|
|
}
|
2013-12-31 00:30:58 +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 boolean removeIngredient(BrewerInventory inventory, Player player) {
|
2014-03-03 14:03:13 +01:00
|
|
|
ItemStack ingredient = inventory.getIngredient() == null ? null : inventory.getIngredient().clone();
|
2013-12-31 00:30:58 +01:00
|
|
|
|
2013-11-16 00:21:00 +01:00
|
|
|
if (isEmpty(ingredient) || !isValidIngredient(player, ingredient)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (ingredient.getAmount() <= 1) {
|
2014-02-11 22:22:57 +01:00
|
|
|
inventory.setIngredient(null);
|
2013-11-16 00:21:00 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ingredient.setAmount(ingredient.getAmount() - 1);
|
2014-02-11 22:22:57 +01:00
|
|
|
inventory.setIngredient(ingredient);
|
2013-11-16 00:21:00 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-12-31 00:30:58 +01:00
|
|
|
|
2013-11-16 00:21:00 +01:00
|
|
|
for (ItemStack ingredient : getValidIngredients(player)) {
|
|
|
|
if (item.isSimilar(ingredient)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2013-12-31 00:30:58 +01:00
|
|
|
|
2013-11-16 00:21:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static List<ItemStack> getValidIngredients(Player player) {
|
2014-02-11 22:22:57 +01:00
|
|
|
return PotionConfig.getInstance().getIngredients((player == null || !Permissions.secondaryAbilityEnabled(player, SecondaryAbility.CONCOCTIONS)) ? 1 : UserManager.getPlayer(player).getAlchemyManager().getTier());
|
2013-11-16 00:21:00 +01:00
|
|
|
}
|
|
|
|
|
2014-01-30 20:45:21 +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;
|
|
|
|
}
|
2013-12-31 00:30:58 +01:00
|
|
|
|
2014-01-30 20:45:21 +01:00
|
|
|
BrewerInventory inventory = ((BrewingStand) brewingStand).getInventory();
|
2013-11-16 00:21:00 +01:00
|
|
|
ItemStack ingredient = inventory.getIngredient() == null ? null : inventory.getIngredient().clone();
|
2013-12-31 00:30:58 +01:00
|
|
|
|
|
|
|
if (!removeIngredient(inventory, player)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-11 22:22:57 +01:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2014-02-15 23:35:14 +01:00
|
|
|
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.getDurability());
|
|
|
|
AlchemyPotion output = PotionConfig.getInstance().getPotion(input.getChildDataValue(ingredient));
|
2013-12-31 00:30:58 +01:00
|
|
|
|
2014-02-15 11:17:31 +01:00
|
|
|
if (output != null) {
|
|
|
|
inventory.setItem(i, output.toItemStack(item.getAmount()).clone());
|
2013-12-31 00:30:58 +01:00
|
|
|
|
2014-02-15 11:17:31 +01:00
|
|
|
if (player != null) {
|
|
|
|
UserManager.getPlayer(player).getAlchemyManager().handlePotionBrewSuccesses(1);
|
2013-11-16 00:21:00 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-31 00:30:58 +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();
|
2013-12-31 00:30:58 +01:00
|
|
|
|
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);
|
|
|
|
}
|
2013-12-31 00:30:58 +01:00
|
|
|
|
2014-02-11 22:22:57 +01:00
|
|
|
from.setAmount(fromAmount - 1);
|
2014-03-23 12:59:43 +01:00
|
|
|
view.setItem(Alchemy.INGREDIENT_SLOT, to);
|
2014-02-27 16:56:21 +01:00
|
|
|
view.setItem(fromSlot, from);
|
2013-12-31 00:30:58 +01:00
|
|
|
|
2013-11-16 00:21:00 +01:00
|
|
|
return true;
|
|
|
|
}
|
2013-12-31 00:30:58 +01:00
|
|
|
|
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-12-31 00:30:58 +01:00
|
|
|
|
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();
|
2013-12-31 00:30:58 +01:00
|
|
|
|
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-12-31 00:30:58 +01:00
|
|
|
|
2013-11-16 00:21:00 +01:00
|
|
|
from.setAmount(left);
|
|
|
|
view.setItem(fromSlot, from);
|
2013-12-31 00:30:58 +01:00
|
|
|
|
2013-11-16 00:21:00 +01:00
|
|
|
return true;
|
|
|
|
}
|
2013-12-31 00:30:58 +01:00
|
|
|
|
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-12-31 00:30:58 +01:00
|
|
|
|
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
|
|
|
}
|