generated from FabricMC/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a40bc3
commit 57afbe0
Showing
9 changed files
with
153 additions
and
9 deletions.
There are no files selected for viewing
Empty file.
27 changes: 27 additions & 0 deletions
27
src/main/java/io/github/fabriccommunity/events/impl/InteractionsImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.github.fabriccommunity.events.impl; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import io.github.fabriccommunity.events.play.ItemEvents; | ||
import net.minecraft.entity.LivingEntity; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.util.ActionResult; | ||
import net.minecraft.world.World; | ||
|
||
/** | ||
* Some longer mixin implementations are done in classes such as this one in order to make debugging easier. | ||
*/ | ||
public final class InteractionsImpl { | ||
private InteractionsImpl() { | ||
} | ||
|
||
public static ItemStack eatFood(LivingEntity self, World world, ItemStack original) { | ||
AtomicReference<ItemStack> eaten = new AtomicReference<>(original); | ||
AtomicReference<ItemStack> moddedResultReference = new AtomicReference<>(); | ||
ActionResult eventResult = ItemEvents.EAT_FOOD.invoker().onPlayerEat(self, world, original, eaten, moddedResultReference); | ||
|
||
ItemStack defaultResult = eventResult == ActionResult.FAIL ? original : self.eatFood(world, eaten.get()); | ||
ItemStack moddedResult = moddedResultReference.get(); | ||
return moddedResult == null ? defaultResult : moddedResult; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/io/github/fabriccommunity/events/mixin/item/MixinItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.github.fabriccommunity.events.mixin.item; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Redirect; | ||
|
||
import io.github.fabriccommunity.events.impl.InteractionsImpl; | ||
import net.minecraft.entity.LivingEntity; | ||
import net.minecraft.item.Item; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.world.World; | ||
|
||
@Mixin(Item.class) | ||
public class MixinItem { | ||
@Redirect( | ||
at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/LivingEntity;eatFood(Lnet/minecraft/world/World;Lnet/minecraft/item/ItemStack;)Lnet/minecraft/item/ItemStack;"), | ||
method = "finishUsing" | ||
) | ||
private ItemStack onEatFood(LivingEntity self, World world, ItemStack original) { | ||
return InteractionsImpl.eatFood(self, world, original); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/io/github/fabriccommunity/events/play/ItemEvents.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.github.fabriccommunity.events.play; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import net.fabricmc.fabric.api.event.Event; | ||
import net.fabricmc.fabric.api.event.EventFactory; | ||
import net.minecraft.entity.LivingEntity; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.util.ActionResult; | ||
import net.minecraft.world.World; | ||
|
||
/** | ||
* Collection of events pertaining to items and item stacks in gameplay. | ||
*/ | ||
public final class ItemEvents { | ||
/** | ||
* An event which runs when an entity tries to eat food. | ||
*/ | ||
public static final Event<EatFood> EAT_FOOD = EventFactory.createArrayBacked(EatFood.class, listeners -> (entity, world, original, eaten, result) -> { | ||
for (EatFood listener : listeners) { | ||
ActionResult eventResult = listener.onPlayerEat(entity, world, original, eaten, result); | ||
|
||
if (eventResult != ActionResult.PASS) { | ||
return eventResult; | ||
} | ||
} | ||
|
||
return ActionResult.PASS; | ||
}); | ||
|
||
/** | ||
* Called when an entity tries to eat an item. | ||
* @author Valoeghese | ||
*/ | ||
@FunctionalInterface | ||
public interface EatFood { | ||
/** | ||
* @param entity the entity eating the food. | ||
* @param world the world the entity is eating the food in. | ||
* @param original the original stack of food to be eaten. | ||
* @param eaten the stack of food which is to be eaten. If the value stored herein differs from {@code original}, the value has been modded.<br/> | ||
* @param result {@code null} by default. if {@code null}, the stack returned from eating will be the result of the eat method. | ||
* If this is altered the resulting stack will be the item stored in here, regardless of whether the event fails.<br/> | ||
* @return | ||
* <ul> | ||
* <li>{@code SUCCESS} or {@code CONSUME} to cancel further event processing and eat the food in {@code stack}<br/> | ||
* <li>{@code PASS} pass event handling on to further processing. If all listeners pass, it is treated as a {@code SUCCESS}. | ||
* <li>{@code FAIL} to cancel further event processing and do not eat the food. | ||
* </ul> | ||
*/ | ||
ActionResult onPlayerEat(LivingEntity entity, World world, final ItemStack original, AtomicReference<ItemStack> eaten, AtomicReference<ItemStack> result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/test/java/io/github/fabriccommunity/events/test/EatFoodTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.github.fabriccommunity.events.test; | ||
|
||
import io.github.fabriccommunity.events.play.ItemEvents; | ||
import net.fabricmc.api.ModInitializer; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.item.Items; | ||
import net.minecraft.util.ActionResult; | ||
|
||
public class EatFoodTest implements ModInitializer { | ||
public static final boolean ENABLED = true; | ||
|
||
@Override | ||
public void onInitialize() { | ||
if (ENABLED) { | ||
// This code makes potatoes behave as if they were golden apples. | ||
ItemEvents.EAT_FOOD.register((entity, world, original, eaten, result) -> { | ||
if (original.getItem() == Items.POTATO) { | ||
eaten.set(new ItemStack(Items.GOLDEN_APPLE)); // use golden apple eat effects | ||
// Make it return what it would be if the original had been eaten. | ||
// Otherwise it will return nothing due to eating a stack of 1 golden apples giving back 0 golden apples, which is nothing. | ||
ItemStack resultstk = original.copy(); | ||
resultstk.decrement(1); | ||
result.set(resultstk); // | ||
return ActionResult.SUCCESS; | ||
} | ||
|
||
return ActionResult.PASS; | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters