2013-01-30 15:17:50 +01:00
|
|
|
package com.gmail.nossr50.party;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
2013-02-09 12:24:02 +01:00
|
|
|
import org.bukkit.entity.Item;
|
2013-01-30 15:17:50 +01:00
|
|
|
import org.bukkit.entity.Player;
|
2013-02-09 12:24:02 +01:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2013-01-30 15:17:50 +01:00
|
|
|
|
|
|
|
import com.gmail.nossr50.config.Config;
|
2013-03-01 06:52:01 +01:00
|
|
|
import com.gmail.nossr50.config.party.ItemWeightConfig;
|
|
|
|
import com.gmail.nossr50.datatypes.party.Party;
|
|
|
|
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
|
|
|
import com.gmail.nossr50.datatypes.skills.SkillType;
|
|
|
|
import com.gmail.nossr50.util.ItemUtils;
|
2013-02-09 12:24:02 +01:00
|
|
|
import com.gmail.nossr50.util.Misc;
|
2013-03-01 06:52:01 +01:00
|
|
|
import com.gmail.nossr50.util.player.UserManager;
|
2013-01-30 15:17:50 +01:00
|
|
|
|
2013-02-01 06:38:25 +01:00
|
|
|
public final class ShareHandler {
|
2013-02-08 17:53:49 +01:00
|
|
|
public enum ShareMode {
|
2013-02-01 06:38:25 +01:00
|
|
|
NONE,
|
2013-02-09 12:24:02 +01:00
|
|
|
EQUAL,
|
|
|
|
RANDOM;
|
2013-02-01 06:38:25 +01:00
|
|
|
|
2013-02-08 17:53:49 +01:00
|
|
|
public static ShareMode getFromString(String string) {
|
2013-02-01 06:38:25 +01:00
|
|
|
try {
|
|
|
|
return valueOf(string);
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException exception) {
|
|
|
|
return NONE;
|
2013-02-01 13:57:00 +01:00
|
|
|
}
|
|
|
|
catch (NullPointerException exception) {
|
|
|
|
return NONE;
|
2013-02-01 06:38:25 +01:00
|
|
|
}
|
2013-01-30 15:17:50 +01:00
|
|
|
}
|
2013-02-01 06:38:25 +01:00
|
|
|
};
|
2013-01-30 17:25:44 +01:00
|
|
|
|
2013-02-01 06:38:25 +01:00
|
|
|
private ShareHandler() {}
|
2013-01-30 15:17:50 +01:00
|
|
|
|
2013-02-27 00:46:15 +01:00
|
|
|
private static List<Player> nearMembers;
|
|
|
|
private static int partySize;
|
|
|
|
|
2013-01-30 15:17:50 +01:00
|
|
|
/**
|
2013-02-02 05:00:39 +01:00
|
|
|
* Distribute Xp amongst party members.
|
2013-01-30 15:17:50 +01:00
|
|
|
*
|
2013-02-02 05:00:39 +01:00
|
|
|
* @param xp Xp without party sharing
|
|
|
|
* @param mcMMOPlayer Player initiating the Xp gain
|
|
|
|
* @param skillType Skill being used
|
2013-02-02 05:06:47 +01:00
|
|
|
* @return True is the xp has been shared
|
2013-01-30 15:17:50 +01:00
|
|
|
*/
|
2013-02-03 12:45:19 +01:00
|
|
|
public static boolean handleXpShare(int xp, McMMOPlayer mcMMOPlayer, SkillType skillType) {
|
2013-02-02 05:00:39 +01:00
|
|
|
Party party = mcMMOPlayer.getParty();
|
2013-02-01 06:38:25 +01:00
|
|
|
|
2013-02-03 05:25:02 +01:00
|
|
|
switch (party.getXpShareMode()) {
|
2013-03-01 06:52:01 +01:00
|
|
|
case EQUAL:
|
|
|
|
Player player = mcMMOPlayer.getPlayer();
|
|
|
|
nearMembers = PartyManager.getNearMembers(player, party, Config.getInstance().getPartyShareRange());
|
2013-01-30 15:17:50 +01:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
if (nearMembers.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-30 15:17:50 +01:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
double partySize = nearMembers.size() + 1;
|
|
|
|
double shareBonus = Config.getInstance().getPartyShareBonusBase() + partySize * Config.getInstance().getPartyShareBonusIncrease();
|
2013-02-02 04:33:26 +01:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
if (shareBonus > Config.getInstance().getPartyShareBonusCap()) {
|
|
|
|
shareBonus = Config.getInstance().getPartyShareBonusCap();
|
|
|
|
}
|
2013-02-02 05:00:39 +01:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
double splitXp = xp / partySize * shareBonus;
|
|
|
|
int roundedXp = (int) Math.ceil(splitXp);
|
2013-02-01 06:38:25 +01:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
for (Player member : nearMembers) {
|
|
|
|
UserManager.getPlayer(member).beginUnsharedXpGain(skillType, roundedXp);
|
|
|
|
}
|
|
|
|
|
|
|
|
mcMMOPlayer.beginUnsharedXpGain(skillType, roundedXp);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
case NONE:
|
|
|
|
// Fallthrough
|
|
|
|
default:
|
|
|
|
return false;
|
2013-02-03 05:25:02 +01:00
|
|
|
}
|
2013-02-01 06:38:25 +01:00
|
|
|
}
|
2013-02-09 12:24:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Distribute Items amongst party members.
|
|
|
|
*
|
|
|
|
* @param item Item that will get shared
|
|
|
|
* @param mcMMOPlayer Player who picked up the item
|
|
|
|
* @return True if the item has been shared
|
|
|
|
*/
|
2013-04-09 22:28:02 +02:00
|
|
|
public static boolean handleItemShare(Item drop, McMMOPlayer mcMMOPlayer) {
|
|
|
|
ItemStack itemStack = drop.getItemStack();
|
2013-02-09 12:24:02 +01:00
|
|
|
Party party = mcMMOPlayer.getParty();
|
2013-02-27 00:46:15 +01:00
|
|
|
Player player = mcMMOPlayer.getPlayer();
|
|
|
|
Player winningPlayer = null;
|
2013-02-09 12:24:02 +01:00
|
|
|
|
2013-02-27 00:46:15 +01:00
|
|
|
ItemStack newStack = itemStack.clone();
|
|
|
|
newStack.setAmount(1);
|
2013-02-09 12:24:02 +01:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
if (ItemUtils.isMobDrop(itemStack) && !party.sharingLootDrops()) {
|
2013-02-09 12:24:02 +01:00
|
|
|
return false;
|
2013-02-27 00:46:15 +01:00
|
|
|
}
|
2013-03-01 06:52:01 +01:00
|
|
|
else if (ItemUtils.isMiningDrop(itemStack) && !party.sharingMiningDrops()) {
|
2013-02-27 00:46:15 +01:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-01 06:52:01 +01:00
|
|
|
else if (ItemUtils.isHerbalismDrop(itemStack) && !party.sharingHerbalismDrops()) {
|
2013-02-27 00:46:15 +01:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-01 06:52:01 +01:00
|
|
|
else if (ItemUtils.isWoodcuttingDrop(itemStack) && !party.sharingWoodcuttingDrops()) {
|
2013-02-27 00:46:15 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (party.getItemShareMode()) {
|
2013-03-01 06:52:01 +01:00
|
|
|
case EQUAL:
|
|
|
|
McMMOPlayer mcMMOTarget;
|
|
|
|
nearMembers = PartyManager.getNearMembers(player, party, Config.getInstance().getPartyShareRange());
|
2013-02-09 12:24:02 +01:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
if (nearMembers.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-09 12:24:02 +01:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
nearMembers.add(player);
|
|
|
|
partySize = nearMembers.size();
|
2013-02-27 00:46:15 +01:00
|
|
|
|
2013-04-09 22:28:02 +02:00
|
|
|
drop.remove();
|
2013-03-01 06:52:01 +01:00
|
|
|
int itemWeight = ItemWeightConfig.getInstance().getItemWeight(itemStack.getType());
|
2013-02-27 00:46:15 +01:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
for (int i = 0; i < itemStack.getAmount(); i++) {
|
|
|
|
int highestRoll = 0;
|
2013-02-27 00:46:15 +01:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
for (Player member : nearMembers) {
|
|
|
|
McMMOPlayer mcMMOMember = UserManager.getPlayer(member);
|
|
|
|
int itemShareModifier = mcMMOMember.getItemShareModifier();
|
|
|
|
int diceRoll = Misc.getRandom().nextInt(itemShareModifier);
|
2013-02-27 00:46:15 +01:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
if (diceRoll > highestRoll) {
|
|
|
|
highestRoll = diceRoll;
|
2013-02-27 00:46:15 +01:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
if (winningPlayer != null) {
|
|
|
|
McMMOPlayer mcMMOWinning = UserManager.getPlayer(winningPlayer);
|
|
|
|
mcMMOWinning.setItemShareModifier(mcMMOWinning.getItemShareModifier() + itemWeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
winningPlayer = member;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mcMMOMember.setItemShareModifier(itemShareModifier + itemWeight);
|
|
|
|
}
|
2013-02-27 00:46:15 +01:00
|
|
|
}
|
2013-03-01 06:52:01 +01:00
|
|
|
|
|
|
|
mcMMOTarget = UserManager.getPlayer(winningPlayer);
|
|
|
|
mcMMOTarget.setItemShareModifier(mcMMOTarget.getItemShareModifier() - itemWeight);
|
|
|
|
|
|
|
|
if (winningPlayer.getInventory().addItem(newStack).size() != 0) {
|
|
|
|
winningPlayer.getWorld().dropItemNaturally(winningPlayer.getLocation(), newStack);
|
2013-02-27 00:46:15 +01:00
|
|
|
}
|
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
winningPlayer.updateInventory();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case RANDOM:
|
|
|
|
nearMembers = PartyManager.getNearMembers(player, party, Config.getInstance().getPartyShareRange());
|
2013-02-27 00:46:15 +01:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
if (nearMembers.isEmpty()) {
|
|
|
|
return false;
|
2013-02-27 00:46:15 +01:00
|
|
|
}
|
2013-02-09 12:24:02 +01:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
partySize = nearMembers.size() + 1;
|
2013-02-09 12:24:02 +01:00
|
|
|
|
2013-04-09 22:28:02 +02:00
|
|
|
drop.remove();
|
2013-02-09 12:24:02 +01:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
for (int i = 0; i < itemStack.getAmount(); i++) {
|
|
|
|
int randomMember = Misc.getRandom().nextInt(partySize);
|
2013-02-27 00:46:15 +01:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
if (randomMember >= nearMembers.size()) {
|
|
|
|
winningPlayer = player;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
winningPlayer = nearMembers.get(randomMember);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (winningPlayer.getInventory().addItem(newStack).size() != 0) {
|
|
|
|
winningPlayer.getWorld().dropItemNaturally(winningPlayer.getLocation(), newStack);
|
|
|
|
}
|
|
|
|
|
|
|
|
winningPlayer.updateInventory();
|
2013-02-09 12:24:02 +01:00
|
|
|
}
|
2013-03-01 06:52:01 +01:00
|
|
|
return true;
|
|
|
|
case NONE:
|
|
|
|
// Fallthrough
|
|
|
|
default:
|
|
|
|
return false;
|
2013-02-09 12:24:02 +01:00
|
|
|
}
|
|
|
|
}
|
2013-03-01 06:52:01 +01:00
|
|
|
}
|