Skip to content

Commit

Permalink
Allow to configure which events should show a message
Browse files Browse the repository at this point in the history
  • Loading branch information
Programie committed Feb 1, 2018
1 parent a51e7b6 commit 3d6f70b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
22 changes: 20 additions & 2 deletions src/main/java/com/selfcoders/talkingmobs/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,43 @@
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;

import java.util.List;

/**
* Class providing all event listeners required for the plugin
*/
class EventListener implements Listener {
private final Message message;
private final TalkingMobs plugin;

/**
* Constructor of the class
*
* @param messageInstance The instance of the Message class
*/
EventListener(Message messageInstance) {
EventListener(Message messageInstance, TalkingMobs pluginInstance) {
message = messageInstance;
plugin = pluginInstance;
}

@EventHandler
public void onCreatureSpawn(CreatureSpawnEvent event) {
Entity entity = event.getEntity();
CreatureSpawnEvent.SpawnReason spawnReason = event.getSpawnReason();

if (spawnReason == CreatureSpawnEvent.SpawnReason.SPAWNER || spawnReason == CreatureSpawnEvent.SpawnReason.SPAWNER_EGG || spawnReason == CreatureSpawnEvent.SpawnReason.CUSTOM) {
List<String> allowedSpawnReasons = plugin.getConfig().getStringList("events.spawned");

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

@EventHandler
public void onEntityAttacked(EntityDamageByEntityEvent event) {
if (!plugin.getConfig().getBoolean("events.attacked")) {
return;
}

Entity damager = event.getDamager();
Entity entity = event.getEntity();

Expand All @@ -47,6 +57,10 @@ public void onEntityAttacked(EntityDamageByEntityEvent event) {

@EventHandler
public void onEntityKilled(EntityDeathEvent event) {
if (!plugin.getConfig().getBoolean("events.killed")) {
return;
}

LivingEntity entity = event.getEntity();
Player killer = entity.getKiller();

Expand All @@ -57,6 +71,10 @@ public void onEntityKilled(EntityDeathEvent event) {

@EventHandler
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
if (!plugin.getConfig().getBoolean("events.interacted")) {
return;
}

message.sendMessage(event.getRightClicked(), event.getPlayer(), Message.EventType.interacted);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/selfcoders/talkingmobs/TalkingMobs.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void onEnable() {
saveConfig();

PluginManager pluginManager = getServer().getPluginManager();
pluginManager.registerEvents(new EventListener(message), this);
pluginManager.registerEvents(new EventListener(message, this), this);
}

@Override
Expand Down
12 changes: 12 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
# Only players in this range to the talking mob will see the message
maxDistance: 0

# Define which events should show messages
# Everything except for "spawned" is a boolean (true or false).
# The "spawned" event expects a list of spawn reasons (see https://jd.bukkit.org/org/bukkit/event/entity/CreatureSpawnEvent.SpawnReason.html for possible spawn reasons).
events:
attacked: true
interacted: true
killed: true
spawned:
- CUSTOM
- SPAWNER
- SPAWNER_EGG

# How to format the messages
# %mobname% is replaced with the name of the talking mob
# %message% is replaced with the message
Expand Down

0 comments on commit 3d6f70b

Please sign in to comment.