Skip to content
Permalink
Browse files

Respawn system

  • Loading branch information...
mcmonkey4eva committed Mar 29, 2016
1 parent d29bdef commit 1731630dd9d673da8cd37700f4ac3550120f9419
@@ -28,7 +28,7 @@ Created by mcmonkey4eva on behalf of the Citizens and Denizen teams.
- Examples:
- To make your NPC attack sword wielders, use `/sentinel addtarget helditem:.*sword`

### Supported things
### Some random supported things

- View `/sentinel addtarget` to view a list of currently supported targets. This includes all entity types and some special options such "NPCs" or "OWNER" (There are several more!)
- Weapons:
@@ -40,6 +40,7 @@ Created by mcmonkey4eva on behalf of the Citizens and Denizen teams.
- Nether star (strikes lightning!)
- Spectral arrow (makes the target glow, without damaging it.)
- (To make a target glow ++ damage it, equip a bow + arm it with spectral arrows!)
- Respawning can be set to "-1" to cause the NPC to delete itself on death, or "0" to prevent respawn.

### TODO

@@ -54,7 +55,6 @@ Created by mcmonkey4eva on behalf of the Citizens and Denizen teams.
- Other:
- old Sentry saves importer?
- Maximum chase range (waiting on Citizens2 issue 718)
- Automated respawn or removal on death

**Future**:

@@ -11,7 +11,10 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.HashMap;
import java.util.HashSet;
@@ -48,6 +51,20 @@ public void onEnable() {
if (getConfig().getInt("config version", 0) != 1) {
getLogger().warning("Outdated Sentinel config - please delete it to regenerate it!");
}
BukkitRunnable postLoad = new BukkitRunnable() {
@Override
public void run() {
for (NPC npc: CitizensAPI.getNPCRegistry()) {
if (!npc.isSpawned() && npc.hasTrait(SentinelTrait.class)) {
SentinelTrait sentinel = npc.getTrait(SentinelTrait.class);
if (sentinel.respawnTime > 0) {
npc.spawn(npc.getStoredLocation());
}
}
}
}
};
postLoad.runTaskLater(this, 40);
tickRate = getConfig().getInt("update rate", 10);
getLogger().info("Sentinel loaded!");
}
@@ -382,6 +399,17 @@ else if (arg0.equals("healrate") && sender.hasPermission("sentinel.healrate") &&
}
return true;
}
else if (arg0.equals("respawntime") && sender.hasPermission("sentinel.respawntime") && args.length > 1) {
try {
long d = Long.valueOf(args[1]);
sentinel.respawnTime = d;
sender.sendMessage(prefixGood + "Respawn time set!");
}
catch (NumberFormatException ex) {
sender.sendMessage(prefixBad + "Invalid time number!");
}
return true;
}
else if (arg0.equals("invincible") && sender.hasPermission("sentinel.invincible")) {
sentinel.setInvincible(!sentinel.invincible);
if (sentinel.invincible) {
@@ -474,6 +502,7 @@ else if (arg0.equals("info") && sender.hasPermission("sentinel.info")) {
sender.sendMessage(prefixGood + "Range: " + ChatColor.AQUA + sentinel.range);
sender.sendMessage(prefixGood + "Attack Rate: " + ChatColor.AQUA + sentinel.attackRate);
sender.sendMessage(prefixGood + "Heal Rate: " + ChatColor.AQUA + sentinel.healRate);
sender.sendMessage(prefixGood + "Respawn Time: " + ChatColor.AQUA + sentinel.respawnTime);
sender.sendMessage(prefixGood + "Invincibility Enabled: " + ChatColor.AQUA + sentinel.invincible);
sender.sendMessage(prefixGood + "Fightback Enabled: " + ChatColor.AQUA + sentinel.fightback);
sender.sendMessage(prefixGood + "Ranged Chasing Enabled: " + ChatColor.AQUA + sentinel.rangedChase);
@@ -505,6 +534,7 @@ else if (arg0.equals("stats") && sender.hasPermission("sentinel.info")) {
if (sender.hasPermission("sentinel.health")) sender.sendMessage(prefixGood + "/sentinel health HEALTH - Sets the NPC's health level.");
if (sender.hasPermission("sentinel.attackrate")) sender.sendMessage(prefixGood + "/sentinel attackrate RATE - Changes the rate at which the NPC attacks, in ticks.");
if (sender.hasPermission("sentinel.healrate")) sender.sendMessage(prefixGood + "/sentinel healrate RATE - Changes the rate at which the NPC heals, in ticks.");
if (sender.hasPermission("sentinel.healrate")) sender.sendMessage(prefixGood + "/sentinel respawntime TIME - Changes the time it takes for the NPC to respawn, in ticks.");
if (sender.hasPermission("sentinel.guard")) sender.sendMessage(prefixGood + "/sentinel guard [PLAYERNAME] - Makes the NPC guard a specific player. Don't specify a player to stop guarding.");
if (sender.hasPermission("sentinel.invincible")) sender.sendMessage(prefixGood + "/sentinel invincible - Toggles whether the NPC is invincible.");
if (sender.hasPermission("sentinel.fightback")) sender.sendMessage(prefixGood + "/sentinel fightback - Toggles whether the NPC will fight back.");
@@ -137,6 +137,9 @@ public SentinelTrait() {
@Persist("needsAmmo")
public boolean needsAmmo = false;

@Persist("respawnTime")
public long respawnTime = 100;

public UUID getGuarding() {
if (guardingLower == 0 && guardingUpper == 0) {
return null;
@@ -224,8 +227,8 @@ public void whenAnEnemyDies(EntityDeathEvent event) {
public void onAttach() {
FileConfiguration config = SentinelPlugin.instance.getConfig();
attackRate = config.getInt("sentinel defaults.attack rate", 30);
healRate
= config.getInt("sentinel defaults.heal rate", 30);
healRate = config.getInt("sentinel defaults.heal rate", 30);
respawnTime = config.getInt("sentinel defaults.respawn time", 100);
rangedChase = config.getBoolean("sentinel defaults.ranged chase target", false);
closeChase = config.getBoolean("sentinel defaults.close chase target", true);
armor = config.getDouble("sentinel defaults.armor", -1);
@@ -913,11 +916,47 @@ public void run() {
}
}

public BukkitRunnable respawnMe;

@Override
public void onSpawn() {
stats_timesSpawned++;
setHealth(health);
setInvincible(invincible);
if (respawnMe != null) {
respawnMe.cancel();
respawnMe = null;
}
}

@EventHandler
public void whenSomethingDies(EntityDeathEvent event) {
if (!CitizensAPI.getNPCRegistry().isNPC(event.getEntity())
|| !CitizensAPI.getNPCRegistry().getNPC(event.getEntity()).getUniqueId().equals(npc.getUniqueId())) {
return;
}
onDeath();
}

public void onDeath() {
if (respawnTime < 0) {
BukkitRunnable removeMe = new BukkitRunnable() {
@Override
public void run() {
npc.destroy();
}
};
removeMe.runTaskLater(SentinelPlugin.instance, 1);
}
else if (respawnTime > 0) {
respawnMe = new BukkitRunnable() {
@Override
public void run() {
npc.spawn(npc.getStoredLocation());
}
};
respawnMe.runTaskLater(SentinelPlugin.instance, respawnTime);
}
}

@Override
@@ -22,6 +22,8 @@ sentinel defaults:
fightback: true
# Whether this NPC needs ammo to fight with.
needs ammo: false
# Default respawn time, in ticks.
respawn time: 100

random:
# What sound to play when the "spectral" attack is used.
@@ -37,6 +37,8 @@ permissions:
sentinel.attackrate: true
# /sentinel healrate RATE
sentinel.healrate: true
# /sentinel respawntime TIME
sentinel.respawntime: true
# /sentinel guard PLAYER
sentinel.guard: true
# /sentinel invincible

0 comments on commit 1731630

Please sign in to comment.
You can’t perform that action at this time.