Skip to content

Commit

Permalink
Add some testing (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrea authored Nov 18, 2024
1 parent c42140a commit ed58c9c
Show file tree
Hide file tree
Showing 34 changed files with 1,910 additions and 149 deletions.
14 changes: 10 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ dependencies {
compileOnly("org.spigotmc:spigot-api:1.18-R0.1-SNAPSHOT")

// Gui library
implementation("com.github.stefvanschie.inventoryframework:IF:0.10.17")
val stefvanschie_IF = "com.github.stefvanschie.inventoryframework:IF:0.10.17"
implementation(stefvanschie_IF)
testRuntimeOnly(stefvanschie_IF)

// EnchantsSquaredRewritten
compileOnly(files("libs/EnchantsSquared.jar"))
Expand Down Expand Up @@ -61,6 +63,10 @@ dependencies {

// include kotlin for the offline jar
implementation(kotlin("stdlib"))

// Test dependency
testImplementation("org.mockbukkit.mockbukkit:mockbukkit-v1.21:4.3.1")
testRuntimeOnly("commons-lang:commons-lang:2.6")
}

allprojects {
Expand All @@ -80,8 +86,8 @@ allprojects {
dependencies {
compileOnly(kotlin("stdlib"))

// Currently not used. but it would be useful to test.
testImplementation(platform("org.junit:junit-bom:5.9.1"))
// Test dependency
testImplementation(platform("org.junit:junit-bom:5.11.3"))
testImplementation("org.junit.jupiter:junit-jupiter")
}

Expand All @@ -92,7 +98,7 @@ allprojects {
// Configure used version of kotlin and java
java {
disableAutoTargetJvm()
toolchain.languageVersion.set(JavaLanguageVersion.of(20))
toolchain.languageVersion.set(JavaLanguageVersion.of(21))
}

// Set target version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,28 @@ import xyz.alexcrea.cuanvil.dependency.gui.ExternGuiTester
class v1_21R1_ExternGuiTester: ExternGuiTester {
override val wesjdAnvilGuiName = "Wrapper1_21_R1"

var tested = false;
var possible = false;

override fun getContainerClass(view: InventoryView): Class<Any>? {
// In case we are in a test environment
if(!tested) testClassExist()
if(!possible) return null

if(view !is CraftInventoryView<*, *>) return null
val container = view.handle

return container.javaClass
}

fun testClassExist(){
tested = true;
try {
Class.forName("org.bukkit.craftbukkit.inventory.CraftInventoryView")
possible = true
} catch (e: ClassNotFoundException){
possible = false
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public AnvilRecipeBuilder setResultItem(ItemStack resultItem) {
*/
@Nullable // null if missing argument
public AnvilCustomRecipe build() {
if(leftItem == null || rightItem == null) return null;
if(leftItem == null || resultItem == null) return null;

return new AnvilCustomRecipe(
this.name,
Expand Down
68 changes: 36 additions & 32 deletions src/main/java/xyz/alexcrea/cuanvil/api/ConflictAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,49 @@
@SuppressWarnings("unused")
public class ConflictAPI {

private ConflictAPI() {}
private ConflictAPI() {
}

private static Object saveChangeTask = null;
private static Object reloadChangeTask = null;

/**
* Write and add a conflict.
* Will not write the conflict if it already exists.
* Will not be successful if the conflict is empty.
*
* @param builder The conflict builder to be based on
* @return True if successful.
*/
public static boolean addConflict(@NotNull ConflictBuilder builder){
public static boolean addConflict(@NotNull ConflictBuilder builder) {
return addConflict(builder, false);
}

/**
* Write and add a conflict.
* Will not write the conflict if it already exists.
* Will not be successful if the conflict is empty.
*
* @param builder The conflict builder to be based on
* @param builder The conflict builder to be based on
* @param overrideDeleted If we should write even if the conflict was previously deleted.
* @return True if successful.
*/
public static boolean addConflict(@NotNull ConflictBuilder builder, boolean overrideDeleted){
public static boolean addConflict(@NotNull ConflictBuilder builder, boolean overrideDeleted) {
FileConfiguration config = ConfigHolder.CONFLICT_HOLDER.getConfig();

// Test if conflict can be added
if(!overrideDeleted && ConfigHolder.CONFLICT_HOLDER.isDeleted(builder.getName())) return false;
if(config.contains(builder.getName())) return false;

if(!writeConflict(builder, false)) return false;
if (!overrideDeleted && ConfigHolder.CONFLICT_HOLDER.isDeleted(builder.getName())) return false;
if (config.contains(builder.getName())) return false;

if (!writeConflict(builder, false)) return false;

EnchantConflictGroup conflict = builder.build();
// Register conflict
ConfigHolder.CONFLICT_HOLDER.getConflictManager().addConflict(conflict);

// Add conflict to gui
EnchantConflictGui conflictGui = EnchantConflictGui.getCurrentInstance();
if(conflictGui != null) conflictGui.updateValueForGeneric(conflict, true);
if (conflictGui != null) conflictGui.updateValueForGeneric(conflict, true);

return true;
}
Expand All @@ -69,10 +71,10 @@ public static boolean addConflict(@NotNull ConflictBuilder builder, boolean over
* <p>
* You may want to use {@link #addConflict(ConflictBuilder)} instead as it is more performance in most case as this function will reload every conflict.
*
* @param builder The builder
* @return True if successful.
* @param builder the builder
* @return true if was written successfully.
*/
public static boolean writeConflict(@NotNull ConflictBuilder builder){
public static boolean writeConflict(@NotNull ConflictBuilder builder) {
return writeConflict(builder, true);
}

Expand All @@ -83,14 +85,14 @@ public static boolean writeConflict(@NotNull ConflictBuilder builder){
*
* @param builder The builder
* @param updatePlanned If we should plan a global update for conflicts
* @return True if successful.
* @return true if was written successfully.
*/
public static boolean writeConflict(@NotNull ConflictBuilder builder, boolean updatePlanned){
public static boolean writeConflict(@NotNull ConflictBuilder builder, boolean updatePlanned) {
FileConfiguration config = ConfigHolder.CONFLICT_HOLDER.getConfig();

String name = builder.getName();
if(name.contains(".")) {
CustomAnvil.instance.getLogger().warning("Conflict " + name +" contain \".\" in its name but should not. this conflict is ignored.");
if (name.contains(".")) {
CustomAnvil.instance.getLogger().warning("Conflict " + name + " contain \".\" in its name but should not. this conflict is ignored.");
logConflictOrigin(builder);
return false;
}
Expand All @@ -99,24 +101,27 @@ public static boolean writeConflict(@NotNull ConflictBuilder builder, boolean up

List<String> enchantments = extractEnchantments(builder);
List<String> excludedGroups = new ArrayList<>(builder.getExcludedGroupNames());
if(!enchantments.isEmpty()) config.set(basePath + "enchantments", enchantments);
if(!excludedGroups.isEmpty()) config.set(basePath + "notAffectedGroups", excludedGroups);
if(builder.getMaxBeforeConflict() > 0) config.set(basePath + "maxEnchantmentBeforeConflict", builder.getMaxBeforeConflict());
if (!enchantments.isEmpty()) config.set(basePath + "enchantments", enchantments);
if (!excludedGroups.isEmpty()) config.set(basePath + "notAffectedGroups", excludedGroups);
if (builder.getMaxBeforeConflict() > 0)
config.set(basePath + "maxEnchantmentBeforeConflict", builder.getMaxBeforeConflict());

if (!config.isConfigurationSection(name)) return false;

prepareSaveTask();
if(updatePlanned) prepareUpdateTask();
if (updatePlanned) prepareUpdateTask();

return true;
}

/**
* Extract every enchantment names from a builder.
*
* @param builder The builder storing the enchantments
* @return Builder's stored enchantment.
*/
@NotNull
private static List<String> extractEnchantments(@NotNull ConflictBuilder builder){
private static List<String> extractEnchantments(@NotNull ConflictBuilder builder) {
List<String> result = new ArrayList<>(builder.getEnchantmentNames());
for (NamespacedKey enchantmentKey : builder.getEnchantmentKeys()) {
result.add(enchantmentKey.toString());
Expand All @@ -131,7 +136,7 @@ private static List<String> extractEnchantments(@NotNull ConflictBuilder builder
* @param conflict The conflict to remove
* @return True if successful.
*/
public static boolean removeConflict(@NotNull EnchantConflictGroup conflict){
public static boolean removeConflict(@NotNull EnchantConflictGroup conflict) {
// Remove from registry
ConfigHolder.CONFLICT_HOLDER.getConflictManager().removeConflict(conflict);

Expand All @@ -141,8 +146,7 @@ public static boolean removeConflict(@NotNull EnchantConflictGroup conflict){

// Remove from gui
EnchantConflictGui conflictGui = EnchantConflictGui.getCurrentInstance();
if(conflictGui != null) conflictGui.removeGeneric(conflict);

if (conflictGui != null) conflictGui.removeGeneric(conflict);

return true;
}
Expand All @@ -151,9 +155,9 @@ public static boolean removeConflict(@NotNull EnchantConflictGroup conflict){
* Prepare a task to save conflict configuration.
*/
private static void prepareSaveTask() {
if(saveChangeTask != null) return;
if (saveChangeTask != null) return;

saveChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, ()->{
saveChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, () -> {
ConfigHolder.CONFLICT_HOLDER.saveToDisk(true);
saveChangeTask = null;
});
Expand All @@ -163,28 +167,28 @@ private static void prepareSaveTask() {
* Prepare a task to reload every conflict.
*/
private static void prepareUpdateTask() {
if(reloadChangeTask != null) return;
if (reloadChangeTask != null) return;

reloadChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, ()->{
reloadChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, () -> {
ConfigHolder.CONFLICT_HOLDER.reload();
EnchantConflictGui conflictGui = EnchantConflictGui.getCurrentInstance();
if(conflictGui != null) conflictGui.reloadValues();
if (conflictGui != null) conflictGui.reloadValues();

reloadChangeTask = null;
});

}

static void logConflictOrigin(@NotNull ConflictBuilder builder){
static void logConflictOrigin(@NotNull ConflictBuilder builder) {
CustomAnvil.instance.getLogger().warning("Conflict " + builder.getName() + " came from " + builder.getSourceName() + ".");
}

/**
* Get every registered conflict.
*
* @return An immutable collection of conflict.
*/
@NotNull
public static List<EnchantConflictGroup> getRegisteredConflict(){
public static List<EnchantConflictGroup> getRegisteredConflict() {
List<EnchantConflictGroup> mutableList = ConfigHolder.CONFLICT_HOLDER.getConflictManager().getConflictList();
return Collections.unmodifiableList(mutableList);
}
Expand Down
Loading

0 comments on commit ed58c9c

Please sign in to comment.