Skip to content

Commit

Permalink
Fix time based happiness working as expected (#10197)
Browse files Browse the repository at this point in the history
Fix time based happiness working as expected [1.21]
  • Loading branch information
Thodor12 authored Sep 15, 2024
1 parent bf99ccb commit 12dcf26
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@ public interface ICitizenHappinessHandler
* Read the handler from NBT.
*
* @param compound the compound to read it from.
* @param persist whether we're reading from persisted data or from networking.
*/
void read(@NotNull final HolderLookup.Provider provider, CompoundTag compound);
void read(@NotNull final HolderLookup.Provider provider, CompoundTag compound, final boolean persist);

/**
* Write the handler to NBT.
*
* @param compound the compound to write it to.
* @param persist whether we're reading from persisted data or from networking.
*/
void write(@NotNull final HolderLookup.Provider provider, CompoundTag compound);
void write(@NotNull final HolderLookup.Provider provider, CompoundTag compound, final boolean persist);

/**
* Get a list of all modifiers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public String getId()
}

@Override
public void read(@NotNull final HolderLookup.Provider provider, final CompoundTag compoundNBT)
public void read(@NotNull final HolderLookup.Provider provider, final CompoundTag compoundNBT, final boolean persist)
{
this.id = compoundNBT.getString(TAG_ID);
this.weight = compoundNBT.getDouble(TAG_WEIGHT);
Expand All @@ -79,7 +79,7 @@ public void read(@NotNull final HolderLookup.Provider provider, final CompoundTa
}

@Override
public void write(@NotNull final HolderLookup.Provider provider, final CompoundTag compoundNBT)
public void write(@NotNull final HolderLookup.Provider provider, final CompoundTag compoundNBT, final boolean persist)
{
compoundNBT.putString(TAG_ID, this.id);
compoundNBT.putDouble(TAG_WEIGHT, this.weight);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,13 @@ public final class ExpirationBasedHappinessModifier extends AbstractHappinessMod
/**
* The number of passed days.
*/
private int days = 0;
private int days;

/**
* Period of time this modifier applies.
*/
private int period;

/**
* If this should give a penalty if not active.
*/
private boolean inverted;

/**
* Create an instance of the happiness modifier.
*
Expand All @@ -40,24 +35,10 @@ public final class ExpirationBasedHappinessModifier extends AbstractHappinessMod
public ExpirationBasedHappinessModifier(final String id, final double weight, final IHappinessSupplierWrapper supplier, final int period)
{
super(id, weight, supplier);
this.days = period;
this.period = period;
}

/**
* Create an instance of the happiness modifier.
*
* @param id its string id.
* @param weight its weight.
* @param period the period.
* @param supplier the supplier to get the factor.
* @param inverted if inverted.
*/
public ExpirationBasedHappinessModifier(final String id, final double weight, final IHappinessSupplierWrapper supplier, final int period, final boolean inverted)
{
this(id, weight, supplier, period);
this.inverted = inverted;
}

/**
* Create an instance of the happiness modifier.
*/
Expand All @@ -69,22 +50,11 @@ public ExpirationBasedHappinessModifier()
@Override
public double getFactor(final ICitizenData data)
{
if (inverted)
if (days > 0 && days <= period)
{
if (days <= period)
{
return 1.0;
}
return super.getFactor(data);
}
else
{
if (days < period)
{
return super.getFactor(data);
}
return 1.0;
}
return 1.0;
}

@Override
Expand All @@ -109,21 +79,19 @@ public int getDays()
}

@Override
public void read(@NotNull final HolderLookup.Provider provider, final CompoundTag compoundNBT)
public void read(@NotNull final HolderLookup.Provider provider, final CompoundTag compoundNBT, final boolean persist)
{
super.read(provider, compoundNBT);
super.read(provider, compoundNBT, persist);
this.days = compoundNBT.getInt(TAG_DAY);
this.inverted = compoundNBT.getBoolean(TAG_INVERTED);
this.period = compoundNBT.getInt(TAG_PERIOD);
}

@Override
public void write(@NotNull final HolderLookup.Provider provider, final CompoundTag compoundNBT)
public void write(@NotNull final HolderLookup.Provider provider, final CompoundTag compoundNBT, final boolean persist)
{
super.write(provider, compoundNBT);
super.write(provider, compoundNBT, persist);
compoundNBT.putString(NbtTagConstants.TAG_MODIFIER_TYPE, HappinessRegistry.EXPIRATION_MODIFIER.toString());
compoundNBT.putInt(TAG_DAY, days);
compoundNBT.putBoolean(TAG_INVERTED, inverted);
compoundNBT.putInt(TAG_PERIOD, period);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ public IHappinessModifier create()
* Static getter to load a happiness modifier from a compound.
*
* @param compound the compound to load it from.
* @param persist whether we're reading from persisted data or from networking.
* @return the modifier instance.
*/
public static IHappinessModifier loadFrom(@NotNull final HolderLookup.Provider provider, @NotNull final CompoundTag compound)
public static IHappinessModifier loadFrom(@NotNull final HolderLookup.Provider provider, @NotNull final CompoundTag compound, final boolean persist)
{
final ResourceLocation modifierType = compound.contains(NbtTagConstants.TAG_MODIFIER_TYPE)
? ResourceLocation.parse(compound.getString(NbtTagConstants.TAG_MODIFIER_TYPE))
Expand All @@ -80,7 +81,7 @@ public static IHappinessModifier loadFrom(@NotNull final HolderLookup.Provider p
{
try
{
modifier.read(provider, compound);
modifier.read(provider, compound, persist);
}
catch (final RuntimeException ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ public interface IHappinessModifier
* Read the modifier from nbt.
*
* @param compoundNBT the compound to read it from.
* @param persist whether we're reading from persisted data or from networking.
*/
void read(@NotNull final HolderLookup.Provider provider, final CompoundTag compoundNBT);
void read(@NotNull final HolderLookup.Provider provider, final CompoundTag compoundNBT, final boolean persist);

/**
* Write it to NBT.
*
* @param compoundNBT the compound to write it to.
* @param persist whether we're reading from persisted data or from networking.
*/
void write(@NotNull final HolderLookup.Provider provider, final CompoundTag compoundNBT);
void write(@NotNull final HolderLookup.Provider provider, final CompoundTag compoundNBT, final boolean persist);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public StaticHappinessModifier()
}

@Override
public void write(@NotNull final HolderLookup.Provider provider, final CompoundTag compoundNBT)
public void write(@NotNull final HolderLookup.Provider provider, final CompoundTag compoundNBT, final boolean persist)
{
super.write(provider, compoundNBT);
super.write(provider, compoundNBT, persist);
compoundNBT.putString(NbtTagConstants.TAG_MODIFIER_TYPE, HappinessRegistry.STATIC_MODIFIER.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiPredicate;

import static com.minecolonies.api.util.constant.NbtTagConstants.*;

Expand All @@ -19,10 +20,15 @@
*/
public final class TimeBasedHappinessModifier extends AbstractHappinessModifier implements ITimeBasedHappinessModifier
{
/**
* A predicate to check whether the current day should roll over or reset.
*/
private BiPredicate<TimeBasedHappinessModifier, ICitizenData> dayRollOverPredicate;

/**
* The time based factors.
*/
private Tuple<Integer, Double>[] timeBasedFactor;
private List<Tuple<Integer, Double>> timeBasedFactor = new ArrayList<>();

/**
* The number of passed days.
Expand All @@ -37,10 +43,32 @@ public final class TimeBasedHappinessModifier extends AbstractHappinessModifier
* @param supplier the supplier to get the factor.
* @param timeBasedFactor tuples about the boost/buff factor over time.
*/
public TimeBasedHappinessModifier(final String id, final double weight, final IHappinessSupplierWrapper supplier, final Tuple<Integer, Double>...timeBasedFactor)
@SafeVarargs
public TimeBasedHappinessModifier(final String id, final double weight, final IHappinessSupplierWrapper supplier, final Tuple<Integer, Double>... timeBasedFactor)
{
this(id, weight, supplier, (modifier, data) -> modifier.getFactor(data) < 1, timeBasedFactor);
}

/**
* Create an instance of the happiness modifier.
*
* @param id its string id.
* @param weight its weight.
* @param supplier the supplier to get the factor.
* @param timeBasedFactor tuples about the boost/buff factor over time.
* @param dayRollOverPredicate a predicate to check whether the current day should roll over or reset.
*/
@SafeVarargs
public TimeBasedHappinessModifier(
final String id,
final double weight,
final IHappinessSupplierWrapper supplier,
final BiPredicate<TimeBasedHappinessModifier, ICitizenData> dayRollOverPredicate,
final Tuple<Integer, Double>... timeBasedFactor)
{
super(id, weight, supplier);
this.timeBasedFactor = timeBasedFactor;
this.dayRollOverPredicate = dayRollOverPredicate;
this.timeBasedFactor = List.of(timeBasedFactor);
}

/**
Expand All @@ -61,7 +89,7 @@ public double getFactor(final ICitizenData citizenData)
{
for (final Tuple<Integer, Double> tuple : timeBasedFactor)
{
if (this.days > tuple.getA())
if (this.days >= tuple.getA())
{
factor = baseFactor * tuple.getB();
}
Expand All @@ -85,7 +113,7 @@ public int getDays()
@Override
public void dayEnd(final ICitizenData data)
{
if (getFactor(data) < 1)
if (dayRollOverPredicate.test(this, data))
{
days++;
}
Expand All @@ -96,34 +124,40 @@ public void dayEnd(final ICitizenData data)
}

@Override
public void read(@NotNull final HolderLookup.Provider provider, final CompoundTag compoundNBT)
public void read(@NotNull final HolderLookup.Provider provider, final CompoundTag compoundNBT, final boolean persist)
{
super.read(provider, compoundNBT);
super.read(provider, compoundNBT, persist);
this.days = compoundNBT.getInt(TAG_DAY);
final ListTag listTag = compoundNBT.getList(TAG_LIST, Constants.TAG_COMPOUND);
final List<Tuple<Integer, Double>> list = new ArrayList<>();
for (int i = 0; i < listTag.size(); i++)
if (!persist)
{
final CompoundTag entryTag = listTag.getCompound(i);
list.add(new Tuple<>(entryTag.getInt(TAG_DAY), entryTag.getDouble(TAG_VALUE)));
final ListTag listTag = compoundNBT.getList(TAG_LIST, Constants.TAG_COMPOUND);
final List<Tuple<Integer, Double>> list = new ArrayList<>();
for (int i = 0; i < listTag.size(); i++)
{
final CompoundTag entryTag = listTag.getCompound(i);
list.add(new Tuple<>(entryTag.getInt(TAG_DAY), entryTag.getDouble(TAG_VALUE)));
}
this.timeBasedFactor = list;
}
this.timeBasedFactor = list.toArray(new Tuple[0]);
}

@Override
public void write(@NotNull final HolderLookup.Provider provider, final CompoundTag compoundNBT)
public void write(@NotNull final HolderLookup.Provider provider, final CompoundTag compoundNBT, final boolean persist)
{
super.write(provider, compoundNBT);
super.write(provider, compoundNBT, persist);
compoundNBT.putString(NbtTagConstants.TAG_MODIFIER_TYPE, HappinessRegistry.TIME_PERIOD_MODIFIER.toString());

compoundNBT.putInt(TAG_DAY, days);
final ListTag listTag = new ListTag();
for (final Tuple<Integer, Double> entry : timeBasedFactor)
if (!persist)
{
final CompoundTag listEntry = new CompoundTag();
listEntry.putInt(TAG_DAY, entry.getA());
listEntry.putDouble(TAG_VALUE, entry.getB());
final ListTag listTag = new ListTag();
for (final Tuple<Integer, Double> entry : timeBasedFactor)
{
final CompoundTag listEntry = new CompoundTag();
listEntry.putInt(TAG_DAY, entry.getA());
listEntry.putDouble(TAG_VALUE, entry.getB());
listTag.add(listEntry);
}
compoundNBT.put(TAG_LIST, listTag);
}
compoundNBT.put(TAG_LIST, listTag);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public final class NbtTagConstants
public static final String TAG_OFFHAND_HELD_ITEM_SLOT = "OffhandHeldItemSlot";
public static final String TAG_STATUS = "status";
public static final String TAG_DAY = "day";
public static final String TAG_INVERTED = "inverted";
public static final String TAG_PERIOD = "period";
public static final String TAG_IS_BUILT = "isBuilt";
public static final String TAG_CUSTOM_NAME = "customName";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private ModHappinessFactorTypeInitializer()
HappinessRegistry.healthFunction = DEFERRED_REGISTER_HAPPINESS_FUNCTION.register(HEALTH_FUNCTION.getPath(), () -> new HappinessFunctionEntry(data -> data.getEntity().isPresent() ? (data.getEntity().get().getCitizenDiseaseHandler().isSick() ? 0.5 : 1.0) : 1.0));
HappinessRegistry.idleatjobFunction = DEFERRED_REGISTER_HAPPINESS_FUNCTION.register(IDLEATJOB_FUNCTION.getPath(), () -> new HappinessFunctionEntry(data -> data.isIdleAtJob() ? 0.5 : 1.0));

HappinessRegistry.sleptTonightFunction = DEFERRED_REGISTER_HAPPINESS_FUNCTION.register(SLEPTTONIGHT_FUNCTION.getPath(), () -> new HappinessFunctionEntry(data -> data.getJob() instanceof AbstractJobGuard ? 1 : 0.0));
HappinessRegistry.foodFunction = DEFERRED_REGISTER_HAPPINESS_FUNCTION.register(FOOD_FUNCTION.getPath(), () -> new HappinessFunctionEntry(data -> (data.getHomeBuilding() == null || data.getHomeBuilding().getBuildingLevel() <= 2) ? 0.0 : data.getHomeBuilding().getBuildingLevel() - 2.0));
HappinessRegistry.sleptTonightFunction = DEFERRED_REGISTER_HAPPINESS_FUNCTION.register(SLEPTTONIGHT_FUNCTION.getPath(), () -> new HappinessFunctionEntry(data -> data.getJob() instanceof AbstractJobGuard ? 1 : 0.5));
HappinessRegistry.foodFunction = DEFERRED_REGISTER_HAPPINESS_FUNCTION.register(FOOD_FUNCTION.getPath(), () -> new HappinessFunctionEntry(data -> (data.getHomeBuilding() == null || data.getHomeBuilding().getBuildingLevel() <= 2) ? 1 : 0.5));
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/minecolonies/core/colony/CitizenData.java
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ public void serializeViewNetworkData(@NotNull final RegistryFriendlyByteBuf buf)
}

final CompoundTag happinessCompound = new CompoundTag();
citizenHappinessHandler.write(buf.registryAccess(), happinessCompound);
citizenHappinessHandler.write(buf.registryAccess(), happinessCompound, false);
buf.writeNbt(happinessCompound);

buf.writeInt(status != null ? status.getId() : -1);
Expand Down Expand Up @@ -1248,7 +1248,7 @@ public CompoundTag serializeNBT(@NotNull final HolderLookup.Provider provider)
nbtTagCompound.put("job", jobCompound);
}

citizenHappinessHandler.write(provider, nbtTagCompound);
citizenHappinessHandler.write(provider, nbtTagCompound, true);
citizenMournHandler.write(nbtTagCompound);

inventory.write(provider, nbtTagCompound);
Expand Down Expand Up @@ -1413,7 +1413,7 @@ public void deserializeNBT(@NotNull final HolderLookup.Provider provider, final
}
}

this.citizenHappinessHandler.read(provider, nbtTagCompound);
this.citizenHappinessHandler.read(provider, nbtTagCompound, true);
this.citizenMournHandler.read(nbtTagCompound);

if (nbtTagCompound.contains(TAG_LEVEL_MAP) && !nbtTagCompound.contains(TAG_NEW_SKILLS))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public void deserialize(@NotNull final RegistryFriendlyByteBuf buf)
sortedInteractions = new ArrayList<>(citizenChatOptions.values());
sortedInteractions.sort(Comparator.comparingInt(e -> -e.getPriority().getPriority()));

citizenHappinessHandler.read(buf.registryAccess(), buf.readNbt());
citizenHappinessHandler.read(buf.registryAccess(), buf.readNbt(), false);

int statusindex = buf.readInt();
statusIcon = statusindex >= 0 ? VisibleCitizenStatus.getForId(statusindex) : null;
Expand Down
Loading

0 comments on commit 12dcf26

Please sign in to comment.