<repo_name>stevengreens10/NodeItems<file_sep>/src/me/NodeDigital/NodeItems/listeners/BlockListener.java
package me.NodeDigital.NodeItems.listeners;

import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;

import me.NodeDigital.NodeItems.NodeItemsMain;
import me.NodeDigital.NodeItems.block.BlockStorage;
import me.NodeDigital.NodeItems.block.BlockType;
import me.NodeDigital.NodeItems.block.NodeBlock;
import me.NodeDigital.NodeItems.item.NodeItems;
import net.md_5.bungee.api.ChatColor;

public class BlockListener implements Listener{

	public BlockListener(NodeItemsMain plugin) {
		plugin.getServer().getPluginManager().registerEvents(this, plugin);
	}
	
	@EventHandler
	public void onBlockPlace(BlockPlaceEvent e) {
		Player player = e.getPlayer();
		ItemStack heldItem = player.getInventory().getItemInMainHand();
		Block block = e.getBlock();
		
		if(heldItem != null && NodeItems.isItemSimilarTo(heldItem, NodeItems.SECRET_CHEST, true)) {
			BlockStorage.storeBlock(block, BlockType.SECRET_CHEST);
		}
	}
	
	@EventHandler
	public void onBlockBreak(BlockBreakEvent e) {
		Block block = e.getBlock();
		NodeBlock nodeBlock = BlockStorage.getNodeBlock(block);
		if(nodeBlock != null) {
			if(nodeBlock.getType() == BlockType.SECRET_CHEST) {
				if(nodeBlock.hasInventory()) {
				ItemStack[] contents = nodeBlock.getInventory().getContents();
					for(ItemStack item : contents) {
						if(item != null) {
							block.getWorld().dropItemNaturally(block.getLocation(), item);
						}
					}
				}
				
				nodeBlock.setInventory(null);

				block.getWorld().dropItemNaturally(block.getLocation(), NodeItems.SECRET_CHEST);
				e.setCancelled(true);
				block.setType(Material.AIR);
				BlockStorage.removeBlock(nodeBlock);
			}
		}
	}
	
	@EventHandler
	public void onPlayerInteract(PlayerInteractEvent e) {
		Player player = e.getPlayer();
		if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
			Block block = e.getClickedBlock();
			NodeBlock nodeBlock = BlockStorage.getNodeBlock(block);
			
			if(nodeBlock != null) {
				if(nodeBlock.getType() == BlockType.SECRET_CHEST && e.getPlayer().isSneaking()) {
					e.setCancelled(true);
					if(nodeBlock.getInventory() == null) {
						player.openInventory(nodeBlock.createInventory(null, 27, ChatColor.GOLD + "Secret Chest"));
					}else {
						player.openInventory(nodeBlock.getInventory());
					}
					player.getWorld().playSound(player.getLocation(), Sound.BLOCK_CHEST_OPEN, 1F, 1F);
				}
			}
		}
	}
	
}
<file_sep>/src/me/NodeDigital/NodeItems/Config.java
package me.NodeDigital.NodeItems;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import me.NodeDigital.NodeItems.block.BlockType;
import me.NodeDigital.NodeItems.block.NodeBlock;

public class Config {

	FileConfiguration config;
	String filePath;
	
	public Config(String path) {
		filePath = path;
		config = YamlConfiguration.loadConfiguration(new File(filePath));
	}
	
	public FileConfiguration getConfig() {
		try {
			config.load(filePath);
		} catch (FileNotFoundException e) {
			try {
				new File(filePath).createNewFile();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InvalidConfigurationException e) {
			e.printStackTrace();
		}
		return config;
	}
	
	public void save() {
		try {
			config.save(new File(filePath));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void set(String path, Object o) {
		config.set(path, o);
	}
	
	public void removeBlock(String path, NodeBlock nodeBlock) {
		config.set(path + "." + nodeBlock.getID(), null);
		save();
	}
	
	public List<NodeBlock> getBlocks(String path){
		List<NodeBlock> nodeBlocks = new ArrayList<NodeBlock>();
		if(config.getConfigurationSection("blocks") != null && config.getConfigurationSection("blocks").getKeys(false) != null) {
			for(int i = 0; i < config.getConfigurationSection(path).getKeys(false).size(); i++) {
				int x = config.getInt(path + "." + i + ".x");
				int y = config.getInt(path + "." + i + ".y");
				int z = config.getInt(path + "." + i + ".z");
				String world = config.getString(path + "." + i + ".world");
				String typeStr = config.getString(path + "." + i + ".type");
				if(world != null && typeStr != null) {
					BlockType type = BlockType.valueOf(typeStr);
					Block block = new Location(Bukkit.getWorld(world),x,y,z).getBlock();
					nodeBlocks.add(new NodeBlock(block, type, i));
				}
				
			}
		}
		return nodeBlocks;
	}
	
	public void setBlock(String path, Block block, BlockType type, int ID) {
		Location loc = block.getLocation();
		int x = loc.getBlockX();
		int y = loc.getBlockY();
		int z = loc.getBlockZ();
		String worldName = loc.getWorld().getName();
		config.set(path + "." + ID + ".x", x);
		config.set(path + "." + ID + ".y", y);
		config.set(path + "." + ID + ".z", z);
		config.set(path + "." + ID + ".world", worldName);
		config.set(path + "." + ID + ".material", block.getType().toString());
		config.set(path + "." + ID + ".type", type.toString());
		save();
		
	}
	
	public void setInventory(String path, Inventory inv) {
		config.set(path + ".size", inv.getSize());
		config.set(path + ".title", inv.getTitle());
		config.set(path +".contents", "");
		ItemStack[] contents = inv.getContents();
		
		for(int i = 0; i < contents.length; i++) {
			ItemStack item = contents[i];
			if(item != null) {
				setItem(path + ".contents." + i, item);
			}
		}
		
		save();
	}
	
	public void setItem(String path, ItemStack item) {
		config.set(path, item);
		save();
	}

	public Inventory getInventory(String path) {
		int size = config.getInt(path + ".size");
		String title = config.getString(path + ".title");
		if(title != null) {
			Inventory inventory = Bukkit.createInventory(null, size, title);
			for(int i = 0; i < size; i++) {
				ItemStack item = getItem(path + ".contents." + i);
				if(item != null) {
					inventory.setItem(i, item);
				}
			}
			return inventory;
		}else {
			return null;
		}
	}

	public Inventory getInventory(String path, int size, String title) {
		Inventory inventory = Bukkit.createInventory(null, size, title);
		for(int i = 0; i < size; i++) {
			ItemStack item = getItem(path + ".contents." + i);
			if(item != null) {
				inventory.setItem(i, item);
			}
		}
		return inventory;
	}

	private ItemStack getItem(String path) {
		ItemStack item = config.getItemStack(path);
		return item;
	}
	
}
<file_sep>/src/me/NodeDigital/NodeItems/tasks/JetBootsTask.java
package me.NodeDigital.NodeItems.tasks;

import org.bukkit.Bukkit;
import org.bukkit.Effect;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

public class JetBootsTask implements Runnable{

	Player player;
	int ID;
	
	public JetBootsTask(Player p) {
		player = p;
	}
	
	public void setID(int id) {
		ID = id;
	}
	
	@Override
	public void run() {
		if(player.isSprinting()) {
			player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 20, 2), true);
			player.getWorld().playSound(player.getLocation(), Sound.BLOCK_FIRE_EXTINGUISH, (float) 0.25, 5L);
			player.getWorld().playEffect(player.getLocation(), Effect.SMOKE, 5);
		}else {
			Bukkit.getScheduler().cancelTask(ID);
		}
	}

}
<file_sep>/src/me/NodeDigital/NodeItems/block/BlockType.java
package me.NodeDigital.NodeItems.block;

public enum BlockType {
	SECRET_CHEST
}
<file_sep>/src/me/NodeDigital/NodeItems/NodeItemsMain.java
package me.NodeDigital.NodeItems;

import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

import me.NodeDigital.NodeItems.block.BlockStorage;
import me.NodeDigital.NodeItems.command.NodeItemsCommand;
import me.NodeDigital.NodeItems.inventory.NodeItemsInventory;
import me.NodeDigital.NodeItems.item.NodeItems;
import me.NodeDigital.NodeItems.listeners.BlockListener;
import me.NodeDigital.NodeItems.listeners.GadgetListener;
import me.NodeDigital.NodeItems.listeners.InventoryListener;
import me.NodeDigital.NodeItems.listeners.ItemListener;
import me.NodeDigital.NodeItems.tasks.ArmorTasks;
import me.NodeDigital.NodeItems.tasks.ItemTasks;
import me.NodeDigital.NodeItems.tasks.NodeBlockTasks;

public class NodeItemsMain extends JavaPlugin{
	public void onEnable() {
		super.onEnable();
		List<ItemStack> items = NodeItems.constructItems();
		NodeItemsInventory.constructInventory(items);
		BlockStorage.loadBlocks();
		
		getCommand("ni").setExecutor(new NodeItemsCommand());
		new GadgetListener(this);
		new InventoryListener(this);
		new ItemListener(this);
		new BlockListener(this);
		
		Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new ArmorTasks(), 0L, 10L);
		Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new ItemTasks(), 0L, 10L);
		Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new NodeBlockTasks(), 0L, 10L);


	}
	
	public void onDisable() {
		
	}
}
<file_sep>/src/me/NodeDigital/NodeItems/Variables.java
package me.NodeDigital.NodeItems;

import net.md_5.bungee.api.ChatColor;

public class Variables {
	public static String PREFIX = ChatColor.GREEN + "NodeItems> " + ChatColor.RESET;
	public static String FILEPATH = "./plugins/NodeItems/";
}
<file_sep>/src/me/NodeDigital/NodeItems/inventory/NodeItemsInventory.java
package me.NodeDigital.NodeItems.inventory;

import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import net.md_5.bungee.api.ChatColor;

public class NodeItemsInventory {
	public static Inventory inventory = Bukkit.createInventory(null, 27, ChatColor.DARK_GREEN + "NodeItems");
	
	public static void constructInventory(List<ItemStack> items) {
		int slot = 0;
		for(ItemStack item : items) {
			if(slot < 27) {
				inventory.setItem(slot, item);
				slot++;
			}
		}
	}
}
<file_sep>/src/me/NodeDigital/NodeItems/command/NodeItemsCommand.java
package me.NodeDigital.NodeItems.command;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import me.NodeDigital.NodeItems.Variables;
import me.NodeDigital.NodeItems.inventory.NodeItemsInventory;
import me.NodeDigital.NodeItems.item.Backpack;
import net.md_5.bungee.api.ChatColor;

public class NodeItemsCommand implements CommandExecutor{

	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
		
		if(sender instanceof Player) {
			Player player = (Player) sender;
			if(args.length >= 1) {
				if(args[0].equalsIgnoreCase("backpack")) {
					if(args.length >= 2) {
						try {
							int id = Integer.parseInt(args[1]);
						
							player.getInventory().addItem(Backpack.getBackpack(player, id));
						}catch(Exception e) {
							player.sendMessage(Variables.PREFIX + ChatColor.RED + "The ID must be a number!");
						}
					}else {
						player.sendMessage(Variables.PREFIX + ChatColor.RED + "You must include an ID!");
					}
				}
			}else {
				player.openInventory(NodeItemsInventory.inventory);
			}
		}
		
		return false;
	}
	
}
<file_sep>/README.md
# NodeItems
A plugin for Minecraft that adds custom items into the game. Heavily inspired by Slimefun by TheBusyBiscuit.
