Search in sources :

Example 1 with QuestContext

use of com.playmonumenta.scriptedquests.quests.QuestContext in project scripted-quests by TeamMonumenta.

the class InteractableManager method handleEvent.

private boolean handleEvent(Plugin plugin, Player player, ItemStack item, InteractType type, List<InteractableEntry> entries) {
    boolean cancelEvent = false;
    QuestContext context = new QuestContext(plugin, player, null, false, null, item);
    for (InteractableEntry entry : entries) {
        if (entry.interactEvent(context, type)) {
            cancelEvent = true;
        }
    }
    return cancelEvent;
}
Also used : InteractableEntry(com.playmonumenta.scriptedquests.quests.InteractableEntry) QuestContext(com.playmonumenta.scriptedquests.quests.QuestContext)

Example 2 with QuestContext

use of com.playmonumenta.scriptedquests.quests.QuestContext in project scripted-quests by TeamMonumenta.

the class NpcTradeManager method onSuccessfulTrade.

/* This is a successful trade - clicking with an empty cursor on a valid result item */
private void onSuccessfulTrade(InventoryClickEvent event) {
    Player player = (Player) event.getWhoClicked();
    MerchantInventory merchInv = (MerchantInventory) event.getInventory();
    PlayerTradeContext context = mOpenTrades.get(player.getUniqueId());
    /*
		 * Have to manually compute which slot this was because merchInv.getSelectedIndex returns the wrong index
		 * if the player leaves the trade on the first slot but puts in materials for one of the other trades
		 */
    MerchantRecipe recipe = merchInv.getSelectedRecipe();
    List<MerchantRecipe> recipes = merchInv.getMerchant().getRecipes();
    int selectedIndex = recipes.indexOf(recipe);
    if (selectedIndex < 0) {
        player.sendMessage(ChatColor.YELLOW + "BUG! Somehow the recipe you selected couldn't be found. Please report this, and include which villager and what you were trading for");
    } else {
        NpcTrade trade = context.getSlotProperties().get(selectedIndex);
        if (trade != null) {
            trade.doActions(new QuestContext(Plugin.getInstance(), player, context.getVillager()));
        }
    }
}
Also used : MerchantRecipe(org.bukkit.inventory.MerchantRecipe) Player(org.bukkit.entity.Player) MerchantInventory(org.bukkit.inventory.MerchantInventory) QuestContext(com.playmonumenta.scriptedquests.quests.QuestContext) NpcTrade(com.playmonumenta.scriptedquests.trades.NpcTrade)

Example 3 with QuestContext

use of com.playmonumenta.scriptedquests.quests.QuestContext in project scripted-quests by TeamMonumenta.

the class Race method lose.

public void lose() {
    if (mLoseActions != null) {
        mLoseActions.doActions(new QuestContext(mPlugin, mPlayer, null));
    }
    mPlayer.teleport(mStopLoc);
    end();
}
Also used : QuestContext(com.playmonumenta.scriptedquests.quests.QuestContext)

Example 4 with QuestContext

use of com.playmonumenta.scriptedquests.quests.QuestContext in project scripted-quests by TeamMonumenta.

the class ClientChatProtocol method sendPacket.

public static void sendPacket(List<QuestComponent> packet, QuestContext context) {
    // implements `S->C 'actions'`
    JsonObject data = JsonObjectBuilder.get().add("type", "actions").add("data", packet.stream().map(v -> v.serializeForClientAPI(context)).map(v -> v.orElse(null)).collect(Collectors.toList())).build();
    sendJson(context.getPlayer(), data);
}
Also used : JsonObject(com.google.gson.JsonObject) CommandSender(org.bukkit.command.CommandSender) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Constants(com.playmonumenta.scriptedquests.Constants) Set(java.util.Set) StandardByteReader(com.floweytf.utils.stdstreams.StandardByteReader) UUID(java.util.UUID) Player(org.bukkit.entity.Player) Collectors(java.util.stream.Collectors) CommandExecutor(org.bukkit.command.CommandExecutor) Plugin(com.playmonumenta.scriptedquests.Plugin) JsonElement(com.google.gson.JsonElement) QuestComponent(com.playmonumenta.scriptedquests.quests.components.QuestComponent) HashSet(java.util.HashSet) IStandardByteReader(com.floweytf.utils.stdstreams.IStandardByteReader) List(java.util.List) PluginMessageListener(org.bukkit.plugin.messaging.PluginMessageListener) ByteArrayInputStream(java.io.ByteArrayInputStream) StandardByteWriter(com.floweytf.utils.stdstreams.StandardByteWriter) Gson(com.google.gson.Gson) Command(org.bukkit.command.Command) QuestContext(com.playmonumenta.scriptedquests.quests.QuestContext) JsonObject(com.google.gson.JsonObject)

Example 5 with QuestContext

use of com.playmonumenta.scriptedquests.quests.QuestContext in project scripted-quests by TeamMonumenta.

the class NpcTradeManager method trade.

/**
 * Initiates a player trade with a villager. Instead of trading with the villager directly,
 * we generate a fake merchant to do the trading. We override this trade event for multiple reasons:
 * <li>This allows multiple players to trade with the same villager at the same time</li>
 * <li>This way, the villager does not gain any trade experience</li>
 * <li>We can filter out randomly-generated vanilla trades, and hide trades based on quest prerequisites</li>
 * @param plugin Used for metadata
 * @param villager The villager to trade with
 * @param player The player trading
 * @param event The interact event that initiated the trade
 */
public void trade(Plugin plugin, Villager villager, Player player, PlayerInteractEntityEvent event) {
    ArrayList<MerchantRecipe> trades = new ArrayList<>();
    NpcTrader trader = mTraders.get(QuestNpc.squashNpcName(villager.getName()));
    StringBuilder lockedSlots = new StringBuilder();
    StringBuilder vanillaSlots = new StringBuilder();
    boolean modified = false;
    // We don't want any vanilla trades to occur, regardless of if trades were changed or not.
    // As a side-effect, right-clicking a villager will not activate interactables
    // This is fine for now, but if we ever want interactables to work on villagers, we need to change this
    event.setCancelled(true);
    Map<Integer, NpcTrade> slotProperties = new HashMap<>();
    // We need to tag the outer loop so inner loops can continue it
    recipes: for (int i = 0; i < villager.getRecipeCount(); i++) {
        MerchantRecipe recipe = villager.getRecipe(i);
        // Remove vanilla trades (those with a regular emerald in any slot)
        List<ItemStack> items = recipe.getIngredients();
        items.add(recipe.getResult());
        for (ItemStack item : items) {
            if (item != null && item.getType() == Material.EMERALD && !item.hasItemMeta()) {
                // Found emerald with no item data
                if (vanillaSlots.length() != 0) {
                    vanillaSlots.append(", ");
                }
                vanillaSlots.append(i);
                modified = true;
                continue recipes;
            }
        }
        // Remove unmatched prereq trades
        if (trader != null) {
            NpcTrade trade = trader.getTrade(i);
            if (trade != null) {
                if (!trade.prerequisiteMet(new QuestContext(plugin, player, villager))) {
                    if (lockedSlots.length() != 0) {
                        lockedSlots.append(", ");
                    }
                    lockedSlots.append(i);
                    modified = true;
                    continue;
                } else {
                    slotProperties.put(trades.size(), trade);
                }
            }
        }
        // This trade was not filtered by any of the above checks. Add to the fake merchant
        trades.add(recipe);
    }
    if (modified && player.getGameMode() == GameMode.CREATIVE && player.isOp()) {
        player.sendMessage(ChatColor.GOLD + "Some trader slots were not shown to you:");
        if (lockedSlots.length() > 0) {
            player.sendMessage(ChatColor.GOLD + "These slots were locked by quest scores: " + lockedSlots);
        }
        if (vanillaSlots.length() > 0) {
            player.sendMessage(ChatColor.GOLD + "These slots contained a vanilla emerald: " + vanillaSlots);
        }
        player.sendMessage(ChatColor.GOLD + "This message only appears to operators in creative mode");
    }
    /*
		 * If this villager still has trades, create a temporary fake merchant to interact with the player
		 * This allows multiple players to trade with the same NPC at the same time, and also gives score-limited trades
		 */
    if (trades.size() > 0) {
        List<TradeWindowOpenEvent.Trade> eventTrades = new ArrayList<>();
        for (int i = 0; i < trades.size(); i++) {
            NpcTrade npcTrade = slotProperties.get(i);
            eventTrades.add(new TradeWindowOpenEvent.Trade(trades.get(i), npcTrade != null ? npcTrade.getActions() : null));
        }
        TradeWindowOpenEvent tradeEvent = new TradeWindowOpenEvent(player, eventTrades);
        Bukkit.getPluginManager().callEvent(tradeEvent);
        if (!tradeEvent.isCancelled() && !tradeEvent.getTrades().isEmpty()) {
            new BukkitRunnable() {

                @Override
                public void run() {
                    Merchant merchant = Bukkit.createMerchant(villager.getName());
                    final List<TradeWindowOpenEvent.Trade> newEventTrades = tradeEvent.getTrades();
                    Map<Integer, NpcTrade> newSlotProperties = new HashMap<>();
                    for (int i = 0; i < newEventTrades.size(); i++) {
                        NpcTrade npcTrade = new NpcTrade(i, new QuestPrerequisites(), newEventTrades.get(i).getActions());
                        newSlotProperties.put(i, npcTrade);
                    }
                    merchant.setRecipes(newEventTrades.stream().map(TradeWindowOpenEvent.Trade::getRecipe).collect(Collectors.toList()));
                    mOpenTrades.put(player.getUniqueId(), new PlayerTradeContext(newSlotProperties, villager, merchant));
                    player.openMerchant(merchant, true);
                }
            }.runTaskLater(plugin, 1);
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NpcTrade(com.playmonumenta.scriptedquests.trades.NpcTrade) Merchant(org.bukkit.inventory.Merchant) ArrayList(java.util.ArrayList) List(java.util.List) NpcTrader(com.playmonumenta.scriptedquests.trades.NpcTrader) MerchantRecipe(org.bukkit.inventory.MerchantRecipe) TradeWindowOpenEvent(com.playmonumenta.scriptedquests.trades.TradeWindowOpenEvent) QuestContext(com.playmonumenta.scriptedquests.quests.QuestContext) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) QuestPrerequisites(com.playmonumenta.scriptedquests.quests.components.QuestPrerequisites) NpcTrade(com.playmonumenta.scriptedquests.trades.NpcTrade) ItemStack(org.bukkit.inventory.ItemStack) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

QuestContext (com.playmonumenta.scriptedquests.quests.QuestContext)8 JsonElement (com.google.gson.JsonElement)2 JsonObject (com.google.gson.JsonObject)2 NpcTrade (com.playmonumenta.scriptedquests.trades.NpcTrade)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Set (java.util.Set)2 IStandardByteReader (com.floweytf.utils.stdstreams.IStandardByteReader)1 StandardByteReader (com.floweytf.utils.stdstreams.StandardByteReader)1 StandardByteWriter (com.floweytf.utils.stdstreams.StandardByteWriter)1 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 Constants (com.playmonumenta.scriptedquests.Constants)1 Plugin (com.playmonumenta.scriptedquests.Plugin)1 InteractableEntry (com.playmonumenta.scriptedquests.quests.InteractableEntry)1 QuestComponent (com.playmonumenta.scriptedquests.quests.components.QuestComponent)1 QuestPrerequisites (com.playmonumenta.scriptedquests.quests.components.QuestPrerequisites)1 ActionBase (com.playmonumenta.scriptedquests.quests.components.actions.ActionBase)1 ActionCommand (com.playmonumenta.scriptedquests.quests.components.actions.ActionCommand)1 ActionDialog (com.playmonumenta.scriptedquests.quests.components.actions.ActionDialog)1