2020-10-29 15:50:03 -04:00
|
|
|
package wayoftime.bloodmagic.util.handler.event;
|
|
|
|
|
2020-11-21 10:54:40 -05:00
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
|
|
|
import net.minecraft.enchantment.EnchantmentHelper;
|
|
|
|
import net.minecraft.enchantment.Enchantments;
|
2020-10-29 15:50:03 -04:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2020-11-11 21:15:58 -05:00
|
|
|
import net.minecraft.entity.player.ServerPlayerEntity;
|
2020-11-21 10:54:40 -05:00
|
|
|
import net.minecraft.inventory.EquipmentSlotType;
|
2020-10-29 15:50:03 -04:00
|
|
|
import net.minecraft.item.ItemStack;
|
2020-11-11 21:15:58 -05:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2020-10-29 15:50:03 -04:00
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
2020-11-21 09:54:13 -05:00
|
|
|
import net.minecraftforge.common.Tags;
|
|
|
|
import net.minecraftforge.common.ToolType;
|
2020-10-29 15:50:03 -04:00
|
|
|
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
|
2020-11-21 10:54:40 -05:00
|
|
|
import net.minecraftforge.event.entity.player.PlayerXpEvent;
|
2020-11-21 09:54:13 -05:00
|
|
|
import net.minecraftforge.event.world.BlockEvent.BlockToolInteractEvent;
|
2020-11-21 10:54:40 -05:00
|
|
|
import net.minecraftforge.eventbus.api.EventPriority;
|
2020-10-29 15:50:03 -04:00
|
|
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
|
|
|
import net.minecraftforge.fml.common.Mod;
|
|
|
|
import wayoftime.bloodmagic.BloodMagic;
|
2020-11-21 09:54:13 -05:00
|
|
|
import wayoftime.bloodmagic.api.item.IBindable;
|
|
|
|
import wayoftime.bloodmagic.common.block.BloodMagicBlocks;
|
2020-11-21 10:54:40 -05:00
|
|
|
import wayoftime.bloodmagic.common.item.ItemExperienceBook;
|
2020-10-29 15:50:03 -04:00
|
|
|
import wayoftime.bloodmagic.core.data.Binding;
|
|
|
|
import wayoftime.bloodmagic.core.data.SoulNetwork;
|
2020-11-11 21:15:58 -05:00
|
|
|
import wayoftime.bloodmagic.demonaura.WorldDemonWillHandler;
|
2020-10-29 15:50:03 -04:00
|
|
|
import wayoftime.bloodmagic.event.ItemBindEvent;
|
2020-11-11 21:15:58 -05:00
|
|
|
import wayoftime.bloodmagic.network.DemonAuraClientPacket;
|
2020-10-29 15:50:03 -04:00
|
|
|
import wayoftime.bloodmagic.orb.BloodOrb;
|
|
|
|
import wayoftime.bloodmagic.orb.IBloodOrb;
|
|
|
|
import wayoftime.bloodmagic.util.helper.BindableHelper;
|
|
|
|
import wayoftime.bloodmagic.util.helper.NetworkHelper;
|
|
|
|
import wayoftime.bloodmagic.util.helper.PlayerHelper;
|
2020-11-11 21:15:58 -05:00
|
|
|
import wayoftime.bloodmagic.will.DemonWillHolder;
|
2020-10-29 15:50:03 -04:00
|
|
|
|
|
|
|
@Mod.EventBusSubscriber(modid = BloodMagic.MODID, bus = Mod.EventBusSubscriber.Bus.MOD)
|
|
|
|
public class GenericHandler
|
|
|
|
{
|
|
|
|
// Handles binding of IBindable's as well as setting a player's highest orb tier
|
|
|
|
@SubscribeEvent
|
|
|
|
public void onInteract(PlayerInteractEvent.RightClickItem event)
|
|
|
|
{
|
|
|
|
if (event.getWorld().isRemote)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PlayerEntity player = event.getPlayer();
|
|
|
|
|
|
|
|
if (PlayerHelper.isFakePlayer(player))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ItemStack held = event.getItemStack();
|
|
|
|
if (!held.isEmpty() && held.getItem() instanceof IBindable)
|
|
|
|
{ // Make sure it's bindable
|
|
|
|
IBindable bindable = (IBindable) held.getItem();
|
|
|
|
Binding binding = bindable.getBinding(held);
|
|
|
|
if (binding == null)
|
|
|
|
{ // If the binding is null, let's create one
|
|
|
|
if (bindable.onBind(player, held))
|
|
|
|
{
|
|
|
|
ItemBindEvent toPost = new ItemBindEvent(player, held);
|
|
|
|
if (MinecraftForge.EVENT_BUS.post(toPost)) // Allow cancellation of binding
|
|
|
|
return;
|
|
|
|
|
|
|
|
BindableHelper.applyBinding(held, player); // Bind item to the player
|
|
|
|
}
|
|
|
|
// If the binding exists, we'll check if the player's name has changed since
|
|
|
|
// they last used it and update that if so.
|
2020-11-11 21:15:58 -05:00
|
|
|
} else if (binding.getOwnerId().equals(player.getGameProfile().getId()) && !binding.getOwnerName().equals(player.getGameProfile().getName()))
|
2020-10-29 15:50:03 -04:00
|
|
|
{
|
|
|
|
binding.setOwnerName(player.getGameProfile().getName());
|
|
|
|
BindableHelper.applyBinding(held, binding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!held.isEmpty() && held.getItem() instanceof IBloodOrb)
|
|
|
|
{
|
|
|
|
IBloodOrb bloodOrb = (IBloodOrb) held.getItem();
|
|
|
|
SoulNetwork network = NetworkHelper.getSoulNetwork(player);
|
|
|
|
|
|
|
|
BloodOrb orb = bloodOrb.getOrb(held);
|
|
|
|
if (orb == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (orb.getTier() > network.getOrbTier())
|
|
|
|
network.setOrbTier(orb.getTier());
|
|
|
|
}
|
|
|
|
}
|
2020-11-11 21:15:58 -05:00
|
|
|
|
2020-11-21 09:54:13 -05:00
|
|
|
@SubscribeEvent
|
|
|
|
public void onHoe(BlockToolInteractEvent event)
|
|
|
|
{
|
|
|
|
if (event.getToolType() == ToolType.HOE && Tags.Blocks.NETHERRACK.contains(event.getState().getBlock()))
|
|
|
|
{
|
|
|
|
event.setFinalState(BloodMagicBlocks.NETHER_SOIL.get().getDefaultState());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 10:54:40 -05:00
|
|
|
// Experience Tome
|
|
|
|
@SubscribeEvent(priority = EventPriority.LOWEST)
|
|
|
|
public void onExperiencePickup(PlayerXpEvent.PickupXp event)
|
|
|
|
{
|
|
|
|
PlayerEntity player = event.getPlayer();
|
|
|
|
Entry<EquipmentSlotType, ItemStack> entry = EnchantmentHelper.getRandomItemWithEnchantment(Enchantments.MENDING, player);
|
|
|
|
|
|
|
|
if (entry != null)
|
|
|
|
{
|
|
|
|
ItemStack itemStack = entry.getValue();
|
|
|
|
if (!itemStack.isEmpty() && itemStack.isDamaged())
|
|
|
|
{
|
|
|
|
int i = Math.min(xpToDurability(event.getOrb().xpValue), itemStack.getDamage());
|
|
|
|
event.getOrb().xpValue -= durabilityToXp(i);
|
|
|
|
itemStack.setDamage(itemStack.getDamage() - i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!player.getEntityWorld().isRemote)
|
|
|
|
{
|
|
|
|
for (ItemStack stack : player.inventory.mainInventory)
|
|
|
|
{
|
|
|
|
if (stack.getItem() instanceof ItemExperienceBook)
|
|
|
|
{
|
|
|
|
ItemExperienceBook.addExperience(stack, event.getOrb().xpValue);
|
|
|
|
event.getOrb().xpValue = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int xpToDurability(int xp)
|
|
|
|
{
|
|
|
|
return xp * 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int durabilityToXp(int durability)
|
|
|
|
{
|
|
|
|
return durability / 2;
|
|
|
|
}
|
|
|
|
|
2020-11-11 21:15:58 -05:00
|
|
|
public static void sendPlayerDemonWillAura(PlayerEntity player)
|
|
|
|
{
|
|
|
|
if (player instanceof ServerPlayerEntity)
|
|
|
|
{
|
|
|
|
BlockPos pos = player.getPosition();
|
|
|
|
DemonWillHolder holder = WorldDemonWillHandler.getWillHolder(WorldDemonWillHandler.getDimensionResourceLocation(player.world), pos.getX() >> 4, pos.getZ() >> 4);
|
|
|
|
if (holder != null)
|
|
|
|
{
|
|
|
|
BloodMagic.packetHandler.sendTo(new DemonAuraClientPacket(holder), (ServerPlayerEntity) player);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
BloodMagic.packetHandler.sendTo(new DemonAuraClientPacket(new DemonWillHolder()), (ServerPlayerEntity) player);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-29 15:50:03 -04:00
|
|
|
}
|