Skip to content
Merged
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
16 changes: 8 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<modelVersion>4.0.0</modelVersion>
<groupId>CraftEnhance</groupId>
<artifactId>CraftEnhance</artifactId>
<version>2.5.6.3.2</version>
<version>2.5.7.0</version>
<name>CraftEnhance</name>
<description>This is a minecraft plugin to enhance crafting.</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.release>8</maven.compiler.release>
<utility-library>0.111</utility-library>
<utility-library>0.116</utility-library>
<dependencys_path>com.dutchjelly.craftenhance.libs</dependencys_path>
</properties>
<build>
Expand Down Expand Up @@ -77,10 +77,10 @@
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<!-- <repository>
<id>itemsadder-repo</id>
<url>https://maven.pkg.github.com/LoneDev6/API-ItemsAdder</url>
</repository>-->
<!-- <repository>
<id>itemsadder-repo</id>
<url>https://maven.pkg.github.com/LoneDev6/API-ItemsAdder</url>
</repository>-->
<repository>
<id>codemc-repo</id>
<url>https://repo.codemc.io/repository/maven-public/</url>
Expand All @@ -102,7 +102,7 @@
<dependency>
<groupId>com.mojang</groupId>
<artifactId>authlib</artifactId>
<version>3.11.49</version>
<version>3.13.56</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/dutchjelly/bukkitadapter/Adapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class Adapter {


public static List<String> CompatibleVersions() {
return Arrays.asList("1.9", "1.10", "1.11", "1.12", "1.13", "1.14", "1.15", "1.16", "1.17", "1.18", "1.19", "1.20");
return Arrays.asList("1.9", "1.10", "1.11", "1.12", "1.13", "1.14", "1.15", "1.16", "1.17", "1.18", "1.19", "1.20","1.21");
}

public final static String GUI_SKULL_MATERIAL_NAME = "GUI_SKULL_ITEM";
Expand Down
40 changes: 31 additions & 9 deletions src/main/java/com/dutchjelly/craftenhance/CraftEnhance.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dutchjelly.bukkitadapter.Adapter;
import com.dutchjelly.craftenhance.api.CraftEnhanceAPI;
import com.dutchjelly.craftenhance.cache.CacheRecipes;
import com.dutchjelly.craftenhance.commandhandling.CustomCmdHandler;
import com.dutchjelly.craftenhance.commands.ceh.ChangeKeyCmd;
import com.dutchjelly.craftenhance.commands.ceh.CleanItemFileCmd;
Expand All @@ -27,6 +28,7 @@
import com.dutchjelly.craftenhance.crafthandling.recipes.furnace.BlastRecipe;
import com.dutchjelly.craftenhance.crafthandling.recipes.furnace.SmokerRecipe;
import com.dutchjelly.craftenhance.crafthandling.util.ItemMatchers;
import com.dutchjelly.craftenhance.database.RecipeDatabase;
import com.dutchjelly.craftenhance.files.CategoryDataCache;
import com.dutchjelly.craftenhance.files.ConfigFormatter;
import com.dutchjelly.craftenhance.files.FileManager;
Expand All @@ -40,6 +42,7 @@
import com.dutchjelly.craftenhance.updatechecking.VersionChecker;
import com.dutchjelly.craftenhance.util.Metrics;
import lombok.Getter;
import lombok.NonNull;
import org.broken.arrow.menu.library.RegisterMenuAPI;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
Expand All @@ -51,17 +54,23 @@

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class CraftEnhance extends JavaPlugin {

private static CraftEnhance plugin;


public static CraftEnhance self() {
return plugin;
}


@Getter
private SaveScheduler saveScheduler;
@Getter
private CacheRecipes cacheRecipes;
@Getter
private RecipeDatabase database;
private Metrics metrics;
@Getter
private FileManager fm;
Expand Down Expand Up @@ -89,6 +98,11 @@ public static CraftEnhance self() {
@Override
public void onEnable() {
plugin = this;

this.database = new RecipeDatabase();
this.saveScheduler = new SaveScheduler();
this.cacheRecipes = new CacheRecipes(this);

//The file manager needs serialization, so firstly register the classes.
registerSerialization();
versionChecker = VersionChecker.init(this);
Expand All @@ -98,12 +112,15 @@ public void onEnable() {
saveDefaultConfig();
Debug.init(this);


if (isReloading)
Bukkit.getScheduler().runTaskAsynchronously(this, () -> loadPluginData(isReloading));
else {
this.loadPluginData(false);
loadRecipes();
}
if (injector == null)
injector = new RecipeInjector(this);
guiManager = new GuiManager(this);

Debug.Send("Setting up listeners and commands");
Expand All @@ -124,6 +141,7 @@ public void onEnable() {
metrics = new Metrics(this, metricsId);
}
CraftEnhanceAPI.registerListener(new ExecuteCommand());
saveScheduler.start();
}


Expand All @@ -148,7 +166,7 @@ public void onDisable() {
fm.saveContainerOwners(injector.getContainerOwners());
Debug.Send("Saving disabled recipes...");
fm.saveDisabledServerRecipes(RecipeLoader.getInstance().getDisabledServerRecipes().stream().map(x -> Adapter.GetRecipeIdentifier(x)).collect(Collectors.toList()));
fm.getRecipes().forEach(EnhancedRecipe::save);
cacheRecipes.getRecipes().forEach(EnhancedRecipe::save);
categoryDataCache.save();

}
Expand Down Expand Up @@ -249,27 +267,31 @@ private void loadRecipes() {
//Most other instances use the file manager, so setup before everything.
Debug.Send("Setting up the file manager for recipes.");
setupFileManager();

Debug.Send("Loading recipes");
final RecipeLoader loader = RecipeLoader.getInstance();
fm.getRecipes().stream().filter(x -> x.validate() == null).forEach((recipe) -> loader.loadRecipe(recipe, isReloading));
loader.printGroupsDebugInfo();
Bukkit.getScheduler().runTaskAsynchronously(this, ()-> {
@NonNull List<EnhancedRecipe> recipes = this.database.loadRecipes();
this.cacheRecipes.addAll(recipes);
Bukkit.getScheduler().runTask(this, ()->loadingRecipes(loader));
});
isReloading = false;
}

private void loadingRecipes(final RecipeLoader loader) {
this.cacheRecipes.getRecipes().stream().filter(x -> x.validate() == null).forEach((recipe) -> loader.loadRecipe(recipe, isReloading));
loader.printGroupsDebugInfo();
loader.disableServerRecipes(
fm.readDisabledServerRecipes().stream().map(x ->
Adapter.FilterRecipes(loader.getServerRecipes(), x)
).collect(Collectors.toList())
);
if (injector == null)
injector = new RecipeInjector(this);
injector.registerContainerOwners(fm.getContainerOwners());
injector.setLoader(loader);
//todo learn recipes are little broken. when you reload it.
if (isReloading && Bukkit.getOnlinePlayers().size() > 0)
if (self().getConfig().getBoolean("learn-recipes"))
for (final Player player : Bukkit.getOnlinePlayers())
Adapter.DiscoverRecipes(player, getCategoryDataCache().getServerRecipes());
isReloading = false;
}

public void openEnhancedCraftingTable(final Player p) {
Expand Down
86 changes: 86 additions & 0 deletions src/main/java/com/dutchjelly/craftenhance/SaveScheduler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.dutchjelly.craftenhance;

import com.dutchjelly.craftenhance.messaging.Debug;
import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import static com.dutchjelly.craftenhance.CraftEnhance.self;

public class SaveScheduler implements Runnable {
private static final long SAVE_TASK_INTERVAL = 600 * 4;
private static final long SAVE_INTERVAL = 60000 * 8;
private int taskId = -1;
private long lastSaveTime = 0;
private long lastSaveTask = 0;
private volatile boolean isRunningTask;
private BukkitTask task;
private final Queue<Runnable> taskQueue = new ConcurrentLinkedQueue<>();

public void start() {
if (task != null && !task.isCancelled()) {
return;
}

this.task = Bukkit.getScheduler().runTaskTimerAsynchronously(self(), this, 20 * 30, 20);
taskId = this.task.getTaskId();
}

public void stop() {
if (taskId == -1) return;

if (Bukkit.getScheduler().isCurrentlyRunning(taskId) && Bukkit.getScheduler().isQueued(taskId)) {
Bukkit.getScheduler().cancelTask(taskId);
}
}

@Override
public void run() {
long now = System.currentTimeMillis();

if (now - lastSaveTask >= SAVE_TASK_INTERVAL && !isRunningTask) {
executeTask();
lastSaveTask = now;
}

if (now - lastSaveTime >= SAVE_INTERVAL) {
Debug.Send("Doing the scheduler save task.");
self().getCacheRecipes().save();
lastSaveTime = now;
}

}

private void executeTask() {
if (taskQueue.isEmpty() || isRunningTask) {
return;
}
Debug.Send("Doing the batch save to the database.");
isRunningTask = true;
new BukkitRunnable() {
@Override
public void run() {
int batchSize = 4;
Debug.Send("Working with the batch of: " + taskQueue.size());
for (int i = 0; i < batchSize && !taskQueue.isEmpty(); i++) {
Runnable nextTask = taskQueue.poll();
if (nextTask != null) {
nextTask.run();
}
}
if (taskQueue.isEmpty()) {
isRunningTask = false;
cancel();
}
}
}.runTaskTimerAsynchronously(self(), 1, 5);
}

public void addTask(Runnable task) {
taskQueue.add(task);
}

}
71 changes: 71 additions & 0 deletions src/main/java/com/dutchjelly/craftenhance/cache/CacheRecipes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.dutchjelly.craftenhance.cache;

import com.dutchjelly.craftenhance.CraftEnhance;
import com.dutchjelly.craftenhance.SaveScheduler;
import com.dutchjelly.craftenhance.crafthandling.recipes.EnhancedRecipe;
import com.dutchjelly.craftenhance.database.RecipeDatabase;
import com.dutchjelly.craftenhance.messaging.Debug;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CacheRecipes {

private final List<EnhancedRecipe> recipes = new ArrayList<>();
private final RecipeDatabase database;
private final SaveScheduler saveSchedule;

public CacheRecipes(final CraftEnhance craftEnhance) {
this.database = craftEnhance.getDatabase();
this.saveSchedule = craftEnhance.getSaveScheduler();
}

public List<EnhancedRecipe> getRecipes() {
return Collections.unmodifiableList(recipes);
}

@Nullable
public EnhancedRecipe getRecipe(final String recipeName) {
for (final EnhancedRecipe recipe : recipes) {
if (recipe.getKey().equals(recipeName))
return recipe;
}
return null;
}

public void add(EnhancedRecipe enhancedRecipe) {
if (enhancedRecipe == null) return;
recipes.add(enhancedRecipe);
}

public void addAll(List<EnhancedRecipe> enhancedRecipes) {
recipes.addAll(enhancedRecipes);
}

public void remove(EnhancedRecipe enhancedRecipe) {
EnhancedRecipe recipe = enhancedRecipe;
saveSchedule.addTask(() -> {
this.database.deleteRecipe(recipe);
});
recipes.remove(enhancedRecipe);
}

public boolean isUniqueRecipeKey(final String key) {
return this.getRecipe(key) == null;
}
public void clear() {
recipes.clear();
}

public void save() {
saveSchedule.addTask(this.database::saveRecipes);
}

public void save(EnhancedRecipe enhancedRecipe) {
saveSchedule.addTask(() -> this.database.saveRecipe(enhancedRecipe));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ public void handlePlayerCommand(Player p, String[] args) {
Messenger.MessageFromConfig("messages.commands.few-arguments", p, "2");
return;
}
EnhancedRecipe recipe = handler.getMain().getFm().getRecipe(args[0]);
EnhancedRecipe recipe = handler.getMain().getCacheRecipes().getRecipe(args[0]);
if(recipe == null) {
Messenger.Message("That recipe key doesn't exist", p);
return;
}

//TODO this method of changing the key changes the order of the recipes, which is a weird side-effect, so resolve this

handler.getMain().getFm().removeRecipe(recipe);
recipe.remove();
RecipeLoader.getInstance().unloadRecipe(recipe);
recipe.setKey(args[1]);
handler.getMain().getFm().saveRecipe(recipe);
recipe.save();
RecipeLoader.getInstance().loadRecipe(recipe);
Messenger.Message("The key has been changed to " + args[1] + ".", p);
}
Expand All @@ -51,15 +51,16 @@ public void handleConsoleCommand(CommandSender sender, String[] args) {
Messenger.MessageFromConfig("messages.commands.few-arguments", sender, "2");
return;
}
EnhancedRecipe recipe = handler.getMain().getFm().getRecipe(args[0]);
EnhancedRecipe recipe = handler.getMain().getCacheRecipes().getRecipe(args[0]);
if(recipe == null) {
Messenger.Message("That recipe key doesn't exist", sender);
return;
}
handler.getMain().getFm().removeRecipe(recipe);

recipe.remove();
RecipeLoader.getInstance().unloadRecipe(recipe);
recipe.setKey(args[1]);
handler.getMain().getFm().saveRecipe(recipe);
recipe.save();
RecipeLoader.getInstance().loadRecipe(recipe);
Messenger.Message("The key has been changed to " + args[1] + ".", sender);
}
Expand Down
Loading
Loading