Add support for whether a player owns a given pet

This commit is contained in:
Spencer 2018-01-06 16:47:07 -05:00 committed by Alexander Meech
parent 9f134b5772
commit 14daa939e3
2 changed files with 82 additions and 30 deletions

View File

@ -8,8 +8,12 @@ import java.util.function.Consumer;
import org.bukkit.entity.Player;
import com.google.gson.Gson;
import mineplex.core.account.CoreClient;
import mineplex.core.common.util.UtilServer;
import mineplex.core.pet.PetClient;
import mineplex.core.pet.repository.token.ClientPetTokenWrapper;
import mineplex.core.powerplayclub.PowerPlayData;
import mineplex.core.shop.ShopBase;
import mineplex.core.shop.page.ShopPageBase;
@ -21,6 +25,7 @@ public class SupportShop extends ShopBase<CustomerSupport>
{
private LinkedTemporaryItem<Integer, List<BonusEntry>> _bonusLog;
private LinkedTemporaryItem<Integer, PowerPlayData> _powerPlayData;
private LinkedTemporaryItem<Integer, PetClient> _petClients;
public SupportShop(CustomerSupport plugin)
{
@ -28,6 +33,7 @@ public class SupportShop extends ShopBase<CustomerSupport>
_bonusLog = new LinkedTemporaryItem<>();
_powerPlayData = new LinkedTemporaryItem<>();
_petClients = new LinkedTemporaryItem<>();
}
public void handleOpen(Player caller, CoreClient target)
@ -36,8 +42,12 @@ public class SupportShop extends ShopBase<CustomerSupport>
// button will just be there since the HomePage
// knows not to put it in.
loadBonusLog(caller, target.getAccountId(), (bS) ->
loadPowerPlay(caller, target.getAccountId(), (pS) ->
openPageAsync(caller, new SupportHomePage(getPlugin(), this, caller, target))));
loadPowerPlay(caller, target.getAccountId(), (ppcS) ->
loadPetClient(caller, target, (petS) ->
openPageAsync(caller, new SupportHomePage(getPlugin(), this, caller, target))
)
)
);
}
public void openPageAsync(Player player, SupportPage page)
@ -92,16 +102,34 @@ public class SupportShop extends ShopBase<CustomerSupport>
});
}
public void loadPetClient(Player caller, CoreClient client, Consumer<Boolean> callback)
{
Gson gson = new Gson();
ClientPetTokenWrapper token;
String response = getPlugin().getClientManager().getRepository().getClientByUUID(client.getUniqueId());
token = gson.fromJson(response, ClientPetTokenWrapper.class);
PetClient petClient = new PetClient();
petClient.load(token.DonorToken);
_petClients.put(caller, client.getAccountId(), petClient);
callback.accept(true);
}
public Map<Integer, List<BonusEntry>> getBonusLog() { return _bonusLog.getPrimaryMap(); }
public Map<Integer, PowerPlayData> getPowerPlayData() { return _powerPlayData.getPrimaryMap(); }
public Map<Integer, PetClient> getPetClients() { return _petClients.getPrimaryMap(); }
@Override
protected void closeShopForPlayer(Player player)
{
super.closeShopForPlayer(player);
_bonusLog.remove(player);
_powerPlayData.remove(player);
_petClients.remove(player);
}
@Override

View File

@ -10,7 +10,6 @@ import org.bukkit.inventory.meta.ItemMeta;
import mineplex.core.account.CoreClient;
import mineplex.core.common.util.C;
import mineplex.core.itemstack.ItemBuilder;
import mineplex.core.pet.PetType;
import mineplex.core.pet.repository.token.PetChangeToken;
import mineplex.core.shop.item.ShopItem;
@ -32,16 +31,28 @@ public class SupportPetPage extends SupportPage
buildPage();
}
private ItemStack getPetIcon(PetType petType)
private ItemStack getPetIcon(PetType petType, boolean owned)
{
ItemStack item = petType.getDisplayItem().clone();
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(C.cGreenB + petType.getName());
meta.setLore(Arrays.asList(
C.mBody + "Click to give this",
C.mBody + "player a " + C.cYellow + petType.getName() + " Pet"
));
if (owned)
{
meta.setDisplayName(C.cRedB + petType.getName());
meta.setLore(Arrays.asList(
C.cYellow + _target.getName() + C.mBody + " already",
C.mBody + "owns a " + C.cYellow + petType.getName() + " Pet" + C.mBody + "!"
));
}
else
{
meta.setDisplayName(C.cGreenB + petType.getName());
meta.setLore(Arrays.asList(
C.mBody + "Click to give this",
C.mBody + "player a " + C.cYellow + petType.getName() + " Pet"
));
}
item.setItemMeta(meta);
@ -50,30 +61,43 @@ public class SupportPetPage extends SupportPage
private void addPet(PetType petType, int slot)
{
addButton(slot, getPetIcon(petType), (p, c) ->
// Owns the pet
if (getShop().getPetClients().get(_target.getAccountId()).getPets().containsKey(petType))
{
PetChangeToken token = new PetChangeToken();
token.AccountId = _target.getAccountId();
token.Name = _target.getName();
token.PetType = petType.toString();
token.PetName = petType.getName();
getPlugin().getPetRepository().AddPet(token);
getPlugin().getInventoryManager().addItemToInventoryForOffline((success) ->
addItem(slot, getPetIcon(petType, true));
addGlow(slot);
}
else
{
addButton(slot, getPetIcon(petType, false), (p, c) ->
{
if (success)
PetChangeToken token = new PetChangeToken();
token.AccountId = _target.getAccountId();
token.Name = _target.getName();
token.PetType = petType.toString();
token.PetName = petType.getName();
getPlugin().getPetRepository().AddPet(token);
getPlugin().getInventoryManager().addItemToInventoryForOffline((success) ->
{
playSuccess();
message("Successfully gave " + C.cYellow + petType.getName() + " Pet" + C.mBody + " to " + C.cYellow + _target.getName());
}
else
{
playFail();
message("Unable to give " + C.cYellow + petType.getName() + " Pet" + C.mBody + " to " + C.cYellow + _target.getName() + C.mBody + ", please try again later.");
}
}, _target.getAccountId(), petType.toString(), 1);
});
if (success)
{
getShop().loadPetClient(getPlayer(), _target, (petSuccess) ->
{
playSuccess();
message("Successfully gave " + C.cYellow + petType.getName() + " Pet" + C.mBody + " to " + C.cYellow + _target.getName());
refresh();
});
}
else
{
playFail();
message("Unable to give " + C.cYellow + petType.getName() + " Pet" + C.mBody + " to " + C.cYellow + _target.getName() + C.mBody + ", please try again later.");
}
}, _target.getAccountId(), petType.toString(), 1);
});
}
}
private LinkedList<PetType> getPetTypeList()