package com.lothrazar.cyclic.enchant;

import com.lothrazar.cyclic.base.EnchantBase;
import net.minecraft.enchantment.EnchantmentType;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.item.ItemStack;
import net.minecraft.item.SwordItem;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;

public class EnchantXp extends EnchantBase {

  public EnchantXp(Rarity rarityIn, EnchantmentType typeIn, EquipmentSlotType... mainhand) {
    super(rarityIn, typeIn, mainhand);
    MinecraftForge.EVENT_BUS.register(this);
  }

  @Override
  public int getMinEnchantability(int enchantmentLevel) {
    return 11 + enchantmentLevel * 20;
  }

  @SubscribeEvent
  public void handleBlockBreakEvent(BlockEvent.BreakEvent event) {
    int level = getCurrentLevelTool(event.getPlayer());
    if (level <= 0) {
      return;
    }
    giveRandomExp(level, event.getPlayer());
  }

  @Override
  public void onEntityDamaged(LivingEntity user, Entity target, int level) {
    super.onEntityDamaged(user, target, level);
    if (user instanceof PlayerEntity && target != null && !target.isAlive()) {
      giveRandomExp(level, (PlayerEntity) user);
    }
  }

  private void giveRandomExp(int level, PlayerEntity p) {
    p.giveExperiencePoints(p.world.rand.nextInt(getMaxLevel()) * (level + 1));
  }

  @Override
  public int getMaxEnchantability(int enchantmentLevel) {
    return super.getMinEnchantability(enchantmentLevel) + 24;
  }

  @Override
  public boolean canApply(ItemStack stack) {
    if (stack.getItem() instanceof SwordItem) {
      return true;//override even though digger type 
    }
    return super.canApply(stack);
  }

  /**
   * Returns the maximum level that the enchantment can have.
   */
  @Override
  public int getMaxLevel() {
    return 3;
  }
}

@@ -132,135 +132,8 @@
 pe;%0A
-import net.minecraft.entity.Entity;%0Aimport net.minecraft.entity.LivingEntity;%0Aimport net.minecraft.entity.player.PlayerEntity;%0A
 impo
@@ -252,16 +252,50 @@
 rdItem;%0A
+import net.minecraft.world.World;%0A
 import n
@@ -335,16 +335,89 @@
 tForge;%0A
+import net.minecraftforge.event.entity.living.LivingExperienceDropEvent;%0A
 import n
@@ -990,24 +990,46 @@
 .getPlayer()
+.getHeldItemMainhand()
 );%0A    if (l
@@ -1069,65 +1069,140 @@
     
-giveRandomExp(level, event.getPlayer());%0A  %7D%0A%0A  @Override
+event.setExpToDrop(event.getExpToDrop() + event.getPlayer().world.rand.nextInt(getMaxLevel()) * (level + 1));%0A  %7D%0A%0A  @SubscribeEvent
 %0A  p
@@ -1216,208 +1216,259 @@
 oid 
-on
+handle
 EntityD
-amaged(LivingEntity user, Entity target, int level) %7B%0A    super.onEntityDamaged(user, target, level);%0A    if (user instanceof PlayerEntity && target != null && !target.isAlive()) %7B%0A      give
+ropEvent(LivingExperienceDropEvent event) %7B%0A    int level = getCurrentLevelTool(event.getAttackingPlayer().getHeldItemMainhand());%0A    if (level %3C= 0) %7B%0A      return;%0A    %7D%0A    event.setDroppedExperience(event.getDroppedExperience() + get
 Rand
@@ -1476,16 +1476,22 @@
 mExp
+Amount
 (level, 
 (Pla
@@ -1490,35 +1490,43 @@
 el, 
-(PlayerEntity) user);%0A    %7D
+event.getAttackingPlayer().world));
 %0A  %7D
@@ -1541,17 +1541,15 @@
 ate 
-void give
+int get
 Rand
@@ -1553,16 +1553,22 @@
 andomExp
+Amount
 (int lev
@@ -1575,55 +1575,34 @@
 el, 
-PlayerEntity p) %7B%0A    p.giveExperiencePoints(p.
+World world) %7B%0A    return 
 worl
@@ -1636,33 +1636,32 @@
 )) * (level + 1)
-)
 ;%0A  %7D%0A%0A  @Overri

Fix #1548 where XP Enchantment triggers when enchanted item is held in the offhand and blocks are broken with the mainhand. Change XP Enchantment to drop orbs when blocks are broken and entities are killed, instead of adding directly to the player's experience.