Skip to content

Commit

Permalink
Complete Rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
endercrest committed Apr 4, 2014
1 parent 3139c69 commit f2b2fc0
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 49 deletions.
20 changes: 8 additions & 12 deletions com/endercrest/BlockBreakListener.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
package com.endercrest;

import org.bukkit.entity.Player;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.ChatColor;

public class BlockBreakListener implements Listener {

DisplayChest plugin;

public BlockBreakListener(DisplayChest instance) {
plugin = instance;
}

@SuppressWarnings("deprecation")
@EventHandler
public void onBlockBreak(BlockBreakEvent event){
Player p = event.getPlayer();

if(plugin.chests.contains(p.getTargetBlock(null, 5).getLocation().toString())){
event.setCancelled(true);
String msg = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Break", "&cYou Can't Break This Block."));

p.sendMessage(msg);
public void onBreakBreak(BlockBreakEvent event){
if(plugin.chests.contains(event.getBlock().getLocation().toString())){
if(event.getBlock().getType() == Material.CHEST || event.getBlock().getType() == Material.TRAPPED_CHEST){
event.setCancelled(true);
}
}
}

}
131 changes: 94 additions & 37 deletions com/endercrest/DisplayChest.java
Original file line number Diff line number Diff line change
@@ -1,74 +1,131 @@
package com.endercrest;

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

import org.bukkit.Material;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;


public class DisplayChest extends JavaPlugin {

public double version = 1.0;

public List<String> chests;
public List<String> chests_id;
public List<String> clicked = new ArrayList<String>();
public List<String> id = new ArrayList<String>();

Server server = this.getServer();
ConsoleCommandSender console = server.getConsoleSender();

Menu menu;

@Override
public void onEnable(){
this.saveDefaultConfig();
getServer().getPluginManager().registerEvents(new BlockBreakListener(this), this);
getServer().getPluginManager().registerEvents(new InventoryClickListener(this), this);
chests = (List<String>) this.getConfig().getStringList("Locations");
if(getServer().getPluginManager().getPlugin("PopupMenuAPI") == null){
this.setEnabled(false);
System.out.println("DisplayChest has been disabled. You are missing PopupMenuAPI");
}else{
this.saveDefaultConfig();
menu = new Menu(this);
getServer().getPluginManager().registerEvents(new PlayerInteractListener(this), this);
getServer().getPluginManager().registerEvents(new BlockBreakListener(this), this);
chests = (List<String>) this.getConfig().getStringList("Chests");
chests_id = (List<String>) this.getConfig().getStringList("Chests_ID");
File folder = new File(this.getDataFolder() + File.separator + "Menus");
if(!folder.exists()){
folder.mkdir();
}
File folder2 = new File(this.getDataFolder() + File.separator + "Old_Menus");
if(!folder2.exists()){
folder2.mkdir();
}
}
}

@Override
public void onDisable(){

}

@SuppressWarnings("deprecation")
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
Player p = (Player)sender;
if(p.hasPermission("dc.create")){

if(cmd.getName().equalsIgnoreCase("displaychest")){
if(p instanceof Player){
if(args.length > 1){
p.sendMessage("You have too many arguments");
}else if(args.length == 0){
p.sendMessage("To use this command: /dc [create/remove/reload]");
return true;
}else if(args.length == 1){
if(args[0].equalsIgnoreCase("create") || args[0].equalsIgnoreCase("remove") || args[0].equalsIgnoreCase("reload")){
if(sender instanceof Player){
Player p = (Player)sender;
if(p.hasPermission("dc.admin")){
if(args.length == 0){
p.sendMessage(ChatColor.GREEN + "This plugin allows you to create a display chest.");
p.sendMessage(ChatColor.GREEN + "To use: '/dc (create/remove/reload) (id)'");
p.sendMessage(ChatColor.GREEN + "This plugins current version is: " + version);
p.sendMessage(ChatColor.GREEN + "This plugin is developed by: " + ChatColor.BOLD + "tcvs");
}else if(args.length == 1){
if(args[0].equalsIgnoreCase("create") || args[0].equalsIgnoreCase("remove") || args[0].equalsIgnoreCase("reload")){
if(args[0].equalsIgnoreCase("create")){
p.sendMessage(ChatColor.RED + "You must include an id");
return true;
}
if(args[0].equalsIgnoreCase("remove")){
p.sendMessage(ChatColor.RED + "You must include an id");
return true;
}

if(args[0].equalsIgnoreCase("reload")){
p.sendMessage(ChatColor.RED + "This has not yet been implemented");
return true;
}
}
}else if(args.length == 2){
if(args[0].equalsIgnoreCase("create") || args[0].equalsIgnoreCase("remove") || args[0].equalsIgnoreCase("reload")){
if(!clicked.contains(p.getName())){
if(args[1].matches("[1-9]+")){
if(args[0].equalsIgnoreCase("create")){
//Code Goes Here To create Chest
if(p.getTargetBlock(null, 5).getType() == Material.CHEST || p.getTargetBlock(null, 5).getType() == Material.TRAPPED_CHEST){
if(chests.contains(p.getTargetBlock(null, 5).getLocation().toString())){
p.sendMessage("This Chest is already a display chest.");
}else{
p.sendMessage("Chest Added! The Chest is now locked");

chests.add(p.getTargetBlock(null, 5).getLocation().toString());
this.getConfig().set("Locations", chests);
this.saveConfig();
}
}
return true;
if(!chests_id.contains(args[1])){
p.sendMessage(ChatColor.GREEN + "The menu has been generated! This is menu id is: " + args[1]);
clicked.add(p.getName());
id.add(args[1] + "");
return true;
}else{
p.sendMessage(ChatColor.RED + "This ID has already been used.");
}
}
if(args[0].equalsIgnoreCase("remove")){
//code goes here to remove chest
if(p.getTargetBlock(null, 5).getType() == Material.CHEST && p.getTargetBlock(null, 5).getType() == Material.TRAPPED_CHEST){
chests.remove(p.getTargetBlock(null, 5).getLocation().toString());
String loc = chests.get(chests_id.indexOf(args[1].toString()));
menu.deleteFile(args[1], loc);
p.sendMessage(ChatColor.GREEN + "The menu has been removed!");
if(clicked.contains(p.getName())){
clicked.remove(p.getName());
}
if(id.contains(args[1])){
id.remove(args[1] + "");
}
return true;
return true;
}

if(args[0].equalsIgnoreCase("reload")){
this.reloadConfig();
p.sendMessage("The Config has been reloaded.");
return true;
p.sendMessage(ChatColor.RED + "This has not yet been implemented");
return true;
}
}else{
p.sendMessage(ChatColor.RED + "Your id contains an invalid character.");
}
}else{
p.sendMessage(ChatColor.RED + "You Still need to click on a chest before making another menu.");
}
}
}else if(args.length > 2){
p.sendMessage(ChatColor.RED + "Too Many Arguments.");
}
}
}else{
//console.sendMessage(Color.RED + "You must be a player to use that command.");
}
}
return true;
}
Expand Down
165 changes: 165 additions & 0 deletions com/endercrest/Menu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package com.endercrest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import me.xhawk87.PopupMenuAPI.MenuItem;
import me.xhawk87.PopupMenuAPI.PopupMenu;
import me.xhawk87.PopupMenuAPI.PopupMenuAPI;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.material.MaterialData;

public class Menu {

DisplayChest plugin;

ArrayList<MenuItem> items = new ArrayList<MenuItem>();

public Menu(DisplayChest instance) {
plugin = instance;
}

/**
* Menu
*
* @param p - The player
* @param id - The id of the menu
*/
@SuppressWarnings("deprecation")
public void MenuGUI(Player p, String id){
File idFile = new File(plugin.getDataFolder() + File.separator + "Menus", id + ".yml");
final FileConfiguration idConfig = YamlConfiguration.loadConfiguration(idFile);
final PopupMenu GUI;
GUI = PopupMenuAPI.createMenu(ChatColor.translateAlternateColorCodes('&', idConfig.get("Title").toString()), idConfig.getInt("Size"));

for(int i=0; i<idConfig.getInt("Size")*9; i++){

if(idConfig.isSet(i + ".Item_ID")){
MenuItem item = new MenuItem(ChatColor.translateAlternateColorCodes('&', idConfig.get(i + ".Name").toString()),new MaterialData(Material.getMaterial(idConfig.getInt(i + ".Item_ID")))){
@Override
public void onClick(Player player) {

}
};

if(idConfig.isSet(i + ".Sub-Text")){
List<String> sub = idConfig.getStringList(i + ".Sub-Text");
for(int s = 0; s < sub.size(); s++){
String text = sub.get(s);
item.addDescription(ChatColor.translateAlternateColorCodes('&', text));
}
}

GUI.addMenuItem(item, i);
}
}

GUI.setExitOnClickOutside(false);
GUI.openMenu(p);
}

/**
* Create a new file
*
*
* @param id - The ID of the menu.
* @param loc - The Location of the chest
*/
public void createFile(String id, String loc){
//File idFile = new File(plugin.getDataFolder() + File.separator + "Menus", id + ".yml");
//final FileConfiguration idConfig = YamlConfiguration.loadConfiguration(idFile);

loadFile("menu.yml", id);

plugin.chests.add(loc);
plugin.chests_id.add(id);
plugin.getConfig().set("Chests", plugin.chests);
plugin.getConfig().set("Chests_ID", plugin.chests_id);
plugin.saveConfig();
}

/**
* Delete file/Possibly relocate file
*
* @param id The ID of the menu.
*/
public void deleteFile(String id, String loc){
File idFile = new File(plugin.getDataFolder() + File.separator + "Menus", id + ".yml");
File oldFile = new File(plugin.getDataFolder() + File.separator + "Old_Menus", id + "_old.yml");

oldFile.delete();
idFile.renameTo(oldFile);

plugin.chests.remove(loc);
plugin.chests_id.remove(id);
plugin.getConfig().set("Chests", plugin.chests);
plugin.getConfig().set("Chests_ID", plugin.chests_id);
plugin.saveConfig();
}

/**
* Copy's default file then rename it.
*
* @param file - The file
*/
public void loadFile(String file, String id){
File t = new File(plugin.getDataFolder() + File.separator + "Menus", file);
File idFile = new File(plugin.getDataFolder() + File.separator + "Menus", id + ".yml");
//System.out.println("Writing new file: "+ t.getAbsolutePath());

try {
t.createNewFile();
FileWriter out = new FileWriter(t);
//System.out.println(file);
InputStream is = getClass().getResourceAsStream("/"+file);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
out.write(line+"\n");
//System.out.println(line);
}
out.flush();
is.close();
isr.close();
br.close();
out.close();
t.renameTo(idFile);
} catch (IOException e) {
e.printStackTrace();
}

}

/**
* Reload Config
*
* @param id The ID of the menu
*/
//TODO Reload file
public void reloadFile(String id){
//File idFile = new File(plugin.getDataFolder() + File.separator + "Menus", id + ".yml");
//final FileConfiguration idConfig = YamlConfiguration.loadConfiguration(idFile);
}

/**
* Reload all config.
*/
//TODO Reload all configs
public void reloadAllFiles(){

}


}

0 comments on commit f2b2fc0

Please sign in to comment.