-
Notifications
You must be signed in to change notification settings - Fork 29
Ajout du wrapper OMCPlayer #1343
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
base: master
Are you sure you want to change the base?
Changes from all commits
02aebcb
8a5bb89
7751ac7
d5ec1c8
0d24aba
d0bdde7
20684ac
8ba26ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,235 @@ | ||
| package fr.openmc.api.entity.player; | ||
|
|
||
| import fr.openmc.core.features.city.CityManager; | ||
| import fr.openmc.core.features.economy.EconomyManager; | ||
| import fr.openmc.core.utils.text.messages.MessageType; | ||
| import fr.openmc.core.utils.text.messages.MessagesManager; | ||
| import fr.openmc.core.utils.text.messages.Prefix; | ||
| import lombok.experimental.Delegate; | ||
| import net.kyori.adventure.text.Component; | ||
| import org.bukkit.entity.Player; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| /** | ||
| * Wrapper autour d'un {@link Player} pour les methodes propres a OpenMC | ||
| * (economie, ville, menus...). | ||
| * <p> | ||
| * Exemples : | ||
| * <pre>{@code | ||
| * // Dans une commande Lamp, en sender ou en argument : | ||
| * @Command("balance") | ||
| * public void balance(OMCPlayer player) { | ||
| * player.message().sendInfo(Component.text("Vous avez " + player.getFormattedBalance())); | ||
| * } | ||
| * | ||
| * // Dans un event : | ||
| * OMCPlayer player = OMCPlayer.of(event.getPlayer()); | ||
| * if (player.hasCity()) { ... } | ||
| * }</pre> | ||
| */ | ||
| @SuppressWarnings({"unused", "removal", "deprecation"}) | ||
| public class OMCPlayer implements Player { | ||
|
|
||
| @Delegate(types = Player.class) | ||
| private final Player player; | ||
|
|
||
| private OMCPlayer(Player player) { | ||
| this.player = player; | ||
| } | ||
|
|
||
| /** | ||
| * Enveloppe un {@link Player} dans un {@link OMCPlayer}. | ||
| * Si le joueur est deja un {@link OMCPlayer}, il est retourne tel quel. | ||
| * | ||
| * @param player le joueur a envelopper | ||
| * @return le {@link OMCPlayer} correspondant | ||
| */ | ||
| public static OMCPlayer of(@NotNull Player player) { | ||
| if (player instanceof OMCPlayer omcPlayer) | ||
| return omcPlayer; | ||
| return new OMCPlayer(player); | ||
| } | ||
|
|
||
| /** | ||
| * Return the {@link Player} wrapped by this {@link OMCPlayer}. | ||
| * | ||
| * @return the wrapped {@link Player} | ||
| */ | ||
| @Override | ||
| public @NotNull Player getPlayer() { | ||
| return player; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (obj instanceof OMCPlayer omcPlayer) { | ||
| obj = omcPlayer.getPlayer(); | ||
| } | ||
| return player.equals(obj); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return player.hashCode(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "OMCPlayer{" + | ||
| "username=" + player.getDisplayName() + | ||
| ", uuid=" + player.getUniqueId() + | ||
| "}"; | ||
| } | ||
|
|
||
| private final Message message = new Message(); | ||
| private final Economy economy = new Economy(); | ||
| private final City city = new City(); | ||
|
|
||
| /** | ||
| * Acces au namespace messages du joueur. | ||
| * <pre>{@code | ||
| * player.message().sendSuccess(Component.text("Ville creee !")); | ||
| * player.message().send(msg, Prefix.CITY, MessageType.ERROR); | ||
| * }</pre> | ||
| */ | ||
| public Message message() { | ||
| return message; | ||
| } | ||
|
|
||
| public Economy economy() { | ||
| return economy; | ||
| } | ||
|
|
||
| public City city() { | ||
| return city; | ||
| } | ||
|
|
||
| public class Message { | ||
|
|
||
| public void send(Component message, Prefix prefix, MessageType type, boolean sound) { | ||
| MessagesManager.sendMessage(player, message, prefix, type, sound); | ||
| } | ||
|
|
||
| public void send(Component message, Prefix prefix, MessageType type) { | ||
| send(message, prefix, type, false); | ||
| } | ||
|
|
||
| public void send(Component message, Prefix prefix) { | ||
| send(message, prefix, MessageType.NONE, false); | ||
| } | ||
|
|
||
| public void send(Component message, MessageType type) { | ||
| send(message, Prefix.OPENMC, type, false); | ||
| } | ||
|
|
||
| public void send(Component message) { | ||
| send(message, Prefix.OPENMC, MessageType.NONE, false); | ||
| } | ||
|
|
||
| public void sendSuccess(Component message, boolean sound) { | ||
| send(message, Prefix.OPENMC, MessageType.SUCCESS, sound); | ||
| } | ||
|
|
||
| public void sendSuccess(Component message) { | ||
| sendSuccess(message, true); | ||
| } | ||
|
|
||
| public void sendError(Component message, Prefix prefix, boolean sound) { | ||
| send(message, prefix, MessageType.ERROR, sound); | ||
| } | ||
|
|
||
| public void sendError(Component message, Prefix prefix) { | ||
| sendError(message, prefix, false); | ||
| } | ||
|
|
||
| public void sendError(Component message, boolean sound) { | ||
| sendError(message, Prefix.OPENMC, sound); | ||
| } | ||
|
|
||
| public void sendError(Component message) { | ||
| sendError(message, true); | ||
| } | ||
|
|
||
| public void sendWarning(Component message, boolean sound) { | ||
| send(message, Prefix.OPENMC, MessageType.WARNING, sound); | ||
| } | ||
|
|
||
| public void sendWarning(Component message) { | ||
| sendWarning(message, true); | ||
| } | ||
|
|
||
| public void sendInfo(Component message, Prefix prefix, boolean sound) { | ||
| send(message, prefix, MessageType.INFO, sound); | ||
| } | ||
|
|
||
| public void sendInfo(Component message, Prefix prefix) { | ||
| send(message, prefix, MessageType.INFO, false); | ||
| } | ||
|
|
||
| public void sendInfo(Component message, boolean sound) { | ||
| send(message, Prefix.OPENMC, MessageType.INFO, sound); | ||
| } | ||
|
|
||
| public void sendInfo(Component message) { | ||
| sendInfo(message, true); | ||
| } | ||
| } | ||
|
|
||
| public class Economy { | ||
| /** | ||
| * Recupere la balance du joueur | ||
| * | ||
| * @return la balance du joueur | ||
| */ | ||
| public double getBalance() { | ||
| return EconomyManager.getBalance(getUniqueId()); | ||
| } | ||
|
|
||
| /** | ||
| * Recupere la balance du joueur formatee avec le symbole de la monnaie | ||
| * | ||
| * @return la balance du joueur formatee | ||
| */ | ||
| public String getFormattedBalance() { | ||
| return EconomyManager.getFormattedBalance(getUniqueId()); | ||
| } | ||
|
|
||
| public void addBalance(double amount) { | ||
| EconomyManager.addBalance(getUniqueId(), amount); | ||
| } | ||
|
|
||
| public void addBalance(double amount, @Nullable String reason) { | ||
| EconomyManager.addBalance(getUniqueId(), amount, reason); | ||
| } | ||
|
|
||
| public boolean withdrawBalance(double amount) { | ||
| return EconomyManager.withdrawBalance(getUniqueId(), amount); | ||
| } | ||
|
|
||
| public boolean withdrawBalance(double amount, @Nullable String reason) { | ||
| return EconomyManager.withdrawBalance(getUniqueId(), amount, reason); | ||
| } | ||
|
|
||
| public void setBalance(double amount) { | ||
| EconomyManager.setBalance(getUniqueId(), amount); | ||
| } | ||
|
|
||
| public boolean pay(UUID targetUUID, double amount, @Nullable String reason) { | ||
| return EconomyManager.transferBalance(getUniqueId(), targetUUID, amount, reason); | ||
| } | ||
| } | ||
|
|
||
| public class City { | ||
| @Nullable | ||
| public fr.openmc.core.features.city.City getCity() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mhh y'a pas pourtant plusieures classes City 🙃
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? pas compris ?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ben je crois que une IA a mis le package entier de la classe fr.openmc.core.features.city.models.City
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hein
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pourquoi avoir mis le package entier pour détecter la classe City ??
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. parceque dans ma class OMCPlayer j'ai deja une class City
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. J'aurais pus la rename c'est vrai, mais j'me suis dis ca que maintenant
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh oui tes sous classes |
||
| return CityManager.getCity(player.getUniqueId()); | ||
| } | ||
|
|
||
| public boolean hasCity() { | ||
| return getCity() != null; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package fr.openmc.api.entity.player; | ||
|
|
||
| import org.jetbrains.annotations.NotNull; | ||
| import revxrsal.commands.autocomplete.SuggestionProvider; | ||
| import revxrsal.commands.bukkit.actor.BukkitCommandActor; | ||
| import revxrsal.commands.bukkit.parameters.PlayerParameterType; | ||
| import revxrsal.commands.node.ExecutionContext; | ||
| import revxrsal.commands.parameter.ParameterType; | ||
| import revxrsal.commands.stream.MutableStringStream; | ||
|
|
||
| public class OMCPlayerParameterType implements ParameterType<BukkitCommandActor, OMCPlayer> { | ||
|
|
||
| private final PlayerParameterType delegate = new PlayerParameterType(false); | ||
|
|
||
| @Override | ||
| public OMCPlayer parse(@NotNull MutableStringStream input, @NotNull ExecutionContext<BukkitCommandActor> context) { | ||
| return OMCPlayer.of(delegate.parse(input, context)); | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull SuggestionProvider<BukkitCommandActor> defaultSuggestions() { | ||
| return delegate.defaultSuggestions(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package fr.openmc.api.entity.player; | ||
|
|
||
| import org.jetbrains.annotations.NotNull; | ||
| import revxrsal.commands.bukkit.actor.BukkitCommandActor; | ||
| import revxrsal.commands.command.CommandParameter; | ||
| import revxrsal.commands.command.ExecutableCommand; | ||
| import revxrsal.commands.process.SenderResolver; | ||
|
|
||
| public class OMCPlayerSenderResolver implements SenderResolver<BukkitCommandActor> { | ||
|
|
||
| @Override | ||
| public boolean isSenderType(@NotNull CommandParameter parameter) { | ||
| return parameter.type() == OMCPlayer.class; | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull Object getSender(@NotNull Class<?> customSenderType, @NotNull BukkitCommandActor actor, @NotNull ExecutableCommand<BukkitCommandActor> command) { | ||
| return OMCPlayer.of(actor.requirePlayer()); | ||
| } | ||
| } |

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pour devoir rallonger la ligne en faisant des omcplayer.message().send au lieu de faire OMCPlayer.sendMessagz ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parcequ'il y a deja les sendMessage de Player
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mhh c'est vrai mais tu peux faire des méthodes différentes genre OMCPlayer.sendMessage(Component, Prefix, MessageType, bool)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dans ce cas la pourquoi faire un OMCPlayer.sendMessage
autant utiliser le MessageManager quoi
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tu penses vraiment que des gens vont faire send(Component) et qu'ils vont savoir que ça envoie avec un prefix OPEN_MC ? Fin je trouve c'est ce complexifier la vie pour rien.
Quand tu utilises cette méthode les gens ne savent pas forcément de quoi il s'agit et de quoi il est utilisé avant de regarder le code de la méthode
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
En vrai stv ping nirbose et toutouchien pour leur demander leur avis car le mien est différent des autres je suppose
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bah c'est pour ca que j'ai mis dans une sous class message, et que c'est une methode custom qui vient de OMCPlayer et pas de Player, donc bon...
apres je peux ajouter la JavaDoc hein
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On va attendre d'autres review ptet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Je veux voir les avis des autres contributeur