Skip to content

Commit

Permalink
Merge pull request #90 from EpiCanard/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
EpiCanard authored Jul 14, 2019
2 parents 091b5c6 + e4c5fdb commit 19837f0
Show file tree
Hide file tree
Showing 29 changed files with 867 additions and 123 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
<modelVersion>4.0.0</modelVersion>
<groupId>fr.epicanard</groupId>
<artifactId>globalmarketchest</artifactId>
<version>1.3.0</version>
<version>1.3.1</version>
<name>GlobalMarketChest</name>
<description>Shop plugin to create economy between user</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<powermock.version>1.7.1</powermock.version>
<spigot.version>1.14.2-R0.1-SNAPSHOT</spigot.version>
<spigot.version>1.14.3-R0.1-SNAPSHOT</spigot.version>
</properties>
<build>
<finalName>${project.name}-${project.version}</finalName>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class AuctionInfo {
private Integer id;
@Getter
private String itemStack;
@Getter
@Getter @Setter
private String itemMeta;
@Getter @Setter
private Integer amount;
Expand All @@ -49,17 +49,17 @@ public AuctionInfo(ResultSet res) {
if (res == null)
throw new NullPointerException("Fail to get auction from database");
this.id = DatabaseUtils.getField("id", res::getInt);
this.itemStack = DatabaseUtils.getField("itemStack", res::getString);
this.itemMeta = DatabaseUtils.getField("itemMeta", res::getString);
this.itemStack = DatabaseUtils.getField("itemStack", res::getString);
this.itemMeta = DatabaseUtils.getField("itemMeta", res::getString);
this.amount = DatabaseUtils.getField("amount", res::getInt);
this.price = DatabaseUtils.getField("price", res::getDouble);
this.ended = DatabaseUtils.getField("ended", res::getBoolean);
this.type = AuctionType.getAuctionType(DatabaseUtils.getField("type", res::getInt));
this.playerStarter = DatabaseUtils.getField("playerStarter", res::getString);
this.playerEnder = DatabaseUtils.getField("playerEnder", res::getString);
this.start = DatabaseUtils.getField("start", res::getTimestamp);
this.end = DatabaseUtils.getField("end", res::getTimestamp);
this.group = DatabaseUtils.getField("group", res::getString);
this.playerStarter = DatabaseUtils.getField("playerStarter", res::getString);
this.playerEnder = DatabaseUtils.getField("playerEnder", res::getString);
this.start = DatabaseUtils.getField("start", res::getTimestamp);
this.end = DatabaseUtils.getField("end", res::getTimestamp);
this.group = DatabaseUtils.getField("group", res::getString);
this.state = StateAuction.getStateAuction(this);

if (this.itemMeta == null && this.itemStack != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang3.StringUtils;
import org.bukkit.command.Command;
Expand All @@ -13,6 +14,7 @@

import fr.epicanard.globalmarketchest.commands.consumers.CloseConsumer;
import fr.epicanard.globalmarketchest.commands.consumers.DetailConsumer;
import fr.epicanard.globalmarketchest.commands.consumers.FixAuctionsConsumer;
import fr.epicanard.globalmarketchest.commands.consumers.HelpConsumer;
import fr.epicanard.globalmarketchest.commands.consumers.ListConsumer;
import fr.epicanard.globalmarketchest.commands.consumers.OpenConsumer;
Expand All @@ -21,6 +23,7 @@
import fr.epicanard.globalmarketchest.commands.consumers.VersionConsumer;
import fr.epicanard.globalmarketchest.GlobalMarketChest;
import fr.epicanard.globalmarketchest.permissions.Permissions;
import fr.epicanard.globalmarketchest.shops.ShopInfo;
import fr.epicanard.globalmarketchest.utils.PlayerUtils;
import fr.epicanard.globalmarketchest.utils.WorldUtils;

Expand Down Expand Up @@ -71,6 +74,21 @@ public CommandHandler() {
.setCommand(new TPConsumer())
.setTabConsumer(this::shopIdTabComplete);
listNode.addSubNode(tpNode);

// Fix - /globalmarketchest fix
CommandNode fixNode = new CommandNode("fix", Permissions.CMD_ADMIN_FIX, false, false);
this.command.addSubNode(fixNode);

// Fix Auctions - /globalmarketchest fix auctions [type]
CommandNode fixAuctionsNode = new CommandNode("auctions", Permissions.CMD_ADMIN_FIX, true, false)
.setCommand(new FixAuctionsConsumer())
.setTabConsumer((player, args) -> {
if (args.length == 1)
return FixAuctionsConsumer.getFixAuctionsType().stream().filter((type) -> type.startsWith(args[0])).collect(Collectors.toList());
return new ArrayList<>();
});
fixNode.addSubNode(fixAuctionsNode);

}

/**
Expand All @@ -95,7 +113,7 @@ public List<String> onTabComplete(CommandSender sender, Command cmd, String msg,

/**
* Return a list of shops matching with start of the first element of args param
*
*
* @param sender Commands's executor (player or console)
* @param args Command arguments
* @return List of shops matching
Expand All @@ -115,27 +133,35 @@ private List<String> shopsTabComplete(CommandSender sender, String[] args) {
* Return a list of shops matching with start of the first element of args param
* If the lenght of args is equal to 2
* Return a list of shop's position with start of the second element of args param
*
*
* @param sender Commands's executor (player or console)
* @param args Command arguments
* @return List of shops matching
*/
private List<String> shopIdTabComplete(CommandSender sender, String[] args) {
if (args.length == 1) {
return this.shopsTabComplete(sender, args);
}
if (args.length == 2) {
return GlobalMarketChest.plugin.shopManager.getShops().stream()
.filter(shop -> shop.getGroup().equals(args[0]) && Integer.toString(shop.getId()).startsWith(args[1]))
.map(shop -> WorldUtils.getStringFromLocation(shop.getSignLocation(), ",", true))
.collect(Collectors.toList());
if (args.length == 1 || args.length == 2) {
final Stream<ShopInfo> shopsStream = GlobalMarketChest.plugin.shopManager.getShops().stream()
.filter(shop -> shop.getExists());

if (args.length == 1) {
return shopsStream
.filter(shop -> shop.getGroup().startsWith(args[0]))
.map(shop -> shop.getGroup())
.collect(Collectors.toList());
}
if (args.length == 2) {
return shopsStream
.filter(shop -> shop.getGroup().equals(args[0]) && Integer.toString(shop.getId()).startsWith(args[1]))
.map(shop -> WorldUtils.getStringFromLocation(shop.getSignLocation(), ",", true))
.collect(Collectors.toList());
}
}
return new ArrayList<>();
}

/**
* Return a list of players matching with start of the first element of args param
*
*
* @param sender Commands's executor (player or console)
* @param args Command arguments
* @return List of shops matching
Expand All @@ -155,7 +181,7 @@ private List<String> playersTabComplete(CommandSender sender, String[] args) {
* Return a list of shops matching with start of the first element of args param
* If the lenght of args is equal to 2
* Return a list of players matching with start of the first element of args param
*
*
* @param sender Commands's executor (player or console)
* @param args Command arguments
* @return List of shops matching
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public Boolean execute(String cmd, CommandSender sender, String[] args) {
if (this.hasParams || args.length == 0) {
if (this.command != null)
return this.command.accept(this, cmd, sender, args);
return true;
return this.invalidCommand(sender, cmd);
}
if (this.getSubNodesName().contains(args[0].toLowerCase())) {
return this.getCommandNode(args[0].toLowerCase()).execute(cmd, sender, Arrays.copyOfRange(args, 1, args.length));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ public Boolean accept(CommandNode node, String command, CommandSender sender, St

for (ShopInfo shop : shops) {
message.addExtra("\n");
TextComponent line = ChatUtils.newComponent(" - " + WorldUtils.getStringFromLocation(shop.getSignLocation()) + " ");
TextComponent line = ChatUtils.newComponent(" - " + shop.getSignLocationString() + " ");

if (Permissions.CMD_LIST_TP.isSetOn(sender, false)) {
if (Permissions.CMD_LIST_TP.isSetOn(sender, false) && shop.getExists()) {
TextComponent linkTP = ChatUtils.createLinkWithBracket(LangUtils.get("Commands.Buttons.TeleportText"),
LangUtils.get("Commands.Buttons.TeleportHover"), ChatColor.DARK_AQUA,
String.format("/globalmarketchest list tp %s %s", shop.getGroup(),
WorldUtils.getStringFromLocation(shop.getSignLocation(), ",", true)));
shop.getSignLocationString()));
line.addExtra(linkTP);
}
message.addExtra(line);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package fr.epicanard.globalmarketchest.commands.consumers;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.inventory.ItemStack;

import fr.epicanard.globalmarketchest.GlobalMarketChest;
import fr.epicanard.globalmarketchest.auctions.AuctionInfo;
import fr.epicanard.globalmarketchest.commands.CommandConsumer;
import fr.epicanard.globalmarketchest.commands.CommandNode;
import fr.epicanard.globalmarketchest.utils.DatabaseUtils;
import fr.epicanard.globalmarketchest.utils.LangUtils;
import fr.epicanard.globalmarketchest.utils.PlayerUtils;
import fr.epicanard.globalmarketchest.utils.ShopUtils;

/**
* Command that convert old minecraft itemMeta to new minecraft version
*
* Command : /globalmarketchest fix auctions [active | all]
* Permission: globalmarketchest.admin.commands.fix
*/
public class FixAuctionsConsumer implements CommandConsumer {

private final static String FIX_ACTIVE = "active";
private final static String FIX_ALL = "all";

public static List<String> getFixAuctionsType() {
return Arrays.asList(
FIX_ACTIVE,
FIX_ALL
);
}

/**
* Method called when consumer is executed
*
* @param node Command node
* @param command Command executed
* @param sender Command's executor (player or console)
* @param args Arguments of command
*/
public Boolean accept(CommandNode node, String command, CommandSender sender, String[] args) {
final String fixType = (args.length == 0) ? FIX_ACTIVE : args[0];

switch (fixType) {
case FIX_ACTIVE:
this.updateAuctions(false, sender);
break;
case FIX_ALL:
this.updateAuctions(true, sender);
break;
default:
return false;
}
return true;

}

/**
* Update all auctions to convert from old auctions to new
* It run execution asynchronously
*
* @param all Define if it convert alsol history
* @param sender Command Sender that executed the command
*/
private void updateAuctions(Boolean all, CommandSender sender) {
PlayerUtils.sendMessageAndConsole(sender, String.format(LangUtils.get("InfoMessages.ConversionMode"), (all) ? FIX_ALL : FIX_ACTIVE));

Bukkit.getScheduler().runTaskAsynchronously(GlobalMarketChest.plugin, () -> {
GlobalMarketChest.plugin.auctionManager.getAllAuctions(all, (auctions) -> {
final Map<String, List<AuctionInfo>> auctionsMap = auctions.stream().collect(Collectors.groupingBy(AuctionInfo::getGroup));

auctionsMap.forEach((group, aucts) -> this.convertShopAuctions(group, aucts, sender));
});
});
}

/**
* Convert auctions of one shop kind
*
* @param group Shop group name
* @param auctions List of auction of current shop group
* @param sender Command Sender that executed the command
*/
private void convertShopAuctions(String group, List<AuctionInfo> auctions, CommandSender sender) {
PlayerUtils.sendMessageAndConsole(sender, String.format(LangUtils.get("InfoMessages.ConvertingShopAuctions"), group));
ShopUtils.lockShop(group);

final List<AuctionInfo> toUpdate = auctions.stream().map(auction -> {
final ItemStack item = DatabaseUtils.deserialize(auction.getItemMeta());
if (item == null)
return null;

final String itemMeta = DatabaseUtils.serialize(item);

if (itemMeta.equals(auction.getItemMeta()))
return null;
auction.setItemMeta(itemMeta);
return auction;
}).filter(Objects::nonNull).collect(Collectors.toList());

GlobalMarketChest.plugin.auctionManager.updateGroupOfAuctionsMetadata(toUpdate);

ShopUtils.unlockShop(group);

PlayerUtils.sendMessageAndConsole(sender, String.format(LangUtils.get("InfoMessages.ShopAuctionsConverted"), group, toUpdate.size(), auctions.size()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class HelpConsumer implements CommandConsumer {

/**
* Method called when consumer is executed
*
*
* @param node Command node
* @param command Command executed
* @param sender Command's executor (player or console)
Expand Down Expand Up @@ -47,6 +47,9 @@ public Boolean accept(CommandNode node, String command, CommandSender sender, St
if (Permissions.CMD_ADMIN_CLOSE.isSetOn(sender)) {
PlayerUtils.sendMessageConfig(sender, "Commands.HelpCommand.Close");
}
if (Permissions.CMD_ADMIN_FIX.isSetOn(sender)) {
PlayerUtils.sendMessageConfig(sender, "Commands.HelpCommand.FixAuctions");
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,41 @@
import fr.epicanard.globalmarketchest.shops.ShopInfo;
import fr.epicanard.globalmarketchest.utils.LangUtils;
import fr.epicanard.globalmarketchest.utils.PlayerUtils;
import fr.epicanard.globalmarketchest.utils.ShopUtils;

/**
* Open a shop from command line
*
*
* Command : /globalmarketchest open <shop>
* Permission: globalmarketchest.commands.open
*
*
* Command : /globalmarketchest open <shop> [player]
* Permission: globalmarketchest.admin.commands.open
*/
public class OpenConsumer implements CommandConsumer {

/**
* Open a globalshop for a player
*
*
* @param player Player on which open the shop
* @param shop Name of the shop to open
*/
private void openShop(Player player, ShopInfo shop) {
private void openShop(Player player, ShopInfo shop) {
GlobalMarketChest.plugin.inventories.removeInventory(player.getUniqueId());
final InventoryGUI inv = new InventoryGUI(player);
GlobalMarketChest.plugin.inventories.addInventory(player.getUniqueId(), inv);
inv.getTransaction().put(TransactionKey.SHOPINFO, shop);
inv.open();
inv.loadInterface("CategoryView");
if (!ShopUtils.isLockedShop(shop.getGroup())) {
final InventoryGUI inv = new InventoryGUI(player);
GlobalMarketChest.plugin.inventories.addInventory(player.getUniqueId(), inv);
inv.getTransaction().put(TransactionKey.SHOPINFO, shop);
inv.open();
inv.loadInterface("CategoryView");
} else {
PlayerUtils.sendMessageConfig(player, "InfoMessages.ShopTemporarilyLocked");
}
}

/**
* Method called when consumer is executed
*
*
* @param node Command node
* @param command Command executed
* @param sender Command's executor (player or console)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Boolean accept(CommandNode node, String command, CommandSender sender, St

try {
List<ShopInfo> shops = GlobalMarketChest.plugin.shopManager.getShops().stream()
.filter(shop -> shop.getGroup().equals(args[0]) && WorldUtils.compareLocations(shop.getSignLocation(), WorldUtils.getLocationFromString(args[1], null))).collect(Collectors.toList());
.filter(shop -> shop.getGroup().equals(args[0]) && shop.getExists() && WorldUtils.compareLocations(shop.getSignLocation(), WorldUtils.getLocationFromString(args[1], null))).collect(Collectors.toList());
if (shops.size() == 0) {
PlayerUtils.sendMessage(sender, String.format("%s%s %s", LangUtils.get("ErrorMessages.UnknownShop"), args[0], args[1]));
return false;
Expand Down
Loading

0 comments on commit 19837f0

Please sign in to comment.