From 4815bd7d1133fc60a1b3586474486f3852e8643e Mon Sep 17 00:00:00 2001 From: nossr50 Date: Tue, 2 Jul 2019 20:07:52 -0700 Subject: [PATCH] Remove Static Abuse - ShareHandler -> Moved into Party --- .../gmail/nossr50/datatypes/party/Party.java | 29 +++++++++++++++++-- .../nossr50/datatypes/player/McMMOPlayer.java | 6 ++-- .../com/gmail/nossr50/party/ShareHandler.java | 23 ++++++++------- 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/datatypes/party/Party.java b/src/main/java/com/gmail/nossr50/datatypes/party/Party.java index 92cf7e4a3..a40f1af0b 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/party/Party.java +++ b/src/main/java/com/gmail/nossr50/datatypes/party/Party.java @@ -1,6 +1,8 @@ package com.gmail.nossr50.datatypes.party; import com.gmail.nossr50.datatypes.player.McMMOPlayer; +import com.gmail.nossr50.mcMMO; +import com.gmail.nossr50.party.ShareHandler; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.sounds.SoundManager; @@ -18,6 +20,7 @@ import java.util.UUID; import java.util.stream.Collectors; public class Party { + private mcMMO pluginRef; private static final String ONLINE_PLAYER_PREFIX = "⬤"; private static final String OFFLINE_PLAYER_PREFIX = "○"; private final LinkedHashMap members = new LinkedHashMap<>(); @@ -30,6 +33,8 @@ public class Party { private int level; private double xp; + private ShareHandler shareHandler; + private ShareMode xpShareMode = ShareMode.NONE; private ShareMode itemShareMode = ShareMode.NONE; @@ -43,27 +48,41 @@ public class Party { this.name = name; } - public Party(PartyLeader leader, String name) { + public Party(PartyLeader leader, String name, mcMMO pluginRef) { + this.pluginRef = pluginRef; this.leader = leader; this.name = name; this.locked = true; this.level = 0; + + initShareHandler(); } - public Party(PartyLeader leader, String name, String password) { + public Party(PartyLeader leader, String name, String password, mcMMO pluginRef) + { + this.pluginRef = pluginRef; this.leader = leader; this.name = name; this.password = password; this.locked = true; this.level = 0; + + initShareHandler(); } - public Party(PartyLeader leader, String name, String password, boolean locked) { + public Party(PartyLeader leader, String name, String password, boolean locked, mcMMO pluginRef) { + this.pluginRef = pluginRef; this.leader = leader; this.name = name; this.password = password; this.locked = locked; this.level = 0; + + initShareHandler(); + } + + private void initShareHandler() { + shareHandler = new ShareHandler(pluginRef); } public LinkedHashMap getMembers() { @@ -517,4 +536,8 @@ public class Party { return this.getName().equals(other.getName()); } + + public ShareHandler getShareHandler() { + return shareHandler; + } } diff --git a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java index d66610e1d..d1dc65877 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java +++ b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java @@ -56,6 +56,7 @@ import java.util.Set; import java.util.UUID; public class McMMOPlayer { + private final mcMMO pluginRef; private final Map skillManagers = new HashMap<>(); private final Map abilityMode = new HashMap<>(); private final Map abilityInformed = new HashMap<>(); @@ -85,7 +86,8 @@ public class McMMOPlayer { private HashMap personalXPModifiers; private String playerName; - public McMMOPlayer(Player player, PlayerProfile profile) { + public McMMOPlayer(Player player, PlayerProfile profile, mcMMO pluginRef) { + this.pluginRef = pluginRef; this.playerName = player.getName(); UUID uuid = player.getUniqueId(); @@ -531,7 +533,7 @@ public class McMMOPlayer { } // Return if the experience has been shared - if (party != null && ShareHandler.handleXpShare(xp, this, skill, ShareHandler.getSharedXpGainReason(xpGainReason))) { + if (party != null && party.getShareHandler().handleXpShare(xp, this, skill, party.getShareHandler().getSharedXpGainReason(xpGainReason))) { return; } diff --git a/src/main/java/com/gmail/nossr50/party/ShareHandler.java b/src/main/java/com/gmail/nossr50/party/ShareHandler.java index bb51c20c8..3dac4f89f 100644 --- a/src/main/java/com/gmail/nossr50/party/ShareHandler.java +++ b/src/main/java/com/gmail/nossr50/party/ShareHandler.java @@ -7,6 +7,7 @@ import com.gmail.nossr50.datatypes.party.Party; import com.gmail.nossr50.datatypes.party.ShareMode; import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.skills.PrimarySkillType; +import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.player.UserManager; import org.bukkit.Material; @@ -17,7 +18,11 @@ import org.bukkit.inventory.ItemStack; import java.util.List; public final class ShareHandler { - private ShareHandler() { + private mcMMO pluginRef; + private Party party; + + public ShareHandler(mcMMO pluginRef) { + this.pluginRef = pluginRef; } /** @@ -28,9 +33,7 @@ public final class ShareHandler { * @param primarySkillType Skill being used * @return True is the xp has been shared */ - public static boolean handleXpShare(double xp, McMMOPlayer mcMMOPlayer, PrimarySkillType primarySkillType, XPGainReason xpGainReason) { - Party party = mcMMOPlayer.getParty(); - + public boolean handleXpShare(double xp, McMMOPlayer mcMMOPlayer, PrimarySkillType primarySkillType, XPGainReason xpGainReason) { if (party.getXpShareMode() != ShareMode.EQUAL) { return false; } @@ -47,7 +50,7 @@ public final class ShareHandler { double shareBonus = Math.min(pluginRef.getPartyXPShareSettings().getPartyShareXPBonusBase() + (partySize * pluginRef.getPartyXPShareSettings().getPartyShareBonusIncrease()), pluginRef.getPartyXPShareSettings().getPartyShareBonusCap()); - double splitXp = (double) (xp / partySize * shareBonus); + double splitXp = (xp / partySize * shareBonus); for (Player member : nearMembers) { //Profile not loaded @@ -68,7 +71,7 @@ public final class ShareHandler { * @param mcMMOPlayer Player who picked up the item * @return True if the item has been shared */ - public static boolean handleItemShare(Item drop, McMMOPlayer mcMMOPlayer) { + public boolean handleItemShare(Item drop, McMMOPlayer mcMMOPlayer) { ItemStack itemStack = drop.getItemStack(); ItemShareType dropType = ItemShareType.getShareType(itemStack); @@ -76,8 +79,6 @@ public final class ShareHandler { return false; } - Party party = mcMMOPlayer.getParty(); - if (!party.sharingDrops(dropType)) { return false; } @@ -156,14 +157,14 @@ public final class ShareHandler { } } - public static int getItemWeight(Material material) { + private int getItemWeight(Material material) { if (pluginRef.getConfigManager().getConfigParty().getPartyItemShare().getItemShareMap().get(material) == null) return 5; else return pluginRef.getConfigManager().getConfigParty().getPartyItemShare().getItemShareMap().get(material); } - public static XPGainReason getSharedXpGainReason(XPGainReason xpGainReason) { + public XPGainReason getSharedXpGainReason(XPGainReason xpGainReason) { if (xpGainReason == XPGainReason.PVE) { return XPGainReason.SHARED_PVE; } else if (xpGainReason == XPGainReason.PVP) { @@ -173,7 +174,7 @@ public final class ShareHandler { } } - private static void awardDrop(Player winningPlayer, ItemStack drop) { + private void awardDrop(Player winningPlayer, ItemStack drop) { if (winningPlayer.getInventory().addItem(drop).size() != 0) { winningPlayer.getWorld().dropItem(winningPlayer.getLocation(), drop); }