Files
MinecraftForge/src/main/java/net/minecraftforge/event/VanillaGameEvent.java
T

77 lines
2.4 KiB
Java
Raw Normal View History

/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
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;
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;
/**
* 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);
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;
2025-06-18 02:34:13 -07:00
public VanillaGameEvent(Level level, GameEvent vanillaEvent, Vec3 position, GameEvent.Context context) {
this.level = level;
this.vanillaEvent = vanillaEvent;
this.position = position;
2022-06-07 15:31:26 -05:00
this.context = context;
}
/**
* @return The level the Vanilla {@link GameEvent} occurred.
*/
2025-06-18 02:34:13 -07:00
public Level getLevel() {
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();
}
/**
* @return The Vanilla event.
*/
2025-06-18 02:34:13 -07:00
public GameEvent getVanillaEvent() {
return vanillaEvent;
}
/**
2022-06-07 15:31:26 -05:00
* @return The position the event took place at.
*/
2025-06-18 02:34:13 -07:00
public Vec3 getEventPosition() {
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;
}
}