Skip to content

Commit

Permalink
transform EnumBloodSource into a context object
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheaterpaul committed Dec 3, 2023
1 parent eaff090 commit 7aeb15e
Show file tree
Hide file tree
Showing 20 changed files with 194 additions and 107 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package de.teamlapen.vampirism.api.entity.player.vampire;

import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockState;

import java.util.Optional;

/**
* Context for {@link de.teamlapen.vampirism.api.entity.vampire.IVampire#drinkBlood(int, float, boolean, IDrinkBloodContext)}
*/
public interface IDrinkBloodContext {

/**
* @return The entity that is the source of the blood if the blood is obtained from an entity
*/
Optional<LivingEntity> getEntity();

/**
* @return The itemstack that is the source of the blood if the blood is obtained from an item
*/
Optional<ItemStack> getStack();

/**
* @return The blockstate that is the source of the blood if the blood is obtained from a block
*/
Optional<BlockState> getBlockState();

/**
* @return The blockpos that is the source of the blood if the blood is obtained from a block
*/
Optional<BlockPos> getBlockPos();

@Deprecated(forRemoval = true)
static IDrinkBloodContext none() {
return new IDrinkBloodContext() {
@Override
public Optional<LivingEntity> getEntity() {
return Optional.empty();
}

@Override
public Optional<ItemStack> getStack() {
return Optional.empty();
}

@Override
public Optional<BlockState> getBlockState() {
return Optional.empty();
}

@Override
public Optional<BlockPos> getBlockPos() {
return Optional.empty();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import de.teamlapen.vampirism.api.VReference;
import de.teamlapen.vampirism.api.entity.factions.IFaction;
import de.teamlapen.vampirism.api.entity.factions.IFactionEntity;
import de.teamlapen.vampirism.api.entity.player.vampire.EnumBloodSource;
import de.teamlapen.vampirism.api.entity.player.vampire.IDrinkBloodContext;
import net.minecraft.world.level.LevelAccessor;
import org.jetbrains.annotations.NotNull;

Expand All @@ -24,27 +24,27 @@ public interface IVampire extends IFactionEntity {
*
* @param amt In blood food unit, not mB. See {@link de.teamlapen.vampirism.api.VReference#FOOD_TO_FLUID_BLOOD} for conversion
* @param saturationMod Similar to the food saturation modifier
* @param bloodSource The type of blood source used. See {@link de.teamlapen.vampirism.api.entity.player.vampire.EnumBloodSource}
* @param drinkContext The context where the blood is obtained from
*/
default void drinkBlood(int amt, float saturationMod, EnumBloodSource bloodSource) {
drinkBlood(amt, saturationMod, true, bloodSource);
default void drinkBlood(int amt, float saturationMod, IDrinkBloodContext drinkContext) {
drinkBlood(amt, saturationMod, true, drinkContext);
}

/**
* @deprecated Use {@link #drinkBlood(int, float, de.teamlapen.vampirism.api.entity.player.vampire.EnumBloodSource)} instead
* @deprecated Use {@link #drinkBlood(int, float, de.teamlapen.vampirism.api.entity.player.vampire.IDrinkBloodContext)} instead
*/
@Deprecated
@Deprecated(forRemoval = true)
default void drinkBlood(int amt, float saturationMod) {
drinkBlood(amt, saturationMod, true, EnumBloodSource.OTHER);
drinkBlood(amt, saturationMod, true, IDrinkBloodContext.none());
}


/**
* @deprecated Use {@link #drinkBlood(int, float, boolean, de.teamlapen.vampirism.api.entity.player.vampire.EnumBloodSource)} instead
* @deprecated Use {@link #drinkBlood(int, float, boolean, de.teamlapen.vampirism.api.entity.player.vampire.IDrinkBloodContext)} instead
*/
@Deprecated
@Deprecated(forRemoval = true)
default void drinkBlood(int amt, float saturationMod, boolean useRemaining) {
drinkBlood(amt, saturationMod, useRemaining, EnumBloodSource.OTHER);
drinkBlood(amt, saturationMod, useRemaining, IDrinkBloodContext.none());
}

/**
Expand All @@ -53,8 +53,9 @@ default void drinkBlood(int amt, float saturationMod, boolean useRemaining) {
*
* @param amt In blood food unit, not mB. See {@link de.teamlapen.vampirism.api.VReference#FOOD_TO_FLUID_BLOOD} for conversion
* @param saturationMod Similar to the food saturation modifier
* @param drinkContext The context where the blood is obtained from
*/
void drinkBlood(int amt, float saturationMod, boolean useRemaining, EnumBloodSource bloodSource);
void drinkBlood(int amt, float saturationMod, boolean useRemaining, IDrinkBloodContext drinkContext);

@NotNull
@Override
Expand Down
38 changes: 9 additions & 29 deletions src/api/java/de/teamlapen/vampirism/api/event/BloodDrinkEvent.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package de.teamlapen.vampirism.api.event;

import de.teamlapen.vampirism.api.entity.player.vampire.EnumBloodSource;
import de.teamlapen.vampirism.api.entity.player.vampire.IDrinkBloodContext;
import de.teamlapen.vampirism.api.entity.player.vampire.IVampirePlayer;
import de.teamlapen.vampirism.api.entity.vampire.IVampire;
import net.minecraft.world.entity.LivingEntity;
import net.minecraftforge.eventbus.api.Event;
import org.jetbrains.annotations.NotNull;

Expand All @@ -14,10 +13,10 @@ public abstract class BloodDrinkEvent extends Event {
private final IVampire vampire;
private int amount;
private float saturation;
private final EnumBloodSource bloodSource;
private final IDrinkBloodContext bloodSource;


private BloodDrinkEvent(@NotNull IVampire vampire, int amount, float saturation, EnumBloodSource bloodSource) {
private BloodDrinkEvent(@NotNull IVampire vampire, int amount, float saturation, IDrinkBloodContext bloodSource) {
this.vampire = vampire;
this.amount = amount;
this.saturation = saturation;
Expand Down Expand Up @@ -46,7 +45,7 @@ public float getSaturation() {
/**
* @return The type of blood source that the blood was obtained from.
*/
public EnumBloodSource getBloodSource() {
public IDrinkBloodContext getBloodSource() {
return this.bloodSource;
}

Expand All @@ -65,38 +64,18 @@ public void setSaturationModifier(float saturation) {
this.saturation = saturation;
}

/**
* Posted whenever a Vampire Player feeds by biting another mob.
* This does not change the amount drained from the entity, meaning it only changes the amount of blood the vampire receives from biting.
*/
public static class BiteFeedEvent extends BloodDrinkEvent {
private final LivingEntity target;
public BiteFeedEvent(@NotNull IVampirePlayer player, LivingEntity target, int amount, float saturation) {
super(player,amount, saturation, EnumBloodSource.BITE_FEED);
this.target = target;
}

/**
* @return The LivingEntity that is being bitten
*/
@NotNull
public LivingEntity getTarget() {
return this.target;
}
}

/**
* Posted whenever a Vampire Player drinks blood, such as when drinking blood from a bottle, eating a human heart or biting another mob
*/
public static class PlayerDrinkBloodEvent extends BloodDrinkEvent {
private boolean useRemaining;

public PlayerDrinkBloodEvent(@NotNull IVampirePlayer player, int amount, float saturation, boolean useRemaining, EnumBloodSource bloodSource) {
public PlayerDrinkBloodEvent(@NotNull IVampirePlayer player, int amount, float saturation, boolean useRemaining, IDrinkBloodContext bloodSource) {
super(player, amount, saturation, bloodSource);
this.useRemaining = useRemaining;
}
/**
* @return Whether the remaining blood should be used, see {@link de.teamlapen.vampirism.api.entity.vampire.IVampire#drinkBlood(int, float, boolean, de.teamlapen.vampirism.api.entity.player.vampire.EnumBloodSource)}.
* @return Whether the remaining blood should be used, see {@link de.teamlapen.vampirism.api.entity.vampire.IVampire#drinkBlood(int, float, boolean, de.teamlapen.vampirism.api.entity.player.vampire.IDrinkBloodContext)}.
*/
public boolean useRemaining() {
return this.useRemaining;
Expand All @@ -109,19 +88,20 @@ public void setUseRemaining(boolean useRemaining) {
this.useRemaining = useRemaining;
}
}

/**
* Posted whenever any non-player vampire entity drinks any blood at all.
*/
public static class EntityDrinkBloodEvent extends BloodDrinkEvent {

private boolean useRemaining;

public EntityDrinkBloodEvent(@NotNull IVampire vampire, int amount, float saturation, boolean useRemaining, EnumBloodSource bloodSource) {
public EntityDrinkBloodEvent(@NotNull IVampire vampire, int amount, float saturation, boolean useRemaining, IDrinkBloodContext bloodSource) {
super(vampire, amount, saturation, bloodSource);
this.useRemaining = useRemaining;
}
/**
* @return Whether the remaining blood should be used, see {@link de.teamlapen.vampirism.api.entity.vampire.IVampire#drinkBlood(int, float, boolean, de.teamlapen.vampirism.api.entity.player.vampire.EnumBloodSource)}.
* @return Whether the remaining blood should be used, see {@link de.teamlapen.vampirism.api.entity.vampire.IVampire#drinkBlood(int, float, boolean, de.teamlapen.vampirism.api.entity.player.vampire.IDrinkBloodContext)}.
*/
public boolean useRemaining() {
return this.useRemaining;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import de.teamlapen.vampirism.VampirismMod;
import de.teamlapen.vampirism.advancements.critereon.VampireActionCriterionTrigger;
import de.teamlapen.vampirism.api.VReference;
import de.teamlapen.vampirism.api.entity.player.vampire.EnumBloodSource;
import de.teamlapen.vampirism.blocks.AltarPillarBlock;
import de.teamlapen.vampirism.blocks.AltarTipBlock;
import de.teamlapen.vampirism.core.*;
import de.teamlapen.vampirism.entity.factions.FactionPlayerHandler;
import de.teamlapen.vampirism.entity.player.VampirismPlayerAttributes;
import de.teamlapen.vampirism.entity.player.vampire.VampireLeveling;
import de.teamlapen.vampirism.entity.player.vampire.VampirePlayer;
import de.teamlapen.vampirism.entity.vampire.DrinkBloodContext;
import de.teamlapen.vampirism.inventory.AltarInfusionMenu;
import de.teamlapen.vampirism.items.PureBloodItem;
import de.teamlapen.vampirism.particle.FlyingBloodParticleOptions;
Expand Down Expand Up @@ -288,7 +288,7 @@ private void tickRitual() {
return;
}
handler.ifPresent(h -> h.setFactionLevel(VReference.VAMPIRE_FACTION, h.getCurrentLevel(VReference.VAMPIRE_FACTION) + 1));
VampirePlayer.getOpt(player).ifPresent(v -> v.drinkBlood(Integer.MAX_VALUE, 0, false, EnumBloodSource.OTHER));
VampirePlayer.getOpt(player).ifPresent(v -> v.drinkBlood(Integer.MAX_VALUE, 0, false, DrinkBloodContext.none()));
if (player instanceof ServerPlayer serverPlayer) {
ModAdvancements.TRIGGER_VAMPIRE_ACTION.trigger(serverPlayer, VampireActionCriterionTrigger.Action.PERFORM_RITUAL_INFUSION);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import de.teamlapen.lib.lib.util.FluidTankWithListener;
import de.teamlapen.vampirism.api.VReference;
import de.teamlapen.vampirism.api.entity.player.vampire.EnumBloodSource;
import de.teamlapen.vampirism.core.ModFluids;
import de.teamlapen.vampirism.core.ModParticles;
import de.teamlapen.vampirism.core.ModTiles;
import de.teamlapen.vampirism.entity.factions.FactionPlayerHandler;
import de.teamlapen.vampirism.entity.player.VampirismPlayerAttributes;
import de.teamlapen.vampirism.entity.player.vampire.VampireLeveling;
import de.teamlapen.vampirism.entity.player.vampire.VampirePlayer;
import de.teamlapen.vampirism.entity.vampire.DrinkBloodContext;
import de.teamlapen.vampirism.items.BloodBottleFluidHandler;
import de.teamlapen.vampirism.particle.FlyingBloodEntityParticleOptions;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -157,7 +157,7 @@ public static void serverTick(@NotNull Level level, @NotNull BlockPos pos, Block
((InternalTank) blockEntity.tank).doDrain(blood, IFluidHandler.FluidAction.EXECUTE);
blockEntity.ritualPlayer.addEffect(new MobEffectInstance(MobEffects.REGENERATION, blockEntity.targetLevel * 10 * 20));
FactionPlayerHandler.getOpt(blockEntity.ritualPlayer).ifPresent(h -> h.setFactionLevel(VReference.VAMPIRE_FACTION, blockEntity.targetLevel));
VampirePlayer.getOpt(blockEntity.ritualPlayer).ifPresent(v -> v.drinkBlood(Integer.MAX_VALUE, 0, false, EnumBloodSource.OTHER));
VampirePlayer.getOpt(blockEntity.ritualPlayer).ifPresent(v -> v.drinkBlood(Integer.MAX_VALUE, 0, false, DrinkBloodContext.none()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import de.teamlapen.lib.lib.util.BasicCommand;
import de.teamlapen.vampirism.api.entity.player.vampire.EnumBloodSource;
import de.teamlapen.vampirism.entity.player.FactionBasePlayer;
import de.teamlapen.vampirism.entity.player.vampire.VampirePlayer;
import de.teamlapen.vampirism.entity.vampire.DrinkBloodContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
Expand Down Expand Up @@ -39,7 +39,7 @@ public class BloodBarCommand extends BasicCommand {
private static int setBloodBar(int amount, @NotNull Collection<ServerPlayer> player) {
player.stream().map(VampirePlayer::getOpt).filter(player1 -> player1.map(FactionBasePlayer::getLevel).orElse(0) > 0).forEach(player1 -> player1.ifPresent(vampire -> {
vampire.useBlood(Integer.MAX_VALUE, true);
vampire.drinkBlood(amount, 0, false, EnumBloodSource.OTHER);
vampire.drinkBlood(amount, 0, false, DrinkBloodContext.none());
}));
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package de.teamlapen.vampirism.entity.ai.goals;

import de.teamlapen.vampirism.api.entity.IExtendedCreatureVampirism;
import de.teamlapen.vampirism.api.entity.player.vampire.EnumBloodSource;
import de.teamlapen.vampirism.api.entity.vampire.IVampireMob;
import de.teamlapen.vampirism.core.ModSounds;
import de.teamlapen.vampirism.entity.ExtendedCreature;
import de.teamlapen.vampirism.entity.vampire.DrinkBloodContext;
import net.minecraft.commands.arguments.EntityAnchorArgument;
import net.minecraft.world.entity.EntitySelector;
import net.minecraft.world.entity.Mob;
Expand Down Expand Up @@ -87,7 +87,7 @@ public void tick() {
if (canFeed(creature)) {
int amount = creature.onBite(vampire);
vampire.playSound(ModSounds.VAMPIRE_BITE.get(), 1, 1);
vampire.drinkBlood(amount, creature.getBloodSaturation(), EnumBloodSource.BITE_FEED);
vampire.drinkBlood(amount, creature.getBloodSaturation(), new DrinkBloodContext(e));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
import de.teamlapen.vampirism.REFERENCE;
import de.teamlapen.vampirism.api.EnumStrength;
import de.teamlapen.vampirism.api.entity.convertible.ICurableConvertedCreature;
import de.teamlapen.vampirism.api.entity.player.vampire.EnumBloodSource;
import de.teamlapen.vampirism.api.entity.player.vampire.IBloodStats;
import de.teamlapen.vampirism.api.entity.player.vampire.IDrinkBloodContext;
import de.teamlapen.vampirism.api.event.BloodDrinkEvent;
import de.teamlapen.vampirism.blockentity.TotemBlockEntity;
import de.teamlapen.vampirism.core.ModAdvancements;
import de.teamlapen.vampirism.core.ModAi;
import de.teamlapen.vampirism.core.ModVillage;
import de.teamlapen.vampirism.entity.VampirismVillagerEntity;
import de.teamlapen.vampirism.entity.player.vampire.VampirePlayer;
import de.teamlapen.vampirism.entity.vampire.DrinkBloodContext;
import de.teamlapen.vampirism.entity.villager.Trades;
import de.teamlapen.vampirism.util.*;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -148,9 +149,9 @@ public boolean doesResistGarlic(EnumStrength strength) {

@Override
public boolean doHurtTarget(@NotNull Entity entity) {
if (!level().isClientSide && wantsBlood() && entity instanceof Player && !Helper.isHunter(entity) && !UtilLib.canReallySee((LivingEntity) entity, this, true)) {
int amt = VampirePlayer.getOpt((Player) entity).map(vampire -> vampire.onBite(this)).orElse(0);
drinkBlood(amt, IBloodStats.MEDIUM_SATURATION, EnumBloodSource.BITE_FEED);
if (!level().isClientSide && wantsBlood() && entity instanceof Player player && !Helper.isHunter(player) && !UtilLib.canReallySee(player, this, true)) {
int amt = VampirePlayer.getOpt(player).map(vampire -> vampire.onBite(this)).orElse(0);
drinkBlood(amt, IBloodStats.MEDIUM_SATURATION, new DrinkBloodContext(player));
return true;
}
return super.doHurtTarget(entity);
Expand All @@ -167,8 +168,8 @@ public boolean doHurtTarget(@NotNull Entity entity) {
}

@Override
public void drinkBlood(int amt, float saturationMod, boolean useRemaining, EnumBloodSource bloodSource) {
BloodDrinkEvent.@NotNull EntityDrinkBloodEvent event = VampirismEventFactory.fireVampireDrinkBlood(this, amt, saturationMod, useRemaining, bloodSource);
public void drinkBlood(int amt, float saturationMod, boolean useRemaining, IDrinkBloodContext drinkContext) {
BloodDrinkEvent.@NotNull EntityDrinkBloodEvent event = VampirismEventFactory.fireVampireDrinkBlood(this, amt, saturationMod, useRemaining, drinkContext);
this.addEffect(new MobEffectInstance(MobEffects.REGENERATION, event.getAmount() * 20));
bloodTimer = -1200 - random.nextInt(1200);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import de.teamlapen.vampirism.api.VReference;
import de.teamlapen.vampirism.api.VampirismAPI;
import de.teamlapen.vampirism.api.entity.convertible.ICurableConvertedCreature;
import de.teamlapen.vampirism.api.entity.player.vampire.EnumBloodSource;
import de.teamlapen.vampirism.api.entity.player.vampire.IDrinkBloodContext;
import de.teamlapen.vampirism.api.event.BloodDrinkEvent;
import de.teamlapen.vampirism.api.items.IVampireFinisher;
import de.teamlapen.vampirism.core.ModAttributes;
Expand Down Expand Up @@ -90,8 +90,8 @@ default InteractionResult mobInteractC(@NotNull Player player, @NotNull Interact
}

@Override
default void drinkBlood(int amt, float saturationMod, boolean useRemaining, EnumBloodSource bloodSource) {
BloodDrinkEvent.@NotNull EntityDrinkBloodEvent event = VampirismEventFactory.fireVampireDrinkBlood(this, amt, saturationMod, useRemaining, bloodSource);
default void drinkBlood(int amt, float saturationMod, boolean useRemaining, IDrinkBloodContext drinkContext) {
BloodDrinkEvent.@NotNull EntityDrinkBloodEvent event = VampirismEventFactory.fireVampireDrinkBlood(this, amt, saturationMod, useRemaining, drinkContext);
((PathfinderMob) this).addEffect(new MobEffectInstance(MobEffects.REGENERATION, event.getAmount() * 20));
}

Expand Down
Loading

0 comments on commit 7aeb15e

Please sign in to comment.