Skip to content

Commit

Permalink
Add action health mechanic
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbit4 committed Sep 21, 2024
1 parent 5f312a1 commit 7886cf0
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package pl.norbit.survivaltweaks.mechanics;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.*;
import org.bukkit.inventory.ItemStack;
import pl.norbit.survivaltweaks.mechanics.model.Mechanic;
import pl.norbit.survivaltweaks.settings.Config;
import pl.norbit.survivaltweaks.utils.PlayerUtils;

import static pl.norbit.survivaltweaks.utils.TaskUtils.sync;

public class ActionHealthMechanic {

private ActionHealthMechanic() {
throw new IllegalStateException("Utility class");
}

protected static void check(Player p, ItemStack itemInMainHand, ItemStack itemInOffHand){
if(MechanicsLoader.isDisabled(Mechanic.ENTITY_HP)) {
return;
}

Material mainType = itemInMainHand.getType();
Material offType = itemInOffHand.getType();

if((mainType == Material.COMPASS || offType == Material.COMPASS
|| mainType == Material.CLOCK || offType == Material.CLOCK
|| mainType == Material.SPYGLASS || offType == Material.SPYGLASS
|| mainType == Material.RECOVERY_COMPASS || offType == Material.RECOVERY_COMPASS)){
return;
}

sync(() ->{
Entity targetEntity = PlayerUtils.getEntityLookingAt(p, 4);

if(targetEntity == null){
return;
}

if(!(targetEntity instanceof LivingEntity livingEntity)){
return;
}

if(targetEntity instanceof ArmorStand){
return;
}

//check npc
if(targetEntity instanceof Player player){
String playerName = player.getName();

if(PlayerUtils.getPlayerByName(playerName) == null){
return;
}
}

String message = Config.getEntityHpDisplay()
.replace("{ENTITY}", targetEntity.getName())
.replace("{HEALTH}", String.valueOf((int) livingEntity.getHealth()));

PlayerUtils.sendActionBar(p, message);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public static boolean isEnabled(Mechanic mechanic) {
case MINE_SPAWNERS -> Config.isMineSpawnersEnabled();
case HORSE_TP -> Config.isHorseTpEnabled();
case AMETHYST -> Config.isAmethystEnabled();
case ENTITY_HP -> Config.isEntityHpEnabled();
};
}

Expand All @@ -61,6 +62,7 @@ private static void heldItemTask(){
ItemStack itemInMainHand = p.getInventory().getItemInMainHand();
ItemStack itemInOffHand = p.getInventory().getItemInOffHand();

ActionHealthMechanic.check(p, itemInMainHand, itemInOffHand);
CompassMechanic.check(p, itemInMainHand, itemInOffHand);
ClockMechanic.check(p, itemInMainHand, itemInOffHand);
RecoveryCompassMechanic.check(p, itemInMainHand, itemInOffHand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void onPlayerInteract(PlayerInteractEvent e) {
return;
}

Entity targetEntity = getEntityLookingAt(p, 80);
Entity targetEntity = PlayerUtils.getEntityLookingAt(p, 80);

if (targetEntity == null) {
return;
Expand All @@ -56,6 +56,18 @@ public void onPlayerInteract(PlayerInteractEvent e) {
return;
}

if(targetEntity instanceof ArmorStand){
return;
}

if(targetEntity instanceof Player player){
String playerName = player.getName();

if(PlayerUtils.getPlayerByName(playerName) == null){
return;
}
}

SpyGlassMechanic.addGlowingEntity(targetEntity, p, 25);

int distance = (int) p.getLocation().distance(targetEntity.getLocation());
Expand All @@ -70,14 +82,4 @@ public void onPlayerInteract(PlayerInteractEvent e) {
int spyglassCooldown = Config.getSpyglassCooldown();
p.setCooldown(Material.SPYGLASS, 20 * spyglassCooldown);
}

public static Entity getEntityLookingAt(Player p, double maxDistance) {
Location eyeLocation = p.getEyeLocation();
Vector direction = eyeLocation.getDirection();

RayTraceResult rayTraceResult = p.getWorld().rayTraceEntities(
eyeLocation, direction, maxDistance, entity -> entity != p);

return rayTraceResult != null ? rayTraceResult.getHitEntity() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public enum Mechanic {
CUSTOM_DEATH_MESSAGES,
MINE_SPAWNERS,
HORSE_TP,
AMETHYST
AMETHYST,
ENTITY_HP
}
18 changes: 18 additions & 0 deletions src/main/java/pl/norbit/survivaltweaks/utils/PlayerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.util.RayTraceResult;
import org.bukkit.util.Vector;
import pl.norbit.survivaltweaks.SurvivalTweaks;

import java.util.ArrayList;
Expand All @@ -23,6 +27,10 @@ public static Optional<Player> getPlayer(UUID playerUUID) {
return Optional.ofNullable(SurvivalTweaks.getInstance().getServer().getPlayer(playerUUID));
}

public static Player getPlayerByName(String playerName) {
return SurvivalTweaks.getInstance().getServer().getPlayer(playerName);
}

public static ItemStack getCustomSkull(OfflinePlayer offlinePlayer) {
ItemStack head = new ItemStack(Material.PLAYER_HEAD);

Expand All @@ -40,4 +48,14 @@ public static List<Player> getOnlinePlayers() {
public static void sendActionBar(Player p, String message) {
p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(ChatUtils.format(message, p)));
}

public static Entity getEntityLookingAt(Player p, double maxDistance) {
Location eyeLocation = p.getEyeLocation();
Vector direction = eyeLocation.getDirection();

RayTraceResult rayTraceResult = p.getWorld().rayTraceEntities(
eyeLocation, direction, maxDistance, entity -> entity != p);

return rayTraceResult != null ? rayTraceResult.getHitEntity() : null;
}
}

0 comments on commit 7886cf0

Please sign in to comment.