use of com.badbones69.crazycrates.api.enums.KeyType in project Crazy-Crates by Crazy-Crew.
the class CrateControl method onCrateOpen.
// This must run as highest, so it doesn't cause other plugins to check
// 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();
FileConfiguration config = Files.CONFIG.getFile();
if (e.getHand() == EquipmentSlot.OFF_HAND) {
if (cc.isKey(player.getInventory().getItemInOffHand())) {
e.setCancelled(true);
player.updateInventory();
}
return;
}
Block clickedBlock = e.getClickedBlock();
if (e.getAction() == Action.LEFT_CLICK_BLOCK) {
// Loops through all loaded physical locations.
for (CrateLocation loc : cc.getCrateLocations()) {
// Checks to see if the clicked block is the same as a physical crate.
if (loc.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);
cc.removeCrateLocation(loc.getID());
player.sendMessage(Messages.REMOVED_PHYSICAL_CRATE.getMessage("%ID%", loc.getID()));
return;
}
e.setCancelled(true);
if (loc.getCrateType() != CrateType.MENU) {
if (loc.getCrate().isPreviewEnabled()) {
Preview.setPlayerInMenu(player, false);
Preview.openNewPreview(player, loc.getCrate());
} else {
player.sendMessage(Messages.PREVIEW_DISABLED.getMessage());
}
}
}
}
} 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 = cc.getNMSSupport().getItemInMainHand(player);
boolean keyInHand = cc.isKey(key);
if (!keyInHand) {
keyInHand = cc.isKey(player.getEquipment().getItemInOffHand());
}
if (keyInHand) {
e.setCancelled(true);
player.updateInventory();
}
// Checks to see if the clicked block is a physical crate.
CrateLocation crateLocation = cc.getCrateLocation(clickedBlock.getLocation());
if (crateLocation != null && crateLocation.getCrate() != null) {
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 (!cc.isInOpeningList(player)) {
GUIMenu.openGUI(player);
}
return;
}
PhysicalCrateKeyCheckEvent event = new PhysicalCrateKeyCheckEvent(player, crateLocation);
CrazyManager.getJavaPlugin().getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) {
boolean hasKey = false;
boolean isPhysical = false;
boolean useQuickCrateAgain = false;
String keyName = crate.getKey().getItemMeta().getDisplayName();
keyName = keyName != null ? keyName : crate.getKey().getType().toString();
if (crate.getCrateType() != CrateType.CRATE_ON_THE_GO && keyInHand && cc.isKeyFromCrate(key, crate)) {
hasKey = true;
isPhysical = true;
}
if (config.getBoolean("Settings.Physical-Accepts-Virtual-Keys") && cc.getVirtualKeys(player, crate) >= 1) {
hasKey = true;
}
if (hasKey) {
// Checks if the player uses the quick crate again.
if (cc.isInOpeningList(player) && cc.getOpeningCrate(player).getCrateType() == CrateType.QUICK_CRATE && inUse.containsKey(player) && inUse.get(player).equals(crateLocation.getLocation())) {
useQuickCrateAgain = true;
}
if (!useQuickCrateAgain) {
if (cc.isInOpeningList(player)) {
player.sendMessage(Messages.ALREADY_OPENING_CRATE.getMessage("%Key%", keyName));
return;
}
if (inUse.containsValue(crateLocation.getLocation())) {
player.sendMessage(Messages.QUICK_CRATE_IN_USE.getMessage());
return;
}
}
if (Methods.isInventoryFull(player)) {
player.sendMessage(Messages.INVENTORY_FULL.getMessage());
return;
}
if (useQuickCrateAgain) {
QuickCrate.endQuickCrate(player, crateLocation.getLocation());
}
KeyType keyType = isPhysical ? KeyType.PHYSICAL_KEY : KeyType.VIRTUAL_KEY;
if (crate.getCrateType() == CrateType.COSMIC) {
// Only cosmic crate type uses this method.
cc.addPlayerKeyType(player, keyType);
}
cc.addPlayerToOpeningList(player, crate);
cc.openCrate(player, crate, keyType, crateLocation.getLocation(), false, true);
} else {
if (crate.getCrateType() != CrateType.CRATE_ON_THE_GO) {
if (config.getBoolean("Settings.KnockBack")) {
knockBack(player, clickedBlock.getLocation());
}
if (config.contains("Settings.Need-Key-Sound")) {
Sound sound = Sound.valueOf(config.getString("Settings.Need-Key-Sound"));
if (sound != null) {
player.playSound(player.getLocation(), sound, 1f, 1f);
}
}
player.sendMessage(Messages.NO_KEY.getMessage("%Key%", keyName));
}
}
}
}
}
}
Aggregations