Skip to content

Commit

Permalink
Internal Permissions and shulker over trade fix
Browse files Browse the repository at this point in the history
- Added support for internal permissions, disabled by default.
- Fixed bug where shops were trading all matching shulkers in their inventory instead of the proper amount.
  • Loading branch information
KillerOfPie committed Oct 26, 2020
1 parent c5c7fc6 commit 22f60f8
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 119 deletions.
32 changes: 21 additions & 11 deletions src/main/java/org/shanerx/tradeshop/TradeShop.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,20 @@
public class TradeShop extends JavaPlugin {


private final NamespacedKey storageKey = new NamespacedKey(this, "tradeshop-storage-data");
private final NamespacedKey signKey = new NamespacedKey(this, "tradeshop-sign-data");
private final NamespacedKey storageKey = new NamespacedKey(this, "tradeshop-storage-data");
private final NamespacedKey signKey = new NamespacedKey(this, "tradeshop-sign-data");

private final int bStatsPluginID = 1690;
private final int bStatsPluginID = 1690;
private Metrics metrics;

private boolean useInternalPerms = false;

private ListManager lists;
private DataStorage dataStorage;

private BukkitVersion version;
private ShopSign signs;
private ShopStorage storages;
private ShopStorage storages;

private Debug debugger;

Expand Down Expand Up @@ -123,15 +125,23 @@ public void onDisable() {
getListManager().clearManager();
}

public NamespacedKey getStorageKey() {
return storageKey;
}
public boolean useInternalPerms() {
return useInternalPerms;
}

public NamespacedKey getSignKey() {
return signKey;
}
public void setUseInternalPerms(boolean useInternalPerms) {
this.useInternalPerms = useInternalPerms;
}

public NamespacedKey getStorageKey() {
return storageKey;
}

public NamespacedKey getSignKey() {
return signKey;
}

public ListManager getListManager() {
public ListManager getListManager() {
return lists;
}

Expand Down
36 changes: 11 additions & 25 deletions src/main/java/org/shanerx/tradeshop/commands/CommandCaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.shanerx.tradeshop.TradeShop;
import org.shanerx.tradeshop.enumys.Commands;
import org.shanerx.tradeshop.enumys.Message;
Expand All @@ -40,7 +39,7 @@
**/
public class CommandCaller implements CommandExecutor {

private TradeShop plugin;
private final TradeShop plugin;
private CommandPass cmdPass;
private Commands command;
private CommandRunner cmdRnnr;
Expand All @@ -54,27 +53,26 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
cmdPass = new CommandPass(sender, cmd, label, args);
command = Commands.getType(cmdPass.getArgAt(0));


if (!cmdPass.hasArgs() || command == null) {
sender.sendMessage(Message.INVALID_ARGUMENTS.getPrefixed());
return true;

}

if (!checkPerm()) {
return true;
switch (command.checkPerm(sender)) {
case NO_PERM:
sender.sendMessage(Message.NO_COMMAND_PERMISSION.getPrefixed());
return true;
case PLAYER_ONLY:
sender.sendMessage(Message.PLAYER_ONLY_COMMAND.getPrefixed());
return true;
}

if (command.getMinArgs() > args.length || command.getMaxArgs() < args.length) {
sender.sendMessage(Message.INVALID_ARGUMENTS.getPrefixed());
return true;
}

if (command.needsPlayer() && !(sender instanceof Player)) {
sender.sendMessage(Message.PLAYER_ONLY_COMMAND.getPrefixed());
return true;
}

cmdRnnr = new CommandRunner(plugin, cmdPass);

switch (command) {
Expand Down Expand Up @@ -141,21 +139,9 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
case REMOVE_COST:
cmdRnnr.removeCost();
break;
}

return true;
}


/**
* Checks if the sender has the required permission
*
* @return true if permission is NONE or sender has permission
*/
public boolean checkPerm() {
if (!command.checkPerm(cmdPass.getSender())) {
cmdPass.getSender().sendMessage(Message.NO_COMMAND_PERMISSION.getPrefixed());
return false;
case PLAYER_LEVEL:
cmdRnnr.playerLevel();
break;
}

return true;
Expand Down
45 changes: 37 additions & 8 deletions src/main/java/org/shanerx/tradeshop/commands/CommandRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@

public class CommandRunner extends Utils {

private TradeShop plugin;
private CommandPass command;
private final TradeShop plugin;
private final CommandPass command;
private Player pSender;

public CommandRunner(TradeShop instance, CommandPass command) {
Expand Down Expand Up @@ -91,7 +91,7 @@ public void help() {
.append("\n\n&b/tradeshop &f &f Display help message\n");

for (Commands c : Commands.values()) {
if (c.checkPerm(command.getSender())) {
if (c.checkPerm(command.getSender()) == PermStatus.GOOD) {
sb.append(Message.colour(String.format("&b/ts %s &f %s\n", c.getFirstName(), c.getDescription())));
}
}
Expand Down Expand Up @@ -539,7 +539,7 @@ public void open() {

if (!(shop.getOwner().getUUID().equals(pSender.getUniqueId()) ||
shop.getManagersUUID().contains(pSender.getUniqueId()) ||
pSender.hasPermission(Permissions.ADMIN.getPerm()))) {
Permissions.hasPermission(pSender, Permissions.ADMIN))) {
sendMessage(Message.NO_EDIT.getPrefixed());
return;
}
Expand Down Expand Up @@ -576,7 +576,7 @@ public void close() {

if (!(shop.getOwner().getUUID().equals(pSender.getUniqueId()) ||
shop.getManagersUUID().contains(pSender.getUniqueId()) ||
pSender.hasPermission(Permissions.ADMIN.getPerm()))) {
Permissions.hasPermission(pSender, Permissions.ADMIN))) {
sendMessage(Message.NO_EDIT.getPrefixed());
return;
}
Expand Down Expand Up @@ -842,16 +842,45 @@ public void multi() {

if (amount < 2)
amount = 2;
else if (amount > Setting.MULTI_TRADE_MAX.getInt())
amount = Setting.MULTI_TRADE_MAX.getInt();
else if (amount > Setting.MULTI_TRADE_MAX.getInt())
amount = Setting.MULTI_TRADE_MAX.getInt();

playerSetting.setMulti(amount);
plugin.getDataStorage().savePlayer(playerSetting);
plugin.getDataStorage().savePlayer(playerSetting);

sendMessage(Message.MULTI_UPDATE.getPrefixed().replaceAll("%amount%", String.valueOf(amount)));
}
}

/*
* Changes/Sets the players permission level if internal permissions is enabled
*/
public void playerLevel() {
if (Bukkit.getOfflinePlayer(command.getArgAt(1)).hasPlayedBefore()) {
PlayerSetting playerSetting = plugin.getDataStorage().loadPlayer(Bukkit.getOfflinePlayer(command.getArgAt(1)).getUniqueId());
if (command.argsSize() == 2) {
sendMessage(Message.VIEW_PLAYER_LEVEL.getMessage()
.replace("%player%", Bukkit.getOfflinePlayer(command.getArgAt(1)).getName())
.replace("%level%", playerSetting.getType() + ""));
} else {
if (isInt(command.getArgAt(2))) {
int newLevel = Integer.parseInt(command.getArgAt(2));

playerSetting.setType(newLevel);
plugin.getDataStorage().savePlayer(playerSetting);

sendMessage(Message.SET_PLAYER_LEVEL.getMessage()
.replace("%player%", Bukkit.getOfflinePlayer(command.getArgAt(1)).getName())
.replace("%level%", playerSetting.getType() + ""));
} else {
sendMessage(Message.INVALID_ARGUMENTS.getMessage());
}
}
} else {
sendMessage(Message.PLAYER_NOT_FOUND.getMessage());
}
}

/**
* Returns the Shop the player is looking at
*
Expand Down
34 changes: 9 additions & 25 deletions src/main/java/org/shanerx/tradeshop/commands/CommandTabCaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,17 @@
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.shanerx.tradeshop.TradeShop;
import org.shanerx.tradeshop.enumys.Commands;
import org.shanerx.tradeshop.enumys.Message;
import org.shanerx.tradeshop.enumys.Permissions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CommandTabCaller implements TabCompleter {

private TradeShop plugin;
private final TradeShop plugin;
private CommandPass cmdPass;
private Commands command;
private CommandTabCompleter tabCompleter;
Expand All @@ -56,13 +54,12 @@ public List<String> onTabComplete(CommandSender sender, Command cmd, String labe

if (command != null) {

if (!checkPerm()) {
return Collections.EMPTY_LIST;
}

if (command.needsPlayer() && !(sender instanceof Player)) {
sender.sendMessage(Message.PLAYER_ONLY_COMMAND.getPrefixed());
return Collections.EMPTY_LIST;
switch (command.checkPerm(sender)) {
case NO_PERM:
return Collections.EMPTY_LIST;
case PLAYER_ONLY:
sender.sendMessage(Message.PLAYER_ONLY_COMMAND.getPrefixed());
return Collections.EMPTY_LIST;
}

tabCompleter = new CommandTabCompleter(plugin, cmdPass);
Expand All @@ -71,18 +68,15 @@ public List<String> onTabComplete(CommandSender sender, Command cmd, String labe
case HELP:
return tabCompleter.help();
case ADD_PRODUCT:
return tabCompleter.addSet();
case ADD_COST:
return tabCompleter.addSet();
case SET_COST:
return tabCompleter.addSet();
case SET_PRODUCT:
return tabCompleter.addSet();
case ADD_MANAGER:
return tabCompleter.fillServerPlayer();
case REMOVE_USER:
return tabCompleter.fillShopPlayer();
case ADD_MANAGER:
case ADD_MEMBER:
case PLAYER_LEVEL:
return tabCompleter.fillServerPlayer();
default:
return Collections.EMPTY_LIST;
Expand All @@ -101,14 +95,4 @@ public List<String> onTabComplete(CommandSender sender, Command cmd, String labe
return Collections.EMPTY_LIST;
}
}


/**
* Checks if the sender has the required permission
*
* @return true if permission is NONE or sender has permission
*/
public boolean checkPerm() {
return cmdPass.getSender().hasPermission(command.getPerm().getPerm()) || command.getPerm().equals(Permissions.NONE);
}
}
31 changes: 21 additions & 10 deletions src/main/java/org/shanerx/tradeshop/enumys/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import com.google.common.collect.Lists;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.List;

Expand All @@ -42,6 +43,7 @@ public enum Commands {
HELP(Lists.newArrayList("help", "?"), Permissions.HELP, 1, 2, false, "Display help message", "/tradeshop $cmd$ [command]"),
SETUP(Lists.newArrayList("setup", "start", "create", "make"), Permissions.HELP, 1, 1, false, "Display shop setup tutorial", "/tradeshop $cmd$"),
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>"),
ADD_MEMBER(Lists.newArrayList("addMember"), Permissions.NONE, 2, 2, true, "Add member to shop", "/tradeshop $cmd$ <name>"),
Expand All @@ -64,37 +66,38 @@ public enum Commands {
/**
* Name of the permission
**/
private String name;
private final String name;

/**
* All names that can be used to call the command
**/
private List<String> names;
private final List<String> names;

/**
* Minimum and Maximum arguments required for the command
**/
private int minArgs, maxArgs;
private final int minArgs;
private final int maxArgs;

/**
* Permission required for the command
**/
private Permissions perm;
private final Permissions perm;

/**
* Whether the command requires a player to run
**/
private boolean needsPlayer;
private final boolean needsPlayer;

/**
* Description for command
*/
private String description;
private final String description;

/**
* Command usage
*/
private String usage;
private final String usage;

Commands(List<String> names, Permissions perm, int minArgs, int maxArgs, boolean needsPlayer, String description, String usage) {
this.names = names;
Expand Down Expand Up @@ -257,7 +260,15 @@ public String getAliases() {
* @param sender sender to check perm for
* @return true is player has perm
*/
public boolean checkPerm(CommandSender sender) {
return getPerm().equals(Permissions.NONE) || sender.hasPermission(getPerm().getPerm());
public PermStatus checkPerm(CommandSender sender) {
if (sender instanceof Player) {
if (!Permissions.hasPermission((Player) sender, getPerm()))
return PermStatus.NO_PERM;
} else {
if (needsPlayer())
return PermStatus.PLAYER_ONLY;
}

return PermStatus.GOOD;
}
}
}
Loading

0 comments on commit 22f60f8

Please sign in to comment.