telepathy enchant

This commit is contained in:
2024-10-18 20:31:54 +02:00
parent 4010e88237
commit 62ae69f3fa
7 changed files with 91 additions and 22 deletions
@@ -1,7 +1,7 @@
package me.youhavetrouble.enchantio;
import me.youhavetrouble.enchantio.listeners.SoulboundListener;
import me.youhavetrouble.enchantio.listeners.TelepathyListener;
import org.bukkit.plugin.java.JavaPlugin;
public final class Enchantio extends JavaPlugin {
@@ -9,7 +9,7 @@ public final class Enchantio extends JavaPlugin {
@Override
public void onEnable() {
getServer().getPluginManager().registerEvents(new SoulboundListener(), this);
getServer().getPluginManager().registerEvents(new TelepathyListener(), this);
}
@Override
@@ -6,9 +6,9 @@ import io.papermc.paper.plugin.bootstrap.PluginProviderContext;
import io.papermc.paper.registry.RegistryKey;
import io.papermc.paper.registry.TypedKey;
import io.papermc.paper.registry.event.RegistryEvents;
import io.papermc.paper.registry.keys.tags.ItemTypeTagKeys;
import me.youhavetrouble.enchantio.enchants.EnchantioEnchant;
import me.youhavetrouble.enchantio.enchants.SoulboundEnchant;
import me.youhavetrouble.enchantio.enchants.TelepathyEnchant;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
@@ -18,6 +18,7 @@ public class EnchantioBootstrap implements PluginBootstrap {
public void bootstrap(@NotNull BootstrapContext context) {
EnchantioEnchant soulbound = new SoulboundEnchant();
EnchantioEnchant telepathy = new TelepathyEnchant();
context.getLifecycleManager().registerEventHandler(RegistryEvents.ENCHANTMENT.freeze().newHandler(event -> {
for (EnchantioEnchant enchant : EnchantioEnchant.getEnchants().values()) {
@@ -29,7 +30,8 @@ public class EnchantioBootstrap implements PluginBootstrap {
enchantment.minimumCost(enchant.getMinimumCost());
enchantment.maximumCost(enchant.getMaximumCost());
enchantment.activeSlots(enchant.getActiveSlots());
enchantment.supportedItems(event.getOrCreateTag(ItemTypeTagKeys.ENCHANTABLE_ARMOR));
enchantment.supportedItems(event.getOrCreateTag(enchant.getSupportedItems()));
enchantment.primaryItems(event.getOrCreateTag(enchant.getPrimaryItems()));
});
}
}));
@@ -24,7 +24,8 @@ public abstract class EnchantioEnchant {
private final Component description;
private final EnchantmentRegistryEntry.EnchantmentCost minimumCost;
private final EnchantmentRegistryEntry.EnchantmentCost maximumCost;
private final Set<TagKey<ItemType>> supportedItems;
private final TagKey<ItemType> supportedItems;
private final TagKey<ItemType> primaryItems;
private final Set<EquipmentSlotGroup> activeSlots;
public EnchantioEnchant(
@@ -35,7 +36,9 @@ public abstract class EnchantioEnchant {
int weight,
EnchantmentRegistryEntry.EnchantmentCost minimumCost,
EnchantmentRegistryEntry.EnchantmentCost maximumCost,
Set<TagKey<ItemType>> supportedItems,
TagKey<ItemType> primaryItems,
TagKey<ItemType> supportedItems,
Set<EquipmentSlotGroup> activeSlots
) {
this.key = key;
@@ -45,6 +48,7 @@ public abstract class EnchantioEnchant {
this.weight = weight;
this.minimumCost = minimumCost;
this.maximumCost = maximumCost;
this.primaryItems = primaryItems;
this.supportedItems = supportedItems;
this.activeSlots = activeSlots;
}
@@ -77,10 +81,14 @@ public abstract class EnchantioEnchant {
return maximumCost;
}
public Set<TagKey<ItemType>> getSupportedItems() {
public TagKey<ItemType> getSupportedItems() {
return supportedItems;
}
public TagKey<ItemType> getPrimaryItems() {
return primaryItems;
}
public Iterable<EquipmentSlotGroup> getActiveSlots() {
return activeSlots;
}
@@ -8,27 +8,20 @@ import org.bukkit.inventory.EquipmentSlotGroup;
import java.util.Set;
public class SoulboundEnchant extends EnchantioEnchant {
public static final Key KEY = Key.key("enchantio:soulbound");
public SoulboundEnchant() {
super(
Key.key("enchantio:soulbound"),
KEY,
Component.translatable("enchantio.enchant.soulbound","Soulbound"),
1,
1,
10,
EnchantmentRegistryEntry.EnchantmentCost.of(1, 1),
EnchantmentRegistryEntry.EnchantmentCost.of(3, 1),
Set.of(
ItemTypeTagKeys.AXES,
ItemTypeTagKeys.PICKAXES,
ItemTypeTagKeys.SWORDS,
ItemTypeTagKeys.HOES,
ItemTypeTagKeys.SHOVELS,
ItemTypeTagKeys.ENCHANTABLE_BOW,
ItemTypeTagKeys.ENCHANTABLE_CROSSBOW,
ItemTypeTagKeys.ENCHANTABLE_MACE,
ItemTypeTagKeys.ENCHANTABLE_WEAPON,
ItemTypeTagKeys.ENCHANTABLE_ARMOR
),
ItemTypeTagKeys.ENCHANTABLE_ARMOR,
ItemTypeTagKeys.ENCHANTABLE_ARMOR,
Set.of(EquipmentSlotGroup.ANY)
);
registerEnchant(this);
@@ -0,0 +1,31 @@
package me.youhavetrouble.enchantio.enchants;
import io.papermc.paper.registry.data.EnchantmentRegistryEntry;
import io.papermc.paper.registry.keys.tags.ItemTypeTagKeys;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.bukkit.inventory.EquipmentSlotGroup;
import java.util.Set;
public class TelepathyEnchant extends EnchantioEnchant {
public static final Key KEY = Key.key("enchantio:telepathy");
public TelepathyEnchant() {
super(
KEY,
Component.translatable("enchantio.enchant.telepathy","Telepathy"),
1,
1,
4,
EnchantmentRegistryEntry.EnchantmentCost.of(1, 1),
EnchantmentRegistryEntry.EnchantmentCost.of(3, 1),
ItemTypeTagKeys.ENCHANTABLE_MINING,
ItemTypeTagKeys.ENCHANTABLE_MINING,
Set.of(EquipmentSlotGroup.ANY)
);
registerEnchant(this);
}
}
@@ -3,7 +3,7 @@ package me.youhavetrouble.enchantio.listeners;
import io.papermc.paper.registry.RegistryAccess;
import io.papermc.paper.registry.RegistryKey;
import me.youhavetrouble.enchantio.Enchantio;
import net.kyori.adventure.key.Key;
import me.youhavetrouble.enchantio.enchants.SoulboundEnchant;
import org.bukkit.Registry;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.EventHandler;
@@ -16,7 +16,7 @@ public class SoulboundListener implements Listener {
@EventHandler(ignoreCancelled = true, priority = EventPriority.NORMAL)
public void onSoulboundEnchantDeath(PlayerDeathEvent event) {
Registry<Enchantment> registry = RegistryAccess.registryAccess().getRegistry(RegistryKey.ENCHANTMENT);
Enchantment soulbound = registry.get(Key.key("enchantio:soulbound"));
Enchantment soulbound = registry.get(SoulboundEnchant.KEY);
Enchantio.getPlugin(Enchantio.class).getLogger().info("Soulbound enchantment: " + soulbound);
event.getPlayer().getInventory().forEach(itemStack -> {
@@ -0,0 +1,35 @@
package me.youhavetrouble.enchantio.listeners;
import io.papermc.paper.registry.RegistryAccess;
import io.papermc.paper.registry.RegistryKey;
import me.youhavetrouble.enchantio.enchants.TelepathyEnchant;
import org.bukkit.Registry;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockDropItemEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.ItemStack;
public class TelepathyListener implements Listener {
@EventHandler(ignoreCancelled = true, priority = EventPriority.NORMAL)
public void onTelepathyTool(BlockDropItemEvent event) {
Registry<Enchantment> registry = RegistryAccess.registryAccess().getRegistry(RegistryKey.ENCHANTMENT);
Enchantment telepathy = registry.get(TelepathyEnchant.KEY);
ItemStack tool = event.getPlayer().getInventory().getItemInMainHand();
if (!tool.getEnchantments().containsKey(telepathy)) return;
for (Item item : event.getItems()) {
item.teleport(event.getPlayer(), PlayerTeleportEvent.TeleportCause.PLUGIN);
item.setPickupDelay(0);
}
}
}