Skip to content

Commit

Permalink
Clean up some smells
Browse files Browse the repository at this point in the history
  • Loading branch information
Jikoo committed Jun 14, 2024
1 parent bb95465 commit 9e8ffe0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* Track and manage effects for enchanted furnace variants.
*/
class EnchantableFurnace extends EnchantableBlock {
public class EnchantableFurnace extends EnchantableBlock {

private static final String PATH_CAN_PAUSE = "silk.enabled";
private static final String PATH_FROZEN_TICKS = "silk.ticks";
Expand Down Expand Up @@ -314,7 +314,7 @@ short getFrozenTicks() {
* @param totalCookTime the original total cook time
* @return the modified cook time
*/
public short applyCookTimeModifiers(double totalCookTime) {
short applyCookTimeModifiers(double totalCookTime) {
// Invert sign of cook modifier to invert sigmoid.
return MathHelper.clampPositiveShort(
MathHelper.sigmoid(totalCookTime, -getCookModifier(), 2.0));
Expand All @@ -330,7 +330,7 @@ public short applyCookTimeModifiers(double totalCookTime) {
* @param burnTime the original burn time
* @return the modified burn time
*/
public short applyBurnTimeModifiers(int burnTime) {
short applyBurnTimeModifiers(int burnTime) {
// Apply burn time modifiers.
double baseTicks = MathHelper.sigmoid(burnTime, getBurnModifier(), 3.0);
// Apply cook speed reduction.
Expand All @@ -353,7 +353,7 @@ public String toString() {
* @param plugin the {@link Plugin} instance
* @param inventory the furnace inventory that may need an update
*/
public static void update(
static void update(
@NotNull Plugin plugin,
@NotNull EnchantableBlockManager manager,
@NotNull FurnaceInventory inventory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@
/**
* Configuration for {@link EnchantableFurnace EnchantableFurnaces}.
*/
class EnchantableFurnaceConfig extends EnchantableBlockConfig {
public class EnchantableFurnaceConfig extends EnchantableBlockConfig {

/** @deprecated use {@link #fortuneListIsBlacklist()} */
@Deprecated(forRemoval = true, since = "4.1.0")
public final @NotNull Setting<Boolean> fortuneListIsBlacklist;
/** @deprecated use {@link #fortuneList()} */
@Deprecated(forRemoval = true, since = "4.1.0")
public final @NotNull Setting<Set<Material>> fortuneList;
private final @NotNull Setting<Boolean> fortuneListIsBlacklist;
private final @NotNull Setting<Set<Material>> fortuneList;

/**
* Construct a new {@code EnchantableFurnaceConfig} with the given {@link ConfigurationSection}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,43 @@
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;

import com.github.jikoo.planarenchanting.util.ItemUtil;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.Server;
import org.bukkit.Tag;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.mockito.ArgumentMatchers;

public class EnchantmentMocks {
private static final Map<NamespacedKey, Enchantment> KEYS_TO_ENCHANTS = new HashMap<>();

public static void init(Server server) {
Registry<?> registry = Registry.ENCHANTMENT;
// Redirect enchantment registry back so that our modified version is always returned.
doReturn(registry).when(server).getRegistry(Enchantment.class);
private static final Tag<Material> TAG_EMPTY = new Tag<>() {
@Override
public boolean isTagged(@NotNull Material item) {
return false;
}

@NotNull
@Override
public Set<Material> getValues() {
return Set.of();
}

@NotNull
@Override
public NamespacedKey getKey() {
return Objects.requireNonNull(NamespacedKey.fromString("mock:empty"));
}
};

public static void init(Server server) {
List<Enchantment> protections = List.of(Enchantment.PROTECTION, Enchantment.FIRE_PROTECTION, Enchantment.BLAST_PROTECTION, Enchantment.PROJECTILE_PROTECTION);
setUpEnchant(Enchantment.PROTECTION, 4, Tag.ITEMS_ENCHANTABLE_ARMOR, protections);
setUpEnchant(Enchantment.FIRE_PROTECTION, 4, Tag.ITEMS_ENCHANTABLE_ARMOR, protections);
Expand All @@ -44,11 +54,11 @@ public static void init(Server server) {
setUpEnchant(Enchantment.THORNS, 3, Tag.ITEMS_ENCHANTABLE_ARMOR);

setUpEnchant(Enchantment.DEPTH_STRIDER, 3, Tag.ITEMS_ENCHANTABLE_FOOT_ARMOR, List.of(Enchantment.FROST_WALKER));
setUpEnchant(Enchantment.FROST_WALKER, 3, ItemUtil.TAG_EMPTY, Tag.ITEMS_ENCHANTABLE_FOOT_ARMOR, List.of(Enchantment.DEPTH_STRIDER));
setUpEnchant(Enchantment.SOUL_SPEED, 3, ItemUtil.TAG_EMPTY, Tag.ITEMS_ENCHANTABLE_FOOT_ARMOR, List.of());
setUpEnchant(Enchantment.SWIFT_SNEAK, 3, ItemUtil.TAG_EMPTY, Tag.ITEMS_ENCHANTABLE_LEG_ARMOR, List.of());
setUpEnchant(Enchantment.FROST_WALKER, 3, TAG_EMPTY, Tag.ITEMS_ENCHANTABLE_FOOT_ARMOR, List.of(Enchantment.DEPTH_STRIDER));
setUpEnchant(Enchantment.SOUL_SPEED, 3, TAG_EMPTY, Tag.ITEMS_ENCHANTABLE_FOOT_ARMOR, List.of());
setUpEnchant(Enchantment.SWIFT_SNEAK, 3, TAG_EMPTY, Tag.ITEMS_ENCHANTABLE_LEG_ARMOR, List.of());

setUpEnchant(Enchantment.BINDING_CURSE, 1, ItemUtil.TAG_EMPTY, Tag.ITEMS_ENCHANTABLE_EQUIPPABLE, List.of());
setUpEnchant(Enchantment.BINDING_CURSE, 1, TAG_EMPTY, Tag.ITEMS_ENCHANTABLE_EQUIPPABLE, List.of());

setUpEnchant(Enchantment.SHARPNESS, 5, Tag.ITEMS_ENCHANTABLE_SWORD, Tag.ITEMS_ENCHANTABLE_SHARP_WEAPON, List.of(Enchantment.BANE_OF_ARTHROPODS, Enchantment.SMITE));
setUpEnchant(Enchantment.SMITE, 5, Tag.ITEMS_ENCHANTABLE_SWORD, Tag.ITEMS_ENCHANTABLE_WEAPON, List.of(Enchantment.SHARPNESS, Enchantment.BANE_OF_ARTHROPODS));
Expand Down Expand Up @@ -81,23 +91,25 @@ public static void init(Server server) {
setUpEnchant(Enchantment.MULTISHOT, 1, Tag.ITEMS_ENCHANTABLE_CROSSBOW, List.of(Enchantment.PIERCING));
setUpEnchant(Enchantment.QUICK_CHARGE, 3, Tag.ITEMS_ENCHANTABLE_CROSSBOW);
setUpEnchant(Enchantment.PIERCING, 4, Tag.ITEMS_ENCHANTABLE_CROSSBOW, List.of(Enchantment.MULTISHOT));
setUpEnchant(Enchantment.WIND_BURST, 3, ItemUtil.TAG_EMPTY, Tag.ITEMS_ENCHANTABLE_MACE, List.of());
setUpEnchant(Enchantment.WIND_BURST, 3, TAG_EMPTY, Tag.ITEMS_ENCHANTABLE_MACE, List.of());
setUpEnchant(Enchantment.BREACH, 4, Tag.ITEMS_ENCHANTABLE_MACE);
setUpEnchant(Enchantment.DENSITY, 5, Tag.ITEMS_ENCHANTABLE_MACE);

setUpEnchant(Enchantment.MENDING, 1, ItemUtil.TAG_EMPTY, Tag.ITEMS_ENCHANTABLE_DURABILITY, List.of(Enchantment.INFINITY));
setUpEnchant(Enchantment.VANISHING_CURSE, 1, ItemUtil.TAG_EMPTY, Tag.ITEMS_ENCHANTABLE_VANISHING, List.of());
setUpEnchant(Enchantment.MENDING, 1, TAG_EMPTY, Tag.ITEMS_ENCHANTABLE_DURABILITY, List.of(Enchantment.INFINITY));
setUpEnchant(Enchantment.VANISHING_CURSE, 1, TAG_EMPTY, Tag.ITEMS_ENCHANTABLE_VANISHING, List.of());

checkMissingEnchantments();
}

private static void checkMissingEnchantments() {
Set<String> missingInternalEnchants = new HashSet<>();
try {
for (Field field : Enchantment.class.getFields()) {
if (Modifier.isStatic(field.getModifiers()) && Enchantment.class.equals(field.getType())) {
Enchantment declaredEnchant = (Enchantment) field.get(null);
Enchantment stored = KEYS_TO_ENCHANTS.get(declaredEnchant.getKey());
if (stored == null) {
// If max leveel is 0, enchantment was not set up.
if (declaredEnchant.getMaxLevel() == 0) {
missingInternalEnchants.add(declaredEnchant.getKey().toString());
} else {
doReturn(field.getName()).when(stored).getName();
}
}
}
Expand All @@ -108,17 +120,6 @@ public static void init(Server server) {
if (!missingInternalEnchants.isEmpty()) {
throw new IllegalStateException("Missing enchantment declarations for " + missingInternalEnchants);
}

// When all enchantments are initialized using Bukkit keys, redirect registry to our map
// so that invalid keys result in the expected null response.
doAnswer(invocation -> KEYS_TO_ENCHANTS.get(invocation.getArgument(0, NamespacedKey.class)))
.when(registry).get(ArgumentMatchers.notNull());
doAnswer(invocation -> KEYS_TO_ENCHANTS.values().stream()).when(registry).stream();
doAnswer(invocation -> KEYS_TO_ENCHANTS.values().iterator()).when(registry).iterator();
}

public static void putEnchant(@NotNull Enchantment enchantment) {
KEYS_TO_ENCHANTS.put(enchantment.getKey(), enchantment);
}

private static void setUpEnchant(
Expand All @@ -142,8 +143,6 @@ private static void setUpEnchant(
@NotNull Tag<Material> tableTarget,
@NotNull Tag<Material> anvilTarget,
@NotNull Collection<Enchantment> conflicts) {
KEYS_TO_ENCHANTS.put(enchantment.getKey(), enchantment);

doReturn(1).when(enchantment).getStartLevel();
doReturn(maxLevel).when(enchantment).getMaxLevel();
// Hopefully in the future the enchantment API gets expanded, making separate table+anvil targets available
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ void testCreateItemAir() {
@DisplayName("Items that do not have a corresponding block do not create blocks.")
@Test
void testCreateItemNotBlock() {
// TODO: items that are not blocks but are placed as blocks, i.e. cauldron item
var item = new ItemStack(Material.DIAMOND);
assertThat(
"Manager must not create block",
Expand Down

0 comments on commit 9e8ffe0

Please sign in to comment.