Skip to content

Commit

Permalink
Pull up compareTo in roles
Browse files Browse the repository at this point in the history
  • Loading branch information
freya022 committed Apr 12, 2024
1 parent ff135ae commit f2d4e9d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 50 deletions.
25 changes: 0 additions & 25 deletions src/main/java/net/dv8tion/jda/internal/entities/RoleImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.awt.*;
import java.time.OffsetDateTime;
import java.util.EnumSet;
import java.util.Objects;

Expand Down Expand Up @@ -356,30 +355,6 @@ public String toString()
.toString();
}

@Override
public int compareTo(@Nonnull Role r)
{
if (this == r)
return 0;
if (!(r instanceof RoleImpl))
throw new IllegalArgumentException("Cannot compare different role implementations");
RoleImpl impl = (RoleImpl) r;

if (this.guild.getIdLong() != impl.guild.getIdLong())
throw new IllegalArgumentException("Cannot compare roles that aren't from the same guild!");

if (this.getPositionRaw() != r.getPositionRaw())
return this.getPositionRaw() - r.getPositionRaw();

OffsetDateTime thisTime = this.getTimeCreated();
OffsetDateTime rTime = r.getTimeCreated();

//We compare the provided role's time to this's time instead of the reverse as one would expect due to how
// discord deals with hierarchy. The more recent a role was created, the lower its hierarchy ranking when
// it shares the same position as another role.
return rTime.compareTo(thisTime);
}

// -- Setters --

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.awt.*;
import java.time.OffsetDateTime;
import java.util.EnumSet;

public class DetachedRoleImpl implements Role, RoleMixin<DetachedRoleImpl>
Expand Down Expand Up @@ -277,30 +276,6 @@ public String toString()
.toString();
}

@Override
public int compareTo(@Nonnull Role r)
{
if (this == r)
return 0;
if (!(r instanceof DetachedRoleImpl))
throw new IllegalArgumentException("Cannot compare different role implementations");
DetachedRoleImpl impl = (DetachedRoleImpl) r;

if (this.guild.getIdLong() != impl.guild.getIdLong())
throw new IllegalArgumentException("Cannot compare roles that aren't from the same guild!");

if (this.getPositionRaw() != r.getPositionRaw())
return this.getPositionRaw() - r.getPositionRaw();

OffsetDateTime thisTime = this.getTimeCreated();
OffsetDateTime rTime = r.getTimeCreated();

//We compare the provided role's time to this's time instead of the reverse as one would expect due to how
// discord deals with hierarchy. The more recent a role was created, the lower its hierarchy ranking when
// it shares the same position as another role.
return rTime.compareTo(thisTime);
}

// -- Setters --

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import net.dv8tion.jda.api.entities.RoleIcon;
import net.dv8tion.jda.api.requests.restaction.RoleAction;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.entities.RoleImpl;
import net.dv8tion.jda.internal.entities.detached.mixin.IDetachableEntityMixin;
import net.dv8tion.jda.internal.utils.Checks;

import javax.annotation.Nonnull;
import java.time.OffsetDateTime;

public interface RoleMixin<T extends RoleMixin<T>>
extends Role,
Expand All @@ -45,6 +47,27 @@ default RoleAction createCopy(@Nonnull Guild guild)
.setIcon(getIcon() == null ? null : getIcon().getEmoji()); // we can only copy the emoji as we don't have access to the Icon instance
}

@Override
default int compareTo(@Nonnull Role r)
{
if (this == r)
return 0;

if (this.getGuild().getIdLong() != r.getGuild().getIdLong())
throw new IllegalArgumentException("Cannot compare roles that aren't from the same guild!");

if (this.getPositionRaw() != r.getPositionRaw())
return this.getPositionRaw() - r.getPositionRaw();

OffsetDateTime thisTime = this.getTimeCreated();
OffsetDateTime rTime = r.getTimeCreated();

//We compare the provided role's time to this's time instead of the reverse as one would expect due to how
// discord deals with hierarchy. The more recent a role was created, the lower its hierarchy ranking when
// it shares the same position as another role.
return rTime.compareTo(thisTime);
}

T setName(String name);

T setColor(int color);
Expand Down

0 comments on commit f2d4e9d

Please sign in to comment.