org.bukkit.inventory.MerchantRecipe Java Examples

The following examples show how to use org.bukkit.inventory.MerchantRecipe. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NMSHandler.java    From Shopkeepers with GNU General Public License v3.0 7 votes vote down vote up
@Override
public boolean openTradeWindow(String title, List<ItemStack[]> recipes, Player player) {
	// create empty merchant:
	Merchant merchant = Bukkit.createMerchant(title);

	// create list of merchant recipes:
	List<MerchantRecipe> merchantRecipes = new ArrayList<MerchantRecipe>();
	for (ItemStack[] recipe : recipes) {
		// skip invalid recipes:
		if (recipe == null || recipe.length != 3 || Utils.isEmpty(recipe[0]) || Utils.isEmpty(recipe[2])) {
			continue;
		}

		// create and add merchant recipe:
		merchantRecipes.add(this.createMerchantRecipe(recipe[0], recipe[1], recipe[2]));
	}

	// set merchant's recipes:
	merchant.setRecipes(merchantRecipes);

	// increase 'talked-to-villager' statistic:
	player.incrementStatistic(Statistic.TALKED_TO_VILLAGER);

	// open merchant:
	return player.openMerchant(merchant, true) != null;
}
 
Example #2
Source File: NMSHandler.java    From Shopkeepers with GNU General Public License v3.0 6 votes vote down vote up
@Override
public ItemStack[] getUsedTradingRecipe(MerchantInventory merchantInventory) {
	MerchantRecipe merchantRecipe = merchantInventory.getSelectedRecipe();
	List<ItemStack> ingredients = merchantRecipe.getIngredients();
	ItemStack[] recipe = new ItemStack[3];
	recipe[0] = ingredients.get(0);
	recipe[1] = null;
	if (ingredients.size() > 1) {
		ItemStack buyItem2 = ingredients.get(1);
		if (!Utils.isEmpty(buyItem2)) {
			recipe[1] = buyItem2;
		}
	}
	recipe[2] = merchantRecipe.getResult();
	return recipe;
}
 
Example #3
Source File: NMSHandler.java    From Shopkeepers with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean openTradeWindow(String title, List<ItemStack[]> recipes, Player player) {
	// create empty merchant:
	Merchant merchant = Bukkit.createMerchant(title);

	// create list of merchant recipes:
	List<MerchantRecipe> merchantRecipes = new ArrayList<MerchantRecipe>();
	for (ItemStack[] recipe : recipes) {
		// skip invalid recipes:
		if (recipe == null || recipe.length != 3 || Utils.isEmpty(recipe[0]) || Utils.isEmpty(recipe[2])) {
			continue;
		}

		// create and add merchant recipe:
		merchantRecipes.add(this.createMerchantRecipe(recipe[0], recipe[1], recipe[2]));
	}

	// set merchant's recipes:
	merchant.setRecipes(merchantRecipes);

	// increase 'talked-to-villager' statistic:
	player.incrementStatistic(Statistic.TALKED_TO_VILLAGER);

	// open merchant:
	return player.openMerchant(merchant, true) != null;
}
 
Example #4
Source File: NMSHandler.java    From Shopkeepers with GNU General Public License v3.0 6 votes vote down vote up
@Override
public ItemStack[] getUsedTradingRecipe(MerchantInventory merchantInventory) {
	MerchantRecipe merchantRecipe = merchantInventory.getSelectedRecipe();
	List<ItemStack> ingredients = merchantRecipe.getIngredients();
	ItemStack[] recipe = new ItemStack[3];
	recipe[0] = ingredients.get(0);
	recipe[1] = null;
	if (ingredients.size() > 1) {
		ItemStack buyItem2 = ingredients.get(1);
		if (!Utils.isEmpty(buyItem2)) {
			recipe[1] = buyItem2;
		}
	}
	recipe[2] = merchantRecipe.getResult();
	return recipe;
}
 
Example #5
Source File: CraftMerchantRecipe.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
public CraftMerchantRecipe(ItemStack result, int uses, int maxUses, boolean experienceReward) {
    super(result, uses, maxUses, experienceReward);
    this.handle = new net.minecraft.village.MerchantRecipe(
            net.minecraft.item.ItemStack.EMPTY,
            net.minecraft.item.ItemStack.EMPTY,
            CraftItemStack.asNMSCopy(result),
            uses,
            maxUses,
            this
    );
}
 
Example #6
Source File: CraftMerchantRecipe.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
public net.minecraft.village.MerchantRecipe toMinecraft() {
    List<ItemStack> ingredients = getIngredients();
    Preconditions.checkState(!ingredients.isEmpty(), "No offered ingredients");
    handle.itemToBuy = CraftItemStack.asNMSCopy(ingredients.get(0));
    if (ingredients.size() > 1) {
        handle.secondItemToBuy = CraftItemStack.asNMSCopy(ingredients.get(1));
    }
    return handle;
}
 
Example #7
Source File: CraftMerchantRecipe.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
public static CraftMerchantRecipe fromBukkit(MerchantRecipe recipe) {
    if (recipe instanceof CraftMerchantRecipe) {
        return (CraftMerchantRecipe) recipe;
    } else {
        CraftMerchantRecipe craft = new CraftMerchantRecipe(recipe.getResult(), recipe.getUses(), recipe.getMaxUses(), recipe.hasExperienceReward());
        craft.setIngredients(recipe.getIngredients());

        return craft;
    }
}
 
Example #8
Source File: CraftMerchant.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setRecipes(List<MerchantRecipe> recipes) {
    MerchantRecipeList recipesList = merchant.getRecipes(null);
    recipesList.clear();
    for (MerchantRecipe recipe : recipes) {
        recipesList.add(CraftMerchantRecipe.fromBukkit(recipe).toMinecraft());
    }
}
 
Example #9
Source File: CraftMerchant.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
@Override
public List<MerchantRecipe> getRecipes() {
    return Collections.unmodifiableList(Lists.transform(merchant.getRecipes(null), new com.google.common.base.Function<net.minecraft.village.MerchantRecipe, MerchantRecipe>() {
        @Override
        public MerchantRecipe apply(net.minecraft.village.MerchantRecipe recipe) {
            return recipe.asBukkit();
        }
    }));
}
 
Example #10
Source File: NMSHandler.java    From Shopkeepers with GNU General Public License v3.0 5 votes vote down vote up
private MerchantRecipe createMerchantRecipe(ItemStack buyItem1, ItemStack buyItem2, ItemStack sellingItem) {
	assert !Utils.isEmpty(sellingItem) && !Utils.isEmpty(buyItem1);
	MerchantRecipe recipe = new MerchantRecipe(sellingItem, 10000); // no max-uses limit
	recipe.setExperienceReward(false); // no experience rewards
	recipe.addIngredient(buyItem1);
	if (!Utils.isEmpty(buyItem2)) {
		recipe.addIngredient(buyItem2);
	}
	return recipe;
}
 
Example #11
Source File: NMSHandler.java    From Shopkeepers with GNU General Public License v3.0 5 votes vote down vote up
private MerchantRecipe createMerchantRecipe(ItemStack buyItem1, ItemStack buyItem2, ItemStack sellingItem) {
	assert !Utils.isEmpty(sellingItem) && !Utils.isEmpty(buyItem1);
	MerchantRecipe recipe = new MerchantRecipe(sellingItem, 10000); // no max-uses limit
	recipe.setExperienceReward(false); // no experience rewards
	recipe.addIngredient(buyItem1);
	if (!Utils.isEmpty(buyItem2)) {
		recipe.addIngredient(buyItem2);
	}
	return recipe;
}
 
Example #12
Source File: VillagerItemShop.java    From BedwarsRel with GNU General Public License v3.0 4 votes vote down vote up
public void openTrading() {
  // As task because of inventory issues
  new BukkitRunnable() {

    @SuppressWarnings("deprecation")
    @Override
    public void run() {
      try {
        EntityVillager entityVillager = VillagerItemShop.this.createVillager();
        CraftVillager craftVillager = (CraftVillager) entityVillager.getBukkitEntity();
        EntityHuman entityHuman = VillagerItemShop.this.getEntityHuman();

        // set location
        List<MerchantRecipe> recipeList = new ArrayList<MerchantRecipe>();

        for (VillagerTrade trade : VillagerItemShop.this.category
            .getFilteredOffers()) {
          ItemStack reward = trade.getRewardItem();
          Method colorable = Utils.getColorableMethod(reward.getType());

          if (Utils.isColorable(reward)) {
            reward
                .setDurability(game.getPlayerTeam(player).getColor().getDyeColor().getWoolData());
          } else if (colorable != null) {
            ItemMeta meta = reward.getItemMeta();
            colorable.setAccessible(true);
            colorable.invoke(meta, new Object[]{VillagerItemShop.this.game
                .getPlayerTeam(VillagerItemShop.this.player).getColor().getColor()});
            reward.setItemMeta(meta);
          }

          if (!(trade.getHandle()
              .getInstance() instanceof net.minecraft.server.v1_11_R1.MerchantRecipe)) {
            continue;
          }

          MerchantRecipe recipe = new MerchantRecipe(trade.getRewardItem(), 1024);
          recipe.addIngredient(trade.getItem1());

          if (trade.getItem2() != null) {
            recipe.addIngredient(trade.getItem2());
          } else {
            recipe.addIngredient(new ItemStack(Material.AIR));
          }

          recipe.setUses(0);
          recipe.setExperienceReward(false);
          recipeList.add(recipe);
        }

        craftVillager.setRecipes(recipeList);
        craftVillager.setRiches(0);
        entityVillager.setTradingPlayer(entityHuman);

        ((CraftPlayer) player).getHandle().openTrade(entityVillager);
      } catch (Exception ex) {
        BedwarsRel.getInstance().getBugsnag().notify(ex);
        ex.printStackTrace();
      }
    }
  }.runTask(BedwarsRel.getInstance());
}
 
Example #13
Source File: VillagerItemShop.java    From BedwarsRel with GNU General Public License v3.0 4 votes vote down vote up
public void openTrading() {
  // As task because of inventory issues
  new BukkitRunnable() {

    @SuppressWarnings("deprecation")
    @Override
    public void run() {
      try {
        EntityVillager entityVillager = VillagerItemShop.this.createVillager();
        CraftVillager craftVillager = (CraftVillager) entityVillager.getBukkitEntity();
        EntityHuman entityHuman = VillagerItemShop.this.getEntityHuman();

        // set location
        List<MerchantRecipe> recipeList = new ArrayList<MerchantRecipe>();

        for (VillagerTrade trade : VillagerItemShop.this.category
            .getFilteredOffers()) {
          ItemStack reward = trade.getRewardItem();
          Method colorable = Utils.getColorableMethod(reward.getType());

          if (Utils.isColorable(reward)) {
            reward
                .setDurability(game.getPlayerTeam(player).getColor().getDyeColor().getWoolData());
          } else if (colorable != null) {
            ItemMeta meta = reward.getItemMeta();
            colorable.setAccessible(true);
            colorable.invoke(meta, new Object[]{VillagerItemShop.this.game
                .getPlayerTeam(VillagerItemShop.this.player).getColor().getColor()});
            reward.setItemMeta(meta);
          }

          if (!(trade.getHandle()
              .getInstance() instanceof net.minecraft.server.v1_9_R1.MerchantRecipe)) {
            continue;
          }

          MerchantRecipe recipe = new MerchantRecipe(trade.getRewardItem(), 1024);
          recipe.addIngredient(trade.getItem1());

          if (trade.getItem2() != null) {
            recipe.addIngredient(trade.getItem2());
          }

          recipeList.add(recipe);
        }

        craftVillager.setRecipes(recipeList);
        craftVillager.setRiches(0);
        entityVillager.setTradingPlayer(entityHuman);

        ((CraftPlayer) player).getHandle().openTrade(entityVillager);
        ((CraftPlayer) player).getHandle().b(StatisticList.F);
      } catch (Exception ex) {
        BedwarsRel.getInstance().getBugsnag().notify(ex);
        ex.printStackTrace();
      }
    }
  }.runTask(BedwarsRel.getInstance());
}
 
Example #14
Source File: VillagerItemShop.java    From BedwarsRel with GNU General Public License v3.0 4 votes vote down vote up
public void openTrading() {
  // As task because of inventory issues
  new BukkitRunnable() {

    @SuppressWarnings("deprecation")
    @Override
    public void run() {
      try {
        EntityVillager entityVillager = VillagerItemShop.this.createVillager();
        CraftVillager craftVillager = (CraftVillager) entityVillager.getBukkitEntity();
        EntityHuman entityHuman = VillagerItemShop.this.getEntityHuman();

        // set location
        List<MerchantRecipe> recipeList = new ArrayList<MerchantRecipe>();

        for (VillagerTrade trade : VillagerItemShop.this.category
            .getFilteredOffers()) {
          ItemStack reward = trade.getRewardItem();
          Method colorable = Utils.getColorableMethod(reward.getType());

          if (Utils.isColorable(reward)) {
            reward
                .setDurability(game.getPlayerTeam(player).getColor().getDyeColor().getWoolData());
          } else if (colorable != null) {
            ItemMeta meta = reward.getItemMeta();
            colorable.setAccessible(true);
            colorable.invoke(meta, new Object[]{VillagerItemShop.this.game
                .getPlayerTeam(VillagerItemShop.this.player).getColor().getColor()});
            reward.setItemMeta(meta);
          }

          if (!(trade.getHandle()
              .getInstance() instanceof net.minecraft.server.v1_12_R1.MerchantRecipe)) {
            continue;
          }

          MerchantRecipe recipe = new MerchantRecipe(trade.getRewardItem(), 1024);
          recipe.addIngredient(trade.getItem1());

          if (trade.getItem2() != null) {
            recipe.addIngredient(trade.getItem2());
          } else {
            recipe.addIngredient(new ItemStack(Material.AIR));
          }

          recipe.setUses(0);
          recipe.setExperienceReward(false);
          recipeList.add(recipe);
        }

        craftVillager.setRecipes(recipeList);
        craftVillager.setRiches(0);
        entityVillager.setTradingPlayer(entityHuman);

        ((CraftPlayer) player).getHandle().openTrade(entityVillager);
      } catch (Exception ex) {
        BedwarsRel.getInstance().getBugsnag().notify(ex);
        ex.printStackTrace();
      }
    }
  }.runTask(BedwarsRel.getInstance());
}
 
Example #15
Source File: VillagerItemShop.java    From BedwarsRel with GNU General Public License v3.0 4 votes vote down vote up
public void openTrading() {
  // As task because of inventory issues
  new BukkitRunnable() {

    @SuppressWarnings("deprecation")
    @Override
    public void run() {
      try {
        EntityVillager entityVillager = VillagerItemShop.this.createVillager();
        CraftVillager craftVillager = (CraftVillager) entityVillager.getBukkitEntity();
        EntityHuman entityHuman = VillagerItemShop.this.getEntityHuman();

        // set location
        List<MerchantRecipe> recipeList = new ArrayList<MerchantRecipe>();

        for (VillagerTrade trade : VillagerItemShop.this.category
            .getFilteredOffers()) {
          ItemStack reward = trade.getRewardItem();
          Method colorable = Utils.getColorableMethod(reward.getType());

          if (Utils.isColorable(reward)) {
            reward
                .setDurability(game.getPlayerTeam(player).getColor().getDyeColor().getWoolData());
          } else if (colorable != null) {
            ItemMeta meta = reward.getItemMeta();
            colorable.setAccessible(true);
            colorable.invoke(meta, new Object[]{VillagerItemShop.this.game
                .getPlayerTeam(VillagerItemShop.this.player).getColor().getColor()});
            reward.setItemMeta(meta);
          }

          if (!(trade.getHandle()
              .getInstance() instanceof net.minecraft.server.v1_10_R1.MerchantRecipe)) {
            continue;
          }

          MerchantRecipe recipe = new MerchantRecipe(trade.getRewardItem(), 1024);
          recipe.addIngredient(trade.getItem1());

          if (trade.getItem2() != null) {
            recipe.addIngredient(trade.getItem2());
          }

          recipeList.add(recipe);
        }

        craftVillager.setRecipes(recipeList);
        craftVillager.setRiches(0);
        entityVillager.setTradingPlayer(entityHuman);

        ((CraftPlayer) player).getHandle().openTrade(entityVillager);
        ((CraftPlayer) player).getHandle().b(StatisticList.F);
      } catch (Exception ex) {
        BedwarsRel.getInstance().getBugsnag().notify(ex);
        ex.printStackTrace();
      }
    }
  }.runTask(BedwarsRel.getInstance());
}
 
Example #16
Source File: VillagerItemShop.java    From BedwarsRel with GNU General Public License v3.0 4 votes vote down vote up
public void openTrading() {
  // As task because of inventory issues
  new BukkitRunnable() {

    @SuppressWarnings("deprecation")
    @Override
    public void run() {
      try {
        EntityVillager entityVillager = VillagerItemShop.this.createVillager();
        CraftVillager craftVillager = (CraftVillager) entityVillager.getBukkitEntity();
        EntityHuman entityHuman = VillagerItemShop.this.getEntityHuman();

        // set location
        List<MerchantRecipe> recipeList = new ArrayList<MerchantRecipe>();

        for (VillagerTrade trade : VillagerItemShop.this.category
            .getFilteredOffers()) {
          ItemStack reward = trade.getRewardItem();
          Method colorable = Utils.getColorableMethod(reward.getType());

          if (Utils.isColorable(reward)) {
            reward
                .setDurability(game.getPlayerTeam(player).getColor().getDyeColor().getWoolData());
          } else if (colorable != null) {
            ItemMeta meta = reward.getItemMeta();
            colorable.setAccessible(true);
            colorable.invoke(meta, new Object[]{VillagerItemShop.this.game
                .getPlayerTeam(VillagerItemShop.this.player).getColor().getColor()});
            reward.setItemMeta(meta);
          }

          if (!(trade.getHandle()
              .getInstance() instanceof net.minecraft.server.v1_9_R2.MerchantRecipe)) {
            continue;
          }

          MerchantRecipe recipe = new MerchantRecipe(trade.getRewardItem(), 1024);
          recipe.addIngredient(trade.getItem1());

          if (trade.getItem2() != null) {
            recipe.addIngredient(trade.getItem2());
          }

          recipeList.add(recipe);
        }

        craftVillager.setRecipes(recipeList);
        craftVillager.setRiches(0);
        entityVillager.setTradingPlayer(entityHuman);

        ((CraftPlayer) player).getHandle().openTrade(entityVillager);
        ((CraftPlayer) player).getHandle().b(StatisticList.F);
      } catch (Exception ex) {
        BedwarsRel.getInstance().getBugsnag().notify(ex);
        ex.printStackTrace();
      }
    }
  }.runTask(BedwarsRel.getInstance());
}
 
Example #17
Source File: CraftMerchantRecipe.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
public CraftMerchantRecipe(net.minecraft.village.MerchantRecipe merchantRecipe) {
    super(CraftItemStack.asBukkitCopy(merchantRecipe.itemToSell), 0);
    this.handle = merchantRecipe;
    addIngredient(CraftItemStack.asBukkitCopy(merchantRecipe.itemToBuy));
    addIngredient(CraftItemStack.asBukkitCopy(merchantRecipe.secondItemToBuy));
}
 
Example #18
Source File: VillagerAcquireTradeEvent.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
public VillagerAcquireTradeEvent(Villager what, MerchantRecipe recipe) {
    super(what);
    this.recipe = recipe;
}
 
Example #19
Source File: CraftMerchant.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void setRecipe(int i, MerchantRecipe merchantRecipe) {
    merchant.getRecipes(null).set(i, CraftMerchantRecipe.fromBukkit(merchantRecipe).toMinecraft());
}
 
Example #20
Source File: CraftMerchant.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
@Override
public MerchantRecipe getRecipe(int i) {
    return merchant.getRecipes(null).get(i).asBukkit();
}
 
Example #21
Source File: CraftInventoryMerchant.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
@Override
public MerchantRecipe getSelectedRecipe() {
    return getInventory().getCurrentRecipe().asBukkit();
}
 
Example #22
Source File: CraftVillager.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void setRecipe(int i, MerchantRecipe merchantRecipe) {
    getMerchant().setRecipe(i, merchantRecipe);
}
 
Example #23
Source File: CraftVillager.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
@Override
public MerchantRecipe getRecipe(int i) {
    return getMerchant().getRecipe(i);
}
 
Example #24
Source File: CraftVillager.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void setRecipes(List<MerchantRecipe> recipes) {
    this.getMerchant().setRecipes(recipes);
}
 
Example #25
Source File: CraftVillager.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
@Override
public List<MerchantRecipe> getRecipes() {
    return getMerchant().getRecipes();
}
 
Example #26
Source File: VillagerReplenishTradeEvent.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
public VillagerReplenishTradeEvent(Villager what, MerchantRecipe recipe, int bonus) {
    super(what);
    this.recipe = recipe;
    this.bonus = bonus;
}
 
Example #27
Source File: VillagerReplenishTradeEvent.java    From Kettle with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Set the recipe to replenish.
 *
 * @param recipe the replenished recipe
 */
public void setRecipe(MerchantRecipe recipe) {
    this.recipe = recipe;
}
 
Example #28
Source File: VillagerReplenishTradeEvent.java    From Kettle with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Get the recipe to replenish.
 *
 * @return the replenished recipe
 */
public MerchantRecipe getRecipe() {
    return recipe;
}
 
Example #29
Source File: VillagerAcquireTradeEvent.java    From Kettle with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Set the recipe to be acquired.
 *
 * @param recipe the new recipe
 */
public void setRecipe(MerchantRecipe recipe) {
    this.recipe = recipe;
}
 
Example #30
Source File: VillagerAcquireTradeEvent.java    From Kettle with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Get the recipe to be acquired.
 *
 * @return the new recipe
 */
public MerchantRecipe getRecipe() {
    return recipe;
}