<commit_before>package com.Acrobot.ChestShop.Listeners.PreShopCreation;

import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.Breeze.Utils.StringUtil;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;

import java.util.regex.Matcher;

import static com.Acrobot.Breeze.Utils.MaterialUtil.*;
import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.INVALID_ITEM;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.ITEM_LINE;

/**
 * @author Acrobot
 */
public class ItemChecker implements Listener {
    private static final short MAXIMUM_SIGN_LETTERS = 15;

    @EventHandler(priority = EventPriority.LOWEST)
    public static void onPreShopCreation(PreShopCreationEvent event) {
        String itemCode = event.getSignLine(ITEM_LINE);
        ItemStack item = MaterialUtil.getItem(itemCode);

        if (item == null) {
            event.setOutcome(INVALID_ITEM);
            return;
        }

        if (Odd.getFromString(itemCode) != null) {
            return; // The OddItem name is OK
        }

        String metadata = getMetadata(itemCode);
        String longName = MaterialUtil.getName(item, LONG_NAME);

        if (longName.length() <= (MAXIMUM_SIGN_LETTERS - metadata.length())) {
            if (isStillValidItem(longName + metadata, item)) {
                String itemName = StringUtil.capitalizeFirstLetter(longName + metadata);

                event.setSignLine(ITEM_LINE, itemName);
                return;
            }
        }

        String code = MaterialUtil.getName(item, SHORT_NAME);

        String[] parts = itemCode.split("(?=:|-|#)", 2);
        String data = (parts.length > 1 ? parts[1] : "");

        if (!data.isEmpty() && code.length() >= (MAXIMUM_SIGN_LETTERS - 1 - data.length())) {
            code = code.substring(0, MAXIMUM_SIGN_LETTERS - 1 - data.length()) + data;
        }

        if (!isStillValidItem(code, item)) {
            code = String.valueOf(item.getTypeId()) + data;
        }

        code = StringUtil.capitalizeFirstLetter(code);

        event.setSignLine(ITEM_LINE, code);
    }

    private static boolean isStillValidItem(String newCode, ItemStack item) {
        ItemStack newItem = MaterialUtil.getItem(newCode);

        return newItem != null && MaterialUtil.equals(newItem, item);
    }

    private static String getMetadata(String itemCode) {
        Matcher m = METADATA.matcher(itemCode);

        if (!m.find()) {
            return "";
        }

        return m.group();
    }
}
<commit_msg>Refractoring + fixed a minor bug<commit_after>package com.Acrobot.ChestShop.Listeners.PreShopCreation;

import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.Breeze.Utils.StringUtil;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;

import java.util.regex.Matcher;

import static com.Acrobot.Breeze.Utils.MaterialUtil.*;
import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.INVALID_ITEM;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.ITEM_LINE;

/**
 * @author Acrobot
 */
public class ItemChecker implements Listener {
    private static final short MAXIMUM_SIGN_LETTERS = 15;

    @EventHandler(priority = EventPriority.LOWEST)
    public static void onPreShopCreation(PreShopCreationEvent event) {
        String itemCode = event.getSignLine(ITEM_LINE);
        ItemStack item = MaterialUtil.getItem(itemCode);

        if (item == null) {
            event.setOutcome(INVALID_ITEM);
            return;
        }

        if (Odd.getFromString(itemCode) != null) {
            return; // The OddItem name is OK
        }

        String metadata = getMetadata(itemCode);
        String longName = MaterialUtil.getName(item);

        if (longName.length() <= (MAXIMUM_SIGN_LETTERS - metadata.length())) {
            if (isSameItem(longName + metadata, item)) {
                String itemName = StringUtil.capitalizeFirstLetter(longName + metadata);

                event.setSignLine(ITEM_LINE, itemName);
                return;
            }
        }

        String code = MaterialUtil.getName(item, SHORT_NAME);

        String[] parts = itemCode.split("(?=:|-|#)", 2);
        String data = (parts.length > 1 ? parts[1] : "");

        if (!data.isEmpty() && code.length() > (MAXIMUM_SIGN_LETTERS - data.length())) {
            code = code.substring(0, MAXIMUM_SIGN_LETTERS - data.length());
        }

        code += data;

        if (!isSameItem(code, item)) {
            code = String.valueOf(item.getTypeId()) + data;
        }

        code = StringUtil.capitalizeFirstLetter(code);

        event.setSignLine(ITEM_LINE, code);
    }

    private static boolean isSameItem(String newCode, ItemStack item) {
        ItemStack newItem = MaterialUtil.getItem(newCode);

        return newItem != null && MaterialUtil.equals(newItem, item);
    }

    private static String getMetadata(String itemCode) {
        Matcher m = METADATA.matcher(itemCode);

        if (!m.find()) {
            return "";
        }

        return m.group();
    }
}

{'fraction_non_alphanumeric': 0.1128157156220767, 'fraction_numerical': 0.0026192703461178674, 'mean_word_length': 2.810406272273699, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '4263645', 'n_tokens_mistral': 1640, 'n_tokens_neox': 1577, 'n_words': 306}