Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement crystal depositing via Island bank #565

Merged
merged 1 commit into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.iridium.iridiumskyblock.gui.IslandBankGUI;
import lombok.NoArgsConstructor;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.Optional;

Expand Down Expand Up @@ -75,7 +76,53 @@ public double withdraw(Player player, Number amount) {
*/
@Override
public double deposit(Player player, Number amount) {
return 0;
Optional<Island> islandOptional = IridiumSkyblock.getInstance().getUserManager().getUser(player).getIsland();
if (!islandOptional.isPresent()) {
player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().noIsland.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix)));
return 0;
}

int remainingItemAmount = amount.intValue();
int depositAmount = 0;

ItemStack[] contents = player.getInventory().getContents();
for (int i = 0; i < contents.length && remainingItemAmount > 0; i++) {
ItemStack itemStack = contents[i];

int crystalsPerItem = IridiumSkyblock.getInstance().getIslandManager().getIslandCrystals(itemStack);
if (crystalsPerItem == 0) continue;

int itemStackAmount = itemStack.getAmount();
if (itemStackAmount <= remainingItemAmount) {
player.getInventory().setItem(i, null);

depositAmount += itemStackAmount * crystalsPerItem;
remainingItemAmount -= itemStackAmount;
} else {
itemStack.setAmount(itemStack.getAmount() - remainingItemAmount);
player.getInventory().setItem(i, itemStack);

depositAmount += remainingItemAmount * crystalsPerItem;
remainingItemAmount = 0;
}
}

if (depositAmount == 0) {
player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().insufficientFundsToDeposit
.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix))
.replace("%type%", getDisplayName())
);
return 0;
}

IslandBank islandBank = IridiumSkyblock.getInstance().getIslandManager().getIslandBank(islandOptional.get(), this);
islandBank.setNumber(islandBank.getNumber() + depositAmount);
player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().bankDeposited
.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix))
.replace("%amount%", String.valueOf(depositAmount))
.replace("%type%", getDisplayName())
);
return depositAmount;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public double withdraw(Player player, Number amount) {
.replace("%amount%", String.valueOf(experience))
.replace("%type%", getDisplayName())
);
}else{
} else {
player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().insufficientFundsToWithdrew
.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix))
.replace("%type%", getDisplayName())
Expand Down Expand Up @@ -88,7 +88,7 @@ public double deposit(Player player, Number amount) {
.replace("%amount%", String.valueOf(experience))
.replace("%type%", getDisplayName())
);
}else{
} else {
player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().insufficientFundsToDeposit
.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix))
.replace("%type%", getDisplayName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public double deposit(Player player, Number amount) {
.replace("%amount%", String.valueOf(money))
.replace("%type%", getDisplayName())
);
}else{
} else {
player.sendMessage(StringUtils.color(IridiumSkyblock.getInstance().getMessages().insufficientFundsToDeposit
.replace("%prefix%", IridiumSkyblock.getInstance().getConfiguration().prefix))
.replace("%type%", getDisplayName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public class Messages {
public String flightEnabled = "%prefix% &7Island flight enabled.";
public String flightDisabled = "%prefix% &7Island flight disabled.";
public String bankWithdrew = "%prefix% &7You successfully withdrew %amount% %type% from your Island bank.";
public String bankDeposited = "%prefix% &7You successfully deposited %amount% %type% from your Island bank.";
public String bankDeposited = "%prefix% &7You successfully deposited %amount% %type% to your Island bank.";
public String insufficientFundsToWithdrew = "%prefix% &7You do not have enough %type% to withdraw from your Island bank.";
public String insufficientFundsToDeposit = "%prefix% &7You do not have enough %type% to deposit into your Island bank.";
public String blockLimitReached = "%prefix% &7The maximum block limit for %block% (%limit%) has been reached!";
Expand Down