From 7a5ad90524782d85e3e11b29b96cd17abd46f966 Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Mon, 27 Jan 2020 19:50:22 +0100 Subject: [PATCH] Add ExternalMobProvider API --- .../dungeonsxl/api/{ => mob}/DungeonMob.java | 2 +- .../api/mob/ExternalMobProvider.java | 77 +++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) rename api/src/main/java/de/erethon/dungeonsxl/api/{ => mob}/DungeonMob.java (97%) create mode 100644 api/src/main/java/de/erethon/dungeonsxl/api/mob/ExternalMobProvider.java diff --git a/api/src/main/java/de/erethon/dungeonsxl/api/DungeonMob.java b/api/src/main/java/de/erethon/dungeonsxl/api/mob/DungeonMob.java similarity index 97% rename from api/src/main/java/de/erethon/dungeonsxl/api/DungeonMob.java rename to api/src/main/java/de/erethon/dungeonsxl/api/mob/DungeonMob.java index 288f0650..62e01f8e 100644 --- a/api/src/main/java/de/erethon/dungeonsxl/api/DungeonMob.java +++ b/api/src/main/java/de/erethon/dungeonsxl/api/mob/DungeonMob.java @@ -12,7 +12,7 @@ * You should have received a copy of the GNU Lesser General Public License along with * this program. If not, see . */ -package de.erethon.dungeonsxl.api; +package de.erethon.dungeonsxl.api.mob; import de.erethon.caliburn.mob.ExMob; import org.bukkit.entity.LivingEntity; diff --git a/api/src/main/java/de/erethon/dungeonsxl/api/mob/ExternalMobProvider.java b/api/src/main/java/de/erethon/dungeonsxl/api/mob/ExternalMobProvider.java new file mode 100644 index 00000000..3a3a7837 --- /dev/null +++ b/api/src/main/java/de/erethon/dungeonsxl/api/mob/ExternalMobProvider.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2014-2020 Daniel Saukel + * + * This library is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNULesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along with + * this program. If not, see . + */ +package de.erethon.dungeonsxl.api.mob; + +import org.bukkit.Bukkit; +import org.bukkit.Location; + +/** + * Other plugins / libraries that can handle and spawn mobs. + * + * @author Daniel Saukel + */ +public interface ExternalMobProvider { + + /** + * Returns the identifier used on mob signs to spawn mobs from this provider. + * + * @return the identifier used on mob signs to spawn mobs from this provider + */ + String getIdentifier(); + + /** + * Returns the raw console spawn command of the provider. + *

+ * This method is necessary for the default implementation of {@link #getCommand(String, String, double, double, double)}. + * + * @return the raw console spawn command of the provider + */ + String getRawCommand(); + + /** + * Returns the console spawn command of the provider with values replaced to spawn the mob represented by the given String. + *

+ * The default implementation uses %mob%, %world%, %x%, %y% and %z% as placeholders and alternatively %block_x% etc. if values without decimals are needed. + *

+ * This method is used in the default implementation of {@link #summon(String, org.bukkit.Location)}. + * + * @param mob the mob identifier + * @param world the game world + * @param x the x coordinate + * @param y the y coordinate + * @param z the z coordinate + * @return the command with replaced variables + */ + default String getCommand(String mob, String world, double x, double y, double z) { + return getRawCommand().replace("%mob%", mob).replace("%world%", world) + .replace("%x%", String.valueOf(x)).replace("%y%", String.valueOf(y)).replace("%z%", String.valueOf(z)) + .replace("%block_x%", String.valueOf(Location.locToBlock(x))) + .replace("%block_y%", String.valueOf(Location.locToBlock(y))) + .replace("%block_z%", String.valueOf(Location.locToBlock(z))); + } + + /** + * Summons the mob. + *

+ * The default implementation requires {@link #getCommand(String, String, double, double, double)} to be implemented. + * + * @param mob the mob identifier + * @param location the location where the mob will be spawned + */ + default void summon(String mob, Location location) { + Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), getCommand(mob, location.getWorld().getName(), location.getX(), location.getY(), location.getZ())); + } + +}