From c73dc029d5bc48ea4dd919e1026c3ce8d6f38f9f Mon Sep 17 00:00:00 2001 From: Michael Wieland Date: Fri, 30 Mar 2018 20:34:52 +0200 Subject: [PATCH] Send message if mob killed by non-player (e.g. other mob), closes #2 --- README.md | 5 +- pom.xml | 2 +- .../selfcoders/talkingmobs/EventListener.java | 40 ++-- .../com/selfcoders/talkingmobs/Message.java | 174 ++++++++++++++---- .../selfcoders/talkingmobs/TalkingMobs.java | 12 +- src/main/resources/config.yml | 5 +- 6 files changed, 189 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index ac2943d..a01176d 100644 --- a/README.md +++ b/README.md @@ -47,11 +47,14 @@ The following event types are currently available and can be used for the type i * `attacked` - Mob has been attacked by a player * `idle` - Mob is idle, for example the mob is just standing/walking/running around (Currently unused) * `interacted` - Player interacted with the mob (Right click on mob) -* `killed` - Mob has been killed by a player +* `killed_player` - Mob has been killed by a player +* `killed_other` - Mob has been killed by something else (not a player) * `looking` - Mob is looking at the player (and player is looking at the mob) * `spawn` - Mob has been spawned (Mob spawner, egg or another plugin by default, but can be configured in config.yml) * `tamed` - Mob has been tamed +Additional to those event types, the `killed` event type can be used in the messages configuration to define a message for both types (player and other). + ## Configuration diff --git a/pom.xml b/pom.xml index 9b32b75..308810e 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ com.selfcoders talkingmobs - 1.3 + 1.4 jar TalkingMobs diff --git a/src/main/java/com/selfcoders/talkingmobs/EventListener.java b/src/main/java/com/selfcoders/talkingmobs/EventListener.java index e9188b2..5cbab63 100644 --- a/src/main/java/com/selfcoders/talkingmobs/EventListener.java +++ b/src/main/java/com/selfcoders/talkingmobs/EventListener.java @@ -15,6 +15,7 @@ import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.util.Vector; +import java.util.ArrayList; import java.util.List; /** @@ -42,7 +43,7 @@ public void onCreatureSpawn(CreatureSpawnEvent event) { List allowedSpawnReasons = plugin.getConfig().getStringList("events.spawned"); if (allowedSpawnReasons.contains(spawnReason.name())) { - message.sendMessage(entity, Message.EventType.spawned); + message.sendMessage(entity, Message.EventType.SPAWNED); } } @@ -56,21 +57,38 @@ public void onEntityAttacked(EntityDamageByEntityEvent event) { Entity entity = event.getEntity(); if (damager instanceof Player && !entity.isDead()) { - message.sendMessage(entity, (Player) damager, Message.EventType.attacked); + message.sendMessage(entity, (Player) damager, Message.EventType.ATTACKED); } } @EventHandler public void onEntityKilled(EntityDeathEvent event) { - if (!plugin.getConfig().getBoolean("events.killed")) { - return; - } - LivingEntity entity = event.getEntity(); Player killer = entity.getKiller(); - if (killer != null) { - message.sendMessage(entity, killer, Message.EventType.killed); + if (killer == null) { + if (!plugin.getConfig().getBoolean("events.killed_other")) { + return; + } + + List eventTypes = new ArrayList<>(); + + eventTypes.add(Message.EventType.KILLED_OTHER); + eventTypes.add(Message.EventType.KILLED); + + message.sendMessage(entity, Message.EventType.KILLED_OTHER, eventTypes); + } else { + // events.killed is used till version 1.3 and is now deprecated + if (!plugin.getConfig().getBoolean("events.killed_player") && !plugin.getConfig().getBoolean("events.killed")) { + return; + } + + List eventTypes = new ArrayList<>(); + + eventTypes.add(Message.EventType.KILLED_PLAYER); + eventTypes.add(Message.EventType.KILLED); + + message.sendMessage(entity, killer, Message.EventType.KILLED_PLAYER, eventTypes); } } @@ -80,7 +98,7 @@ public void onPlayerInteractEntity(PlayerInteractEntityEvent event) { return; } - message.sendMessage(event.getRightClicked(), event.getPlayer(), Message.EventType.interacted); + message.sendMessage(event.getRightClicked(), event.getPlayer(), Message.EventType.INTERACTED); } @EventHandler @@ -97,7 +115,7 @@ public void onEntityTame(EntityTameEvent event) { Player player = (Player) owner; - message.sendMessage(event.getEntity(), player, Message.EventType.tamed); + message.sendMessage(event.getEntity(), player, Message.EventType.TAMED); } @EventHandler @@ -133,7 +151,7 @@ public void onPlayerMove(PlayerMoveEvent event) { } private void onEntityFacingPlayer(LivingEntity entity, Player player) { - message.sendMessage(entity, player, Message.EventType.looking); + message.sendMessage(entity, player, Message.EventType.LOOKING); } /** diff --git a/src/main/java/com/selfcoders/talkingmobs/Message.java b/src/main/java/com/selfcoders/talkingmobs/Message.java index a3a53da..e6ed04a 100644 --- a/src/main/java/com/selfcoders/talkingmobs/Message.java +++ b/src/main/java/com/selfcoders/talkingmobs/Message.java @@ -26,31 +26,65 @@ public enum EventType { /** * Mob has been attacked by a player */ - attacked, + ATTACKED("attacked", true), /** * Mob is idle (Is just standing/walking/running around) -> Unused */ - idle, + IDLE("idle", true), /** * Player interacted with the mob (Right clicked on mob) */ - interacted, + INTERACTED("interacted", true), + /** + * Mob has been killed + */ + KILLED("killed", false), /** * Mob has been killed by a player */ - killed, + KILLED_PLAYER("killed_player", true), + /** + * Mob has been killed by something else (not a player) + */ + KILLED_OTHER("killed_other", true), /** * Mob is looking at the player (and player is looking at the mob) */ - looking, + LOOKING("looking", true), /** * Mob has been spawned (Mob spawner, egg or another plugin by default, but can be configured in config.yml) */ - spawned, + SPAWNED("spawned", true), /** * Mob has been tamed */ - tamed + TAMED("tamed", true); + + private String type; + private boolean toggleable; + + EventType(String type, boolean toggleable) { + this.type = type; + this.toggleable = toggleable; + } + + public String getType() { + return type; + } + + public boolean isToggleable() { + return toggleable; + } + + public static EventType fromString(String type) { + for (EventType eventType : values()) { + if (eventType.getType().equalsIgnoreCase(type)) { + return eventType; + } + } + + return null; + } } /** @@ -73,6 +107,29 @@ public void reloadConfig() { playersConfig.reload(); } + /** + * Get a list of messages configured for the specified mob and event type + * + * @param mob The entity of the mob for which the messages should be fetched + * @param eventType The event type of the messages + * @return A list of messages or null if there a none + */ + private List getMessages(Entity mob, EventType eventType) { + List messages; + String mobTypeName = mob.getType().name().toLowerCase(); + + messages = messagesConfig.getConfig().getStringList(mobTypeName + "." + eventType.getType()); + if (messages.size() == 0) { + messages = messagesConfig.getConfig().getStringList("default." + eventType.getType()); + + if (messages.size() == 0) { + return null; + } + } + + return messages; + } + /** * Get a random message for the specified mob and event type * @@ -81,38 +138,33 @@ public void reloadConfig() { * @return A string containing the message */ private String getMessage(Entity mob, EventType eventType) { - List messages; String mobTypeName = mob.getType().name().toLowerCase(); - messages = messagesConfig.getConfig().getStringList(mobTypeName + "." + eventType.name()); - if (messages.size() == 0) { - messages = messagesConfig.getConfig().getStringList("default." + eventType.name()); + List messages = getMessages(mob, eventType); + + if (messages == null) { + plugin.getLogger().log(Level.INFO, "No messages for event ''{0}'' of mob ''{1}'' defined!", new Object[]{eventType.getType(), mobTypeName}); + return null; } - if (messages.size() > 0) { - Random randomGenerator = new Random(); + Random randomGenerator = new Random(); - String message = plugin.getConfig().getString("messageFormat." + eventType.name()); + String message = plugin.getConfig().getString("messageFormat." + eventType.getType()); - if (message == null) { - message = plugin.getConfig().getString("messageFormat.default"); + if (message == null) { + message = plugin.getConfig().getString("messageFormat.default"); - if (message == null) { - message = "[&a%mobname%&r] %message%"; - plugin.getLogger().log(Level.INFO, "Message format for event type ''{0}'' not defined!", eventType.name()); - } + if (message == null) { + message = "[&a%mobname%&r] %message%"; + plugin.getLogger().log(Level.INFO, "Message format for event type ''{0}'' not defined!", eventType.getType()); } - - message = message.replaceAll("%message%", messages.get(randomGenerator.nextInt(messages.size()))); - message = message.replaceAll("%mobname%", mobTypeName); - message = message.replaceAll("%event%", eventType.name()); - - return message; - } else { - plugin.getLogger().log(Level.INFO, "No messages for event ''{0}'' of mob ''{1}'' defined!", new Object[]{eventType.name(), mobTypeName}); } - return null; + message = message.replaceAll("%message%", messages.get(randomGenerator.nextInt(messages.size()))); + message = message.replaceAll("%mobname%", mobTypeName); + message = message.replaceAll("%event%", eventType.getType()); + + return message; } /** @@ -138,7 +190,7 @@ public boolean isEnabled(Player player, EventType eventType) { return false; } - return playersConfig.getConfig().getBoolean(player.getName() + ".enabled." + eventType.name(), true); + return playersConfig.getConfig().getBoolean(player.getName() + ".enabled." + eventType.getType(), true); } /** @@ -152,7 +204,7 @@ public boolean isEnabled(Player player) { } private boolean isSpamming(Entity mob, Player player, EventType eventType) { - String key = String.valueOf(mob.getEntityId()) + "/" + String.valueOf(player.getEntityId()) + "/" + eventType.name(); + String key = String.valueOf(mob.getEntityId()) + "/" + String.valueOf(player.getEntityId()) + "/" + eventType.getType(); Long now = System.currentTimeMillis(); Long spamTimeout = plugin.getConfig().getLong("spam-timeout"); @@ -197,6 +249,27 @@ public void sendMessage(Entity mob, Player player, EventType eventType) { sendMessage(mob, player, eventType, message); } + /** + * Send a mob message to the player + * + * @param mob The mob which sends the message + * @param player The player which should receive the message + * @param originalEventType The original event type which triggered this message + * @param possibleEventTypes The event types of which to pick the first possible one for getting the message + */ + public void sendMessage(Entity mob, Player player, EventType originalEventType, List possibleEventTypes) { + for (EventType eventType : possibleEventTypes) { + if (getMessages(mob, eventType) == null) { + continue; + } + + String message = getMessage(mob, eventType); + + sendMessage(mob, player, originalEventType, message); + return; + } + } + /** * Send a mob message to all players * @@ -222,6 +295,37 @@ public void sendMessage(Entity mob, EventType eventType) { } } + /** + * Send a mob message to all players + * + * @param mob The mob which sends the message + * @param originalEventType The original event type which triggered this message + * @param possibleEventTypes The event types of which to pick the first possible one for getting the message + */ + public void sendMessage(Entity mob, EventType originalEventType, List possibleEventTypes) { + if (mob instanceof Player) { + return; + } + + if (!(mob instanceof LivingEntity)) { + return; + } + + for (EventType eventType : possibleEventTypes) { + if (getMessages(mob, eventType) == null) { + continue; + } + + String message = getMessage(mob, eventType); + + for (Player player : plugin.getServer().getOnlinePlayers()) { + sendMessage(mob, player, originalEventType, message); + } + + return; + } + } + /** * Send the given mob message to the player * @@ -271,7 +375,7 @@ private void sendMessage(Entity mob, Player player, EventType eventType, String * @param state The new state */ public void setEnabled(Player player, EventType eventType, Boolean state) { - playersConfig.getConfig().set(player.getName() + ".enabled." + eventType.name(), state); + playersConfig.getConfig().set(player.getName() + ".enabled." + eventType.getType(), state); playersConfig.getConfig().set(player.getName() + ".enabled.all", true); playersConfig.save(); @@ -287,7 +391,11 @@ public void setEnabled(Player player, Boolean state) { playersConfig.getConfig().set(player.getName() + ".enabled.all", state); for (EventType eventType : EventType.values()) { - playersConfig.getConfig().set(player.getName() + ".enabled." + eventType.name(), state); + if (!eventType.isToggleable()) { + continue; + } + + playersConfig.getConfig().set(player.getName() + ".enabled." + eventType.getType(), state); } playersConfig.save(); diff --git a/src/main/java/com/selfcoders/talkingmobs/TalkingMobs.java b/src/main/java/com/selfcoders/talkingmobs/TalkingMobs.java index 2cc0960..75686c5 100644 --- a/src/main/java/com/selfcoders/talkingmobs/TalkingMobs.java +++ b/src/main/java/com/selfcoders/talkingmobs/TalkingMobs.java @@ -118,8 +118,12 @@ private void toggle(CommandSender sender, String[] args) { Message.EventType eventType; try { - eventType = Message.EventType.valueOf(type); + eventType = Message.EventType.fromString(type); } catch (IllegalArgumentException exception) { + return; + } + + if (eventType == null || !eventType.isToggleable()) { sender.sendMessage(ChatColor.RED + "Invalid type: " + type); return; } @@ -139,7 +143,11 @@ private String messageTypesList() { eventTypes.add("all"); for (Message.EventType eventType : Message.EventType.values()) { - eventTypes.add(eventType.name()); + if (!eventType.isToggleable()) { + continue; + } + + eventTypes.add(eventType.getType()); } return "Message types: " + ChatColor.DARK_GREEN + StringUtils.join(eventTypes, ChatColor.WHITE + ", " + ChatColor.DARK_GREEN); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index b18b717..48ab61c 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -14,7 +14,8 @@ spam-timeout: 3000 events: attacked: true interacted: true - killed: true + killed_player: true + killed_other: true looking: true spawned: - CUSTOM @@ -32,6 +33,8 @@ messageFormat: # attacked: "[&a%mobname%&r] %message%" # interacted: "[&a%mobname%&r] %message%" # killed: "[&a%mobname%&r] &c%message%" +# killed_player: "[&a%mobname%&r] &c%message%" +# killed_other: "[&a%mobname%&r] &c%message%" # looking: "[&a%mobname%&r] &c%message%" # spawned: "[&a%mobname%&r] %message%" # tamed: "[&a%mobname%&r] %message%" \ No newline at end of file