Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(model): improved model decoding system #228

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/main/java/com/seailz/discordjar/DiscordJar.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.seailz.discordjar.utils.Checker;
import com.seailz.discordjar.utils.HTTPOnlyInfo;
import com.seailz.discordjar.utils.URLS;
import com.seailz.discordjar.utils.model.ModelDecoder;
import com.seailz.discordjar.utils.permission.Permission;
import com.seailz.discordjar.utils.rest.DiscordRequest;
import com.seailz.discordjar.utils.rest.DiscordResponse;
Expand Down Expand Up @@ -509,7 +510,7 @@ public User getUserById(String id) {
@Nullable
public User getSelfUser() {
if (this.getSelfUserCache != null && getSelfUserCache.get() != null)
return User.decompile(getSelfUserCache.get(), this);
return (User) ModelDecoder.decodeObject(getSelfUserCache.get(), User.class, this);

DiscordRequest req = new DiscordRequest(
new JSONObject(), new HashMap<>(),
Expand All @@ -529,7 +530,7 @@ public User getSelfUser() {
getSelfUserCache.reset(60000);
}
this.getSelfUserCache.update(response.body());
return User.decompile(response.body(), this);
return (User) ModelDecoder.decodeObject(response.body(), User.class, this);
}

/**
Expand Down Expand Up @@ -770,11 +771,11 @@ public Guild getGuildById(String id) {
public Sticker getStickerById(String id) {
Checker.isSnowflake(id, "Given id is not a snowflake");
try {
return Sticker.decompile(new DiscordRequest(
return (Sticker) ModelDecoder.decodeObject(new DiscordRequest(
new JSONObject(), new HashMap<>(),
URLS.GET.STICKER.GET_STICKER.replace("{sticker.id}", id),
this, URLS.GET.STICKER.GET_STICKER, RequestMethod.GET
).invoke().body(), this);
).invoke().body(), Sticker.class, this);
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
if (e.getHttpCode() == 404) return null;
throw new DiscordRequest.DiscordAPIErrorException(e);
Expand Down Expand Up @@ -838,7 +839,7 @@ public Cache<Guild> getGuildCache() {
@Nullable
public Application getSelfInfo() {
if (this.selfUserCache != null && !selfUserCache.isEmpty())
return Application.decompile(selfUserCache.get(), this);
return (Application) ModelDecoder.decodeObject(selfUserCache.get(), Application.class, this);

DiscordRequest request = new DiscordRequest(
new JSONObject(), new HashMap<>(),
Expand All @@ -857,7 +858,7 @@ public Application getSelfInfo() {
this.selfUserCache = JsonCache.newc(response.body(), request);
this.selfUserCache.update(response.body());

return Application.decompile(response.body(), this);
return (Application) ModelDecoder.decodeObject(response.body(), Application.class, this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.seailz.discordjar.model.guild.Guild;
import com.seailz.discordjar.utils.Checker;
import com.seailz.discordjar.utils.URLS;
import com.seailz.discordjar.utils.model.ModelDecoder;
import com.seailz.discordjar.utils.rest.DiscordRequest;
import com.seailz.discordjar.utils.rest.DiscordResponse;
import org.json.JSONObject;
Expand Down Expand Up @@ -112,7 +113,7 @@ public List<Guild> run() {
}

List<Guild> returnGuilds = new ArrayList<>();
response.arr().forEach(guild -> returnGuilds.add(Guild.decompile((JSONObject) guild, discordJar)));
response.arr().forEach(guild -> returnGuilds.add((Guild) ModelDecoder.decodeObject((JSONObject) guild, Guild.class, discordJar)));

returnGuilds.forEach(g -> discordJar.getGuildCache().cache(g));
return returnGuilds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.model.emoji.sticker.Sticker;
import com.seailz.discordjar.utils.URLS;
import com.seailz.discordjar.utils.model.ModelDecoder;
import com.seailz.discordjar.utils.rest.DiscordRequest;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.RequestMethod;
Expand Down Expand Up @@ -54,7 +55,7 @@ public CompletableFuture<Sticker> run() {
CompletableFuture<Sticker> stickerCompletableFuture = new CompletableFuture<>();
stickerCompletableFuture.completeAsync(() -> {
try {
return Sticker.decompile(
return (Sticker) ModelDecoder.decodeObject(
new DiscordRequest(
new JSONObject()
.put("name", name != null ? name : JSONObject.NULL)
Expand All @@ -69,6 +70,7 @@ public CompletableFuture<Sticker> run() {
URLS.PATCH.GUILD.STICKER.MODIFY_GUILD_STICKER,
RequestMethod.PATCH
).invoke().body(),
Sticker.class,
discordJar
);
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
Expand Down
34 changes: 21 additions & 13 deletions src/main/java/com/seailz/discordjar/cache/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.model.guild.Guild;
import com.seailz.discordjar.model.guild.Member;
import com.seailz.discordjar.utils.model.Model;
import com.seailz.discordjar.utils.model.ModelDecoder;
import com.seailz.discordjar.utils.rest.DiscordRequest;
import com.seailz.discordjar.utils.rest.DiscordResponse;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -171,26 +173,32 @@ public T getById(String id) throws DiscordRequest.UnhandledDiscordAPIErrorExcept
try {
decompile = clazz.getMethod("decompile", JSONObject.class, DiscordJar.class, String.class, Guild.class);
} catch (NoSuchMethodException exx) {
Logger.getLogger("DiscordJar").severe("Was unable to return object from cache, please report this to discord.jar's github!");
throw new RuntimeException(exx);
decompile = null;
}
}
}

try {
if (decompile == null) {
if (response == null) return null;
returnObject.set(decompile.invoke(null, response.body(), discordJar));
} catch (IllegalAccessException | InvocationTargetException | IllegalArgumentException e) {
returnObject.set(ModelDecoder.decodeObject(
response.body(), (Class<? extends Model>) clazz, discordJar
));
} else {
try {
returnObject.set(decompile.invoke(null, response.body()));
} catch (IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
if (response == null) return null;
returnObject.set(decompile.invoke(null, response.body(), discordJar));
} catch (IllegalAccessException | InvocationTargetException | IllegalArgumentException e) {
try {
if (guild != null) {
returnObject.set(decompile.invoke(null, response.body(), discordJar, guild.id(), guild));
} else throw new IllegalArgumentException(ex);
} catch (IllegalAccessException | InvocationTargetException | IllegalArgumentException e1) {
Logger.getLogger("DiscordJar").severe("Was unable to return object from cache, please report this to discord.jar's github!");
return null;
returnObject.set(decompile.invoke(null, response.body()));
} catch (IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
try {
if (guild != null) {
returnObject.set(decompile.invoke(null, response.body(), discordJar, guild.id(), guild));
} else throw new IllegalArgumentException(ex);
} catch (IllegalAccessException | InvocationTargetException | IllegalArgumentException e1) {
Logger.getLogger("DiscordJar").severe("Was unable to return object from cache, please report this to discord.jar's github!");
return null;
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/seailz/discordjar/cache/JsonCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public interface JsonCache {
*
* @param interval The interval on which to invalidate the cache.
*/
default void reset(int interval) {
default JsonCache reset(int interval) {
new Thread(() -> {
while (true) {
try {
Expand All @@ -73,6 +73,7 @@ default void reset(int interval) {
invalidate();
}
}, "djar--CacheInvalidate" + new Random().nextInt(999)).start();
return this;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.seailz.discordjar.model.application.Application;
import com.seailz.discordjar.model.guild.UnavailableGuild;
import com.seailz.discordjar.model.user.User;
import com.seailz.discordjar.utils.model.ModelDecoder;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

Expand All @@ -28,7 +29,7 @@ public ReadyEvent(@NotNull DiscordJar bot, long sequence, @NotNull JSONObject da
*/
@NotNull
public User getUser() {
return User.decompile(getJson().getJSONObject("d").getJSONObject("user"), getBot());
return (User) ModelDecoder.decodeObject(getJson().getJSONObject("d").getJSONObject("user"), User.class, getBot());
}

@NotNull
Expand Down Expand Up @@ -60,7 +61,7 @@ public String getResumeGatewayUrl() {

@NotNull
public Application getApplication() {
return Application.decompile(getJson().getJSONObject("d").getJSONObject("application"), getBot());
return (Application) ModelDecoder.decodeObject(getJson().getJSONObject("d").getJSONObject("application"), Application.class, getBot());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.events.model.Event;
import com.seailz.discordjar.model.guild.Guild;
import com.seailz.discordjar.utils.model.ModelDecoder;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

Expand All @@ -21,6 +22,6 @@ public GuildCreateEvent(@NotNull DiscordJar bot, long sequence, @NotNull JSONObj

@NotNull
public Guild getGuild() {
return Guild.decompile(getJson().getJSONObject("d"), getBot());
return (Guild) ModelDecoder.decodeObject(getJson().getJSONObject("d"), Guild.class, getBot());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.events.model.Event;
import com.seailz.discordjar.model.guild.Guild;
import com.seailz.discordjar.utils.model.ModelDecoder;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

Expand All @@ -13,6 +14,6 @@ public GuildUpdateEvent(@NotNull DiscordJar bot, long sequence, @NotNull JSONObj

@NotNull
public Guild getGuild() {
return Guild.decompile(getJson().getJSONObject("d"), getBot());
return (Guild) ModelDecoder.decodeObject(getJson().getJSONObject("d"), Guild.class, getBot());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.events.model.guild.GuildEvent;
import com.seailz.discordjar.model.user.User;
import com.seailz.discordjar.utils.model.ModelDecoder;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

Expand Down Expand Up @@ -30,7 +31,7 @@ public GuildMemberRemoveEvent(@NotNull DiscordJar bot, long sequence, @NotNull J
* @return {@link User} object.
*/
public User getUser() {
return User.decompile(getJson().getJSONObject("d").getJSONObject("user"), getBot());
return (User) ModelDecoder.decodeObject(getJson().getJSONObject("d").getJSONObject("user"), User.class, getBot());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.model.user.User;
import com.seailz.discordjar.utils.URLS;
import com.seailz.discordjar.utils.model.ModelDecoder;
import com.seailz.discordjar.utils.rest.DiscordRequest;
import com.seailz.discordjar.utils.rest.DiscordResponse;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -39,7 +40,7 @@ public User getTarget() {
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
throw new DiscordRequest.DiscordAPIErrorException(e);
}
return User.decompile(response.body(), getBot());
return (User) ModelDecoder.decodeObject(response.body(), User.class, getBot());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.seailz.discordjar.model.interaction.reply.InteractionModalResponse;
import com.seailz.discordjar.model.user.User;
import com.seailz.discordjar.utils.registry.components.ChannelSelectRegistry;
import com.seailz.discordjar.utils.registry.components.StringSelectRegistry;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.seailz.discordjar.model.interaction.callback.InteractionCallbackType;
import com.seailz.discordjar.utils.TriFunction;
import com.seailz.discordjar.utils.URLS;
import com.seailz.discordjar.utils.model.ModelDecoder;
import com.seailz.discordjar.utils.rest.DiscordRequest;
import com.seailz.discordjar.voice.model.VoiceServerUpdate;
import com.seailz.discordjar.voice.model.VoiceState;
Expand Down Expand Up @@ -115,7 +116,7 @@ public enum DispatchedEvents {
if (p.getJSONObject("d").has("unavailable") && p.getJSONObject("d").getBoolean("unavailable"))
// Guild is unavailable, don't cache it
return GuildCreateEvent.class;
Guild guild = Guild.decompile(p.getJSONObject("d"), g);
Guild guild = (Guild) ModelDecoder.decodeObject(p.getJSONObject("d"), Guild.class, g);
g.getGuildCache().cache(guild);

JSONArray arr = p.getJSONObject("d").getJSONArray("channels");
Expand Down Expand Up @@ -152,14 +153,14 @@ public enum DispatchedEvents {
}),
GUILD_UPDATE((p, g, d) -> {
// modify cached guild, if it exists
Guild guild = Guild.decompile(p.getJSONObject("d"), d);
Guild guild = (Guild) ModelDecoder.decodeObject(p.getJSONObject("d"), Guild.class, d);
d.getGuildCache().cache(guild);

return GuildUpdateEvent.class;
}),
GUILD_DELETE((p, g, d) -> {
// remove cached guild, if it exists
Guild guild = Guild.decompile(p.getJSONObject("d"), d);
Guild guild = (Guild) ModelDecoder.decodeObject(p.getJSONObject("d"), Guild.class, d);
d.getGuildCache().remove(guild);

return GuildDeleteEvent.class;
Expand Down
Loading