-
-
Notifications
You must be signed in to change notification settings - Fork 414
Food Components #8218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Absolutionism
wants to merge
4
commits into
SkriptLang:dev/feature
Choose a base branch
from
Absolutionism:dev/FoodComponents
base: dev/feature
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Food Components #8218
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8e25528
Merge remote-tracking branch 'upstream/dev/patch' into dev/feature
Absolutionism 4190cf1
Merge branch 'dev/feature' of https://github.com/SkriptLang/Skript in…
Absolutionism aa125e3
Initial Commit
Absolutionism b5ae9c4
Merge branch 'dev/feature' into dev/FoodComponents
Absolutionism File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
20 changes: 20 additions & 0 deletions
20
src/main/java/org/skriptlang/skript/bukkit/itemcomponents/food/FoodExperimentalSyntax.java
This file contains hidden or 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,20 @@ | ||
| package org.skriptlang.skript.bukkit.itemcomponents.food; | ||
|
|
||
| import ch.njol.skript.lang.SyntaxElement; | ||
| import ch.njol.skript.registrations.Feature; | ||
| import org.skriptlang.skript.lang.experiment.ExperimentData; | ||
| import org.skriptlang.skript.lang.experiment.SimpleExperimentalSyntax; | ||
|
|
||
| /** | ||
| * Typed {@link SimpleExperimentalSyntax} for {@link SyntaxElement}s that require {@link Feature#FOOD_COMPONENTS}. | ||
| */ | ||
| public interface FoodExperimentalSyntax extends SimpleExperimentalSyntax { | ||
|
|
||
| ExperimentData EXPERIMENT_DATA = ExperimentData.createSingularData(Feature.FOOD_COMPONENTS); | ||
|
|
||
| @Override | ||
| default ExperimentData getExperimentData() { | ||
| return EXPERIMENT_DATA; | ||
| } | ||
|
|
||
| } |
98 changes: 98 additions & 0 deletions
98
src/main/java/org/skriptlang/skript/bukkit/itemcomponents/food/FoodModule.java
This file contains hidden or 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,98 @@ | ||
| package org.skriptlang.skript.bukkit.itemcomponents.food; | ||
|
|
||
| import ch.njol.skript.Skript; | ||
| import ch.njol.skript.aliases.ItemType; | ||
| import ch.njol.skript.classes.ClassInfo; | ||
| import ch.njol.skript.classes.Parser; | ||
| import ch.njol.skript.expressions.base.EventValueExpression; | ||
| import ch.njol.skript.lang.ParseContext; | ||
| import ch.njol.skript.registrations.Classes; | ||
| import ch.njol.skript.util.ItemSource; | ||
| import ch.njol.skript.util.slot.Slot; | ||
| import io.papermc.paper.datacomponent.item.FoodProperties; | ||
| import org.bukkit.inventory.ItemStack; | ||
| import org.skriptlang.skript.addon.AddonModule; | ||
| import org.skriptlang.skript.addon.SkriptAddon; | ||
| import org.skriptlang.skript.bukkit.itemcomponents.food.elements.CondFoodCompAlwaysEat; | ||
| import org.skriptlang.skript.bukkit.itemcomponents.food.elements.EffFoodCompAlwaysEat; | ||
| import org.skriptlang.skript.bukkit.itemcomponents.food.elements.ExprFoodCompNutrition; | ||
| import org.skriptlang.skript.bukkit.itemcomponents.food.elements.ExprFoodCompSaturation; | ||
| import org.skriptlang.skript.bukkit.itemcomponents.food.elements.ExprFoodComponent; | ||
| import org.skriptlang.skript.bukkit.itemcomponents.food.elements.ExprSecBlankFoodComp; | ||
| import org.skriptlang.skript.lang.converter.Converter; | ||
| import org.skriptlang.skript.lang.converter.Converters; | ||
| import org.skriptlang.skript.registration.SyntaxRegistry; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.function.Consumer; | ||
|
|
||
| public class FoodModule implements AddonModule { | ||
|
|
||
| @Override | ||
| public boolean canLoad(SkriptAddon addon) { | ||
| return Skript.classExists("io.papermc.paper.datacomponent.item.FoodProperties"); | ||
| } | ||
|
|
||
| @Override | ||
| public void init(SkriptAddon addon) { | ||
| Classes.registerClass(new ClassInfo<>(FoodWrapper.class, "foodcomponent") | ||
| .user("food ?components?") | ||
| .name("Food Component") | ||
| .description(""" | ||
| Represents a food component used for items. | ||
| NOTE: Food component elements are experimental. Thus, they are subject to change and may not work as intended. | ||
| """) | ||
| .requiredPlugins("Minecraft 1.21.3+") | ||
| .since("INSERT VERSION") | ||
| .defaultExpression(new EventValueExpression<>(FoodWrapper.class)) | ||
| .parser(new Parser<>() { | ||
| @Override | ||
| public boolean canParse(ParseContext context) { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString(FoodWrapper wrapper, int flags) { | ||
| return "food component"; | ||
| } | ||
|
|
||
| @Override | ||
| public String toVariableNameString(FoodWrapper wrapper) { | ||
| return "food component#" + wrapper.hashCode(); | ||
| } | ||
| }) | ||
| .after("itemstack", "itemtype", "slot") | ||
| ); | ||
|
|
||
| Converters.registerConverter(FoodProperties.class, FoodWrapper.class, FoodWrapper::new, Converter.NO_RIGHT_CHAINING); | ||
| Converters.registerConverter(ItemStack.class, FoodWrapper.class, FoodWrapper::new, Converter.NO_RIGHT_CHAINING); | ||
| Converters.registerConverter(ItemType.class, FoodWrapper.class, itemType -> new FoodWrapper(new ItemSource<>(itemType)), Converter.NO_RIGHT_CHAINING); | ||
| Converters.registerConverter(Slot.class, FoodWrapper.class, slot -> { | ||
| ItemSource<Slot> itemSource = ItemSource.fromSlot(slot); | ||
| if (itemSource == null) | ||
| return null; | ||
| return new FoodWrapper(itemSource); | ||
| }, Converter.NO_RIGHT_CHAINING); | ||
| } | ||
|
|
||
| @Override | ||
| public void load(SkriptAddon addon) { | ||
| register(addon.syntaxRegistry(), | ||
|
|
||
| CondFoodCompAlwaysEat::register, | ||
|
|
||
| EffFoodCompAlwaysEat::register, | ||
|
|
||
| ExprFoodCompNutrition::register, | ||
| ExprFoodComponent::register, | ||
| ExprFoodCompSaturation::register, | ||
|
|
||
| ExprSecBlankFoodComp::register | ||
| ); | ||
| } | ||
|
|
||
| private void register(SyntaxRegistry registry, Consumer<SyntaxRegistry>... consumers) { | ||
| Arrays.stream(consumers).forEach(consumer -> consumer.accept(registry)); | ||
| } | ||
|
|
||
| } |
95 changes: 95 additions & 0 deletions
95
src/main/java/org/skriptlang/skript/bukkit/itemcomponents/food/FoodWrapper.java
This file contains hidden or 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,95 @@ | ||
| package org.skriptlang.skript.bukkit.itemcomponents.food; | ||
|
|
||
| import ch.njol.skript.util.ItemSource; | ||
| import io.papermc.paper.datacomponent.DataComponentType.Valued; | ||
| import io.papermc.paper.datacomponent.DataComponentTypes; | ||
| import io.papermc.paper.datacomponent.item.FoodProperties; | ||
| import io.papermc.paper.datacomponent.item.FoodProperties.Builder; | ||
| import org.bukkit.inventory.ItemStack; | ||
| import org.skriptlang.skript.bukkit.itemcomponents.ComponentWrapper; | ||
|
|
||
| /** | ||
| * A {@link ComponentWrapper} for getting and setting data on a {@link FoodProperties} component. | ||
| */ | ||
| @SuppressWarnings("UnstableApiUsage") | ||
| public class FoodWrapper extends ComponentWrapper<FoodProperties, Builder> { | ||
|
|
||
| public FoodWrapper(ItemStack itemStack) { | ||
| super(itemStack); | ||
| } | ||
|
|
||
| public FoodWrapper(ItemSource<?> itemSource) { | ||
| super(itemSource); | ||
| } | ||
|
|
||
| public FoodWrapper(FoodProperties component) { | ||
| super(component); | ||
| } | ||
|
|
||
| public FoodWrapper(Builder builder) { | ||
| super(builder); | ||
| } | ||
|
|
||
| @Override | ||
| public Valued<FoodProperties> getDataComponentType() { | ||
| return DataComponentTypes.FOOD; | ||
| } | ||
|
|
||
| @Override | ||
| protected FoodProperties getComponent(ItemStack itemStack) { | ||
| FoodProperties food = itemStack.getData(DataComponentTypes.FOOD); | ||
| if (food != null) | ||
| return food; | ||
| return FoodProperties.food().build(); | ||
| } | ||
|
|
||
| @Override | ||
| protected Builder getBuilder(ItemStack itemStack) { | ||
| FoodProperties food = itemStack.getData(DataComponentTypes.FOOD); | ||
| if (food != null) | ||
| return food.toBuilder(); | ||
| return FoodProperties.food(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void setComponent(ItemStack itemStack, FoodProperties component) { | ||
| itemStack.setData(DataComponentTypes.FOOD, component); | ||
| } | ||
|
|
||
| @Override | ||
| protected Builder getBuilder(FoodProperties component) { | ||
| return component.toBuilder(); | ||
| } | ||
|
|
||
| @Override | ||
| public FoodWrapper clone() { | ||
| FoodProperties base = getComponent(); | ||
| FoodWrapper clone = newWrapper(); | ||
| clone.editBuilder(builder -> { | ||
| builder.canAlwaysEat(base.canAlwaysEat()); | ||
| builder.nutrition(base.nutrition()); | ||
| builder.saturation(base.saturation()); | ||
| }); | ||
| return clone; | ||
| } | ||
|
|
||
| @Override | ||
| public FoodProperties newComponent() { | ||
| return newBuilder().build(); | ||
| } | ||
|
|
||
| @Override | ||
| public Builder newBuilder() { | ||
| return FoodProperties.food(); | ||
| } | ||
|
|
||
| @Override | ||
| public FoodWrapper newWrapper() { | ||
| return newInstance(); | ||
| } | ||
|
|
||
| public static FoodWrapper newInstance() { | ||
| return new FoodWrapper(FoodProperties.food().build()); | ||
| } | ||
|
|
||
| } |
55 changes: 55 additions & 0 deletions
55
...java/org/skriptlang/skript/bukkit/itemcomponents/food/elements/CondFoodCompAlwaysEat.java
This file contains hidden or 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,55 @@ | ||
| package org.skriptlang.skript.bukkit.itemcomponents.food.elements; | ||
|
|
||
| import ch.njol.skript.conditions.base.PropertyCondition; | ||
| import ch.njol.skript.doc.Description; | ||
| import ch.njol.skript.doc.Example; | ||
| import ch.njol.skript.doc.Name; | ||
| import ch.njol.skript.doc.RequiredPlugins; | ||
| import ch.njol.skript.doc.Since; | ||
| import org.skriptlang.skript.bukkit.itemcomponents.food.FoodExperimentalSyntax; | ||
| import org.skriptlang.skript.bukkit.itemcomponents.food.FoodWrapper; | ||
| import org.skriptlang.skript.registration.SyntaxRegistry; | ||
|
|
||
| @Name("Food Component - Can Always Be Eaten") | ||
| @Description(""" | ||
| Whether an item can be eaten when the player's hunger bar is full. | ||
| NOTE: Food component elements are experimental. Thus, they are subject to change and may not work as intended. | ||
| """) | ||
| @Example(""" | ||
| if {_item} can not always be eaten: | ||
| allow {_item} to always be eaten | ||
| """) | ||
| @Example(""" | ||
| set {_component} to the food component of {_item} | ||
| if {_component} can be eaten when full: | ||
| prevent {_component} from being eaten when full | ||
| """) | ||
| @RequiredPlugins("Minecraft 1.21.3+") | ||
| @Since("INSERT VERSION") | ||
| public class CondFoodCompAlwaysEat extends PropertyCondition<FoodWrapper> implements FoodExperimentalSyntax { | ||
|
|
||
| public static void register(SyntaxRegistry registry) { | ||
| registry.register( | ||
| SyntaxRegistry.CONDITION, | ||
| infoBuilder( | ||
| CondFoodCompAlwaysEat.class, | ||
| PropertyType.CAN, | ||
| "(always be eaten|be eaten when full)", | ||
| "foodcomponents" | ||
| ) | ||
| .supplier(CondFoodCompAlwaysEat::new) | ||
| .build() | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean check(FoodWrapper wrapper) { | ||
| return wrapper.getComponent().canAlwaysEat(); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getPropertyName() { | ||
| return "always be eaten"; | ||
| } | ||
|
|
||
| } |
80 changes: 80 additions & 0 deletions
80
.../java/org/skriptlang/skript/bukkit/itemcomponents/food/elements/EffFoodCompAlwaysEat.java
This file contains hidden or 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,80 @@ | ||
| package org.skriptlang.skript.bukkit.itemcomponents.food.elements; | ||
|
|
||
| import ch.njol.skript.doc.Description; | ||
| import ch.njol.skript.doc.Example; | ||
| import ch.njol.skript.doc.Name; | ||
| import ch.njol.skript.doc.RequiredPlugins; | ||
| import ch.njol.skript.doc.Since; | ||
| import ch.njol.skript.lang.Effect; | ||
| import ch.njol.skript.lang.Expression; | ||
| import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
| import ch.njol.skript.lang.SyntaxStringBuilder; | ||
| import ch.njol.util.Kleenean; | ||
| import org.bukkit.event.Event; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.skriptlang.skript.bukkit.itemcomponents.food.FoodExperimentalSyntax; | ||
| import org.skriptlang.skript.bukkit.itemcomponents.food.FoodWrapper; | ||
| import org.skriptlang.skript.registration.SyntaxInfo; | ||
| import org.skriptlang.skript.registration.SyntaxRegistry; | ||
|
|
||
| @Name("Food Component - Always Be Eaten") | ||
| @Description(""" | ||
| Whether an item should be eaten when the player's hunger bar is full. | ||
| NOTE: Food component elements are experimental. Thus, they are subject to change and may not work as intended. | ||
| """) | ||
| @Example(""" | ||
| if {_item} can not always be eaten: | ||
| allow {_item} to always be eaten | ||
| """) | ||
| @Example(""" | ||
| set {_component} to the food component of {_item} | ||
| if {_component} can be eaten when full: | ||
| prevent {_component} from being eaten when full | ||
| """) | ||
| @RequiredPlugins("Minecraft 1.21.3+") | ||
| @Since("INSERT VERSION") | ||
| public class EffFoodCompAlwaysEat extends Effect implements FoodExperimentalSyntax { | ||
|
|
||
| public static void register(SyntaxRegistry registry) { | ||
| registry.register( | ||
| SyntaxRegistry.EFFECT, | ||
| SyntaxInfo.builder(EffFoodCompAlwaysEat.class) | ||
| .addPatterns( | ||
| "(allow|force) %foodcomponents% to (always be eaten|be eaten when full)", | ||
| "prevent %foodcomponents% from (always being eaten|being eaten when full)" | ||
| ) | ||
| .supplier(EffFoodCompAlwaysEat::new) | ||
| .build() | ||
| ); | ||
| } | ||
|
|
||
| private boolean alwaysEat; | ||
| private Expression<FoodWrapper> wrappers; | ||
|
|
||
| @Override | ||
| public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
| alwaysEat = matchedPattern == 0; | ||
| //noinspection unchecked | ||
| wrappers = (Expression<FoodWrapper>) exprs[0]; | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| protected void execute(Event event) { | ||
| wrappers.stream(event).forEach(wrapper -> wrapper.editBuilder(builder -> { | ||
| builder.canAlwaysEat(alwaysEat); | ||
| })); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString(@Nullable Event event, boolean debug) { | ||
| SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug); | ||
| if (alwaysEat) { | ||
| builder.append("allow", wrappers, "to always be eaten"); | ||
| } else { | ||
| builder.append("prevent", wrappers, "from always being eaten"); | ||
| } | ||
| return builder.toString(); | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.