Skip to content

Commit

Permalink
Resolvido problema com RequiredKeys > 1
Browse files Browse the repository at this point in the history
  • Loading branch information
SrBedrock committed Nov 1, 2024
1 parent aa7b900 commit 099b0e8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,62 +49,39 @@ public class CrateControlListener implements Listener {
// the items that were added to the players inventory and replaced the item in the player's hand.
// This is only an issue with QuickCrate
@EventHandler(priority = EventPriority.HIGHEST)
public void onCrateOpen(PlayerInteractEvent e) {
Player player = e.getPlayer();

if (e.getHand() == EquipmentSlot.OFF_HAND) {
// if (this.plugin.getCrateManager().isKey(player.getInventory().getItemInOffHand())) {
// e.setCancelled(true);
// player.updateInventory();
// }

return;
}

Block clickedBlock = e.getClickedBlock();
public void onCrateOpen(final PlayerInteractEvent e) {
final Player player = e.getPlayer();
if (e.getHand() == EquipmentSlot.OFF_HAND) return;
final Block clickedBlock = e.getClickedBlock();

if (e.getAction() == Action.LEFT_CLICK_BLOCK) {
// Loops through all loaded physical locations.
for (CrateLocation loc : this.plugin.getCrateManager().getCrateLocations()) {
for (final CrateLocation location : this.plugin.getCrateManager().getCrateLocations()) {
// Checks to see if the clicked block is the same as a physical crate.
//todo() if someone complains, remove null check.
if (clickedBlock != null && loc.getLocation().equals(clickedBlock.getLocation())) {
if (clickedBlock != null && location.getLocation().equals(clickedBlock.getLocation())) {
// Checks to see if the player is removing a crate location.
if (player.getGameMode() == GameMode.CREATIVE && player.isSneaking() && player.hasPermission("crazycrates.admin")) {
e.setCancelled(true);
this.plugin.getCrateManager().removeCrateLocation(loc.getID());
player.sendMessage(Messages.removed_physical_crate.getMessage("%id%", loc.getID(), player));
this.plugin.getCrateManager().removeCrateLocation(location.getID());
player.sendMessage(Messages.removed_physical_crate.getMessage("%id%", location.getID(), player));
return;
}

e.setCancelled(true);

if (loc.getCrateType() != CrateType.menu) {
Crate crate = loc.getCrate();
if (location.getCrateType() != CrateType.menu) {
final Crate crate = location.getCrate();

if (loc.getCrate().isPreviewEnabled()) {
if (location.getCrate().isPreviewEnabled()) {
this.inventoryManager.addViewer(player);
this.inventoryManager.openNewCratePreview(player, loc.getCrate(), crate.getCrateType() == CrateType.cosmic || crate.getCrateType() == CrateType.casino);
this.inventoryManager.openNewCratePreview(player, location.getCrate(), crate.getCrateType() == CrateType.cosmic || crate.getCrateType() == CrateType.casino);
} else {
player.sendMessage(Messages.preview_disabled.getMessage(player));
}
}
}
}
} else if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
// Checks if the item in their hand is a key and if so it stops them from right-clicking with it.
// ItemStack key = player.getInventory().getItemInMainHand();
// boolean keyInHand = this.crateManager.isKey(key);
//
// if (!keyInHand) {
// keyInHand = this.crateManager.isKey(player.getEquipment().getItemInOffHand());
// }
//
// if (keyInHand) {
// e.setCancelled(true);
// player.updateInventory();
// }

//Checks to see if the clicked block is a physical crate.
CrateLocation crateLocation = null;

Expand All @@ -114,13 +91,13 @@ public void onCrateOpen(PlayerInteractEvent e) {
}

if (crateLocation != null && crateLocation.getCrate() != null) {
Crate crate = crateLocation.getCrate();
final Crate crate = crateLocation.getCrate();
e.setCancelled(true);

if (crate.getCrateType() == CrateType.menu) {
//This is to stop players in QuadCrate to not be able to try and open a crate set to menu.
if (!this.crateManager.isInOpeningList(player) && this.config.getProperty(ConfigKeys.enable_crate_menu)) {
CrateMainMenu crateMainMenu = new CrateMainMenu(player, this.config.getProperty(ConfigKeys.inventory_size), this.config.getProperty(ConfigKeys.inventory_name));
final CrateMainMenu crateMainMenu = new CrateMainMenu(player, this.config.getProperty(ConfigKeys.inventory_size), this.config.getProperty(ConfigKeys.inventory_name));

player.openInventory(crateMainMenu.build().getInventory());
} else {
Expand All @@ -130,21 +107,20 @@ public void onCrateOpen(PlayerInteractEvent e) {
return;
}

KeyCheckEvent event = new KeyCheckEvent(player, crateLocation);
final KeyCheckEvent event = new KeyCheckEvent(player, crateLocation);
player.getServer().getPluginManager().callEvent(event);

if (!event.isCancelled()) {
boolean hasKey = false;
// boolean isPhysical = false;
boolean useQuickCrateAgain = false;
String keyName = crate.getKeyName();
final String keyName = crate.getKeyName();

int requiredKeys = this.plugin.getCrateManager().getCrateFromName(crate.getName()).getRequiredKeys();
final int requiredKeys = this.plugin.getCrateManager().getCrateFromName(crate.getName()).getRequiredKeys();

int totalKeys = this.plugin.getCrazyHandler().getUserManager().getTotalKeys(player.getUniqueId(), crate.getName());
final int totalKeys = this.plugin.getCrazyHandler().getUserManager().getTotalKeys(player.getUniqueId(), crate.getName());

if (requiredKeys > 0 && totalKeys < requiredKeys) {
HashMap<String, String> placeholders = new HashMap<>();
final HashMap<String, String> placeholders = new HashMap<>();
placeholders.put("%key-amount%", String.valueOf(requiredKeys));
placeholders.put("%crate%", crate.getPreviewName());
placeholders.put("%amount%", String.valueOf(totalKeys));
Expand All @@ -153,11 +129,6 @@ public void onCrateOpen(PlayerInteractEvent e) {
return;
}

// if (crate.getCrateType() != CrateType.crate_on_the_go && keyInHand && this.crateManager.isKeyFromCrate(key, crate) && this.config.getProperty(ConfigKeys.physical_accepts_physical_keys)) {
// hasKey = true;
// isPhysical = true;
// }

if (this.config.getProperty(ConfigKeys.physical_accepts_virtual_keys) && this.plugin.getCrazyHandler().getUserManager().getVirtualKeys(player.getUniqueId(), crate.getName()) >= 1) {
hasKey = true;
}
Expand Down Expand Up @@ -189,7 +160,7 @@ public void onCrateOpen(PlayerInteractEvent e) {
this.plugin.getCrateManager().endQuickCrate(player, crateLocation.getLocation(), crate, true);
}

KeyType keyType = KeyType.virtual_key;
final KeyType keyType = KeyType.virtual_key;

// Only cosmic crate type uses this method.
if (crate.getCrateType() == CrateType.cosmic) {
Expand Down Expand Up @@ -218,8 +189,8 @@ public void onCrateOpen(PlayerInteractEvent e) {
}

@EventHandler
public void onPlayerQuit(PlayerQuitEvent e) {
Player player = e.getPlayer();
public void onPlayerQuit(final PlayerQuitEvent e) {
final Player player = e.getPlayer();

if (this.crateManager.hasCrateTask(player)) {
this.crateManager.endCrate(player);
Expand All @@ -234,8 +205,8 @@ public void onPlayerQuit(PlayerQuitEvent e) {
}
}

private void knockBack(Player player, Location location) {
Vector vector = player.getLocation().toVector().subtract(location.toVector()).normalize().multiply(1).setY(.1);
private void knockBack(final Player player, final Location location) {
final Vector vector = player.getLocation().toVector().subtract(location.toVector()).normalize().multiply(1).setY(.1);

if (player.isInsideVehicle() && player.getVehicle() != null) {
player.getVehicle().setVelocity(vector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@

public class CsgoCrate extends CrateBuilder {

public CsgoCrate(Crate crate, Player player, int size) {
public CsgoCrate(final Crate crate, final Player player, final int size) {
super(crate, player, size);
}

@Override
public void open(KeyType type, boolean checkHand) {
public void open(final KeyType type, final boolean checkHand) {
// Crate event failed so we return.
if (isCrateEventValid(type, checkHand)) {
return;
}

boolean keyCheck = this.plugin.getCrazyHandler().getUserManager().takeKeys(1, getPlayer().getUniqueId(), getCrate().getName(), type, checkHand);
final boolean keyCheck = this.plugin.getCrazyHandler().getUserManager().takeKeys(getCrate().getRequiredKeys(), getPlayer().getUniqueId(), getCrate().getName(), type, checkHand);

if (!keyCheck) {
// Send the message about failing to take the key.
Expand Down Expand Up @@ -85,10 +85,10 @@ public void run() {

plugin.getCrateManager().endCrate(getPlayer());

ItemStack item = getInventory().getItem(13);
final ItemStack item = getInventory().getItem(13);

if (item != null) {
Prize prize = getCrate().getPrize(item);
final Prize prize = getCrate().getPrize(item);

PrizeManager.givePrize(getPlayer(), getCrate(), prize);
}
Expand All @@ -113,13 +113,13 @@ public void run() {
}

private void populate() {
HashMap<Integer, ItemStack> glass = new HashMap<>();
final HashMap<Integer, ItemStack> glass = new HashMap<>();

for (int index = 0; index < 10; index++) {
if (index < 9 && index != 3) glass.put(index, getInventory().getItem(index));
}

for (int index : glass.keySet()) {
for (final int index : glass.keySet()) {
if (getInventory().getItem(index) == null) {
setCustomGlassPane(index);
setCustomGlassPane(index + 18);
Expand All @@ -141,7 +141,7 @@ private void populate() {
setItem(3, glass.get(5));
setItem(3 + 18, glass.get(5));

ItemStack itemStack = new ItemBuilder().setMaterial(Material.BLACK_STAINED_GLASS_PANE).setName(" ").build();
final ItemStack itemStack = new ItemBuilder().setMaterial(Material.BLACK_STAINED_GLASS_PANE).setName(" ").build();
setItem(4, itemStack);
setItem(4 + 18, itemStack);

Expand All @@ -164,7 +164,7 @@ private void populate() {
}

private void moveItemsAndSetGlass() {
List<ItemStack> items = new ArrayList<>();
final List<ItemStack> items = new ArrayList<>();

for (int i = 9; i > 8 && i < 17; i++) {
items.add(getInventory().getItem(i));
Expand Down

0 comments on commit 099b0e8

Please sign in to comment.