Add random item share mode for parties.

Use /party itemshare random to enable random item sharing. Disable with
/party itemshare [none | false | off]
Whenever a party member picks up an item, everyone has a chance of
receiving that item instead of the player who picked it up.
This commit is contained in:
TfT_02 2013-02-09 12:24:02 +01:00
parent b60d9032be
commit 74df4a4fb6
7 changed files with 277 additions and 9 deletions

View File

@ -3,6 +3,7 @@ package com.gmail.nossr50.listeners;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -14,6 +15,7 @@ import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerFishEvent; import org.bukkit.event.player.PlayerFishEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -26,6 +28,7 @@ import com.gmail.nossr50.datatypes.McMMOPlayer;
import com.gmail.nossr50.datatypes.PlayerProfile; import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.party.Party; import com.gmail.nossr50.party.Party;
import com.gmail.nossr50.party.ShareHandler;
import com.gmail.nossr50.skills.fishing.Fishing; import com.gmail.nossr50.skills.fishing.Fishing;
import com.gmail.nossr50.skills.herbalism.Herbalism; import com.gmail.nossr50.skills.herbalism.Herbalism;
import com.gmail.nossr50.skills.mining.BlastMining; import com.gmail.nossr50.skills.mining.BlastMining;
@ -37,6 +40,7 @@ import com.gmail.nossr50.skills.taming.TamingManager;
import com.gmail.nossr50.skills.utilities.SkillTools; import com.gmail.nossr50.skills.utilities.SkillTools;
import com.gmail.nossr50.skills.utilities.SkillType; import com.gmail.nossr50.skills.utilities.SkillType;
import com.gmail.nossr50.util.BlockChecks; import com.gmail.nossr50.util.BlockChecks;
import com.gmail.nossr50.util.ItemChecks;
import com.gmail.nossr50.util.Motd; import com.gmail.nossr50.util.Motd;
import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Permissions;
@ -110,6 +114,27 @@ public class PlayerListener implements Listener {
} }
} }
/**
* Monitor PlayerPickupItem events.
*
* @param event The event to watch
*/
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerPickupItem(PlayerPickupItemEvent event) {
Player player = event.getPlayer();
Item item = event.getItem();
if (Misc.isNPCPlayer(player)) {
return;
}
McMMOPlayer mcMMOPlayer = Users.getPlayer(player);
if (mcMMOPlayer.inParty() && ItemChecks.isShareable(item.getItemStack())) {
ShareHandler.handleItemShare(event, mcMMOPlayer);
}
}
/** /**
* Monitor PlayerQuit events. * Monitor PlayerQuit events.
* *

View File

@ -14,6 +14,7 @@ public class Party {
private String password; private String password;
private boolean locked; private boolean locked;
private ShareHandler.ShareMode xpShareMode; private ShareHandler.ShareMode xpShareMode;
private ShareHandler.ShareMode itemShareMode;
public List<String> getMembers() { public List<String> getMembers() {
return members; return members;
@ -71,4 +72,12 @@ public class Party {
public ShareHandler.ShareMode getXpShareMode() { public ShareHandler.ShareMode getXpShareMode() {
return xpShareMode; return xpShareMode;
} }
public void setItemShareMode(ShareHandler.ShareMode itemShareMode) {
this.itemShareMode = itemShareMode;
}
public ShareHandler.ShareMode getItemShareMode() {
return itemShareMode;
}
} }

View File

@ -475,6 +475,7 @@ public final class PartyManager {
party.setPassword(partiesFile.getString(partyName + ".Password")); party.setPassword(partiesFile.getString(partyName + ".Password"));
party.setLocked(partiesFile.getBoolean(partyName + ".Locked")); party.setLocked(partiesFile.getBoolean(partyName + ".Locked"));
party.setXpShareMode(ShareHandler.ShareMode.getFromString(partiesFile.getString(partyName + ".ExpShareMode"))); party.setXpShareMode(ShareHandler.ShareMode.getFromString(partiesFile.getString(partyName + ".ExpShareMode")));
party.setItemShareMode(ShareHandler.ShareMode.getFromString(partiesFile.getString(partyName + ".ItemShareMode")));
party.getMembers().addAll(partiesFile.getStringList(partyName + ".Members")); party.getMembers().addAll(partiesFile.getStringList(partyName + ".Members"));
parties.add(party); parties.add(party);
@ -500,6 +501,7 @@ public final class PartyManager {
partiesFile.set(partyName + ".Password", party.getPassword()); partiesFile.set(partyName + ".Password", party.getPassword());
partiesFile.set(partyName + ".Locked", party.isLocked()); partiesFile.set(partyName + ".Locked", party.isLocked());
partiesFile.set(partyName + ".ExpShareMode", party.getXpShareMode().toString()); partiesFile.set(partyName + ".ExpShareMode", party.getXpShareMode().toString());
partiesFile.set(partyName + ".ItemShareMode", party.getItemShareMode().toString());
partiesFile.set(partyName + ".Members", party.getMembers()); partiesFile.set(partyName + ".Members", party.getMembers());
try { try {

View File

@ -2,17 +2,22 @@ package com.gmail.nossr50.party;
import java.util.List; import java.util.List;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.inventory.ItemStack;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.McMMOPlayer; import com.gmail.nossr50.datatypes.McMMOPlayer;
import com.gmail.nossr50.skills.utilities.SkillType; import com.gmail.nossr50.skills.utilities.SkillType;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Users; import com.gmail.nossr50.util.Users;
public final class ShareHandler { public final class ShareHandler {
public enum ShareMode { public enum ShareMode {
NONE, NONE,
EQUAL; EQUAL,
RANDOM;
public static ShareMode getFromString(String string) { public static ShareMode getFromString(String string) {
try { try {
@ -69,5 +74,56 @@ public final class ShareHandler {
return false; return false;
} }
} }
/**
* 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
*/
public static boolean handleItemShare(PlayerPickupItemEvent event, McMMOPlayer mcMMOPlayer) {
Item item = event.getItem();
ItemStack itemStack = item.getItemStack();
Party party = mcMMOPlayer.getParty();
switch (party.getItemShareMode()) {
case EQUAL:
return false;
case RANDOM:
Player player = mcMMOPlayer.getPlayer();
List<Player> nearMembers = PartyManager.getNearMembers(player, party, Config.getInstance().getPartyShareRange());
if (nearMembers.isEmpty()) {
return false;
}
int partySize = nearMembers.size() + 1;
event.setCancelled(true);
item.remove();
Player targetPlayer;
ItemStack newStack = itemStack.clone();
newStack.setAmount(1);
//TODO Improve this, if possible make this faster.
for (int i = 0; i < itemStack.getAmount(); i++) {
int randomMember = Misc.getRandom().nextInt(partySize);
if (randomMember >= nearMembers.size()) {
targetPlayer = player;
} else {
targetPlayer = nearMembers.get(randomMember);
}
targetPlayer.getInventory().addItem(newStack);
targetPlayer.updateInventory();
}
return true;
case NONE:
default:
return false;
}
}
} }

View File

@ -28,6 +28,7 @@ public class PartyCommand implements CommandExecutor {
private CommandExecutor partyCreateCommand = new PartyCreateCommand(); private CommandExecutor partyCreateCommand = new PartyCreateCommand();
private CommandExecutor partyQuitCommand = new PartyQuitCommand(); private CommandExecutor partyQuitCommand = new PartyQuitCommand();
private CommandExecutor partyExpShareCommand = new PartyExpShareCommand(); private CommandExecutor partyExpShareCommand = new PartyExpShareCommand();
private CommandExecutor partyItemShareCommand = new PartyItemShareCommand();
private CommandExecutor partyInviteCommand = new PartyInviteCommand(); private CommandExecutor partyInviteCommand = new PartyInviteCommand();
@Override @Override
@ -72,8 +73,8 @@ public class PartyCommand implements CommandExecutor {
else if (args[0].equalsIgnoreCase("expshare") || args[0].equalsIgnoreCase("xpshare") || args[0].equalsIgnoreCase("sharexp") || args[0].equalsIgnoreCase("shareexp")) { else if (args[0].equalsIgnoreCase("expshare") || args[0].equalsIgnoreCase("xpshare") || args[0].equalsIgnoreCase("sharexp") || args[0].equalsIgnoreCase("shareexp")) {
return partyExpShareCommand.onCommand(sender, command, label, args); return partyExpShareCommand.onCommand(sender, command, label, args);
} }
else if (args[0].equalsIgnoreCase("itemshare")) { else if (args[0].equalsIgnoreCase("itemshare") || args[0].equalsIgnoreCase("shareitem") || args[0].equalsIgnoreCase("shareitems")) {
return shareItem(); return partyItemShareCommand.onCommand(sender, command, label, args);
} }
else if (args[0].equalsIgnoreCase("invite")) { else if (args[0].equalsIgnoreCase("invite")) {
return partyInviteCommand.onCommand(sender, command, label, args); return partyInviteCommand.onCommand(sender, command, label, args);
@ -125,7 +126,6 @@ public class PartyCommand implements CommandExecutor {
String ItemShare = ""; String ItemShare = "";
String ExpShare = ""; String ExpShare = "";
String Split = ""; String Split = "";
String itemShareMode = "NONE";
for (String otherPlayerName : party.getMembers()) { for (String otherPlayerName : party.getMembers()) {
if (leader.equals(otherPlayerName)) { if (leader.equals(otherPlayerName)) {
@ -156,7 +156,7 @@ public class PartyCommand implements CommandExecutor {
} }
if (itemShareEnabled) { if (itemShareEnabled) {
ItemShare = LocaleLoader.getString("Commands.Party.ItemShare", itemShareMode); ItemShare = LocaleLoader.getString("Commands.Party.ItemShare", party.getItemShareMode().toString());
} }
if (xpShareEnabled && itemShareEnabled) { if (xpShareEnabled && itemShareEnabled) {
@ -177,10 +177,6 @@ public class PartyCommand implements CommandExecutor {
return true; return true;
} }
private boolean shareItem() {
return (!CommandHelper.noCommandPermissions(player, "mcmmo.commands.party.itemshare"));
}
private boolean printHelp() { private boolean printHelp() {
player.sendMessage(LocaleLoader.getString("Party.Help.3")); player.sendMessage(LocaleLoader.getString("Party.Help.3"));
player.sendMessage(LocaleLoader.getString("Party.Help.1")); player.sendMessage(LocaleLoader.getString("Party.Help.1"));

View File

@ -0,0 +1,67 @@
package com.gmail.nossr50.party.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.party.Party;
import com.gmail.nossr50.party.ShareHandler;
import com.gmail.nossr50.party.ShareHandler.ShareMode;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.Users;
public class PartyItemShareCommand implements CommandExecutor {
private Player player;
private Party playerParty;
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!Permissions.hasPermission(sender, "mcmmo.commands.party.itemshare")) {
sender.sendMessage(command.getPermissionMessage());
return true;
}
switch (args.length) {
case 2:
player = (Player) sender;
playerParty = Users.getPlayer(player).getParty();
if (!playerParty.getLeader().equals(player.getName())) {
sender.sendMessage(LocaleLoader.getString("Party.NotOwner"));
return true;
}
if (args[1].equalsIgnoreCase("none") || args[1].equalsIgnoreCase("off") || args[1].equalsIgnoreCase("false")) {
handleChangingShareMode(ShareMode.NONE);
}
// else if (args[1].equalsIgnoreCase("equal") || args[1].equalsIgnoreCase("even")) {
// handleChangingShareMode(ShareMode.EQUAL);
// }
else if (args[1].equalsIgnoreCase("random")) {
handleChangingShareMode(ShareMode.RANDOM);
}
else {
// sender.sendMessage(LocaleLoader.getString("Commands.Usage.2", "party", "itemshare", "[NONE | EQUAL | RANDOM]"));
sender.sendMessage(LocaleLoader.getString("Commands.Usage.2", "party", "itemshare", "[NONE | RANDOM]"));
}
return true;
default:
// sender.sendMessage(LocaleLoader.getString("Commands.Usage.2", "party", "itemshare", "[NONE | EQUAL | RANDOM]"));
sender.sendMessage(LocaleLoader.getString("Commands.Usage.2", "party", "itemshare", "[NONE | RANDOM]"));
return true;
}
}
private void handleChangingShareMode(ShareHandler.ShareMode mode) {
playerParty.setItemShareMode(mode);
for (Player member : playerParty.getOnlineMembers()) {
member.sendMessage(LocaleLoader.getString("Commands.Party.SetSharing", LocaleLoader.getString("Party.ShareType.Item"), LocaleLoader.getString("Party.ShareMode." + Misc.getCapitalized(mode.toString()))));
}
}
}

View File

@ -516,4 +516,117 @@ public class ItemChecks {
return false; return false;
} }
} }
/**
* Check if an item is sharable.
*
* @param item Item that will get shared
* @return True if the item can be shared.
*/
public static boolean isShareable(ItemStack is) {
return isMiningDrop(is) || isWoodcuttingDrop(is) || isMobDrop(is) || isHerbalismDrop(is);
}
/**
* Checks to see if an item is a mining drop.
*
* @param is Item to check
* @return true if the item is a mining drop, false otherwise
*/
public static boolean isMiningDrop(ItemStack is) {
switch (is.getType()) {
case COAL:
case COAL_ORE:
case DIAMOND:
case DIAMOND_ORE:
case EMERALD:
case EMERALD_ORE:
case GOLD_ORE:
case IRON_ORE:
case LAPIS_ORE:
case REDSTONE_ORE:
case REDSTONE:
case GLOWSTONE_DUST:
return true;
case INK_SACK:
if (is.getData().getData() == DyeColor.BLUE.getDyeData()) {
return true;
}
default:
return false;
}
}
public static boolean isHerbalismDrop(ItemStack is) {
switch (is.getType()) {
case WHEAT:
case SEEDS:
case CARROT:
case POTATO:
case COCOA:
case NETHER_WARTS:
case APPLE:
case BROWN_MUSHROOM:
case RED_MUSHROOM:
case RED_ROSE:
case YELLOW_FLOWER:
case CACTUS:
case SUGAR_CANE:
//TODO Add the rest
return true;
default:
return false;
}
}
public static boolean isMobDrop(ItemStack is) {
switch (is.getType()) {
case STRING:
case FEATHER:
case RAW_CHICKEN:
case COOKED_CHICKEN:
case LEATHER:
case RAW_BEEF:
case COOKED_BEEF:
case PORK:
case GRILLED_PORK:
case WOOL:
case RED_ROSE:
case IRON_INGOT:
case SNOW_BALL:
case BLAZE_ROD:
case SPIDER_EYE:
case SULPHUR:
case ENDER_PEARL:
case GHAST_TEAR:
case MAGMA_CREAM:
case BONE:
case ARROW:
case SLIME_BALL:
case NETHER_STAR:
case COAL:
case ROTTEN_FLESH:
case GOLD_NUGGET:
case EGG:
return true;
default:
return false;
}
}
public static boolean isWoodcuttingDrop(ItemStack is) {
switch (is.getType()) {
case LOG:
case LEAVES:
case SAPLING:
return true;
default:
return false;
}
}
} }