Add LivingSwapHandItemsEvent

This commit is contained in:
JustRed23 authored and LexManos committed 2023-07-15 11:15:57 -07:00
1 parent 09bc73d5cd
commit 00ad61b4ae
6 files changed
+154 -1

No files matched your search

@@ -160,6 +160,7 @@ import net.minecraftforge.event.entity.living.LivingChangeTargetEvent.ILivingTar
import net.minecraftforge.event.entity.living.LivingFallEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.event.entity.living.LivingKnockBackEvent;
import net.minecraftforge.event.entity.living.LivingSwapItemsEvent;
import net.minecraftforge.event.entity.living.LivingUseTotemEvent;
import net.minecraftforge.event.entity.living.LootingLevelEvent;
import net.minecraftforge.event.entity.living.ShieldBlockEvent;
@@ -1226,6 +1227,13 @@ public class ForgeHooks
return e;
}
public static LivingSwapItemsEvent.Hands onLivingSwapHandItems(LivingEntity livingEntity)
{
LivingSwapItemsEvent.Hands event = new LivingSwapItemsEvent.Hands(livingEntity);
MinecraftForge.EVENT_BUS.post(event);
return event;
}
public static void writeAdditionalLevelSaveData(WorldData worldData, CompoundTag levelTag)
{
CompoundTag fmlData = new CompoundTag();
@@ -1536,7 +1544,7 @@ public class ForgeHooks
* Handles living entities being under water. This fires the {@link LivingBreatheEvent} and if the entity's air supply
* is less than or equal to zero also the {@link LivingDrownEvent}. Additionally when the entity is under water it will
* dismount if {@link IForgeEntity#canBeRiddenUnderFluidType(FluidType, Entity)} returns false.
*
*
* @param entity The living entity which is currently updated
* @param consumeAirAmount The amount of air to consume when the entity is unable to breathe
* @param refillAirAmount The amount of air to refill when the entity is able to breathe
@@ -0,0 +1,79 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.event.entity.living;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.eventbus.api.Cancelable;
import org.jetbrains.annotations.ApiStatus;
public class LivingSwapItemsEvent extends LivingEvent
{
@ApiStatus.Internal
public LivingSwapItemsEvent(LivingEntity entity)
{
super(entity);
}
/**
* This event is fired when a living entity is about to swap the items in their main and offhand.
* This event is executed in {@link ServerGamePacketListenerImpl#handlePlayerAction}
*
* <p>This event is {@linkplain Cancelable cancellable}, and does not {@linkplain HasResult have a result}.
*/
@Cancelable
public static class Hands extends LivingSwapItemsEvent
{
private ItemStack toMainHand;
private ItemStack toOffHand;
@ApiStatus.Internal
public Hands(LivingEntity entity)
{
super(entity);
this.toMainHand = entity.getOffhandItem(); //the main hand will be swapped with the offhand
this.toOffHand = entity.getMainHandItem(); //the offhand will be swapped with the main hand
}
/**
* Gets the item that will be swapped to the main hand of the entity.
* @return The item that will be swapped to the main hand.
*/
public ItemStack getItemSwappedToMainHand()
{
return toMainHand;
}
/**
* Gets the item that will be swapped to the offhand of the entity.
* @return The item that will be swapped to the offhand.
*/
public ItemStack getItemSwappedToOffHand()
{
return toOffHand;
}
/**
* Sets the item that will be swapped to the main hand of the entity.
* @param item The item to swap to the main hand.
*/
public void setItemSwappedToMainHand(ItemStack item)
{
this.toMainHand = item;
}
/**
* Sets the item that will be swapped to the offhand of the entity.
* @param item The item to swap to the offhand.
*/
public void setItemSwappedToOffHand(ItemStack item)
{
this.toOffHand = item;
}
}
}