java类org.bukkit.inventory.MerchantRecipe的实例源码

MerchantRecipeConverter.java 文件源码 项目:SimpleEgg 阅读 21 收藏 0 点赞 0 评论 0
/**
 * Returns a String representation of a MerchantRecipe. Spaces are used as
 * delimeters between attributes. Utilizes
 * {@link ItemStackConverter ItemStackConverter} for the ItemStacks.
 * </b>
 * @param merchantRecipe - The MerchantRecipe to stringify.
 * @return A String that can be stored and passed in to
 * {@link #fromString(String) fromString(String)} later.
 * @throws IllegalArgumentException If MerchantRecipe is null.
 */
public static String toString(MerchantRecipe merchantRecipe) throws IllegalArgumentException {
    if (merchantRecipe == null) {
        throw new IllegalArgumentException("Cannot stringify a null MerchantRecipe!");
    }

    String ret = new String();
    ret += ItemStackConverter.toString(merchantRecipe.getResult()) + " ";

    for (ItemStack itemStack : merchantRecipe.getIngredients()) {
        ret += ItemStackConverter.toString(itemStack) + ",";
    }

    ret += " " + merchantRecipe.getUses();
    ret += " " + merchantRecipe.getMaxUses();
    ret += " " + merchantRecipe.hasExperienceReward();
    return ret;
}
MerchantRecipeConverter.java 文件源码 项目:SimpleEgg 阅读 20 收藏 0 点赞 0 评论 0
/**
 * Takes in a String produced by
 * {@link #toString(MerchantRecipe) toString(MerchantRecipe)} and converts
 * it to a MerchantRecipe.
 * @param string - The String to convert.
 * @return A MerchantRecipe with the attributes specified in the String.
 * @throws IllegalArgumentException If the String has an unexpected number
 * of attributes.
 */
public static MerchantRecipe fromString(String string) throws IllegalArgumentException {
    String[] attributes = string.split(" ");

    if (attributes.length != 5) {
        throw new IllegalArgumentException("Input string has an unexpected number of attributes!");
    }

    String resultString = attributes[0], ingredientString = attributes[1], usesString = attributes[2], maxUsesString = attributes[3], experienceString = attributes[4];

    ItemStack result = itemStackListFromString(resultString).get(0);
    ArrayList<ItemStack> ingredients = itemStackListFromString(ingredientString);
    int uses = Integer.parseInt(usesString);
    int maxUses = Integer.parseInt(maxUsesString);
    boolean experience = Boolean.parseBoolean(experienceString);
    MerchantRecipe ret = new MerchantRecipe(result, uses, maxUses, experience);
    ret.setIngredients(ingredients);
    return ret;
}
NMSHandler.java 文件源码 项目:Shopkeepers 阅读 33 收藏 0 点赞 0 评论 0
@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;
}
NMSHandler.java 文件源码 项目:Shopkeepers 阅读 34 收藏 0 点赞 0 评论 0
@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;
}
NMSHandler.java 文件源码 项目:Shopkeepers 阅读 26 收藏 0 点赞 0 评论 0
@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;
}
NMSHandler.java 文件源码 项目:Shopkeepers 阅读 24 收藏 0 点赞 0 评论 0
@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;
}
CraftVillager.java 文件源码 项目:SpigotSource 阅读 18 收藏 0 点赞 0 评论 0
@Override
public List<MerchantRecipe> getRecipes() {
    return Collections.unmodifiableList(Lists.transform(getHandle().getOffers(null), new Function<net.minecraft.server.MerchantRecipe, MerchantRecipe>() {
        @Override
        public MerchantRecipe apply(net.minecraft.server.MerchantRecipe recipe) {
            return recipe.asBukkit();
        }
    }));
}
CraftVillager.java 文件源码 项目:SpigotSource 阅读 21 收藏 0 点赞 0 评论 0
@Override
public void setRecipes(List<MerchantRecipe> list) {
    MerchantRecipeList recipes = getHandle().getOffers(null);
    recipes.clear();
    for (MerchantRecipe m : list) {
        recipes.add(CraftMerchantRecipe.fromBukkit(m).toMinecraft());
    }
}
CraftMerchantRecipe.java 文件源码 项目:SpigotSource 阅读 19 收藏 0 点赞 0 评论 0
public CraftMerchantRecipe(ItemStack result, int uses, int maxUses, boolean experienceReward) {
    super(result, uses, maxUses, experienceReward);
    this.handle = new net.minecraft.server.MerchantRecipe(
            null,
            null,
            CraftItemStack.asNMSCopy(result),
            uses,
            maxUses,
            this
    );
}
CraftMerchantRecipe.java 文件源码 项目:SpigotSource 阅读 23 收藏 0 点赞 0 评论 0
public net.minecraft.server.MerchantRecipe toMinecraft() {
    List<ItemStack> ingredients = getIngredients();
    Preconditions.checkState(!ingredients.isEmpty(), "No offered ingredients");
    handle.buyingItem1 = CraftItemStack.asNMSCopy(ingredients.get(0));
    if (ingredients.size() > 1) {
        handle.buyingItem2 = CraftItemStack.asNMSCopy(ingredients.get(1));
    }
    return handle;
}
CraftMerchantRecipe.java 文件源码 项目:SpigotSource 阅读 17 收藏 0 点赞 0 评论 0
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;
    }
}
NMSHandler.java 文件源码 项目:Shopkeepers 阅读 26 收藏 0 点赞 0 评论 0
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;
}
NMSHandler.java 文件源码 项目:Shopkeepers 阅读 30 收藏 0 点赞 0 评论 0
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;
}
MerchantUtils.java 文件源码 项目:WC 阅读 21 收藏 0 点赞 0 评论 0
public static List<MerchantRecipe> stringToRecipe(String s){
    List<MerchantRecipe> recipes = new ArrayList<>();
    List<String> recipe = Arrays.asList(s.replace("[", "").replace("]", "").split(","));

    return recipes;
}
SNMob.java 文件源码 项目:WC 阅读 19 收藏 0 点赞 0 评论 0
public MerchantRecipe[] getRecipes(){
    Villager v;
    if (!mu.isVillager(entity)) return null;
    v = (Villager)entity;
    return v.getRecipes().toArray(new MerchantRecipe[v.getRecipes().size()]);
}
SNMob.java 文件源码 项目:PA 阅读 22 收藏 0 点赞 0 评论 0
public MerchantRecipe[] getRecipes(){
    Villager v;
    if (!mu.isVillager(entity)) return null;
    v = (Villager)entity;
    return v.getRecipes().toArray(new MerchantRecipe[v.getRecipes().size()]);
}
EEntity.java 文件源码 项目:GameBoxx 阅读 20 收藏 0 点赞 0 评论 0
public List<MerchantRecipe> getRecipes() {
    if (entity instanceof Villager) {
        return ((Villager)entity).getRecipes();
    }
    return new ArrayList<>();
}
EEntity.java 文件源码 项目:GameBoxx 阅读 18 收藏 0 点赞 0 评论 0
public EEntity setRecipes(List<MerchantRecipe> recipes) {
    if (entity instanceof Villager) {
        ((Villager)entity).setRecipes(recipes);
    }
    return this;
}
EEntity.java 文件源码 项目:GameBoxx 阅读 25 收藏 0 点赞 0 评论 0
public MerchantRecipe getRecipe(int i) {
    if (entity instanceof Villager) {
        return ((Villager)entity).getRecipe(i);
    }
    return null;
}
EEntity.java 文件源码 项目:GameBoxx 阅读 22 收藏 0 点赞 0 评论 0
public EEntity setRecipe(int i, MerchantRecipe recipe) {
    if (entity instanceof Villager) {
        ((Villager)entity).setRecipe(i, recipe);
    }
    return this;
}
VillagerItemShop.java 文件源码 项目:BedwarsRel 阅读 23 收藏 0 点赞 0 评论 0
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());
}
VillagerItemShop.java 文件源码 项目:BedwarsRel 阅读 23 收藏 0 点赞 0 评论 0
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());
}
VillagerItemShop.java 文件源码 项目:BedwarsRel 阅读 24 收藏 0 点赞 0 评论 0
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());
}
VillagerItemShop.java 文件源码 项目:BedwarsRel 阅读 20 收藏 0 点赞 0 评论 0
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());
}
VillagerItemShop.java 文件源码 项目:BedwarsRel 阅读 23 收藏 0 点赞 0 评论 0
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());
}
CraftVillager.java 文件源码 项目:SpigotSource 阅读 23 收藏 0 点赞 0 评论 0
@Override
public MerchantRecipe getRecipe(int i) {
    return getHandle().getOffers(null).get(i).asBukkit();
}
CraftVillager.java 文件源码 项目:SpigotSource 阅读 21 收藏 0 点赞 0 评论 0
@Override
public void setRecipe(int i, MerchantRecipe merchantRecipe) {
    getHandle().getOffers(null).set(i, CraftMerchantRecipe.fromBukkit(merchantRecipe).toMinecraft());
}
CraftInventoryMerchant.java 文件源码 项目:SpigotSource 阅读 28 收藏 0 点赞 0 评论 0
@Override
public MerchantRecipe getSelectedRecipe() {
    return getInventory().getRecipe().asBukkit();
}
CraftMerchantRecipe.java 文件源码 项目:SpigotSource 阅读 23 收藏 0 点赞 0 评论 0
public CraftMerchantRecipe(net.minecraft.server.MerchantRecipe merchantRecipe) {
    super(CraftItemStack.asBukkitCopy(merchantRecipe.sellingItem), 0);
    this.handle = merchantRecipe;
    addIngredient(CraftItemStack.asBukkitCopy(merchantRecipe.buyingItem1));
    addIngredient(CraftItemStack.asBukkitCopy(merchantRecipe.buyingItem2));
}


问题


面经


文章

微信
公众号

扫码关注公众号