Skip to content

Commit

Permalink
Move to default methods
Browse files Browse the repository at this point in the history
This isn't great practice, so I may change it again or convert to an abstract class.
  • Loading branch information
Jikoo committed Dec 31, 2024
1 parent 385673a commit cdaf583
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 86 deletions.
6 changes: 3 additions & 3 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ An implementation that only allows the input to be renamed:
```java
class RenameOnlyAnvil {
public void getResult(@NotNull AnvilView view) {
AnvilState state = new AnvilState(VanillaAnvil.BEHAVIOR, view);
AnvilState state = new AnvilState(AnvilBehavior.VANILLA, view);

// Require first item to be set, second item to be unset.
if (ItemUtil.isEmpty(state.getBase().getItem())
Expand Down Expand Up @@ -148,7 +148,7 @@ Leiningen.

### Maven

Replace `$planarVersion` with the version you desire to work with. The `minimizeJar` option is
Replace `${versions.planarenchanting}` with the version you desire to work with or define it in your properties. The `minimizeJar` option is
recommended to prevent inflating your plugin with unnecessary classes.

Sample configuration:
Expand All @@ -166,7 +166,7 @@ Sample configuration:
<dependency>
<groupId>com.github.jikoo</groupId>
<artifactId>planarenchanting</artifactId>
<version>$planarVersion</version>
<version>${versions.planarenchanting}</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
package com.github.jikoo.planarenchanting.anvil;

import com.github.jikoo.planarenchanting.util.MetaCachedStack;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

public interface AnvilBehavior {

static AnvilBehavior VANILLA = new AnvilBehavior() {};

/**
* Get whether an {@link Enchantment} is applicable for a wrapped {@link ItemStack}.
*
* @param enchantment the {@code Enchantment} to check for applicability
* @param base the item that may be enchanted
* @return whether the {@code Enchantment} can be applied
*/
boolean enchantApplies(@NotNull Enchantment enchantment, @NotNull MetaCachedStack base);
default boolean enchantApplies(@NotNull Enchantment enchantment, @NotNull MetaCachedStack base) {
return enchantment.canEnchantItem(base.getItem());
}

/**
* Get whether two {@link Enchantment Enchantments} conflict.
*
* @return whether the {@code Enchantments} conflict
*/
boolean enchantsConflict(@NotNull Enchantment enchant1, @NotNull Enchantment enchant2);
default boolean enchantsConflict(@NotNull Enchantment enchant1, @NotNull Enchantment enchant2) {
return enchant1.conflictsWith(enchant2);
}

/**
* Get the maximum level for an {@link Enchantment}.
*
* @return the maximum level for an {@code Enchantment}
*/
int getEnchantMaxLevel(@NotNull Enchantment enchantment);
default int getEnchantMaxLevel(@NotNull Enchantment enchantment) {
return enchantment.getMaxLevel();
}

/**
* Get whether an item should combine its {@link Enchantment Enchantments} with another item.
Expand All @@ -37,7 +46,10 @@ public interface AnvilBehavior {
* @param addition the item added
* @return whether items should combine {@code Enchantments}
*/
boolean itemsCombineEnchants(@NotNull MetaCachedStack base, @NotNull MetaCachedStack addition);
default boolean itemsCombineEnchants(@NotNull MetaCachedStack base, @NotNull MetaCachedStack addition) {
Material additionType = addition.getItem().getType();
return base.getItem().getType() == additionType || additionType == Material.ENCHANTED_BOOK;
}

/**
* Get whether an item is repaired by another item. This is not the same as a repair via
Expand All @@ -49,6 +61,8 @@ public interface AnvilBehavior {
* @param repairMat the item used to repair
* @return the method determining whether an item is repaired by another item
*/
public boolean itemRepairedBy(@NotNull MetaCachedStack repaired, @NotNull MetaCachedStack repairMat);
default boolean itemRepairedBy(@NotNull MetaCachedStack repaired, @NotNull MetaCachedStack repairMat) {
return RepairMaterial.repairs(repaired.getItem(), repairMat.getItem());
}

}
Original file line number Diff line number Diff line change
@@ -1,46 +1,15 @@
package com.github.jikoo.planarenchanting.anvil;

import com.github.jikoo.planarenchanting.util.ItemUtil;
import com.github.jikoo.planarenchanting.util.MetaCachedStack;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.view.AnvilView;
import org.jetbrains.annotations.NotNull;

public class VanillaAnvil {

public static final AnvilBehavior BEHAVIOR = new AnvilBehavior() {
@Override
public boolean enchantApplies(@NotNull Enchantment enchantment, @NotNull MetaCachedStack base) {
return enchantment.canEnchantItem(base.getItem());
}

@Override
public boolean enchantsConflict(@NotNull Enchantment enchant1, @NotNull Enchantment enchant2) {
return enchant1.conflictsWith(enchant2);
}

@Override
public int getEnchantMaxLevel(@NotNull Enchantment enchantment) {
return enchantment.getMaxLevel();
}

@Override
public boolean itemsCombineEnchants(@NotNull MetaCachedStack base, @NotNull MetaCachedStack addition) {
Material additionType = addition.getItem().getType();
return base.getItem().getType() == additionType || additionType == Material.ENCHANTED_BOOK;
}

@Override
public boolean itemRepairedBy(@NotNull MetaCachedStack repaired, @NotNull MetaCachedStack repairMat) {
return RepairMaterial.repairs(repaired.getItem(), repairMat.getItem());
}
};

private final @NotNull AnvilBehavior behavior;

public VanillaAnvil() {
this(BEHAVIOR);
this(AnvilBehavior.VANILLA);
}

public VanillaAnvil(@NotNull AnvilBehavior behavior) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class PriorWorkLevelCost {
@Test
void testPriorWorkLevelCostApplies() {
var anvil = getMockView(new ItemStack(BASE_MAT), new ItemStack(BASE_MAT));
var behavior = VanillaAnvil.BEHAVIOR;
var behavior = AnvilBehavior.VANILLA;
var state = new AnvilState(behavior, anvil);

assertThat("Prior work level cost always applies", function.canApply(behavior, state));
Expand All @@ -99,7 +99,7 @@ void testPriorWorkLevelCostValues(int baseWork, int addedWork) {
var anvil = getMockView(
prepareItem(baseItem, 0, baseWork),
prepareItem(new ItemStack(BASE_MAT), 0, addedWork));
var behavior = VanillaAnvil.BEHAVIOR;
var behavior = AnvilBehavior.VANILLA;
var state = new ReadableResultState(behavior, anvil);

assertThat("Prior work level cost always applies", function.canApply(behavior, state));
Expand Down Expand Up @@ -144,7 +144,7 @@ class Rename {
void testRenameRequiresMeta() {
var base = getNullMetaItem();
var inventory = getMockView(base, null);
var behavior = VanillaAnvil.BEHAVIOR;
var behavior = AnvilBehavior.VANILLA;
var state = new AnvilState(behavior, inventory);

assertThat("Base meta is null", state.getBase().getMeta(), is(nullValue()));
Expand All @@ -159,7 +159,7 @@ void testRenameRequiresDifferentName(
boolean canApply) {
var base = new ItemStack(BASE_MAT);
var inventory = getMockView(base, null);
var behavior = VanillaAnvil.BEHAVIOR;
var behavior = AnvilBehavior.VANILLA;
var state = new AnvilState(behavior, inventory);

assertThat("Base meta is not null", state.getBase().getMeta(), is(notNullValue()));
Expand All @@ -181,7 +181,7 @@ void testRenameApplication(BiConsumer<ItemMeta, AnvilView> setup) {
base.setItemMeta(baseMeta);
inventory.setItem(0, base);

var behavior = VanillaAnvil.BEHAVIOR;
var behavior = AnvilBehavior.VANILLA;
var state = new ReadableResultState(behavior, inventory);

AnvilFunctionResult result = function.getResult(behavior, state);
Expand Down Expand Up @@ -244,7 +244,7 @@ class UpdatePriorWorkCost {
void testBaseNotRepairable() {
var base = getNullMetaItem();
var inventory = getMockView(base, null);
var behavior = VanillaAnvil.BEHAVIOR;
var behavior = AnvilBehavior.VANILLA;
var state = new AnvilState(behavior, inventory);

assertThat(
Expand All @@ -258,7 +258,7 @@ void testBaseRepairable() {
var base = new ItemStack(BASE_MAT);
var addition = getNullMetaItem();
var inventory = getMockView(base, addition);
var behavior = VanillaAnvil.BEHAVIOR;
var behavior = AnvilBehavior.VANILLA;
var state = new AnvilState(behavior, inventory);

assertThat(
Expand All @@ -274,7 +274,7 @@ void testPriorWorkUpdate(int baseWork, int addedWork) {
var anvil = getMockView(
prepareItem(baseItem, 0, baseWork),
prepareItem(new ItemStack(BASE_MAT), 0, addedWork));
var behavior = VanillaAnvil.BEHAVIOR;
var behavior = AnvilBehavior.VANILLA;
var state = new ReadableResultState(behavior, anvil);

assertThat("Prior work level cost always applies", function.canApply(behavior, state));
Expand Down Expand Up @@ -304,7 +304,7 @@ void testPriorWorkUpdate(int baseWork, int addedWork) {
void testMetaNotRepairable() {
var base = getNullMetaItem();
var inventory = getMockView(base, null);
var behavior = VanillaAnvil.BEHAVIOR;
var behavior = AnvilBehavior.VANILLA;
var state = new AnvilState(behavior, inventory);

var result = function.getResult(behavior, state);
Expand All @@ -321,7 +321,7 @@ class RepairWithMaterial {
@Test
void testCanApplyNotRepairedBy() {
var inventory = getMockView(null, null);
var behavior = spy(VanillaAnvil.BEHAVIOR);
var behavior = spy(AnvilBehavior.VANILLA);
doReturn(false).when(behavior).itemRepairedBy(notNull(), notNull());
var state = new AnvilState(behavior, inventory);

Expand All @@ -334,7 +334,7 @@ void testCanApplyNotRepairedBy() {
@Test
void testCanApplyNotDurable() {
var inventory = getMockView(null, null);
var behavior = spy(VanillaAnvil.BEHAVIOR);
var behavior = spy(AnvilBehavior.VANILLA);
doReturn(true).when(behavior).itemRepairedBy(notNull(), notNull());
var state = new AnvilState(behavior, inventory);

Expand All @@ -349,7 +349,7 @@ void testCanApplyNotDamageable() {
var base = getNullMetaItem();
base.setType(BASE_MAT);
var inventory = getMockView(base, null);
var behavior = spy(VanillaAnvil.BEHAVIOR);
var behavior = spy(AnvilBehavior.VANILLA);
doReturn(true).when(behavior).itemRepairedBy(notNull(), notNull());
var state = new AnvilState(behavior, inventory);

Expand All @@ -366,7 +366,7 @@ void testCanApplyNotDamageable() {
@Test
void testCanApplyNotDamaged() {
var inventory = getMockView(new ItemStack(BASE_MAT), null);
var behavior = spy(VanillaAnvil.BEHAVIOR);
var behavior = spy(AnvilBehavior.VANILLA);
doReturn(true).when(behavior).itemRepairedBy(notNull(), notNull());
var state = new AnvilState(behavior, inventory);

Expand All @@ -385,7 +385,7 @@ void testCanApplyNotDamaged() {
void testRepair(int repairMats) {
var baseItem = getMaxDamageItem();
var inventory = getMockView(baseItem, new ItemStack(REPAIR_MAT, repairMats));
var behavior = spy(VanillaAnvil.BEHAVIOR);
var behavior = spy(AnvilBehavior.VANILLA);
doReturn(true).when(behavior).itemRepairedBy(notNull(), notNull());
var state = new ReadableResultState(behavior, inventory);

Expand Down Expand Up @@ -435,7 +435,7 @@ class RepairWithCombination {
@Test
void testCanApplyNotRepairedBy() {
var inventory = getMockView(new ItemStack(BASE_MAT), new ItemStack(REPAIR_MAT));
var behavior = VanillaAnvil.BEHAVIOR;
var behavior = AnvilBehavior.VANILLA;
var state = new AnvilState(behavior, inventory);

assertThat(
Expand All @@ -447,7 +447,7 @@ void testCanApplyNotRepairedBy() {
@Test
void testCanApplyNotDurable() {
var inventory = getMockView(null, null);
var behavior = VanillaAnvil.BEHAVIOR;
var behavior = AnvilBehavior.VANILLA;
var state = new AnvilState(behavior, inventory);

assertThat(
Expand All @@ -462,7 +462,7 @@ void testCanApplyNotDamageable() {
nullMetaItem.setType(BASE_MAT);
ItemStack normalItem = new ItemStack(BASE_MAT);
var inventory = getMockView(nullMetaItem, normalItem);
var behavior = VanillaAnvil.BEHAVIOR;
var behavior = AnvilBehavior.VANILLA;
var state = new AnvilState(behavior, inventory);

assertThat(
Expand All @@ -484,7 +484,7 @@ void testCanApplyNotDamageable() {
@Test
void testCanApplyNotDamaged() {
var inventory = getMockView(new ItemStack(BASE_MAT), new ItemStack(BASE_MAT));
var behavior = VanillaAnvil.BEHAVIOR;
var behavior = AnvilBehavior.VANILLA;
var state = new AnvilState(behavior, inventory);

assertThat(
Expand All @@ -501,7 +501,7 @@ void testCanApplyNotDamaged() {
void testRepair() {
var baseItem = getMaxDamageItem();
var inventory = getMockView(baseItem, baseItem.clone());
var behavior = VanillaAnvil.BEHAVIOR;
var behavior = AnvilBehavior.VANILLA;
var state = new ReadableResultState(behavior, inventory);

assertThat(
Expand Down
Loading

0 comments on commit cdaf583

Please sign in to comment.