2022-02-22 18:12:06 -05:00
/*
2022-04-14 03:42:00 +00:00
* Copyright (c) Forge Development LLC and contributors
2022-02-28 13:41:23 -08:00
* SPDX-License-Identifier: LGPL-2.1-only
2022-02-22 18:12:06 -05:00
*/
package net.minecraftforge.event ;
import net.minecraft.world.entity.Entity ;
import net.minecraft.world.level.Level ;
import net.minecraft.world.level.gameevent.GameEvent ;
2022-06-07 15:31:26 -05:00
import net.minecraft.world.phys.Vec3 ;
2022-02-22 18:12:06 -05:00
import net.minecraftforge.common.MinecraftForge ;
2025-06-18 02:34:13 -07:00
import net.minecraftforge.eventbus.api.bus.CancellableEventBus ;
import net.minecraftforge.eventbus.api.event.MutableEvent ;
import net.minecraftforge.eventbus.api.event.characteristic.Cancellable ;
2022-06-07 15:31:26 -05:00
import org.jetbrains.annotations.Nullable ;
2022-02-22 18:12:06 -05:00
/**
* VanillaGameEvent is fired on the server whenever one of Vanilla's {@link GameEvent GameEvents} fire. <br>
* <br>
* This allows for listening to Vanilla's events in a more structured and global way that is not tied to needing a block entity listener. <br>
* <br>
* This event is fired on the {@link MinecraftForge#EVENT_BUS}. <br>
* <br>
* Cancel this event to prevent Vanilla from posting the {@link GameEvent} to all nearby {@link net.minecraft.world.level.gameevent.GameEventListener GameEventListeners}.
**/
2025-06-18 02:34:13 -07:00
public final class VanillaGameEvent extends MutableEvent implements Cancellable {
public static final CancellableEventBus < VanillaGameEvent > BUS = CancellableEventBus . create ( VanillaGameEvent . class );
2022-02-22 18:12:06 -05:00
private final Level level ;
private final GameEvent vanillaEvent ;
2022-06-07 15:31:26 -05:00
private final Vec3 position ;
private final GameEvent . Context context ;
2022-02-22 18:12:06 -05:00
2025-06-18 02:34:13 -07:00
public VanillaGameEvent ( Level level , GameEvent vanillaEvent , Vec3 position , GameEvent . Context context ) {
2022-02-22 18:12:06 -05:00
this . level = level ;
this . vanillaEvent = vanillaEvent ;
this . position = position ;
2022-06-07 15:31:26 -05:00
this . context = context ;
2022-02-22 18:12:06 -05:00
}
/**
* @return The level the Vanilla {@link GameEvent} occurred.
*/
2025-06-18 02:34:13 -07:00
public Level getLevel () {
2022-02-22 18:12:06 -05:00
return level ;
}
/**
* @return The entity that was the source or "cause" of the {@link GameEvent}.
*/
@Nullable
2025-06-18 02:34:13 -07:00
public Entity getCause () {
2022-06-07 15:31:26 -05:00
return context . sourceEntity ();
2022-02-22 18:12:06 -05:00
}
/**
* @return The Vanilla event.
*/
2025-06-18 02:34:13 -07:00
public GameEvent getVanillaEvent () {
2022-02-22 18:12:06 -05:00
return vanillaEvent ;
}
/**
2022-06-07 15:31:26 -05:00
* @return The position the event took place at.
2022-02-22 18:12:06 -05:00
*/
2025-06-18 02:34:13 -07:00
public Vec3 getEventPosition () {
2022-02-22 18:12:06 -05:00
return position ;
}
2022-06-07 15:31:26 -05:00
/**
* @return the context of the vanilla event
*/
2025-06-18 02:34:13 -07:00
public GameEvent . Context getContext () {
2022-06-07 15:31:26 -05:00
return context ;
}
2022-02-22 18:12:06 -05:00
}