Skip to content

Commit

Permalink
Add Deflective Retribution enchantment
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrykCholewa committed Nov 17, 2024
1 parent 304c4ef commit 24fc80e
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 6 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@ Works on Paper

- Adds additional drops, when fox is killed by a player attack.
- Adds new Curse of Anchoring enchantment (if GravesX is used then don't work properly on storage.mode=EXACT)
- Adds new Deflective Retribution enchantment (can be crafted with recipe below)

| | A | |
|---|---|---|
| B | C | D |
| A | E | A |

A - lapis, B - spectral arrow, C - book, D - tipped arrow, E - arrow
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.github.PatrykCholewa.FoxKiller;

import com.github.PatrykCholewa.FoxKiller.listeners.CurseOfAnchoringEnchantListener;
import com.github.PatrykCholewa.FoxKiller.listeners.DeflectiveRetributionEnchantListener;
import com.github.PatrykCholewa.FoxKiller.listeners.FoxKillListener;
import com.github.PatrykCholewa.FoxKiller.recipes.RecipeDefinitions;
import org.bukkit.Bukkit;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.Plugin;
Expand All @@ -24,6 +26,10 @@ public void onEnable() {
PluginManager pluginManager = Bukkit.getPluginManager();
pluginManager.registerEvents(new FoxKillListener(), instance);
pluginManager.registerEvents(new CurseOfAnchoringEnchantListener(), instance);
pluginManager.registerEvents(new DeflectiveRetributionEnchantListener(), instance);

new RecipeDefinitions(instance).createDefinitions()
.forEach(getServer()::addRecipe);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public class EnchantmentAssignmentBuilder {
private final String keyName;
private Component title;
private TagKey<ItemType> supportedItems;
private RegistryKeySet<ItemType> supportedItemsExact;
private TagKey<ItemType> primaryItems;
private RegistryKeySet<ItemType> primaryItemsExact;
private int weight = 1;
private int maxLevel = 1;
private EnchantmentRegistryEntry.EnchantmentCost minimumCost = EnchantmentRegistryEntry.EnchantmentCost.of(30, 1);
Expand All @@ -41,11 +43,21 @@ public EnchantmentAssignmentBuilder supportedItems(TagKey<ItemType> supportedIte
return this;
}

public EnchantmentAssignmentBuilder supportedItems(RegistryKeySet<ItemType> supportedItems) {
this.supportedItemsExact = supportedItems;
return this;
}

public EnchantmentAssignmentBuilder primaryItems(TagKey<ItemType> primaryItems) {
this.primaryItems = primaryItems;
return this;
}

public EnchantmentAssignmentBuilder primaryItems(RegistryKeySet<ItemType> primaryItems) {
this.primaryItemsExact = primaryItems;
return this;
}

public EnchantmentAssignmentBuilder weight(int weight) {
this.weight = weight;
return this;
Expand Down Expand Up @@ -95,10 +107,14 @@ private EnchantmentRegistryEntry.Builder apply(RegistryFreezeEvent<?, ?> event,
.anvilCost(anvilCost)
.activeSlots(equipmentSlotGroups);

if (supportedItems != null) {
if (supportedItemsExact != null) {
builder = builder.supportedItems(supportedItemsExact);
} else if (supportedItems != null) {
builder = builder.supportedItems(event.getOrCreateTag(supportedItems));
}
if (primaryItems != null) {
if (primaryItemsExact != null) {
builder = builder.primaryItems(primaryItemsExact);
} else if (primaryItems != null) {
builder = builder.primaryItems(event.getOrCreateTag(primaryItems));
}
if (exclusiveWith != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.List;

import static io.papermc.paper.registry.keys.EnchantmentKeys.VANISHING_CURSE;
import static io.papermc.paper.registry.keys.ItemTypeKeys.BOOK;
import static io.papermc.paper.registry.keys.ItemTypeKeys.SHIELD;

public class EnchantmentDefinitions {

Expand All @@ -20,7 +22,13 @@ public class EnchantmentDefinitions {
.exclusiveWith(RegistrySet.keySet(RegistryKey.ENCHANTMENT, List.of(VANISHING_CURSE)))
.build();

public static EnchantmentAssignment DEFLECTIVE_RETRIBUTION = new EnchantmentAssignmentBuilder("deflective_retribution")
.title(Component.text("Deflective Retribution"))
.primaryItems(RegistrySet.keySet(RegistryKey.ITEM, List.of(SHIELD, BOOK)))
.supportedItems(RegistrySet.keySet(RegistryKey.ITEM, List.of(SHIELD)))
.build();

public static Collection<EnchantmentAssignment> getDefinitions() {
return Lists.newArrayList(CURSE_OF_ANCHORING);
return Lists.newArrayList(CURSE_OF_ANCHORING, DEFLECTIVE_RETRIBUTION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
import java.util.List;
import java.util.Optional;

import static com.github.PatrykCholewa.FoxKiller.utils.EnchantmentUtils.getEnchantmentRef;

public class CurseOfAnchoringEnchantListener implements Listener {

private static final Enchantment ENCHANTMENT = RegistryAccess.registryAccess()
.getRegistry(RegistryKey.ENCHANTMENT)
.get(EnchantmentDefinitions.CURSE_OF_ANCHORING.getKey());
private static final Enchantment ENCHANTMENT = getEnchantmentRef(EnchantmentDefinitions.CURSE_OF_ANCHORING);

@EventHandler
public static void onDeath(PlayerDeathEvent event) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.github.PatrykCholewa.FoxKiller.listeners;

import com.github.PatrykCholewa.FoxKiller.enchantments.EnchantmentDefinitions;
import io.papermc.paper.registry.RegistryAccess;
import io.papermc.paper.registry.RegistryKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;

import static com.github.PatrykCholewa.FoxKiller.utils.EnchantmentUtils.getEnchantmentRef;

public class DeflectiveRetributionEnchantListener implements Listener {

private static final Enchantment ENCHANTMENT = getEnchantmentRef(EnchantmentDefinitions.DEFLECTIVE_RETRIBUTION);

@EventHandler
public static void onDeath(EntityDamageByEntityEvent event) {
if (!(event.getEntity() instanceof Player)) {
return;
}

Player player = (Player) event.getEntity();

if (!player.getActiveItem().containsEnchantment(ENCHANTMENT)) {
return;
}

if (!player.isBlocking() && event.getDamage(EntityDamageEvent.DamageModifier.BLOCKING) >= 0) {
return;
}

Entity cause = event.getDamageSource().getDirectEntity();

if (!(cause instanceof Projectile)) {
return;
}

cause.setVelocity(cause.getVelocity().multiply(25));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.github.PatrykCholewa.FoxKiller.recipes;

import com.github.PatrykCholewa.FoxKiller.enchantments.EnchantmentDefinitions;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.ShapelessRecipe;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.Plugin;

import java.util.List;

import static com.github.PatrykCholewa.FoxKiller.utils.EnchantmentUtils.getEnchantmentRef;


public class RecipeDefinitions {

private Plugin plugin;

public RecipeDefinitions(Plugin plugin) {
this.plugin = plugin;
}

public List<Recipe> createDefinitions() {
return List.of(createDeflectiveRetributionBook());
}

private Recipe createDeflectiveRetributionBook() {
NamespacedKey key = new NamespacedKey(plugin, "WarriorSword");
ItemStack item = new ItemStack(Material.ENCHANTED_BOOK);
EnchantmentStorageMeta itemMeta = (EnchantmentStorageMeta) item.getItemMeta();
itemMeta.addStoredEnchant(getEnchantmentRef(EnchantmentDefinitions.DEFLECTIVE_RETRIBUTION), 1, false);
item.setItemMeta(itemMeta);

ShapedRecipe recipe = new ShapedRecipe(key, item);
recipe.shape(" A ", "BCD", "AEA");
recipe.setIngredient('A', Material.LAPIS_LAZULI);
recipe.setIngredient('C', Material.BOOK);
recipe.setIngredient('B', Material.SPECTRAL_ARROW);
recipe.setIngredient('D', Material.TIPPED_ARROW);
recipe.setIngredient('E', Material.ARROW);

return recipe;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.PatrykCholewa.FoxKiller.utils;

import com.github.PatrykCholewa.FoxKiller.enchantments.EnchantmentAssignment;
import io.papermc.paper.registry.RegistryAccess;
import io.papermc.paper.registry.RegistryKey;
import org.bukkit.enchantments.Enchantment;

public class EnchantmentUtils {

private EnchantmentUtils() {

}

public static Enchantment getEnchantmentRef(EnchantmentAssignment enchantmentAssignment) {
return RegistryAccess.registryAccess()
.getRegistry(RegistryKey.ENCHANTMENT)
.get(enchantmentAssignment.getKey());
}

}

0 comments on commit 24fc80e

Please sign in to comment.