<commit_before>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package com.skelril.skree.content.itemrestriction;

import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.item.inventory.ChangeInventoryEvent;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;

import java.util.Collection;
import java.util.Optional;

public class ItemCraftBlockingListener {
    private Collection<String> blockedItems;

    public ItemCraftBlockingListener(Collection<String> blockedItems) {
        this.blockedItems = blockedItems;
    }

    @Listener
    public void onItemCraft(ChangeInventoryEvent event) {
        event.getTransactions().forEach((t) -> {
            if (blockedItems.contains(t.getFinal().getType().getId())) {
                Optional<Player> optPlayer = event.getCause().first(Player.class);
                if (optPlayer.isPresent()) {
                    Player player = optPlayer.get();
                    player.sendMessage(Text.of(TextColors.RED, "This item has been disabled."));
                }

                event.setCancelled(true);
            }
        });
    }
}<commit_msg>Don't freak out when using the hotbar to change the held item, and a blocked item is present<commit_after>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package com.skelril.skree.content.itemrestriction;

import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.item.inventory.ChangeInventoryEvent;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;

import java.util.Collection;
import java.util.Optional;

public class ItemCraftBlockingListener {
    private Collection<String> blockedItems;

    public ItemCraftBlockingListener(Collection<String> blockedItems) {
        this.blockedItems = blockedItems;
    }

    @Listener
    public void onItemCraft(ChangeInventoryEvent event) {
        if (event instanceof ChangeInventoryEvent.Held) {
            return;
        }

        event.getTransactions().forEach((t) -> {
            if (blockedItems.contains(t.getFinal().getType().getId())) {
                Optional<Player> optPlayer = event.getCause().first(Player.class);
                if (optPlayer.isPresent()) {
                    Player player = optPlayer.get();
                    player.sendMessage(Text.of(TextColors.RED, "This item has been disabled."));
                }

                event.setCancelled(true);
            }
        });
    }
}
{'fraction_non_alphanumeric': 0.10590631364562118, 'fraction_numerical': 0.0027155465037338763, 'mean_word_length': 2.8522875816993465, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1698929', 'n_tokens_mistral': 859, 'n_tokens_neox': 823, 'n_words': 192}