Skip to content

Commit

Permalink
Patch du bug du /settings et mailbox + cache (#457)
Browse files Browse the repository at this point in the history
*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
Xernas78 authored Nov 22, 2024
2 parents 96b099c + b2eb54c commit 77034f7
Show file tree
Hide file tree
Showing 18 changed files with 572 additions and 569 deletions.
150 changes: 73 additions & 77 deletions src/main/java/fr/communaywen/core/AywenCraftPlugin.java

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions src/main/java/fr/communaywen/core/Managers.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
import fr.communaywen.core.corpse.CorpseManager;
import fr.communaywen.core.credit.Credit;
import fr.communaywen.core.credit.FeatureManager;
import fr.communaywen.core.dreamdim.DimensionManager;
import fr.communaywen.core.customitems.managers.CustomItemsManager;
import fr.communaywen.core.dreamdim.DimensionManager;
import fr.communaywen.core.economy.EconomyManager;
import fr.communaywen.core.friends.FriendsManager;
import fr.communaywen.core.guideline.advancements.dream.DreamTabManager;
import fr.communaywen.core.homes.world.DisabledWorldHome;
import fr.communaywen.core.guideline.GuidelineManager;
import fr.communaywen.core.guideline.advancements.dream.DreamTabManager;
import fr.communaywen.core.homes.HomeUpgradeManager;
import fr.communaywen.core.homes.HomesManagers;
import fr.communaywen.core.homes.world.DisabledWorldHome;
import fr.communaywen.core.levels.LevelsDataManager;
import fr.communaywen.core.levels.LevelsManager;
import fr.communaywen.core.luckyblocks.managers.LBPlayerManager;
import fr.communaywen.core.luckyblocks.managers.LuckyBlockManager;
import fr.communaywen.core.managers.LeaderboardManager;
import fr.communaywen.core.personalhome.Home;
import fr.communaywen.core.personalhome.HomeManager;
import fr.communaywen.core.luckyblocks.managers.LBPlayerManager;
import fr.communaywen.core.luckyblocks.managers.LuckyBlockManager;
import fr.communaywen.core.scoreboard.ScoreboardManager;
import fr.communaywen.core.settings.SettingsManager;
import fr.communaywen.core.space.moon.MoonDimensionManager;
Expand All @@ -46,7 +46,7 @@
@Credit("Xernas")
@Getter
public class Managers {

private AywenCraftPlugin plugin;
private JumpManager jumpManager;
private HeadManager headManager;
Expand Down Expand Up @@ -135,7 +135,7 @@ public void init(AywenCraftPlugin plugin) {
guidelineManager = new GuidelineManager(plugin);
contestCache = new ContestCache(plugin);
contestManager = new ContestManager(plugin, contestCache);
this.teamManager = new TeamManager(plugin);
teamManager = new TeamManager(plugin);
scoreboardManager = new ScoreboardManager(plugin);
dreamdimManager = new DimensionManager(plugin);
homeManager = new HomeManager(plugin);
Expand Down Expand Up @@ -165,6 +165,7 @@ public void init(AywenCraftPlugin plugin) {
homesManagers.loadHomes();
homesManagers.loadHomeLimits();
disabledWorldHome.loadConfig();
settingsManager.init();
}

public void cleanup() {
Expand All @@ -181,6 +182,6 @@ public void cleanup() {
corpseManager.removeAll();
teamManager.getTeamCache().saveAllTeamsToDatabase();
disabledWorldHome.saveConfig();

settingsManager.saveSettings();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class AdminShopBuy extends Menu {
BaseItems items;
private final AtomicInteger number;
private final AtomicDouble prize;
private static final int MAX_ITEMS = 255;
private static final int MAX_ITEMS = 256;
private String material = null;

public AdminShopBuy(Player player, BaseItems items) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package fr.communaywen.core.listeners;

import fr.communaywen.core.AywenCraftPlugin;
import fr.communaywen.core.utils.FreezeUtils;
import org.bukkit.BanList;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.PlayerCommandSendEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerTeleportEvent;

import java.util.Collection;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -45,7 +41,7 @@ public void onPlayerQuit(PlayerQuitEvent event) {
public void onPlayerMove(PlayerMoveEvent e) {
Player player = e.getPlayer();
if (AywenCraftPlugin.frozenPlayers.contains(player)) {
e.setTo(e.getFrom());
e.setCancelled(true);
}
}

Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void onPlayerMove(PlayerMoveEvent event) {
int blockZ = to.getBlockZ();


if (blockX != from.getBlockX() || blockZ != from.getBlockZ())
if ((blockX != from.getBlockX() || blockZ != from.getBlockZ()) && !AywenCraftPlugin.frozenPlayers.contains(player))
QuestsManager.manageQuestsPlayer(player.getUniqueId(), QUESTS.WALK_BLOCKS, 1, "Block(s) marché(s)");

// if (blockX == NINJA_JUMP_END.getBlockX() && blockY == NINJA_JUMP_END.getBlockY() && blockZ == NINJA_JUMP_END.getBlockZ())
Expand Down
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 src/main/java/fr/communaywen/core/settings/SettingsCache.java
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

}
Loading

0 comments on commit 77034f7

Please sign in to comment.