mirror of
https://github.com/YouHaveTrouble/Enchantio.git
synced 2026-05-11 21:56:55 +00:00
add replanting enchant
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package me.youhavetrouble.enchantio;
|
package me.youhavetrouble.enchantio;
|
||||||
|
|
||||||
|
import me.youhavetrouble.enchantio.listeners.ReplantingListener;
|
||||||
import me.youhavetrouble.enchantio.listeners.SoulboundListener;
|
import me.youhavetrouble.enchantio.listeners.SoulboundListener;
|
||||||
import me.youhavetrouble.enchantio.listeners.TelepathyListener;
|
import me.youhavetrouble.enchantio.listeners.TelepathyListener;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
@@ -8,9 +9,9 @@ public final class Enchantio extends JavaPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
|
|
||||||
getServer().getPluginManager().registerEvents(new SoulboundListener(), this);
|
getServer().getPluginManager().registerEvents(new SoulboundListener(), this);
|
||||||
getServer().getPluginManager().registerEvents(new TelepathyListener(), this);
|
getServer().getPluginManager().registerEvents(new TelepathyListener(), this);
|
||||||
|
getServer().getPluginManager().registerEvents(new ReplantingListener(), this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import io.papermc.paper.registry.keys.tags.EnchantmentTagKeys;
|
|||||||
import io.papermc.paper.registry.keys.tags.ItemTypeTagKeys;
|
import io.papermc.paper.registry.keys.tags.ItemTypeTagKeys;
|
||||||
import io.papermc.paper.tag.TagEntry;
|
import io.papermc.paper.tag.TagEntry;
|
||||||
import me.youhavetrouble.enchantio.enchants.EnchantioEnchant;
|
import me.youhavetrouble.enchantio.enchants.EnchantioEnchant;
|
||||||
|
import me.youhavetrouble.enchantio.enchants.ReplantingEnchant;
|
||||||
import me.youhavetrouble.enchantio.enchants.SoulboundEnchant;
|
import me.youhavetrouble.enchantio.enchants.SoulboundEnchant;
|
||||||
import me.youhavetrouble.enchantio.enchants.TelepathyEnchant;
|
import me.youhavetrouble.enchantio.enchants.TelepathyEnchant;
|
||||||
import org.bukkit.enchantments.Enchantment;
|
import org.bukkit.enchantments.Enchantment;
|
||||||
@@ -25,7 +26,8 @@ public class EnchantioBootstrap implements PluginBootstrap {
|
|||||||
|
|
||||||
Set<EnchantioEnchant> enchantioEnchants = Set.of(
|
Set<EnchantioEnchant> enchantioEnchants = Set.of(
|
||||||
new SoulboundEnchant(),
|
new SoulboundEnchant(),
|
||||||
new TelepathyEnchant()
|
new TelepathyEnchant(),
|
||||||
|
new ReplantingEnchant()
|
||||||
);
|
);
|
||||||
|
|
||||||
context.getLifecycleManager().registerEventHandler(LifecycleEvents.TAGS.preFlatten(RegistryKey.ITEM).newHandler((event) -> {
|
context.getLifecycleManager().registerEventHandler(LifecycleEvents.TAGS.preFlatten(RegistryKey.ITEM).newHandler((event) -> {
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
package me.youhavetrouble.enchantio.enchants;
|
||||||
|
|
||||||
|
import io.papermc.paper.registry.RegistryKey;
|
||||||
|
import io.papermc.paper.registry.data.EnchantmentRegistryEntry;
|
||||||
|
import io.papermc.paper.registry.keys.tags.ItemTypeTagKeys;
|
||||||
|
import io.papermc.paper.registry.tag.TagKey;
|
||||||
|
import io.papermc.paper.tag.TagEntry;
|
||||||
|
import net.kyori.adventure.key.Key;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import org.bukkit.inventory.EquipmentSlotGroup;
|
||||||
|
import org.bukkit.inventory.ItemType;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
@SuppressWarnings("UnstableApiUsage")
|
||||||
|
public class ReplantingEnchant implements EnchantioEnchant {
|
||||||
|
|
||||||
|
public static final Key KEY = Key.key("enchantio:replanting");
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Key getKey() {
|
||||||
|
return KEY;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Component getDescription() {
|
||||||
|
return Component.translatable("enchantio.enchant.replanting", "Replanting");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getAnvilCost() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMaxLevel() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getWeight() {
|
||||||
|
return 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EnchantmentRegistryEntry.EnchantmentCost getMinimumCost() {
|
||||||
|
return EnchantmentRegistryEntry.EnchantmentCost.of(1, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EnchantmentRegistryEntry.EnchantmentCost getMaximumCost() {
|
||||||
|
return EnchantmentRegistryEntry.EnchantmentCost.of(65, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterable<EquipmentSlotGroup> getActiveSlots() {
|
||||||
|
return Set.of(EquipmentSlotGroup.HAND);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canGetFromEnchantingTable() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TagKey<ItemType> getTagForSupportedItems() {
|
||||||
|
return TagKey.create(RegistryKey.ITEM, Key.key("enchantio:replanting_enchantable"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<TagEntry<ItemType>> getSupportedItems() {
|
||||||
|
return Set.of(
|
||||||
|
TagEntry.tagEntry(ItemTypeTagKeys.HOES)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
package me.youhavetrouble.enchantio.listeners;
|
||||||
|
|
||||||
|
import io.papermc.paper.registry.RegistryAccess;
|
||||||
|
import io.papermc.paper.registry.RegistryKey;
|
||||||
|
import me.youhavetrouble.enchantio.enchants.ReplantingEnchant;
|
||||||
|
import org.bukkit.*;
|
||||||
|
import org.bukkit.block.BlockState;
|
||||||
|
import org.bukkit.enchantments.Enchantment;
|
||||||
|
import org.bukkit.entity.Item;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.EventPriority;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.block.BlockDropItemEvent;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.PlayerInventory;
|
||||||
|
|
||||||
|
public class ReplantingListener implements Listener {
|
||||||
|
|
||||||
|
private final Registry<Enchantment> enchantmentRegistry = RegistryAccess.registryAccess().getRegistry(RegistryKey.ENCHANTMENT);
|
||||||
|
private final Tag<Material> cropTag = Tag.CROPS;
|
||||||
|
private final Enchantment replanting = enchantmentRegistry.get(ReplantingEnchant.KEY);
|
||||||
|
|
||||||
|
@EventHandler(ignoreCancelled = true, priority = EventPriority.NORMAL)
|
||||||
|
public void onReplantingTool(BlockDropItemEvent event) {
|
||||||
|
if (replanting == null) return;
|
||||||
|
|
||||||
|
ItemStack tool = event.getPlayer().getInventory().getItemInMainHand();
|
||||||
|
if (!tool.containsEnchantment(replanting)) return;
|
||||||
|
|
||||||
|
BlockState block = event.getBlockState();
|
||||||
|
|
||||||
|
if (cropTag == null) return;
|
||||||
|
if (!cropTag.isTagged(block.getType())) return;
|
||||||
|
|
||||||
|
Material placementMaterial = block.getBlockData().getPlacementMaterial();
|
||||||
|
|
||||||
|
Player player = event.getPlayer();
|
||||||
|
PlayerInventory inventory = player.getInventory();
|
||||||
|
|
||||||
|
boolean removed = false;
|
||||||
|
|
||||||
|
// try to remove seed from the player's inventory
|
||||||
|
for (ItemStack item : inventory.getContents()) {
|
||||||
|
if (item == null) continue;
|
||||||
|
if (item.getType() == placementMaterial) {
|
||||||
|
item.setAmount(item.getAmount() - 1);
|
||||||
|
removed = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!removed) {
|
||||||
|
// Try to remove the seed from the items dropped by the block
|
||||||
|
for (Item item : event.getItems()) {
|
||||||
|
ItemStack itemStack = item.getItemStack();
|
||||||
|
if (itemStack.getType().equals(placementMaterial)) {
|
||||||
|
itemStack.setAmount(itemStack.getAmount() - 1);
|
||||||
|
removed = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!removed) return;
|
||||||
|
|
||||||
|
// Replant the crop
|
||||||
|
event.getBlock().setType(block.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user