From 7be7f5f6491ee412e8b5cb6e605dab28546d8733 Mon Sep 17 00:00:00 2001 From: Kitlith Date: Tue, 21 Jul 2020 16:15:45 -0700 Subject: [PATCH] Dispatch to EntityEvents from ForgeHooks, etc. (#134) * Move onLivingDrops to EntityEvents * Dispatch from patchwork-god-classes to EntityEvents --- .../impl/event/entity/EntityEvents.java | 10 ++- .../mixin/event/entity/MixinLivingEntity.java | 4 +- patchwork-god-classes/build.gradle | 1 + .../net/minecraftforge/common/ForgeHooks.java | 74 +++++++++++++++++++ .../event/ForgeEventFactory.java | 25 +++++++ .../fml/hooks/BasicEventHooks.java | 5 ++ 6 files changed, 114 insertions(+), 5 deletions(-) diff --git a/patchwork-events-entity/src/main/java/net/patchworkmc/impl/event/entity/EntityEvents.java b/patchwork-events-entity/src/main/java/net/patchworkmc/impl/event/entity/EntityEvents.java index 9355e90f..73cf8679 100644 --- a/patchwork-events-entity/src/main/java/net/patchworkmc/impl/event/entity/EntityEvents.java +++ b/patchwork-events-entity/src/main/java/net/patchworkmc/impl/event/entity/EntityEvents.java @@ -20,6 +20,7 @@ package net.patchworkmc.impl.event.entity; import java.util.List; +import java.util.Collection; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.extensions.IForgeItem; @@ -28,6 +29,7 @@ import net.minecraftforge.event.entity.living.LivingAttackEvent; import net.minecraftforge.event.entity.living.LivingDamageEvent; import net.minecraftforge.event.entity.living.LivingDeathEvent; +import net.minecraftforge.event.entity.living.LivingDropsEvent; import net.minecraftforge.event.entity.living.LivingEvent; import net.minecraftforge.event.entity.living.LivingFallEvent; import net.minecraftforge.event.entity.living.LivingHurtEvent; @@ -47,13 +49,13 @@ import net.minecraft.entity.Entity; import net.minecraft.entity.EntityDimensions; import net.minecraft.entity.EntityPose; +import net.minecraft.entity.ItemEntity; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.SpawnType; import net.minecraft.entity.damage.DamageSource; import net.minecraft.entity.mob.MobEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; -import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; @@ -97,7 +99,7 @@ public static void onEnteringChunk(Entity entity, int newChunkX, int newChunkZ, } // PlayerEvents - public static void onPlayerLoggedIn(ServerPlayerEntity playerEntity) { + public static void onPlayerLoggedIn(PlayerEntity playerEntity) { MinecraftForge.EVENT_BUS.post(new PlayerEvent.PlayerLoggedInEvent(playerEntity)); } @@ -128,6 +130,10 @@ public static float onLivingDamage(LivingEntity entity, DamageSource src, float return MinecraftForge.EVENT_BUS.post(event) ? 0 : event.getAmount(); } + public static boolean onLivingDrops(LivingEntity entity, DamageSource source, Collection drops, int lootingLevel, boolean recentlyHit) { + return MinecraftForge.EVENT_BUS.post(new LivingDropsEvent(entity, source, drops, lootingLevel, recentlyHit)); + } + public static float getEyeHeight(Entity entity, EntityPose pose, EntityDimensions size, float defaultHeight) { EntityEvent.EyeHeight event = new EntityEvent.EyeHeight(entity, pose, size, defaultHeight); MinecraftForge.EVENT_BUS.post(event); diff --git a/patchwork-events-entity/src/main/java/net/patchworkmc/mixin/event/entity/MixinLivingEntity.java b/patchwork-events-entity/src/main/java/net/patchworkmc/mixin/event/entity/MixinLivingEntity.java index 21fe772a..d7e2f0d1 100644 --- a/patchwork-events-entity/src/main/java/net/patchworkmc/mixin/event/entity/MixinLivingEntity.java +++ b/patchwork-events-entity/src/main/java/net/patchworkmc/mixin/event/entity/MixinLivingEntity.java @@ -22,9 +22,7 @@ import java.util.ArrayList; import java.util.Collection; -import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.extensions.IForgeEntity; -import net.minecraftforge.event.entity.living.LivingDropsEvent; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -154,7 +152,7 @@ private void hookDropForDropsEvent(DamageSource src, CallbackInfo info) { IForgeEntity forgeEntity = (IForgeEntity) this; Collection drops = forgeEntity.captureDrops(null); - if (!MinecraftForge.EVENT_BUS.post(new LivingDropsEvent(entity, src, drops, dropLootingLevel.get(), playerHitTimer > 0))) { + if (!EntityEvents.onLivingDrops(entity, src, drops, dropLootingLevel.get(), playerHitTimer > 0)) { for (ItemEntity item : drops) { forgeEntity.getEntity().world.spawnEntity(item); } diff --git a/patchwork-god-classes/build.gradle b/patchwork-god-classes/build.gradle index 914132e0..24901999 100644 --- a/patchwork-god-classes/build.gradle +++ b/patchwork-god-classes/build.gradle @@ -4,6 +4,7 @@ version = getSubprojectVersion(project, "0.1.0") dependencies { compile project(path: ':patchwork-fml', configuration: 'dev') compile project(path: ':patchwork-capabilities', configuration: 'dev') + compile project(path: ':patchwork-events-entity', configuration: 'dev') compile project(path: ':patchwork-events-lifecycle', configuration: 'dev') compile project(path: ':patchwork-events-rendering', configuration: 'dev') } diff --git a/patchwork-god-classes/src/main/java/net/minecraftforge/common/ForgeHooks.java b/patchwork-god-classes/src/main/java/net/minecraftforge/common/ForgeHooks.java index ed4d8220..dea1ec51 100644 --- a/patchwork-god-classes/src/main/java/net/minecraftforge/common/ForgeHooks.java +++ b/patchwork-god-classes/src/main/java/net/minecraftforge/common/ForgeHooks.java @@ -19,9 +19,83 @@ package net.minecraftforge.common; +import java.util.Collection; + +import javax.annotation.Nullable; + +import net.minecraftforge.event.ForgeEventFactory; +import net.minecraftforge.eventbus.api.Event; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.ItemEntity; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.SpawnType; +import net.minecraft.entity.damage.DamageSource; +import net.minecraft.entity.mob.MobEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; +import net.minecraft.world.IWorld; +import net.minecraft.world.MobSpawnerLogic; + +import net.patchworkmc.impl.event.entity.EntityEvents; + /* * Note: this class is intended for mod use only, to dispatch to the implementations kept in their own modules. * Do not keep implementation details here, methods should be thin wrappers around methods in other modules. */ public class ForgeHooks { + public static int canEntitySpawn(MobEntity entity, IWorld world, double x, double y, double z, MobSpawnerLogic spawner, SpawnType spawnReason) { + Event.Result res = ForgeEventFactory.canEntitySpawn(entity, world, x, y, z, null, spawnReason); + return res == Event.Result.DEFAULT ? 0 : res == Event.Result.DENY ? -1 : 1; + } + + // TODO: onInteractEntityAt + + public static ActionResult onInteractEntity(PlayerEntity player, Entity entity, Hand hand) { + return EntityEvents.onInteractEntity(player, entity, hand); + } + + public static boolean onLivingDeath(LivingEntity entity, DamageSource src) { + return EntityEvents.onLivingDeath(entity, src); + } + + public static boolean onLivingUpdate(LivingEntity entity) { + return EntityEvents.onLivingUpdateEvent(entity); + } + + // TODO: forge calls the equivilant to this in LivingEntity, but patchwork only calls the equivilant to onPlayerAttack + public static boolean onLivingAttack(LivingEntity entity, DamageSource src, float amount) { + return entity instanceof PlayerEntity || onPlayerAttack(entity, src, amount); + } + + public static boolean onPlayerAttack(LivingEntity entity, DamageSource src, float amount) { + return !EntityEvents.onLivingAttack(entity, src, amount); + } + + // optifine wants this? O.o + public static void onLivingSetAttackTarget(LivingEntity entity, LivingEntity target) { + EntityEvents.onLivingSetAttackTarget(entity, target); + } + + public static float onLivingHurt(LivingEntity entity, DamageSource src, float amount) { + return EntityEvents.onLivingHurt(entity, src, amount); + } + + @Nullable + public static float[] onLivingFall(LivingEntity entity, float distance, float damageMultiplier) { + return EntityEvents.onLivingFall(entity, distance, damageMultiplier); + } + + public static float onLivingDamage(LivingEntity entity, DamageSource src, float amount) { + return EntityEvents.onLivingDamage(entity, src, amount); + } + + public static boolean onLivingDrops(LivingEntity entity, DamageSource source, Collection drops, int lootingLevel, boolean recentlyHit) { + return EntityEvents.onLivingDrops(entity, source, drops, lootingLevel, recentlyHit); + } + + public static boolean onPlayerAttackTarget(PlayerEntity player, Entity target) { + return EntityEvents.attackEntity(player, target); + } } diff --git a/patchwork-god-classes/src/main/java/net/minecraftforge/event/ForgeEventFactory.java b/patchwork-god-classes/src/main/java/net/minecraftforge/event/ForgeEventFactory.java index 9d526437..f6bc5485 100644 --- a/patchwork-god-classes/src/main/java/net/minecraftforge/event/ForgeEventFactory.java +++ b/patchwork-god-classes/src/main/java/net/minecraftforge/event/ForgeEventFactory.java @@ -23,8 +23,17 @@ import net.minecraftforge.common.capabilities.CapabilityDispatcher; import net.minecraftforge.common.capabilities.ICapabilityProvider; +import net.minecraftforge.eventbus.api.Event; + +import net.minecraft.entity.SpawnType; +import net.minecraft.entity.mob.MobEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.world.IWorld; +import net.minecraft.world.MobSpawnerLogic; +import net.minecraft.world.World; import net.patchworkmc.impl.capability.CapabilityEvents; +import net.patchworkmc.impl.event.entity.EntityEvents; /* * Note: this class is intended for mod use only, to dispatch to the implementations kept in their own modules. @@ -40,4 +49,20 @@ public static CapabilityDispatcher gatherCapabilities(Class typ public static CapabilityDispatcher gatherCapabilities(Class type, T provider, @Nullable ICapabilityProvider parent) { return CapabilityEvents.gatherCapabilities(type, provider, parent); } + + public static Event.Result canEntitySpawn(MobEntity entity, IWorld world, double x, double y, double z, MobSpawnerLogic spawner, SpawnType spawnReason) { + return EntityEvents.canEntitySpawn(entity, world, x, y, z, spawner, spawnReason); + } + + public static boolean canEntitySpawnSpawner(MobEntity entity, World world, float x, float y, float z, MobSpawnerLogic spawner) { + return EntityEvents.canEntitySpawnFromSpawner(entity, world, x, y, z, spawner); + } + + public static void onPlayerFall(PlayerEntity player, float distance, float multiplier) { + EntityEvents.onFlyablePlayerFall(player, distance, multiplier); + } + + public static boolean doSpecialSpawn(MobEntity entity, World world, float x, float y, float z, MobSpawnerLogic spawner, SpawnType spawnReason) { + return EntityEvents.doSpecialSpawn(entity, world, x, y, z, spawner, spawnReason); + } } diff --git a/patchwork-god-classes/src/main/java/net/minecraftforge/fml/hooks/BasicEventHooks.java b/patchwork-god-classes/src/main/java/net/minecraftforge/fml/hooks/BasicEventHooks.java index aaebffd3..ca968de2 100644 --- a/patchwork-god-classes/src/main/java/net/minecraftforge/fml/hooks/BasicEventHooks.java +++ b/patchwork-god-classes/src/main/java/net/minecraftforge/fml/hooks/BasicEventHooks.java @@ -24,6 +24,7 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.world.World; +import net.patchworkmc.impl.event.entity.EntityEvents; import net.patchworkmc.impl.event.lifecycle.LifecycleEvents; /* @@ -31,6 +32,10 @@ * Do not keep implementation details here, methods should be thin wrappers around methods in other modules. */ public class BasicEventHooks { + public static void firePlayerLoggedIn(PlayerEntity player) { + EntityEvents.onPlayerLoggedIn(player); + } + public static void onPlayerPreTick(PlayerEntity player) { LifecycleEvents.firePlayerTickEvent(TickEvent.Phase.START, player); }