Skip to content

Instantly share code, notes, and snippets.

@Lanse505
Last active July 26, 2020 16:26
Show Gist options
  • Save Lanse505/35917e7fab527ad794d3b11f71f92af3 to your computer and use it in GitHub Desktop.
Save Lanse505/35917e7fab527ad794d3b11f71f92af3 to your computer and use it in GitHub Desktop.
/**
* This method comprises the logic of {@link CrossbowItem#hasAmmo(LivingEntity, ItemStack)} & {@link CrossbowItem#func_220023_a(LivingEntity, ItemStack, ItemStack, boolean, boolean)}
* Those two methods are being deprecated in favor of this hook method that in-lines the code.
* @param pair The provided Pair of Ammo and Ammo Consumer.
* @param shootable The shootable itemstack.
* @param shooter The shooter.
* @return returns a boolean if it succeeds in charging the crossbow.
*/
public static boolean handleCrossbowCharging(Pair<ItemStack, Consumer<ItemStack>> pair, ItemStack shootable, LivingEntity shooter)
{
int i = EnchantmentHelper.getEnchantmentLevel(Enchantments.MULTISHOT, shootable);
int j = i == 0 ? 1 : 3;
boolean isCreative = shooter instanceof PlayerEntity && ((PlayerEntity)shooter).abilities.isCreativeMode;
ItemStack itemstack = pair.getKey();
ItemStack stackCopy = itemstack.copy();
for(int k = 0; k < j; ++k)
{
if (k > 0)
itemstack = stackCopy.copy();
if (itemstack.isEmpty() && isCreative)
{
itemstack = new ItemStack(Items.ARROW);
stackCopy = itemstack.copy();
}
boolean addedAmmo = false;
if (!itemstack.isEmpty())
{
boolean notCreativeAndIsArrow = isCreative && itemstack.getItem() instanceof ArrowItem;
ItemStack itemstack2;
if (!notCreativeAndIsArrow && !isCreative && (k > 0))
{
itemstack2 = itemstack.split(1);
} else
{
itemstack2 = itemstack;
}
pair.getValue().accept(itemstack);
CrossbowItem.addChargedProjectile(shootable, itemstack2);
addedAmmo = true;
}
if (!addedAmmo)
return false;
}
return true;
}
/**@deprecated Forge: This method has been inlined to get control over the ammo consumption chain, Use {@link net.minecraftforge.common.ForgeHooks#handleCrossbowCharging(org.apache.commons.lang3.tuple.Pair, ItemStack, LivingEntity)}*/
@Deprecated
private static boolean hasAmmo(LivingEntity entityIn, ItemStack stack) {
int i = EnchantmentHelper.getEnchantmentLevel(Enchantments.MULTISHOT, stack);
int j = i == 0 ? 1 : 3;
boolean flag = entityIn instanceof PlayerEntity && ((PlayerEntity)entityIn).abilities.isCreativeMode;
ItemStack itemstack = entityIn.findAmmo(stack);
ItemStack itemstack1 = itemstack.copy();
for(int k = 0; k < j; ++k) {
if (k > 0) {
itemstack = itemstack1.copy();
}
if (itemstack.isEmpty() && flag) {
itemstack = new ItemStack(Items.ARROW);
itemstack1 = itemstack.copy();
}
if (!func_220023_a(entityIn, stack, itemstack, k > 0, flag)) {
return false;
}
}
return true;
}
/**@deprecated Forge: This method has been inlined to get control over the ammo consumption chain, Use {@link net.minecraftforge.common.ForgeHooks#handleCrossbowCharging(org.apache.commons.lang3.tuple.Pair, ItemStack, LivingEntity)}*/
@Deprecated
private static boolean func_220023_a(LivingEntity p_220023_0_, ItemStack p_220023_1_, ItemStack p_220023_2_, boolean p_220023_3_, boolean p_220023_4_) {
if (p_220023_2_.isEmpty()) {
return false;
} else {
boolean flag = p_220023_4_ && p_220023_2_.getItem() instanceof ArrowItem;
ItemStack itemstack;
if (!flag && !p_220023_4_ && !p_220023_3_) {
itemstack = p_220023_2_.split(1);
if (p_220023_2_.isEmpty() && p_220023_0_ instanceof PlayerEntity) {
((PlayerEntity)p_220023_0_).inventory.deleteStack(p_220023_2_);
}
} else {
itemstack = p_220023_2_.copy();
}
addChargedProjectile(p_220023_1_, itemstack);
return true;
}
}
@SubscribeEvent
public static void onPlayerFindAmmo(LivingFindAmmoEvent event)
{
if (!ENABLE) return;
if (event.getEntityLiving() instanceof PlayerEntity)
{
LOGGER.info("The LivingFindAmmoEvent has been called!");
LOGGER.info("Firing Player targeted code!");
PlayerEntity player = (PlayerEntity) event.getEntityLiving();
LOGGER.info("Starting search of player Ender Chest for Ammo");
EnderChestInventory inventory = player.getInventoryEnderChest();
ItemStack ammo = ItemStack.EMPTY;
int slot = 0;
for (int i = 0; i < inventory.getSizeInventory() && ammo.isEmpty(); i++)
{
ItemStack stack = inventory.getStackInSlot(i);
if (event.getAmmoPredicate().test(stack))
{
LOGGER.info("Found valid ammo in player Ender Chest");
ammo = stack;
slot = i;
break;
}
}
if (!ammo.isEmpty())
{
LOGGER.info("Set found ammo to Event");
int finalSlot = slot;
event.setAmmo(ammo, stack -> {
stack.shrink(1);
if (stack.isEmpty()) inventory.removeStackFromSlot(finalSlot);
});
}
} else
{
event.setAmmo(new ItemStack(Items.SPECTRAL_ARROW), stack -> {});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment