Search in sources :

Example 1 with MerchantRecipe

use of org.bukkit.inventory.MerchantRecipe in project Glowstone by GlowstoneMC.

the class VillagerStore method save.

@Override
public void save(GlowVillager entity, CompoundTag tag) {
    super.save(entity, tag);
    if (entity.getProfession() != null) {
        tag.putInt("Profession", entity.getProfession().ordinal());
    }
    tag.putInt("Riches", entity.getRiches());
    tag.putBool("Willing", entity.isWilling());
    tag.putInt("CareerLevel", entity.getCareerLevel());
    // Recipes
    CompoundTag offers = new CompoundTag();
    List<CompoundTag> recipesList = new ArrayList<>();
    for (MerchantRecipe recipe : entity.getRecipes()) {
        CompoundTag recipeTag = new CompoundTag();
        recipeTag.putBool("rewardExp", recipe.hasExperienceReward());
        recipeTag.putInt("uses", recipe.getUses());
        recipeTag.putInt("maxUses", recipe.getMaxUses());
        recipeTag.putCompound("sell", NbtSerialization.writeItem(recipe.getResult(), 0));
        recipeTag.putCompound("buy", NbtSerialization.writeItem(recipe.getIngredients().get(0), 0));
        if (recipe.getIngredients().size() > 1) {
            recipeTag.putCompound("buyB", NbtSerialization.writeItem(recipe.getIngredients().get(1), 0));
        }
        recipesList.add(recipeTag);
    }
    offers.putCompoundList("Recipes", recipesList);
    tag.putCompound("Offers", offers);
// TODO: remaining data
}
Also used : MerchantRecipe(org.bukkit.inventory.MerchantRecipe) ArrayList(java.util.ArrayList) CompoundTag(net.glowstone.util.nbt.CompoundTag)

Example 2 with MerchantRecipe

use of org.bukkit.inventory.MerchantRecipe in project Denizen-For-Bukkit by DenizenScript.

the class TradeTag method duplicate.

@Override
public TradeTag duplicate() {
    MerchantRecipe result = new MerchantRecipe(recipe.getResult(), recipe.getUses(), recipe.getMaxUses(), recipe.hasExperienceReward(), recipe.getVillagerExperience(), recipe.getPriceMultiplier());
    result.setIngredients(recipe.getIngredients());
    return new TradeTag(result);
}
Also used : MerchantRecipe(org.bukkit.inventory.MerchantRecipe)

Example 3 with MerchantRecipe

use of org.bukkit.inventory.MerchantRecipe in project Glowstone by GlowstoneMC.

the class GlowVillager method sendRecipes.

private void sendRecipes(GlowMerchantInventory inventory, GlowPlayer player) {
    // TODO: Move this to a new 'GlowMerchant' class, to allow custom Merchant windows
    checkNotNull(inventory);
    checkNotNull(player);
    int windowId = player.getOpenWindowId();
    if (windowId == -1) {
        return;
    }
    ByteBuf payload = Unpooled.buffer();
    payload.writeInt(windowId);
    payload.writeByte(this.recipes.size());
    for (MerchantRecipe recipe : this.recipes) {
        if (recipe.getIngredients().isEmpty()) {
            GlowBufUtils.writeSlot(payload, InventoryUtil.createEmptyStack());
        } else {
            GlowBufUtils.writeSlot(payload, recipe.getIngredients().get(0));
        }
        GlowBufUtils.writeSlot(payload, recipe.getResult());
        boolean secondIngredient = recipe.getIngredients().size() > 1;
        payload.writeBoolean(secondIngredient);
        if (secondIngredient) {
            GlowBufUtils.writeSlot(payload, recipe.getIngredients().get(1));
        }
        // todo: no isDisabled() in MerchantRecipe?
        payload.writeBoolean(false);
        payload.writeInt(recipe.getUses());
        payload.writeInt(recipe.getMaxUses());
    }
    player.getSession().send(new PluginMessage("MC|TrList", payload.array()));
}
Also used : MerchantRecipe(org.bukkit.inventory.MerchantRecipe) PluginMessage(net.glowstone.net.message.play.game.PluginMessage) ByteBuf(io.netty.buffer.ByteBuf)

Example 4 with MerchantRecipe

use of org.bukkit.inventory.MerchantRecipe in project Glowstone by GlowstoneMC.

the class VillagerStore method load.

@Override
public void load(GlowVillager entity, CompoundTag compound) {
    super.load(entity, compound);
    compound.tryGetInt("Profession").filter(GlowVillager::isValidProfession).map(GlowVillager::getProfessionById).ifPresent(entity::setProfession);
    compound.tryGetInt("Career").map(GlowVillager::getProfessionById).ifPresent(career -> {
        entity.setProfession(career);
        entity.setCareerLevel(compound.tryGetInt("CareerLevel").orElse(1));
    });
    compound.readInt("Riches", entity::setRiches);
    compound.readBoolean("Willing", entity::setWilling);
    // Recipes
    compound.readCompound("Offers", offers -> offers.readCompoundList("Recipes", recipesList -> {
        // clear defaults
        entity.clearRecipes();
        List<MerchantRecipe> recipes = new ArrayList<>(recipesList.size());
        for (CompoundTag recipeTag : recipesList) {
            recipeTag.readItem("sell", sell -> {
                List<ItemStack> ingredients = new ArrayList<>(2);
                recipeTag.readItem("buy", ingredients::add);
                recipeTag.readItem("buyB", ingredients::add);
                boolean experienceReward = recipeTag.getBoolean("rewardExp", false);
                int uses = recipeTag.getInt("uses");
                int maxUses = recipeTag.getInt("maxUses");
                MerchantRecipe recipe = new MerchantRecipe(sell, uses, maxUses, experienceReward);
                recipe.setIngredients(ingredients);
                recipes.add(recipe);
            });
        }
        entity.setRecipes(recipes);
    }));
// TODO: remaining data
}
Also used : CompoundTag(net.glowstone.util.nbt.CompoundTag) List(java.util.List) MerchantRecipe(org.bukkit.inventory.MerchantRecipe) GlowVillager(net.glowstone.entity.passive.GlowVillager) EntityType(org.bukkit.entity.EntityType) NbtSerialization(net.glowstone.io.nbt.NbtSerialization) ItemStack(org.bukkit.inventory.ItemStack) ArrayList(java.util.ArrayList) MerchantRecipe(org.bukkit.inventory.MerchantRecipe) GlowVillager(net.glowstone.entity.passive.GlowVillager) List(java.util.List) ArrayList(java.util.ArrayList) CompoundTag(net.glowstone.util.nbt.CompoundTag)

Example 5 with MerchantRecipe

use of org.bukkit.inventory.MerchantRecipe in project Denizen-For-Bukkit by DenizenScript.

the class TradeTag method valueOf.

// <--[ObjectType]
// @name TradeTag
// @prefix trade
// @base ElementTag
// @implements PropertyHolderObject
// @format
// The identity format for trades is just the text 'trade'. All other data is specified through properties.
// 
// @description
// Merchant trades are the parts of a special merchant inventory that is typically viewed by right clicking
// a villager entity. Any number of trades can fit in a single merchant inventory.
// 
// Trades are represented by TradeTags.
// 
// The properties that can be used to customize a merchant trade are:
// 
// result=<item>
// inputs=<item>(|<item>)
// uses=<number of uses>
// max_uses=<maximum number of uses>
// has_xp=true/false
// 
// For example, the following command opens a virtual merchant inventory with two merchant trades.
// The first trade offers a sponge for two emeralds, can be used up to 10 times,
// and offers XP upon a successful transaction.
// The second trade has zero maximum uses and displays a barrier.
// <code>
// - opentrades trade[max_uses=10;inputs=emerald[quantity=2];result=sponge]|trade[result=barrier]
// </code>
// 
// -->
@Fetchable("trade")
public static TradeTag valueOf(String string, TagContext context) {
    if (string == null) {
        return null;
    }
    if (ObjectFetcher.isObjectWithProperties(string)) {
        return ObjectFetcher.getObjectFromWithProperties(TradeTag.class, string, context);
    }
    string = CoreUtilities.toLowerCase(string).replace("trade@", "");
    if (string.toLowerCase().matches("trade")) {
        MerchantRecipe recipe = new MerchantRecipe(new ItemStack(Material.AIR), 0);
        recipe.setIngredients(Collections.singletonList(new ItemStack(Material.AIR)));
        return new TradeTag(recipe);
    }
    return null;
}
Also used : MerchantRecipe(org.bukkit.inventory.MerchantRecipe) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

MerchantRecipe (org.bukkit.inventory.MerchantRecipe)9 ArrayList (java.util.ArrayList)5 TradeTag (com.denizenscript.denizen.objects.TradeTag)3 ItemStack (org.bukkit.inventory.ItemStack)3 ListTag (com.denizenscript.denizencore.objects.core.ListTag)2 List (java.util.List)2 CompoundTag (net.glowstone.util.nbt.CompoundTag)2 Merchant (org.bukkit.inventory.Merchant)2 EntityTag (com.denizenscript.denizen.objects.EntityTag)1 ItemTag (com.denizenscript.denizen.objects.ItemTag)1 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)1 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1 ByteBuf (io.netty.buffer.ByteBuf)1 GlowVillager (net.glowstone.entity.passive.GlowVillager)1 NbtSerialization (net.glowstone.io.nbt.NbtSerialization)1 PluginMessage (net.glowstone.net.message.play.game.PluginMessage)1 EntityType (org.bukkit.entity.EntityType)1