diff --git a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java index de603d9a2..90931c866 100644 --- a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java @@ -3,6 +3,7 @@ package com.gmail.nossr50.listeners; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.entity.Entity; +import org.bukkit.entity.Item; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; 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.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerPickupItemEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerRespawnEvent; 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.locale.LocaleLoader; import com.gmail.nossr50.party.Party; +import com.gmail.nossr50.party.ShareHandler; import com.gmail.nossr50.skills.fishing.Fishing; import com.gmail.nossr50.skills.herbalism.Herbalism; 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.SkillType; import com.gmail.nossr50.util.BlockChecks; +import com.gmail.nossr50.util.ItemChecks; import com.gmail.nossr50.util.Motd; import com.gmail.nossr50.util.Misc; 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. * diff --git a/src/main/java/com/gmail/nossr50/party/Party.java b/src/main/java/com/gmail/nossr50/party/Party.java index 170d7201f..9998b5c72 100644 --- a/src/main/java/com/gmail/nossr50/party/Party.java +++ b/src/main/java/com/gmail/nossr50/party/Party.java @@ -14,6 +14,7 @@ public class Party { private String password; private boolean locked; private ShareHandler.ShareMode xpShareMode; + private ShareHandler.ShareMode itemShareMode; public List getMembers() { return members; @@ -71,4 +72,12 @@ public class Party { public ShareHandler.ShareMode getXpShareMode() { return xpShareMode; } + + public void setItemShareMode(ShareHandler.ShareMode itemShareMode) { + this.itemShareMode = itemShareMode; + } + + public ShareHandler.ShareMode getItemShareMode() { + return itemShareMode; + } } diff --git a/src/main/java/com/gmail/nossr50/party/PartyManager.java b/src/main/java/com/gmail/nossr50/party/PartyManager.java index 56878c3f1..18ba78880 100644 --- a/src/main/java/com/gmail/nossr50/party/PartyManager.java +++ b/src/main/java/com/gmail/nossr50/party/PartyManager.java @@ -475,6 +475,7 @@ public final class PartyManager { party.setPassword(partiesFile.getString(partyName + ".Password")); party.setLocked(partiesFile.getBoolean(partyName + ".Locked")); 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")); parties.add(party); @@ -500,6 +501,7 @@ public final class PartyManager { partiesFile.set(partyName + ".Password", party.getPassword()); partiesFile.set(partyName + ".Locked", party.isLocked()); partiesFile.set(partyName + ".ExpShareMode", party.getXpShareMode().toString()); + partiesFile.set(partyName + ".ItemShareMode", party.getItemShareMode().toString()); partiesFile.set(partyName + ".Members", party.getMembers()); try { diff --git a/src/main/java/com/gmail/nossr50/party/ShareHandler.java b/src/main/java/com/gmail/nossr50/party/ShareHandler.java index 15a7eac08..41431780a 100644 --- a/src/main/java/com/gmail/nossr50/party/ShareHandler.java +++ b/src/main/java/com/gmail/nossr50/party/ShareHandler.java @@ -2,17 +2,22 @@ package com.gmail.nossr50.party; import java.util.List; +import org.bukkit.entity.Item; 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.datatypes.McMMOPlayer; import com.gmail.nossr50.skills.utilities.SkillType; +import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Users; public final class ShareHandler { public enum ShareMode { NONE, - EQUAL; + EQUAL, + RANDOM; public static ShareMode getFromString(String string) { try { @@ -69,5 +74,56 @@ public final class ShareHandler { 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 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; + } + } } diff --git a/src/main/java/com/gmail/nossr50/party/commands/PartyCommand.java b/src/main/java/com/gmail/nossr50/party/commands/PartyCommand.java index 72287ddfc..fbb1183c5 100644 --- a/src/main/java/com/gmail/nossr50/party/commands/PartyCommand.java +++ b/src/main/java/com/gmail/nossr50/party/commands/PartyCommand.java @@ -28,6 +28,7 @@ public class PartyCommand implements CommandExecutor { private CommandExecutor partyCreateCommand = new PartyCreateCommand(); private CommandExecutor partyQuitCommand = new PartyQuitCommand(); private CommandExecutor partyExpShareCommand = new PartyExpShareCommand(); + private CommandExecutor partyItemShareCommand = new PartyItemShareCommand(); private CommandExecutor partyInviteCommand = new PartyInviteCommand(); @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")) { return partyExpShareCommand.onCommand(sender, command, label, args); } - else if (args[0].equalsIgnoreCase("itemshare")) { - return shareItem(); + else if (args[0].equalsIgnoreCase("itemshare") || args[0].equalsIgnoreCase("shareitem") || args[0].equalsIgnoreCase("shareitems")) { + return partyItemShareCommand.onCommand(sender, command, label, args); } else if (args[0].equalsIgnoreCase("invite")) { return partyInviteCommand.onCommand(sender, command, label, args); @@ -125,7 +126,6 @@ public class PartyCommand implements CommandExecutor { String ItemShare = ""; String ExpShare = ""; String Split = ""; - String itemShareMode = "NONE"; for (String otherPlayerName : party.getMembers()) { if (leader.equals(otherPlayerName)) { @@ -156,7 +156,7 @@ public class PartyCommand implements CommandExecutor { } if (itemShareEnabled) { - ItemShare = LocaleLoader.getString("Commands.Party.ItemShare", itemShareMode); + ItemShare = LocaleLoader.getString("Commands.Party.ItemShare", party.getItemShareMode().toString()); } if (xpShareEnabled && itemShareEnabled) { @@ -177,10 +177,6 @@ public class PartyCommand implements CommandExecutor { return true; } - private boolean shareItem() { - return (!CommandHelper.noCommandPermissions(player, "mcmmo.commands.party.itemshare")); - } - private boolean printHelp() { player.sendMessage(LocaleLoader.getString("Party.Help.3")); player.sendMessage(LocaleLoader.getString("Party.Help.1")); diff --git a/src/main/java/com/gmail/nossr50/party/commands/PartyItemShareCommand.java b/src/main/java/com/gmail/nossr50/party/commands/PartyItemShareCommand.java new file mode 100644 index 000000000..c0f38a98f --- /dev/null +++ b/src/main/java/com/gmail/nossr50/party/commands/PartyItemShareCommand.java @@ -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())))); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/gmail/nossr50/util/ItemChecks.java b/src/main/java/com/gmail/nossr50/util/ItemChecks.java index 6592900df..00305d675 100644 --- a/src/main/java/com/gmail/nossr50/util/ItemChecks.java +++ b/src/main/java/com/gmail/nossr50/util/ItemChecks.java @@ -516,4 +516,117 @@ public class ItemChecks { 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; + } + } }