Skip to content

Commit

Permalink
Version 1.3
Browse files Browse the repository at this point in the history
Now support reloading the config. #1
Added basic command to view currernt tax /um info #3
No more errors in console when running some commands from console. #7
If your inventory is full you can't buy or remove listings. #12
Fixed negative price bug #15
A owner can not remove their listing if someones already bought it. #16
  • Loading branch information
Xwaffle1 committed Oct 28, 2018
1 parent 826bca8 commit 01e0e77
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 33 deletions.
Expand Up @@ -32,7 +32,7 @@
authors = {
"Xwaffle"
},
version = "1.0"
version = "1.3"
)
public class UniversalMarket {

Expand Down
Expand Up @@ -29,16 +29,25 @@ public MarketCommand() {
public CommandResult process(CommandSource source, String arguments) throws CommandException {
String[] args = arguments.split(" ");

Player player = (Player) source;
Player player = null;
if (source instanceof Player) {
player = (Player) source;
}

long expireTime = UniversalMarket.getInstance().getMarket().getExpireTime();
long totalListings = UniversalMarket.getInstance().getMarket().getTotalItemsCanSell();


if (arguments.isEmpty() || arguments.equalsIgnoreCase("")) {
if (player.hasPermission("com.xwaffle.universalmarket.open")) {
UniversalMarket.getInstance().getMarket().openMarket(player);

if (player != null) {
if (player.hasPermission("com.xwaffle.universalmarket.open")) {
UniversalMarket.getInstance().getMarket().openMarket(player);
} else {
source.sendMessage(Text.of(TextColors.RED, "You do not have permission to view the market."));
}
} else {
player.sendMessage(Text.of(TextColors.RED, "You do not have permission to view the market."));
source.sendMessage(Text.of(TextColors.RED + "You can't open market from console!"));
}
return CommandResult.success();
}
Expand All @@ -47,6 +56,8 @@ public CommandResult process(CommandSource source, String arguments) throws Comm
switch (args[0].toLowerCase()) {
case "open":
case "o":
if (player == null)
break;
if (player.hasPermission("com.xwaffle.universalmarket.open")) {
UniversalMarket.getInstance().getMarket().openMarket(player);
} else {
Expand All @@ -55,8 +66,8 @@ public CommandResult process(CommandSource source, String arguments) throws Comm
break;
case "add":
case "a":


if (player == null)
break;
if (!player.hasPermission("com.xwaffle.universalmarket.add")) {
player.sendMessage(Text.of(TextColors.RED, "You do not have permission to add items to the market."));
return CommandResult.success();
Expand All @@ -76,7 +87,6 @@ public CommandResult process(CommandSource source, String arguments) throws Comm


if (UniversalMarket.getInstance().getMarket().isUsePermissionToSell()) {
System.out.println("User Perm Sell");
int userMaxSellPerm = 0;
for (int i = 1; i < 99; i++) {
if (player.hasPermission("com.xwaffle.universalmarket.addmax." + i)) {
Expand All @@ -99,6 +109,12 @@ public CommandResult process(CommandSource source, String arguments) throws Comm
double price;
try {
price = Double.parseDouble(args[1]);

if (price < 0) {
player.sendMessage(Text.of(TextColors.RED, "You must enter a positive price!"));
return CommandResult.success();

}
} catch (Exception exc) {
player.sendMessage(Text.of(TextColors.RED, "Invalid Price for Item!"));
player.sendMessage(Text.of(TextColors.YELLOW, "/um " + args[0].toLowerCase() + " (price of item in hand) (<optional> Amount)"));
Expand All @@ -113,6 +129,9 @@ public CommandResult process(CommandSource source, String arguments) throws Comm
if (amount <= 0) {
player.sendMessage(Text.of(TextColors.RED, "You must enter a positive number to sell in the market!"));
return CommandResult.success();
} else if (amount > stack.getQuantity()) {
player.sendMessage(Text.of(TextColors.RED, "You can not sell more than what you're holding."));
return CommandResult.success();
}
} catch (Exception exc) {
player.sendMessage(Text.of(TextColors.RED, "Invalid Amount for Item!"));
Expand All @@ -123,6 +142,12 @@ public CommandResult process(CommandSource source, String arguments) throws Comm

if (UniversalMarket.getInstance().getMarket().useTax()) {
double tax = price * UniversalMarket.getInstance().getMarket().getTax();

if (UniversalMarket.getInstance().getEconomyService() == null) {
source.sendMessage(Text.of(TextColors.RED, "This server is not using a currency plugin! This is required to use Universal Market!"));
return CommandResult.success();
}

UniqueAccount account = UniversalMarket.getInstance().getEconomyService().getOrCreateAccount(player.getUniqueId()).get();
Currency currency = UniversalMarket.getInstance().getEconomyService().getDefaultCurrency();
if (account.getBalance(currency).doubleValue() < tax) {
Expand Down Expand Up @@ -163,11 +188,6 @@ public CommandResult process(CommandSource source, String arguments) throws Comm
player.setItemInHand(HandTypes.MAIN_HAND, null);
} else {

if (amount > stack.getQuantity()) {
player.sendMessage(Text.of(TextColors.RED, "You can not sell more than what you're holding."));
return CommandResult.success();
}

stack.setQuantity(amount);
}

Expand All @@ -185,20 +205,35 @@ public CommandResult process(CommandSource source, String arguments) throws Comm
} else {
player.sendMessage(Text.of(TextColors.RED, "Place an item in your hand to sell!"));
}


break;
case "help":
case "h":
case "?":
player.sendMessage(Text.of(TextColors.DARK_AQUA, "Universal Market Help"));
player.sendMessage(Text.of(TextColors.YELLOW, "/um or /universalmarket"));
player.sendMessage(Text.of(TextColors.YELLOW, "/um a (price) (<optional> amount) or /um add (price) (<optional> amount)", TextColors.GRAY, " - ", TextColors.GREEN, "Sells current held ItemStack for price."));
player.sendMessage(Text.of(TextColors.YELLOW, "/um o or /um open", TextColors.GRAY, " - ", TextColors.GREEN, "Open the Universal Market."));
source.sendMessage(Text.of(TextColors.DARK_AQUA, "Universal Market Help"));
source.sendMessage(Text.of(TextColors.YELLOW, "/um or /universalmarket"));
source.sendMessage(Text.of(TextColors.YELLOW, "/um a (price) (<optional> amount) or /um add (price) (<optional> amount)", TextColors.GRAY, " - ", TextColors.GREEN, "Sells current held ItemStack for price."));
source.sendMessage(Text.of(TextColors.YELLOW, "/um o or /um open", TextColors.GRAY, " - ", TextColors.GREEN, "Open the Universal Market."));
source.sendMessage(Text.of(TextColors.YELLOW, "/um i or /um info", TextColors.GRAY, " - ", TextColors.GREEN, "Display the current configuration of the market."));
source.sendMessage(Text.of(TextColors.YELLOW, "/um r or /um reload", TextColors.GRAY, " - ", TextColors.GREEN, "Reloads the market config."));
break;
case "reload":
case "r":
if (source.hasPermission("com.xwaffle.universalmarket.reload")) {
UniversalMarket.getInstance().getMarket().reloadConfig();
source.sendMessage(Text.of(TextColors.GREEN, "Market Config Reloaded!"));
} else {
source.sendMessage(Text.of(TextColors.RED, "Youre missing permissions to reload the market!"));
}
break;
case "info":
case "i":
source.sendMessage(Text.of(TextColors.DARK_AQUA, "Current Tax Percentage: ", TextColors.AQUA, UniversalMarket.getInstance().getMarket().getTax()));
break;
}
} else {
UniversalMarket.getInstance().getMarket().openMarket(player);
if (player != null) {
UniversalMarket.getInstance().getMarket().openMarket(player);
}
}

return CommandResult.success();
Expand Down
Expand Up @@ -27,7 +27,7 @@ public MarketConfig() {
setup();
}

private Path configFile = Paths.get(UniversalMarket.getInstance().configDir.getParent() + File.separator + "UniversalMarket" + File.separator + "config.conf", new String[0]);
private Path configFile = Paths.get(UniversalMarket.getInstance().configDir.getParent() + File.separator + "UniversalMarket" + File.separator + "config.conf");
private ConfigurationLoader<CommentedConfigurationNode> configLoader = ((HoconConfigurationLoader.Builder) HoconConfigurationLoader.builder().setPath(this.configFile)).build();
private CommentedConfigurationNode configNode;

Expand All @@ -38,9 +38,9 @@ public void setup() {
configDirectory.mkdirs();
}

if (!Files.exists(this.configFile, new LinkOption[0])) {
if (!Files.exists(this.configFile)) {
try {
Files.createFile(this.configFile, new FileAttribute[0]);
Files.createFile(this.configFile);
load();
populate();
save();
Expand All @@ -53,7 +53,7 @@ public void setup() {
}

public boolean isExternal() {
return get().getNode(new Object[]{"Database", "use-external"}).getBoolean();
return get().getNode("Database", "use-external").getBoolean();
}

public void load() {
Expand Down
65 changes: 56 additions & 9 deletions src/main/java/com/xwaffle/universalmarket/market/Market.java
Expand Up @@ -18,9 +18,12 @@
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.InventoryProperty;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.entity.Hotbar;
import org.spongepowered.api.item.inventory.entity.MainPlayerInventory;
import org.spongepowered.api.item.inventory.property.SlotIndex;
import org.spongepowered.api.item.inventory.query.QueryOperation;
import org.spongepowered.api.item.inventory.query.QueryOperationTypes;
import org.spongepowered.api.service.economy.Currency;
import org.spongepowered.api.service.economy.account.UniqueAccount;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;
Expand Down Expand Up @@ -81,6 +84,11 @@ public int getFlatPrice() {
}

public Market() {
reloadConfig();
}


public void reloadConfig() {
this.totalItemsCanSell = UniversalMarket.getConfig().get().getNode("Market", "total-items-player-can-sell").getInt();
this.expireItems = UniversalMarket.getConfig().get().getNode("Market", "enable-market-expire").getBoolean();
this.expireTime = UniversalMarket.getConfig().get().getNode("Market", "time-market-expires").getLong();
Expand All @@ -89,17 +97,20 @@ public Market() {
this.payFlatPrice = UniversalMarket.getConfig().get().getNode("Market", "pay-to-sell").getBoolean();
this.flatPrice = UniversalMarket.getConfig().get().getNode("Market", "market-price").getInt();
this.usePermissionToSell = UniversalMarket.getConfig().get().getNode("Market", "use-permissions-to-sell").getBoolean();
this.useFKey = UniversalMarket.getConfig().get().getNode("Market", "f-key-open-market").getBoolean();

try {
this.blacklist = UniversalMarket.getConfig().get().getNode("Market", "blacklist").getList(TypeToken.of(String.class));
} catch (ObjectMappingException e) {
this.blacklist = new ArrayList<String>();
System.out.println("Creating new Config option.");
this.blacklist = new ArrayList<>();
UniversalMarket.getConfig().get().getNode("Market", "blacklist").setValue(new ArrayList<String>()); // Write to Config.
UniversalMarket.getConfig().save();
}

this.useFKey = UniversalMarket.getConfig().get().getNode("Market", "f-key-open-market").getBoolean();

}

List<MarketItem> marketItems = new ArrayList<>();

private List<MarketItem> marketItems = new ArrayList<>();

// private int marketID = -1;

Expand Down Expand Up @@ -190,9 +201,12 @@ public void onClickInventoryEvent(ClickInventoryEvent e) {
openMarket(player, nextPage);
}).delayTicks(1).submit(UniversalMarket.getInstance());
} else if (slotClicked == 47 && stack.getItem() == ItemTypes.NAME_TAG) {

Iterator<MarketItem> iterator = getListings().iterator();
while (iterator.hasNext()) {
if (player.getInventory().totalItems() == player.getInventory().capacity()) {
player.sendMessage(Text.of(TextColors.RED, "You do not have room in your inventory."));
break;
}
MarketItem marketItem = iterator.next();
if (marketItem.getOwnerUUID().equals(player.getUniqueId())) {
player.getInventory().offer(marketItem.getItem());
Expand Down Expand Up @@ -273,9 +287,29 @@ public void onClickInventoryEvent(ClickInventoryEvent e) {
if (e.getTransactions().size() != 0) {
int slotClicked = ((SlotAdapter) e.getTransactions().get(0).getSlot()).slotNumber;

Inventory playerInv = getMainInventory(player.getInventory());

if (slotClicked == 0 && marketItem.getOwnerUUID().equals(player.getUniqueId())) {
if (playerInv.size() == playerInv.capacity()) {
player.sendMessage(Text.of(TextColors.RED, "You do not have room in your inventory."));
Sponge.getScheduler().createTaskBuilder().execute(() ->
player.closeInventory()).submit(UniversalMarket.getInstance());

return;
}
ItemStack stack = myList.get(4).peek().get();
net.minecraft.item.ItemStack nmsStack = ItemStackUtil.toNative(stack);
NBTTagCompound nbt = nmsStack.getTagCompound();
int databaseID = nbt != null ? nbt.getInteger("id") : -1;

if (!UniversalMarket.getInstance().getMarket().doesItemExist(databaseID)) {
player.sendMessage(Text.of(TextColors.RED, "It appears that item is no longer for sale!"));
Sponge.getScheduler().createTaskBuilder().execute(() -> UniversalMarket.getInstance().getMarket().openMarket(player)).submit(UniversalMarket.getInstance());
return;
}

player.sendMessage(Text.of(TextColors.DARK_GRAY, "Removed item from UniversalMarket."));
player.getInventory().offer(marketItem.getItem());
playerInv.offer(marketItem.getItem() );
marketItem.delete();
Sponge.getScheduler().createTaskBuilder().execute(() ->
player.closeInventory()).submit(UniversalMarket.getInstance());
Expand All @@ -286,13 +320,20 @@ public void onClickInventoryEvent(ClickInventoryEvent e) {
int databaseID = nbt != null ? nbt.getInteger("id") : -1;
MarketItem item = UniversalMarket.getInstance().getMarket().getMarketItem(databaseID);
UniqueAccount account = UniversalMarket.getInstance().getEconomyService().getOrCreateAccount(player.getUniqueId()).get();
org.spongepowered.api.service.economy.Currency currency = UniversalMarket.getInstance().getEconomyService().getDefaultCurrency();
Currency currency = UniversalMarket.getInstance().getEconomyService().getDefaultCurrency();

if (!UniversalMarket.getInstance().getMarket().doesItemExist(databaseID)) {
player.sendMessage(Text.of(TextColors.RED, "It appears that item is no longer for sale!"));
Sponge.getScheduler().createTaskBuilder().execute(() -> UniversalMarket.getInstance().getMarket().openMarket(player)).submit(UniversalMarket.getInstance());
return;
}
if (playerInv.size() == playerInv.capacity()) {
player.sendMessage(Text.of(TextColors.RED, "You do not have room in your inventory."));
Sponge.getScheduler().createTaskBuilder().execute(() ->
player.closeInventory()).submit(UniversalMarket.getInstance());

return;
}


if (account.getBalance(currency).doubleValue() >= marketItem.getPrice()) {
Expand All @@ -304,7 +345,7 @@ public void onClickInventoryEvent(ClickInventoryEvent e) {
Sponge.getServer().getPlayer(marketItem.getOwnerUUID()).ifPresent(seller -> seller.sendMessage(Text.of(TextColors.YELLOW, "+ ", TextColors.GREEN, marketItem.getPrice())));
player.sendMessage(Text.of(TextColors.DARK_RED, "- ", TextColors.RED, marketItem.getPrice()));
player.sendMessage(Text.of(TextColors.YELLOW, "New Balance: ", TextColors.GREEN, account.getBalance(currency)));
player.getInventory().offer(item.getItem());
playerInv.offer(item.getItem());
Sponge.getScheduler().createTaskBuilder().execute(player::closeInventory).submit(UniversalMarket.getInstance());
} else {
player.sendMessage(Text.of(TextColors.RED, "Insufficient funds."));
Expand Down Expand Up @@ -356,6 +397,12 @@ public void onClickInventoryEvent(ClickInventoryEvent e) {

}


public Inventory getMainInventory(Inventory inventory) {
return inventory.query(QueryOperationTypes.INVENTORY_TYPE.of(Hotbar.class))
.union(inventory.query(QueryOperationTypes.INVENTORY_TYPE.of(MainPlayerInventory.class)));
}

public int countListings(UUID uniqueId) {

int listings = 0;
Expand Down

0 comments on commit 01e0e77

Please sign in to comment.