Skip to content

Commit

Permalink
Replace long to long suppiler.
Browse files Browse the repository at this point in the history
  • Loading branch information
vLuckyyy committed Oct 25, 2023
1 parent 9d509c9 commit cccb226
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public void update(ExperienceChangeEvent event) {


try {
long channelId = event.channelId();
LongSupplier channelId = event.channelId();

MessageChannel channel = this.getChannelById(() -> channelId);
MessageChannel channel = this.getChannelById(channelId);

if (channel == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.eternalcode.discordapp.leveling.experience;

public record ExperienceChangeEvent(Experience experience, long channelId) {
import java.util.function.LongSupplier;

public record ExperienceChangeEvent(Experience experience, LongSupplier channelId) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.LongSupplier;

public class ExperienceService {

Expand All @@ -16,15 +17,15 @@ public ExperienceService(DatabaseManager databaseManager, ObserverRegistry obser
this.observerRegistry = observerRegistry;
}

public CompletableFuture<Experience> saveExperience(Experience experience, long channelId) {
public CompletableFuture<Experience> saveExperience(Experience experience, LongSupplier channelId) {
return this.experienceRepository.saveExperience(experience).whenComplete((experience1, throwable) -> {
if (throwable == null) {
this.observerRegistry.publish(new ExperienceChangeEvent(experience1, channelId));
}
});
}

public CompletableFuture<Experience> modifyPoints(long userId, double points, boolean add, long channelId) {
public CompletableFuture<Experience> modifyPoints(long userId, double points, boolean add, LongSupplier channelId) {
return this.experienceRepository.modifyPoints(userId, points, add).whenComplete((experience, throwable) -> {
if (throwable == null) {
this.observerRegistry.publish(new ExperienceChangeEvent(experience, channelId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import java.util.function.LongSupplier;

public class ExperienceMessageListener extends ListenerAdapter {

private final ExperienceConfig experienceConfig;
Expand Down Expand Up @@ -35,7 +37,7 @@ private void givePoints(MessageReceivedEvent event) {
double points = (double) message.length / this.experienceConfig.messageExperience.howManyWords * basePoints;
long userId = event.getAuthor().getIdLong();

long channelId = event.getChannel().getIdLong();
LongSupplier channelId = () -> event.getChannel().getIdLong();
this.experienceService.modifyPoints(userId, points, true, channelId).whenComplete((experience, throwable) -> {
if (throwable != null) {
throwable.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void onMessageReactionRemove(MessageReactionRemoveEvent event) {
private void modifyPoints(User event, long userId, double points) {
long channel = event.openPrivateChannel().complete().getIdLong();

this.experienceService.modifyPoints(userId, points, true, channel)
this.experienceService.modifyPoints(userId, points, true, () -> channel)
.whenComplete((experience, throwable) -> {
if (throwable != null) {
throwable.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.time.Duration;
import java.time.Instant;
import java.util.function.LongSupplier;

public class ExperienceVoiceListener extends ListenerAdapter {

Expand Down Expand Up @@ -52,7 +53,7 @@ private void leaveVoiceChannel(GuildVoiceUpdateEvent event) {
long userId = event.getMember().getIdLong();
this.usersVoiceActivityData.usersOnVoiceChannel.remove(event.getMember().getIdLong());

long voiceLeftChannelId = event.getChannelLeft().getIdLong();
LongSupplier voiceLeftChannelId = () -> event.getChannelLeft().getIdLong();
this.experienceService.modifyPoints(userId, this.calculatePoints(event), true, voiceLeftChannelId).whenComplete((experience, throwable) -> {
if (throwable != null) {
throwable.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.time.Duration;
import java.time.Instant;
import java.util.Random;
import java.util.function.LongSupplier;

public class CodeGameAnswerController extends ListenerAdapter {

Expand All @@ -34,9 +35,9 @@ public void onMessageReceived(MessageReceivedEvent event) {
return;
}

long channelId = event.getChannel().getIdLong();
LongSupplier channelId = () -> event.getChannel().getIdLong();

if (channelId != this.codeGameConfiguration.channelId) {
if (channelId.getAsLong() != this.codeGameConfiguration.channelId) {
return;
}

Expand Down Expand Up @@ -68,6 +69,7 @@ public void onMessageReceived(MessageReceivedEvent event) {
.setDescription(formatter.format(this.codeGameConfiguration.embedSettings.description))
.setFooter(this.codeGameConfiguration.embedSettings.footer);


this.experienceService.modifyPoints(event.getAuthor().getIdLong(), points, true, channelId)
.whenComplete((experience, throwable) -> {
if (throwable != null) {
Expand Down

0 comments on commit cccb226

Please sign in to comment.