2019-08-07 20:35:19 +02:00
|
|
|
package com.massivecraft.factions.missions;
|
|
|
|
|
|
|
|
import com.massivecraft.factions.FPlayer;
|
|
|
|
import com.massivecraft.factions.FPlayers;
|
2019-09-14 21:13:01 +02:00
|
|
|
import com.massivecraft.factions.FactionsPlugin;
|
2019-08-07 20:35:19 +02:00
|
|
|
import com.massivecraft.factions.zcore.util.TL;
|
|
|
|
import org.bukkit.configuration.ConfigurationSection;
|
2019-09-15 11:08:00 +02:00
|
|
|
import org.bukkit.entity.Player;
|
2019-08-07 20:35:19 +02:00
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.Listener;
|
|
|
|
import org.bukkit.event.block.BlockBreakEvent;
|
|
|
|
import org.bukkit.event.block.BlockPlaceEvent;
|
2019-09-16 06:01:34 +02:00
|
|
|
import org.bukkit.event.enchantment.EnchantItemEvent;
|
2019-08-07 20:35:19 +02:00
|
|
|
import org.bukkit.event.entity.EntityDeathEvent;
|
2019-09-15 11:08:00 +02:00
|
|
|
import org.bukkit.event.entity.EntityTameEvent;
|
2019-08-07 20:35:19 +02:00
|
|
|
import org.bukkit.event.player.PlayerFishEvent;
|
2019-09-16 06:01:34 +02:00
|
|
|
import org.bukkit.event.player.PlayerItemConsumeEvent;
|
2019-08-07 20:35:19 +02:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
public class MissionHandler implements Listener {
|
|
|
|
|
2019-09-15 11:19:06 +02:00
|
|
|
private FactionsPlugin plugin;
|
2019-08-07 20:35:19 +02:00
|
|
|
|
2019-09-15 11:19:06 +02:00
|
|
|
public MissionHandler(FactionsPlugin plugin) {
|
|
|
|
this.plugin = plugin;
|
|
|
|
}
|
2019-08-07 20:35:19 +02:00
|
|
|
|
2019-09-15 11:19:06 +02:00
|
|
|
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
|
|
|
public void onPlayerTame(EntityTameEvent event) {
|
|
|
|
if (!(event.getOwner() instanceof Player)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FPlayer fPlayer = FPlayers.getInstance().getByPlayer((Player) event.getOwner());
|
|
|
|
if (fPlayer == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
List<Mission> missions = fPlayer.getFaction().getMissions().values().stream().filter(mission -> mission.getType().equalsIgnoreCase("tame")).collect(Collectors.toList());
|
|
|
|
for (Mission mission2 : missions) {
|
|
|
|
ConfigurationSection section = plugin.getConfig().getConfigurationSection("Missions").getConfigurationSection(mission2.getName());
|
|
|
|
if (!event.getEntityType().toString().equals(section.getConfigurationSection("Mission").getString("EntityType")) && !section.getConfigurationSection("Mission").getString("EntityType").equalsIgnoreCase("ALL")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mission2.incrementProgress();
|
|
|
|
checkIfDone(fPlayer, mission2, section);
|
|
|
|
}
|
|
|
|
}
|
2019-09-15 11:08:00 +02:00
|
|
|
|
2019-09-15 11:19:06 +02:00
|
|
|
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
|
|
|
public void onEntityDeath(EntityDeathEvent event) {
|
|
|
|
if (event.getEntity() == null || event.getEntity().getKiller() == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FPlayer fPlayer = FPlayers.getInstance().getByPlayer(event.getEntity().getKiller());
|
|
|
|
if (fPlayer == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
List<Mission> missions = fPlayer.getFaction().getMissions().values().stream().filter(mission -> mission.getType().equalsIgnoreCase("kill")).collect(Collectors.toList());
|
|
|
|
for (Mission mission2 : missions) {
|
|
|
|
ConfigurationSection section = plugin.getConfig().getConfigurationSection("Missions").getConfigurationSection(mission2.getName());
|
|
|
|
if (!event.getEntityType().toString().equals(section.getConfigurationSection("Mission").getString("EntityType"))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mission2.incrementProgress();
|
|
|
|
checkIfDone(fPlayer, mission2, section);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 20:35:19 +02:00
|
|
|
|
2019-09-15 11:19:06 +02:00
|
|
|
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
|
|
|
public void onBlockBreak(BlockBreakEvent event) {
|
|
|
|
FPlayer fPlayer = FPlayers.getInstance().getByPlayer(event.getPlayer());
|
|
|
|
if (fPlayer == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
List<Mission> missions = fPlayer.getFaction().getMissions().values().stream().filter(mission -> mission.getType().equalsIgnoreCase("mine")).collect(Collectors.toList());
|
|
|
|
for (Mission mission2 : missions) {
|
|
|
|
ConfigurationSection section = plugin.getConfig().getConfigurationSection("Missions").getConfigurationSection(mission2.getName());
|
|
|
|
if (!event.getBlock().getType().toString().equals(section.getConfigurationSection("Mission").getString("Material"))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mission2.incrementProgress();
|
|
|
|
checkIfDone(fPlayer, mission2, section);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 20:35:19 +02:00
|
|
|
|
2019-09-15 11:19:06 +02:00
|
|
|
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
|
|
|
public void onBlockPlace(BlockPlaceEvent event) {
|
|
|
|
FPlayer fPlayer = FPlayers.getInstance().getByPlayer(event.getPlayer());
|
|
|
|
if (fPlayer == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
List<Mission> missions = fPlayer.getFaction().getMissions().values().stream().filter(mission -> mission.getType().equalsIgnoreCase("place")).collect(Collectors.toList());
|
|
|
|
for (Mission mission2 : missions) {
|
|
|
|
ConfigurationSection section = plugin.getConfig().getConfigurationSection("Missions").getConfigurationSection(mission2.getName());
|
|
|
|
if (!event.getBlock().getType().toString().equals(section.getConfigurationSection("Mission").getString("Material"))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mission2.incrementProgress();
|
|
|
|
checkIfDone(fPlayer, mission2, section);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 20:35:19 +02:00
|
|
|
|
2019-09-15 11:19:06 +02:00
|
|
|
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
|
|
|
public void onPlayerFish(PlayerFishEvent event) {
|
|
|
|
if (event.getState() != PlayerFishEvent.State.CAUGHT_FISH) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FPlayer fPlayer = FPlayers.getInstance().getByPlayer(event.getPlayer());
|
|
|
|
if (fPlayer == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
List<Mission> missions = fPlayer.getFaction().getMissions().values().stream().filter(mission -> mission.getType().equalsIgnoreCase("fish")).collect(Collectors.toList());
|
|
|
|
for (Mission mission2 : missions) {
|
|
|
|
ConfigurationSection section = plugin.getConfig().getConfigurationSection("Missions").getConfigurationSection(mission2.getName());
|
|
|
|
mission2.incrementProgress();
|
|
|
|
checkIfDone(fPlayer, mission2, section);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 20:35:19 +02:00
|
|
|
|
2019-09-16 06:01:34 +02:00
|
|
|
@EventHandler
|
|
|
|
public void onPlayerEnchant(EnchantItemEvent e) {
|
|
|
|
FPlayer fPlayer = FPlayers.getInstance().getByPlayer(e.getEnchanter());
|
|
|
|
if (fPlayer == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
List<Mission> missions = fPlayer.getFaction().getMissions().values().stream().filter(mission -> mission.getType().equalsIgnoreCase("enchant")).collect(Collectors.toList());
|
|
|
|
for (Mission mission2 : missions) {
|
|
|
|
ConfigurationSection section = plugin.getConfig().getConfigurationSection("Missions").getConfigurationSection(mission2.getName());
|
|
|
|
mission2.incrementProgress();
|
|
|
|
checkIfDone(fPlayer, mission2, section);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void onPlayerConsume(PlayerItemConsumeEvent e) {
|
|
|
|
FPlayer fPlayer = FPlayers.getInstance().getByPlayer(e.getPlayer());
|
|
|
|
if (fPlayer == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
List<Mission> missions = fPlayer.getFaction().getMissions().values().stream().filter(mission -> mission.getType().equalsIgnoreCase("consume")).collect(Collectors.toList());
|
|
|
|
for (Mission mission2 : missions) {
|
|
|
|
ConfigurationSection section = plugin.getConfig().getConfigurationSection("Missions").getConfigurationSection(mission2.getName());
|
|
|
|
if (!e.getItem().toString().contains(section.getConfigurationSection("Mission").getString("Item")) && !section.getConfigurationSection("Mission").getString("Item").equalsIgnoreCase("ALL")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mission2.incrementProgress();
|
|
|
|
checkIfDone(fPlayer, mission2, section);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-15 11:19:06 +02:00
|
|
|
private void checkIfDone(FPlayer fPlayer, Mission mission, ConfigurationSection section) {
|
|
|
|
if (mission.getProgress() < section.getConfigurationSection("Mission").getLong("Amount")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (String command : section.getConfigurationSection("Reward").getStringList("Commands")) {
|
|
|
|
FactionsPlugin.getInstance().getServer().dispatchCommand(FactionsPlugin.getInstance().getServer().getConsoleSender(), command.replace("%faction%", fPlayer.getFaction().getTag()));
|
|
|
|
}
|
|
|
|
fPlayer.getFaction().getMissions().remove(mission.getName());
|
|
|
|
fPlayer.getFaction().msg(TL.MISSION_MISSION_FINISHED, plugin.color(section.getString("Name")));
|
|
|
|
}
|
2019-08-07 20:35:19 +02:00
|
|
|
}
|