Skip to content

Commit

Permalink
Merge branch '1.18' into 1.19
Browse files Browse the repository at this point in the history
  • Loading branch information
Siphalor committed Dec 26, 2022
2 parents 561763d + ebf1e2a commit ef4ad76
Show file tree
Hide file tree
Showing 20 changed files with 231 additions and 120 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ repositories {
dependencies {
// Use the latest version from the badge at the top of this README
modImplementation("de.siphalor:capsaicin-1.19:1.1.0")
modImplementation("de.siphalor:capsaicin-1.19:1.2.0+mc1.19.3")
// Alternatively, you may embed (jar-in-jar) this library with the following dependency configuration
include(modApi("de.siphalor:capsaicin-1.19:1.1.0"))
include(modApi("de.siphalor:capsaicin-1.19:1.2.0+mc1.19.3"))
}
```
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ dependencies {

// AppleSkin
modCompileOnly "squeek.appleskin:appleskin-fabric:mc1.19.3-2.4.2:api"
modLocalRuntime("squeek.appleskin:appleskin-fabric:mc1.19.3-2.4.2") {
modLocalRuntime(modCompileOnly("squeek.appleskin:appleskin-fabric:mc1.19.3-2.4.2")) {
exclude module: "modmenu"
}
modLocalRuntime "net.fabricmc.fabric-api:fabric-api:0.70.0+1.19.3"
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ yarn_mappings = 2
loader_version = 0.14.12

# Mod Properties
mod_version = 1.1.0
mod_version = 1.2.0
release_type = release
maven_group = de.siphalor
archives_base_name = capsaicin
12 changes: 12 additions & 0 deletions src/main/java/de/siphalor/capsaicin/api/food/FoodContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,16 @@ public interface FoodContext {
* @return the entity or <code>null</code> if no entity is known in the current context
*/
@Nullable LivingEntity user();

/**
* The amount of hunger that is restored <b>before any modifications</b>.
* @return the amount of hunger
*/
int originalFoodHunger();

/**
* The saturation modifier <b>before any modifications</b>.
* @return the saturation modifier
*/
float originalFoodSaturationModifier();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class AppleSkinPlugin implements AppleSkinApi {
public void registerEvents() {
FoodValuesEvent.EVENT.register(event -> {
FoodProperties foodProperties = new FoodPropertiesImpl(event.modifiedFoodValues.hunger, event.modifiedFoodValues.saturationModifier, false, new ArrayList<>());
FoodProperties newFoodProperties = FoodModifications.PROPERTIES_MODIFIERS.apply(foodProperties, new FoodContextImpl(event.itemStack, null, event.player));
FoodValues defaultFoodValues = event.defaultFoodValues;
FoodProperties newFoodProperties = FoodModifications.PROPERTIES_MODIFIERS.apply(foodProperties, new FoodContextImpl(event.itemStack, null, defaultFoodValues.hunger, defaultFoodValues.saturationModifier, event.player));
if (foodProperties != newFoodProperties || foodProperties.isChanged()) {
event.modifiedFoodValues = new FoodValues(newFoodProperties.getHunger(), newFoodProperties.getSaturationModifier());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
import org.jetbrains.annotations.Nullable;

@ApiStatus.Internal
public record FoodContextImpl(@Nullable ItemStack stack, @Nullable BlockState blockState, @Nullable LivingEntity user) implements FoodContext {
public record FoodContextImpl(@Nullable ItemStack stack, @Nullable BlockState blockState, int originalFoodHunger, float originalFoodSaturationModifier, @Nullable LivingEntity user) implements FoodContext {

}
134 changes: 134 additions & 0 deletions src/main/java/de/siphalor/capsaicin/impl/food/FoodHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package de.siphalor.capsaicin.impl.food;

import com.mojang.datafixers.util.Pair;
import de.siphalor.capsaicin.api.food.FoodContext;
import de.siphalor.capsaicin.api.food.FoodModifications;
import de.siphalor.capsaicin.api.food.FoodProperties;
import de.siphalor.capsaicin.impl.food.properties.FoodPropertiesImpl;
import de.siphalor.capsaicin.impl.util.IItem;
import net.minecraft.block.BlockState;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.item.FoodComponent;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import org.jetbrains.annotations.ApiStatus;

import java.util.ArrayList;

@ApiStatus.Internal
public class FoodHandler {
public static final ThreadLocal<FoodHandler> INSTANCE = ThreadLocal.withInitial(FoodHandler::new);

private FoodProperties foodProperties;
private int eatingTime;
private ItemStack stack;
private FoodComponent stackFoodComponent;
private BlockState blockState;
private LivingEntity user;

public ItemStack getStack() {
return stack;
}

public BlockState getBlockState() {
return blockState;
}

public LivingEntity getUser() {
return user;
}

public void withStack(ItemStack stack) {
this.stack = stack;
Item item = stack.getItem();
if (item instanceof IItem iItem) {
stackFoodComponent = iItem.capsaicin$getVanillaFoodComponent();
if (stackFoodComponent != null) {
foodProperties = FoodPropertiesImpl.from(stackFoodComponent);
}
} else {
stackFoodComponent = null;
}
if (item != null) {
// Must not call stack.getMaxUseTime() here!
// This would cause a stack overflow
eatingTime = item.getMaxUseTime(stack);
} else {
eatingTime = 0;
}
}

public void withBlockState(BlockState blockState, FoodProperties foodProperties) {
this.blockState = blockState;
this.foodProperties = foodProperties;
this.eatingTime = 0;
}

public void withUser(LivingEntity user) {
this.user = user;
}

public void reset() {
foodProperties = null;
stack = null;
stackFoodComponent = null;
blockState = null;
user = null;
}

public boolean canApply() {
return stack != null || blockState != null;
}

public FoodContext createContext() {
if (canApply()) {
return new FoodContextImpl(stack, blockState, foodProperties.getHunger(), foodProperties.getSaturationModifier(), user);
}
return null;
}

public FoodComponent getFoodComponent() {
FoodProperties propertiesIn;
if (stack != null && stackFoodComponent != null) {
propertiesIn = FoodProperties.from(stackFoodComponent);
} else {
propertiesIn = new FoodPropertiesImpl(foodProperties.getHunger(), foodProperties.getSaturationModifier(), false, new ArrayList<>());
}
FoodProperties propertiesOut = getFoodProperties(propertiesIn);
if (propertiesOut == propertiesIn && !propertiesIn.isChanged()) {
if (stackFoodComponent != null) {
return stackFoodComponent;
}
return new FoodComponent.Builder()
.hunger(propertiesIn.getHunger())
.saturationModifier(propertiesIn.getSaturationModifier())
.build();
}

FoodComponent.Builder builder = new FoodComponent.Builder()
.hunger(propertiesOut.getHunger())
.saturationModifier(propertiesOut.getSaturationModifier());
if (propertiesOut.isAlwaysEdible()) {
builder.alwaysEdible();
}
for (Pair<StatusEffectInstance, Float> statusEffect : propertiesOut.getStatusEffects()) {
builder.statusEffect(statusEffect.getFirst(), statusEffect.getSecond());
}
if (stackFoodComponent.isSnack()) {
builder.snack();
}
if (stackFoodComponent.isMeat()) {
builder.meat();
}
return builder.build();
}

public FoodProperties getFoodProperties(FoodProperties foodProperties) {
return FoodModifications.PROPERTIES_MODIFIERS.apply(foodProperties, createContext());
}

public int getEatingTime() {
return FoodModifications.EATING_TIME_MODIFIERS.apply(eatingTime, createContext());
}
}

This file was deleted.

This file was deleted.

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

@ApiStatus.Internal
public class CapsaicinMixinConfig implements IMixinConfigPlugin {
private final boolean appleskinLoaded = FabricLoader.getInstance().isModLoaded("appleskin");
private String ItemStack$getMaxUseTime$remapped;
private String EatingTimeHandler$getEatingTime$desc;

Expand All @@ -34,6 +35,9 @@ public String getRefMapperConfig() {

@Override
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
if (mixinClassName.startsWith("de.siphalor.capsaicin.impl.mixin.client.appleskin")) {
return appleskinLoaded;
}
return true;
}

Expand Down
19 changes: 10 additions & 9 deletions src/main/java/de/siphalor/capsaicin/impl/mixin/MixinCakeBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import com.mojang.datafixers.util.Pair;
import de.siphalor.capsaicin.api.food.FoodEvents;
import de.siphalor.capsaicin.api.food.FoodProperties;
import de.siphalor.capsaicin.impl.food.GenericFoodHandler;
import de.siphalor.capsaicin.impl.food.FoodHandler;
import de.siphalor.capsaicin.impl.food.event.EatenEvent;
import de.siphalor.capsaicin.impl.food.properties.FoodPropertiesImpl;
import net.minecraft.block.BlockState;
Expand All @@ -12,6 +11,7 @@
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.player.HungerManager;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.FoodComponent;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.random.Random;
Expand All @@ -30,25 +30,26 @@
public class MixinCakeBlock {
@Inject(method = "tryEat", at = @At("HEAD"))
private static void onEat(WorldAccess world, BlockPos pos, BlockState state, PlayerEntity player, CallbackInfoReturnable<ActionResult> cir) {
GenericFoodHandler.currentUser = player;
GenericFoodHandler.currentBlockState = state;
GenericFoodHandler.currentStack = null;
FoodHandler foodHandler = FoodHandler.INSTANCE.get();
foodHandler.withUser(player);
foodHandler.withBlockState(state, new FoodPropertiesImpl(2, 0.1F, false, new ArrayList<>()));
}

@Redirect(method = "tryEat", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/HungerManager;add(IF)V"))
private static void eat(HungerManager hungerManager, int hunger, float saturationModifier) {
FoodProperties foodProperties = GenericFoodHandler.getFoodProperties(new FoodPropertiesImpl(hunger, saturationModifier, false, new ArrayList<>()));
FoodHandler foodHandler = FoodHandler.INSTANCE.get();
FoodComponent foodProperties = foodHandler.getFoodComponent();

LivingEntity user = GenericFoodHandler.currentUser;
LivingEntity user = foodHandler.getUser();
Random random = user.getRandom();
hungerManager.add(foodProperties.getHunger(), foodProperties.getSaturationModifier());
for (Pair<StatusEffectInstance, Float> effect : foodProperties.getStatusEffects()) {
if (random.nextFloat() < effect.getSecond()) {
user.addStatusEffect(effect.getFirst());
}
}
FoodEvents.EATEN.emit(new EatenEvent(GenericFoodHandler.createContext()));
FoodEvents.EATEN.emit(new EatenEvent(foodHandler.createContext()));

GenericFoodHandler.reset();
foodHandler.reset();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.siphalor.capsaicin.impl.mixin;

import de.siphalor.capsaicin.impl.food.GenericFoodHandler;
import net.minecraft.entity.LivingEntity;
import de.siphalor.capsaicin.impl.food.FoodHandler;
import net.minecraft.entity.passive.CatEntity;
import net.minecraft.entity.passive.WolfEntity;
import net.minecraft.entity.player.PlayerEntity;
Expand All @@ -19,12 +18,13 @@
public class MixinFeedableEntities {
@Inject(method = "interactMob", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/Item;getFoodComponent()Lnet/minecraft/item/FoodComponent;"), locals = LocalCapture.CAPTURE_FAILSOFT)
public void onFeedMob(PlayerEntity player, Hand hand, CallbackInfoReturnable<ActionResult> cir, ItemStack stack, Item item) {
GenericFoodHandler.currentUser = (LivingEntity) (Object) this;
GenericFoodHandler.currentStack = stack;
FoodHandler foodHandler = FoodHandler.INSTANCE.get();
foodHandler.withUser(player);
foodHandler.withStack(stack);
}

@Inject(method = "interactMob", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/Item;getFoodComponent()Lnet/minecraft/item/FoodComponent;", shift = At.Shift.AFTER))
public void onMobFed(CallbackInfoReturnable<ActionResult> cir) {
GenericFoodHandler.reset();
FoodHandler.INSTANCE.get().reset();
}
}
Loading

0 comments on commit ef4ad76

Please sign in to comment.