-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Patch du bug du /settings et mailbox + cache (#457)
*Avez vous lu le [Code de Conduite](https://github.com/Margouta/PluginOpenMC/blob/main/CODE_OF_CONDUCT.md)?*: oui *Votre code se compile t-il en local ?*: oui *Avez-vous supprimez les imports inutilisés ?*: oui ## Décrivez vos changements *Clairement et avec des screenshots si nécessaires* - Ecriture des paramètres automatique lors de la connexion pour ne pas retourner _null_ lors de l'ouverture des paramètres - Ajout d'un cache pour éviter les requêtes SQL permanentes - Réparation du /players - Réparation de la mailbox - Nouveau namespace pour le /settings : ### **Pensez à retirer le namespace *omc_settings*, s'il existe et de mettre celui là** [settings.zip](https://github.com/user-attachments/files/17487001/settings.zip)
- Loading branch information
Showing
18 changed files
with
572 additions
and
569 deletions.
There are no files selected for viewing
150 changes: 73 additions & 77 deletions
150
src/main/java/fr/communaywen/core/AywenCraftPlugin.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
219 changes: 99 additions & 120 deletions
219
src/main/java/fr/communaywen/core/mailboxes/menu/letter/SendingLetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,139 +1,118 @@ | ||
package fr.communaywen.core.mailboxes.menu.letter; | ||
|
||
import fr.communaywen.core.AywenCraftPlugin; | ||
import fr.communaywen.core.friends.FriendsManager; | ||
import fr.communaywen.core.mailboxes.MailboxManager; | ||
import fr.communaywen.core.mailboxes.utils.MailboxInv; | ||
import fr.communaywen.core.mailboxes.utils.MailboxMenuManager; | ||
import fr.communaywen.core.teams.TeamManager; | ||
import fr.communaywen.core.settings.SettingsCache; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.OfflinePlayer; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.inventory.ItemStack; | ||
|
||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import static fr.communaywen.core.mailboxes.utils.MailboxMenuManager.*; | ||
import static fr.communaywen.core.mailboxes.utils.MailboxUtils.getHead; | ||
import static fr.communaywen.core.mailboxes.utils.MailboxUtils.sendFailureMessage; | ||
|
||
public class SendingLetter extends MailboxInv { | ||
private final static String INV_NAME = "\uF990\uE003"; | ||
private final OfflinePlayer receiver; | ||
private final AywenCraftPlugin plugin; | ||
private final TeamManager teamManager; | ||
private List<String> playerFriends = new ArrayList<>(); | ||
|
||
int mail_accept; | ||
|
||
public SendingLetter(Player player, OfflinePlayer receiver, AywenCraftPlugin plugin) throws SQLException { | ||
super(player); | ||
this.receiver = receiver; | ||
this.plugin = plugin; | ||
this.teamManager = plugin.getManagers().getTeamManager(); | ||
inventory = Bukkit.createInventory(this, 54, MailboxMenuManager.getInvTitle(INV_NAME)); | ||
inventory.setItem(49, getHead(receiver)); | ||
inventory.setItem(45, homeBtn()); | ||
inventory.setItem(48, sendBtn()); | ||
inventory.setItem(50, cancelBtn()); | ||
|
||
for (int i = 0; i < 9; i++) inventory.setItem(i, transparentItem()); | ||
|
||
FriendsManager friendsManager = new FriendsManager(plugin.getManagers().getDatabaseManager(), this.plugin); | ||
friendsManager.getFriendsAsync(player.getName()).thenAccept(friends -> { | ||
this.playerFriends = friends; | ||
plugin.getLogger().info("Amis chargés pour " + player.getName()); | ||
}).exceptionally(ex -> { | ||
plugin.getLogger().severe("Erreur lors du chargement des amis : " + ex.getMessage()); | ||
return null; | ||
}); | ||
|
||
this.mail_accept = plugin.getManagers().getSettingsManager().findPlayerSettingsByUUID(Objects.requireNonNull(receiver.getPlayer())).mail_accept(); | ||
} | ||
|
||
@Override | ||
public void openInventory() { | ||
player.openInventory(this.inventory); | ||
} | ||
|
||
public ItemStack[] getItems() { | ||
List<ItemStack> itemsList = new ArrayList<>(27); | ||
for (int slot = 9; slot < 36; slot++) { | ||
ItemStack item = inventory.getItem(slot); | ||
if (item != null && !item.getType().isAir()) itemsList.add(item); | ||
} | ||
return itemsList.toArray(new ItemStack[0]); | ||
} | ||
|
||
public void sendLetter() { | ||
ItemStack[] items = getItems(); | ||
inventory.clear(); | ||
player.closeInventory(); | ||
if (items.length == 0) { | ||
sendFailureMessage(player, "Vous ne pouvez pas envoyer de lettre vide"); | ||
return; | ||
} | ||
|
||
plugin.getLogger().info("Mail Accept: " + mail_accept); | ||
|
||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { | ||
switch (mail_accept) { | ||
case 0: | ||
Bukkit.getScheduler().runTask(plugin, () -> sendFailureMessage(player, "Ce joueur n'accepte pas les lettres")); | ||
break; | ||
|
||
case 1: | ||
if (!playerFriends.contains(receiver.getName())) { | ||
Bukkit.getScheduler().runTask(plugin, () -> sendFailureMessage(player, "Ce joueur n'accepte pas les lettres")); | ||
} else { | ||
sendMailItems(player, receiver, items); | ||
} | ||
break; | ||
|
||
case 2: | ||
String playerTeamName = teamManager.getTeamByPlayer(player.getUniqueId()).getName(); | ||
String receiverTeamName = teamManager.getTeamByPlayer(receiver.getUniqueId()).getName(); | ||
plugin.getLogger().info("Nom de l'équipe du joueur : " + playerTeamName); | ||
plugin.getLogger().info("Nom de l'équipe du destinataire : " + receiverTeamName); | ||
|
||
if (!playerTeamName.equals(receiverTeamName)) { | ||
Bukkit.getScheduler().runTask(plugin, () -> sendFailureMessage(player, "Ce joueur n'accepte pas les lettres")); | ||
} else { | ||
sendMailItems(player, receiver, items); | ||
} | ||
break; | ||
|
||
default: | ||
sendMailItems(player, receiver, items); | ||
break; | ||
} | ||
}); | ||
} | ||
|
||
private void sendMailItems(Player player, OfflinePlayer receiver, ItemStack[] items) { | ||
Bukkit.getScheduler().runTask(plugin, () -> { | ||
if (!MailboxManager.sendItems(player, receiver, items)) { | ||
MailboxManager.givePlayerItems(player, items); | ||
} | ||
}); | ||
} | ||
|
||
public void giveItems() { | ||
MailboxManager.givePlayerItems(player, getItems()); | ||
} | ||
|
||
public boolean noSpace(ItemStack item) { | ||
if (item == null || item.getType().isAir()) return false; | ||
int size = item.getAmount(); | ||
for (int slot = 9; slot < 36; slot++) { | ||
ItemStack targetItem = inventory.getItem(slot); | ||
if (targetItem == null || targetItem.getType().isAir()) return false; | ||
if (targetItem.isSimilar(item)) size -= targetItem.getMaxStackSize() - targetItem.getAmount(); | ||
if (size <= 0) return false; | ||
} | ||
return true; | ||
} | ||
} | ||
private final static String INV_NAME = "\uF990\uE003"; | ||
private final OfflinePlayer receiver; | ||
private final AywenCraftPlugin plugin; | ||
// private final TeamManager teamManager; | ||
// private final List<String> playerFriends; -> Temporaire | ||
|
||
int mailAccept; | ||
|
||
public SendingLetter(Player player, OfflinePlayer receiver, AywenCraftPlugin plugin) { | ||
super(player); | ||
this.receiver = receiver; | ||
this.plugin = plugin; | ||
// this.teamManager = plugin.getManagers().getTeamManager(); | ||
// FriendsManager friendsManager = plugin.getManagers().getFriendsManager(); | ||
// playerFriends = (List<String>) friendsManager.getFriendsAsync(player.getName()); | ||
mailAccept = SettingsCache.settingsMap.get(receiver.getUniqueId().toString()).mailAccept(); | ||
inventory = Bukkit.createInventory(this, 54, MailboxMenuManager.getInvTitle(INV_NAME)); | ||
inventory.setItem(49, getHead(receiver)); | ||
inventory.setItem(45, homeBtn()); | ||
inventory.setItem(48, sendBtn()); | ||
inventory.setItem(50, cancelBtn()); | ||
|
||
for (int i = 0; i < 9; i++) inventory.setItem(i, transparentItem()); | ||
} | ||
|
||
@Override | ||
public void openInventory() { | ||
player.openInventory(inventory); | ||
} | ||
|
||
public ItemStack[] getItems() { | ||
List<ItemStack> itemsList = new ArrayList<>(27); | ||
for (int slot = 9; slot < 36; slot++) { | ||
ItemStack item = inventory.getItem(slot); | ||
if (item != null && ! item.getType().isAir()) itemsList.add(item); | ||
} | ||
return itemsList.toArray(new ItemStack[0]); | ||
} | ||
|
||
public void sendLetter() { | ||
ItemStack[] items = getItems(); | ||
inventory.clear(); | ||
player.closeInventory(); | ||
if (items.length == 0) { | ||
sendFailureMessage(player, "Vous ne pouvez pas envoyer de lettre vide"); | ||
return; | ||
} | ||
plugin.getLogger().info("Mail Accept" + mailAccept); | ||
switch (mailAccept) { | ||
case 0: | ||
sendFailureMessage(player, "Ce joueur n'accepte pas les lettres"); | ||
for (ItemStack item : items) player.getWorld().dropItemNaturally(player.getLocation(), item); | ||
break; | ||
|
||
// case 1: | ||
// if (! playerFriends.contains(receiver.getName())) { | ||
// sendFailureMessage(player, "Ce joueur n'accepte pas les lettres"); | ||
// } else { | ||
// if (! MailboxManager.sendItems(player, receiver, items)) { | ||
// MailboxManager.givePlayerItems(player, items); | ||
// } | ||
// } | ||
// break; | ||
|
||
// case 2: | ||
// String playerTeamName = teamManager.getTeamByPlayer(player.getUniqueId()).getName(); | ||
// String receiverTeamName = teamManager.getTeamByPlayer(receiver.getUniqueId()).getName(); | ||
// if (! playerTeamName.equals(receiverTeamName)) { | ||
// sendFailureMessage(player, "Ce joueur n'accepte pas les lettres"); | ||
// } else { | ||
// if (! MailboxManager.sendItems(player, receiver, items)) { | ||
// MailboxManager.givePlayerItems(player, items); | ||
// } | ||
// } | ||
// break; | ||
|
||
default: | ||
if (! MailboxManager.sendItems(player, receiver, items)) MailboxManager.givePlayerItems(player, items); | ||
break; | ||
} | ||
} | ||
|
||
public void giveItems() { | ||
MailboxManager.givePlayerItems(player, getItems()); | ||
} | ||
|
||
public boolean noSpace(ItemStack item) { | ||
if (item == null || item.getType().isAir()) return false; | ||
int size = item.getAmount(); | ||
for (int slot = 9; slot < 36; slot++) { | ||
ItemStack targetItem = inventory.getItem(slot); | ||
if (targetItem == null || targetItem.getType().isAir()) return false; | ||
if (targetItem.isSimilar(item)) size -= targetItem.getMaxStackSize() - targetItem.getAmount(); | ||
if (size <= 0) return false; | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4
src/main/java/fr/communaywen/core/settings/PlayerSettings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
package fr.communaywen.core.settings; | ||
|
||
public record PlayerSettings(String uuid, int mail_accept, int trade_accept, int tpa_accept) { | ||
|
||
} | ||
public record PlayerSettings(String uuid, int mailAccept, int tradeAccept, int tpaAccept) {} |
10 changes: 10 additions & 0 deletions
10
src/main/java/fr/communaywen/core/settings/SettingsCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package fr.communaywen.core.settings; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class SettingsCache { | ||
|
||
public static Map<String, PlayerSettings> settingsMap = new HashMap<>(); // UUID; PlayerSettings | ||
|
||
} |
Oops, something went wrong.