Skip to content

Commit

Permalink
refactor: rearrange some methods and adjust small things (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
Snabeldier authored Jan 26, 2025
1 parent bb0f03f commit 4b8df70
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/main/java/minevalley/core/api/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ public static ItemBuilder createItem(@Nonnull Player player) throws IllegalArgum
@Nonnull
@Contract("_ -> new")
public static ItemBuilder createItem(@Nonnull OnlineUser user) throws IllegalArgumentException {
return server.createItem(user.getPlayer());
return server.createItem(user.player());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public HandlerList getHandlers() {
public void setCancelled(boolean cancel) {
this.cancelled = true;
event.setCancelled(true);
user.getPlayer().setVelocity(event.getFrom().toVector().subtract(event.getTo().toVector()));
user.player().setVelocity(event.getFrom().toVector().subtract(event.getTo().toVector()));
}
}
133 changes: 79 additions & 54 deletions src/main/java/minevalley/core/api/users/OnlineUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Contract;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.Duration;
import java.util.function.Consumer;

@SuppressWarnings("unused")
Expand All @@ -37,13 +39,48 @@ public interface OnlineUser extends User, MessageReceiver {
* @return player object
*/
@Nonnull
Player getPlayer();
Player player();

/**
* Gets if the player has any type of team-rank.
*
* @return true, if the user is part of the server-team (and in team-service)
*/
@Contract(pure = true)
boolean isTeamler();

/**
* Gets whether the user has any of the listed team-ranks.
*
* @param ranks list of team-ranks to be checked for
* @return true, if the user has one of the ranks
*/
@Contract(pure = true)
boolean hasTeamRank(@Nonnull TeamRank... ranks);

/**
* Gets the team-member object of this user.
*
* @return team-member object
* @throws UserNotPermittedException if the user is no team-member
* @see #isTeamler()
*/
@Nonnull
TeamMember team() throws UserNotPermittedException;

/**
* Joins the team-service.
*
* @throws UnsupportedOperationException if the user is not allowed to join the team-service
* @throws IllegalStateException if the user is already in a service
*/
void joinTeamService() throws UnsupportedOperationException, IllegalStateException;

/**
* Closes the inventory of this user.
*/
default void closeInventory() {
getPlayer().closeInventory();
player().closeInventory();
}

/**
Expand Down Expand Up @@ -186,7 +223,9 @@ void input(@Nonnull String text, @Nonnull Instruction instruction, @Nonnull Cons
* @param callback callback with the chosen bank account
* @param requiredPermissions permissions that need to be granted to this user to let him choose the specific bank account
*/
void askForBankAccount(@Nonnull Consumer<BankAccount> callback, @Nonnull AccountUser.BankAccountUserPermission... requiredPermissions) throws IllegalArgumentException;
void askForBankAccount(@Nonnull Consumer<BankAccount> callback,
@Nonnull AccountUser.BankAccountUserPermission... requiredPermissions)
throws IllegalArgumentException;

// FractionService

Expand All @@ -207,7 +246,8 @@ void input(@Nonnull String text, @Nonnull Instruction instruction, @Nonnull Cons
* @throws UserNotPermittedException if the user is not allowed to enter the service
* @throws IllegalStateException if the user is already in a service
*/
void enterFractionService(@Nonnull Fraction service) throws IllegalArgumentException, UserNotPermittedException, IllegalStateException;
void enterFractionService(@Nonnull Fraction service) throws IllegalArgumentException, UserNotPermittedException,
IllegalStateException;

/**
* Lets the user leave the fraction-service.
Expand All @@ -217,39 +257,18 @@ void input(@Nonnull String text, @Nonnull Instruction instruction, @Nonnull Cons
void leaveFractionService() throws IllegalStateException;

/**
* Gets if the player has any type of team-rank.
*
* @return true, if the user is part of the server-team (and in team-service)
*/
@Contract(pure = true)
boolean isTeamler();

/**
* Gets whether the user has any of the listed team-ranks.
* Sets the user's health to the maximum.
*
* @param ranks list of team-ranks to be checked for
* @return true, if the user has one of the ranks
* @return the amount of half hearts the user was healed
*/
@Contract(pure = true)
boolean hasTeamRank(@Nonnull TeamRank... ranks);
@Nonnegative
int heal();

/**
* Gets the team-member object of this user.
* Revives the user if he's dead.
*
* @return team-member object
* @throws UserNotPermittedException if the user is no team-member
* @see #isTeamler()
*/
@Nonnull
TeamMember team() throws UserNotPermittedException;

/**
* Lets the user enter the team-service. If the user isn't teamler, nothing happens.
* @throws IllegalStateException if the user is not dead
*/
void joinTeamService() throws UnsupportedOperationException, IllegalStateException;

void heal();

void revive() throws IllegalStateException;

/**
Expand All @@ -269,22 +288,26 @@ void input(@Nonnull String text, @Nonnull Instruction instruction, @Nonnull Cons
boolean isVanish();

/**
* Defines whether this user is vanished.
* <b>Note:</b> this method only changes the state (The user is still visible)
* Sets whether this user is vanished.
*
* @param vanish vanish state
* @param vanish true, if this user should be vanished
* @throws UnsupportedOperationException if the user is not allowed to vanish
*/
void setVanish(boolean vanish) throws UnsupportedOperationException;

/**
* Imprisons this user.
* Imprisons this user for the given duration.
*
* @param duration duration in minutes
* @param duration duration of the imprisonment
* @throws UnsupportedOperationException if the user cannot be imprisoned (e.g. because he's a team-member)
* @throws IllegalStateException if the user is already in prison
*/
void imprison(int duration) throws UnsupportedOperationException, IllegalStateException;
void imprison(@Nonnull Duration duration) throws UnsupportedOperationException, IllegalStateException;

/**
* Releases this user from prison (im imprisoned).
* Releases this user from prison.
*
* @throws IllegalStateException if the user is not in prison
*/
void releaseFromPrison() throws IllegalStateException;

Expand All @@ -305,7 +328,8 @@ void input(@Nonnull String text, @Nonnull Instruction instruction, @Nonnull Cons
@Contract(pure = true)
boolean isAllowedToUse(Block block);

void changeSign(@Nonnull Block block, @Nullable String line1, @Nullable String line2, @Nullable String line3, @Nullable String line4) throws IllegalArgumentException;
void changeSign(@Nonnull Block block, @Nullable String line1, @Nullable String line2, @Nullable String line3,
@Nullable String line4) throws IllegalArgumentException;

void resetSign(@Nonnull Block block) throws IllegalArgumentException;

Expand All @@ -317,7 +341,7 @@ void input(@Nonnull String text, @Nonnull Instruction instruction, @Nonnull Cons

@Contract(pure = true)
default LoadedVehicle getVehicle() {
final Entity vehicle = getPlayer().getVehicle();
final Entity vehicle = player().getVehicle();
if (vehicle == null) return null;
if (!(vehicle instanceof ArmorStand)) return null;
return VehicleManager.getVehicle((ArmorStand) vehicle);
Expand All @@ -326,10 +350,12 @@ default LoadedVehicle getVehicle() {
void checkRegistration();

default void chat(@Nonnull ChatType type, @Nonnull String message) throws IllegalArgumentException {
if (type == null) throw new IllegalArgumentException("Type cannot be null");
ChatHandler.chat(this, type, message);
}

default void chat(@Nonnull String message) throws IllegalArgumentException {
if (message == null) throw new IllegalArgumentException("Message cannot be null");
ChatHandler.chat(this, ChatType.NORMAL, message);
}

Expand All @@ -350,19 +376,14 @@ default boolean hasFreeInventorySlot() {
PlayerLocation getLocation();

/**
* Creates a clickable message without any specifications.
*
* @return new clickable message
*/
ClickableMessage createClickableMessage();

/**
* Creates a clickable message that executes the given callback.
* Creates a clickable message that executes the given callback, and is only clickable once (if selfCancelling is true).
*
* @param callback callback to be executed if player clicks the message
* @param selfCancelling defines if the message is clickable multiple times
* @return new clickable message
*/
ClickableMessage createClickableMessage(Runnable callback);
@Nonnull
@Contract("_ -> new")
ClickableMessage createClickableMessage(boolean selfCancelling);

/**
* Creates a clickable message that executes the given callback, and is only clickable once (if selfCancelling is true).
Expand All @@ -371,7 +392,9 @@ default boolean hasFreeInventorySlot() {
* @param callback callback to be executed if player clicks the message
* @return new clickable message
*/
ClickableMessage createClickableMessage(boolean selfCancelling, Runnable callback);
@Nonnull
@Contract("_, _ -> new")
ClickableMessage createClickableMessage(boolean selfCancelling, @Nonnull Runnable callback);

/**
* Checks whether the user is in a virtual box whose span in each direction is twice the specified range.
Expand All @@ -382,7 +405,7 @@ default boolean hasFreeInventorySlot() {
* @param range maximum range for the result to be true
* @return true, if user is in range
*/
boolean isInCubicRange(@Nonnull Location location, int range) throws IllegalArgumentException;
boolean isInCubicRange(@Nonnull Location location, @Nonnegative int range) throws IllegalArgumentException;

/**
* Checks whether the user is in a virtual sphere whose radius is the square root of specified range.
Expand All @@ -394,7 +417,7 @@ default boolean hasFreeInventorySlot() {
* @param rangeSquared maximum range for the result to be true
* @return true, if user is in range
*/
boolean isInSquaredRange(@Nonnull Location location, int rangeSquared) throws IllegalArgumentException;
boolean isInSquaredRange(@Nonnull Location location, @Nonnegative int rangeSquared) throws IllegalArgumentException;

/**
* Checks whether the user is in a virtual sphere whose radius is the specified range.
Expand All @@ -403,7 +426,9 @@ default boolean hasFreeInventorySlot() {
* @param range maximum range for the result to be true
* @return true, if user is in range
*/
default boolean isInRange(@Nonnull Location location, int range) throws IllegalArgumentException {
default boolean isInRange(@Nonnull Location location, @Nonnegative int range) throws IllegalArgumentException {
if (location == null) throw new IllegalArgumentException("Location cannot be null");
if (range < 0) throw new IllegalArgumentException("Range cannot be negative");
return isInSquaredRange(location, range * range);
}

Expand Down
26 changes: 20 additions & 6 deletions src/main/java/minevalley/core/api/utils/ClickableMessage.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package minevalley.core.api.utils;

import minevalley.core.api.users.OnlineUser;
import org.jetbrains.annotations.Contract;

import javax.annotation.Nonnull;

@SuppressWarnings("unused")
public interface ClickableMessage {

/**
* Disables this clickable message
*/
void disable();

/**
* Defines if this clickable message gets disabled automatically when the player clicks it.
*
* @param isSelfCancelling boolean that defines if this clickable message is self cancelling
* @return this clickable message
*/
@Nonnull
@Contract("_ -> this")
ClickableMessage selfCancelling(boolean isSelfCancelling);

/**
Expand All @@ -24,14 +24,28 @@ public interface ClickableMessage {
* @param runnable will be called if the player clicks the message
* @return runnable
*/
ClickableMessage setCallback(Runnable runnable);
@Nonnull
@Contract("_ -> this")
ClickableMessage setCallback(@Nonnull Runnable callback);

/**
* Disables this clickable message
*/
void disable();

/**
* Gets the command which executes the callback. Put this into the click-event of your component-builder.
*
* @return the command as string
*/
@Nonnull
String getCommand();

/**
* Gets the user who this clickable message is made for.
*
* @return the user who this clickable message is made for
*/
@Nonnull
OnlineUser getUser();
}

0 comments on commit 4b8df70

Please sign in to comment.