Last active
June 6, 2017 13:58
-
-
Save Alvin-LB/c80cd5bb287526bc7fccef8ff5762ffc to your computer and use it in GitHub Desktop.
ItemSafe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bringholm.itemsafe.bukkitutils; | |
import com.google.common.base.Charsets; | |
import com.google.common.collect.Maps; | |
import com.google.common.io.Files; | |
import org.apache.commons.lang.Validate; | |
import org.bukkit.Bukkit; | |
import org.bukkit.configuration.InvalidConfigurationException; | |
import org.bukkit.configuration.file.FileConfiguration; | |
import org.bukkit.configuration.file.YamlConfiguration; | |
import java.io.*; | |
import java.util.Map; | |
import java.util.logging.Level; | |
public class CommentYamlConfiguration extends YamlConfiguration { | |
private Map<Integer, String> comments = Maps.newHashMap(); | |
@Override | |
public void load(Reader reader) throws IOException, InvalidConfigurationException { | |
StringBuilder builder = new StringBuilder(); | |
String line; | |
try (BufferedReader input = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader)) { | |
int index = 0; | |
while ((line = input.readLine()) != null) { | |
if (line.startsWith("#") || line.isEmpty()) { | |
comments.put(index, line); | |
} | |
builder.append(line); | |
builder.append(System.lineSeparator()); | |
index++; | |
} | |
} | |
this.loadFromString(builder.toString()); | |
} | |
@Override | |
public void save(File file) throws IOException { | |
Validate.notNull(file, "File cannot be null"); | |
Files.createParentDirs(file); | |
String data = this.saveToString(); | |
if (comments.size() != 0) { | |
String[] stringArray = data.split("\n"); | |
StringBuilder stringBuilder = new StringBuilder(); | |
int arrayIndex = 0; | |
for (int i = 0; i < stringArray.length + comments.size(); i++) { | |
if (comments.containsKey(i)) { | |
stringBuilder.append(comments.get(i)).append(System.lineSeparator()); | |
} else { | |
if (arrayIndex >= stringArray.length) { | |
stringBuilder.append(System.lineSeparator()); | |
} else { | |
stringBuilder.append(stringArray[arrayIndex++]).append(System.lineSeparator()); | |
} | |
} | |
} | |
data = stringBuilder.toString(); | |
} | |
try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), Charsets.UTF_8)) { | |
writer.write(data); | |
} | |
} | |
@Override | |
protected String buildHeader() { | |
return ""; | |
} | |
@Override | |
protected String parseHeader(String input) { | |
return ""; | |
} | |
public static YamlConfiguration loadConfiguration(File file) { | |
Validate.notNull(file, "File cannot be null"); | |
YamlConfiguration config = new CommentYamlConfiguration(); | |
try { | |
config.load(file); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException | InvalidConfigurationException var4) { | |
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, var4); | |
} | |
return config; | |
} | |
public static class JavaPlugin extends org.bukkit.plugin.java.JavaPlugin { | |
private File configFile; | |
private FileConfiguration config; | |
@Override | |
public void reloadConfig() { | |
if (configFile == null) { | |
configFile = new File(this.getDataFolder(), "config.yml"); | |
} | |
config = loadConfiguration(configFile); | |
InputStream defConfigStream = this.getResource("config.yml"); | |
if (defConfigStream != null) { | |
config.setDefaults(loadConfiguration(new InputStreamReader(defConfigStream, Charsets.UTF_8))); | |
} | |
} | |
@Override | |
public FileConfiguration getConfig() { | |
if (this.config == null) { | |
this.reloadConfig(); | |
} | |
return this.config; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Configuration file for ItemSafe | |
# Notify players that they have been given an item? | |
notify-players: true | |
# Internal storage of the ItemSafe. Avoid modifying this manually, use the in-game options instead. | |
item-safe: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bringholm.itemsafe; | |
import org.bukkit.inventory.Inventory; | |
import org.bukkit.inventory.InventoryHolder; | |
public class InventoryIdentifier implements InventoryHolder { | |
private final int page; | |
public InventoryIdentifier(int page) { | |
this.page = page; | |
} | |
public int getPage() { | |
return page; | |
} | |
@Override | |
public Inventory getInventory() { | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bringholm.itemsafe; | |
import com.bringholm.itemsafe.bukkitutils.CommentYamlConfiguration; | |
import com.google.common.collect.Lists; | |
import org.apache.commons.lang.WordUtils; | |
import org.bukkit.Bukkit; | |
import org.bukkit.ChatColor; | |
import org.bukkit.command.Command; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.inventory.InventoryCloseEvent; | |
import org.bukkit.inventory.Inventory; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import java.util.*; | |
public class ItemSafe extends CommentYamlConfiguration.JavaPlugin implements Listener { | |
private List<Inventory> inventories; | |
private boolean notifyPlayers; | |
@Override | |
public void onEnable() { | |
reloadConfigValues(); | |
this.getServer().getPluginManager().registerEvents(this, this); | |
} | |
@Override | |
public void onDisable() { | |
saveConfigValues(); | |
} | |
@SuppressWarnings("unchecked") | |
private void reloadConfigValues() { | |
saveDefaultConfig(); | |
reloadConfig(); | |
notifyPlayers = getConfig().getBoolean("notify-players"); | |
Set<String> keys = getConfig().getConfigurationSection("item-safe").getKeys(false); | |
if (keys.size() == 0) { | |
inventories = Lists.<Inventory>newArrayList(Bukkit.createInventory(new InventoryIdentifier(0), 54, ChatColor.GREEN + "Page 1")); | |
} else { | |
inventories = Lists.newArrayList(); | |
for (int i = 0; i < keys.size(); i++) { | |
Inventory inventory = Bukkit.createInventory(new InventoryIdentifier(i), 54, ChatColor.GREEN + "Page " + (i + 1) + (i + 1 == keys.size() ? "" : ", next: /itemsafe open " + (i + 2))); | |
Map<String, ItemStack> inventoryContents = (Map) getConfig().getConfigurationSection("item-safe").getConfigurationSection(String.valueOf(i)).getValues(false); | |
for (int i2 = 0; i2 < 54; i2++) { | |
ItemStack item = inventoryContents.get(String.valueOf(i2)); | |
if (item != null) { | |
inventory.setItem(i2, item); | |
} | |
} | |
inventories.add(inventory); | |
} | |
} | |
} | |
private void saveConfigValues() { | |
if (inventories.size() == 1 && Arrays.stream(inventories.get(0).getContents()).allMatch(Objects::isNull)) { | |
getConfig().createSection("item-safe"); | |
} else { | |
for (int i = 0; i < inventories.size(); i++) { | |
Inventory inventory = inventories.get(i); | |
Map<String, ItemStack> inventoryContents = new HashMap<>(); | |
for (int i2 = 0; i2 < 54; i2++) { | |
ItemStack item = inventory.getItem(i2); | |
if (item != null) { | |
inventoryContents.put(String.valueOf(i2), item); | |
} | |
} | |
getConfig().createSection("item-safe." + i, inventoryContents); | |
} | |
} | |
saveConfig(); | |
} | |
@Override | |
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { | |
if (args.length > 0) { | |
if (args[0].equalsIgnoreCase("open")) { | |
if (sender.hasPermission("itemsafe.open")) { | |
if (sender instanceof Player) { | |
if (args.length > 1) { | |
try { | |
int i = Integer.parseInt(args[1]) - 1; | |
if (i < 0 || i >= inventories.size()) { | |
sender.sendMessage(addPrefix(ChatColor.RED + "'" + (i + 1) + "' is not a currently available page!")); | |
} else { | |
((Player) sender).openInventory(inventories.get(i)); | |
} | |
} catch (NumberFormatException e) { | |
sender.sendMessage(addPrefix(ChatColor.RED + "\"" + args[1] + "\" is not a valid number!")); | |
} | |
} else { | |
((Player) sender).openInventory(inventories.get(0)); | |
} | |
} else { | |
sender.sendMessage(addPrefix(ChatColor.RED + "Only players can do this!")); | |
} | |
} else { | |
sender.sendMessage(addPrefix(ChatColor.RED + "You don't have permission to do this!")); | |
} | |
} else if (args[0].equalsIgnoreCase("give")) { | |
if (sender.hasPermission("itemsafe.give")) { | |
if (args.length > 3) { | |
try { | |
int page = Integer.parseInt(args[1]) - 1; | |
if (page < 0 || page >= inventories.size()) { | |
sender.sendMessage(addPrefix(ChatColor.RED + "'" + (page + 1) + "' is not a currently available page!")); | |
} else { | |
try { | |
int id = Integer.parseInt(args[2]); | |
if (id < 0 || id > 53) { | |
sender.sendMessage(addPrefix(ChatColor.RED + "'" + id + "' is not an available index! (0-53)")); | |
} else { | |
@SuppressWarnings("deprecation") | |
Player player = Bukkit.getPlayer(args[3]); | |
if (player == null) { | |
sender.sendMessage(addPrefix(ChatColor.RED + args[3] + " is not a currently online player!")); | |
} else { | |
int amount = -1; | |
if (args.length > 4) { | |
try { | |
amount = Integer.parseInt(args[4]); | |
} catch (NumberFormatException e) { | |
sender.sendMessage(addPrefix(ChatColor.RED + "\"" + args[4] + "\" is not a valid number!")); | |
} | |
} | |
ItemStack itemStack = inventories.get(page).getItem(id); | |
if (itemStack == null) { | |
sender.sendMessage(addPrefix(ChatColor.RED + "Index " + id + " does not currently contain an item!")); | |
return true; | |
} | |
if (amount != -1) { | |
itemStack.setAmount(amount); | |
} | |
if (player.getInventory().addItem(itemStack).size() != 0) { | |
player.getWorld().dropItem(player.getLocation(), itemStack); | |
sender.sendMessage(addPrefix(ChatColor.GREEN + player.getName() + "'" + (player.getName().endsWith("s") ? "" : "s") + " inventory was full, so the item was dropped on the ground next to them.")); | |
if (notifyPlayers && !sender.equals(player)) { | |
player.sendMessage(addPrefix(ChatColor.GREEN + "You have been given " + WordUtils.capitalizeFully(itemStack.getType().name().replace("_", " ")) + ", but your inventory was full, so it was dropped on the ground.")); | |
} | |
} else { | |
sender.sendMessage(addPrefix(ChatColor.GREEN + player.getName() + " was given item " + id + " on page " + (page + 1) + "!")); | |
if (notifyPlayers && !sender.equals(player)) { | |
player.sendMessage(addPrefix(ChatColor.GREEN + "You have been given " + WordUtils.capitalizeFully(itemStack.getType().name().replace("_", " ")) + "!")); | |
} | |
} | |
} | |
} | |
} catch (NumberFormatException e) { | |
sender.sendMessage(addPrefix(ChatColor.RED + "\"" + args[2] + "\" is not a valid number!")); | |
} | |
} | |
} catch (NumberFormatException e) { | |
sender.sendMessage(addPrefix(ChatColor.RED + "\"" + args[1] + "\" is not a valid number!")); | |
} | |
} else { | |
sender.sendMessage(addPrefix(ChatColor.RED + "Incorrect arguments!")); | |
} | |
} else { | |
sender.sendMessage(addPrefix(ChatColor.RED + "You don't have permission to do this!")); | |
} | |
} else if (args[0].equalsIgnoreCase("reload")) { | |
if (sender.hasPermission("itemsafe.reload")) { | |
reloadConfigValues(); | |
sender.sendMessage(addPrefix(ChatColor.GREEN + "Reloaded config!")); | |
} else { | |
sender.sendMessage(addPrefix(ChatColor.RED + "You don't have permission to do this!")); | |
} | |
} else if (args[0].equalsIgnoreCase("help")) { | |
int i = -1; | |
if (args.length == 2) { | |
try { | |
i = Integer.parseInt(args[1]); | |
} catch (NumberFormatException ignored) { | |
} | |
} | |
if (i == -1) { | |
Bukkit.getServer().dispatchCommand(sender, "help /itemsafe"); | |
} else { | |
Bukkit.getServer().dispatchCommand(sender, "help /itemsafe " + i); | |
} | |
} else { | |
sender.sendMessage(addPrefix(ChatColor.RED + "Incorrect arguments!")); | |
} | |
} else { | |
sender.sendMessage(addPrefix(ChatColor.RED + "Incorrect arguments!")); | |
} | |
return true; | |
} | |
private String addPrefix(String message) { | |
return ChatColor.BLUE + "[" + ChatColor.GOLD + "ItemSafe" + ChatColor.BLUE + "] " + message; | |
} | |
@EventHandler | |
public void onInventoryClose(InventoryCloseEvent e) { | |
if (e.getInventory().getHolder() instanceof InventoryIdentifier) { | |
int page = ((InventoryIdentifier) e.getInventory().getHolder()).getPage(); | |
Inventory inventory = inventories.get(page); | |
if (Arrays.stream(inventory.getContents()).allMatch(Objects::isNull) && page != 0) { | |
Inventory prevInventory = inventories.get(page - 1); | |
if (Arrays.stream(prevInventory.getContents()).anyMatch(Objects::isNull) && page + 1 == inventories.size()) { | |
inventories.remove(inventory); | |
Inventory newInventory = Bukkit.createInventory(new InventoryIdentifier(page - 1), 54, ChatColor.GREEN + "Page " + page); | |
newInventory.setContents(prevInventory.getContents()); | |
inventories.set(page - 1, newInventory); | |
} | |
} else if (Arrays.stream(inventory.getContents()).allMatch(Objects::nonNull) && page + 1 == inventories.size()) { | |
Inventory newInventory = Bukkit.createInventory(new InventoryIdentifier(page + 1), 54, ChatColor.GREEN + "Page " + (page + 2)); | |
inventories.add(newInventory); | |
Inventory titleEditedInventory = Bukkit.createInventory(new InventoryIdentifier(page), 54, ChatColor.GREEN + "Page " + (page + 1) + ", next: /itemsafe open " + (page + 2)); | |
titleEditedInventory.setContents(inventory.getContents()); | |
inventories.set(page, titleEditedInventory); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: ItemSafe | |
version: 1.0 | |
main: com.bringholm.itemsafe.ItemSafe | |
author: AlvinB | |
commands: | |
itemsafe: | |
description: | | |
/itemsafe reload - Reloads the configuration file. | |
/itemsafe open [page] - Opens the ItemSafe, optionally the page specified. | |
/itemsafe give <page> <index> <player> [amount] - Gives an item from the ItemSafe to the specified player. | |
/itemsafe help [page] - Displays the help. | |
usage: /itemsafe <reload|open|give|help> [page] [index] [player] [amount] | |
permissions: | |
itemsafe.*: | |
description: Gives access to all permissions of this plugin. | |
children: | |
itemsafe.give: true | |
itemsafe.open: true | |
itemsafe.reload: true | |
itemsafe.give: | |
description: Allows access to /itemsafe give | |
default: op | |
itemsafe.open: | |
description: Allows access to /itemsafe open | |
default: op | |
itemsafe.reload: | |
description: Allows access to /itemsafe reload | |
default: op |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment