Last active
December 27, 2015 06:48
-
-
Save NeatMonster/de72a265bd4409b5b031 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 fr.neatmonster.emerauldbank; | |
import org.bukkit.Bukkit; | |
import org.bukkit.ChatColor; | |
import org.bukkit.Material; | |
import org.bukkit.command.Command; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.entity.Player; | |
import org.bukkit.entity.Villager; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.player.PlayerInteractEntityEvent; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.metadata.FixedMetadataValue; | |
import org.bukkit.plugin.java.JavaPlugin; | |
public class EmerauldBank extends JavaPlugin implements Listener { | |
@Override | |
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { | |
if (!command.getName().equalsIgnoreCase("emerauldbank")) | |
return false; | |
if (!(sender instanceof Player)) | |
sender.sendMessage(ChatColor.RED + "This command is for players only!"); | |
else { | |
final Player player = (Player) sender; | |
if (args.length != 1 || !args[0].equalsIgnoreCase("deposit") && !args[0].equalsIgnoreCase("withdraw")) | |
player.sendMessage(ChatColor.YELLOW + "Usage: /eb <withdraw/deposit>"); | |
else if (!player.hasPermission("emerauldsbank.create." + args[0].toLowerCase())) | |
player.sendMessage(ChatColor.RED + "You're not allowed to use this command!"); | |
else { | |
player.setMetadata("emerauldbank." + args[0].toLowerCase(), new FixedMetadataValue(this, null)); | |
player.sendMessage(ChatColor.GREEN + "Please select the villager you want to use."); | |
} | |
} | |
return true; | |
} | |
@Override | |
public void onEnable() { | |
Bukkit.getPluginManager().registerEvents(this, this); | |
} | |
@SuppressWarnings("deprecation") | |
@EventHandler | |
public void onPlayerInteractEntity(final PlayerInteractEntityEvent event) { | |
if (!(event.getRightClicked() instanceof Villager)) | |
return; | |
final Player player = event.getPlayer(); | |
final Villager villager = (Villager) event.getRightClicked(); | |
final String name = villager.isCustomNameVisible() ? villager.getCustomName() : ""; | |
if (name != null && (name.equals("Deposit") || name.equals("Withdraw")) | |
&& !player.hasPermission("emerauldbank.use." + name.toLowerCase())) | |
player.sendMessage(ChatColor.RED + "You're not allowed to interact with this villager."); | |
else if (name != null && name.equals("Deposit")) { | |
int count = 0; | |
for (final ItemStack stack : player.getInventory().all(Material.EMERALD).values()) | |
count += stack.getAmount(); | |
player.getInventory().remove(Material.EMERALD); | |
player.updateInventory(); | |
getConfig().set(player.getName(), getConfig().getInt(player.getName(), 0) + count); | |
player.sendMessage(ChatColor.YELLOW + (count == 0 ? "No" : "" + count) + " emerauld(s) deposed."); | |
} else if (name != null && name.equals("Withdraw")) { | |
final int count = getConfig().getInt(player.getName(), 0); | |
getConfig().set(player.getName(), 0); | |
if (count > 0) | |
player.getInventory().addItem(new ItemStack(Material.EMERALD, count)); | |
player.updateInventory(); | |
player.sendMessage(ChatColor.YELLOW + (count == 0 ? "No" : "" + count) + " emerauld(s) withdrawed."); | |
} else { | |
for (final String action : new String[] { "deposit", "withdraw" }) | |
if (player.hasMetadata("emerauldbank." + action)) { | |
villager.setCustomName(action.substring(0, 1).toUpperCase() + action.substring(1)); | |
villager.setCustomNameVisible(true); | |
player.removeMetadata("emerauldbank." + action, this); | |
player.sendMessage(ChatColor.GREEN + "This villager now allows " + action + "s."); | |
} | |
} | |
} | |
} |
This file contains hidden or 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: EmerauldBank | |
version: 1.0 | |
author: NeatMonster | |
main: fr.neatmonster.emerauldbank.EmerauldBank | |
commands: | |
emerauldbank: | |
aliases: [eb] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment