Skip to content

Commit

Permalink
Reorganized code.
Browse files Browse the repository at this point in the history
Reorganized methods and attributes for better structure and clarity across multiple classes.
  • Loading branch information
NonSwag committed Jan 17, 2025
1 parent 8aaef56 commit d7d0a54
Show file tree
Hide file tree
Showing 16 changed files with 491 additions and 480 deletions.
40 changes: 20 additions & 20 deletions api/src/main/java/net/thenextlvl/character/Character.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public interface Character<T extends Entity> {
@Nullable
Component getDisplayName();

void setDisplayName(@Nullable Component displayName);

EntityType getType();

@Nullable
Expand All @@ -35,16 +37,22 @@ public interface Character<T extends Entity> {
@Nullable
Location getSpawnLocation();

void setSpawnLocation(@Nullable Location location);

@Unmodifiable
Map<String, ClickAction<?>> getActions();

@Nullable
NamedTextColor getGlowColor();

void setGlowColor(@Nullable NamedTextColor color);

Optional<T> getEntity();

Pose getPose();

void setPose(Pose pose);

@Unmodifiable
Set<UUID> getViewers();

Expand All @@ -71,22 +79,34 @@ public interface Character<T extends Entity> {

boolean isCollidable();

void setCollidable(boolean collidable);

boolean isDisplayNameVisible();

void setDisplayNameVisible(boolean visible);

boolean isInvincible();

void setInvincible(boolean invincible);

boolean isPersistent();

void setPersistent(boolean persistent);

boolean isSpawned();

boolean isTicking();

void setTicking(boolean ticking);

boolean isTrackedBy(Player player);

boolean isViewer(UUID player);

boolean isVisibleByDefault();

void setVisibleByDefault(boolean visible);

boolean persist();

boolean removeAction(String name);
Expand All @@ -105,25 +125,5 @@ public interface Character<T extends Entity> {

void remove();

void setCollidable(boolean collidable);

void setDisplayName(@Nullable Component displayName);

void setDisplayNameVisible(boolean visible);

void setGlowColor(@Nullable NamedTextColor color);

void setGravity(boolean gravity);

void setInvincible(boolean invincible);

void setPersistent(boolean persistent);

void setPose(Pose pose);

void setSpawnLocation(@Nullable Location location);

void setTicking(boolean ticking);

void setVisibleByDefault(boolean visible);
}
10 changes: 5 additions & 5 deletions api/src/main/java/net/thenextlvl/character/PlayerCharacter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ public interface PlayerCharacter extends Character<Player> {

SkinParts getSkinParts();

void setSkinParts(SkinParts builder);

UUID getUniqueId();

boolean clearTextures();

boolean isListed();

boolean isRealPlayer();

boolean setTextures(String value, @Nullable String signature);

void setListed(boolean listed);

boolean isRealPlayer();

void setRealPlayer(boolean real);

void setSkinParts(SkinParts builder);
boolean setTextures(String value, @Nullable String signature);
}
61 changes: 30 additions & 31 deletions api/src/main/java/net/thenextlvl/character/action/ClickAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@

@NullMarked
public class ClickAction<T> {
private final ActionType<T> actionType;
private final Map<Player, Long> cooldowns = new WeakHashMap<>();
private @Nullable String permission;
private EnumSet<ClickType> clickTypes;
private Duration cooldown;
private T input;
private final ActionType<T> actionType;

public ClickAction(ActionType<T> actionType, EnumSet<ClickType> clickTypes, T input) {
this(actionType, clickTypes, input, Duration.ZERO, null);
Expand All @@ -30,54 +31,40 @@ public ClickAction(ActionType<T> actionType, EnumSet<ClickType> clickTypes, T in
this.permission = permission;
}

private final Map<Player, Long> cooldowns = new WeakHashMap<>();

public boolean isOnCooldown(Player player) {
return cooldown.isPositive() && cooldowns.computeIfPresent(player, (ignored, lastUsed) -> {
if (System.currentTimeMillis() - cooldown.toMillis() > lastUsed) return null;
return lastUsed;
}) != null;
}

public boolean resetCooldown(Player player) {
return cooldowns.remove(player) != null;
}

public boolean canInvoke(Player player) {
return (permission == null || player.hasPermission(permission)) && !isOnCooldown(player);
}

public boolean invoke(Player player) {
if (!canInvoke(player)) return false;
if (cooldown.isPositive()) cooldowns.put(player, System.currentTimeMillis());
actionType.invoke(player, input);
return true;
}

public boolean isSupportedClickType(ClickType type) {
return clickTypes.contains(type);
public boolean canInvoke(Player player) {
return (permission == null || player.hasPermission(permission)) && !isOnCooldown(player);
}

public void setClickTypes(EnumSet<ClickType> clickTypes) {
this.clickTypes = clickTypes;
public boolean isOnCooldown(Player player) {
return cooldown.isPositive() && cooldowns.computeIfPresent(player, (ignored, lastUsed) -> {
if (System.currentTimeMillis() - cooldown.toMillis() > lastUsed) return null;
return lastUsed;
}) != null;
}

public void setCooldown(Duration cooldown) {
this.cooldown = cooldown;
public boolean isSupportedClickType(ClickType type) {
return clickTypes.contains(type);
}

public void setInput(T input) {
this.input = input;
public @Nullable String getPermission() {
return permission;
}

public void setPermission(@Nullable String permission) {
this.permission = permission;
}

public @Nullable String getPermission() {
return permission;
}

public ActionType<T> getActionType() {
return actionType;
}
Expand All @@ -86,14 +73,31 @@ public Duration getCooldown() {
return cooldown;
}

public void setCooldown(Duration cooldown) {
this.cooldown = cooldown;
}

public EnumSet<ClickType> getClickTypes() {
return clickTypes;
}

public void setClickTypes(EnumSet<ClickType> clickTypes) {
this.clickTypes = clickTypes;
}

public T getInput() {
return input;
}

public void setInput(T input) {
this.input = input;
}

@Override
public int hashCode() {
return Objects.hash(permission, clickTypes, cooldown, input, actionType, cooldowns);
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
Expand All @@ -104,9 +108,4 @@ public boolean equals(Object o) {
&& Objects.equals(input, that.input)
&& Objects.equals(actionType, that.actionType);
}

@Override
public int hashCode() {
return Objects.hash(permission, clickTypes, cooldown, input, actionType, cooldowns);
}
}
Loading

0 comments on commit d7d0a54

Please sign in to comment.