Created
January 11, 2017 12:30
-
-
Save heinrichquirit/85149ef68ece03a3284d412049e15bbb to your computer and use it in GitHub Desktop.
Spigot Plugin
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
# Strictly follow this format -- CASE SENSITIVE | |
#nameofsign: | |
# rank-name: Operator | |
# rank-price: 100 | |
trustedsign: | |
rank-name: Trusted | |
rank-price: 50 | |
membersign: | |
rank-name: Member | |
rank-price: 20 |
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 io.github.heinrichquirit.ranksellsigns.commands; | |
import org.bukkit.command.Command; | |
import org.bukkit.entity.Player; | |
/** | |
* User: Heinrich Quirit | |
* Last Modified: 1/10/17 | |
* Time: 1:18 PM | |
*/ | |
public abstract class BaseCommand { | |
public abstract void execute(Player p, Command cmd, String[] args); | |
} |
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 io.github.heinrichquirit.ranksellsigns.commands; | |
import io.github.heinrichquirit.ranksellsigns.Perm; | |
import io.github.heinrichquirit.ranksellsigns.RankSellPlugin; | |
import io.github.heinrichquirit.ranksellsigns.Utils; | |
import org.bukkit.command.Command; | |
import org.bukkit.command.CommandExecutor; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.entity.Player; | |
import static net.md_5.bungee.api.ChatColor.RED; | |
/** | |
* User: Heinrich Quirit | |
* Last Modified: 1/10/17 | |
* Time: 1:20 PM | |
*/ | |
public class CommandManager implements CommandExecutor { | |
private RankSellPlugin plugin; | |
public CommandManager(RankSellPlugin plugin) { | |
this.plugin = plugin; | |
} | |
public boolean onCommand(CommandSender sender, Command cmd, String lbl, String[] args) { | |
if (!(sender instanceof Player)) { | |
sender.sendMessage("You must use this command in-game!"); | |
return true; | |
} | |
Player p = (Player) sender; | |
if (cmd.getName().equalsIgnoreCase("rss")) { | |
if (args.length == 0) { | |
if (!Utils.hasPermission(p, Perm.RSS_USE)) { | |
p.sendMessage(RED + "You have insufficient permission for this command!"); | |
return true; | |
} | |
new InfoCommand(plugin).execute(p, cmd, args); | |
} | |
else if (args.length == 1) { | |
if (args[0].equalsIgnoreCase("list")) { | |
if (!Utils.hasPermission(p, Perm.RSS_CREATE)) { | |
p.sendMessage(RED + "You have insufficient permission for this command!"); | |
return true; | |
} | |
new ListCommand(plugin).execute(p, cmd, args); | |
} | |
else if (args[0].equalsIgnoreCase("select")) { | |
if (!Utils.hasPermission(p, Perm.RSS_CREATE)) { | |
p.sendMessage(RED + "You have insufficient permission for this command!"); | |
return true; | |
} | |
p.sendMessage(RED + "Incorrect syntax! Usage: /rss select <id>"); | |
} | |
} | |
else if (args.length == 2) { | |
if (args[0].equalsIgnoreCase("select")) { | |
if (!Utils.hasPermission(p, Perm.RSS_CREATE)) { | |
p.sendMessage(RED + "You have insufficient permission for this command!"); | |
return true; | |
} | |
new SelectCommand(plugin).execute(p, cmd, args); | |
} | |
} | |
} | |
return true; | |
} | |
} |
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 io.github.heinrichquirit.ranksellsigns.commands; | |
import io.github.heinrichquirit.ranksellsigns.RankSellPlugin; | |
import org.bukkit.command.Command; | |
import org.bukkit.entity.Player; | |
import org.bukkit.plugin.PluginDescriptionFile; | |
import static net.md_5.bungee.api.ChatColor.GRAY; | |
import static net.md_5.bungee.api.ChatColor.GREEN; | |
/** | |
* User: Heinrich Quirit | |
* Last Modified: 1/10/17 | |
* Time: 1:19 PM | |
*/ | |
public class InfoCommand extends BaseCommand { | |
private RankSellPlugin plugin; | |
public InfoCommand(RankSellPlugin plugin) { | |
this.plugin = plugin; | |
} | |
public void execute(Player p, Command cmd, String[] args) { | |
PluginDescriptionFile pdf = plugin.getDescription(); | |
p.sendMessage(GREEN + "Plugin Info."); | |
p.sendMessage(GREEN + "Name: " + GRAY + pdf.getName()); | |
p.sendMessage(GREEN + "Version: " + GRAY + pdf.getVersion()); | |
p.sendMessage(GREEN + "Dependencies: " + GRAY + pdf.getDepend()); | |
p.sendMessage(GREEN + "Author: " + GRAY + pdf.getAuthors()); | |
} | |
} |
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 io.github.heinrichquirit.ranksellsigns.commands; | |
import io.github.heinrichquirit.ranksellsigns.RankSellPlugin; | |
import net.md_5.bungee.api.chat.ComponentBuilder; | |
import net.md_5.bungee.api.chat.HoverEvent; | |
import net.md_5.bungee.api.chat.TextComponent; | |
import org.bukkit.command.Command; | |
import org.bukkit.entity.Player; | |
import static net.md_5.bungee.api.ChatColor.GRAY; | |
import static net.md_5.bungee.api.ChatColor.GREEN; | |
/** | |
* User: Heinrich Quirit | |
* Last Modified: 1/11/2017 | |
* Time: 1:43 PM | |
*/ | |
public class ListCommand extends BaseCommand { | |
private RankSellPlugin plugin; | |
public ListCommand(RankSellPlugin plugin) { | |
this.plugin = plugin; | |
} | |
public void execute(Player p, Command cmd, String[] args) { | |
p.sendMessage(GREEN + "Here are a list of rank signs to choose from."); | |
int index = 0; | |
String rankName; | |
double rankPrice; | |
for (String s : plugin.cfgKeys) { | |
index++; | |
TextComponent text = new TextComponent(GREEN + "" + index + ": " + GRAY + s); | |
rankName = plugin.getConfig().getString(s + ".rank-name"); | |
rankPrice = plugin.getConfig().getDouble(s + ".rank-price"); | |
text.setHoverEvent(new HoverEvent( | |
HoverEvent.Action.SHOW_TEXT, new ComponentBuilder( | |
GREEN + "RankName: " + GRAY + rankName | |
+ "\n" + | |
GREEN + "RankPrice: " + GRAY + rankPrice) | |
.create())); | |
p.spigot().sendMessage(text); | |
} | |
} | |
} |
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 io.github.heinrichquirit.ranksellsigns.commands; | |
import io.github.heinrichquirit.ranksellsigns.RankSellPlugin; | |
import io.github.heinrichquirit.ranksellsigns.Utils; | |
import org.bukkit.command.Command; | |
import org.bukkit.entity.Player; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.UUID; | |
import static net.md_5.bungee.api.ChatColor.GRAY; | |
import static net.md_5.bungee.api.ChatColor.GREEN; | |
/** | |
* User: Heinrich Quirit | |
* Last Modified: 1/11/2017 | |
* Time: 4:48 PM | |
*/ | |
public class SelectCommand extends BaseCommand { | |
// Each player that uses the select command | |
// will be stored in a map with their selected sign and name | |
public static Map<UUID, Integer> playerMap = new HashMap<UUID, Integer>(); | |
private RankSellPlugin plugin; | |
public SelectCommand(RankSellPlugin plugin) { | |
this.plugin = plugin; | |
} | |
public void execute(Player p, Command cmd, String[] args) { | |
int selection = Utils.parseInt(args[1]); | |
playerMap.put(p.getUniqueId(), selection); | |
p.sendMessage(GREEN + "You have selected " + GRAY + selection + "."); | |
p.sendMessage(GREEN + "When you're ready, punch a sign to create a RankSellSign!"); | |
} | |
} |
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 io.github.heinrichquirit.ranksellsigns.listeners; | |
import io.github.heinrichquirit.ranksellsigns.Perm; | |
import net.md_5.bungee.api.ChatColor; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.block.SignChangeEvent; | |
/** | |
* User: Heinrich Quirit | |
* Last Modified: 1/10/17 | |
* Time: 1:35 PM | |
*/ | |
public class SignChangeListener implements Listener { | |
/* Prevent unauthorized users to create rss sign */ | |
@EventHandler | |
public void onSignChange(SignChangeEvent e) { | |
Player p = e.getPlayer(); | |
if (e.getLine(0).contains("[RSS]")) { | |
if (!(p.hasPermission(Perm.RSS_CREATE))) { | |
e.setCancelled(true); | |
p.sendMessage(ChatColor.RED + "You do not have permission to create an RSS Sign!"); | |
} | |
} | |
} | |
} |
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 io.github.heinrichquirit.ranksellsigns.listeners; | |
import io.github.heinrichquirit.ranksellsigns.RankSellPlugin; | |
import io.github.heinrichquirit.ranksellsigns.SellSign; | |
import io.github.heinrichquirit.ranksellsigns.Utils; | |
import io.github.heinrichquirit.ranksellsigns.commands.SelectCommand; | |
import org.bukkit.Material; | |
import org.bukkit.block.Block; | |
import org.bukkit.block.Sign; | |
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.player.PlayerInteractEvent; | |
/** | |
* User: Heinrich Quirit | |
* Last Modified: 1/10/17 | |
* Time: 1:40 PM | |
*/ | |
public class SignCreateListener implements Listener { | |
private RankSellPlugin plugin; | |
public SignCreateListener(RankSellPlugin plugin) { | |
this.plugin = plugin; | |
} | |
/* Permitted users create signs */ | |
@EventHandler | |
public void onSignCreate(PlayerInteractEvent e) { | |
Player p = e.getPlayer(); | |
if (e.getAction() == Action.LEFT_CLICK_BLOCK) { | |
Block b = e.getClickedBlock(); | |
if (b.getType() == Material.SIGN_POST || b.getType() == Material.WALL_SIGN) { | |
if (SelectCommand.playerMap.containsKey(p.getUniqueId())) { | |
int selection = SelectCommand.playerMap.get(p.getUniqueId()); | |
String cfgKey = plugin.cfgMap.get(selection); | |
String rankName = plugin.getConfig().getString(cfgKey + ".rank-name"); | |
double rankPrice = plugin.getConfig().getDouble(cfgKey + ".rank-price"); | |
SellSign rss = new SellSign("[RSS]", rankName, rankPrice); | |
rss.create(((Sign) b.getState())); | |
SelectCommand.playerMap.remove(p.getUniqueId()); | |
} | |
} | |
} | |
} | |
} |
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 io.github.heinrichquirit.ranksellsigns.listeners; | |
import io.github.heinrichquirit.ranksellsigns.Perm; | |
import io.github.heinrichquirit.ranksellsigns.RankSellPlugin; | |
import io.github.heinrichquirit.ranksellsigns.Utils; | |
import org.bukkit.Material; | |
import org.bukkit.OfflinePlayer; | |
import org.bukkit.block.Block; | |
import org.bukkit.block.Sign; | |
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.player.PlayerInteractEvent; | |
import static net.md_5.bungee.api.ChatColor.RED; | |
/** | |
* User: Heinrich Quirit | |
* Last Modified: 1/11/2017 | |
* Time: 5:26 PM | |
*/ | |
public class UserBuyListener implements Listener { | |
private RankSellPlugin plugin; | |
public UserBuyListener(RankSellPlugin plugin) { | |
this.plugin = plugin; | |
} | |
@EventHandler | |
public void onRankBuy(PlayerInteractEvent e) { | |
Player p = e.getPlayer(); | |
if (e.getAction() == Action.RIGHT_CLICK_BLOCK) { | |
Block b = e.getClickedBlock(); | |
if (b.getType() == Material.SIGN_POST || b.getType() == Material.WALL_SIGN) { | |
Sign sign = (Sign) b.getState(); | |
if (sign.getLine(0).contains("[RSS]")) { | |
String rankName = sign.getLine(1); | |
double rankPrice = Integer.parseInt(sign.getLine(2)); | |
OfflinePlayer offPlayer = plugin.getServer().getOfflinePlayer(p.getUniqueId()); | |
double playerBal = RankSellPlugin.economy.getBalance(offPlayer); | |
if (!Utils.hasPermission(p, Perm.RSS_BUY)) { | |
p.sendMessage(RED + "You have insufficient permission to buy from this sign!"); | |
return; | |
} | |
if (RankSellPlugin.permission.playerInGroup(p, rankName)) { | |
p.sendMessage(RED + "You cannot buy from this sign as you already have rank: " + rankName); | |
return; | |
} | |
if (playerBal < rankPrice) { | |
p.sendMessage(RED + "You have insufficient funds! Needed: $" + (rankPrice - playerBal)); | |
return; | |
} | |
RankSellPlugin.permission.playerAddGroup(p, rankName); | |
RankSellPlugin.economy.withdrawPlayer(offPlayer, rankPrice); | |
} | |
} | |
} | |
} | |
} |
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 io.github.heinrichquirit.ranksellsigns; | |
/** | |
* User: Heinrich Quirit | |
* Last Modified: 1/10/17 | |
* Time: 1:14 PM | |
*/ | |
public class Perm { | |
public static final String RSS_BUY = "ranksellsigns.buy"; | |
public static final String RSS_USE = "ranksellsigns.use"; | |
public static final String RSS_CREATE = "ranksellsigns.create"; | |
} |
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 io.github.heinrichquirit.ranksellsigns; | |
import io.github.heinrichquirit.ranksellsigns.commands.CommandManager; | |
import io.github.heinrichquirit.ranksellsigns.listeners.SignChangeListener; | |
import io.github.heinrichquirit.ranksellsigns.listeners.SignCreateListener; | |
import io.github.heinrichquirit.ranksellsigns.listeners.UserBuyListener; | |
import net.milkbowl.vault.chat.Chat; | |
import net.milkbowl.vault.economy.Economy; | |
import net.milkbowl.vault.permission.Permission; | |
import org.bukkit.plugin.RegisteredServiceProvider; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Set; | |
/** | |
* User: Heinrich Quirit | |
* Last Modified: 1/10/17 | |
* Time: 12:53 PM | |
*/ | |
public class RankSellPlugin extends JavaPlugin { | |
// Prepare map of config id and config keys for the select command | |
public Map<Integer, String> cfgMap = new HashMap<Integer, String>(); | |
public Set<String> cfgKeys; | |
public static Permission permission = null; | |
public static Economy economy = null; | |
public void onEnable() { | |
saveDefaultConfig(); | |
registerEvents(); | |
getCommand("rss").setExecutor(new CommandManager(this)); | |
setupPermissions(); | |
setupEconomy(); | |
// Load config keys in list | |
cfgKeys = getConfig().getKeys(false); | |
// Load config keys with ids for the select command | |
int maxSize = cfgKeys.size(); | |
int i = 0; | |
for (String s : cfgKeys) { | |
i++; | |
cfgMap.put(i, s); | |
} | |
} | |
private boolean setupPermissions() { | |
RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class); | |
if (permissionProvider != null) { | |
permission = permissionProvider.getProvider(); | |
} | |
return (permission != null); | |
} | |
private boolean setupEconomy() { | |
RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class); | |
if (economyProvider != null) { | |
economy = economyProvider.getProvider(); | |
} | |
return (economy != null); | |
} | |
private void registerEvents() { | |
getServer().getPluginManager().registerEvents(new SignChangeListener(), this); | |
getServer().getPluginManager().registerEvents(new SignCreateListener(this), this); | |
getServer().getPluginManager().registerEvents(new UserBuyListener(this), this); | |
} | |
} |
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 io.github.heinrichquirit.ranksellsigns; | |
import org.bukkit.block.Sign; | |
import static net.md_5.bungee.api.ChatColor.GRAY; | |
import static net.md_5.bungee.api.ChatColor.GREEN; | |
/** | |
* User: Heinrich Quirit | |
* Last Modified: 1/10/17 | |
* Time: 12:55 PM | |
*/ | |
public class SellSign { | |
private String prefix; | |
private String rankName; | |
private double price; | |
public SellSign(String prefix, String rankName, double price) { | |
this.prefix = prefix; | |
this.rankName = rankName; | |
this.price = price; | |
} | |
public void create(Sign sign) { | |
sign.setLine(0, GREEN + prefix); | |
sign.setLine(1, GRAY + rankName); | |
sign.setLine(2, GRAY + "$" + price); | |
sign.update(); | |
} | |
} |
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 io.github.heinrichquirit.ranksellsigns; | |
import net.md_5.bungee.api.ChatColor; | |
import org.bukkit.entity.Player; | |
import java.util.Map; | |
/** | |
* User: Heinrich Quirit | |
* Last Modified: 1/11/2017 | |
* Time: 4:52 PM | |
*/ | |
public class Utils { | |
public static boolean hasPermission(Player p, String perm) { | |
return p.isOp() || p.hasPermission("*") || p.hasPermission(perm); | |
} | |
public static int parseInt(String s) { | |
try { | |
return Integer.parseInt(s); | |
} catch (NumberFormatException e) { | |
e.printStackTrace(); | |
} | |
return 0; | |
} | |
} |
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: RankSellSigns | |
main: io.github.heinrichquirit.ranksellsigns.RankSellPlugin | |
version: 1.0 | |
depend: [Vault] | |
author: BigBadHenz | |
commands: | |
rss: | |
description: Create rank sell signs. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment