Skip to content

Commit

Permalink
Prevent other placeable blocks from being placed on blocks if a key
Browse files Browse the repository at this point in the history
Prevents placeable blocks from being placed on blocks if a key like tripwire hook or candle
Updated how interaction with crates has been handled.
  • Loading branch information
ryderbelserion committed Jul 8, 2024
1 parent 2d123d7 commit 5c77071
Showing 1 changed file with 33 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockPistonExtendEvent;
import org.bukkit.event.block.BlockPistonRetractEvent;
import org.bukkit.event.player.PlayerInteractEvent;
Expand Down Expand Up @@ -50,55 +50,24 @@ public class CrateControlListener implements Listener {
private @NotNull final BukkitUserManager userManager = this.plugin.getUserManager();

@EventHandler
public void onLeftClickCrate(PlayerInteractEvent event) {
public void onGroundClick(PlayerInteractEvent event) {
final Player player = event.getPlayer();

if (event.getAction() != Action.LEFT_CLICK_BLOCK) return;
if (!event.getAction().isRightClick()) return;

final Block clickedBlock = event.getClickedBlock();

if (clickedBlock == null) return;

final CrateLocation crateLocation = this.crateManager.getCrateLocation(clickedBlock.getLocation());

if (crateLocation == null) return;

final boolean isKey = event.getHand() == EquipmentSlot.OFF_HAND ? this.crateManager.isKey(player.getInventory().getItemInOffHand()) : this.crateManager.isKey(player.getInventory().getItemInMainHand());

if (isKey) {
event.setCancelled(true);

player.updateInventory();

return;
}

event.setCancelled(true);

if (player.getGameMode() == GameMode.CREATIVE && player.isSneaking() && player.hasPermission("crazycrates.admin")) {
if (crateLocation.getLocation().equals(clickedBlock.getLocation())) {
this.crateManager.removeCrateLocation(crateLocation.getID());

player.sendRichMessage(Messages.removed_physical_crate.getMessage(player, "{id}", crateLocation.getID()));
}

return;
}

if (crateLocation.getCrateType() == CrateType.menu) return;

final Crate crate = crateLocation.getCrate();

if (crate.isPreviewEnabled()) {
this.inventoryManager.addViewer(player);
this.inventoryManager.openNewCratePreview(player, crateLocation.getCrate());
} else {
player.sendRichMessage(Messages.preview_disabled.getMessage(player, "{crate}", crate.getName()));
event.setUseItemInHand(Event.Result.DENY);
}
}

@EventHandler
public void onInteract(PlayerInteractEvent event) {
public void onCrateInteract(PlayerInteractEvent event) {
final Player player = event.getPlayer();

final Block clickedBlock = event.getClickedBlock();
Expand All @@ -110,9 +79,20 @@ public void onInteract(PlayerInteractEvent event) {
// If location is null, return.
if (crateLocation == null) return;

event.setUseInteractedBlock(Event.Result.DENY);
event.setUseItemInHand(Event.Result.DENY);

final Crate crate = crateLocation.getCrate();

event.setCancelled(true);
if (player.getGameMode() == GameMode.CREATIVE && player.isSneaking() && player.hasPermission("crazycrates.admin")) {
if (crateLocation.getLocation().equals(clickedBlock.getLocation())) {
this.crateManager.removeCrateLocation(crateLocation.getID());

player.sendRichMessage(Messages.removed_physical_crate.getMessage(player, "{id}", crateLocation.getID()));
}

return;
}

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.
Expand All @@ -123,6 +103,13 @@ public void onInteract(PlayerInteractEvent event) {
} else {
player.sendRichMessage(Messages.feature_disabled.getMessage(player));
}
} else {
if (crate.isPreviewEnabled()) {
this.inventoryManager.addViewer(player);
this.inventoryManager.openNewCratePreview(player, crateLocation.getCrate());
} else {
player.sendRichMessage(Messages.preview_disabled.getMessage(player, "{crate}", crate.getName()));
}
}
}

Expand All @@ -133,15 +120,14 @@ public void onInteract(PlayerInteractEvent event) {
public void onRightClick(PlayerInteractEvent event) {
final Player player = event.getPlayer();

if (event.getAction() != Action.RIGHT_CLICK_BLOCK || event.getHand() != EquipmentSlot.HAND) return;
if (!event.getAction().isRightClick() || event.getHand() != EquipmentSlot.HAND) return;

final Block clickedBlock = event.getClickedBlock();

if (clickedBlock == null) return;

final CrateLocation crateLocation = this.crateManager.getCrateLocation(clickedBlock.getLocation());

// If location is null, return.
if (crateLocation == null) return;

final Crate crate = crateLocation.getCrate();
Expand All @@ -150,12 +136,10 @@ public void onRightClick(PlayerInteractEvent event) {

final boolean isKey = event.getHand() == EquipmentSlot.OFF_HAND ? ItemUtils.isSimilar(player.getInventory().getItemInOffHand(), crate) : ItemUtils.isSimilar(player.getInventory().getItemInMainHand(), crate);

if (isKey) {
event.setCancelled(true);
player.updateInventory();
}
if (!isKey) return;

event.setCancelled(true);
event.setUseInteractedBlock(Event.Result.DENY);
event.setUseItemInHand(Event.Result.DENY);

final KeyCheckEvent keyCheckEvent = new KeyCheckEvent(player, crateLocation);
player.getServer().getPluginManager().callEvent(keyCheckEvent);
Expand Down Expand Up @@ -186,17 +170,17 @@ public void onRightClick(PlayerInteractEvent event) {

final ItemStack itemStack = player.getInventory().getItemInMainHand();

if (crate.getCrateType() != CrateType.crate_on_the_go && isKey && ItemUtils.isSimilar(itemStack, crate) && this.config.getProperty(ConfigKeys.physical_accepts_physical_keys)) {
if (crate.getCrateType() != CrateType.crate_on_the_go && ItemUtils.isSimilar(itemStack, crate) && this.config.getProperty(ConfigKeys.physical_accepts_physical_keys)) {
hasKey = true;
isPhysical = true;
}

if (this.config.getProperty(ConfigKeys.physical_accepts_virtual_keys) && this.userManager.getVirtualKeys(player.getUniqueId(), crate.getName()) >= 1) hasKey = true;

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

placeholders.put("{crate}", crate.getName());
placeholders.put("{key}", keyName);
final Map<String, String> placeholders = new HashMap<>() {{
put("{crate}", crate.getName());
put("{key}", keyName);
}};

if (hasKey) {
// Checks if the player uses the quick crate again.
Expand Down

0 comments on commit 5c77071

Please sign in to comment.