From 73cb9d696821928ad9f73764b4dad6148e5c84e8 Mon Sep 17 00:00:00 2001 From: gnembon Date: Sun, 7 Jun 2020 18:53:17 -0300 Subject: [PATCH] added hunger, saturation, and exhaustion entity API calls. --- docs/scarpet/api/Entities.md | 18 ++++++++++++++++++ .../carpet/fakes/HungerManagerInterface.java | 3 ++- .../HungerManagerMixin_scarpetEventsMixin.java | 16 ++++++++-------- .../java/carpet/script/value/EntityValue.java | 7 +++++-- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/docs/scarpet/api/Entities.md b/docs/scarpet/api/Entities.md index 36365a78..f2d7bb97 100644 --- a/docs/scarpet/api/Entities.md +++ b/docs/scarpet/api/Entities.md @@ -314,6 +314,13 @@ query(p,'effect','resistance') => null Number indicating remaining entity health, or `null` if not applicable. +### `query(e, 'hunger')` +### `query(e, 'saturation')` +### `query(e, 'exhaustion')` + +Retrieves player hunger related information. For non-players, returns `null`. + + ### `query(e, 'holds', slot?)` Returns triple of short name, stack count, and NBT of item held in `slot`, or `null` if nothing or not applicable. Available options for `slot` are: @@ -513,6 +520,17 @@ Toggles gravity for the entity. Will set entity on fire for `ticks` ticks. Set to 0 to extinguish. +### `modify(e, 'hunger', value)` +### `modify(e, 'saturation', value)` +### `modify(e, 'exhaustion', value)` + +Modifies directly player raw hunger components. Has no effect on non-players + +### `modify(e, 'add_exhaustion', value)` + +adds exhaustion value to the current player exhaustion level - that's the method you probably want to use +to manipulate how much 'food' some action cost. + ## Entity Events There is a number of events that happen to entities that you can attach your own code to in the form of event handlers. diff --git a/src/main/java/carpet/fakes/HungerManagerInterface.java b/src/main/java/carpet/fakes/HungerManagerInterface.java index 028c0bf3..b4df2334 100644 --- a/src/main/java/carpet/fakes/HungerManagerInterface.java +++ b/src/main/java/carpet/fakes/HungerManagerInterface.java @@ -1,5 +1,6 @@ package carpet.fakes; public interface HungerManagerInterface { - float getExhaustion(); + float getExhaustionCM(); + void setExhaustionCM(float exhaust); } diff --git a/src/main/java/carpet/mixins/HungerManagerMixin_scarpetEventsMixin.java b/src/main/java/carpet/mixins/HungerManagerMixin_scarpetEventsMixin.java index c9f341e3..3e6fcdc2 100644 --- a/src/main/java/carpet/mixins/HungerManagerMixin_scarpetEventsMixin.java +++ b/src/main/java/carpet/mixins/HungerManagerMixin_scarpetEventsMixin.java @@ -1,20 +1,20 @@ package carpet.mixins; -import carpet.fakes.EntityInterface; import carpet.fakes.HungerManagerInterface; -import carpet.script.EntityEventsGroup; -import net.minecraft.entity.damage.DamageSource; import net.minecraft.entity.player.HungerManager; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(HungerManager.class) public class HungerManagerMixin_scarpetEventsMixin implements HungerManagerInterface { @Shadow private float exhaustion; - public float getExhaustion(){ + public float getExhaustionCM(){ return exhaustion; } -} \ No newline at end of file + + @Override + public void setExhaustionCM(float exhaust) + { + exhaustion = exhaust; + } +} diff --git a/src/main/java/carpet/script/value/EntityValue.java b/src/main/java/carpet/script/value/EntityValue.java index faec676c..0b053f2c 100644 --- a/src/main/java/carpet/script/value/EntityValue.java +++ b/src/main/java/carpet/script/value/EntityValue.java @@ -299,8 +299,7 @@ public class EntityValue extends Value }); put("exhaustion",(e, a)->{ - if(e instanceof PlayerEntity) - return new NumericValue(((HungerManagerInterface)((PlayerEntity) e).getHungerManager()).getExhaustion()); + if(e instanceof PlayerEntity) return new NumericValue(((HungerManagerInterface)((PlayerEntity) e).getHungerManager()).getExhaustionCM()); return Value.NULL; }); @@ -966,6 +965,10 @@ public class EntityValue extends Value }); put("exhaustion", (e, v)-> { + if(e instanceof PlayerEntity) ((HungerManagerInterface) ((PlayerEntity) e).getHungerManager()).setExhaustionCM(NumericValue.asNumber(v).getFloat()); + }); + + put("add_exhaustion", (e, v)-> { if(e instanceof PlayerEntity) ((PlayerEntity) e).getHungerManager().addExhaustion((int) NumericValue.asNumber(v).getLong()); });