Skip to content

Instantly share code, notes, and snippets.

@osvein
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osvein/89875c5264e7f236d2bd to your computer and use it in GitHub Desktop.
Save osvein/89875c5264e7f236d2bd to your computer and use it in GitHub Desktop.
public class KitPlugin extends JavaPlugin {
private final ConcurrentHashMap<String, Kit> kits = new ConcurrentHashMap<>();
@Override
public void onEnable() {
this.saveDefaultConfig();
// read configuration
ConfigurationSection kitsConfig = this.getConfig().getConfigurationSection(CONFIG_KITS);
for (String kitName : kitsConfig.getValues(false).keySet()) {
ConfigurationSection kitConfig = kitsConfig.getConfigurationSection(kitName);
List<Map<String, Object>> rawKitContents = kitConfig.getMapList(CONFIG_KIT_CONTENTS);
ArrayList<ItemStack> kitContents = new ArrayList<>();
long kitCooldown = kitConfig.getLong(CONFIG_KIT_COOLDOWN);
for (Map<String, Object> rawKitContent : rawKitContents) {
Object rawKitContentMaterial = rawKitContent.get(CONFIG_KIT_CONTENT_MATERIAL);
Object rawKitContentAmount = rawKitContent.get(CONFIG_KIT_CONTENT_AMOUNT);
Object rawKitContentDamage = rawKitContent.get(CONFIG_KIT_CONTENT_DAMAGE);
Material kitContentMaterial = Material.getMaterial((String) rawKitContentMaterial);
int kitContentAmount = (int) rawKitContentAmount;
short kitContentDamage = (short) rawKitContentDamage;
ItemStack kitContent = new ItemStack(kitContentMaterial, kitContentAmount, kitContentDamage);
kitContents.add(kitContent);
}
Kit kit = new Kit(kitName, kitContents, kitCooldown);
this.kits.put(kitName, kit);
}
}
@Override
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String args[]) {
if (args.length >= 1) {
Kit kit = this.getKit(args[0]);
HumanEntity recipient;
if (args.length == 2) {
recipient = this.getServer().getPlayer(args[1]);
}
else if (sender instanceof HumanEntity) {
recipient = (HumanEntity) sender;
}
else {
sender.sendMessage("Missing recipient"); //TODO
return false;
}
if (!this.hasPermission(sender, kit, recipient)) {
sender.sendMessage(command.getPermissionMessage());
return false;
}
long cooldown = this.getCooldown(sender, kit);
if (cooldown > 0) {
sender.sendMessage("Cooldown " + cooldown); //TODO
return false;
}
this.setCooldown(sender, kit);
KitPlugin.dispense(sender, kit, recipient);
return true;
}
else {
//TODO
StringBuilder kitList = new StringBuilder("Available kits: ");
for (String kitName : this.getKits().keySet()) {
kitList.append(kitName);
kitList.append(", ");
}
sender.sendMessage(kitList.toString());
return false;
}
}
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String args[]) {
// TODO: Complete kit name
}
public ConcurrentMap<String, Kit> getKits() {
return this.kits;
}
public long getCooldown(CommandSender sender, Kit kit) {
Path cooldownFile = this.getCooldownFile(sender, kit);
long lastUse = Files.getLastModifiedTime(cooldownFile).toMillis();
return lastUse - System.currentTimeMillis() - kit.cooldown();
}
public void setCooldown(CommandSender sender, Kit kit) {
Path cooldownFile = this.getCooldownFile(sender, kit);
Files.setLastModifiedTime(cooldownFile, System.currentTimeMillis());
}
private Path getCooldownFile(CommandSender sender, Kit kit) {
Path cooldownDir = this.getDataFolder().toPath().resolve(PATH_COOLDOWN);
UUID id;
if (sender instanceof OfflinePlayer)
id = ((OfflinePlayer) sender).getUniqueId();
else if (sender instanceof Entity)
id = ((Entity) sender).getUniqueId();
else
id = UUID.nameUUIDFromBytes(sender.getName().getBytes());
return cooldownDir.resolve(id.toString()).resolve(kit.name());
}
public HashMap<Integer, ItemStack> dispense(Kit kit, Inventory recipient) {
return recipient.addItem(kit.contents());
}
public HashMap<Integer, ItemStack> dispense(CommandSender sender, Kit kit, HumanEntity recipient) {
String titleFormat = recipient == sender ? TEXT_INVENTORYTITLE_SELF : TEXT_INVENTORYTITLE_OTHER;
String title = String.format(titleFormat, kit.name(), sender.getName());
Inventory kitInventory = this.getServer().createInventory(null, 27, title);
HashMap<Integer, ItemStack> remainders = this.dispense(kit, kitInventory);
recipient.openInventory(kitInventory);
return remainders;
}
public boolean hasPermission(Permissible sender, Kit kit, Permissible recipient) {
String permissionFormat = recipient == sender ? PERMISSION_KIT_SELF : PERMISSION_KIT_OTHER;
String permission = String.format(permissionFormat, kit.name());
return sender.hasPermission(permission);
}
}
public class Kit {
private final String name;
private final Collection<ItemStack> contents;
private final long cooldown;
public Kit(final String name, final Collection<ItemStack> contents, final long cooldown) {
this.name = name;
this.contents = contents;
this.cooldown = cooldown;
}
public Kit(final String name, final Collection<ItemStack> contents) {
this(name, contents, 0);
}
public String name() {
return this.name;
}
public Collection<ItemStack> contents() {
ArrayList<ItemStack> clones = new ArrayList<>();
for (ItemStack content : this.contents)
clones.add(content.clone());
return clones;
}
public long cooldown() {
return this.cooldown;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment