Skip to content

Commit

Permalink
Send message if mob killed by non-player (e.g. other mob), closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Programie committed Mar 30, 2018
1 parent 750366d commit c73dc02
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 49 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>com.selfcoders</groupId>
<artifactId>talkingmobs</artifactId>
<version>1.3</version>
<version>1.4</version>
<packaging>jar</packaging>

<name>TalkingMobs</name>
Expand Down
40 changes: 29 additions & 11 deletions src/main/java/com/selfcoders/talkingmobs/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.util.Vector;

import java.util.ArrayList;
import java.util.List;

/**
Expand Down Expand Up @@ -42,7 +43,7 @@ public void onCreatureSpawn(CreatureSpawnEvent event) {
List<String> allowedSpawnReasons = plugin.getConfig().getStringList("events.spawned");

if (allowedSpawnReasons.contains(spawnReason.name())) {
message.sendMessage(entity, Message.EventType.spawned);
message.sendMessage(entity, Message.EventType.SPAWNED);
}
}

Expand All @@ -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<Message.EventType> 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<Message.EventType> eventTypes = new ArrayList<>();

eventTypes.add(Message.EventType.KILLED_PLAYER);
eventTypes.add(Message.EventType.KILLED);

message.sendMessage(entity, killer, Message.EventType.KILLED_PLAYER, eventTypes);
}
}

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand Down
174 changes: 141 additions & 33 deletions src/main/java/com/selfcoders/talkingmobs/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/**
Expand All @@ -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<String> getMessages(Entity mob, EventType eventType) {
List<String> 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
*
Expand All @@ -81,38 +138,33 @@ public void reloadConfig() {
* @return A string containing the message
*/
private String getMessage(Entity mob, EventType eventType) {
List<String> 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<String> 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;
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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");

Expand Down Expand Up @@ -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<EventType> 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
*
Expand All @@ -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<EventType> 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
*
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
Loading

0 comments on commit c73dc02

Please sign in to comment.