Skip to content

Commit

Permalink
Fix a few bugs and update commands (#714)
Browse files Browse the repository at this point in the history
## Fixes:
* Fixed an issue where open-others defaults to everyone, It definitely should not be defaulted for everyone especially now with the recent changes.

## Command Changes
`/crazycrates forceopen` was merged with `/crazycrates open-others`

You simply specific `free` or `f` for the KeyType while using the command.
  • Loading branch information
ryderbelserion authored May 16, 2024
1 parent 07e951f commit 33f92a4
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public static void load() {

commandManager.registerSuggestion(SuggestionKey.of("keys"), (sender, context) -> List.of("virtual", "v", "physical", "p"));

commandManager.registerSuggestion(SuggestionKey.of("admin-keys"), (sender, context) -> List.of("virtual", "v", "physical", "p", "free", "f"));

commandManager.registerSuggestion(SuggestionKey.of("players"), (sender, context) -> plugin.getServer().getOnlinePlayers().stream().map(Player::getName).toList());

commandManager.registerSuggestion(SuggestionKey.of("locations"), (sender, context) -> crateManager.getCrateLocations().stream().map(CrateLocation::getID).toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.badbones69.crazycrates.api.objects.Crate;
import com.badbones69.crazycrates.api.objects.Prize;
import com.badbones69.crazycrates.api.utils.MiscUtils;
import com.badbones69.crazycrates.api.utils.MsgUtils;
import com.badbones69.crazycrates.tasks.BukkitUserManager;
import com.badbones69.crazycrates.tasks.InventoryManager;
import com.badbones69.crazycrates.tasks.crates.CrateManager;
Expand Down Expand Up @@ -96,18 +95,15 @@ protected void takeKey(@NotNull final CommandSender sender, @Nullable final Offl
/**
* Get the key type from string
*
* @param sender the sender requesting to give keys.
* @param type the keytype to check.
* @return the keytype or virtual key if none found.
*/
protected @NotNull final KeyType getKeyType(@NotNull final CommandSender sender, @NotNull final String type) {
protected @NotNull final KeyType getKeyType(@NotNull final String type, boolean openOthers) {
if (type.isEmpty()) return KeyType.virtual_key;

KeyType keyType = KeyType.getFromName(type);

if (keyType == null || keyType == KeyType.free_key) {
sender.sendRichMessage(MsgUtils.getPrefix("<red>You did not specify a key type, Defaulting to key type virtual"));

if (keyType == null || keyType == KeyType.free_key && !openOthers) {
return KeyType.virtual_key;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void give(CommandSender sender, @Suggestion("keys") String type, @Suggest
return;
}

final KeyType keyType = getKeyType(sender, type);
final KeyType keyType = getKeyType(type, false);

if (amount <= 0) {
sender.sendRichMessage(Messages.not_a_number.getMessage(sender, "{number}", String.valueOf(amount)));
Expand Down Expand Up @@ -68,7 +68,7 @@ public void all(CommandSender sender, @Suggestion("keys") String type, @Suggesti
return;
}

final KeyType keyType = getKeyType(sender, type);
final KeyType keyType = getKeyType(type, false);

if (amount <= 0) {
sender.sendRichMessage(Messages.not_a_number.getMessage(sender, "{number}", String.valueOf(amount)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
import com.badbones69.crazycrates.commands.crates.types.BaseCommand;
import dev.triumphteam.cmd.bukkit.annotation.Permission;
import dev.triumphteam.cmd.core.annotations.Command;
import dev.triumphteam.cmd.core.annotations.CommandFlags;
import dev.triumphteam.cmd.core.annotations.Flag;
import dev.triumphteam.cmd.core.annotations.Optional;
import dev.triumphteam.cmd.core.annotations.Suggestion;
import dev.triumphteam.cmd.core.argument.keyed.Flags;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.sound.Sound;
import org.bukkit.command.CommandSender;
Expand All @@ -24,7 +20,6 @@
import com.badbones69.crazycrates.config.impl.ConfigKeys;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

public class CommandOpen extends BaseCommand {

Expand Down Expand Up @@ -89,7 +84,7 @@ public void open(Player player, @Suggestion("crates") String crateName, @Suggest
return;
}

KeyType keyType = getKeyType(player, type);
KeyType keyType = getKeyType(type, false);

boolean hasKey = this.config.getProperty(ConfigKeys.virtual_accepts_physical_keys) && keyType == KeyType.physical_key ? this.userManager.getTotalKeys(player.getUniqueId(), crate.getName()) >= 1 : this.userManager.getVirtualKeys(player.getUniqueId(), crate.getName()) >= 1;

Expand Down Expand Up @@ -117,9 +112,8 @@ public void open(Player player, @Suggestion("crates") String crateName, @Suggest
}

@Command("open-others")
@CommandFlags({@Flag(flag = "f", argument = boolean.class)})
@Permission(value = "crazycrates.open-others", def = PermissionDefault.TRUE)
public void others(CommandSender sender, @Suggestion("crates") String crateName, @Suggestion("players") Player player, @Suggestion("keys") String type, @Suggestion("numbers") int amount, @Optional Flags flags) {
@Permission(value = "crazycrates.open-others", def = PermissionDefault.OP)
public void others(CommandSender sender, @Suggestion("crates") String crateName, @Suggestion("players") Player player, @Suggestion("admin-keys") String type) {
// If the command is cancelled.
if (isCancelled(player, crateName)) return;

Expand Down Expand Up @@ -156,23 +150,17 @@ public void others(CommandSender sender, @Suggestion("crates") String crateName,
return;
}

AtomicReference<KeyType> keyType = new AtomicReference<>(getKeyType(sender, type));
KeyType keyType = getKeyType(type, true);

if (sender == player && keyType.get() != KeyType.free_key) {
if (sender == player && keyType != KeyType.free_key) {
open(player, crate.getName(), type);

return;
}

flags.getFlagValue("f").ifPresent(arg -> {
boolean isForced = Boolean.parseBoolean(arg);

if (isForced) keyType.set(KeyType.free_key);
});

boolean hasKey = this.config.getProperty(ConfigKeys.virtual_accepts_physical_keys) && keyType.get() == KeyType.physical_key ? this.userManager.getTotalKeys(player.getUniqueId(), crate.getName()) >= 1 : this.userManager.getVirtualKeys(player.getUniqueId(), crate.getName()) >= 1;
boolean hasKey = this.config.getProperty(ConfigKeys.virtual_accepts_physical_keys) && keyType == KeyType.physical_key ? this.userManager.getTotalKeys(player.getUniqueId(), crate.getName()) >= 1 : this.userManager.getVirtualKeys(player.getUniqueId(), crate.getName()) >= 1;

if (!hasKey && keyType.get() != KeyType.free_key) {
if (!hasKey && keyType != KeyType.free_key) {
//todo() convert this to a bean property!
if (this.config.getProperty(ConfigKeys.need_key_sound_toggle)) {
Sound sound = Sound.sound(Key.key(this.config.getProperty(ConfigKeys.need_key_sound)), Sound.Source.PLAYER, 1f, 1f);
Expand All @@ -185,7 +173,7 @@ public void others(CommandSender sender, @Suggestion("crates") String crateName,
return;
}

this.crateManager.openCrate(player, crate, keyType.get(), player.getLocation(), true, false);
this.crateManager.openCrate(player, crate, keyType, player.getLocation(), true, false);

final Map<String, String> placeholders = new HashMap<>();

Expand Down Expand Up @@ -234,7 +222,7 @@ public void mass(Player player, @Suggestion("crates") String crateName, @Suggest
return;
}

final KeyType keyType = getKeyType(player, type);
final KeyType keyType = getKeyType(type, false);

int keys = keyType == KeyType.physical_key ? this.userManager.getPhysicalKeys(player.getUniqueId(), crate.getName()) : this.userManager.getVirtualKeys(player.getUniqueId(), crate.getName());
int used = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void take(CommandSender sender, @Suggestion("keys") String type, @Suggest
return;
}

final KeyType keyType = getKeyType(sender, type);
final KeyType keyType = getKeyType(type, false);

final Crate crate = getCrate(sender, crateName, false);

Expand Down

0 comments on commit 33f92a4

Please sign in to comment.