mirror of
https://gitlab.com/phoenix-dvpmt/mmocore.git
synced 2025-01-22 09:31:22 +01:00
First Draft Spawn Point.
This commit is contained in:
parent
65d18509d0
commit
a2e359d411
@ -80,6 +80,8 @@ public class MMOCore extends JavaPlugin {
|
||||
public final MMOLoadManager loadManager = new MMOLoadManager();
|
||||
public final RestrictionManager restrictionManager = new RestrictionManager();
|
||||
public final SkillTreeManager skillTreeManager = new SkillTreeManager();
|
||||
|
||||
public final SpawnPointManager spawnPointManager = new SpawnPointManager();
|
||||
public final StatManager statManager = new StatManager();
|
||||
public final GuildDataManager nativeGuildManager = new YAMLGuildDataManager();
|
||||
public final PlayerDataManager playerDataManager = new PlayerDataManager(this);
|
||||
|
@ -11,6 +11,10 @@ import net.Indyuce.mmocore.api.quest.trigger.*;
|
||||
import net.Indyuce.mmocore.experience.source.*;
|
||||
import net.Indyuce.mmocore.loot.chest.condition.*;
|
||||
import net.Indyuce.mmocore.loot.droptable.dropitem.*;
|
||||
import net.Indyuce.mmocore.spawnpoint.def.DefaultSpawnOption;
|
||||
import net.Indyuce.mmocore.spawnpoint.def.GlobalSpawnOption;
|
||||
import net.Indyuce.mmocore.spawnpoint.def.LastSpawnOption;
|
||||
import net.Indyuce.mmocore.spawnpoint.def.LocationSpawnOption;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
public class DefaultMMOLoader extends MMOLoader {
|
||||
@ -204,4 +208,15 @@ public class DefaultMMOLoader extends MMOLoader {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DefaultSpawnOption loadDefaultSpawnOption(MMOLineConfig config) {
|
||||
if (config.getKey().equals("global"))
|
||||
return new GlobalSpawnOption();
|
||||
if (config.getKey().equals("last"))
|
||||
return new LastSpawnOption();
|
||||
if (config.getKey().equals("location"))
|
||||
return new LocationSpawnOption(config);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import net.Indyuce.mmocore.experience.source.type.ExperienceSource;
|
||||
import net.Indyuce.mmocore.loot.chest.condition.Condition;
|
||||
import net.Indyuce.mmocore.loot.droptable.dropitem.DropItem;
|
||||
import net.Indyuce.mmocore.api.block.BlockType;
|
||||
import net.Indyuce.mmocore.spawnpoint.def.DefaultSpawnOption;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
/**
|
||||
@ -40,4 +41,9 @@ public class MMOLoader {
|
||||
public BlockType loadBlockType(MMOLineConfig config) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public DefaultSpawnOption loadDefaultSpawnOption(MMOLineConfig config) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ import net.Indyuce.mmocore.api.quest.trigger.Trigger;
|
||||
import net.Indyuce.mmocore.experience.dispenser.ExperienceDispenser;
|
||||
import net.Indyuce.mmocore.loot.chest.condition.Condition;
|
||||
import net.Indyuce.mmocore.loot.droptable.dropitem.DropItem;
|
||||
import net.Indyuce.mmocore.spawnpoint.def.DefaultSpawnOption;
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
@ -60,6 +61,11 @@ public class MMOLoadManager {
|
||||
return load("block type", config, loader -> loader.loadBlockType(config));
|
||||
}
|
||||
|
||||
public DefaultSpawnOption loadDefaultSpawnOption(MMOLineConfig config) {
|
||||
return load("default spawn", config, loader -> loader.loadDefaultSpawnOption(config));
|
||||
}
|
||||
|
||||
|
||||
private <T> T load(String objName, MMOLineConfig config, Function<MMOLoader, T> func) {
|
||||
|
||||
for (MMOLoader loader : loaders)
|
||||
|
@ -0,0 +1,56 @@
|
||||
package net.Indyuce.mmocore.manager;
|
||||
|
||||
import io.lumine.mythic.lib.UtilityMethods;
|
||||
import io.lumine.mythic.lib.api.MMOLineConfig;
|
||||
import io.lumine.mythic.lib.util.ConfigFile;
|
||||
import io.lumine.mythic.lib.util.configobject.ConfigSectionObject;
|
||||
import net.Indyuce.mmocore.MMOCore;
|
||||
import net.Indyuce.mmocore.spawnpoint.SpawnPoint;
|
||||
import net.Indyuce.mmocore.spawnpoint.def.DefaultSpawnOption;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class SpawnPointManager implements MMOCoreManager {
|
||||
/**
|
||||
* Ordered by world for faster access time.
|
||||
*/
|
||||
private final Map<World, List<SpawnPoint>> spawnPoints = new HashMap<>();
|
||||
|
||||
private Location globalSpawn;
|
||||
|
||||
private List<DefaultSpawnOption> defaultSpawnOptions = new ArrayList<>();
|
||||
|
||||
|
||||
public List<SpawnPoint> getSpawnPoints(World world) {
|
||||
return spawnPoints.getOrDefault(world, new ArrayList<>());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(boolean clearBefore) {
|
||||
FileConfiguration config = new ConfigFile(MMOCore.plugin, "", "spawn-points").getConfig();
|
||||
if (config.isConfigurationSection("spawn-points"))
|
||||
for (String key : config.getConfigurationSection("spawn-points").getKeys(false)) {
|
||||
SpawnPoint spawnPoint = new SpawnPoint(config.getConfigurationSection("spawn-points." + key));
|
||||
spawnPoints.putIfAbsent(spawnPoint.getLocation().getWorld(), new ArrayList<>());
|
||||
spawnPoints.get(spawnPoint.getLocation().getWorld()).add(spawnPoint);
|
||||
}
|
||||
globalSpawn = UtilityMethods.readLocation(new ConfigSectionObject(config.getConfigurationSection("global-spawn")));
|
||||
for (String defaultSpawnOption : config.getStringList("default-spawn")) {
|
||||
defaultSpawnOptions.add(MMOCore.plugin.loadManager.loadDefaultSpawnOption(new MMOLineConfig(defaultSpawnOption)));
|
||||
}
|
||||
}
|
||||
|
||||
public Location getGlobalSpawn() {
|
||||
return globalSpawn;
|
||||
}
|
||||
|
||||
public List<DefaultSpawnOption> getDefaultSpawnOptions() {
|
||||
return new ArrayList(defaultSpawnOptions);
|
||||
}
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
package net.Indyuce.mmocore.spawnpoint;
|
||||
|
||||
import io.lumine.mythic.lib.MythicLib;
|
||||
import io.lumine.mythic.lib.api.player.EquipmentSlot;
|
||||
import io.lumine.mythic.lib.player.PlayerMetadata;
|
||||
import io.lumine.mythic.lib.script.Script;
|
||||
import io.lumine.mythic.lib.script.condition.Condition;
|
||||
import io.lumine.mythic.lib.skill.SimpleSkill;
|
||||
import io.lumine.mythic.lib.skill.Skill;
|
||||
import io.lumine.mythic.lib.skill.handler.MythicLibSkillHandler;
|
||||
import io.lumine.mythic.lib.skill.trigger.TriggerMetadata;
|
||||
import io.lumine.mythic.lib.skill.trigger.TriggerType;
|
||||
import io.lumine.mythic.lib.util.configobject.ConfigSectionObject;
|
||||
import net.Indyuce.mmocore.MMOCore;
|
||||
import net.Indyuce.mmocore.api.player.PlayerData;
|
||||
import net.Indyuce.mmocore.player.Unlockable;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class SpawnPoint implements Unlockable, SpawnProvider {
|
||||
private final String id;
|
||||
Location location;
|
||||
Condition unlockCondition;
|
||||
double strength;
|
||||
|
||||
private Optional<Skill> unlockScript;
|
||||
private Optional<Skill> respawnScript;
|
||||
|
||||
public SpawnPoint(ConfigurationSection section) {
|
||||
id = section.getName();
|
||||
location = new Location(MMOCore.plugin.getServer().getWorld(section.getString("world")), section.getDouble("x"), section.getDouble("y"), section.getDouble("z"));
|
||||
unlockCondition = MythicLib.plugin.getSkills().loadCondition(
|
||||
new ConfigSectionObject(section.getConfigurationSection("unlock-condition")));
|
||||
strength = section.getDouble("strength");
|
||||
if (section.isConfigurationSection("script.unlock"))
|
||||
try {
|
||||
final Script script = MythicLib.plugin.getSkills().loadScript(section.getConfigurationSection("script.unlock"));
|
||||
unlockScript = Optional.of(new SimpleSkill(TriggerType.CAST, new MythicLibSkillHandler(script)));
|
||||
} catch (RuntimeException exception) {
|
||||
MMOCore.plugin.getLogger().log(Level.WARNING, "Could not load unlock script: " + exception.getMessage());
|
||||
}
|
||||
if (section.isConfigurationSection("script.respawn"))
|
||||
try {
|
||||
final Script script = MythicLib.plugin.getSkills().loadScript(section.getConfigurationSection("script.respawn"));
|
||||
respawnScript = Optional.of(new SimpleSkill(TriggerType.CAST, new MythicLibSkillHandler(script)));
|
||||
} catch (RuntimeException exception) {
|
||||
MMOCore.plugin.getLogger().log(Level.WARNING, "Could not load respawn script: " + exception.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public Location getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUnlockNamespacedKey() {
|
||||
return "spawnpoint:" + id;
|
||||
}
|
||||
|
||||
public double getStrength() {
|
||||
return strength;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUnlockedByDefault() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void whenLocked(PlayerData playerData) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void whenUnlocked(PlayerData playerData) {
|
||||
PlayerMetadata caster = playerData.getMMOPlayerData().getStatMap().cache(EquipmentSlot.OTHER);
|
||||
unlockScript.ifPresent(skill -> skill.cast(new TriggerMetadata(caster)));
|
||||
}
|
||||
|
||||
public void whenRespawn(PlayerData playerData) {
|
||||
|
||||
}
|
||||
|
||||
//TODO: Support BungeeCord. Point compliqué car il faut executer le script sur le serveur dans lequel le joueur va etre tp.
|
||||
@Override
|
||||
public void respawn(PlayerData playerData) {
|
||||
playerData.getPlayer().teleport(location);
|
||||
PlayerMetadata caster = playerData.getMMOPlayerData().getStatMap().cache(EquipmentSlot.OTHER);
|
||||
respawnScript.ifPresent(skill -> skill.cast(new TriggerMetadata(caster)));
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package net.Indyuce.mmocore.spawnpoint;
|
||||
|
||||
import net.Indyuce.mmocore.api.player.PlayerData;
|
||||
|
||||
public interface SpawnProvider {
|
||||
void respawn(PlayerData playerData);
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package net.Indyuce.mmocore.spawnpoint.def;
|
||||
|
||||
import io.lumine.mythic.lib.api.MMOLineConfig;
|
||||
import net.Indyuce.mmocore.api.player.PlayerData;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public abstract class DefaultSpawnOption {
|
||||
|
||||
protected final String worldExpression;
|
||||
|
||||
public DefaultSpawnOption(MMOLineConfig config) {
|
||||
this.worldExpression = config.getString("world-expression");
|
||||
}
|
||||
|
||||
public boolean matches(World world) {
|
||||
String worldName = world.getName();
|
||||
Pattern pattern = Pattern.compile(this.worldExpression);
|
||||
Matcher matcher = pattern.matcher(worldName);
|
||||
return matcher.matches();
|
||||
}
|
||||
|
||||
public abstract Location getSpawnLocation(PlayerData playerData);
|
||||
|
||||
public abstract void whenRespawn(PlayerData playerData);
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package net.Indyuce.mmocore.spawnpoint.def;
|
||||
|
||||
import io.lumine.mythic.lib.api.MMOLineConfig;
|
||||
import net.Indyuce.mmocore.MMOCore;
|
||||
import net.Indyuce.mmocore.api.player.PlayerData;
|
||||
import org.bukkit.Location;
|
||||
|
||||
public class GlobalSpawnOption extends DefaultSpawnOption {
|
||||
|
||||
public GlobalSpawnOption(MMOLineConfig config) {
|
||||
super(config);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location getSpawnLocation(PlayerData playerData) {
|
||||
return MMOCore.plugin.spawnPointManager.getGlobalSpawn();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void whenRespawn() {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package net.Indyuce.mmocore.spawnpoint.def;
|
||||
|
||||
import io.lumine.mythic.lib.api.MMOLineConfig;
|
||||
import net.Indyuce.mmocore.api.player.PlayerData;
|
||||
import org.bukkit.Location;
|
||||
|
||||
public class LastSpawnOption extends DefaultSpawnOption
|
||||
{
|
||||
|
||||
public LastSpawnOption(MMOLineConfig config) {
|
||||
super(config);
|
||||
}
|
||||
|
||||
//TODO
|
||||
@Override
|
||||
public Location getSpawnLocation(PlayerData playerData) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void whenRespawn(PlayerData playerData) {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package net.Indyuce.mmocore.spawnpoint.def;
|
||||
|
||||
import io.lumine.mythic.lib.api.MMOLineConfig;
|
||||
import io.lumine.mythic.lib.util.configobject.ConfigObject;
|
||||
import net.Indyuce.mmocore.api.player.PlayerData;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
|
||||
import javax.swing.text.html.Option;
|
||||
import java.util.Optional;
|
||||
|
||||
public class LocationSpawnOption extends DefaultSpawnOption {
|
||||
private final double x, y, z;
|
||||
private final Optional<World> world;
|
||||
|
||||
public LocationSpawnOption(MMOLineConfig config) {
|
||||
super(config);
|
||||
this.x = config.getDouble("spawn-x");
|
||||
this.y = config.getDouble("spawn-y");
|
||||
this.z = config.getDouble("spawn-z");
|
||||
this.world = Optional.ofNullable(Bukkit.getWorld(config.getString("spawn-world")));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location getSpawnLocation(PlayerData playerData) {
|
||||
return new Location(world.orElseGet(() -> playerData.getPlayer().getWorld()), x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void whenRespawn(PlayerData playerData) {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package net.Indyuce.mmocore.listener;
|
||||
|
||||
import net.Indyuce.mmocore.MMOCore;
|
||||
import net.Indyuce.mmocore.api.player.PlayerData;
|
||||
import net.Indyuce.mmocore.spawnpoint.SpawnPoint;
|
||||
import net.Indyuce.mmocore.spawnpoint.def.DefaultSpawnOption;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.PlayerDeathEvent;
|
||||
import org.bukkit.event.player.PlayerTeleportEvent;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SpawnPointsListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onTeleport(PlayerTeleportEvent event) {
|
||||
event.getFrom()
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onDeath(PlayerDeathEvent event) {
|
||||
//TODO Only when module enabled
|
||||
PlayerData playerData = PlayerData.get(event.getEntity());
|
||||
World world = playerData.getPlayer().getWorld();
|
||||
List<SpawnPoint> reachableSpawnPoints = MMOCore.plugin.spawnPointManager.
|
||||
getSpawnPoints(world)
|
||||
.stream()
|
||||
.filter(spawnPoint -> playerData.hasUnlocked(spawnPoint))
|
||||
.toList();
|
||||
if (!reachableSpawnPoints.isEmpty()) {
|
||||
double minDistance = Double.MAX_VALUE;
|
||||
SpawnPoint closestSpawnPoint = null;
|
||||
for (SpawnPoint spawnPoint : reachableSpawnPoints) {
|
||||
double distance = spawnPoint.getLocation().distance(playerData.getPlayer().getLocation());
|
||||
distance = distance / spawnPoint.getStrength();
|
||||
if (distance < minDistance) {
|
||||
minDistance = distance;
|
||||
closestSpawnPoint = spawnPoint;
|
||||
}
|
||||
}
|
||||
playerData.getPlayer().teleport(closestSpawnPoint.getLocation());
|
||||
closestSpawnPoint.whenRespawn(playerData);
|
||||
} else
|
||||
for (DefaultSpawnOption defaultSpawnOption : MMOCore.plugin.spawnPointManager.getDefaultSpawnOptions())
|
||||
if (defaultSpawnOption.matches(world)) {
|
||||
playerData.getPlayer().teleport(defaultSpawnOption.getSpawnLocation(playerData));
|
||||
defaultSpawnOption.whenRespawn();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user