Sueta
DI

DisableAnvilRenaming

1.0

Simple plugin to disable renaming of items

·Plugin

This was a quick plugin for a guy I met on Reddit.

It is a very simple plugin which can remove the ability to rename item with a colored name or disable renaming if the lore contains a certain line. If renaming of colored name item is enabled, the format of the name will be kept and transfered to the result item if the player do not rename it

If you want other features implemented just let me know and I'll see what I can do!

Here is the config:

Config
#Colored name will automatically transfer if the item name is untouched

#You can disable item renaming with the following options

#Do you want players to be able to rename item with colored name
disableRenamingColoredName: false

#Disable item renaming if the lore contains one of the following line
#Color codes are taken into account
disableRenamingIfLoreContains:
  - "this line"

Since it is only a one class plugin, I'll just leave the code here:

code
package fr.station47.disableAnvilRenaming;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class Main extends JavaPlugin implements Listener {

    private YamlConfiguration config;
    private boolean disableColoredNameRenaming = false;
    private List<String> disablingLore;

    @Override
    public void onEnable(){
        File configFile = new File(getDataFolder(), "config.yml");
        if (!configFile.exists()) {
            saveResource("config.yml", false);
        }

        config = new YamlConfiguration();
        try {
            config.load(configFile);
        } catch (InvalidConfigurationException ex){
            Bukkit.getServer().getPluginManager().disablePlugin(this);
            Bukkit.getLogger().severe("The config is incorrectly formatted!");
            ex.printStackTrace();

        } catch (IOException ex){
            Bukkit.getServer().getPluginManager().disablePlugin(this);
            Bukkit.getLogger().severe("Erreur IOException!");
            ex.printStackTrace();
        }
        disableColoredNameRenaming = config.getBoolean("disableRenamingColoredName");
        disablingLore = config.getStringList("disableRenamingIfLoreContains");

        this.getServer().getPluginManager().registerEvents(this,this);
    }

    @EventHandler
    public void preventRenaming(PrepareAnvilEvent event){
        ItemStack original = event.getInventory().getItem(0);
        if (original == null || original.getItemMeta().getDisplayName() == null || event.getResult()==null) return;
        if (disableColoredNameRenaming){
            String displayName = original.getItemMeta().getDisplayName();
            if (!displayName.equals(ChatColor.stripColor(displayName))){
                ItemMeta resultMeta = event.getResult().getItemMeta();
                resultMeta.setDisplayName(displayName);
                event.getResult().setItemMeta(resultMeta);
                return;
            }
        }
        if (disablingLore.size() > 0){
            if (original.getItemMeta().getLore()!=null){
                for (String line:disablingLore){
                    for (String lore:original.getItemMeta().getLore()){
                        if (ChatColor.translateAlternateColorCodes('&',line).equals(lore)){
                            ItemMeta resultMeta = event.getResult().getItemMeta();
                            resultMeta.setDisplayName(original.getItemMeta().getDisplayName());
                            event.getResult().setItemMeta(resultMeta);
                            return;
                        }
                    }
                }
            }
        }
        String displayName = original.getItemMeta().getDisplayName();
        if (displayName.replaceAll("§","").equals(event.getInventory().getRenameText())){
            ItemMeta resultMeta = event.getResult().getItemMeta();
            resultMeta.setDisplayName(displayName);
            event.getResult().setItemMeta(resultMeta);
        }

    }
}

Similar projects

Plugin →