Add VanillaGameEvent to allow for globally listening to vanilla's GameEvents (#8472)

This commit is contained in:
Sara Freimer authored and GitHub committed 2022-02-22 15:12:06 -08:00
1 parent edbc07ed22
commit 2ab4685149
5 files changed
+174

No files matched your search

@@ -69,6 +69,7 @@ import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.item.*;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.level.storage.WorldData;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.LootTable;
@@ -136,6 +137,7 @@ import net.minecraftforge.event.DifficultyChangeEvent;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.event.ItemAttributeModifierEvent;
import net.minecraftforge.event.ServerChatEvent;
import net.minecraftforge.event.VanillaGameEvent;
import net.minecraftforge.event.entity.EntityAttributeCreationEvent;
import net.minecraftforge.event.entity.EntityAttributeModificationEvent;
import net.minecraftforge.event.entity.EntityEvent;
@@ -427,6 +429,11 @@ public class ForgeHooks
return event.getEntityItem();
}
public static boolean onVanillaGameEvent(Level level, @Nullable Entity cause, GameEvent vanillaEvent, BlockPos position)
{
return !MinecraftForge.EVENT_BUS.post(new VanillaGameEvent(level, cause, vanillaEvent, position));
}
@Nullable
public static Component onServerChatEvent(ServerGamePacketListenerImpl net, String raw, Component comp)
{
@@ -0,0 +1,89 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2022.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.event;
import javax.annotation.Nullable;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.Event;
/**
* 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}.
**/
@Cancelable
public class VanillaGameEvent extends Event
{
private final Level level;
@Nullable
private final Entity cause;
private final GameEvent vanillaEvent;
private final BlockPos position;
public VanillaGameEvent(Level level, @Nullable Entity cause, GameEvent vanillaEvent, BlockPos position)
{
this.level = level;
this.cause = cause;
this.vanillaEvent = vanillaEvent;
this.position = position;
}
/**
* @return The level the Vanilla {@link GameEvent} occurred.
*/
public Level getLevel()
{
return level;
}
/**
* @return The entity that was the source or "cause" of the {@link GameEvent}.
*/
@Nullable
public Entity getCause()
{
return cause;
}
/**
* @return The Vanilla event.
*/
public GameEvent getVanillaEvent()
{
return vanillaEvent;
}
/**
* @return The position the event took place at. This may be a block or the block position of the entity targeted.
*/
public BlockPos getEventPosition()
{
return position;
}
}
@@ -0,0 +1,68 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2022.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.debug;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.VanillaGameEvent;
import net.minecraftforge.fml.common.Mod;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Tests {@link VanillaGameEvent} by listening for and printing out any uses of shears in the overworld.
*/
@Mod(VanillaGameEventTest.MODID)
public class VanillaGameEventTest
{
static final String MODID = "vanilla_game_event_test";
private static final boolean ENABLED = true;
private static final Logger LOGGER = LogManager.getLogger();
public VanillaGameEventTest()
{
if (!ENABLED) return;
MinecraftForge.EVENT_BUS.addListener(this::vanillaEvent);
}
public void vanillaEvent(VanillaGameEvent event)
{
if (event.getVanillaEvent() == GameEvent.SHEAR && event.getLevel().dimension() == Level.OVERWORLD)
{
Entity cause = event.getCause();
if (cause == null)
{
//One case this will be fired is when a dispenser shears an entity like a sheep
LOGGER.info("Target at {} in the overworld was sheared.", event.getEventPosition());
}
else
{
//This will be fired if a player shears an entity like a sheep or carves a block like a pumpkin
LOGGER.info("{} sheared a target at {} in the overworld.", cause, event.getEventPosition());
}
}
}
}
+2
View File
@@ -16,6 +16,8 @@ license="LGPL v2.1"
modId="block_place_event_test"
[[mods]]
modId="slipperiness_test"
[[mods]]
modId="vanilla_game_event_test"
[[mods]]
modId="custom_slime_block_test"
[[mods]]