<commit_before>/*
 *     Copyright (c) 2016-2017 SparklingComet @ http://shanerx.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *              http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * NOTICE: All modifications made by others to the source code belong
 * to the respective contributor. No contributor should be held liable for
 * any damages of any kind, whether be material or moral, which were
 * caused by their contribution(s) to the project. See the full License for more information
 */

package org.shanerx.tradeshop;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;

import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.shanerx.tradeshop.admin.AdminEventListener;
import org.shanerx.tradeshop.commands.Executor;
import org.shanerx.tradeshop.itrade.IShopCreateEventListener;
import org.shanerx.tradeshop.itrade.ITradeEventListener;
import org.shanerx.tradeshop.trade.ShopCreateEventListener;
import org.shanerx.tradeshop.trade.TradeEventListener;

public class TradeShop extends JavaPlugin {
	
	private File messagesFile = new File(this.getDataFolder(), "messages.yml");
	private FileConfiguration messages;
	private File settingsFile = new File(this.getDataFolder(), "config.yml");
	private FileConfiguration settings;
	
	private ArrayList<Material> inventories = new ArrayList<>();
	
	public File getMessagesFile() {
		return messagesFile;
	}
	
	public FileConfiguration getMessages() {
		return messages;
	}
	
	public File getSettingsFile() {
		return settingsFile;
	}
	
	public FileConfiguration getSettings() {
		return settings;
	}
	
	@Deprecated
	@Override
	public FileConfiguration getConfig() {
		return settings;
	}
	
	@Override
	public void reloadConfig() {
        messages = YamlConfiguration.loadConfiguration(messagesFile);
        addMessageDefaults();
        settings = YamlConfiguration.loadConfiguration(settingsFile);
        addSettingsDefaults();

        addMaterials();
    }
	
	public ArrayList<Material> getAllowedInventories() {
		return inventories;
	}
	
	@Override
	public void onEnable() {
		createConfigs();
		reloadConfig();
		
		PluginManager pm = getServer().getPluginManager();
		pm.registerEvents(new TradeEventListener(this), this);
		pm.registerEvents(new ShopCreateEventListener(this), this);
		pm.registerEvents(new AdminEventListener(this), this);
		pm.registerEvents(new ITradeEventListener(this), this);
		pm.registerEvents(new IShopCreateEventListener(this), this);
		
		getCommand("tradeshop").setExecutor(new Executor(this));
	}
	
	private void addMaterials()
    {
        ArrayList<Material> AllowedOld = new ArrayList<>();
        AllowedOld.addAll(Arrays.asList(new Material[] {Material.CHEST, Material.TRAPPED_CHEST, Material.DROPPER, Material.HOPPER, Material.DISPENSER}));

        for(String str : getConfig().getStringList("allowed-shops"))
        {
            if(str.equalsIgnoreCase("shulker"))
            {
                try{
                    inventories.addAll(Arrays.asList(new Material[] {Material.BLACK_SHULKER_BOX, 
                            Material.BLUE_SHULKER_BOX, 
                            Material.BROWN_SHULKER_BOX, 
                            Material.CYAN_SHULKER_BOX, 
                            Material.GRAY_SHULKER_BOX, 
                            Material.GREEN_SHULKER_BOX, 
                            Material.LIGHT_BLUE_SHULKER_BOX, 
                            Material.LIME_SHULKER_BOX, 
                            Material.MAGENTA_SHULKER_BOX, 
                            Material.ORANGE_SHULKER_BOX, 
                            Material.PINK_SHULKER_BOX, 
                            Material.RED_SHULKER_BOX, 
                            Material.SILVER_SHULKER_BOX, 
                            Material.WHITE_SHULKER_BOX, 
                            Material.YELLOW_SHULKER_BOX, 
                            Material.PURPLE_SHULKER_BOX}));
                } catch(Throwable t){
                    getLogger().info(getName() + "You are on a version before 1.9, Shulkers are disabled!");
                }
            }
            else
            {
                if(Material.valueOf(str) != null && AllowedOld.contains(Material.valueOf(str)))
                    inventories.add(Material.valueOf(str));

            }
        }
    }
	
	private boolean addMessage(String node, String message) {
		if (messages.getString(node) == null) {
			messages.set(node, message);
			return true;
		}
		return false;
	}
	
	private boolean addSetting(String node, Object value) {
		if (settings.getString(node) == null) {
			settings.set(node, value);
			return true;
		}
		return false;
	}
	
	private void addMessageDefaults() {
		addMessage("invalid-arguments", "&eTry &6/tradeshop help &eto display help!");
		addMessage("no-command-permission", "&aYou do not have permission to execute this command");
		addMessage("setup-help", "\n&2Setting up a TradeShop is easy! Just make sure to follow these steps:"
				+ "\n \nStep 1: &ePlace down a chest."
				+ "\n&2Step 2: &ePlace a sign on top of the chest."
				+ "\n&2Step 3: &eWrite the following on the sign"
				+ "\n&6[Trade]\n<amount> <item_you_sell>\n<amount> <item_you_buy>\n&6&oEmpty line"
				+ "\n&2Step 4: &eIf you are unsure what the item is, use &6/tradeshop item");
		addMessage("no-ts-create-permission", "&cYou don't have permission to create TradeShops!");
		addMessage("no-chest", "&cYou need to put a chest under the sign!");
		addMessage("invalid-sign", "&cInvalid sign format!");
		addMessage("no-ts-destroy", "&cYou may not destroy that TradeShop");
		addMessage("successful-setup", "&aYou have sucessfully setup a TradeShop!");
		addMessage("no-ts-open", "&cThat TradeShop does not belong to you");
		addMessage("empty-ts-on-setup", "&cTradeShop empty, please remember to fill it!");
		addMessage("on-trade", "&aYou have traded your&e {AMOUNT2} {ITEM2} &a for &e {AMOUNT1} {ITEM1} &awith {SELLER}");
		addMessage("insufficient-items", "&cYou do not have &e {AMOUNT} {ITEM}&c!");
		addMessage("shop-full-amount", "&cThe shop does not have &e{AMOUNT} &cof a single type of &e{ITEM}&c!");
		addMessage("full-amount", "&cYou must have &e{AMOUNT} &cof a single type of &e{ITEM}&c!");
		addMessage("shop-empty", "&cThis TradeShop does not have &e {AMOUNT} {ITEM}&c!");
		addMessage("shop-full", "&cThis TradeShop is full, please contact the owner to get it emptied!");
		addMessage("player-full", "&cYour inventory is full, please make room before trading items!");
		addMessage("confirm-trade", "&eTrade &6 {AMOUNT1} {ITEM1} &e for &6 {AMOUNT2} {ITEM2} &e?");
		addMessage("held-item", "\n&6You are curently holding: \n&2Material: &e{MATERIAL}\n&2ID Number: &e{ID}\n&2Durability: &e{DURABILITY}\n&2Amount: &e{AMOUNT}\n&6Put this on your TradeShop sign: \n&e{AMOUNT} {MATERIAL}:{DURABILITY} \n&e{AMOUNT} {ID}:{DURABILITY}");
		addMessage("held-empty", "&eYou are currently holding nothing.");
		addMessage("player-only-command", "&eThis command is only available to players.");
		addMessage("missing-shop", "&cThere is not currently a shop here, please tell the owner or come back later!");
		
		save();
	}
	
	private void addSettingsDefaults() {
		addSetting("allowed-shops", new String[]{"CHEST", "TRAPPED_CHEST", "SHULKER"});
		
		save();
	}
	
	private void save() {
		if (messages != null)
			try {
				messages.save(messagesFile);
			} catch (IOException e) {
				e.printStackTrace();
			}
		
		if (settings != null)
			try {
				settings.save(settingsFile);
			} catch (IOException e) {
				e.printStackTrace();
			}<commit_msg>Added directions setting and needed methods<commit_after>/*
 *     Copyright (c) 2016-2017 SparklingComet @ http://shanerx.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *              http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * NOTICE: All modifications made by others to the source code belong
 * to the respective contributor. No contributor should be held liable for
 * any damages of any kind, whether be material or moral, which were
 * caused by their contribution(s) to the project. See the full License for more information
 */

package org.shanerx.tradeshop;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;

import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.shanerx.tradeshop.admin.AdminEventListener;
import org.shanerx.tradeshop.commands.Executor;
import org.shanerx.tradeshop.itrade.IShopCreateEventListener;
import org.shanerx.tradeshop.itrade.ITradeEventListener;
import org.shanerx.tradeshop.trade.ShopCreateEventListener;
import org.shanerx.tradeshop.trade.TradeEventListener;

public class TradeShop extends JavaPlugin {
	
	private File messagesFile = new File(this.getDataFolder(), "messages.yml");
	private FileConfiguration messages;
	private File settingsFile = new File(this.getDataFolder(), "config.yml");
	private FileConfiguration settings;
	
	private ArrayList<Material> inventories = new ArrayList<>();
	private ArrayList<Material> directions = new ArrayList<>();
	
	public File getMessagesFile() {
		return messagesFile;
	}
	
	public FileConfiguration getMessages() {
		return messages;
	}
	
	public File getSettingsFile() {
		return settingsFile;
	}
	
	public FileConfiguration getSettings() {
		return settings;
	}
	
	@Deprecated
	@Override
	public FileConfiguration getConfig() {
		return settings;
	}
	
	@Override
	public void reloadConfig() {
        messages = YamlConfiguration.loadConfiguration(messagesFile);
        addMessageDefaults();
        settings = YamlConfiguration.loadConfiguration(settingsFile);
        addSettingsDefaults();

        addMaterials();
	addDirections();
    }
	
	public ArrayList<Material> getAllowedInventories() {
		return inventories;
	}
	
	public ArrayList<Material> getAllowedDirections() {
		return inventories;
	}
	
	@Override
	public void onEnable() {
		createConfigs();
		reloadConfig();
		
		PluginManager pm = getServer().getPluginManager();
		pm.registerEvents(new TradeEventListener(this), this);
		pm.registerEvents(new ShopCreateEventListener(this), this);
		pm.registerEvents(new AdminEventListener(this), this);
		pm.registerEvents(new ITradeEventListener(this), this);
		pm.registerEvents(new IShopCreateEventListener(this), this);
		
		getCommand("tradeshop").setExecutor(new Executor(this));
	}
	
	private void addMaterials()
    {
        ArrayList<Material> AllowedOld = new ArrayList<>();
        AllowedOld.addAll(Arrays.asList(new Material[] {Material.CHEST, Material.TRAPPED_CHEST, Material.DROPPER, Material.HOPPER, Material.DISPENSER}));

        for(String str : getConfig().getStringList("allowed-shops"))
        {
            if(str.equalsIgnoreCase("shulker"))
            {
                try{
                    inventories.addAll(Arrays.asList(new Material[] {Material.BLACK_SHULKER_BOX, 
                            Material.BLUE_SHULKER_BOX, 
                            Material.BROWN_SHULKER_BOX, 
                            Material.CYAN_SHULKER_BOX, 
                            Material.GRAY_SHULKER_BOX, 
                            Material.GREEN_SHULKER_BOX, 
                            Material.LIGHT_BLUE_SHULKER_BOX, 
                            Material.LIME_SHULKER_BOX, 
                            Material.MAGENTA_SHULKER_BOX, 
                            Material.ORANGE_SHULKER_BOX, 
                            Material.PINK_SHULKER_BOX, 
                            Material.RED_SHULKER_BOX, 
                            Material.SILVER_SHULKER_BOX, 
                            Material.WHITE_SHULKER_BOX, 
                            Material.YELLOW_SHULKER_BOX, 
                            Material.PURPLE_SHULKER_BOX}));
                } catch(Throwable t){
                    getLogger().info(getName() + "You are on a version before 1.9, Shulkers are disabled!");
                }
            }
            else
            {
                if(Material.valueOf(str) != null && AllowedOld.contains(Material.valueOf(str)))
                    inventories.add(Material.valueOf(str));

            }
        }
    }
	
	private void addDirections()
    	{
        ArrayList<Material> Allowed = new ArrayList<>();
        AllowedOld.addAll(Arrays.asList(new Material[] {BlockFace.DOWN, BlockFace.WEST, BlockFace.SOUTH, BlockFace.EAST, BlockFace.NORTH, BlockFace.UP}));

	for(String str : getConfig().getStringList("allowed-directions"))
        {
            if(Material.valueOf(str) != null && Allowed.contains(Material.valueOf(str)))
                    directions.add(Material.valueOf(str));
        }
    }
	
	private boolean addMessage(String node, String message) {
		if (messages.getString(node) == null) {
			messages.set(node, message);
			return true;
		}
		return false;
	}
	
	private boolean addSetting(String node, Object value) {
		if (settings.getString(node) == null) {
			settings.set(node, value);
			return true;
		}
		return false;
	}
	
	private void addMessageDefaults() {
		addMessage("invalid-arguments", "&eTry &6/tradeshop help &eto display help!");
		addMessage("no-command-permission", "&aYou do not have permission to execute this command");
		addMessage("setup-help", "\n&2Setting up a TradeShop is easy! Just make sure to follow these steps:"
				+ "\n \nStep 1: &ePlace down a chest."
				+ "\n&2Step 2: &ePlace a sign on top of the chest."
				+ "\n&2Step 3: &eWrite the following on the sign"
				+ "\n&6[Trade]\n<amount> <item_you_sell>\n<amount> <item_you_buy>\n&6&oEmpty line"
				+ "\n&2Step 4: &eIf you are unsure what the item is, use &6/tradeshop item");
		addMessage("no-ts-create-permission", "&cYou don't have permission to create TradeShops!");
		addMessage("no-chest", "&cYou need to put a chest under the sign!");
		addMessage("invalid-sign", "&cInvalid sign format!");
		addMessage("no-ts-destroy", "&cYou may not destroy that TradeShop");
		addMessage("successful-setup", "&aYou have sucessfully setup a TradeShop!");
		addMessage("no-ts-open", "&cThat TradeShop does not belong to you");
		addMessage("empty-ts-on-setup", "&cTradeShop empty, please remember to fill it!");
		addMessage("on-trade", "&aYou have traded your&e {AMOUNT2} {ITEM2} &a for &e {AMOUNT1} {ITEM1} &awith {SELLER}");
		addMessage("insufficient-items", "&cYou do not have &e {AMOUNT} {ITEM}&c!");
		addMessage("shop-full-amount", "&cThe shop does not have &e{AMOUNT} &cof a single type of &e{ITEM}&c!");
		addMessage("full-amount", "&cYou must have &e{AMOUNT} &cof a single type of &e{ITEM}&c!");
		addMessage("shop-empty", "&cThis TradeShop does not have &e {AMOUNT} {ITEM}&c!");
		addMessage("shop-full", "&cThis TradeShop is full, please contact the owner to get it emptied!");
		addMessage("player-full", "&cYour inventory is full, please make room before trading items!");
		addMessage("confirm-trade", "&eTrade &6 {AMOUNT1} {ITEM1} &e for &6 {AMOUNT2} {ITEM2} &e?");
		addMessage("held-item", "\n&6You are curently holding: \n&2Material: &e{MATERIAL}\n&2ID Number: &e{ID}\n&2Durability: &e{DURABILITY}\n&2Amount: &e{AMOUNT}\n&6Put this on your TradeShop sign: \n&e{AMOUNT} {MATERIAL}:{DURABILITY} \n&e{AMOUNT} {ID}:{DURABILITY}");
		addMessage("held-empty", "&eYou are currently holding nothing.");
		addMessage("player-only-command", "&eThis command is only available to players.");
		addMessage("missing-shop", "&cThere is not currently a shop here, please tell the owner or come back later!");
		
		save();
	}
	
	private void addSettingsDefaults() {
		addSetting("allowed-shops", new String[]{"CHEST", "TRAPPED_CHEST", "SHULKER"});
		addSetting("allowed-directions", new String[]{"DOWN", "WEST", "SOUTH", "EAST", "NORTH", "UP"});
		
		save();
	}
	
	private void save() {
		if (messages != null)
			try {
				messages.save(messagesFile);
			} catch (IOException e) {
				e.printStackTrace();
			}
		
		if (settings != null)
			try {
				settings.save(settingsFile);
			} catch (IOException e) {
				e.printStackTrace();
			}
{'fraction_non_alphanumeric': 0.11699664235266875, 'fraction_numerical': 0.004862799583188607, 'mean_word_length': 3.2972636815920398, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7122', 'n_tokens_mistral': 5513, 'n_tokens_neox': 5064, 'n_words': 1340}