diff --git a/.gitignore b/.gitignore index f0105f2..00716b5 100644 --- a/.gitignore +++ b/.gitignore @@ -14,5 +14,4 @@ RemoteSystemsTempFiles/.project out/ target/ dependency-reduced-pom.xml -SaneEconomySignShop/ diff --git a/SaneEconomySignShop/pom.xml b/SaneEconomySignShop/pom.xml new file mode 100644 index 0000000..b696d0c --- /dev/null +++ b/SaneEconomySignShop/pom.xml @@ -0,0 +1,42 @@ + + + + SaneEconomy + org.appledash + 0.8.4-SNAPSHOT + + 4.0.0 + + SaneEconomySignShop + 0.1.0-SNAPSHOT + + + ${project.artifactId}-${project.version} + + + src/main/resources + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-jar-plugin + 2.4 + + ../out/ + + + + + diff --git a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/SaneEconomySignShop.java b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/SaneEconomySignShop.java new file mode 100644 index 0000000..655fb31 --- /dev/null +++ b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/SaneEconomySignShop.java @@ -0,0 +1,19 @@ +package org.appledash.saneeconomysignshop; + +import org.appledash.saneeconomysignshop.listeners.SignChangeListener; +import org.appledash.saneeconomysignshop.signshop.SignShopManager; +import org.bukkit.plugin.java.JavaPlugin; + +/** + * Created by appledash on 10/2/16. + * Blackjack is still best pony. + */ +public class SaneEconomySignShop extends JavaPlugin { + private final SignShopManager signShopManager = new SignShopManager(); + + @Override + public void onEnable() { + signShopManager.loadSignShops(); + getServer().getPluginManager().registerEvents(new SignChangeListener(this), this); + } +} diff --git a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/listeners/SignChangeListener.java b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/listeners/SignChangeListener.java new file mode 100644 index 0000000..6180889 --- /dev/null +++ b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/listeners/SignChangeListener.java @@ -0,0 +1,108 @@ +package org.appledash.saneeconomysignshop.listeners; + +import com.google.common.base.Strings; +import org.appledash.saneeconomysignshop.SaneEconomySignShop; +import org.appledash.saneeconomysignshop.signshop.SignShop; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.SignChangeEvent; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Created by appledash on 10/2/16. + * Blackjack is still best pony. + */ +public class SignChangeListener implements Listener { + private SaneEconomySignShop plugin; + + public SignChangeListener(SaneEconomySignShop plugin) { + this.plugin = plugin; + } + + @EventHandler + public void onSignChange(SignChangeEvent evt) { + + } + + private ParsedSignShop parseSignShop(SignChangeEvent evt) { + String[] lines = evt.getLines(); + Player player = evt.getPlayer(); + Location location = evt.getBlock().getLocation(); + + if ((lines[0] == null) || !lines[0].equalsIgnoreCase("Admin Shop")) { // First line must say "Admin Shop" + return new ParsedSignShop(); + } + + if (Strings.isNullOrEmpty(lines[1])) { // Second line must contain an item name + return new ParsedSignShop("No item name specified."); + } + + if (Strings.isNullOrEmpty(lines[2])) { // Second line must contain buy/sell prices + return new ParsedSignShop("No buy/sell price(s) specified."); + } + + if (Strings.isNullOrEmpty(lines[3])) { // Third line must contain item amount. + return new ParsedSignShop("No item amount specified."); + } + + String itemName = lines[1]; + String buySellRaw = lines[2]; + String amountRaw = lines[3]; + + Material mat = Material.getMaterial(itemName.toUpperCase().replace(" ", "_")); + + if (mat == null) { + // Invalid material. + return new ParsedSignShop("Invalid item name specified."); + } + + Matcher m = Pattern.compile("(B:(?[0-9.]+))?[ ]*(S:(?[0-9.]+))?").matcher(buySellRaw); + + if (!m.find()) { + return new ParsedSignShop("Invalid buy/sell prices specified."); + } + + double buy = Strings.isNullOrEmpty(m.group("buy")) ? -1.0 : Double.valueOf(m.group("buy")); + double sell = Strings.isNullOrEmpty(m.group("sell")) ? -1.0 : Double.valueOf(m.group("sell")); + + if ((buy == -1) && (sell == -1)) { + return new ParsedSignShop("Buy and sell amounts for this shop are both invalid."); + } + + int itemAmount; + + try { + itemAmount = Integer.valueOf(amountRaw); + + if (itemAmount <= 0) { + throw new NumberFormatException(); + } + } catch (NumberFormatException e) { + return new ParsedSignShop("Item amount is not a positive integer."); + } + + return new ParsedSignShop(new SignShop(player.getUniqueId(), location, mat, buy, sell)); + } + + private class ParsedSignShop { + private SignShop shop; + private String error; + + private ParsedSignShop(String error) { + this.error = error; + } + + private ParsedSignShop() { + + } + + private ParsedSignShop(SignShop shop) { + this.shop = shop; + } + } +} diff --git a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/SignShop.java b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/SignShop.java new file mode 100644 index 0000000..0c42e4d --- /dev/null +++ b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/SignShop.java @@ -0,0 +1,54 @@ +package org.appledash.saneeconomysignshop.signshop; + +import org.bukkit.Location; +import org.bukkit.Material; + +import java.util.UUID; + +/** + * Created by appledash on 10/2/16. + * Blackjack is still best pony. + */ +public class SignShop { + private final UUID ownerUuid; + private final Location location; + private final Material item; + private final double buyAmount; + private final double sellAmount; + + public SignShop(UUID ownerUuid, Location location, Material item, double buyAmount, double sellAmount) { + this.ownerUuid = ownerUuid; + this.location = location; + this.item = item; + this.buyAmount = buyAmount; + this.sellAmount = sellAmount; + } + + public Location getLocation() { + return location; + } + + public Material getItem() { + return item; + } + + public double getBuyAmount() { + return buyAmount; + } + + public double getSellAmount() { + return sellAmount; + } + + public boolean canBuy() { + return buyAmount >= 0; + } + + public boolean canSell() { + return sellAmount >= 0; + } + + public UUID getOwnerUuid() { + return ownerUuid; + } +} diff --git a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/SignShopManager.java b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/SignShopManager.java new file mode 100644 index 0000000..a85e0a9 --- /dev/null +++ b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/SignShopManager.java @@ -0,0 +1,27 @@ +package org.appledash.saneeconomysignshop.signshop; + +import org.bukkit.Location; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +/** + * Created by appledash on 10/2/16. + * Blackjack is still best pony. + */ +public class SignShopManager { + private Map signShops = new HashMap(); + + public void loadSignShops() { + + } + + public void addSignShop(SignShop signShop) { + signShops.put(signShop.getLocation(), signShop); + } + + public Optional getSignShop(Location location) { + return Optional.ofNullable(signShops.get(location)); + } +} diff --git a/SaneEconomySignShop/src/main/resources/plugin.yml b/SaneEconomySignShop/src/main/resources/plugin.yml new file mode 100644 index 0000000..9d73d86 --- /dev/null +++ b/SaneEconomySignShop/src/main/resources/plugin.yml @@ -0,0 +1,5 @@ +name: SaneEconomySignShop +main: org.appledash.saneeconomysignshop.SaneEconomySignshop +author: AppleDash +version: 0.1.0 +depend: [SaneEconomy]