149 lines
4.5 KiB
Java
149 lines
4.5 KiB
Java
package de.anura.core;
|
|
|
|
import de.anura.core.Features.Feature;
|
|
import java.util.Arrays;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import org.bukkit.ChatColor;
|
|
import org.bukkit.Location;
|
|
import org.bukkit.entity.Entity;
|
|
import org.bukkit.entity.EntityType;
|
|
import org.bukkit.potion.PotionEffect;
|
|
import org.bukkit.potion.PotionEffectType;
|
|
import org.bukkit.entity.Villager;
|
|
import org.bukkit.entity.Villager.Profession;
|
|
|
|
public class AnuraVillager {
|
|
|
|
private final VillagerType type;
|
|
private Villager villager;
|
|
private Location loc;
|
|
|
|
public AnuraVillager(VillagerType type) {
|
|
this.type = type;
|
|
villagers.put(type, this);
|
|
}
|
|
|
|
public AnuraVillager(VillagerType type, Villager villager) {
|
|
this(type);
|
|
setVillager(villager);
|
|
loc = villager.getLocation();
|
|
}
|
|
|
|
public void spawn(Location loc) {
|
|
Villager v = buildVillager(loc, type.getName());
|
|
setVillager(v);
|
|
this.loc = loc;
|
|
}
|
|
|
|
private void setVillager(Villager v) {
|
|
villager = v;
|
|
byEntity.put(v, this);
|
|
}
|
|
|
|
private Villager buildVillager(Location loc, String name) {
|
|
Villager vi = (Villager) loc.getWorld().spawn(loc, Villager.class);
|
|
vi.setProfession(Profession.PRIEST);
|
|
vi.setBaby();
|
|
vi.setAgeLock(true);
|
|
vi.setBreed(false);
|
|
vi.setCanPickupItems(false);
|
|
vi.setCustomName(ChatColor.GOLD + name);
|
|
vi.setCustomNameVisible(true);
|
|
vi.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 999999999, 999));
|
|
return vi;
|
|
}
|
|
|
|
public void remove() {
|
|
byEntity.remove(villager);
|
|
villagers.remove(type);
|
|
}
|
|
|
|
public VillagerType getType() {
|
|
return type;
|
|
}
|
|
|
|
public void tp() {
|
|
villager.teleport(loc);
|
|
}
|
|
|
|
public static void addVillagers(Entity[] entities) {
|
|
addVillagers(Arrays.asList(entities));
|
|
}
|
|
|
|
public static void addVillagers(List<Entity> entities) {
|
|
for (Entity e : entities) {
|
|
if (e.isValid() && e.getType().equals(EntityType.VILLAGER)) {
|
|
Villager vi = (Villager) e;
|
|
if (vi.getCustomName() != null && VillagerType.getByName(vi.getCustomName()) != null) {
|
|
new AnuraVillager(VillagerType.getByName(vi.getCustomName()), vi);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void removeVillagers(Entity[] entities) {
|
|
for (Entity e : entities) {
|
|
if (e.isValid() && e.getType().equals(EntityType.VILLAGER)) {
|
|
Villager vi = (Villager) e;
|
|
if (vi.getCustomName() != null && VillagerType.getByName(vi.getCustomName()) != null) {
|
|
AnuraVillager av = getByEntity(vi);
|
|
if (av == null) continue;
|
|
av.remove();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static AnuraVillager getByEntity(Villager v) {
|
|
return byEntity.get(v);
|
|
}
|
|
|
|
public static AnuraVillager getByType(VillagerType type) {
|
|
return villagers.get(type);
|
|
}
|
|
|
|
private static final HashMap<Villager, AnuraVillager> byEntity = new HashMap<>();
|
|
public static final HashMap<VillagerType, AnuraVillager> villagers = new HashMap<>();
|
|
|
|
|
|
public enum VillagerType {
|
|
DOUBLE_JUMP(Feature.DOUBLE_JUMP, "Double jump"),
|
|
AUTO_BOAT(Feature.BOAT, "AutoBoat");
|
|
|
|
private VillagerType(Feature feature, String name) {
|
|
this.feature = feature;
|
|
this.name = name;
|
|
}
|
|
|
|
private final Feature feature;
|
|
private final String name;
|
|
|
|
public String getName() {
|
|
return name;
|
|
}
|
|
|
|
public Feature getFeature() {
|
|
return feature;
|
|
}
|
|
|
|
private final static HashMap<Feature, VillagerType> BY_FEATURE = new HashMap<>();
|
|
private final static HashMap<String, VillagerType> BY_NAME = new HashMap<>();
|
|
|
|
static {
|
|
for (VillagerType type : values()) {
|
|
BY_FEATURE.put(type.getFeature(), type);
|
|
BY_NAME.put(type.getName(), type);
|
|
}
|
|
}
|
|
|
|
public static VillagerType getByFeature(Feature feature) {
|
|
return BY_FEATURE.get(feature);
|
|
}
|
|
|
|
public static VillagerType getByName(String name) {
|
|
return BY_NAME.containsKey(name) ? BY_NAME.get(name) : BY_NAME.get(name.substring(2));
|
|
}
|
|
}
|
|
}
|