-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
20 changed files
with
231 additions
and
120 deletions.
There are no files selected for viewing
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
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
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
134 changes: 134 additions & 0 deletions
134
src/main/java/de/siphalor/capsaicin/impl/food/FoodHandler.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,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()); | ||
} | ||
} |
65 changes: 0 additions & 65 deletions
65
src/main/java/de/siphalor/capsaicin/impl/food/GenericFoodHandler.java
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
src/main/java/de/siphalor/capsaicin/impl/food/eatingtime/EatingTimeHandler.java
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.