Skip to content

Commit

Permalink
Implement crystal depositing via Island bank (#565)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlsf authored Jan 17, 2022
1 parent e807a45 commit 1c5f154
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
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

0 comments on commit 1c5f154

Please sign in to comment.