Skip to content

Commit

Permalink
Add warnings/greetings, fixes #38
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jun 27, 2016
1 parent 3289f74 commit 162f239
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 9 deletions.
7 changes: 6 additions & 1 deletion README.md
Expand Up @@ -66,6 +66,9 @@ Created by mcmonkey4eva on behalf of the Citizens and Denizen teams.
- /sentinel targettime TIME - Sets the NPC's enemy target time limit.
- /sentinel speed - Sets the NPC's speed modifier.
- /sentinel targettime TIME - Sets the NPCs enemy target time limit.
- /sentinel greeting GREETING - Sets a greeting message for the NPC to say.
- /sentinel warning WARNING - Sets a warning message for the NPC to say.
- /sentinel greetrange RANGE - Sets how far a player can be from an NPC before they are greeted.

### Sentry user?

Expand All @@ -74,7 +77,9 @@ Type "/sentinel sentryimport" on a server running both Sentry and Sentinel to in
### Permissions
- sentinel.basic for the /sentinel command
- sentinel.admin to edit other player's Sentinel NPCs.
- Everything else is "sentinel.X" where "X" is the command name, EG "sentinel.info".
- sentinel.greet for commands: greeting, warning, greetrange
- sentinel.info for commands: info, stats, targets
- Everything else is "sentinel.X" where "X" is the command name, EG "sentinel.damage".

### Targets

Expand Down
45 changes: 37 additions & 8 deletions src/main/java/org/mcmonkey/sentinel/SentinelPlugin.java
Expand Up @@ -429,7 +429,7 @@ else if (arg0.equals("range") && sender.hasPermission("sentinel.range") && args.
sender.sendMessage(prefixGood + "Range set!");
}
else {
throw new NumberFormatException("Number out or range.");
throw new NumberFormatException("Number out of range.");
}
}
catch (NumberFormatException ex) {
Expand All @@ -445,7 +445,7 @@ else if (arg0.equals("damage") && sender.hasPermission("sentinel.damage") && arg
sender.sendMessage(prefixGood + "Damage set!");
}
else {
throw new NumberFormatException("Number out or range.");
throw new NumberFormatException("Number out of range.");
}
}
catch (NumberFormatException ex) {
Expand All @@ -461,14 +461,30 @@ else if (arg0.equals("speed") && sender.hasPermission("sentinel.speed") && args.
sender.sendMessage(prefixGood + "Speed set!");
}
else {
throw new NumberFormatException("Number out or range.");
throw new NumberFormatException("Number out of range.");
}
}
catch (NumberFormatException ex) {
sender.sendMessage(prefixBad + "Invalid speed number!");
}
return true;
}
else if (arg0.equals("greetrange") && sender.hasPermission("sentinel.speed") && args.length > 1) {
try {
Double d = Double.valueOf(args[1]);
if (d < 100) {
sentinel.greetRange = d;
sender.sendMessage(prefixGood + "Range set!");
}
else {
throw new NumberFormatException("Number out of range.");
}
}
catch (NumberFormatException ex) {
sender.sendMessage(prefixBad + "Invalid range number!");
}
return true;
}
else if (arg0.equals("armor") && sender.hasPermission("sentinel.armor") && args.length > 1) {
try {
Double d = Double.valueOf(args[1]);
Expand All @@ -477,7 +493,7 @@ else if (arg0.equals("armor") && sender.hasPermission("sentinel.armor") && args.
sender.sendMessage(prefixGood + "Armor set!");
}
else {
throw new NumberFormatException("Number out or range.");
throw new NumberFormatException("Number out of range.");
}
}
catch (NumberFormatException ex) {
Expand All @@ -493,7 +509,7 @@ else if (arg0.equals("health") && sender.hasPermission("sentinel.health") && arg
sender.sendMessage(prefixGood + "Health set!");
}
else {
throw new NumberFormatException("Number out or range.");
throw new NumberFormatException("Number out of range.");
}
}
catch (NumberFormatException ex) {
Expand All @@ -509,7 +525,7 @@ else if (arg0.equals("attackrate") && sender.hasPermission("sentinel.attackrate"
sender.sendMessage(prefixGood + "Attack rate set!");
}
else {
throw new NumberFormatException("Number out or range.");
throw new NumberFormatException("Number out of range.");
}
}
catch (NumberFormatException ex) {
Expand All @@ -525,7 +541,7 @@ else if (arg0.equals("healrate") && sender.hasPermission("sentinel.healrate") &&
sender.sendMessage(prefixGood + "Heal rate set!");
}
else {
throw new NumberFormatException("Number out or range.");
throw new NumberFormatException("Number out of range.");
}
}
catch (NumberFormatException ex) {
Expand All @@ -541,7 +557,7 @@ else if (arg0.equals("targettime") && sender.hasPermission("sentinel.targettime"
sender.sendMessage(prefixGood + "Target time set!");
}
else {
throw new NumberFormatException("Number out or range.");
throw new NumberFormatException("Number out of range.");
}
}
catch (NumberFormatException ex) {
Expand Down Expand Up @@ -712,6 +728,16 @@ else if (arg0.equals("respawn") && sender.hasPermission("sentinel.respawn")) {
sender.sendMessage(prefixGood + "Respawned!");
return true;
}
else if (arg0.equals("greeting") && sender.hasPermission("sentinel.greet")) {
sentinel.greetingText = SentinelUtilities.concatWithSpaces(args, 1);
sender.sendMessage(prefixGood + "Set!");
return true;
}
else if (arg0.equals("warning") && sender.hasPermission("sentinel.greet")) {
sentinel.warningText = SentinelUtilities.concatWithSpaces(args, 1);
sender.sendMessage(prefixGood + "Set!");
return true;
}
else if (arg0.equals("targets") && sender.hasPermission("sentinel.info")) {
sender.sendMessage(prefixGood + ChatColor.RESET + sentinel.getNPC().getFullName() + ColorBasic
+ ": owned by " + ChatColor.RESET + getOwner(sentinel.getNPC()));
Expand Down Expand Up @@ -793,6 +819,9 @@ else if (arg0.equals("stats") && sender.hasPermission("sentinel.info")) {
if (sender.hasPermission("sentinel.respawn")) sender.sendMessage(prefixGood + "/sentinel respawn - Respawns the NPC.");
if (sender.hasPermission("sentinel.targettime")) sender.sendMessage(prefixGood + "/sentinel targettime TIME - Sets the NPC's enemy target time limit.");
if (sender.hasPermission("sentinel.speed")) sender.sendMessage(prefixGood + "/sentinel speed SPEED - Sets the NPC's speed modifier.");
if (sender.hasPermission("sentinel.greet")) sender.sendMessage(prefixGood + "/sentinel greeting GREETING - Sets a greeting message for the NPC to say.");
if (sender.hasPermission("sentinel.greet")) sender.sendMessage(prefixGood + "/sentinel warning WARNING - Sets a warning message for the NPC to say.");
if (sender.hasPermission("sentinel.greet")) sender.sendMessage(prefixGood + "/sentinel greetrange RANGE - Sets how far a player can be from an NPC before they are greeted.");
if (sender.hasPermission("sentinel.info")) sender.sendMessage(prefixGood + "/sentinel info - Shows info on the current NPC.");
if (sender.hasPermission("sentinel.info")) sender.sendMessage(prefixGood + "/sentinel targets - Shows the targets of the current NPC.");
if (sender.hasPermission("sentinel.info")) sender.sendMessage(prefixGood + "/sentinel stats - Shows statistics about the current NPC.");
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/org/mcmonkey/sentinel/SentinelTrait.java
Expand Up @@ -3,10 +3,13 @@
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.ai.TargetType;
import net.citizensnpcs.api.ai.TeleportStuckAction;
import net.citizensnpcs.api.ai.speech.SpeechContext;
import net.citizensnpcs.api.ai.speech.VocalChord;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.trait.Inventory;
import net.citizensnpcs.api.trait.trait.Owner;
import net.citizensnpcs.npc.ai.speech.CitizensSpeechFactory;
import net.citizensnpcs.trait.waypoint.Waypoint;
import net.citizensnpcs.trait.waypoint.WaypointProvider;
import net.citizensnpcs.trait.waypoint.Waypoints;
Expand All @@ -22,6 +25,7 @@
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
Expand Down Expand Up @@ -180,6 +184,15 @@ public SentinelTrait() {
@Persist("speed")
public double speed = 1;

@Persist("warning_text")
public String warningText = "";

@Persist("greeting_text")
public String greetingText = "";

@Persist("greet_range")
public double greetRange = 10;

public LivingEntity chasing = null;

public UUID getGuarding() {
Expand Down Expand Up @@ -871,6 +884,8 @@ public boolean shouldTarget(LivingEntity entity) {

private HashSet<SentinelCurrentTarget> currentTargets = new HashSet<SentinelCurrentTarget>();

private HashSet<UUID> greetedAlready = new HashSet<UUID>();

public void addTarget(UUID id) {
SentinelCurrentTarget target = new SentinelCurrentTarget();
target.targetID = id;
Expand Down Expand Up @@ -1142,6 +1157,34 @@ public void onSpawn() {
}
}

public void sayTo(Player player, String message) {
SpeechContext sc = new SpeechContext(npc, message, player);
npc.getDefaultSpeechController().speak(sc, "chat");
}

@EventHandler
public void onPlayerMovesInRange(PlayerMoveEvent event) {
if (!npc.isSpawned()) {
return;
}
double dist = event.getTo().distanceSquared(getLivingEntity().getLocation());
boolean known = greetedAlready.contains(event.getPlayer().getUniqueId());
if (dist < greetRange && !known && canSee(event.getPlayer())) {
greetedAlready.add(event.getPlayer().getUniqueId());
boolean enemy = isTargeted(event.getPlayer()) && !isIgnored(event.getPlayer());
if (enemy && warningText != null && warningText.length() > 0) {
sayTo(event.getPlayer(), warningText);
}
else if (!enemy && greetingText != null && greetingText.length() > 0) {
sayTo(event.getPlayer(), greetingText);
}
}
else if (dist >= greetRange + 1 && known) {
greetedAlready.remove(event.getPlayer().getUniqueId());
// TODO: Farewell text perhaps?
}
}

public HashMap<UUID, Boolean> needsDropsClear = new HashMap<UUID, Boolean>();

@EventHandler(priority = EventPriority.HIGHEST)
Expand Down Expand Up @@ -1179,6 +1222,8 @@ public void whenSomethingDies(EntityDeathEvent event) {
}

public void onDeath() {
greetedAlready.clear();
currentTargets.clear();
if (respawnTime < 0) {
BukkitRunnable removeMe = new BukkitRunnable() {
@Override
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/mcmonkey/sentinel/SentinelUtilities.java
Expand Up @@ -32,4 +32,12 @@ public static double hangtime(double launchAngle, double vel, double deltaY, dou
}
return (a + Math.sqrt(a2)) / gravity;
}

public static String concatWithSpaces(String[] strs, int start) {
String temp = "";
for (int i = start; i < strs.length; i++) {
temp += strs[i] + " ";
}
return temp;
}
}
2 changes: 2 additions & 0 deletions src/main/resources/plugin.yml
Expand Up @@ -72,6 +72,8 @@ permissions:
sentinel.targettime: true
# /sentinel speed SPEED
sentinel.speed: true
# /sentinel greeting GREETING, sentinel warning WARNING, sentinel greetrange RANGE
sentinel.greet: true
# /sentinel info, /sentinel targets, /sentinel stats
sentinel.info: true
# Edit other people's NPCs
Expand Down

0 comments on commit 162f239

Please sign in to comment.