Skip to content

Instantly share code, notes, and snippets.

@Lanse505
Last active June 30, 2020 18:03
Show Gist options
  • Save Lanse505/c982abfc2f78073651d41703f55a5887 to your computer and use it in GitHub Desktop.
Save Lanse505/c982abfc2f78073651d41703f55a5887 to your computer and use it in GitHub Desktop.
package net.minecraftforge.event.anvil;
import net.minecraft.block.BlockState;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.Event;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@Cancelable
public class AnvilDamageEvent extends Event
{
/**
* A simple event fired whenever code is fired from the FallingBlockEntity to damage the Anvil.
* If the event is canceled, vanilla behaviour will not run and the Anvil's BlockState will not change (It won't be damaged).
* If the event is not canceled, vanilla behaviour will run and the Anvil will be damaged.
*/
public static class Pre extends AnvilDamageEvent
{
/**
* The current blockstate beneath the FallingBlockEntity for the Anvil.
*/
@Nullable
private final BlockState groundState;
public Pre(@Nonnull BlockState currentState, @Nullable BlockState groundState) {
super(currentState);
this.groundState = groundState;
}
@Nullable
public BlockState getGroundState() {
return groundState;
}
}
/**
* The current state of the Anvil pre-damage
*/
@Nonnull
private final BlockState currentState;
/**
* A simple event fired whenever code is fired to attempt to damage the Anvil.
* If the event is canceled, vanilla behaviour will not run and the Anvil's BlockState will not change (It won't be damaged).
* If the event is not canceled, vanilla behaviour will run and the Anvil will be damaged.
*/
public AnvilDamageEvent(@Nonnull BlockState currentState)
{
this.currentState = currentState;
}
@Nonnull
public BlockState getCurrentState() { return currentState; }
}
public static boolean onAnvilDamagePre(BlockState currentState, BlockState groundState) {
AnvilDamageEvent.Pre e = new AnvilDamageEvent.Pre(currentState, groundState);
return MinecraftForge.EVENT_BUS.post(e);
}
public static boolean onAnvilDamage(BlockState currentState) {
AnvilDamageEvent e = new AnvilDamageEvent(currentState);
return MinecraftForge.EVENT_BUS.post(e);
}
@Nullable
public static BlockState damage(BlockState state) {
if (net.minecraftforge.common.ForgeHooks.onAnvilDamage(state)) return state;
if (state.isIn(Blocks.ANVIL)) {
return Blocks.CHIPPED_ANVIL.getDefaultState().with(FACING, state.get(FACING));
} else {
return state.isIn(Blocks.CHIPPED_ANVIL) ? Blocks.DAMAGED_ANVIL.getDefaultState().with(FACING, state.get(FACING)) : null;
}
}
public boolean onLivingFall(float distance, float damageMultiplier) {
if (this.hurtEntities) {
int i = MathHelper.ceil(distance - 1.0F);
if (i > 0) {
List<Entity> list = Lists.newArrayList(this.world.getEntitiesWithinAABBExcludingEntity(this, this.getBoundingBox()));
boolean flag = this.fallTile.func_235714_a_(BlockTags.ANVIL);
DamageSource damagesource = flag ? DamageSource.ANVIL : DamageSource.FALLING_BLOCK;
for(Entity entity : list) {
entity.attackEntityFrom(damagesource, (float)Math.min(MathHelper.floor((float)i * this.fallHurtAmount), this.fallHurtMax));
}
if (flag && (double)this.rand.nextFloat() < (double)0.05F + (double)i * 0.05D && !net.minecraftforge.common.ForgeHooks.onAnvilDamagePre(this.fallTile, this.onGround ? this.getEntityWorld().getBlockState(this.getPositionUnderneath()) : null)) {
BlockState blockstate = AnvilBlock.damage(this.fallTile);
if (blockstate == null) {
this.dontSetBlock = true;
} else {
this.fallTile = blockstate;
}
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment