Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for item frames #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,17 @@ public void runCreate(Player p, String id) {
return;
}

// Test for item frame
Location finalLocation = targetBlock.getLocation();
final Location blockFaceLocation = Utils.getBlockFaceLocation(p);

if (blockFaceLocation != null && Utils.isItemFrame(blockFaceLocation)) {
finalLocation = blockFaceLocation;
}

final CommandButton button = new CommandButton(
id,
new ArrayList<>(Collections.singletonList(targetBlock.getLocation())),
new ArrayList<>(Collections.singletonList(finalLocation)),
"none",
true,
3000L,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.bukkit.entity.Player;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.EquipmentSlot;
Expand All @@ -56,6 +57,48 @@ public class ButtonListener implements TerminableModule {
@Override
public void setup(@NotNull TerminableConsumer consumer) {

Events.subscribe(PlayerInteractEntityEvent.class, EventPriority.HIGH)
.handler(e -> {
if (e.getRightClicked() == null
|| (Common.isServerVersionAtLeast(9) && e.getHand() == EquipmentSlot.OFF_HAND)) {
return;
}

final CommandButton button = i.getButtonsManager()
.getButtonByLocation(e.getRightClicked().getLocation());

if (button == null) {
return;
}

if (lastInteracted.getOrDefault(e.getPlayer(), 0L) + TIMEOUT
>= System.currentTimeMillis()) {
e.setCancelled(true);
return;
}

lastInteracted.put(e.getPlayer(), System.currentTimeMillis());

final boolean success = button.use(e.getPlayer());

// If the command button use was successful.
if (success) {

// Cancel interaction if material is on the "disable interaction" list.
if (i.getSettings().getStringList("disable-interaction").stream()
.anyMatch(str -> str.equalsIgnoreCase(e.getRightClicked().getType().name()))) {
e.setCancelled(true);
}

return;
}

// Cancel interaction if unsuccessful.
e.setCancelled(true);
})
.bindWith(consumer);


Events.subscribe(PlayerInteractEvent.class, EventPriority.HIGH)
.handler(e -> {
if (e.getClickedBlock() == null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import dev.demeng.commandbuttons.CommandButtons;
import dev.demeng.commandbuttons.model.CommandButton;
import dev.demeng.commandbuttons.util.LocationSerializer;
import dev.demeng.commandbuttons.util.Utils;
import dev.demeng.pluginbase.Common;
import dev.demeng.pluginbase.Time;
import dev.demeng.pluginbase.Time.DurationFormatter;
Expand Down Expand Up @@ -75,8 +76,10 @@ private void addLocationsButton() {
lore.add("&6Current:");

for (Location loc : button.getLocations()) {
final String blockName = (Utils.isItemFrame(loc)) ? "Item Frame" : loc.getBlock().getType().name();

lore.add("&f- " + LocationSerializer.serialize(loc)
+ " (" + loc.getBlock().getType().name() + ")");
+ " (" + blockName + ")");
}

lore.add("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public ButtonsListMenu(CommandButtons i, Player p) {
if (!Utils.isAir(loc.getBlock())) {
stack = new ItemStack(loc.getBlock().getType());
break;
} else if (Utils.isItemFrame(loc)) {
stack = new ItemStack(Material.ITEM_FRAME);
break;
}
}

Expand All @@ -104,8 +107,10 @@ public ButtonsListMenu(CommandButtons i, Player p) {
lore.add("&6Locations");

for (Location loc : button.getLocations()) {
final String blockName = (Utils.isItemFrame(loc)) ? "Item Frame" : loc.getBlock().getType().name();

lore.add("&f- " + LocationSerializer.serialize(loc)
+ " (" + loc.getBlock().getType().name() + ")");
+ " (" + blockName + ")");
}

lore.add("");
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/dev/demeng/commandbuttons/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@
package dev.demeng.commandbuttons.util;

import dev.demeng.pluginbase.Common;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.Player;

import java.util.List;

/**
* General utilities.
Expand All @@ -43,4 +49,37 @@ public static boolean isAir(Block block) {
return block == null || block.getType() == Material.AIR
|| (Common.isServerVersionAtLeast(13) && block.getType().isAir());
}

/**
* Gets the Location of the BlockFace of the block the player is currently targeting.
*
* @param player the player whose targeted blocks BlockFace is to be checked.
* @return the Location of the BlockFace of the targeted block, or null if the targeted block is non-occluding.
*/
public static Location getBlockFaceLocation(Player player) {
List<Block> lastTwoTargetBlocks = player.getLastTwoTargetBlocks(null, 100);
if (lastTwoTargetBlocks.size() != 2 || !lastTwoTargetBlocks.get(1).getType().isOccluding()) return null;
Block targetBlock = lastTwoTargetBlocks.get(1);
Block adjacentBlock = lastTwoTargetBlocks.get(0);

return adjacentBlock.getLocation();
}

/**
* Checks if the provided block has an item frame occupying it.
*
* @param location the location to be checked
* @return true or false
*/
public static Boolean isItemFrame(Location location) {
for (Entity e : location.getChunk().getEntities()) {
if (e instanceof ItemFrame
&& (e.getLocation().getBlockX() == location.getBlockX())
&& (e.getLocation().getBlockY() == location.getBlockY())
&& (e.getLocation().getBlockZ() == location.getBlockZ())) {
return true;
}
}
return false;
}
}