From 240ef0930106e55288248afef2be4767d46ddd6a Mon Sep 17 00:00:00 2001 From: BuildTools Date: Tue, 31 Oct 2023 15:41:57 +0000 Subject: [PATCH] add placeholder for entity name --- .../steve4744/whatisthis/WhatIsThisPlaceholders.java | 5 +++++ .../whatisthis/commands/WhatIsThisCommand.java | 11 +++++++---- .../github/steve4744/whatisthis/data/DataHandler.java | 2 +- .../io/github/steve4744/whatisthis/utils/Utils.java | 10 +++++++++- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/github/steve4744/whatisthis/WhatIsThisPlaceholders.java b/src/main/java/io/github/steve4744/whatisthis/WhatIsThisPlaceholders.java index 28676eb..278f6ef 100644 --- a/src/main/java/io/github/steve4744/whatisthis/WhatIsThisPlaceholders.java +++ b/src/main/java/io/github/steve4744/whatisthis/WhatIsThisPlaceholders.java @@ -27,6 +27,7 @@ of this software and associated documentation files (the "Software"), to deal import org.bukkit.OfflinePlayer; import org.bukkit.block.Block; import org.bukkit.entity.Player; +import org.bukkit.util.RayTraceResult; import io.github.steve4744.whatisthis.utils.Utils; import me.clip.placeholderapi.expansion.PlaceholderExpansion; @@ -81,6 +82,10 @@ public String onRequest(OfflinePlayer p, String identifier) { } else if (identifier.equals("blockname")) { return plugin.getDataHandler().getDisplayName(Utils.getTargetBlock(player), player); + } else if (identifier.equals("entityname")) { + RayTraceResult result = Utils.getRayTraceResult(player); + return Utils.isEntity(result) ? plugin.getDataHandler().getEntityDisplayName(result.getHitEntity(), player) : ""; + } else if (identifier.equals("resourcename")) { return plugin.getDataHandler().getCustomResourceName(Utils.getTargetBlock(player)); diff --git a/src/main/java/io/github/steve4744/whatisthis/commands/WhatIsThisCommand.java b/src/main/java/io/github/steve4744/whatisthis/commands/WhatIsThisCommand.java index 8120fec..b513755 100644 --- a/src/main/java/io/github/steve4744/whatisthis/commands/WhatIsThisCommand.java +++ b/src/main/java/io/github/steve4744/whatisthis/commands/WhatIsThisCommand.java @@ -1,7 +1,7 @@ /* * MIT License -Copyright (c) 2022 steve4744 +Copyright (c) 2023 steve4744 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -48,7 +48,7 @@ public WhatIsThisCommand(String version, WhatIsThis plugin) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { String prefix = ChatColor.GREEN + "[WhatIsThis] " + ChatColor.WHITE; String infoMessage = prefix + "Version " + version + " : plugin by "+ ChatColor.AQUA + "steve4744"; - + if (!sender.hasPermission("whatisthis.use")) { sender.sendMessage(prefix + "You do not have permission to run this command"); return true; @@ -78,16 +78,19 @@ public boolean onCommand(CommandSender sender, Command command, String label, St sender.sendMessage(infoMessage); return false; } + Player player = (Player) sender; RayTraceResult result = Utils.getRayTraceResult(player); if (result == null) { return true; } - if (result.getHitBlock() != null) { + + if (Utils.isBlock(result)) { plugin.getDataHandler().processBlock(result.getHitBlock(), player); - } else if (result.getHitEntity() != null) { + } else if (Utils.isEntity(result)) { plugin.getDataHandler().processEntity(result.getHitEntity(), player); } + return true; } diff --git a/src/main/java/io/github/steve4744/whatisthis/data/DataHandler.java b/src/main/java/io/github/steve4744/whatisthis/data/DataHandler.java index 0404954..8821385 100644 --- a/src/main/java/io/github/steve4744/whatisthis/data/DataHandler.java +++ b/src/main/java/io/github/steve4744/whatisthis/data/DataHandler.java @@ -155,7 +155,7 @@ public String getDisplayName(Block block, Player player) { return translatedName; } - private String getEntityDisplayName(Entity entity, Player player) { + public String getEntityDisplayName(Entity entity, Player player) { if (isBlacklistedEntity(entity)) { return ""; } diff --git a/src/main/java/io/github/steve4744/whatisthis/utils/Utils.java b/src/main/java/io/github/steve4744/whatisthis/utils/Utils.java index 4eb3c40..b8416ed 100644 --- a/src/main/java/io/github/steve4744/whatisthis/utils/Utils.java +++ b/src/main/java/io/github/steve4744/whatisthis/utils/Utils.java @@ -1,7 +1,7 @@ /* * MIT License -Copyright (c) 2022 steve4744 +Copyright (c) 2023 steve4744 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -78,6 +78,14 @@ public static RayTraceResult getRayTraceResult(Player player) { entity -> !entity.getUniqueId().equals(player.getUniqueId())); } + public static boolean isBlock(RayTraceResult result) { + return result != null && result.getHitBlock() != null; + } + + public static boolean isEntity(RayTraceResult result) { + return result != null && result.getHitEntity() != null; + } + /** * Get a string representation of the location of the targeted block. *