EnchantItemEvent Question

Discussion in 'Plugin Development' started by NotoriousV2, Jun 14, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    NotoriousV2

    Currently, I'm working on a small, simple listener and have ran into a problem. I am new to Java and Bukkit, so please bare with me if there is a 'right-in-front-of-you' solution.

    Currently, I am listening for an enchant item event. Upon the event happening, I cancel the event and continue to do what I want to do which is: grab the enchantments to add map, get the item to enchant, and put the enchantments onto the item manually.

    The problem I'm running into is since I'm cancelling the event before it actually does anything, and manually adding the enchantment map, the actual map never changes the enchantments inside. For example, if the map contains the enchantment 'Fortune III', that is all it will contain until the event actually fires (which is never since I'm cancelling it.)

    Is there a way to manually have this map reset (if that's the word)? How would I go about this? Is there another solution to my problem? Thanks in advance.

    Still working on this without a solution. The only thing I can currently think of is manually having percentages and letting the plugin decide what to put on the items randomly... but I really rather have Minecraft do that part.

    Anyone have a solution/suggestion? What I'm trying to do is have the enchantment take away the proper amount of levels (so for a level 30 enchantment, it takes away 30 level.) This sounded simple to me, but the problem is there is a weird issue where if you wait for the event to happen, check how much levels the enchantment is, and take away the levels, it won't actually add the enchantments to the item UNLESS you've got double the levels (for a level 30 enchantment, you would need level 60, it would take 30, and you'd be left with 30 levels with enchantments on the item. If you enchanted with only 30 levels, it would take the 30 levels but not add the enchantments.)

    The way I got around that problem is making it sloppy: a delayed task. However, this is acting up and really is a half-ass way to do it. Sometimes it will take the 30 levels, other times it will not.
     
    Last edited by a moderator: Jun 15, 2015
  2. Offline

    NotoriousV2

    Just bumping this. Still need a solution or suggestion to this. Original post was on Sunday, and edited post was yesterday... so hasn't been bumped since Sunday. ^_^
     
  3. Offline

    NotoriousV2

    Bumping... anyone? :/
     
  4. Offline

    NotoriousV2

    Would doing everything manually be the only solution here? (Cancel event, pick random enchantments with certain percentages, and add to the item?)
     
  5. Offline

    xXkguyXx

    I Am new to Bukkit coding But if your Going to Cancel the event why do you even have it in the first place?
     
  6. Offline

    NotoriousV2

    I'm not entirely sure what you're asking here. If I'm going to do everything manually, I must cancel the enchantment event or it'll go off and do the default task. If I'm manually getting enchantments to add to the item, I don't want the event to go off... I just want to listen for it.
     
  7. Offline

    Ratismal

    @NotoriousV2 So if I'm understanding correctly, you want your listener to override the default EnchantItemEvent listener so yours activates instead?

    I'm not an expert at cancelling events, but have you tried cancelling after your listener does what it does rather than prior?

    I've personally never heard of this bug, where enchantments cost twice as much as they should. Do you perhaps have another plugin that is doubling it? Also, does the enchantment require double the levels, or merely take away double the levels?
     
  8. Offline

    NotoriousV2

    @Ratismal
    What I am trying to do is have the proper levels taken away from a player (like in 1.7) instead of having it take away 1-3 levels depending on which button you click. The problems I've run into are listed in the original post.

    As for the whole double the levels bug, it seems to not like that I take away 30 levels immediately. I'll give you an example so it's easier understood: say the player has 30 levels. They go to the enchantment table to enchant, and they click the level 30 enchantment button. Normally, in 1.8, it will take 3 levels and leave the player at level 27. What I'm doing is listening for the event to happen, NOT CANCELLING IT, and subtracting the enchantment cost (getExpLevelCost iirc) from the player's level. The levels are subtracted properly from the player, BUT the enchantments are not added to the item. It seems even though the client requires you to have 30 levels, the server doesn't actually check your levels instantly when clicking the enchantment button.

    So, what my plugin was doing is listening for when the player clicks the enchantment button (hence the EnchantItemEvent which is called when someone actually enchants an item) and subtracting the proper amount of levels from the player's total level. Since it seems the server checks on a delay, when it actually checks... the player's level is 0, so the enchantment doesn't get added.

    To combat this bug, I added a delayed task from when the enchantment goes off to when my plugin actually subtracts the levels. This isn't reliable, and really is a coin toss as to when it wants to take the levels or revert back to just taking 3 levels.
     
Thread Status:
Not open for further replies.

Share This Page