Skip to content

Commit

Permalink
Merge pull request #30 from shantek/2.0.0-Dev
Browse files Browse the repository at this point in the history
Switch to ray trace for look at block and fixed issues with clearing …
  • Loading branch information
shantek authored Sep 22, 2024
2 parents 281d73c + acfdded commit b07b911
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 10 deletions.
65 changes: 55 additions & 10 deletions src/main/java/io/shantek/functions/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
if (player.hasPermission("shantek.postoffice.register") || player.isOp()) {

// Get the block the player is looking at (sign or barrel)
Block targetBlock = player.getTargetBlock(null, 10);
Block targetBlock = postOffice.helpers.getBlockLookingAt(player, 6);

// Exit out if they aren't looking at a block or are too far away
if (targetBlock == null) {
player.sendMessage(ChatColor.translateAlternateColorCodes('&', postOffice.language.lookAtPostBox));
return true;
}

Block barrelBlock = null;

Expand Down Expand Up @@ -63,9 +69,19 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
Block signBlock = postOffice.helpers.getSignForBarrel(barrelBlock);
if (signBlock != null && signBlock.getState() instanceof Sign) {
Sign sign = (Sign) signBlock.getState();
sign.setLine(1, ""); // Clear the second line
sign.setLine(2, ""); // Clear the third line
sign.update(); // Update the sign

// Clear all lines of the sign (if you want to fully reset it)
for (int i = 0; i < 4; i++) {
sign.setLine(i, "");
}

// Finally, update the sign
boolean signUpdated = sign.update();

// Log if the sign was successfully updated
if (!signUpdated) {
player.sendMessage(ChatColor.RED + "There was an issue updating the sign.");
}
}

// Call the helper to remove the barrel from the cache and config
Expand Down Expand Up @@ -102,7 +118,14 @@ else if (args.length == 1 && args[0].equalsIgnoreCase("reload")) {
if (sender instanceof Player) {
Player player = (Player) sender;

Block targetBlock = player.getTargetBlock(null, 10); // Get the block the player is looking at
// Get the block the player is looking at (sign or barrel)
Block targetBlock = postOffice.helpers.getBlockLookingAt(player, 6);

// Exit out if they aren't looking at a block or are too far away
if (targetBlock == null) {
player.sendMessage(ChatColor.translateAlternateColorCodes('&', postOffice.language.lookAtPostBox));
return true;
}

Block barrelBlock = null;

Expand Down Expand Up @@ -153,8 +176,15 @@ else if (args.length == 1 && args[0].equalsIgnoreCase("reload")) {
Player player = (Player) sender;
if (sender.hasPermission("shantek.postoffice.register") || sender.isOp()) {

// Ensure they are looking at a sign
Block targetBlock = player.getTargetBlock(null, 10); // Max distance 10 blocks
// Get the block the player is looking at (sign or barrel)
Block targetBlock = postOffice.helpers.getBlockLookingAt(player, 6);

// Exit out if they aren't looking at a block or are too far away
if (targetBlock == null) {
player.sendMessage(ChatColor.translateAlternateColorCodes('&', postOffice.language.lookAtPostBox));
return true;
}

if (targetBlock == null || !(targetBlock.getState() instanceof Sign)) {
player.sendMessage(ChatColor.translateAlternateColorCodes('&', postOffice.language.lookAtPostBox));
return true;
Expand Down Expand Up @@ -224,7 +254,15 @@ else if (args.length == 1 && args[0].equalsIgnoreCase("reload")) {
}

Player player = (Player) sender;
Block targetBlock = player.getTargetBlock(null, 10); // Max distance 10 blocks
// Get the block the player is looking at (sign or barrel)
Block targetBlock = postOffice.helpers.getBlockLookingAt(player, 6);

// Exit out if they aren't looking at a block or are too far away
if (targetBlock == null) {
player.sendMessage(ChatColor.translateAlternateColorCodes('&', postOffice.language.lookAtPostBox));
return true;
}

if (targetBlock == null || !(targetBlock.getState() instanceof Sign)) {
player.sendMessage(ChatColor.translateAlternateColorCodes('&', postOffice.language.lookAtPostBox));
return true;
Expand Down Expand Up @@ -313,8 +351,15 @@ else if (args.length == 1 && args[0].equalsIgnoreCase("reload")) {

if (sender.hasPermission("shantek.postoffice.claim") || sender.isOp()) {

// Ensure they are looking at a sign
Block targetBlock = player.getTargetBlock(null, 10); // Max distance 10 blocks
// Get the block the player is looking at (sign or barrel)
Block targetBlock = postOffice.helpers.getBlockLookingAt(player, 6);

// Exit out if they aren't looking at a block or are too far away
if (targetBlock == null) {
player.sendMessage(ChatColor.translateAlternateColorCodes('&', postOffice.language.lookAtPostBox));
return true;
}

if (targetBlock == null || !(targetBlock.getState() instanceof Sign)) {
player.sendMessage(ChatColor.translateAlternateColorCodes('&', postOffice.language.lookAtPostBox));
return true;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/io/shantek/functions/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.RayTraceResult;

public class Helpers {

Expand Down Expand Up @@ -276,6 +277,20 @@ public boolean isSignNextToProtectedBarrel(Block signBlock) {
return false;
}

public Block getBlockLookingAt(Player player, double maxDistance) {

// Perform a ray trace to get the block the player is directly looking at
Block targetBlock = null;
RayTraceResult result = player.rayTraceBlocks(maxDistance);

// Check if the ray trace hit a block
if (result != null && result.getHitBlock() != null) {
targetBlock = result.getHitBlock();
}

return targetBlock;
}

//endregion

//region Configuration loading/saving
Expand Down

0 comments on commit b07b911

Please sign in to comment.