#Plugin drops unwanted item

1 messages · Page 1 of 1 (latest)

scenic knoll
#

Could someone help me fix the bug in plugin I made year ago? I have been coding in C# recently so I don't remember java. https://hastebin.com/afuxilihuv.java the plugin drops steve head with player one for some reason, how could I fix it?

prime notch
#

so it drops both heads?

scenic knoll
#

steve head and player one

prime notch
#

is this multiversion or something, i dont get the point of the 2 playerHead ='s

scenic knoll
#

I don't remember why there are two of them

prime notch
#

remove the try catch and change ItemStack playerHead to ItemStack playerHead = new ItemStack(Material.PLAYER_HEAD, 1);

scenic knoll
#

Now it gives 2 playerheads

scenic knoll
#

You know what may be causing the 2nd one to drop?

prime notch
#

Check the drops adding

#

could be adding it twice

scenic knoll
#

I tried removing the bottom itemstack but it still dropped 2

prime notch
#

send the updated code

scenic knoll
#

dont mind the stuff behind //

prime notch
#
public class Getkilled implements Listener {
    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event) {
        Player getKilled  = event.getEntity();
        String killerName = getKilled.getKiller().getName();

        if (event.getEntity().getKiller() == null) return;

        ItemStack playerHead = new ItemStack(Material.PLAYER_HEAD, 1);

        SkullMeta meta = (SkullMeta) playerHead.getItemMeta();
        meta.setOwner(getKilled.getName());
        meta.setLore(Arrays.asList("Killed by " + killerName)
        playerHead.setItemMeta(meta);

        getKilled.getWorld().dropItemNaturally(getKilled.getLocation(), playerHead);
    }
}
scenic knoll
#

thank you so much

#

It works now

prime notch
#

np

scenic knoll
#

How can I make a second lore line?

prime notch
#

meta.setLore(Arrays.asList("Killed by " + killerName, "Lore Line 2"))

scenic knoll
#

oh it goes in the same bracket

prime notch
#

just a comma between differnet lines

scenic knoll
#

yea, tried to put the other text in extra brackets after comma before

prime notch
#

ah lol

scenic knoll
#

How can I change color and make text for example bold in the item lore?

prime notch
#

meta.setLore(Arrays.asList(ChatColor.GREEN.toString() + ChatColor.BOLD + "Killed by " + killerName, "Lore Line 2"))

scenic knoll
#

okay, thank you

scenic knoll
#

Hello once again, how can I make so the color1 and format1 can be controlled from config?
code: https://hastebin.com/uwazenunop.swift
config: https://hastebin.com/odihamuyed.apache

prime notch
#

Create a file called config.yml in the resources folder and add the default there then you can use that instead of copying defaults another way

scenic knoll
#

The second one is my config file that I created

prime notch
#

config:

Color1: "WHITE"
Format1: "ITALIC"
Color2: "WHITE"
Format2: "ITALIC"
Drop Chance: 1.0
  # Color1 Is color of the killer name and Format1 is format aka bold, italic and so one, for kill date is color2 and format2
  # Write exactly as in the link, https://hub.spigotmc.org/javadocs/spigot/org/bukkit/ChatColor.html
  # It must be in " "
  # Drop chance: 1.0
  # 1.0 = 100%, 0.1 = 10%, 0 = 0%, 0 = disabled

Code:

https://paste.md-5.net/usigitalak.java

project formatting:

scenic knoll
#

Does the drop chance code work with any percentage or only with 100% and 10%?

prime notch
#

only 1.0 and 0.1 because thats what the comments on config said

#

its not hard to make it work with 100.0 and between 1.0

scenic knoll
#

Probally the most stupid question yet, but how do I change so when I build the plugin jar version wont always be 1.0-snapshot?

prime notch
#

maven or gradle

scenic knoll
#

maven

prime notch
#

send your pom

#

?paste

#

fuck

scenic knoll
#

where do I find it?

#

nvm

#

Do I have to change it in pom?

prime notch
#

<version>1.0-SNAPSHOT</version> im guessing

scenic knoll
#

Do I need to write some code more for the plugin to actually create config file when in server?

prime notch
#

just needs the config in resources and it should do it automatically from the saveDefaultConfig(); on onEnable

scenic knoll
#

btw is the chance thing correct?

prime notch
#

just copy that

#

just change the package name up top and class name

scenic knoll
#

isn't the percentage for 100 or 10% only?

prime notch
#

do you want Drop Chance in config to be a 100 to 1 percent

#

so Drop Chance: 95 has a 95% chance of dropping

prime notch
#
@EventHandler
    public void onPlayerDeath(PlayerDeathEvent event) {
        int chance = getConfig().getInt("Drop Chance");


        // Config
        String color1 = getConfig().getString("Color1");
        String format1 = getConfig().getString("Format1");
        String color2 = getConfig().getString("Color2");
        String format2 = getConfig().getString("Format2");

        Player getKilled  = event.getEntity();
        String killerName = getKilled.getKiller().getName();
        // LocalDate date = LocalDate.now();
        Date date = Calendar.getInstance().getTime();
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        String today = formatter.format(date);


        if (event.getEntity().getKiller() == null) return;

        ItemStack playerHead = new ItemStack(Material.PLAYER_HEAD, 1);

        SkullMeta meta = (SkullMeta) playerHead.getItemMeta();
        meta.setOwner(getKilled.getName());
        Bukkit.getLogger().info(getConfig().getString("drop chance" + "is my name"));
        meta.setLore(Arrays.asList(ChatColor.of(color1).toString() + ChatColor.of(format1) + "Killed by " + killerName, ChatColor.of(color2).toString() + ChatColor.of(format2) + "Killed on "+ today ));
        playerHead.setItemMeta(meta);
        Double randomChance = ThreadLocalRandom.current().nextInt(0, 100);
        if (chance > randomChance) getKilled.getWorld().dropItemNaturally(getKilled.getLocation(), playerHead);


    }
scenic knoll
#

What does it want to int?

prime notch
#

oh change the first Double to int

#

ln 56

scenic knoll
#

for some reason it didn't make a config file in server nor drops a head

prime notch
#

do you have the onEnable code

scenic knoll
prime notch
#

is the default config in the resources folder

scenic knoll
#

I think so

prime notch
#

i know the problem

#

Is main or getkilled your main class

#

if there isnt anything in main, delete it

scenic knoll
#

there's this stuff but idk if it's needed

prime notch
#

remove the extends JavaPlugin and onEnable from getKilled

#

ill get the code for di

#

Main should have ```
@Override
public void onEnable() {
// Plugin startup logic
saveDefaultConfig();
getServer().getPluginManager().registerEvents(new GetKilled(this), this);

}
#

oh wait forgot something

#

And getKilled should look like

public class GetKilled implements Listener {
    private final MainClassName plugin;

    public CurrentClassName(MainClassName plugin) {
        this.plugin = plugin;
    }

    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event) {
        int chance = plugin.getConfig().getDouble("Drop Chance");


        // Config
        String color1 = plugin.getConfig().getString("Color1");
        String format1 = plugin.getConfig().getString("Format1");
        String color2 = plugin.getConfig().getString("Color2");
        String format2 = plugin.getConfig().getString("Format2");

        Player getKilled  = event.getEntity();
        String killerName = getKilled.getKiller().getName();
        // LocalDate date = LocalDate.now();
        Date date = Calendar.getInstance().getTime();
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        String today = formatter.format(date);


        if (event.getEntity().getKiller() == null) return;

        ItemStack playerHead = new ItemStack(Material.PLAYER_HEAD, 1);

        SkullMeta meta = (SkullMeta) playerHead.getItemMeta();
        meta.setOwner(getKilled.getName());
        Bukkit.getLogger().info(plugin.getConfig().getString("drop chance" + "is my name"));
        meta.setLore(Arrays.asList(ChatColor.of(color1).toString() + ChatColor.of(format1) + "Killed by " + killerName, ChatColor.of(color2).toString() + ChatColor.of(format2) + "Killed on "+ today ));
        playerHead.setItemMeta(meta);
        int randomChance = ThreadLocalRandom.current().nextDouble(0.0, 100.0);
        if (percentage > randomChance) getKilled.getWorld().dropItemNaturally(getKilled.getLocation(), playerHead);


    }
}
scenic knoll
#

theres some errors now

prime notch
#

chance the top to the correct stuff, its just placeholders

#

private final MainClassName plugin;

public CurrentClassName(MainClassName plugin) {
        this.plugin = plugin;
    }
scenic knoll
#

oh ok

prime notch
#

MainClassName = Main
CurrentClassName = Getkilled

scenic knoll
#

this.plugin too?

prime notch
#

this.plugin is fine to stay

scenic knoll
#

and what about all these cannot resolve method "getConfig()"

prime notch
#

should fix itself after getting the correct class names

scenic knoll
#

it says it needs int but there is int

prime notch
#

change nextDouble to nextInt

#

numbers to 0 and 100

#

int randomChance = ThreadLocalRandom.current().nextInt(0, 100);

scenic knoll
prime notch
#

numbers to 0 and 100

#

nextInt(0, 100)

scenic knoll
#

oops

#

but isn't it declared as public?

prime notch
#

class name is wrong

#

Getkilled not GetKilled

#

forgot to change the int and chance stuff

@EventHandler
    public void onPlayerDeath(PlayerDeathEvent event) {
        int chance = plugin.getConfig().getDouble("Drop Chance");
``` should be the top of the event sorry
scenic knoll
#

it gives me error that it should be int not double so getDouble to getInt?

prime notch
#

correct

prime notch
#

oh yeah

#

forgot about that

#

sorry

scenic knoll
#

no problem

#

also ty for all the help this far

prime notch
#

np lol

#
    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event) {
        int chance = plugin.getConfig().getInt("Drop Chance");


        // Config
        String color1 = plugin.getConfig().getString("Color1");
        String format1 = plugin.getConfig().getString("Format1");
        String color2 = plugin.getConfig().getString("Color2");
        String format2 = plugin.getConfig().getString("Format2");

        Player getKilled  = event.getEntity();
        String killerName = getKilled.getKiller().getName();
        // LocalDate date = LocalDate.now();
        Date date = Calendar.getInstance().getTime();
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        String today = formatter.format(date);


        if (event.getEntity().getKiller() == null) return;

        ItemStack playerHead = new ItemStack(Material.PLAYER_HEAD, 1);

        SkullMeta meta = (SkullMeta) playerHead.getItemMeta();
        meta.setOwner(getKilled.getName());
        Bukkit.getLogger().info(plugin.getConfig().getString("drop chance" + "is my name"));
        meta.setLore(Arrays.asList(ChatColor.of(color1).toString() + ChatColor.of(format1) + "Killed by " + killerName, ChatColor.of(color2).toString() + ChatColor.of(format2) + "Killed on "+ today ));
        playerHead.setItemMeta(meta);
        int randomChance = ThreadLocalRandom.current().nextInt(0, 100);
        if (chance >= randomChance) getKilled.getWorld().dropItemNaturally(getKilled.getLocation(), playerHead);


    }
``` should be the 100% correct code lol
scenic knoll
#

Welp I forgot yesterday but I hope it wouldn't be too hard to be able to separately disable line 1 of lore and line 2 from config, that would be the final thing that I would need to add to the plugin.

prime notch
#

Thats not hard to do, ill get it when im on my pc

scenic knoll
#

I am in school rn so you don't have to hurry

prime notch
#
    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event) {
        ArrayList lore = new ArrayList();
        int chance = plugin.getConfig().getInt("Drop Chance");
        Player getKilled  = event.getEntity();
        String killerName = getKilled.getKiller().getName();
        
        // Date
        Date date = Calendar.getInstance().getTime();
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        String today = formatter.format(date);

        // Config
        String color1 = plugin.getConfig().getString("Color1");
        String format1 = plugin.getConfig().getString("Format1");
        String color2 = plugin.getConfig().getString("Color2");
        String format2 = plugin.getConfig().getString("Format2");
        
        String line1Enabled = plugin.getConfig().getString("Line1");
        String line1 = ChatColor.of(color1).toString() + ChatColor.of(format1) + "Killed by " + killerName;
        String line2Enabled = plugin.getConfig().getString("Line2");
        String line2 = ChatColor.of(color2).toString() + ChatColor.of(format2) + "Killed on " + today;
        
        if (line1Enabled.equals("enabled")) lore.add(line1);
        if (line2Enabled.equals("enabled")) lore.add(line2);

        if (event.getEntity().getKiller() == null) return;

        ItemStack playerHead = new ItemStack(Material.PLAYER_HEAD, 1);

        SkullMeta meta = (SkullMeta) playerHead.getItemMeta();
        meta.setOwner(getKilled.getName());
        Bukkit.getLogger().info(plugin.getConfig().getString("drop chance" + "is my name"));
        meta.setLore(lore);
        playerHead.setItemMeta(meta);
        int randomChance = ThreadLocalRandom.current().nextInt(0, 100);
        if (chance >= randomChance) getKilled.getWorld().dropItemNaturally(getKilled.getLocation(), playerHead);


    }
scenic knoll
#

for some reason it doesn't seem to want to drop the head

prime notch
#

whats drop chance in config

scenic knoll
#

100

prime notch
#

add a System.out.println for chance and random chance to check its not doing the math correctly

scenic knoll
#

and where do I see when it prints them?

prime notch
#

console

scenic knoll
prime notch
#

oh add

Line1: enabled
Line2: enabled
``` to config.yml on server and resource