Skip to content

Commit

Permalink
Replace LinkUserTemplate in api module (#112)
Browse files Browse the repository at this point in the history
* Replace LinkUserTemplate#of with LinkUser#of

* Remove LinkUserTemplate

* Remove unnecessary field declaration in AccountLinkAdapter
  • Loading branch information
bivashy authored Nov 4, 2023
1 parent 787f500 commit f52bc7b
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 57 deletions.
4 changes: 1 addition & 3 deletions api/src/main/java/com/bivashy/auth/api/account/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.bivashy.auth.api.crypto.HashedPassword;
import com.bivashy.auth.api.link.LinkType;
import com.bivashy.auth.api.link.user.LinkUser;
import com.bivashy.auth.api.link.user.LinkUserTemplate;
import com.bivashy.auth.api.link.user.info.LinkUserInfo;
import com.bivashy.auth.api.model.PlayerIdSupplier;
import com.bivashy.auth.api.server.player.ServerPlayer;
Expand Down Expand Up @@ -76,7 +75,7 @@ default LinkUser findFirstLinkUserOrCreate(Predicate<LinkUser> filter, LinkUser
}

default LinkUser findFirstLinkUserOrNew(Predicate<LinkUser> filter, LinkType linkType) {
return findFirstLinkUserOrCreate(filter, LinkUserTemplate.of(linkType, this, LinkUserInfo.of(linkType.getDefaultIdentificator())));
return findFirstLinkUserOrCreate(filter, LinkUser.of(linkType, this, LinkUserInfo.of(linkType.getDefaultIdentificator())));
}

@Deprecated
Expand Down Expand Up @@ -152,5 +151,4 @@ default Optional<ServerPlayer> getPlayer() {
default boolean isRegistered() {
return getPasswordHash().getHash() != null;
}

}
21 changes: 21 additions & 0 deletions api/src/main/java/com/bivashy/auth/api/link/user/LinkUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@
import com.bivashy.auth.api.link.user.info.LinkUserInfo;

public interface LinkUser {

static LinkUser of(LinkType linkType, Account account, LinkUserInfo userInfo) {
return new LinkUser() {
@Override
public LinkType getLinkType() {
return linkType;
}

@Override
public Account getAccount() {
return account;
}

@Override
public LinkUserInfo getLinkUserInfo() {
return userInfo;
}
};
}

/**
* @return link type, for example link type of VK, link type of DISCORD,link
* type of TELEGRAM.
Expand All @@ -29,4 +49,5 @@ default boolean isIdentifierDefaultOrNull() {
.map(identificator -> identificator.equals(getLinkType().getDefaultIdentificator()) || identificator.asString() == null)
.orElse(true);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@

import com.bivashy.auth.api.AuthPlugin;
import com.bivashy.auth.api.account.Account;
import com.bivashy.auth.api.link.LinkType;
import com.bivashy.auth.api.link.user.info.LinkUserIdentificator;
import com.bivashy.auth.api.link.user.info.LinkUserInfo;

import me.mastercapexd.auth.database.model.AccountLink;

public class AccountLinkAdapter extends LinkUserTemplate {
private final LinkUserInfo linkUserInfo;

public AccountLinkAdapter(AccountLink accountLink, Account account) {
super(AuthPlugin.instance()
super(findLinkType(accountLink), account, LinkUserInfo.of(LinkUserIdentificator.ofParsed(accountLink.getLinkUserId()), accountLink.isLinkEnabled()));
}

private static LinkType findLinkType(AccountLink accountLink) {
return AuthPlugin.instance()
.getLinkTypeProvider()
.getLinkType(accountLink.getLinkType())
.orElseThrow(() -> new IllegalArgumentException("Link type " + accountLink.getLinkType() + " not exists!")), account);
this.linkUserInfo = LinkUserInfo.of(LinkUserIdentificator.ofParsed(accountLink.getLinkUserId()), accountLink.isLinkEnabled());
.orElseThrow(() -> new IllegalArgumentException("Link type " + accountLink.getLinkType() + " not exists!"));
}

@Override
public LinkUserInfo getLinkUserInfo() {
return linkUserInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import com.bivashy.auth.api.link.user.LinkUser;
import com.bivashy.auth.api.link.user.info.LinkUserInfo;

public abstract class LinkUserTemplate implements LinkUser {
protected final LinkType linkType;
protected final Account account;
public class LinkUserTemplate implements LinkUser {
private final LinkType linkType;
private final Account account;
private final LinkUserInfo linkUserInfo;

public LinkUserTemplate(LinkType linkType, Account account) {
public LinkUserTemplate(LinkType linkType, Account account, LinkUserInfo linkUserInfo) {
this.linkType = linkType;
this.account = account;
this.linkUserInfo = linkUserInfo;
}

@Override
Expand All @@ -24,12 +26,12 @@ public Account getAccount() {
return account;
}

@Override
public LinkUserInfo getLinkUserInfo() {
return linkUserInfo;
}

public static LinkUser of(LinkType linkType, Account account, LinkUserInfo linkUserInfo) {
return new LinkUserTemplate(linkType, account) {
@Override
public LinkUserInfo getLinkUserInfo() {
return linkUserInfo;
}
};
return new LinkUserTemplate(linkType, account, linkUserInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class BaseLinkEntryUser extends LinkUserTemplate implements LinkEntryUser
protected boolean confirmed = false;

public BaseLinkEntryUser(LinkType linkType, Account account, LinkUserInfo linkUserInfo) {
super(linkType, account);
super(linkType, account, linkUserInfo);
this.linkUserInfo = linkUserInfo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import com.bivashy.auth.api.config.message.Messages;
import com.bivashy.auth.api.database.AccountDatabase;
import com.bivashy.auth.api.link.user.LinkUser;
import com.bivashy.auth.api.link.user.LinkUserTemplate;
import com.bivashy.auth.api.link.user.info.LinkUserIdentificator;
import com.bivashy.auth.api.link.user.info.LinkUserInfo;
import com.bivashy.auth.api.server.message.ServerComponent;
import com.bivashy.auth.api.server.player.ServerPlayer;

import me.mastercapexd.auth.link.google.GoogleLinkType;
import me.mastercapexd.auth.link.user.LinkUserTemplate;
import me.mastercapexd.auth.server.commands.annotations.GoogleUse;
import me.mastercapexd.auth.shared.commands.annotation.CommandCooldown;
import revxrsal.commands.annotation.Command;
Expand Down

0 comments on commit f52bc7b

Please sign in to comment.