Skip to content

Commit

Permalink
Remove uses of Event.Result and update events to have their own Res…
Browse files Browse the repository at this point in the history
…ult classes (#588)
  • Loading branch information
Shadows-of-Fire authored May 25, 2024
1 parent d61ba73 commit 02a1f61
Show file tree
Hide file tree
Showing 89 changed files with 1,570 additions and 1,110 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ neoforge_snapshot_next_stable=21.0
mergetool_version=2.0.0
accesstransformers_version=10.0.1
coremods_version=6.0.4
eventbus_version=7.2.0
eventbus_version=8.0.1
modlauncher_version=11.0.2
securejarhandler_version=3.0.4
bootstraplauncher_version=2.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
this.startPrediction(this.minecraft.level, p_233728_ -> {
boolean flag = !blockstate1.isAir();
if (flag && this.destroyProgress == 0.0F) {
+ if (event.getUseBlock() != net.neoforged.bus.api.Event.Result.DENY)
+ if (event.getUseBlock() != net.neoforged.neoforge.common.util.TriState.FALSE)
blockstate1.attack(this.minecraft.level, p_105270_, this.minecraft.player);
}

+ ServerboundPlayerActionPacket packet = new ServerboundPlayerActionPacket(ServerboundPlayerActionPacket.Action.START_DESTROY_BLOCK, p_105270_, p_105271_, p_233728_);
+ if (event.getUseItem() == net.neoforged.bus.api.Event.Result.DENY) return packet;
+ if (event.getUseItem().isFalse()) return packet;
if (flag && blockstate1.getDestroyProgress(this.minecraft.player, this.minecraft.player.level(), p_105270_) >= 1.0F) {
this.destroyBlock(p_105270_);
} else {
Expand Down Expand Up @@ -69,7 +69,7 @@

this.destroyTicks++;
this.minecraft.getTutorial().onDestroyBlock(this.minecraft.level, p_105284_, blockstate, Mth.clamp(this.destroyProgress, 0.0F, 1.0F));
+ if (net.neoforged.neoforge.common.CommonHooks.onClientMineHold(this.minecraft.player, p_105284_, p_105285_).getUseItem() == net.neoforged.bus.api.Event.Result.DENY) return true;
+ if (net.neoforged.neoforge.common.CommonHooks.onClientMineHold(this.minecraft.player, p_105284_, p_105285_).getUseItem().isFalse()) return true;
if (this.destroyProgress >= 1.0F) {
this.isDestroying = false;
this.startPrediction(this.minecraft.level, p_233739_ -> {
Expand All @@ -95,7 +95,7 @@
} else {
- boolean flag = !p_233747_.getMainHandItem().isEmpty() || !p_233747_.getOffhandItem().isEmpty();
+ UseOnContext useoncontext = new UseOnContext(p_233747_, p_233748_, p_233749_);
+ if (event.getUseItem() != net.neoforged.bus.api.Event.Result.DENY) {
+ if (event.getUseItem() != net.neoforged.neoforge.common.util.TriState.FALSE) {
+ InteractionResult result = itemstack.onItemUseFirst(useoncontext);
+ if (result != InteractionResult.PASS) {
+ return result;
Expand All @@ -104,7 +104,7 @@
+ boolean flag = !p_233747_.getMainHandItem().doesSneakBypassUse(p_233747_.level(), blockpos, p_233747_) || !p_233747_.getOffhandItem().doesSneakBypassUse(p_233747_.level(), blockpos, p_233747_);
boolean flag1 = p_233747_.isSecondaryUseActive() && flag;
- if (!flag1) {
+ if (event.getUseBlock() == net.neoforged.bus.api.Event.Result.ALLOW || (event.getUseBlock() != net.neoforged.bus.api.Event.Result.DENY && !flag1)) {
+ if (event.getUseBlock().isTrue() || (event.getUseBlock().isDefault() && !flag1)) {
BlockState blockstate = this.minecraft.level.getBlockState(blockpos);
if (!this.connection.isFeatureEnabled(blockstate.getBlock().requiredFeatures())) {
return InteractionResult.FAIL;
Expand All @@ -114,10 +114,10 @@

- if (!itemstack.isEmpty() && !p_233747_.getCooldowns().isOnCooldown(itemstack.getItem())) {
- UseOnContext useoncontext = new UseOnContext(p_233747_, p_233748_, p_233749_);
+ if (event.getUseItem() == net.neoforged.bus.api.Event.Result.DENY) {
+ if (event.getUseItem().isFalse()) {
+ return InteractionResult.PASS;
+ }
+ if (event.getUseItem() == net.neoforged.bus.api.Event.Result.ALLOW || (!itemstack.isEmpty() && !p_233747_.getCooldowns().isOnCooldown(itemstack.getItem()))) {
+ if (event.getUseItem().isTrue() || (!itemstack.isEmpty() && !p_233747_.getCooldowns().isOnCooldown(itemstack.getItem()))) {
InteractionResult interactionresult1;
if (this.localPlayerMode.isCreative()) {
int i = itemstack.getCount();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
--- a/net/minecraft/client/renderer/entity/EntityRenderer.java
+++ b/net/minecraft/client/renderer/entity/EntityRenderer.java
@@ -71,8 +_,10 @@
@@ -71,8 +_,11 @@
}

public void render(T p_114485_, float p_114486_, float p_114487_, PoseStack p_114488_, MultiBufferSource p_114489_, int p_114490_) {
- if (this.shouldShowName(p_114485_)) {
- this.renderNameTag(p_114485_, p_114485_.getDisplayName(), p_114488_, p_114489_, p_114490_, p_114487_);
+ var renderNameTagEvent = new net.neoforged.neoforge.client.event.RenderNameTagEvent(p_114485_, p_114485_.getDisplayName(), this, p_114488_, p_114489_, p_114490_, p_114487_);
+ net.neoforged.neoforge.common.NeoForge.EVENT_BUS.post(renderNameTagEvent);
+ if (renderNameTagEvent.getResult() != net.neoforged.bus.api.Event.Result.DENY && (renderNameTagEvent.getResult() == net.neoforged.bus.api.Event.Result.ALLOW || this.shouldShowName(p_114485_))) {
+ this.renderNameTag(p_114485_, renderNameTagEvent.getContent(), p_114488_, p_114489_, p_114490_, p_114487_);
+ // Neo: Post the RenderNameTagEvent and conditionally wrap #renderNameTag based on the result.
+ var event = new net.neoforged.neoforge.client.event.RenderNameTagEvent(p_114485_, p_114485_.getDisplayName(), this, p_114488_, p_114489_, p_114490_, p_114487_);
+ net.neoforged.neoforge.common.NeoForge.EVENT_BUS.post(event);
+ if (event.canRender().isTrue() || event.canRender().isDefault() && this.shouldShowName(p_114485_)) {
+ this.renderNameTag(p_114485_, event.getContent(), p_114488_, p_114489_, p_114490_, p_114487_);
}
}

Expand Down
46 changes: 33 additions & 13 deletions patches/net/minecraft/server/level/ServerPlayer.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,45 @@
}

return this;
@@ -912,6 +_,9 @@
@@ -912,6 +_,15 @@

@Override
public Either<Player.BedSleepingProblem, Unit> startSleepInBed(BlockPos p_9115_) {
+ java.util.Optional<BlockPos> optAt = java.util.Optional.of(p_9115_);
+ Player.BedSleepingProblem ret = net.neoforged.neoforge.event.EventHooks.onPlayerSleepInBed(this, optAt);
+ if (ret != null) return Either.left(ret);
+ // Neo: Encapsulate the vanilla check logic to supply to the CanPlayerSleepEvent
+ var vanillaResult = ((java.util.function.Supplier<Either<BedSleepingProblem, Unit>>) () -> {
+ // Guard against modded beds that may not have the FACING property.
+ // We just return success (Unit) here. Modders will need to implement conditions in the CanPlayerSleepEvent
+ if (!this.level().getBlockState(p_9115_).hasProperty(HorizontalDirectionalBlock.FACING)) {
+ return Either.right(Unit.INSTANCE);
+ }
+
+ // Start vanilla code
Direction direction = this.level().getBlockState(p_9115_).getValue(HorizontalDirectionalBlock.FACING);
if (this.isSleeping() || !this.isAlive()) {
return Either.left(Player.BedSleepingProblem.OTHER_PROBLEM);
@@ -923,7 +_,7 @@
return Either.left(Player.BedSleepingProblem.OBSTRUCTED);
} else {
this.setRespawnPosition(this.level().dimension(), p_9115_, this.getYRot(), false, true);
- if (this.level().isDay()) {
+ if (!net.neoforged.neoforge.event.EventHooks.fireSleepingTimeCheck(this, optAt)) {
return Either.left(Player.BedSleepingProblem.NOT_POSSIBLE_NOW);
} else {
if (!this.isCreative()) {
@@ -940,7 +_,21 @@
return Either.left(Player.BedSleepingProblem.NOT_SAFE);
}
}
-
+ // End vanilla code
+ }
+ }
+ return Either.right(Unit.INSTANCE);
+ }).get();
+
+ // Fire the event. Return the error if one exists after the event, otherwise use the vanilla logic to start sleeping.
+ vanillaResult = net.neoforged.neoforge.event.EventHooks.canPlayerStartSleeping(this, p_9115_, vanillaResult);
+ if (vanillaResult.left().isPresent()) {
+ return vanillaResult;
+ }
+
+ {
+ {
+ // Start vanilla code
Either<Player.BedSleepingProblem, Unit> either = super.startSleepInBed(p_9115_).ifRight(p_9029_ -> {
this.awardStat(Stats.SLEEP_IN_BED);
CriteriaTriggers.SLEPT_IN_BED.trigger(this);
@@ -962,6 +_,7 @@
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public void handleBlockBreakAction(BlockPos p_215120_, ServerboundPlayerActionPacket.Action p_215121_, Direction p_215122_, int p_215123_, int p_215124_) {
+ net.neoforged.neoforge.event.entity.player.PlayerInteractEvent.LeftClickBlock event = net.neoforged.neoforge.common.CommonHooks.onLeftClickBlock(player, p_215120_, p_215122_, p_215121_);
+ if (event.isCanceled() || (!this.isCreative() && event.getResult() == net.neoforged.bus.api.Event.Result.DENY)) {
+ if (event.isCanceled()) {
+ return;
+ }
if (!this.player.canInteractWithBlock(p_215120_, 1.0)) {
Expand All @@ -26,7 +26,7 @@
float f = 1.0F;
BlockState blockstate = this.level.getBlockState(p_215120_);
if (!blockstate.isAir()) {
+ if (event.getUseBlock() != net.neoforged.bus.api.Event.Result.DENY)
+ if (event.getUseBlock() != net.neoforged.neoforge.common.util.TriState.FALSE)
blockstate.attack(this.level, p_215120_, this.player);
f = blockstate.getDestroyProgress(this.player, this.player.level(), p_215120_);
}
Expand Down Expand Up @@ -119,7 +119,7 @@
}
} else {
+ UseOnContext useoncontext = new UseOnContext(p_9266_, p_9269_, p_9270_);
+ if (event.getUseItem() != net.neoforged.bus.api.Event.Result.DENY) {
+ if (event.getUseItem() != net.neoforged.neoforge.common.util.TriState.FALSE) {
+ InteractionResult result = p_9268_.onItemUseFirst(useoncontext);
+ if (result != InteractionResult.PASS) return result;
+ }
Expand All @@ -128,7 +128,7 @@
+ boolean flag1 = (p_9266_.isSecondaryUseActive() && flag) && !(p_9266_.getMainHandItem().doesSneakBypassUse(p_9267_, blockpos, p_9266_) && p_9266_.getOffhandItem().doesSneakBypassUse(p_9267_, blockpos, p_9266_));
ItemStack itemstack = p_9268_.copy();
- if (!flag1) {
+ if (event.getUseBlock() == net.neoforged.bus.api.Event.Result.ALLOW || (event.getUseBlock() != net.neoforged.bus.api.Event.Result.DENY && !flag1)) {
+ if (event.getUseBlock().isTrue() || (event.getUseBlock().isDefault() && !flag1)) {
ItemInteractionResult iteminteractionresult = blockstate.useItemOn(p_9266_.getItemInHand(p_9269_), p_9267_, p_9266_, p_9269_, p_9270_);
if (iteminteractionresult.consumesAction()) {
CriteriaTriggers.ITEM_USED_ON_BLOCK.trigger(p_9266_, blockpos, itemstack);
Expand All @@ -138,8 +138,8 @@

- if (!p_9268_.isEmpty() && !p_9266_.getCooldowns().isOnCooldown(p_9268_.getItem())) {
- UseOnContext useoncontext = new UseOnContext(p_9266_, p_9269_, p_9270_);
+ if (event.getUseItem() == net.neoforged.bus.api.Event.Result.ALLOW || (!p_9268_.isEmpty() && !p_9266_.getCooldowns().isOnCooldown(p_9268_.getItem()))) {
+ if (event.getUseItem() == net.neoforged.bus.api.Event.Result.DENY) return InteractionResult.PASS;
+ if (event.getUseItem().isTrue() || (!p_9268_.isEmpty() && !p_9266_.getCooldowns().isOnCooldown(p_9268_.getItem()))) {
+ if (event.getUseItem().isFalse()) return InteractionResult.PASS;
InteractionResult interactionresult1;
if (this.isCreative()) {
int i = p_9268_.getCount();
2 changes: 1 addition & 1 deletion patches/net/minecraft/world/entity/Entity.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@
+ public boolean canTrample(BlockState state, BlockPos pos, float fallDistance) {
+ return level.random.nextFloat() < fallDistance - 0.5F
+ && this instanceof LivingEntity
+ && (this instanceof Player || net.neoforged.neoforge.event.EventHooks.getMobGriefingEvent(level, this))
+ && (this instanceof Player || net.neoforged.neoforge.event.EventHooks.canEntityGrief(level, this))
+ && this.getBbWidth() * this.getBbWidth() * this.getBbHeight() > 0.512F;
+ }
+
Expand Down
40 changes: 30 additions & 10 deletions patches/net/minecraft/world/entity/LivingEntity.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,44 @@
iterator.remove();
}

@@ -975,6 +_,7 @@
@@ -970,11 +_,12 @@
}

public boolean addEffect(MobEffectInstance p_147208_, @Nullable Entity p_147209_) {
- if (!this.canBeAffected(p_147208_)) {
+ if (!net.neoforged.neoforge.common.CommonHooks.canMobEffectBeApplied(this, p_147208_)) {
return false;
} else {
MobEffectInstance mobeffectinstance = this.activeEffects.get(p_147208_.getEffect());
boolean flag = false;
+ net.neoforged.neoforge.common.NeoForge.EVENT_BUS.post(new net.neoforged.neoforge.event.entity.living.MobEffectEvent.Added(this, mobeffectinstance, p_147208_, p_147209_));
if (mobeffectinstance == null) {
this.activeEffects.put(p_147208_.getEffect(), p_147208_);
this.onEffectAdded(p_147208_, p_147209_);
@@ -991,6 +_,9 @@
@@ -990,6 +_,14 @@
}
}

+ /**
+ * Neo: Override-Only. Call via {@link net.neoforged.neoforge.common.CommonHooks#canMobEffectBeApplied(LivingEntity, MobEffectInstance)}
+ *
+ * @param p_21197_ A mob effect instance
+ * @return If the mob effect instance can be applied to this entity
+ */
+ @Deprecated
+ @org.jetbrains.annotations.ApiStatus.OverrideOnly
public boolean canBeAffected(MobEffectInstance p_21197_) {
+ net.neoforged.neoforge.event.entity.living.MobEffectEvent.Applicable event = new net.neoforged.neoforge.event.entity.living.MobEffectEvent.Applicable(this, p_21197_);
+ net.neoforged.neoforge.common.NeoForge.EVENT_BUS.post(event);
+ if (event.getResult() != net.neoforged.bus.api.Event.Result.DEFAULT) return event.getResult() == net.neoforged.bus.api.Event.Result.ALLOW;
if (this.getType().is(EntityTypeTags.IMMUNE_TO_INFESTED)) {
return !p_21197_.is(MobEffects.INFESTED);
} else if (this.getType().is(EntityTypeTags.IMMUNE_TO_OOZING)) {
@@ -1003,7 +_,7 @@
}

public void forceAddEffect(MobEffectInstance p_147216_, @Nullable Entity p_147217_) {
- if (this.canBeAffected(p_147216_)) {
+ if (net.neoforged.neoforge.common.CommonHooks.canMobEffectBeApplied(this, p_147216_)) {
MobEffectInstance mobeffectinstance = this.activeEffects.put(p_147216_.getEffect(), p_147216_);
if (mobeffectinstance == null) {
this.onEffectAdded(p_147216_, p_147217_);
@@ -1024,6 +_,7 @@
}

Expand Down Expand Up @@ -221,7 +241,7 @@
boolean flag = false;
if (p_21269_ instanceof WitherBoss) {
- if (this.level().getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING)) {
+ if (net.neoforged.neoforge.event.EventHooks.getMobGriefingEvent(this.level(), p_21269_)) {
+ if (net.neoforged.neoforge.event.EventHooks.canEntityGrief(this.level(), p_21269_)) {
BlockPos blockpos = this.blockPosition();
BlockState blockstate = Blocks.WITHER_ROSE.defaultBlockState();
if (this.level().getBlockState(blockpos).isAir() && blockstate.canSurvive(this.level(), blockpos)) {
Expand Down Expand Up @@ -579,9 +599,9 @@

private boolean checkBedExists() {
- return this.getSleepingPos().map(p_337701_ -> this.level().getBlockState(p_337701_).getBlock() instanceof BedBlock).orElse(false);
+ return this.getSleepingPos().map((p_289310_) -> {
+ return net.neoforged.neoforge.event.EventHooks.fireSleepingLocationCheck(this, p_289310_);
+ }).orElse(false);
+ // Neo: Overwrite the vanilla instanceof BedBlock check with isBed and fire the CanContinueSleepingEvent.
+ boolean hasBed = this.getSleepingPos().map(pos -> this.level().getBlockState(pos).isBed(this.level(), pos, this)).orElse(false);
+ return net.neoforged.neoforge.event.EventHooks.canEntityContinueSleeping(this, hasBed ? null : Player.BedSleepingProblem.NOT_POSSIBLE_HERE);
}

public void stopSleeping() {
Expand Down
21 changes: 7 additions & 14 deletions patches/net/minecraft/world/entity/Mob.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,18 @@
&& this.isAlive()
&& !this.dead
- && this.level().getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING)) {
+ && net.neoforged.neoforge.event.EventHooks.getMobGriefingEvent(this.level(), this)) {
+ && net.neoforged.neoforge.event.EventHooks.canEntityGrief(this.level(), this)) {
Vec3i vec3i = this.getPickupReach();

for (ItemEntity itementity : this.level()
@@ -736,6 +_,14 @@
@@ -732,6 +_,7 @@

@Override
public void checkDespawn() {
+ if (net.neoforged.neoforge.event.EventHooks.checkMobDespawn(this)) return;
if (this.level().getDifficulty() == Difficulty.PEACEFUL && this.shouldDespawnInPeaceful()) {
this.discard();
} else if (!this.isPersistenceRequired() && !this.requiresCustomPersistence()) {
Entity entity = this.level().getNearestPlayer(this, -1.0);
+ net.neoforged.bus.api.Event.Result result = net.neoforged.neoforge.event.EventHooks.canEntityDespawn(this, (ServerLevel) this.level());
+ if (result == net.neoforged.bus.api.Event.Result.DENY) {
+ noActionTime = 0;
+ entity = null;
+ } else if (result == net.neoforged.bus.api.Event.Result.ALLOW) {
+ this.discard();
+ entity = null;
+ }
if (entity != null) {
double d0 = entity.distanceToSqr(this);
int i = this.getType().getCategory().getDespawnDistance();
@@ -1135,6 +_,11 @@
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

protected boolean checkExtraStartConditions(ServerLevel p_23174_, Villager p_23175_) {
- if (!p_23174_.getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING)) {
+ if (!net.neoforged.neoforge.event.EventHooks.getMobGriefingEvent(p_23174_, p_23175_)) {
+ if (!net.neoforged.neoforge.event.EventHooks.canEntityGrief(p_23174_, p_23175_)) {
return false;
} else if (p_23175_.getVillagerData().getProfession() != VillagerProfession.FARMER) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
BlockPos blockpos = this.mob.blockPosition();
if (IS_TALL_GRASS.test(this.level.getBlockState(blockpos))) {
- if (this.level.getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING)) {
+ if (net.neoforged.neoforge.event.EventHooks.getMobGriefingEvent(this.level, this.mob)) {
+ if (net.neoforged.neoforge.event.EventHooks.canEntityGrief(this.level, this.mob)) {
this.level.destroyBlock(blockpos, false);
}

Expand All @@ -14,7 +14,7 @@
BlockPos blockpos1 = blockpos.below();
if (this.level.getBlockState(blockpos1).is(Blocks.GRASS_BLOCK)) {
- if (this.level.getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING)) {
+ if (net.neoforged.neoforge.event.EventHooks.getMobGriefingEvent(this.level, this.mob)) {
+ if (net.neoforged.neoforge.event.EventHooks.canEntityGrief(this.level, this.mob)) {
this.level.levelEvent(2001, blockpos1, Block.getId(Blocks.GRASS_BLOCK.defaultBlockState()));
this.level.setBlock(blockpos1, Blocks.DIRT.defaultBlockState(), 2);
}
Loading

0 comments on commit 02a1f61

Please sign in to comment.