Skip to content

Commit

Permalink
[Feature] Update des homes + fix de quelque bug + opti (#466)
Browse files Browse the repository at this point in the history
J'ai ajouter des menus custom + des icons au homes pour rendre joli

[namespace.zip](https://github.com/user-attachments/files/17441281/omc_homes.zip)
  • Loading branch information
MathiasDPX authored Oct 22, 2024
2 parents b07a156 + b568049 commit fde4979
Show file tree
Hide file tree
Showing 48 changed files with 800 additions and 254 deletions.
3 changes: 2 additions & 1 deletion src/main/java/fr/communaywen/core/Managers.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void init(AywenCraftPlugin plugin) {
dreamdimManager = new DimensionManager(plugin);
homeManager = new HomeManager(plugin);
quizManager = new QuizManager(plugin, quizzesConfig, contestManager);
economyManager = new EconomyManager(plugin.getDataFolder());
economyManager = new EconomyManager();
friendsManager = new FriendsManager(databaseManager, plugin);
corpseManager = new CorpseManager();
fbeManager = new FallingBlocksExplosionManager();
Expand All @@ -162,6 +162,7 @@ public void init(AywenCraftPlugin plugin) {
homeManager.init();
moonDimManager.init();
homesManagers.loadHomes();
homesManagers.loadHomeLimits();
disabledWorldHome.loadConfig();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/fr/communaywen/core/QuizManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void onPlayerChat(AsyncPlayerChatEvent event) {
}

event.setCancelled(true);
this.plugin.getManagers().getEconomyManager().addBalance(event.getPlayer(), money);
this.plugin.getManagers().getEconomyManager().addBalance(event.getPlayer().getUniqueId(), money);
currentQuiz = null;
this.timeoutExecutor.shutdownNow();
this.timeoutExecutor = Executors.newScheduledThreadPool(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ public void onInventoryClick(InventoryClickEvent inventoryClickEvent) {
return;
} else {
EconomyManager economy = AywenCraftPlugin.getInstance().getManagers().getEconomyManager();
double balance = economy.getBalance(getOwner());
double balance = economy.getBalance(getOwner().getUniqueId());
if(balance < (items.getPrize() * quantity)) {
getOwner().sendMessage(ChatColor.RED + "Vous n'avez pas assez d'argent pour acheter cette item.");
} else {
int maxStackSize = items.getMaxStack();
int totalQuantity = quantity;
Material materials = Material.getMaterial((material == null) ? items.named() : (items.named() + "_" + material));

economy.withdrawBalance(getOwner(), (items.getPrize() * totalQuantity));
economy.withdrawBalance(getOwner().getUniqueId(), (items.getPrize() * totalQuantity));

transactionsManager.addTransaction(new Transaction(
"CONSOLE",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void onInventoryClick(InventoryClickEvent inventoryClickEvent) {}
else totalAmount = items.getPrize() * quantity;

ItemUtils.removeItemsFromInventory(getOwner(), Material.getMaterial(items.named()), quantity);
economy.addBalance(getOwner(), totalAmount);
economy.addBalance(getOwner().getUniqueId(), totalAmount);
getOwner().sendMessage("§aVente confirmée !");
getOwner().sendMessage(" §4- §c" + quantity + " " + items.getName() + " §7pour §a" + String.format("%.2f", totalAmount) + "$");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ public void adminShop(Player player) {
AdminShopMenu menu = new AdminShopMenu(player);
menu.open();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import revxrsal.commands.annotation.Command;
import revxrsal.commands.annotation.Description;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.text.NumberFormat;
import java.util.*;

@Feature("baltop")
@Credit("ddemile")
Expand Down Expand Up @@ -47,7 +47,7 @@ public void onCommand(Player player) {
} else if (Bukkit.getOfflinePlayer(playerBalance.playerId) != null) {
playerName = Bukkit.getOfflinePlayer(playerBalance.playerId).getName();
}
lines.add(MessageFormat.format("{0}. {1}: {2}", getColor(index) + index, ChatColor.GRAY + playerName, ChatColor.GREEN + playerBalance.balance.toString()));
lines.add(MessageFormat.format("{0}. {1}: {2}", getColor(index) + index, ChatColor.GRAY + playerName, ChatColor.GREEN + playerBalance.getFormattedBalance()));

index++;
}
Expand Down Expand Up @@ -84,5 +84,14 @@ public PlayerBalance(UUID playerId, Double balance) {
this.playerId = playerId;
this.balance = balance;
}

public String getFormattedBalance() {
String balance = String.valueOf(this.balance);
Currency currency = Currency.getInstance("EUR");
NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.UK);
formatter.setCurrency(currency);
BigDecimal bd = new BigDecimal(balance);
return formatter.format(bd);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package fr.communaywen.core.commands.economy;

import fr.communaywen.core.AywenCraftPlugin;
import fr.communaywen.core.Managers;
import fr.communaywen.core.credit.Credit;
import fr.communaywen.core.credit.Feature;
import fr.communaywen.core.economy.EconomyManager;
import fr.communaywen.core.utils.Transaction;
import fr.communaywen.core.utils.TransactionsMenu;
import fr.communaywen.core.utils.constant.MessageManager;
import fr.communaywen.core.utils.constant.MessageType;
import fr.communaywen.core.utils.constant.Prefix;
import fr.communaywen.core.utils.database.TransactionsManager;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import revxrsal.commands.annotation.*;
Expand All @@ -31,8 +36,12 @@ public MoneyCommand(AywenCraftPlugin plugin) {
}

@DefaultFor("~")
public void balance(Player player) {
player.sendMessage("Balance: " + economyManager.getBalance(player));
public void balance(Player player, @Default("self") OfflinePlayer target) {
if(target == player) {
MessageManager.sendMessage(player, "§aVotre argent: §e" + economyManager.getFormattedBalance(target.getUniqueId()), Prefix.OPENMC, MessageType.INFO);
return;
}
MessageManager.sendMessage(player, "§aArgent de §2" + target.getName() + "§a: §e" + economyManager.getFormattedBalance(target.getUniqueId()), Prefix.OPENMC, MessageType.INFO);
}

@Subcommand("help")
Expand Down Expand Up @@ -63,7 +72,7 @@ public void history(CommandSender sender, @Optional Player target){
@Description("Transfère de l'argent d'un joueur à un autre.")
public void transfer(Player player, @Named("joueur") Player target, @Named("montant") @Range(min = 1) int amount) {
if(!player.equals(target)) {
if (economyManager.transferBalance(player, target, amount)) {
if (economyManager.transferBalance(player.getUniqueId(), target.getUniqueId(), amount)) {
player.sendMessage("§aVous venez de transférer §e" + amount + "$ §aà §e" + target.getName());
target.sendMessage("§aVous venez de recevoir §e" + amount + "$ §ade la part de §e" + player.getName());

Expand All @@ -85,7 +94,7 @@ public void transfer(Player player, @Named("joueur") Player target, @Named("mont
@Description("Ajoute de l'argent à un joueur")
@CommandPermission("openmc.money.add")
public void add(Player player, @Named("joueur") Player target, @Named("montant") @Range(min = 1) int amount) {
economyManager.addBalance(target, amount);
economyManager.addBalance(target.getUniqueId(), amount);
player.sendMessage("§aVous venez d'ajouter §e" + amount + "$ §aà " + target.getName());
target.sendMessage("§aVous venez de recevoir §e" + amount + "$");
transactionsManager.addTransaction(new Transaction(
Expand All @@ -100,7 +109,7 @@ public void add(Player player, @Named("joueur") Player target, @Named("montant")
@Description("Enlève de l'argent à un joueur")
@CommandPermission("openmc.money.remove")
public void remove(Player player, @Named("joueur") Player target, @Named("montant") @Range(min = 1) int amount) {
economyManager.withdrawBalance(target, amount);
economyManager.withdrawBalance(target.getUniqueId(), amount);
player.sendMessage("§aVous venez d'enlever §e" + amount + "$ §aà " + target.getName());
target.sendMessage("§aVous venez de perdre §e" + amount + "$");
transactionsManager.addTransaction(new Transaction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class PayCommands {
public void payCommands(Player player, @Named("joueur") Player target, @Named("montant") int amount) {
EconomyManager economyManager = AywenCraftPlugin.getInstance().getManagers().getEconomyManager();
if(!player.equals(target)) {
if (economyManager.transferBalance(player, target, amount)) {
if (economyManager.transferBalance(player.getUniqueId(), target.getUniqueId(), amount)) {
player.sendMessage("§aVous venez de transférer §e" + amount + "$ §aà §e" + target.getName());
target.sendMessage("§aVous venez de recevoir §e" + amount + "$ §ade la part de §e" + player.getName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void homes(Player player, @Default("~") @Named("homes") String name) {
return;
}

HomesMenu menu = new HomesMenu(player, homes, target.getName());
HomesMenu menu = new HomesMenu(player, homes, upgradeManager, true);
menu.open();

MessageManager.sendMessage(player, "§aHome de §e" + target.getName() + "§a: " + homes.stream().map(Home::getName).collect(Collectors.joining(", ")), Prefix.HOME, MessageType.INFO);
Expand All @@ -101,13 +101,15 @@ public void homes(Player player, @Default("~") @Named("homes") String name) {
.filter(home -> home.getPlayer().equals(player.getUniqueId().toString()))
.toList();

AywenCraftPlugin.getInstance().getLogger().info("Homes: " + homes);

if(name.equals("~")) {
if(homes.isEmpty()) {
MessageManager.sendMessageType(player, "§cTu n'as pas de home.", Prefix.HOME, MessageType.ERROR, true);
return;
}

HomesMenu menu = new HomesMenu(player, homes, player.getName());
HomesMenu menu = new HomesMenu(player, homes, upgradeManager, false);
menu.open();
} else if(name.equalsIgnoreCase("upgrade")) {
new UpgradeHomesMenu(player, upgradeManager, homesManagers).open();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import fr.communaywen.core.homes.Home;
import fr.communaywen.core.homes.HomesManagers;

import fr.communaywen.core.homes.menu.utils.HomeIcons;
import fr.communaywen.core.homes.menu.utils.HomeMenuUtils;
import fr.communaywen.core.utils.constant.MessageManager;
import fr.communaywen.core.utils.constant.MessageType;
import fr.communaywen.core.utils.constant.Prefix;
Expand Down Expand Up @@ -67,7 +69,7 @@ public void sethome(Player player, @Named("nom") String name) {
int currentHomes = (int) HomesManagers.homes.stream()
.filter(home -> home.getPlayer().equals(player.getUniqueId().toString()))
.count();
int homesLimit = homesManagers.getCurrentHomesLimit(player.getUniqueId());
int homesLimit = homesManagers.getHomeLimit(player.getUniqueId());

if (currentHomes >= homesLimit) {
MessageManager.sendMessageType(player, "§cTu as atteint ta limite de homes. Améliore ta limite pour en définir plus.", Prefix.HOME, MessageType.ERROR, true);
Expand All @@ -85,7 +87,9 @@ public void sethome(Player player, @Named("nom") String name) {
}
}

homesManagers.addHome(new Home(player.getUniqueId().toString(), name, location));
HomeIcons icon = HomeMenuUtils.getDefaultHomeIcon(name);

homesManagers.addHome(new Home(player.getUniqueId().toString(), name, location, icon));
MessageManager.sendMessageType(player, "§aHome §e" + name + " §adéfini avec succès !", Prefix.HOME, MessageType.SUCCESS, true);
}
public static boolean isRegionConflict(Player player, Location location) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ public void transferMoney(Player player, @Named("montant") int amount) {
}
EconomyManager economyManager = AywenCraftPlugin.getInstance().getManagers().getEconomyManager();

if(amount > 0 && economyManager.getBalance(player) >= amount) {
AywenCraftPlugin.getInstance().getManagers().getEconomyManager().withdrawBalance(player, amount);
if(amount > 0 && economyManager.getBalance(player.getUniqueId()) >= amount) {
AywenCraftPlugin.getInstance().getManagers().getEconomyManager().withdrawBalance(player.getUniqueId(), amount);
EconomieTeam.addBalance(team.getName(), amount);
player.sendMessage("§aVous venez de transférer §e" + amount + "$ §adans la banque de votre team.");
} else {
Expand All @@ -256,7 +256,7 @@ public void removeMoney(Player player, @Named("montant") int amount) {
}
double balances = EconomieTeam.getTeamBalances(team.getName());
if(balances >= amount && amount > 0) {
AywenCraftPlugin.getInstance().getManagers().getEconomyManager().addBalance(player, amount);
AywenCraftPlugin.getInstance().getManagers().getEconomyManager().addBalance(player.getUniqueId(), amount);
EconomieTeam.removeBalance(team.getName(), amount);
player.sendMessage("§aVous venez de prendre §e" + amount + "$ §ade la banque de votre team.");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ public class ContestManager extends DatabaseConnector {
private static final Logger log = LoggerFactory.getLogger(ContestManager.class);
FileConfiguration config;
AywenCraftPlugin plugins;
EconomyManager economyManager;

private final ArrayList<String> colorContest = new ArrayList<>();
public ContestManager(AywenCraftPlugin plugin) {
config = plugin.getConfig();
plugins = plugin;
economyManager = AywenCraftPlugin.getInstance().getManagers().getEconomyManager();
colorContest.add("WHITE");
colorContest.add("YELLOW");
colorContest.add("LIGHT_PURPLE");
Expand Down Expand Up @@ -309,7 +311,7 @@ public void initPhase3(JavaPlugin plugin, FileConfiguration eventConfig) {
moneyMax = (int) (moneyMax * multi);

money = giveRandomly(moneyMin, moneyMax);
EconomyManager.addBalanceOffline(player, money);
economyManager.addBalance(player.getUniqueId(), money);

int luckyMin = 3;
int luckyMax = 6;
Expand All @@ -328,7 +330,7 @@ public void initPhase3(JavaPlugin plugin, FileConfiguration eventConfig) {
moneyMax = (int) (moneyMax * multi);

money = giveRandomly(moneyMin, moneyMax);
EconomyManager.addBalanceOffline(player, money);
economyManager.addBalance(player.getUniqueId(), money);

int luckyMin = 1;
int luckyMax = 3;
Expand Down
68 changes: 20 additions & 48 deletions src/main/java/fr/communaywen/core/economy/EconomyData.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,64 +18,36 @@
import java.util.UUID;

public class EconomyData extends DatabaseConnector {
public static void saveBalances(Player player, Map<UUID, Double> balances) {
Bukkit.getScheduler().runTaskAsynchronously(AywenCraftPlugin.getInstance(), () -> {
try {
PreparedStatement statement = connection.prepareStatement("SELECT * FROM economie WHERE player = ?");
statement.setString(1, player.getUniqueId().toString());

ResultSet rs = statement.executeQuery();
PreparedStatement stm;
if (rs.next()) {
stm = connection.prepareStatement("UPDATE economie SET balance = ? WHERE player = ?");
stm.setDouble(1, balances.getOrDefault(player.getUniqueId(), 0.0));
stm.setString(2, player.getUniqueId().toString());
} else {
stm = connection.prepareStatement("INSERT INTO economie (player, balance) VALUES (?, ?)");
stm.setString(1, player.getUniqueId().toString());
stm.setDouble(2, balances.getOrDefault(player.getUniqueId(), 0.0));
}

int affectedRows = stm.executeUpdate();

if (affectedRows == 0) {
throw new SQLException("Saving balance failed, no rows affected.");
}

} catch (SQLException ignored) {
}
});
}

public static void saveBalancesOffline(OfflinePlayer player, Map<UUID, Double> balances) {
public static void saveBalances(UUID player, Map<UUID, Double> balances) {
Bukkit.getScheduler().runTaskAsynchronously(AywenCraftPlugin.getInstance(), () -> {
try {
PreparedStatement statement = connection.prepareStatement("SELECT * FROM economie WHERE player = ?");
statement.setString(1, player.getUniqueId().toString());
try {
PreparedStatement statement = connection.prepareStatement("SELECT * FROM economie WHERE player = ?");
statement.setString(1, player.toString());

ResultSet rs = statement.executeQuery();
PreparedStatement stm;
if (rs.next()) {
stm = connection.prepareStatement("UPDATE economie SET balance = ? WHERE player = ?");
stm.setDouble(1, balances.getOrDefault(player.getUniqueId(), 0.0));
stm.setString(2, player.getUniqueId().toString());
} else {
stm = connection.prepareStatement("INSERT INTO economie (player, balance) VALUES (?, ?)");
stm.setString(1, player.getUniqueId().toString());
stm.setDouble(2, balances.getOrDefault(player.getUniqueId(), 0.0));
}
ResultSet rs = statement.executeQuery();
PreparedStatement stm;
if (rs.next()) {
stm = connection.prepareStatement("UPDATE economie SET balance = ? WHERE player = ?");
stm.setDouble(1, balances.getOrDefault(player, 0.0));
stm.setString(2, player.toString());
} else {
stm = connection.prepareStatement("INSERT INTO economie (player, balance) VALUES (?, ?)");
stm.setString(1, player.toString());
stm.setDouble(2, balances.getOrDefault(player, 0.0));
}
int affectedRows = stm.executeUpdate();

int affectedRows = stm.executeUpdate();

if (affectedRows == 0) {
throw new SQLException("Saving balance failed, no rows affected.");
}
if (affectedRows == 0) {
throw new SQLException("Saving balance failed, no rows affected.");
}

} catch (SQLException ignored) {
}
});
}


public static Map<UUID, Double> loadBalances() {
try {
Map<UUID, Double> balances = new HashMap<>();
Expand Down
Loading

0 comments on commit fde4979

Please sign in to comment.