Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.newt</groupId>
<artifactId>EnchantmentLock</artifactId>
<version>MC-1.16.1</version>
<version>MC-1.16.5</version>
<description>Prevent enchantments on items from being changed by players.</description>

<repositories>
Expand All @@ -22,7 +22,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.16.2-R0.1-SNAPSHOT</version>
<version>1.16.5-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/me/newt/enchantmentlock/EnchantmentLock.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class EnchantmentLock extends JavaPlugin {
public boolean block_anvil_repair;
public boolean block_grindstone;
public boolean block_smithing;
public boolean block_name_change;
public ItemManager itemManager;
public MessageManager messageManager;

Expand All @@ -40,6 +41,7 @@ public void onEnable() {

block_enchanting_table = configuration.getBoolean("block_enchanting_table");
block_anvil_enchanting = configuration.getBoolean("block_anvil_enchanting");
block_name_change = configuration.getBoolean("block_name_change");
block_anvil_repair = configuration.getBoolean("block_anvil_repair");
block_grindstone = configuration.getBoolean("block_grindstone");
block_smithing = configuration.getBoolean("block_smithing");
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/me/newt/enchantmentlock/MessageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class MessageManager {
public String cannot_disenchant;
public String cannot_repair;
public String cannot_smith;
public String cannot_rename;

/**
* Constructor for the MessageManager.
Expand All @@ -22,15 +23,18 @@ public MessageManager(EnchantmentLock enchantmentLock) {
this.cannot_disenchant = enchantmentLock.getConfig().getString("cannot_disenchant");
this.cannot_repair = enchantmentLock.getConfig().getString("cannot_repair");
this.cannot_smith = enchantmentLock.getConfig().getString("cannot_smith");
this.cannot_rename = enchantmentLock.getConfig().getString("cannot_rename");

assert cannot_enchant != null : "Invalid configuration. Could not load message.";
assert cannot_disenchant != null : "Invalid configuration. Could not load message.";
assert cannot_repair != null : "Invalid configuration. Could not load message.";
assert cannot_smith != null : "Invalid configuration. Could not load message.";
assert cannot_rename != null : "Invalid configuration. Could not load message.";

cannot_enchant = ChatColor.translateAlternateColorCodes('&', cannot_enchant);
cannot_disenchant = ChatColor.translateAlternateColorCodes('&', cannot_disenchant);
cannot_repair = ChatColor.translateAlternateColorCodes('&', cannot_repair);
cannot_smith = ChatColor.translateAlternateColorCodes('&', cannot_smith);
cannot_rename = ChatColor.translateAlternateColorCodes('&', cannot_rename);
}
}
53 changes: 53 additions & 0 deletions src/main/java/me/newt/enchantmentlock/feature/BlockAnvil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

/**
* Updated to support minecraft 1.16.1.
*
* @author Newt
*/
public class BlockAnvil implements Listener {
Expand All @@ -22,14 +24,54 @@ public class BlockAnvil implements Listener {

/**
* Constructor for the BlockEnchantingTable Listener.
*
* @param enchantmentLock Instance of the main class.
*/
public BlockAnvil(EnchantmentLock enchantmentLock) {
this.enchantmentLock = enchantmentLock;
}

/**
* Returns whether or not the item has been renamed
* @param item ItemStack original item put in anvil
* @param result ItemStack result from using the anvil
* @return Whether or not the item has been renamed
*/
private boolean isChangeInName(ItemStack item, ItemStack result) {
String itemName;
String resultName;

if (item.hasItemMeta()) {
ItemMeta itemMeta = item.getItemMeta();
if (itemMeta.hasDisplayName()) {
itemName = item.getItemMeta().getDisplayName();
} else {
itemName = item.getType().name();
}
} else {
itemName = item.getType().name();
}

if (result.hasItemMeta()) {
ItemMeta resultMeta = result.getItemMeta();
if (resultMeta.hasDisplayName()) {
resultName = item.getItemMeta().getDisplayName();
} else {
resultName = item.getType().name();
}
} else {
resultName = item.getType().name();
}

if (!itemName.equals(resultName)) {
return true;
}
return false;
}

/**
* Anvil event.
*
* @param event The respective event listened to.
*/
@EventHandler
Expand All @@ -53,6 +95,7 @@ public void onInventoryClick(InventoryClickEvent event) {
boolean involvesLockedItem = false;
boolean isRepair = false;
boolean isChangeInEnchantments = false;
boolean isChangeInName = false;

if (item == null) return;
if (result == null) return;
Expand All @@ -68,6 +111,8 @@ public void onInventoryClick(InventoryClickEvent event) {
isRepair = false;
isChangeInEnchantments = true;
}

isChangeInName = isChangeInName(item, result);
}

if (!involvesLockedItem) return;
Expand All @@ -78,9 +123,17 @@ public void onInventoryClick(InventoryClickEvent event) {
return;
}

if (enchantmentLock.block_name_change && isChangeInName) {
event.setCancelled(true);
player.sendMessage(enchantmentLock.messageManager.cannot_rename);
return;
}

if (enchantmentLock.block_anvil_repair && isRepair) {
event.setCancelled(true);
player.sendMessage(enchantmentLock.messageManager.cannot_repair);
}


}
}
4 changes: 4 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ cannot_enchant: "&cThis item cannot be enchanted."
cannot_disenchant: "&cThe enchantments on this item cannot be removed."
cannot_repair: "&cThis item cannot be repaired."
cannot_smith: "&cThis item cannot be upgraded to netherite."
cannot_rename: "&cThis item cannot be renamed."

# Toggle parts of the plugin. By default, all ways to (dis)enchant an item are prevented.
# Prevent players from enchanting locked items with an Enchanting Table.
Expand All @@ -30,6 +31,9 @@ block_anvil_enchanting: true
# Prevent players from repairing & combining locked items with an Anvil.
block_anvil_repair: true

# Prevent players from changing the name of locked items with an Anvil.
block_name_change: true

# Prevent players from disenchanting locked items with a Grindstone.
block_grindstone: true

Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ name: ${project.name}
version: ${project.version}
description: ${project.description}

authors: [Newt]
authors:
- Newt
- William278
main: me.newt.enchantmentlock.EnchantmentLock
website: https://github.com/JustDJplease/EnchantmentLock

Expand Down