mirror of
https://github.com/amalthea-mc/ShopChest.git
synced 2024-11-22 02:12:25 +00:00
Fix removeall command
This commit is contained in:
parent
30a6b33cdf
commit
0002055e5f
@ -231,7 +231,7 @@ public class ShopCommand {
|
||||
CommandMap commandMap = (CommandMap) commandMapObject;
|
||||
pluginCommand.unregister(commandMap);
|
||||
|
||||
Field fKnownCommands = commandMap.getClass().getDeclaredField("knownCommands");
|
||||
Field fKnownCommands = SimpleCommandMap.class.getDeclaredField("knownCommands");
|
||||
fKnownCommands.setAccessible(true);
|
||||
|
||||
Object knownCommandsObject = fKnownCommands.get(commandMap);
|
||||
|
@ -38,6 +38,7 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
@ -544,13 +545,10 @@ class ShopCommandExecutor implements CommandExecutor {
|
||||
|
||||
plugin.debug(sender.getName() + " is removing all shops of " + vendor.getName());
|
||||
|
||||
List<Shop> shops = new ArrayList<>();
|
||||
|
||||
for (Shop shop : shopUtils.getShops()) {
|
||||
if (shop.getVendor().getUniqueId().equals(vendor.getUniqueId())) {
|
||||
shops.add(shop);
|
||||
}
|
||||
}
|
||||
plugin.getShopUtils().getShops(vendor, new Callback<Collection<Shop>>(plugin) {
|
||||
@Override
|
||||
public void onResult(Collection<Shop> result) {
|
||||
List<Shop> shops = new ArrayList<>(result);
|
||||
|
||||
ShopRemoveAllEvent event = new ShopRemoveAllEvent(sender, vendor, shops);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
@ -567,4 +565,14 @@ class ShopCommandExecutor implements CommandExecutor {
|
||||
new Replacement(Placeholder.AMOUNT, String.valueOf(shops.size())),
|
||||
new Replacement(Placeholder.VENDOR, vendor.getName())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable throwable) {
|
||||
sender.sendMessage(LanguageUtils.getMessage(Message.ERROR_OCCURRED,
|
||||
new Replacement(Placeholder.ERROR, "Failed to get player's shops")));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -2,14 +2,18 @@ package de.epiceric.shopchest.command;
|
||||
|
||||
import de.epiceric.shopchest.ShopChest;
|
||||
import de.epiceric.shopchest.config.Config;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
class ShopTabCompleter implements TabCompleter {
|
||||
|
||||
@ -29,6 +33,7 @@ class ShopTabCompleter implements TabCompleter {
|
||||
List<String> townyShopPlots = Arrays.asList("ARENA", "COMMERCIAL", "EMBASSY", "FARM", "INN", "JAIL", "RESIDENTIAL", "SPLEEF", "WILDS");
|
||||
|
||||
Set<String> configValues = plugin.getConfig().getKeys(true);
|
||||
List<String> playerNames = Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList());
|
||||
|
||||
ArrayList<String> returnCompletions = new ArrayList<>();
|
||||
|
||||
@ -45,6 +50,18 @@ class ShopTabCompleter implements TabCompleter {
|
||||
} else {
|
||||
return configSubCommands;
|
||||
}
|
||||
} else if (args[0].equals("removeall")) {
|
||||
if (!args[1].equals("")) {
|
||||
for (String name : playerNames) {
|
||||
if (name.startsWith(args[1])) {
|
||||
returnCompletions.add(name);
|
||||
}
|
||||
}
|
||||
|
||||
return returnCompletions;
|
||||
} else {
|
||||
return playerNames;
|
||||
}
|
||||
}
|
||||
} else if (args.length == 3) {
|
||||
if (args[0].equals("config")) {
|
||||
|
@ -403,6 +403,63 @@ public abstract class Database {
|
||||
}.runTaskAsynchronously(plugin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all shops of a player, including admin shops
|
||||
*
|
||||
* @param callback Callback that returns a set of shops of the given player
|
||||
*/
|
||||
public void getShops(UUID playerUuid, final Callback<Collection<Shop>> callback) {
|
||||
new BukkitRunnable(){
|
||||
@Override
|
||||
public void run() {
|
||||
try (Connection con = dataSource.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT * FROM " + tableShops + " WHERE vendor = ?")) {
|
||||
ps.setString(1, playerUuid.toString());
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
plugin.debug("Getting a player's shops from database");
|
||||
|
||||
Set<Shop> result = new HashSet<>();
|
||||
while (rs.next()) {
|
||||
int id = rs.getInt("id");
|
||||
|
||||
plugin.debug("Getting Shop... (#" + id + ")");
|
||||
|
||||
int x = rs.getInt("x");
|
||||
int y = rs.getInt("y");
|
||||
int z = rs.getInt("z");
|
||||
|
||||
World world = plugin.getServer().getWorld(rs.getString("world"));
|
||||
Location location = new Location(world, x, y, z);
|
||||
OfflinePlayer vendor = Bukkit.getOfflinePlayer(UUID.fromString(rs.getString("vendor")));
|
||||
ItemStack itemStack = Utils.decode(rs.getString("product"));
|
||||
int amount = rs.getInt("amount");
|
||||
ShopProduct product = new ShopProduct(itemStack, amount);
|
||||
double buyPrice = rs.getDouble("buyprice");
|
||||
double sellPrice = rs.getDouble("sellprice");
|
||||
ShopType shopType = ShopType.valueOf(rs.getString("shoptype"));
|
||||
|
||||
plugin.debug("Initializing new shop... (#" + id + ")");
|
||||
|
||||
result.add(new Shop(id, plugin, vendor, product, location, buyPrice, sellPrice, shopType));
|
||||
}
|
||||
|
||||
if (callback != null) {
|
||||
callback.callSyncResult(result);
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
if (callback != null) {
|
||||
callback.callSyncError(ex);
|
||||
}
|
||||
|
||||
plugin.getLogger().severe("Failed to get player's shops from database");
|
||||
plugin.debug("Failed to get player's shops from database");
|
||||
plugin.debug(ex);
|
||||
}
|
||||
}
|
||||
}.runTaskAsynchronously(plugin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all shops from the database that are located in the given chunks
|
||||
*
|
||||
|
@ -133,6 +133,7 @@ public class ShopUtils {
|
||||
public void removeShop(Shop shop, boolean removeFromDatabase, Callback<Void> callback) {
|
||||
plugin.debug("Removing shop (#" + shop.getID() + ")");
|
||||
|
||||
if (shop.isCreated()) {
|
||||
InventoryHolder ih = shop.getInventoryHolder();
|
||||
|
||||
if (ih instanceof DoubleChest) {
|
||||
@ -148,6 +149,7 @@ public class ShopUtils {
|
||||
|
||||
shop.removeItem();
|
||||
shop.removeHologram();
|
||||
}
|
||||
|
||||
if (removeFromDatabase) {
|
||||
if (shop.getShopType() != ShopType.ADMIN) {
|
||||
@ -268,6 +270,34 @@ public class ShopUtils {
|
||||
return playerShopAmount.getOrDefault(p.getUniqueId(), new Counter()).get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all shops of a player from the database without loading them
|
||||
* @param p Player, whose shops should be get
|
||||
* @param callback Callback that returns a collection of the given player's shops
|
||||
*/
|
||||
public void getShops(OfflinePlayer p, Callback<Collection<Shop>> callback) {
|
||||
plugin.getShopDatabase().getShops(p.getUniqueId(), new Callback<Collection<Shop>>(plugin) {
|
||||
@Override
|
||||
public void onResult(Collection<Shop> result) {
|
||||
Set<Shop> shops = new HashSet<>();
|
||||
for (Shop playerShop : result) {
|
||||
Shop loadedShop = getShop(playerShop.getLocation());
|
||||
if (loadedShop != null && loadedShop.equals(playerShop)) {
|
||||
shops.add(loadedShop);
|
||||
} else {
|
||||
shops.add(playerShop);
|
||||
}
|
||||
}
|
||||
if (callback != null) callback.onResult(shops);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable throwable) {
|
||||
if (callback != null) callback.onError(throwable);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the amount of shops for each player
|
||||
* @param callback Callback that returns the amount of shops for each player
|
||||
|
Loading…
Reference in New Issue
Block a user