mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2025-01-09 09:57:33 +01:00
Implement NPC limits
This commit is contained in:
parent
a226a7bff2
commit
751784010f
@ -1,13 +1,16 @@
|
||||
package net.citizensnpcs;
|
||||
|
||||
import net.citizensnpcs.Settings.Setting;
|
||||
import net.citizensnpcs.api.CitizensAPI;
|
||||
import net.citizensnpcs.api.event.NPCDamageByBlockEvent;
|
||||
import net.citizensnpcs.api.event.NPCDamageByEntityEvent;
|
||||
import net.citizensnpcs.api.event.NPCDamageEvent;
|
||||
import net.citizensnpcs.api.event.NPCLeftClickEvent;
|
||||
import net.citizensnpcs.api.event.NPCRightClickEvent;
|
||||
import net.citizensnpcs.api.event.PlayerCreateNPCEvent;
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
import net.citizensnpcs.api.npc.NPCRegistry;
|
||||
import net.citizensnpcs.api.trait.trait.Owner;
|
||||
import net.citizensnpcs.editor.Editor;
|
||||
import net.citizensnpcs.npc.entity.EntityHumanNPC;
|
||||
import net.citizensnpcs.trait.CurrentLocation;
|
||||
@ -138,6 +141,31 @@ public class EventListen implements Listener {
|
||||
// undesirable as player NPCs are not real players and confuse plugins.
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onPlayerCreateNPC(PlayerCreateNPCEvent event) {
|
||||
if (event.getCreator().hasPermission("citizens.admin.avoid-limits"))
|
||||
return;
|
||||
int limit = Setting.DEFAULT_NPC_LIMIT.asInt();
|
||||
int maxChecks = Setting.MAX_NPC_LIMIT_CHECKS.asInt();
|
||||
for (int i = maxChecks; i >= 0; i--) {
|
||||
if (!event.getCreator().hasPermission("citizens.npc.limit." + i))
|
||||
continue;
|
||||
limit = i;
|
||||
break;
|
||||
}
|
||||
if (limit < 0)
|
||||
return;
|
||||
int owned = 0;
|
||||
for (NPC npc : npcRegistry) {
|
||||
if (npc.getTrait(Owner.class).isOwnedBy(event.getCreator()))
|
||||
owned++;
|
||||
}
|
||||
if (limit >= owned + 1 || limit == 0) {
|
||||
event.setCancelled(true);
|
||||
event.setCancelReason(String.format("Over the NPC limit of %d.", limit));
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
|
||||
if (!npcRegistry.isNPC(event.getRightClicked()))
|
||||
|
@ -53,6 +53,7 @@ public class Settings {
|
||||
DEBUG_MODE("general.debug-mode", false),
|
||||
DEFAULT_LOOK_CLOSE("npc.default.look-close.enabled", false),
|
||||
DEFAULT_LOOK_CLOSE_RANGE("npc.default.look-close.range", 5),
|
||||
DEFAULT_NPC_LIMIT("npc.limits.default-limit", 10),
|
||||
DEFAULT_PATHFINDING_RANGE("npc.default.pathfinding.range", 25F),
|
||||
DEFAULT_RANDOM_TALKER("npc.default.random-talker", true),
|
||||
DEFAULT_REALISTIC_LOOKING("npc.default.realistic-looking", false),
|
||||
@ -67,6 +68,7 @@ public class Settings {
|
||||
value = list;
|
||||
}
|
||||
},
|
||||
MAX_NPC_LIMIT_CHECKS("npc.limits.max-permission-checks", 100),
|
||||
NPC_COST("economy.npc.cost", 100D),
|
||||
QUICK_SELECT("npc.selection.quick-select", false),
|
||||
SAVE_TASK_DELAY("storage.save-task.delay", 20 * 60 * 60),
|
||||
|
@ -201,6 +201,7 @@ public class NPCCommands {
|
||||
msg += " as a baby";
|
||||
}
|
||||
}
|
||||
|
||||
if (args.hasValueFlag("trait")) {
|
||||
msg += " with traits ";
|
||||
Iterable<String> parts = Splitter.on(",").trimResults().split(args.getFlag("trait"));
|
||||
|
Loading…
Reference in New Issue
Block a user