Skip to content

Commit

Permalink
temp queue
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSchlueter committed Dec 25, 2024
1 parent 2c41a28 commit 8570236
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main/java/de/oliver/fancynpcs/FancyNpcs.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public FancyNpcs() {
}
}
JsonAppender jsonAppender = new JsonAppender(false, false, true, logsFile.getPath());
this.fancyLogger = new ExtendedFancyLogger("FancyNpcs", LogLevel.INFO, List.of(consoleAppender, jsonAppender), new ArrayList<>());
this.fancyLogger = new ExtendedFancyLogger("FancyNpcs", LogLevel.DEBUG, List.of(consoleAppender, jsonAppender), new ArrayList<>());

this.npcThread = Executors.newSingleThreadScheduledExecutor(
new ThreadFactoryBuilder()
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/de/oliver/fancynpcs/skins/MineSkinQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package de.oliver.fancynpcs.skins;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class MineSkinQueue {

private final static ScheduledExecutorService EXECUTOR = Executors.newScheduledThreadPool(1, new ThreadFactoryBuilder()
.setNameFormat("MineSkinQueue")
.build());
private static MineSkinQueue INSTANCE;
private final Queue<Runnable> queue;

private MineSkinQueue() {
this.queue = new LinkedList<>();

run();
}

public static MineSkinQueue get() {
if (INSTANCE == null) {
INSTANCE = new MineSkinQueue();
}

return INSTANCE;
}

public void add(Runnable runnable) {
this.queue.add(runnable);
}

private void run() {
EXECUTOR.scheduleAtFixedRate(this::poll, 5, 60, TimeUnit.SECONDS);
}

private void poll() {
System.out.println("Polling queue");
if (this.queue.isEmpty()) {
return;
}

System.out.println("Running runnables");
int counter = 0;
while (!this.queue.isEmpty() && counter < 9) {
Runnable runnable = this.queue.poll();
if (runnable != null) {
runnable.run();
}
counter++;
}
}
}
24 changes: 20 additions & 4 deletions src/main/java/de/oliver/fancynpcs/skins/SkinManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class SkinManagerImpl implements SkinManager {
private final SkinCache memCache;

public SkinManagerImpl(SkinCache fileCache, SkinCache memCache) {
this.executor = Executors.newScheduledThreadPool(2, new ThreadFactoryBuilder()
this.executor = Executors.newScheduledThreadPool(5, new ThreadFactoryBuilder()
.setNameFormat("FancyNpcs-Skins")
.build());

Expand Down Expand Up @@ -244,11 +244,21 @@ private SkinInfo executeRequest(GenerateRequest req) {
// submit job to the queue
CompletableFuture<QueueResponse> queueResp = client.queue().submit(req);

queueResp.exceptionally(throwable -> {
FancyNpcs.getInstance().getFancyLogger().error("Could not submit job to MineSkin queue: " + throwable.getMessage());
return null;
});

// wait for job completion
CompletableFuture<JobReference> jobResp = queueResp.thenCompose(
queueResponse -> queueResponse.getJob().waitForCompletion(client)
);

jobResp.exceptionally(throwable -> {
FancyNpcs.getInstance().getFancyLogger().error("Could not wait for job completion: " + throwable.getMessage());
return null;
});

// get skin from job or load it from the API
CompletableFuture<SkinInfo> skinResp = jobResp.thenCompose(
jobResponse -> jobResponse.getOrLoadSkin(client)
Expand All @@ -275,11 +285,17 @@ private SkinInfo executeRequest(GenerateRequest req) {
return null;
});

return skinResp.join();
try {
SkinInfo skinInfo = skinResp.join();
return skinInfo;
} catch (Exception e) {
System.out.println("Error in mineskin req: " + e.getMessage());
return null;
}
}

private SkinData tryToGetFromCache(String identifier, SkinData.SkinVariant variant) {
FancyNpcs.getInstance().getFancyLogger().debug("Trying to get skin from mem cache: " + identifier);
// FancyNpcs.getInstance().getFancyLogger().debug("Trying to get skin from mem cache: " + identifier);

SkinCacheData data = memCache.getSkin(identifier);
if (data != null) {
Expand All @@ -290,7 +306,7 @@ private SkinData tryToGetFromCache(String identifier, SkinData.SkinVariant varia
return data.skinData();
}

FancyNpcs.getInstance().getFancyLogger().debug("Trying to get skin from file cache: " + identifier);
// FancyNpcs.getInstance().getFancyLogger().debug("Trying to get skin from file cache: " + identifier);

data = fileCache.getSkin(identifier);
if (data != null) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/de/oliver/fancynpcs/skins/SkinUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static boolean isFile(String identifier) {
}

public static void applySkinLater(String npcID, String skinID, SkinData.SkinVariant variant, Runnable successCallback, Runnable errorCallback) {
FancyNpcs.getInstance().getSkinManagerImpl().getExecutor().submit(() -> {
MineSkinQueue.get().add(() -> {
FancyNpcs.getInstance().getFancyLogger().debug("Loading skin for npc '" + npcID + "'. Skin: " + skinID + ", Variant: " + variant);

SkinData skin = FancyNpcs.getInstance().getSkinManagerImpl().getByIdentifier(skinID, variant);
Expand All @@ -49,6 +49,7 @@ public static void applySkinLater(String npcID, String skinID, SkinData.SkinVari
FancyNpcs.getInstance().getFancyLogger().debug("Successfully applied skin for npc '" + npcID + "'");
successCallback.run();
});
System.out.println("Added skin to queue");
}

public static void applySkinLater(String npcID, String skinID, SkinData.SkinVariant variant) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/languages/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ messages:
npc_nearby: "<dark_gray>› <gray>Syntax: {primaryColor}/npc nearby {secondaryColor}[filters...]"
npc_remove: "<dark_gray>› <gray>Syntax: {primaryColor}/npc remove {secondaryColor}(npc)"
npc_show_in_tab: "<dark_gray>› <gray>Syntax: {primaryColor}/npc show_in_tab {secondaryColor}(npc) (state)"
npc_skin: "<dark_gray>› <gray>Syntax: {primaryColor}/npc skin {secondaryColor}(npc) (@none | @mirror | name | url) [--slim]"
npc_skin: "<dark_gray>› <gray>Syntax: {primaryColor}/npc skin {secondaryColor}(npc) (@none | @mirror | name | uuid | placeholder | url | file name) [--slim]"
npc_teleport: "<dark_gray>› <gray>Syntax: {primaryColor}/npc teleport {secondaryColor}(npc)"
npc_turn_to_player: "<dark_gray>› <gray>Syntax: {primaryColor}/npc turn_to_player {secondaryColor}(npc) (state)"
npc_type: "<dark_gray>› <gray>Syntax: {primaryColor}/npc type {secondaryColor}(npc) (type)"
Expand Down Expand Up @@ -292,7 +292,6 @@ messages:
npc_skin_set: "<dark_gray>› <gray>NPC {warningColor}{npc}<gray> is now using the {warningColor}{name}<gray> skin."
npc_skin_set_mirror: "<dark_gray>› <gray>NPC {warningColor}{npc}<gray> is now mirroring player skin."
npc_skin_set_none: "<dark_gray>› <gray>NPC {warningColor}{npc}<gray> is no longer using any skin."
npc_skin_failure_invalid: "<dark_gray>› {errorColor}Argument {warningColor}{input}{errorColor} is invalid. Common issues: invalid URL, player not found, player has no skin."

# Commands (npc teleport)
npc_teleport_success: "<dark_gray>› <gray>You have been teleported to NPC {warningColor}{npc}<gray>."
Expand Down

0 comments on commit 8570236

Please sign in to comment.