Add feature villagers
This commit is contained in:
@@ -77,6 +77,9 @@ public class AnuraCore extends JavaPlugin {
|
||||
this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", new Teleports());
|
||||
sql.queryUpdate("DELETE FROM coreServers WHERE name = '" + this.getConfig().getString("server-name") + "'");
|
||||
sql.queryUpdate("INSERT INTO coreServers (name, maxPlayers) VALUES ('" + this.getConfig().getString("server-name") + "','" + this.getServer().getMaxPlayers() + "')");
|
||||
for (World w : Bukkit.getWorlds()) {
|
||||
AnuraVillager.addVillagers(w.getEntities());
|
||||
}
|
||||
} else {
|
||||
Bukkit.getPluginManager().disablePlugin(this);
|
||||
}
|
||||
|
||||
148
src/de/anura/core/AnuraVillager.java
Normal file
148
src/de/anura/core/AnuraVillager.java
Normal file
@@ -0,0 +1,148 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,10 @@ public class Features implements Listener {
|
||||
}
|
||||
private final int featureId;
|
||||
private static final HashMap<Integer, Feature> BY_ID = new HashMap<>();
|
||||
|
||||
public int getId() {
|
||||
return featureId;
|
||||
}
|
||||
|
||||
public static Feature getById(Integer id) {
|
||||
return BY_ID.get(id);
|
||||
|
||||
@@ -74,6 +74,7 @@ public class Setup {
|
||||
new DropItem(Core.getMainClass());
|
||||
new EntityChangeBlock(Core.getMainClass());
|
||||
new BlockFade(Core.getMainClass());
|
||||
new VillagerEvents(Core.getMainClass());
|
||||
}
|
||||
|
||||
public static void setupClasses() {
|
||||
@@ -164,6 +165,7 @@ public class Setup {
|
||||
AnuraCore.getInstance().signs.updateServerSigns();
|
||||
}
|
||||
}, 20 * 5, 20 * 5);
|
||||
Bukkit.getScheduler().scheduleSyncRepeatingTask(Core.getMainClass(), new VillagerTask(), 20, 20);
|
||||
} catch (Throwable e) {
|
||||
Errors.reportException(e);
|
||||
}
|
||||
@@ -207,6 +209,7 @@ public class Setup {
|
||||
Core.getMainClass().getCommand("bug").setExecutor(pc);
|
||||
Core.getMainClass().getCommand("warplist").setExecutor(tc);
|
||||
Core.getMainClass().getCommand("addInvItems").setExecutor(oc);
|
||||
Core.getMainClass().getCommand("villager").setExecutor(ac);
|
||||
}
|
||||
|
||||
private static void setupInventories() {
|
||||
|
||||
11
src/de/anura/core/VillagerTask.java
Normal file
11
src/de/anura/core/VillagerTask.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package de.anura.core;
|
||||
|
||||
public class VillagerTask implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for (AnuraVillager av : AnuraVillager.villagers.values()) {
|
||||
av.tp();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,9 @@ package de.anura.core.commands;
|
||||
import de.anura.core.API.Core;
|
||||
import de.anura.core.API.Errors;
|
||||
import de.anura.core.AnuraCore;
|
||||
import de.anura.core.AnuraVillager;
|
||||
import de.anura.core.AnuraVillager.VillagerType;
|
||||
import de.anura.core.Features.Feature;
|
||||
import de.anura.core.LanguageSupport;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -13,7 +16,9 @@ import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.entity.Arrow;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Villager;
|
||||
|
||||
public class AdminCommands implements CommandExecutor {
|
||||
|
||||
@@ -88,6 +93,45 @@ public class AdminCommands implements CommandExecutor {
|
||||
Core.statusMsg(P, "spawn_set", true);
|
||||
return true;
|
||||
}
|
||||
} else if (cmd.getName().equalsIgnoreCase("villager")) {
|
||||
if (P == null) {
|
||||
sender.sendMessage("Du bist kein Spieler!");
|
||||
return true;
|
||||
}
|
||||
if (!(P.hasPermission("core.commands.villager"))) {
|
||||
Core.statusMsg(P, "no_perms", false);
|
||||
return true;
|
||||
}
|
||||
if (args.length < 1) return false;
|
||||
if (args[0].equalsIgnoreCase("create")) {
|
||||
if (args.length < 2) return false;
|
||||
Feature f;
|
||||
try {
|
||||
f = Feature.valueOf(args[1].toUpperCase());
|
||||
} catch(IllegalArgumentException e) {
|
||||
return false;
|
||||
}
|
||||
if (f == null) return false;
|
||||
if (AnuraVillager.getByType(VillagerType.getByFeature(f)) != null) {
|
||||
P.sendMessage("Es existiert bereits ein Villager diesen Types!");
|
||||
return true;
|
||||
}
|
||||
new AnuraVillager(VillagerType.getByFeature(f)).spawn(P.getLocation());
|
||||
P.sendMessage("Villager erfolgreich gespawnt!");
|
||||
} else if (args[0].equalsIgnoreCase("remove")) {
|
||||
for (Entity e : P.getNearbyEntities(1, 1, 1)) {
|
||||
if (e instanceof Villager) {
|
||||
Villager v = (Villager) e;
|
||||
if (AnuraVillager.getByEntity(v) == null) continue;
|
||||
AnuraVillager.getByEntity(v).remove();
|
||||
v.remove();
|
||||
P.sendMessage("Villager erfolgreich entfernt!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
P.sendMessage("Kein Anura-Villager gefunden!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch(Throwable e) {
|
||||
Errors.reportException(e);
|
||||
|
||||
64
src/de/anura/core/events/VillagerEvents.java
Normal file
64
src/de/anura/core/events/VillagerEvents.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package de.anura.core.events;
|
||||
|
||||
import de.anura.core.API.Core;
|
||||
import de.anura.core.AnuraCore;
|
||||
import de.anura.core.AnuraVillager;
|
||||
import de.anura.core.Features;
|
||||
import de.anura.core.Features.Feature;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Villager;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntityDamageEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
||||
import org.bukkit.event.world.ChunkLoadEvent;
|
||||
import org.bukkit.event.world.ChunkUnloadEvent;
|
||||
|
||||
public class VillagerEvents implements Listener {
|
||||
|
||||
public VillagerEvents(AnuraCore plugin) {
|
||||
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onClick(PlayerInteractEntityEvent e) {
|
||||
Entity e1 = e.getRightClicked();
|
||||
if (!e1.getType().equals(EntityType.VILLAGER) || !e1.isValid()) return;
|
||||
Villager v = (Villager) e1;
|
||||
if (AnuraVillager.getByEntity(v) == null) return;
|
||||
AnuraVillager av = AnuraVillager.getByEntity(v);
|
||||
Feature f = av.getType().getFeature();
|
||||
Features fs = Core.getMainClass().getFeatures();
|
||||
Player P = e.getPlayer();
|
||||
if (fs.hasFeature(P, f)) {
|
||||
Core.statusMsg(P, "already_has_feature", Boolean.FALSE);
|
||||
return;
|
||||
}
|
||||
Core.getMySql().queryUpdate("INSERT INTO coreFeatures (playerId, featureId) VALUES ((SELECT id FROM players WHERE uuid='" + P.getUniqueId().toString() + "'),'"+f.getId()+"');");
|
||||
Core.statusMsg(P, "feature_unlocked_" + f.name().toLowerCase(), Boolean.TRUE);
|
||||
Core.updateFeatures(P);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onDamage(EntityDamageEvent e) {
|
||||
Entity en = e.getEntity();
|
||||
if (en.isValid() && e.getEntityType().equals(EntityType.VILLAGER)) {
|
||||
Villager ven = (Villager) en;
|
||||
if (AnuraVillager.getByEntity(ven) != null) {
|
||||
e.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onLoad(ChunkLoadEvent e) {
|
||||
AnuraVillager.addVillagers(e.getChunk().getEntities());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onUnload(ChunkUnloadEvent e) {
|
||||
AnuraVillager.removeVillagers(e.getChunk().getEntities());
|
||||
}
|
||||
}
|
||||
@@ -96,4 +96,7 @@ commands:
|
||||
usage: /<command>
|
||||
addInvItems:
|
||||
description: Add inv items
|
||||
usage: /<command> [player]
|
||||
usage: /<command> [player]
|
||||
villager:
|
||||
description: Anura-Villager commands
|
||||
usage: /<command> (remove)|(create double_jump|boat)
|
||||
Reference in New Issue
Block a user