diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/config/Settings.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/config/Settings.java index 1ebe9310..9d90867b 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/config/Settings.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/config/Settings.java @@ -2,7 +2,6 @@ package com.hibiscusmc.hmccosmetics.config; import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin; import com.hibiscusmc.hmccosmetics.cosmetic.CosmeticSlot; -import com.hibiscusmc.hmccosmetics.util.MessagesUtil; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.util.Vector; import org.spongepowered.configurate.ConfigurationNode; @@ -37,6 +36,8 @@ public class Settings { private static final String HOOK_ITEMADDER_PATH = "itemsadder"; private static final String HOOK_RELOAD_CHANGE_PATH = "reload-on-change"; private static final String COSMETIC_EMOTE_CHECK_PATH = "emote-block-check"; + private static final String COSMETIC_EMOTE_DAMAGE_PATH = "emote-damage-leave"; + private static final String COSMETIC_EMOTE_INVINCIBLE_PATH = "emote-invincible"; private static final String COSMETIC_ADD_ENCHANTS_HELMET_PATH = "helmet-add-enchantments"; private static final String COSMETIC_ADD_ENCHANTS_CHESTPLATE_PATH = "chest-add-enchantments"; private static final String COSMETIC_ADD_ENCHANTS_LEGGINGS_PATH = "leggings-add-enchantments"; @@ -61,6 +62,8 @@ public class Settings { private static boolean addChestplateEnchants; private static boolean addLeggingEnchants; private static boolean addBootsEnchants; + private static boolean emoteDamageLeave; + private static boolean emoteInvincible; private static int lookDownPitch; private static int viewDistance; private static int tickPeriod; @@ -93,6 +96,8 @@ public class Settings { forcePermissionJoin = cosmeticSettings.node(FORCE_PERMISSION_JOIN_PATH).getBoolean(false); emoteDistance = cosmeticSettings.node(EMOTE_DISTANCE_PATH).getDouble(-3); cosmeticEmoteBlockCheck = cosmeticSettings.node(COSMETIC_EMOTE_CHECK_PATH).getBoolean(true); + emoteDamageLeave = cosmeticSettings.node(COSMETIC_EMOTE_DAMAGE_PATH).getBoolean(false); + emoteInvincible = cosmeticSettings.node(COSMETIC_EMOTE_INVINCIBLE_PATH).getBoolean(false); addHelmetEnchants = cosmeticSettings.node(COSMETIC_ADD_ENCHANTS_HELMET_PATH).getBoolean(false); addChestplateEnchants = cosmeticSettings.node(COSMETIC_ADD_ENCHANTS_CHESTPLATE_PATH).getBoolean(false); addLeggingEnchants = cosmeticSettings.node(COSMETIC_ADD_ENCHANTS_LEGGINGS_PATH).getBoolean(false); @@ -240,6 +245,14 @@ public class Settings { return cosmeticEmoteBlockCheck; } + public static boolean isEmoteDamageLeave() { + return emoteDamageLeave; + } + + public static boolean isEmoteInvincible() { + return emoteInvincible; + } + public static boolean getShouldAddEnchants(EquipmentSlot slot) { switch (slot) { case HEAD -> { diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerConnectionListener.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerConnectionListener.java index 66fde09a..65bbed6f 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerConnectionListener.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerConnectionListener.java @@ -5,6 +5,7 @@ import com.hibiscusmc.hmccosmetics.config.DatabaseSettings; import com.hibiscusmc.hmccosmetics.database.Database; import com.hibiscusmc.hmccosmetics.user.CosmeticUser; import com.hibiscusmc.hmccosmetics.user.CosmeticUsers; +import com.hibiscusmc.hmccosmetics.user.manager.UserEmoteManager; import com.hibiscusmc.hmccosmetics.util.MessagesUtil; import org.bukkit.Bukkit; import org.bukkit.entity.Entity; @@ -60,7 +61,7 @@ public class PlayerConnectionListener implements Listener { } if (user.isInWardrobe()) user.leaveWardrobe(); if (user.getUserEmoteManager().isPlayingEmote()) { - user.getUserEmoteManager().stopEmote(); + user.getUserEmoteManager().stopEmote(UserEmoteManager.StopEmoteReason.CONNECTION); event.getPlayer().setInvisible(false); } Database.save(user); diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerGameListener.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerGameListener.java index 6ea66878..d0d75f96 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerGameListener.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerGameListener.java @@ -20,6 +20,7 @@ import com.hibiscusmc.hmccosmetics.gui.Menu; import com.hibiscusmc.hmccosmetics.gui.Menus; import com.hibiscusmc.hmccosmetics.user.CosmeticUser; import com.hibiscusmc.hmccosmetics.user.CosmeticUsers; +import com.hibiscusmc.hmccosmetics.user.manager.UserEmoteManager; import com.hibiscusmc.hmccosmetics.util.InventoryUtils; import com.hibiscusmc.hmccosmetics.util.MessagesUtil; import org.bukkit.Bukkit; @@ -83,7 +84,7 @@ public class PlayerGameListener implements Listener { if (user == null) return; if (event.isSneaking()) { - user.getUserEmoteManager().stopEmote(); + user.getUserEmoteManager().stopEmote(UserEmoteManager.StopEmoteReason.SNEAK); } if (!event.isSneaking()) return; @@ -153,6 +154,26 @@ public class PlayerGameListener implements Listener { event.setCancelled(true); } + @EventHandler + public void onPlayerDamaged(EntityDamageEvent event) { + if (event.isCancelled()) return; + if (!(event.getEntity() instanceof Player)) return; + Player player = ((Player) event.getEntity()).getPlayer(); + CosmeticUser user = CosmeticUsers.getUser(player); + if (user == null) return; + if (user.getUserEmoteManager().isPlayingEmote()) { + if (Settings.isEmoteInvincible()) { + event.setCancelled(true); + } + if (Settings.isEmoteDamageLeave()) { + user.getUserEmoteManager().stopEmote(UserEmoteManager.StopEmoteReason.DAMAGE); + } + } + if (user.isInWardrobe()) { + user.leaveWardrobe(); + } + } + @EventHandler public void onPlayerLook(PlayerMoveEvent event) { if (event.isCancelled()) return; diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/manager/UserEmoteManager.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/manager/UserEmoteManager.java index 8e4cc462..028de85b 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/manager/UserEmoteManager.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/manager/UserEmoteManager.java @@ -1,8 +1,12 @@ package com.hibiscusmc.hmccosmetics.user.manager; +import com.hibiscusmc.hmccosmetics.api.PlayerCosmeticRemoveEvent; +import com.hibiscusmc.hmccosmetics.api.PlayerEmoteStartEvent; +import com.hibiscusmc.hmccosmetics.api.PlayerEmoteStopEvent; import com.hibiscusmc.hmccosmetics.cosmetic.types.CosmeticEmoteType; import com.hibiscusmc.hmccosmetics.user.CosmeticUser; import com.hibiscusmc.hmccosmetics.util.MessagesUtil; +import org.bukkit.Bukkit; import org.jetbrains.annotations.NotNull; public class UserEmoteManager { @@ -22,6 +26,13 @@ public class UserEmoteManager { public void playEmote(String animationId) { if (isPlayingEmote()) return; if (user.isInWardrobe()) return; + // API + PlayerEmoteStartEvent event = new PlayerEmoteStartEvent(user, animationId); + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; + } + // Internal try { model = new UserEmoteModel(user); model.playAnimation(animationId); @@ -35,8 +46,21 @@ public class UserEmoteManager { return model.isPlayingAnimation(); } - public void stopEmote() { + public void stopEmote(StopEmoteReason emoteReason) { if (!isPlayingEmote()) return; + // API + PlayerEmoteStopEvent event = new PlayerEmoteStopEvent(user, emoteReason); + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; + } + // Internal model.stopAnimation(); } + + public enum StopEmoteReason { + SNEAK, + DAMAGE, + CONNECTION + } } diff --git a/common/src/main/resources/config.yml b/common/src/main/resources/config.yml index 8824bde7..af2da0ad 100644 --- a/common/src/main/resources/config.yml +++ b/common/src/main/resources/config.yml @@ -24,7 +24,9 @@ cosmetic-settings: force-permission-join: true # Checks a player permission if they can have a cosmetic when they join the server. emote-distance: -3 # This shows how far away the camera should be while a player is doing an emote. Negative is behind player. - emote-block-check: true # If the server should check if the block is open (prevents players viewing through blocks) + emote-block-check: true # If the server should check if the block is open where the camera is placed (prevents players viewing through blocks) + emote-damage-leave: true # If the player should leave the emote when they take damage + emote-invincible: false # If the player should not take damage while doing an emote helmet-add-enchantments: false # If the plugin should keep enchants on helmets. This is useful as some enchantments are client side only. chest-add-enchantments: false # If the plugin should keep enchants on chestplate. This is useful as some enchantments are client side only.