playing around with enchants

This commit is contained in:
2024-10-17 19:19:25 +02:00
commit 4010e88237
10 changed files with 435 additions and 0 deletions
+113
View File
@@ -0,0 +1,113 @@
# User-specific stuff
.idea/
*.iml
*.ipr
*.iws
# IntelliJ
out/
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
*~
# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*
# KDE directory preferences
.directory
# Linux trash folder which might appear on any partition or disk
.Trash-*
# .nfs files are created when an open file is removed but is still being accessed
.nfs*
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
.flattened-pom.xml
# Common working directory
run/
+73
View File
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.youhavetrouble</groupId>
<artifactId>Enchantio</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<name>Enchantio</name>
<description>Custom enchants for paper servers</description>
<properties>
<java.version>21</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<url>https://youhavetrouble.me</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>papermc-repo</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.21.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,19 @@
package me.youhavetrouble.enchantio;
import me.youhavetrouble.enchantio.listeners.SoulboundListener;
import org.bukkit.plugin.java.JavaPlugin;
public final class Enchantio extends JavaPlugin {
@Override
public void onEnable() {
getServer().getPluginManager().registerEvents(new SoulboundListener(), this);
}
@Override
public void onDisable() {
// Plugin shutdown logic
}
}
@@ -0,0 +1,43 @@
package me.youhavetrouble.enchantio;
import io.papermc.paper.plugin.bootstrap.BootstrapContext;
import io.papermc.paper.plugin.bootstrap.PluginBootstrap;
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 org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
@SuppressWarnings("all")
public class EnchantioBootstrap implements PluginBootstrap {
@Override
public void bootstrap(@NotNull BootstrapContext context) {
EnchantioEnchant soulbound = new SoulboundEnchant();
context.getLifecycleManager().registerEventHandler(RegistryEvents.ENCHANTMENT.freeze().newHandler(event -> {
for (EnchantioEnchant enchant : EnchantioEnchant.getEnchants().values()) {
event.registry().register(TypedKey.create(RegistryKey.ENCHANTMENT, enchant.getKey()), enchantment -> {
enchantment.description(enchant.getDescription());
enchantment.anvilCost(enchant.getAnvilCost());
enchantment.maxLevel(enchant.getMaxLevel());
enchantment.weight(enchant.getWeight());
enchantment.minimumCost(enchant.getMinimumCost());
enchantment.maximumCost(enchant.getMaximumCost());
enchantment.activeSlots(enchant.getActiveSlots());
enchantment.supportedItems(event.getOrCreateTag(ItemTypeTagKeys.ENCHANTABLE_ARMOR));
});
}
}));
}
@Override
public @NotNull JavaPlugin createPlugin(@NotNull PluginProviderContext context) {
return PluginBootstrap.super.createPlugin(context);
}
}
@@ -0,0 +1,16 @@
package me.youhavetrouble.enchantio;
public class EnchantioConfig {
private final Enchantio plugin;
protected EnchantioConfig(Enchantio plugin) {
this.plugin = plugin;
plugin.saveDefaultConfig();
plugin.reloadConfig();
}
}
@@ -0,0 +1,96 @@
package me.youhavetrouble.enchantio.enchants;
import io.papermc.paper.registry.data.EnchantmentRegistryEntry;
import io.papermc.paper.registry.tag.TagKey;
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.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@SuppressWarnings("all")
public abstract class EnchantioEnchant {
private static final Map<String, EnchantioEnchant> enchantioEnchants = new HashMap();
private final Key key;
private final int anvilCost;
private final int maxLevel;
private final int weight;
private final Component description;
private final EnchantmentRegistryEntry.EnchantmentCost minimumCost;
private final EnchantmentRegistryEntry.EnchantmentCost maximumCost;
private final Set<TagKey<ItemType>> supportedItems;
private final Set<EquipmentSlotGroup> activeSlots;
public EnchantioEnchant(
Key key,
Component description,
int anvilCost,
int maxLevel,
int weight,
EnchantmentRegistryEntry.EnchantmentCost minimumCost,
EnchantmentRegistryEntry.EnchantmentCost maximumCost,
Set<TagKey<ItemType>> supportedItems,
Set<EquipmentSlotGroup> activeSlots
) {
this.key = key;
this.description = description;
this.anvilCost = anvilCost;
this.maxLevel = maxLevel;
this.weight = weight;
this.minimumCost = minimumCost;
this.maximumCost = maximumCost;
this.supportedItems = supportedItems;
this.activeSlots = activeSlots;
}
public Key getKey() {
return key;
}
public Component getDescription() {
return description;
}
public int getAnvilCost() {
return anvilCost;
}
public int getMaxLevel() {
return maxLevel;
}
public int getWeight() {
return weight;
}
public EnchantmentRegistryEntry.EnchantmentCost getMinimumCost() {
return minimumCost;
}
public EnchantmentRegistryEntry.EnchantmentCost getMaximumCost() {
return maximumCost;
}
public Set<TagKey<ItemType>> getSupportedItems() {
return supportedItems;
}
public Iterable<EquipmentSlotGroup> getActiveSlots() {
return activeSlots;
}
protected static void registerEnchant(EnchantioEnchant enchant) {
enchantioEnchants.put(enchant.getKey().asString(), enchant);
}
public static Map<String, EnchantioEnchant> getEnchants() {
return Collections.unmodifiableMap(enchantioEnchants);
}
}
@@ -0,0 +1,36 @@
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 SoulboundEnchant extends EnchantioEnchant {
public SoulboundEnchant() {
super(
Key.key("enchantio:soulbound"),
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
),
Set.of(EquipmentSlotGroup.ANY)
);
registerEnchant(this);
}
}
@@ -0,0 +1,31 @@
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 org.bukkit.Registry;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
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"));
Enchantio.getPlugin(Enchantio.class).getLogger().info("Soulbound enchantment: " + soulbound);
event.getPlayer().getInventory().forEach(itemStack -> {
if (itemStack != null && itemStack.getEnchantments().containsKey(soulbound)) {
event.getItemsToKeep().add(itemStack);
event.getDrops().remove(itemStack);
}
});
}
}
View File
+8
View File
@@ -0,0 +1,8 @@
name: Enchantio
version: '${project.version}'
main: me.youhavetrouble.enchantio.Enchantio
bootstrapper: me.youhavetrouble.enchantio.EnchantioBootstrap
api-version: '1.21'
authors: [YouHaveTrouble]
description: Custom enchants for paper servers
website: https://youhavetrouble.me