Search in sources :

Example 1 with IHungerManager

use of de.siphalor.spiceoffabric.util.IHungerManager in project spiceoffabric by Siphalor.

the class MixinCakeBlock method eat.

@Redirect(method = "tryEat", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/HungerManager;add(IF)V"))
private static void eat(HungerManager hungerManager, int baseHunger, float baseSaturation, WorldAccess world, BlockPos pos, BlockState state, PlayerEntity player) {
    ItemStack stack = new ItemStack(Items.CAKE);
    FoodHistory foodHistory = ((IHungerManager) hungerManager).spiceOfFabric_getFoodHistory();
    Config.setHungerExpressionValues(foodHistory.getTimesEaten(stack), baseHunger, baseSaturation, stack.getMaxUseTime());
    hungerManager.add(Config.getHungerValue(), Config.getSaturationValue());
    if (!world.isClient() && player instanceof ServerPlayerEntity) {
        SpiceOfFabric.onEaten((ServerPlayerEntity) player, foodHistory, stack);
    }
}
Also used : IHungerManager(de.siphalor.spiceoffabric.util.IHungerManager) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) ItemStack(net.minecraft.item.ItemStack) FoodHistory(de.siphalor.spiceoffabric.foodhistory.FoodHistory) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 2 with IHungerManager

use of de.siphalor.spiceoffabric.util.IHungerManager in project spiceoffabric by Siphalor.

the class SoFClient method onInitializeClient.

@Override
public void onInitializeClient() {
    ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> {
        if (lastSyncPacket != null) {
            ((IHungerManager) client.player.getHungerManager()).spiceOfFabric_getFoodHistory().read(lastSyncPacket);
            lastSyncPacket.release();
            lastSyncPacket = null;
        }
    });
    ClientPlayNetworking.registerGlobalReceiver(SpiceOfFabric.SYNC_FOOD_HISTORY_S2C_PACKET, (client, handler, buf, responseSender) -> {
        if (client.player != null && client.player.getHungerManager() != null) {
            ((IHungerManager) client.player.getHungerManager()).spiceOfFabric_getFoodHistory().read(buf);
        } else {
            lastSyncPacket = new PacketByteBuf(buf.copy());
            lastSyncPacket.retain();
        }
    });
    ClientPlayNetworking.registerGlobalReceiver(SpiceOfFabric.ADD_FOOD_S2C_PACKET, (client, handler, buf, responseSender) -> ((IHungerManager) client.player.getHungerManager()).spiceOfFabric_getFoodHistory().addFood(FoodHistoryEntry.from(buf)));
    ClientPlayNetworking.registerGlobalReceiver(SpiceOfFabric.CLEAR_FOODS_S2C_PACKET, (client, handler, buf, responseSender) -> ((IHungerManager) client.player.getHungerManager()).spiceOfFabric_clearHistory());
}
Also used : IHungerManager(de.siphalor.spiceoffabric.util.IHungerManager) PacketByteBuf(net.minecraft.network.PacketByteBuf)

Example 3 with IHungerManager

use of de.siphalor.spiceoffabric.util.IHungerManager in project spiceoffabric by Siphalor.

the class AppleSkinPlugin method registerEvents.

@Override
public void registerEvents() {
    FoodValuesEvent.EVENT.register(event -> {
        if (event.player != null) {
            Config.setHungerExpressionValues(((IHungerManager) event.player.getHungerManager()).spiceOfFabric_getFoodHistory().getTimesEaten(event.itemStack), event.modifiedFoodValues.hunger, event.modifiedFoodValues.saturationModifier, event.itemStack.getMaxUseTime());
            event.modifiedFoodValues = new FoodValues(Config.getHungerValue(), Config.getSaturationValue());
        }
    });
}
Also used : IHungerManager(de.siphalor.spiceoffabric.util.IHungerManager) FoodValues(squeek.appleskin.api.food.FoodValues)

Example 4 with IHungerManager

use of de.siphalor.spiceoffabric.util.IHungerManager in project spiceoffabric by Siphalor.

the class SpiceOfFabric method updateMaxHealth.

public static void updateMaxHealth(ServerPlayerEntity player, boolean sync, boolean announce) {
    EntityAttributeInstance maxHealthAttr = player.getAttributeInstance(EntityAttributes.GENERIC_MAX_HEALTH);
    double oldValue = maxHealthAttr.getValue();
    maxHealthAttr.removeModifier(PLAYER_HEALTH_MODIFIER_UUID);
    FoodHistory foodHistory = ((IHungerManager) player.getHungerManager()).spiceOfFabric_getFoodHistory();
    maxHealthAttr.addPersistentModifier(createHealthModifier(foodHistory.getCarrotHealthOffset(player)));
    if (sync) {
        player.networkHandler.sendPacket(new EntityAttributesS2CPacket(player.getId(), Collections.singleton(maxHealthAttr)));
        player.networkHandler.sendPacket(new HealthUpdateS2CPacket(player.getHealth(), player.getHungerManager().getFoodLevel(), player.getHungerManager().getSaturationLevel()));
    }
    if (announce && maxHealthAttr.getValue() > oldValue) {
        player.world.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.ENTITY_PLAYER_LEVELUP, SoundCategory.PLAYERS, 1F, 1F);
    }
}
Also used : IHungerManager(de.siphalor.spiceoffabric.util.IHungerManager) HealthUpdateS2CPacket(net.minecraft.network.packet.s2c.play.HealthUpdateS2CPacket) EntityAttributeInstance(net.minecraft.entity.attribute.EntityAttributeInstance) FoodHistory(de.siphalor.spiceoffabric.foodhistory.FoodHistory) EntityAttributesS2CPacket(net.minecraft.network.packet.s2c.play.EntityAttributesS2CPacket)

Example 5 with IHungerManager

use of de.siphalor.spiceoffabric.util.IHungerManager in project spiceoffabric by Siphalor.

the class MixinServerPlayerEntity method onPlayerCopied.

@Inject(method = "copyFrom", at = @At("RETURN"))
public void onPlayerCopied(ServerPlayerEntity reference, boolean exact, CallbackInfo callbackInfo) {
    if (!exact) {
        Pair<Double, Double> respawnHunger = Config.getRespawnHunger(reference.getHungerManager().getFoodLevel(), reference.getHungerManager().getSaturationLevel());
        hungerManager.setFoodLevel((int) Math.max(respawnHunger.getFirst(), reference.getHungerManager().getFoodLevel()));
        ((IHungerManager) hungerManager).spiceOfFabric_setSaturationLevel((float) (double) respawnHunger.getSecond());
        FoodHistory foodHistory = ((IHungerManager) reference.getHungerManager()).spiceOfFabric_getFoodHistory();
        if (Config.respawn.resetHistory) {
            foodHistory.resetHistory();
        }
        if (Config.respawn.resetCarrotMode) {
            foodHistory.resetCarrotHistory();
            EntityAttributeInstance maxHealthAttr = getAttributeInstance(EntityAttributes.GENERIC_MAX_HEALTH);
            maxHealthAttr.removeModifier(SpiceOfFabric.PLAYER_HEALTH_MODIFIER_UUID);
            maxHealthAttr.addPersistentModifier(SpiceOfFabric.createHealthModifier(foodHistory.getCarrotHealthOffset(((ServerPlayerEntity) (Object) this))));
        }
        ((IHungerManager) hungerManager).spiceOfFabric_setFoodHistory(foodHistory);
        SpiceOfFabric.syncFoodHistory((ServerPlayerEntity) (Object) this);
    }
}
Also used : IHungerManager(de.siphalor.spiceoffabric.util.IHungerManager) IServerPlayerEntity(de.siphalor.spiceoffabric.util.IServerPlayerEntity) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) FoodHistory(de.siphalor.spiceoffabric.foodhistory.FoodHistory) EntityAttributeInstance(net.minecraft.entity.attribute.EntityAttributeInstance) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

IHungerManager (de.siphalor.spiceoffabric.util.IHungerManager)7 FoodHistory (de.siphalor.spiceoffabric.foodhistory.FoodHistory)4 ServerPlayerEntity (net.minecraft.server.network.ServerPlayerEntity)3 EntityAttributeInstance (net.minecraft.entity.attribute.EntityAttributeInstance)2 PacketByteBuf (net.minecraft.network.PacketByteBuf)2 Inject (org.spongepowered.asm.mixin.injection.Inject)2 CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)1 IServerPlayerEntity (de.siphalor.spiceoffabric.util.IServerPlayerEntity)1 ItemStack (net.minecraft.item.ItemStack)1 NbtCompound (net.minecraft.nbt.NbtCompound)1 EntityAttributesS2CPacket (net.minecraft.network.packet.s2c.play.EntityAttributesS2CPacket)1 HealthUpdateS2CPacket (net.minecraft.network.packet.s2c.play.HealthUpdateS2CPacket)1 LiteralText (net.minecraft.text.LiteralText)1 TranslatableText (net.minecraft.text.TranslatableText)1 Redirect (org.spongepowered.asm.mixin.injection.Redirect)1 FoodValues (squeek.appleskin.api.food.FoodValues)1