Skip to content
This repository has been archived by the owner on Jan 14, 2022. It is now read-only.

Commit

Permalink
Merge branch 'eat-event' into 1.18-forge
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Jan 1, 2022
2 parents 189458c + b7ae6b3 commit 0776baf
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;

public class CommonEventRegistry {

private static final IEventBus forgeEventBus = MinecraftForge.EVENT_BUS;

@Deprecated(since = "3.1.1")
public static boolean postPistonAddBlockLineEvent(BlockState blockState, Level level, BlockPos blockPos, Direction direction) {
return MinecraftForge.EVENT_BUS.post(new org.auioc.mods.ahutils.common.event.impl.PistonAddBlockLineEvent(blockState, level, blockPos, direction));
return forgeEventBus.post(new org.auioc.mods.ahutils.common.event.impl.PistonAddBlockLineEvent(blockState, level, blockPos, direction));
}

public static boolean postPistonCheckPushableEvent(BlockState blockState, Level level, BlockPos blockPos, Direction pushDirection, boolean p_60209_, Direction p_60210_) {
return MinecraftForge.EVENT_BUS.post(new PistonCheckPushableEvent(blockState, level, blockPos, pushDirection, p_60209_, p_60210_));
return forgeEventBus.post(new PistonCheckPushableEvent(blockState, level, blockPos, pushDirection, p_60209_, p_60210_));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.auioc.mods.ahutils.mixin.server;

import java.util.ArrayList;
import java.util.List;
import com.mojang.datafixers.util.Pair;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;

@Mixin(value = LivingEntity.class)
public class MixinLivingEntity {

// @org.spongepowered.asm.mixin.Debug(export = true, print = true)
@Redirect(
method = "Lnet/minecraft/world/entity/LivingEntity;eat(Lnet/minecraft/world/level/Level;Lnet/minecraft/world/item/ItemStack;)Lnet/minecraft/world/item/ItemStack;",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/entity/LivingEntity;addEatEffect(Lnet/minecraft/world/item/ItemStack;Lnet/minecraft/world/level/Level;Lnet/minecraft/world/entity/LivingEntity;)V",
ordinal = 0
),
require = 1,
allow = 1
)
private void onEatAddEffect(LivingEntity thisClass, ItemStack food, Level level, LivingEntity entity) {
if (!level.isClientSide) {
List<MobEffectInstance> effects = new ArrayList<>();
for (Pair<MobEffectInstance, Float> pair : food.getItem().getFoodProperties().getEffects()) {
if (pair.getFirst() != null && level.random.nextFloat() < pair.getSecond()) {
effects.add(new MobEffectInstance(pair.getFirst()));
}
}
effects = org.auioc.mods.ahutils.server.event.ServerEventRegistry.postLivingEatAddEffectEvent(entity, food, effects);
for (MobEffectInstance instance : effects) {
entity.addEffect(instance);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.auioc.mods.ahutils.server.event;

import static org.auioc.mods.ahutils.AHUtils.LOGGER;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.apache.logging.log4j.Marker;
import org.auioc.mods.ahutils.server.event.impl.LivingEatAddEffectEvent;
import org.auioc.mods.ahutils.server.event.impl.ServerLoginEvent;
import org.auioc.mods.ahutils.server.event.impl.ServerPlayerEntitySendMessageEvent;
import org.auioc.mods.ahutils.utils.LogUtil;
Expand All @@ -12,11 +15,16 @@
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.protocol.handshake.ClientIntentionPacket;
import net.minecraft.network.protocol.login.ClientboundLoginDisconnectPacket;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;

public class ServerEventRegistry {

private static final Marker marker = LogUtil.getMarker("ServerHooks");
private static final IEventBus forgeEventBus = MinecraftForge.EVENT_BUS;

// Return true if the event was Cancelable cancelled

Expand All @@ -40,4 +48,9 @@ public static boolean postServerPlayerEntitySendMessageEvent(Component message,
return MinecraftForge.EVENT_BUS.post(new ServerPlayerEntitySendMessageEvent(message, type, uuid));
}

public static List<MobEffectInstance> postLivingEatAddEffectEvent(LivingEntity entity, ItemStack food, List<MobEffectInstance> effects) {
LivingEatAddEffectEvent event = new LivingEatAddEffectEvent(entity, food, effects);
return forgeEventBus.post(event) ? new ArrayList<>() : event.getEffects();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.auioc.mods.ahutils.server.event.impl;

import java.util.List;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.event.entity.living.LivingEvent;
import net.minecraftforge.eventbus.api.Cancelable;

@Cancelable
public class LivingEatAddEffectEvent extends LivingEvent {

private final ItemStack food;
private final List<MobEffectInstance> effects;

public LivingEatAddEffectEvent(LivingEntity entity, ItemStack food, List<MobEffectInstance> effects) {
super(entity);
this.food = food;
this.effects = effects;
}

public ItemStack getFood() {
return food;
}

public List<MobEffectInstance> getEffects() {
return effects;
}

}
2 changes: 1 addition & 1 deletion src/main/resources/ahutils.mixin.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"required": true,
"package": "org.auioc.mods.ahutils.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": ["server.MixinLootContext", "server.MixinServerPlayerEntity", "common.MixinPistonBaseBlock"],
"mixins": ["server.MixinLootContext", "server.MixinServerPlayerEntity", "common.MixinPistonBaseBlock", "server.MixinLivingEntity"],
"client": [],
"server": ["server.MixinServerLifecycleHooks"],
"injectors": {
Expand Down

0 comments on commit 0776baf

Please sign in to comment.