Different multiplayer implementation

This commit is contained in:
AlessioDP 2019-09-06 11:05:20 +02:00
parent f176271b8f
commit 3ff05fb5a8
No known key found for this signature in database
GPG Key ID: B92B813840FFCC9F
3 changed files with 1786 additions and 1862 deletions

View File

@ -195,7 +195,7 @@ public class Quest {
// Multiplayer
try {
if (opts.getShareProgressLevel() == 3) {
List<Quester> mq = q.getMultiplayerQuesters(this);
List<Quester> mq = q.getMultiplayerQuesters();
if (mq != null) {
for (Quester qq : mq) {
if (qq.getCurrentQuests().containsKey(this)) {
@ -691,7 +691,7 @@ public class Quest {
// Multiplayer
if (opts.getShareProgressLevel() == 4) {
List<Quester> mq = q.getMultiplayerQuesters(this);
List<Quester> mq = q.getMultiplayerQuesters();
if (mq != null) {
for (Quester qq : mq) {
if (qq.getCurrentQuests().containsKey(this)) {

View File

@ -26,6 +26,7 @@ import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -1669,7 +1670,7 @@ public class Quester {
* Mark location as reached if the Quester has such an objective
*
* @param quest The quest for which the location is being reached
* @param n The location being reached
* @param l The location being reached
*/
public void reachLocation(Quest quest, Location l) {
if (getQuestData(quest).locationsReached == null) {
@ -1980,7 +1981,7 @@ public class Quester {
// Multiplayer
if (quest.getOptions().getShareProgressLevel() == 2) {
List<Quester> mq = getMultiplayerQuesters(quest);
List<Quester> mq = getMultiplayerQuesters();
if (mq != null) {
for (Quester q : mq) {
if (q.getCurrentQuests().containsKey(quest)) {
@ -3112,18 +3113,32 @@ public class Quester {
return playerAmount >= is.getAmount();
}
/**
* Dispatch any event to the right quest if multiplayer enabled
*
* @param eventName The event name
* @param fun The function to execute, the event call
*/
public void dispatchMultiplayerEvent(String eventName, BiFunction<Quester, Quest, Void> fun) {
List<Quester> mq = getMultiplayerQuesters();
if (mq != null) {
for (Quester q : mq) {
for (Quest quest : q.getCurrentQuests().keySet()) {
if (q.containsObjective(quest, eventName) && quest.getOptions().getShareProgressLevel() == 1) {
fun.apply(q, quest);
}
}
}
}
}
/**
* Get a list of fellow Questers in a party or group
*
* @param quest The quest which uses a linked plugin, i.e. Parties or DungeonsXL
* @return null if quest is null, no linked plugins, or party/group is null
* @return null if no linked plugins, or party/group is null
*/
public List<Quester> getMultiplayerQuesters(Quest quest) {
if (quest == null) {
return null;
}
public List<Quester> getMultiplayerQuesters() {
if (plugin.getDependencies().getPartiesApi() != null) {
if (quest.getOptions().getUsePartiesPlugin()) {
Party party = plugin.getDependencies().getPartiesApi().getParty(plugin.getDependencies().getPartiesApi().getPartyPlayer(getUUID()).getPartyName());
if (party != null) {
List<Quester> mq = new LinkedList<Quester>();
@ -3135,9 +3150,7 @@ public class Quester {
return mq;
}
}
}
if (plugin.getDependencies().getDungeonsApi() != null) {
if (quest.getOptions().getUseDungeonsXLPlugin()) {
DGroup group = DGroup.getByPlayer(getPlayer());
if (group != null) {
List<Quester> mq = new LinkedList<Quester>();
@ -3149,7 +3162,6 @@ public class Quester {
return mq;
}
}
}
return null;
}

View File

@ -204,25 +204,20 @@ public class PlayerListener implements Listener {
final Player player = evt.getPlayer();
boolean hasObjective = false;
if (evt.isCancelled() == false) {
final ItemStack blockItemStack = new ItemStack(evt.getClickedBlock().getType(), 1, evt.getClickedBlock().getState().getData().toItemStack().getDurability());
for (Quest quest : quester.getCurrentQuests().keySet()) {
if (quester.containsObjective(quest, "useBlock")) {
ItemStack i = new ItemStack(evt.getClickedBlock().getType(), 1, evt.getClickedBlock().getState().getData().toItemStack().getDurability());
quester.useBlock(quest, i);
quester.useBlock(quest, blockItemStack);
hasObjective = true;
}
}
// Multiplayer
if (quest.getOptions().getShareProgressLevel() == 1) {
List<Quester> mq = quester.getMultiplayerQuesters(quest);
if (mq != null) {
for (Quester q : mq) {
if (q.getCurrentQuests().containsKey(quest)) {
q.useBlock(quest, i);
}
}
}
}
}
}
quester.dispatchMultiplayerEvent("useBlock", (Quester q, Quest quest) -> {
q.useBlock(quest, blockItemStack);
return null;
});
}
if (!hasObjective) {
if (plugin.getQuestFactory().getSelectedBlockStarts().containsKey(evt.getPlayer().getUniqueId())) {
@ -371,20 +366,14 @@ public class PlayerListener implements Listener {
}
if (quester.containsObjective(quest, "password")) {
quester.sayPassword(quest, evt);
}
}
// Multiplayer
if (quest.getOptions().getShareProgressLevel() == 1) {
List<Quester> mq = quester.getMultiplayerQuesters(quest);
if (mq != null) {
for (Quester q : mq) {
if (q.getCurrentQuests().containsKey(quest)) {
quester.dispatchMultiplayerEvent("password", (Quester q, Quest quest) -> {
q.sayPassword(quest, evt);
}
}
}
}
}
}
return null;
});
}
}
}
@ -429,25 +418,20 @@ public class PlayerListener implements Listener {
@EventHandler
public void onBlockDamage(BlockDamageEvent evt) {
if (plugin.checkQuester(evt.getPlayer().getUniqueId()) == false) {
final ItemStack blockItemStack = new ItemStack(evt.getBlock().getType(), 1, evt.getBlock().getState().getData().toItemStack().getDurability());
Quester quester = plugin.getQuester(evt.getPlayer().getUniqueId());
for (Quest quest : quester.getCurrentQuests().keySet()) {
if (quester.containsObjective(quest, "damageBlock")) {
ItemStack i = new ItemStack(evt.getBlock().getType(), 1, evt.getBlock().getState().getData().toItemStack().getDurability());
quester.damageBlock(quest, i);
quester.damageBlock(quest, blockItemStack);
}
}
// Multiplayer
if (quest.getOptions().getShareProgressLevel() == 1) {
List<Quester> mq = quester.getMultiplayerQuesters(quest);
if (mq != null) {
for (Quester q : mq) {
if (q.getCurrentQuests().containsKey(quest)) {
q.damageBlock(quest, i);
}
}
}
}
}
}
quester.dispatchMultiplayerEvent("placeBlock", (Quester q, Quest quest) -> {
q.placeBlock(quest, blockItemStack);
return null;
});
}
}
@ -455,27 +439,22 @@ public class PlayerListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST)
public void onBlockPlace(BlockPlaceEvent evt) {
if (plugin.checkQuester(evt.getPlayer().getUniqueId()) == false) {
final ItemStack blockItemStack = new ItemStack(evt.getBlock().getType(), 1, evt.getBlock().getState().getData().toItemStack().getDurability());
Quester quester = plugin.getQuester(evt.getPlayer().getUniqueId());
for (Quest quest : quester.getCurrentQuests().keySet()) {
if (quester.containsObjective(quest, "placeBlock")) {
if (evt.isCancelled() == false) {
ItemStack i = new ItemStack(evt.getBlock().getType(), 1, evt.getBlock().getState().getData().toItemStack().getDurability());
quester.placeBlock(quest, i);
quester.placeBlock(quest, blockItemStack);
}
}
}
// Multiplayer
if (quest.getOptions().getShareProgressLevel() == 1) {
List<Quester> mq = quester.getMultiplayerQuesters(quest);
if (mq != null) {
for (Quester q : mq) {
if (q.getCurrentQuests().containsKey(quest)) {
q.placeBlock(quest, i);
}
}
}
}
}
}
}
quester.dispatchMultiplayerEvent("placeBlock", (Quester q, Quest quest) -> {
q.placeBlock(quest, blockItemStack);
return null;
});
}
}
@ -483,25 +462,14 @@ public class PlayerListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST)
public void onBlockBreak(BlockBreakEvent evt) {
if (plugin.checkQuester(evt.getPlayer().getUniqueId()) == false) {
final ItemStack blockItemStack = new ItemStack(evt.getBlock().getType(), 1, evt.getBlock().getState().getData().toItemStack().getDurability());
Quester quester = plugin.getQuester(evt.getPlayer().getUniqueId());
for (Quest quest : quester.getCurrentQuests().keySet()) {
if (evt.isCancelled() == false) {
if (quester.containsObjective(quest, "breakBlock")) {
if (evt.getPlayer().getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH) == false) {
ItemStack i = new ItemStack(evt.getBlock().getType(), 1, evt.getBlock().getState().getData().toItemStack().getDurability());
quester.breakBlock(quest, i);
// Multiplayer
if (quest.getOptions().getShareProgressLevel() == 1) {
List<Quester> mq = quester.getMultiplayerQuesters(quest);
if (mq != null) {
for (Quester q : mq) {
if (q.getCurrentQuests().containsKey(quest)) {
q.breakBlock(quest, i);
}
}
}
}
quester.breakBlock(quest, blockItemStack);
}
}
if (quester.containsObjective(quest, "placeBlock")) {
@ -515,48 +483,55 @@ public class PlayerListener implements Listener {
}
if (quester.containsObjective(quest, "cutBlock")) {
if (evt.getPlayer().getItemInHand().getType().equals(Material.SHEARS)) {
ItemStack i = new ItemStack(evt.getBlock().getType(), 1, evt.getBlock().getState().getData().toItemStack().getDurability());
quester.cutBlock(quest, i);
quester.cutBlock(quest, blockItemStack);
}
}
}
}
// Multiplayer
if (quest.getOptions().getShareProgressLevel() == 1) {
List<Quester> mq = quester.getMultiplayerQuesters(quest);
if (mq != null) {
for (Quester q : mq) {
if (q.getCurrentQuests().containsKey(quest)) {
q.cutBlock(quest, i);
}
}
}
}
}
quester.dispatchMultiplayerEvent("breakBlock", (Quester q, Quest quest) -> {
if (evt.getPlayer().getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH) == false) {
q.breakBlock(quest, blockItemStack);
}
return null;
});
quester.dispatchMultiplayerEvent("placeBlock", (Quester q, Quest quest) -> {
for (ItemStack is : q.getQuestData(quest).blocksPlaced) {
if (evt.getBlock().getType().equals(is.getType()) && is.getAmount() > 0) {
int index = q.getQuestData(quest).blocksPlaced.indexOf(is);
is.setAmount(is.getAmount() - 1);
q.getQuestData(quest).blocksPlaced.set(index, is);
}
}
return null;
});
quester.dispatchMultiplayerEvent("cutBlock", (Quester q, Quest quest) -> {
if (evt.getPlayer().getItemInHand().getType().equals(Material.SHEARS)) {
q.cutBlock(quest, blockItemStack);
}
return null;
});
}
}
@EventHandler
public void onPlayerShearEntity(PlayerShearEntityEvent evt) {
if (evt.getEntity().getType() == EntityType.SHEEP) {
if (plugin.canUseQuests(evt.getPlayer().getUniqueId())) {
Sheep sheep = (Sheep) evt.getEntity();
Quester quester = plugin.getQuester(evt.getPlayer().getUniqueId());
for (Quest quest : quester.getCurrentQuests().keySet()) {
if (evt.getEntity().getType() == EntityType.SHEEP && quester.containsObjective(quest, "shearSheep")) {
Sheep sheep = (Sheep) evt.getEntity();
if (quester.containsObjective(quest, "shearSheep")) {
quester.shearSheep(quest, sheep.getColor());
}
}
// Multiplayer
if (quest.getOptions().getShareProgressLevel() == 1) {
List<Quester> mq = quester.getMultiplayerQuesters(quest);
if (mq != null) {
for (Quester q : mq) {
if (q.getCurrentQuests().containsKey(quest)) {
quester.dispatchMultiplayerEvent("shearSheep", (Quester q, Quest quest) -> {
q.shearSheep(quest, sheep.getColor());
}
}
}
}
}
return null;
});
}
}
}
@ -570,20 +545,14 @@ public class PlayerListener implements Listener {
for (Quest quest : quester.getCurrentQuests().keySet()) {
if (quester.containsObjective(quest, "tameMob")) {
quester.tameMob(quest, evt.getEntityType());
}
}
// Multiplayer
if (quest.getOptions().getShareProgressLevel() == 1) {
List<Quester> mq = quester.getMultiplayerQuesters(quest);
if (mq != null) {
for (Quester q : mq) {
if (q.getCurrentQuests().containsKey(quest)) {
quester.dispatchMultiplayerEvent("tameMob", (Quester q, Quest quest) -> {
q.tameMob(quest, evt.getEntityType());
}
}
}
}
}
}
return null;
});
}
}
}
@ -594,13 +563,30 @@ public class PlayerListener implements Listener {
public void onCraftItem(CraftItemEvent evt) {
if (evt.getWhoClicked() instanceof Player) {
if (plugin.checkQuester(evt.getWhoClicked().getUniqueId()) == false) {
final ItemStack craftedItem = getCraftedItem(evt);
Quester quester = plugin.getQuester(evt.getWhoClicked().getUniqueId());
for (Quest quest : quester.getCurrentQuests().keySet()) {
if (quester.containsObjective(quest, "craftItem")) {
quester.craftItem(quest, craftedItem);
}
}
// Multiplayer
quester.dispatchMultiplayerEvent("craftItem", (Quester q, Quest quest) -> {
q.craftItem(quest, craftedItem);
return null;
});
}
}
}
@SuppressWarnings("deprecation")
private ItemStack getCraftedItem(CraftItemEvent evt) {
if (evt.isShiftClick()) {
ItemStack result = evt.getRecipe().getResult();
int numberOfItems = result.getAmount();
if (evt.isShiftClick()) {
int itemsChecked = 0;
for (ItemStack item : evt.getInventory().getMatrix()) {
if (item != null && !item.getType().equals(Material.AIR)) {
if (itemsChecked == 0) {
@ -611,39 +597,11 @@ public class PlayerListener implements Listener {
}
}
}
quester.craftItem(quest, new ItemStack(result.getType(), numberOfItems, result.getDurability()));
return new ItemStack(result.getType(), numberOfItems, result.getDurability());
}
return evt.getCurrentItem();
}
// Multiplayer
if (quest.getOptions().getShareProgressLevel() == 1) {
List<Quester> mq = quester.getMultiplayerQuesters(quest);
if (mq != null) {
for (Quester q : mq) {
if (q.getCurrentQuests().containsKey(quest)) {
q.craftItem(quest, new ItemStack(result.getType(), numberOfItems, result.getDurability()));
}
}
}
}
} else {
quester.craftItem(quest, evt.getCurrentItem());
// Multiplayer
if (quest.getOptions().getShareProgressLevel() == 1) {
List<Quester> mq = quester.getMultiplayerQuesters(quest);
if (mq != null) {
for (Quester q : mq) {
if (q.getCurrentQuests().containsKey(quest)) {
q.craftItem(quest, evt.getCurrentItem());
}
}
}
}
}
}
}
}
}
}
@EventHandler
public void onInventoryClick(InventoryClickEvent evt) {
@ -654,20 +612,14 @@ public class PlayerListener implements Listener {
for (Quest quest : quester.getCurrentQuests().keySet()) {
if (quester.containsObjective(quest, "smeltItem")) {
quester.smeltItem(quest, evt.getCurrentItem());
}
}
// Multiplayer
if (quest.getOptions().getShareProgressLevel() == 1) {
List<Quester> mq = quester.getMultiplayerQuesters(quest);
if (mq != null) {
for (Quester q : mq) {
if (q.getCurrentQuests().containsKey(quest)) {
quester.dispatchMultiplayerEvent("smeltItem", (Quester q, Quest quest) -> {
q.smeltItem(quest, evt.getCurrentItem());
}
}
}
}
}
}
return null;
});
}
} else if (evt.getInventory().getType() == InventoryType.BREWING) {
if (evt.getSlotType() == SlotType.CRAFTING) {
@ -675,20 +627,14 @@ public class PlayerListener implements Listener {
for (Quest quest : quester.getCurrentQuests().keySet()) {
if (quester.containsObjective(quest, "brewItem")) {
quester.brewItem(quest, evt.getCurrentItem());
}
}
// Multiplayer
if (quest.getOptions().getShareProgressLevel() == 1) {
List<Quester> mq = quester.getMultiplayerQuesters(quest);
if (mq != null) {
for (Quester q : mq) {
if (q.getCurrentQuests().containsKey(quest)) {
quester.dispatchMultiplayerEvent("brewItem", (Quester q, Quest quest) -> {
q.brewItem(quest, evt.getCurrentItem());
}
}
}
}
}
}
return null;
});
}
}
}
@ -702,21 +648,17 @@ public class PlayerListener implements Listener {
if (quester.containsObjective(quest, "enchantItem")) {
for (Enchantment e : evt.getEnchantsToAdd().keySet()) {
quester.enchantItem(quest, e, evt.getItem().getType());
}
}
}
// Multiplayer
if (quest.getOptions().getShareProgressLevel() == 1) {
List<Quester> mq = quester.getMultiplayerQuesters(quest);
if (mq != null) {
for (Quester q : mq) {
if (q.getCurrentQuests().containsKey(quest)) {
quester.dispatchMultiplayerEvent("enchantItem", (Quester q, Quest quest) -> {
for (Enchantment e : evt.getEnchantsToAdd().keySet()) {
q.enchantItem(quest, e, evt.getItem().getType());
}
}
}
}
}
}
}
return null;
});
}
}
@ -771,20 +713,14 @@ public class PlayerListener implements Listener {
for (Quest quest : quester.getCurrentQuests().keySet()) {
if (quester.containsObjective(quest, "killNPC")) {
quester.killNPC(quest, CitizensAPI.getNPCRegistry().getNPC(target));
}
}
// Multiplayer
if (quest.getOptions().getShareProgressLevel() == 1) {
List<Quester> mq = quester.getMultiplayerQuesters(quest);
if (mq != null) {
for (Quester q : mq) {
if (q.getCurrentQuests().containsKey(quest)) {
quester.dispatchMultiplayerEvent("killNPC", (Quester q, Quest quest) -> {
q.killNPC(quest, CitizensAPI.getNPCRegistry().getNPC(target));
}
}
}
}
}
}
return null;
});
return;
}
}
@ -792,20 +728,14 @@ public class PlayerListener implements Listener {
for (Quest quest : quester.getCurrentQuests().keySet()) {
if (quester.containsObjective(quest, "killMob")) {
quester.killMob(quest, target.getLocation(), target.getType());
}
}
// Multiplayer
if (quest.getOptions().getShareProgressLevel() == 1) {
List<Quester> mq = quester.getMultiplayerQuesters(quest);
if (mq != null) {
for (Quester q : mq) {
if (q.getCurrentQuests().containsKey(quest)) {
quester.dispatchMultiplayerEvent("killMob", (Quester q, Quest quest) -> {
q.killMob(quest, target.getLocation(), target.getType());
}
}
}
}
}
}
return null;
});
}
}
@ -893,20 +823,14 @@ public class PlayerListener implements Listener {
for (Quest quest : quester.getCurrentQuests().keySet()) {
if (quester.containsObjective(quest, "killPlayer")) {
quester.killPlayer(quest, (Player)target);
}
}
// Multiplayer
if (quest.getOptions().getShareProgressLevel() == 1) {
List<Quester> mq = quester.getMultiplayerQuesters(quest);
if (mq != null) {
for (Quester q : mq) {
if (q.getCurrentQuests().containsKey(quest)) {
quester.dispatchMultiplayerEvent("killPlayer", (Quester q, Quest quest) -> {
q.killPlayer(quest, (Player)target);
}
}
}
}
}
}
return null;
});
}
}
@ -918,20 +842,14 @@ public class PlayerListener implements Listener {
for (Quest quest : quester.getCurrentQuests().keySet()) {
if (quester.containsObjective(quest, "catchFish") && evt.getState().equals(State.CAUGHT_FISH)) {
quester.catchFish(quest);
}
}
// Multiplayer
if (quest.getOptions().getShareProgressLevel() == 1) {
List<Quester> mq = quester.getMultiplayerQuesters(quest);
if (mq != null) {
for (Quester q : mq) {
if (q.getCurrentQuests().containsKey(quest)) {
quester.dispatchMultiplayerEvent("catchFish", (Quester q, Quest quest) -> {
q.catchFish(quest);
}
}
}
}
}
}
return null;
});
}
}
@ -1056,20 +974,14 @@ public class PlayerListener implements Listener {
for (Quest quest : quester.getCurrentQuests().keySet()) {
if (quester.containsObjective(quest, "reachLocation")) {
quester.reachLocation(quest, evt.getTo());
}
}
// Multiplayer
if (quest.getOptions().getShareProgressLevel() == 1) {
List<Quester> mq = quester.getMultiplayerQuesters(quest);
if (mq != null) {
for (Quester q : mq) {
if (q.getCurrentQuests().containsKey(quest)) {
quester.dispatchMultiplayerEvent("reachLocation", (Quester q, Quest quest) -> {
q.reachLocation(quest, evt.getTo());
}
}
}
}
}
}
return null;
});
}
}
}