Skip to content

Commit

Permalink
Additional Work on shop statuses
Browse files Browse the repository at this point in the history
- Added `ts status` command
- Player gets text status on join and gui status oon command
- Changed reload command to new Manage_Plugin permission
- New methods in Message to potentially support json messages but don't know if they work for that, they do run normal messages properly, will move everything over to using them later.
- Fixed an NPE from Shop
- Fixed NPE fromShopProtectionListener when destroying iTrade signs
  • Loading branch information
KillerOfPie committed Oct 28, 2020
1 parent dc8a4a2 commit dfdc526
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
case PLAYER_LEVEL:
cmdRnnr.playerLevel();
break;
case STATUS:
cmdRnnr.status();
break;
}

return true;
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/org/shanerx/tradeshop/commands/CommandRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ else if (amount > Setting.MULTI_TRADE_MAX.getInt())
}
}

/*
/**
* Changes/Sets the players permission level if internal permissions is enabled
*/
public void playerLevel() {
Expand Down Expand Up @@ -881,6 +881,26 @@ public void playerLevel() {
}
}

/**
* Changes/Sets the players permission level if internal permissions is enabled
*/
public void status() {
if (command.hasArgAt(1)) {
if (!Permissions.hasPermission(pSender, Permissions.ADMIN)) {
Message.NO_COMMAND_PERMISSION.sendMessage(pSender);
return;
}
if (Bukkit.getOfflinePlayer(command.getArgAt(1)).hasPlayedBefore()) {
plugin.getDataStorage().loadPlayer(Bukkit.getOfflinePlayer(command.getArgAt(1)).getUniqueId())
.getInvolvedStatusesInventory().show(pSender.getPlayer());
} else {
sendMessage(Message.PLAYER_NOT_FOUND.getMessage());
}
} else {
plugin.getDataStorage().loadPlayer(pSender.getUniqueId()).getInvolvedStatusesInventory().show(pSender.getPlayer());
}
}

/**
* Returns the Shop the player is looking at
*
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/shanerx/tradeshop/enumys/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public enum Commands {
BUGS(Lists.newArrayList("bugs", "bug"), Permissions.NONE, 1, 1, false, "Report bugs to the developers", "/tradeshop $cmd$"),
PLAYER_LEVEL(Lists.newArrayList("playerlevel", "pl"), Permissions.MANAGE_PLUGIN, 2, 3, false, "If Internal Permissions is enable this allows the getting and setting of player permission levels.", "/tradeshop $cmd$ <name> <newlevel>"),
ADD_MANAGER(Lists.newArrayList("addManager"), Permissions.NONE, 2, 2, true, "Add manager to shop", "/tradeshop $cmd$ <name>"),
REMOVE_USER(Lists.newArrayList("removeUser", "removeManager", "removeMember"), Permissions.NONE, 2, 2, true, "Remove user from shop", "/tradeshop $cmd$ <name>"),
REMOVE_USER(Lists.newArrayList("removeUser", "removeManager", "removeMember"), Permissions.NONE, 2, 2, true, "Remove user from shop", "/tradeshop $cmd$ <Name>"),
ADD_MEMBER(Lists.newArrayList("addMember"), Permissions.NONE, 2, 2, true, "Add member to shop", "/tradeshop $cmd$ <name>"),
ADD_PRODUCT(Lists.newArrayList("addProduct"), Permissions.NONE, 1, 3, true, "Add product to shop", "/tradeshop $cmd$ [Amount] [Material]"),
ADD_COST(Lists.newArrayList("addCost"), Permissions.NONE, 1, 3, true, "Add cost to shop", "/tradeshop $cmd$ [Amount] [Material]"),
Expand All @@ -59,9 +59,10 @@ public enum Commands {
CLOSE(Lists.newArrayList("close"), Permissions.NONE, 1, 1, true, "Close shop", "/tradeshop $cmd$"),
WHO(Lists.newArrayList("who"), Permissions.INFO, 1, 1, true, "Shop members of shop", "/tradeshop $cmd$"),
WHAT(Lists.newArrayList("what", "peek", "shop", "view"), Permissions.INFO, 1, 1, true, "Peek at shop inventory", "/tradeshop $cmd$"),
RELOAD(Lists.newArrayList("reload"), Permissions.ADMIN, 1, 1, false, "Reload configuration files", "/tradeshop $cmd$"),
RELOAD(Lists.newArrayList("reload"), Permissions.MANAGE_PLUGIN, 1, 1, false, "Reload configuration files", "/tradeshop $cmd$"),
SWITCH(Lists.newArrayList("switch"), Permissions.EDIT, 1, 1, true, "Switch shop type", "/tradeshop $cmd$"),
MULTI(Lists.newArrayList("multi", "multiply", "many"), Permissions.NONE, 1, 2, true, "Changes trade multiplier for this login", "/tradeshop $cmd$ <Amount>");
MULTI(Lists.newArrayList("multi", "multiply", "many"), Permissions.NONE, 1, 2, true, "Changes trade multiplier for this login", "/tradeshop $cmd$ <Amount>"),
STATUS(Lists.newArrayList("status", "stats", "s"), Permissions.INFO, 1, 2, true, "Displays the status of all shops the player has a relation to", "/tradeshop $cmd$ [Name]");

/**
* Name of the permission
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/org/shanerx/tradeshop/enumys/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.shanerx.tradeshop.TradeShop;
import org.yaml.snakeyaml.Yaml;

import java.io.*;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;

Expand Down Expand Up @@ -232,6 +236,41 @@ public String toString() {
public String getPrefixed() {
return colour(PREFIX + toString());
}

public void sendMessage(Player player, Map<String, String> replacements) {
String message = getPrefixed();
replacements.forEach(message::replaceAll);

if (getMessage().startsWith("#json ")) {
message.replaceFirst("#json ", "");
player.sendRawMessage(colour(message));
} else {
player.sendMessage(colour(message));
}
}

public void sendMessage(Player player) {
sendMessage(player, Collections.emptyMap());
}

public void sendMessage(CommandSender sender) {
sendMessage(sender, Collections.emptyMap());
}

public void sendMessage(CommandSender sender, Map<String, String> replacements) {
if (sender instanceof Player) {
sendMessage((Player) sender, replacements);
return;
}
String message = getPrefixed();
replacements.forEach(message::replaceAll);

if (getMessage().startsWith("#json ")) {
message.replaceFirst("#json ", "");
}

sender.sendMessage(colour(message));
}
}

enum MessageSectionKeys {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@
import org.shanerx.tradeshop.TradeShop;
import org.shanerx.tradeshop.enumys.Message;
import org.shanerx.tradeshop.enumys.Permissions;
import org.shanerx.tradeshop.enumys.ShopRole;
import org.shanerx.tradeshop.objects.PlayerSetting;
import org.shanerx.tradeshop.objects.Shop;
import org.shanerx.tradeshop.objects.ShopLocation;
import org.shanerx.tradeshop.utils.BukkitVersion;
import org.shanerx.tradeshop.utils.Updater;
import org.shanerx.tradeshop.utils.Utils;
Expand Down Expand Up @@ -66,32 +63,7 @@ public void onJoin(PlayerJoinEvent event) {
player.sendMessage(Message.PLUGIN_BEHIND.getPrefixed());
}

StringBuilder sb = new StringBuilder();
sb.append("%eInventory status of your shops: \n");
sb.append("&eShop Role &f| &eLocation &f| &eInventory Status\n&b");
if (playerSetting.getOwnedShops().size() > 0) {
playerSetting.getOwnedShops().forEach(s -> {
Shop shop = plugin.getDataStorage().loadShopFromSign(ShopLocation.deserialize(s));
if (shop.checkRole(player.getUniqueId()) != ShopRole.SHOPPER) {
sb.append(shop.checkRole(player.getUniqueId()).toString()).append(" &f|&d ");
sb.append(s).append(" &f| ");
sb.append(shop.getStatus().getLine()).append("\n&b");
}
});
}
if (playerSetting.getStaffShops().size() > 0) {
playerSetting.getOwnedShops().forEach(s -> {
Shop shop = plugin.getDataStorage().loadShopFromSign(ShopLocation.deserialize(s));
if (shop.checkRole(player.getUniqueId()) != ShopRole.SHOPPER) {
sb.append(shop.checkRole(player.getUniqueId()).toString()).append(" &f|&d ");
sb.append(s).append(" &f| ");
sb.append(shop.getStatus().getLine()).append("\n&b");
}
});
}

sb.deleteCharAt(sb.lastIndexOf("\n"));
player.sendMessage(colorize(sb.toString()));
player.sendMessage(playerSetting.getInvolvedStatusesString());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ public void onBlockBreak(BlockBreakEvent event) {
return;
}

shop.getChestAsSC().resetName();
if (shop.getChestAsSC() != null)
shop.getChestAsSC().resetName();
shop.remove();
return;
}
Expand Down
101 changes: 98 additions & 3 deletions src/main/java/org/shanerx/tradeshop/objects/PlayerSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@

import com.google.common.collect.Sets;
import com.google.gson.Gson;
import de.themoep.inventorygui.GuiElementGroup;
import de.themoep.inventorygui.GuiPageElement;
import de.themoep.inventorygui.InventoryGui;
import de.themoep.inventorygui.StaticGuiElement;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.shanerx.tradeshop.enumys.Setting;
import org.shanerx.tradeshop.enumys.ShopRole;
import org.shanerx.tradeshop.utils.Utils;

import java.io.Serializable;
import java.util.Map;
Expand All @@ -37,11 +46,12 @@
public class PlayerSetting implements Serializable {

private transient UUID uuid;
private String uuidString;
private final String uuidString;
private final Set<String> ownedShops;

private int type = 0, multi = Setting.MULTI_TRADE_DEFAULT.getInt();

private Set<String> ownedShops, staffShops;
private final Set<String> staffShops;
private transient Utils utils = new Utils();

public PlayerSetting(UUID playerUUID, Map<String, Integer> data) {
this.uuid = playerUUID;
Expand Down Expand Up @@ -125,10 +135,95 @@ public Set<String> getStaffShops() {
public void load() {
if (uuid == null) uuid = UUID.fromString(uuidString);
if (multi > Setting.MULTI_TRADE_MAX.getInt()) multi = Setting.MULTI_TRADE_MAX.getInt();
utils = new Utils();
}

public String serialize() {
return new Gson().toJson(this);
}

public String getInvolvedStatusesString() {
StringBuilder sb = new StringBuilder();
sb.append("&eStatus of your shops: \n");
sb.append("&eShop Role &f| &eType &f| &eLocation &f| &eInventory Status\n&b");
if (getOwnedShops().size() > 0) {
getOwnedShops().forEach(s -> {
Shop shop = utils.plugin.getDataStorage().loadShopFromSign(ShopLocation.deserialize(s));
if (shop.checkRole(uuid) != ShopRole.SHOPPER) {
sb.append(shop.checkRole(uuid).toString()).append(" &f|&a ");
sb.append(shop.getShopType().toString()).append(" &f|&d ");
sb.append(s).append(" &f| ");
sb.append(shop.getStatus().getLine()).append("\n&b");
}
});
}
if (getStaffShops().size() > 0) {
getStaffShops().forEach(s -> {
Shop shop = utils.plugin.getDataStorage().loadShopFromSign(ShopLocation.deserialize(s));
if (shop.checkRole(uuid) != ShopRole.SHOPPER) {
sb.append(shop.checkRole(uuid).toString()).append(" &f|&a ");
sb.append(shop.getShopType().toString()).append(" &f|&d ");
sb.append(s).append(" &f| ");
sb.append(shop.getStatus().getLine()).append("\n&b");
}
});
}

sb.deleteCharAt(sb.lastIndexOf("\n"));
return utils.colorize(sb.toString());
}

public InventoryGui getInvolvedStatusesInventory() {
InventoryGui gui = new InventoryGui(utils.plugin, Bukkit.getOfflinePlayer(uuid).getName() + "'s Shops", new String[]{"ggggggggg", "ggggggggg", " fp ln "});
GuiElementGroup group = new GuiElementGroup('g');
if (getOwnedShops().size() > 0) {
getOwnedShops().forEach(s -> {
Shop shop = utils.plugin.getDataStorage().loadShopFromSign(ShopLocation.deserialize(s));
if (shop.checkRole(uuid) != ShopRole.SHOPPER) {
group.addElement(new StaticGuiElement('e',
new ItemStack(shop.getInventoryLocation() != null ?
shop.getInventoryLocation().getBlock().getType() :
Material.getMaterial(shop.getShopLocation().getBlock().getType().toString().replaceAll("WALL_", ""))),
utils.colorize("&d" + s),
utils.colorize("&a" + shop.getShopType().toString()),
utils.colorize("&b" + shop.checkRole(uuid).toString()),
utils.colorize(shop.getStatus().getLine())));
}
});
}
if (getStaffShops().size() > 0) {
getStaffShops().forEach(s -> {
Shop shop = utils.plugin.getDataStorage().loadShopFromSign(ShopLocation.deserialize(s));
if (shop.checkRole(uuid) != ShopRole.SHOPPER) {
group.addElement(new StaticGuiElement('e',
new ItemStack(shop.getInventoryLocation() != null ?
shop.getInventoryLocation().getBlock().getType() :
Material.getMaterial(shop.getShopLocation().getBlock().getType().toString().replaceAll("WALL_", ""))),
utils.colorize("&d" + s),
utils.colorize("&a" + shop.getShopType().toString()),
utils.colorize("&b" + shop.checkRole(uuid).toString()),
utils.colorize(shop.getStatus().getLine())));
}
});
}
gui.addElement(group);

// First page
gui.addElement(new GuiPageElement('f', new ItemStack(Material.STICK), GuiPageElement.PageAction.FIRST, "Go to first page (current: %page%)"));

// Previous page
gui.addElement(new GuiPageElement('p', new ItemStack(Material.POTION), GuiPageElement.PageAction.PREVIOUS, "Go to previous page (%prevpage%)"));

// Next page
gui.addElement(new GuiPageElement('n', new ItemStack(Material.SPLASH_POTION), GuiPageElement.PageAction.NEXT, "Go to next page (%nextpage%)"));

// Last page
gui.addElement(new GuiPageElement('l', new ItemStack(Material.ARROW), GuiPageElement.PageAction.LAST, "Go to last page (%pages%)"));

//Blank Item
gui.setFiller(new ItemStack(Material.GRAY_STAINED_GLASS, 1));

return gui;
}
}

2 changes: 1 addition & 1 deletion src/main/java/org/shanerx/tradeshop/objects/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ public List<UUID> getMembersUUID() {
* @return inventory location as Location
*/
public Location getInventoryLocation() {
return chestLoc.getLocation();
return chestLoc != null ? chestLoc.getLocation() : null;
}

/**
Expand Down

0 comments on commit dfdc526

Please sign in to comment.