mirror of
https://github.com/amalthea-mc/ShopChest.git
synced 2024-11-22 10:22:29 +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;
|
CommandMap commandMap = (CommandMap) commandMapObject;
|
||||||
pluginCommand.unregister(commandMap);
|
pluginCommand.unregister(commandMap);
|
||||||
|
|
||||||
Field fKnownCommands = commandMap.getClass().getDeclaredField("knownCommands");
|
Field fKnownCommands = SimpleCommandMap.class.getDeclaredField("knownCommands");
|
||||||
fKnownCommands.setAccessible(true);
|
fKnownCommands.setAccessible(true);
|
||||||
|
|
||||||
Object knownCommandsObject = fKnownCommands.get(commandMap);
|
Object knownCommandsObject = fKnownCommands.get(commandMap);
|
||||||
|
@ -38,6 +38,7 @@ import org.bukkit.entity.Player;
|
|||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
@ -544,27 +545,34 @@ class ShopCommandExecutor implements CommandExecutor {
|
|||||||
|
|
||||||
plugin.debug(sender.getName() + " is removing all shops of " + vendor.getName());
|
plugin.debug(sender.getName() + " is removing all shops of " + vendor.getName());
|
||||||
|
|
||||||
List<Shop> shops = new ArrayList<>();
|
plugin.getShopUtils().getShops(vendor, new Callback<Collection<Shop>>(plugin) {
|
||||||
|
@Override
|
||||||
|
public void onResult(Collection<Shop> result) {
|
||||||
|
List<Shop> shops = new ArrayList<>(result);
|
||||||
|
|
||||||
for (Shop shop : shopUtils.getShops()) {
|
ShopRemoveAllEvent event = new ShopRemoveAllEvent(sender, vendor, shops);
|
||||||
if (shop.getVendor().getUniqueId().equals(vendor.getUniqueId())) {
|
Bukkit.getPluginManager().callEvent(event);
|
||||||
shops.add(shop);
|
if (event.isCancelled()){
|
||||||
|
plugin.debug("Remove all event cancelled");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Shop shop : shops) {
|
||||||
|
shopUtils.removeShop(shop, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
sender.sendMessage(LanguageUtils.getMessage(Message.ALL_SHOPS_REMOVED,
|
||||||
|
new Replacement(Placeholder.AMOUNT, String.valueOf(shops.size())),
|
||||||
|
new Replacement(Placeholder.VENDOR, vendor.getName())));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
ShopRemoveAllEvent event = new ShopRemoveAllEvent(sender, vendor, shops);
|
@Override
|
||||||
Bukkit.getPluginManager().callEvent(event);
|
public void onError(Throwable throwable) {
|
||||||
if (event.isCancelled()){
|
sender.sendMessage(LanguageUtils.getMessage(Message.ERROR_OCCURRED,
|
||||||
plugin.debug("Remove all event cancelled");
|
new Replacement(Placeholder.ERROR, "Failed to get player's shops")));
|
||||||
return;
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
for (Shop shop : shops) {
|
|
||||||
shopUtils.removeShop(shop, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
sender.sendMessage(LanguageUtils.getMessage(Message.ALL_SHOPS_REMOVED,
|
|
||||||
new Replacement(Placeholder.AMOUNT, String.valueOf(shops.size())),
|
|
||||||
new Replacement(Placeholder.VENDOR, vendor.getName())));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,14 +2,18 @@ package de.epiceric.shopchest.command;
|
|||||||
|
|
||||||
import de.epiceric.shopchest.ShopChest;
|
import de.epiceric.shopchest.ShopChest;
|
||||||
import de.epiceric.shopchest.config.Config;
|
import de.epiceric.shopchest.config.Config;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.TabCompleter;
|
import org.bukkit.command.TabCompleter;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
class ShopTabCompleter implements TabCompleter {
|
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");
|
List<String> townyShopPlots = Arrays.asList("ARENA", "COMMERCIAL", "EMBASSY", "FARM", "INN", "JAIL", "RESIDENTIAL", "SPLEEF", "WILDS");
|
||||||
|
|
||||||
Set<String> configValues = plugin.getConfig().getKeys(true);
|
Set<String> configValues = plugin.getConfig().getKeys(true);
|
||||||
|
List<String> playerNames = Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList());
|
||||||
|
|
||||||
ArrayList<String> returnCompletions = new ArrayList<>();
|
ArrayList<String> returnCompletions = new ArrayList<>();
|
||||||
|
|
||||||
@ -45,6 +50,18 @@ class ShopTabCompleter implements TabCompleter {
|
|||||||
} else {
|
} else {
|
||||||
return configSubCommands;
|
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) {
|
} else if (args.length == 3) {
|
||||||
if (args[0].equals("config")) {
|
if (args[0].equals("config")) {
|
||||||
|
@ -403,6 +403,63 @@ public abstract class Database {
|
|||||||
}.runTaskAsynchronously(plugin);
|
}.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
|
* Get all shops from the database that are located in the given chunks
|
||||||
*
|
*
|
||||||
|
@ -133,22 +133,24 @@ public class ShopUtils {
|
|||||||
public void removeShop(Shop shop, boolean removeFromDatabase, Callback<Void> callback) {
|
public void removeShop(Shop shop, boolean removeFromDatabase, Callback<Void> callback) {
|
||||||
plugin.debug("Removing shop (#" + shop.getID() + ")");
|
plugin.debug("Removing shop (#" + shop.getID() + ")");
|
||||||
|
|
||||||
InventoryHolder ih = shop.getInventoryHolder();
|
if (shop.isCreated()) {
|
||||||
|
InventoryHolder ih = shop.getInventoryHolder();
|
||||||
|
|
||||||
if (ih instanceof DoubleChest) {
|
if (ih instanceof DoubleChest) {
|
||||||
DoubleChest dc = (DoubleChest) ih;
|
DoubleChest dc = (DoubleChest) ih;
|
||||||
Chest r = (Chest) dc.getRightSide();
|
Chest r = (Chest) dc.getRightSide();
|
||||||
Chest l = (Chest) dc.getLeftSide();
|
Chest l = (Chest) dc.getLeftSide();
|
||||||
|
|
||||||
shopLocation.remove(r.getLocation());
|
shopLocation.remove(r.getLocation());
|
||||||
shopLocation.remove(l.getLocation());
|
shopLocation.remove(l.getLocation());
|
||||||
} else {
|
} else {
|
||||||
shopLocation.remove(shop.getLocation());
|
shopLocation.remove(shop.getLocation());
|
||||||
|
}
|
||||||
|
|
||||||
|
shop.removeItem();
|
||||||
|
shop.removeHologram();
|
||||||
}
|
}
|
||||||
|
|
||||||
shop.removeItem();
|
|
||||||
shop.removeHologram();
|
|
||||||
|
|
||||||
if (removeFromDatabase) {
|
if (removeFromDatabase) {
|
||||||
if (shop.getShopType() != ShopType.ADMIN) {
|
if (shop.getShopType() != ShopType.ADMIN) {
|
||||||
playerShopAmount.compute(shop.getVendor().getUniqueId(), (uuid, amount) -> amount == null ? new Counter() : amount.decrement());
|
playerShopAmount.compute(shop.getVendor().getUniqueId(), (uuid, amount) -> amount == null ? new Counter() : amount.decrement());
|
||||||
@ -268,6 +270,34 @@ public class ShopUtils {
|
|||||||
return playerShopAmount.getOrDefault(p.getUniqueId(), new Counter()).get();
|
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
|
* Loads the amount of shops for each player
|
||||||
* @param callback Callback that returns 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