Skip to content
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

Quadratic Progression to Extra Hearts Milestones #32

Merged
merged 9 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ExampleMod tag to use as Blowdryer (Spotless, etc.) settings version, leave empty to disable.
# LOCAL to test local config updates.
gtnh.settings.blowdryerTag = 0.2.0
gtnh.settings.blowdryerTag = 0.2.2

# Human-readable mod name, available for mcmod.info population.
modName = The Spice of Life - Carrot Edition
Expand Down Expand Up @@ -112,7 +112,7 @@ usesShadowedDependencies = true

# If disabled, won't remove unused classes from shadowed dependencies. Some libraries use reflection to access
# their own classes, making the minimization unreliable.
minimizeShadowedDependencies = false
minimizeShadowedDependencies = true

# If disabled, won't rename the shadowed classes.
relocateShadowedDependencies = true
Expand Down
315 changes: 200 additions & 115 deletions src/main/java/squeek/spiceoflife/ModConfig.java

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions src/main/java/squeek/spiceoflife/ModContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public static void registerItems() {

public static void registerRecipes() {
GameRegistry
.addShapelessRecipe(new ItemStack(foodJournal), new ItemStack(Items.wheat), new ItemStack(Items.paper));
.addShapelessRecipe(new ItemStack(foodJournal), new ItemStack(Items.wheat), new ItemStack(Items.paper));
GameRegistry.addShapedRecipe(
new ItemStack(lunchBox),
"_ _",
" _ ",
'_',
new ItemStack(Blocks.heavy_weighted_pressure_plate));
new ItemStack(lunchBox),
"_ _",
" _ ",
'_',
new ItemStack(Blocks.heavy_weighted_pressure_plate));
GameRegistry.addShapedRecipe(new ItemStack(lunchBag), "p p", " p ", 'p', new ItemStack(Items.paper));
}
}
4 changes: 3 additions & 1 deletion src/main/java/squeek/spiceoflife/ModSpiceOfLife.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public void preInit(FMLPreInitializationEvent event) {
public void init(FMLInitializationEvent event) {
GuiHelper.init();
FoodTracker foodTracker = new FoodTracker();
FMLCommonHandler.instance().bus().register(foodTracker);
FMLCommonHandler.instance()
.bus()
.register(foodTracker);
MinecraftForge.EVENT_BUS.register(foodTracker);
MinecraftForge.EVENT_BUS.register(new FoodModifier());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public void sendTo(PacketBase packet, EntityPlayerMP player) {
@Override
public void sendToAllAround(PacketBase packet, PacketTarget packetTarget) {
NetworkRegistry.TargetPoint targetPoint = new NetworkRegistry.TargetPoint(
packetTarget.dimension,
packetTarget.x,
packetTarget.y,
packetTarget.z,
packetTarget.range);
packetTarget.dimension,
packetTarget.x,
packetTarget.y,
packetTarget.z,
packetTarget.range);
PacketHandler.channel.sendToAllAround(packet, targetPoint);
}

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/squeek/spiceoflife/foodtracker/FoodEaten.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public String toString() {

@Override
public int hashCode() {
return itemStack.getItem().hashCode();
return itemStack.getItem()
.hashCode();
}

@Override
Expand All @@ -42,9 +43,9 @@ public boolean equals(Object obj) {
if (other.itemStack == null || this.itemStack == null) return false;
final Item item = itemStack.getItem();
final Item otherItem = other.itemStack.getItem();
return (Item.itemRegistry.getNameForObject(item).equals(Item.itemRegistry.getNameForObject(otherItem))
&& item.equals(otherItem)
&& this.itemStack.getItemDamage() == other.itemStack.getItemDamage());
return (Item.itemRegistry.getNameForObject(item)
.equals(Item.itemRegistry.getNameForObject(otherItem)) && item.equals(otherItem)
&& this.itemStack.getItemDamage() == other.itemStack.getItemDamage());
}

public static FoodEaten loadFromNBTData(NBTTagCompound nbtFood) {
Expand Down
62 changes: 40 additions & 22 deletions src/main/java/squeek/spiceoflife/foodtracker/FoodHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public int getFoodCountForFoodGroup(ItemStack food, FoodGroup foodGroup) {
for (FoodEaten foodEaten : recentHistory) {
if (foodEaten.itemStack == null) continue;

if (food.isItemEqual(foodEaten.itemStack) || foodEaten.getFoodGroups().contains(foodGroup)) {
if (food.isItemEqual(foodEaten.itemStack) || foodEaten.getFoodGroups()
.contains(foodGroup)) {
count += 1;
}
}
Expand All @@ -106,7 +107,7 @@ public boolean containsFoodOrItsFoodGroups(ItemStack food) {
if (foodEaten.itemStack == null) continue;

if (food.isItemEqual(foodEaten.itemStack)
|| MiscHelper.collectionsOverlap(foodGroups, foodEaten.getFoodGroups())) {
|| MiscHelper.collectionsOverlap(foodGroups, foodEaten.getFoodGroups())) {
return true;
}
}
Expand All @@ -124,7 +125,8 @@ public FoodValues getTotalFoodValuesForFoodGroup(ItemStack food, FoodGroup foodG
for (FoodEaten foodEaten : recentHistory) {
if (foodEaten.itemStack == null) continue;

if (food.isItemEqual(foodEaten.itemStack) || foodEaten.getFoodGroups().contains(foodGroup)) {
if (food.isItemEqual(foodEaten.itemStack) || foodEaten.getFoodGroups()
.contains(foodGroup)) {
totalHunger += foodEaten.foodValues.hunger;
totalSaturation += foodEaten.foodValues.getSaturationIncrement();
}
Expand Down Expand Up @@ -269,21 +271,22 @@ private static void spawnParticles(EntityPlayer player, String type, int count)

// this function sends a packet to the client
world.func_147487_a(
type,
(float) player.posX,
(float) player.posY + 2,
(float) player.posZ,
count,
1F,
1F,
1F,
0.20000000298023224D);
type,
(float) player.posX,
(float) player.posY + 2,
(float) player.posZ,
count,
1F,
1F,
1F,
0.20000000298023224D);
}

@Override
// null compound parameter means save persistent data only
public void writeToNBTData(NBTTagCompound data) {
NBTTagCompound rootPersistentCompound = player.getEntityData().getCompoundTag(EntityPlayer.PERSISTED_NBT_TAG);
NBTTagCompound rootPersistentCompound = player.getEntityData()
.getCompoundTag(EntityPlayer.PERSISTED_NBT_TAG);
NBTTagCompound nonPersistentCompound = new NBTTagCompound();
NBTTagCompound persistentCompound = new NBTTagCompound();

Expand All @@ -298,9 +301,15 @@ public void writeToNBTData(NBTTagCompound data) {
}
}
if (fullHistory.size() > 0) {
NBTTagCompound nbtFullHistory = new NBTTagCompound();
fullHistory.writeToNBTData(nbtFullHistory);
persistentCompound.setTag("FullHistory", nbtFullHistory);
if (data != null || ModConfig.FOOD_MILESTONES_PERSISTS_THROUGH_DEATH) {
NBTTagCompound nbtFullHistory = new NBTTagCompound();

fullHistory.writeToNBTData(nbtFullHistory);

if (ModConfig.FOOD_MILESTONES_PERSISTS_THROUGH_DEATH)
persistentCompound.setTag("FullHistory", nbtFullHistory);
else nonPersistentCompound.setTag("FullHistory", nbtFullHistory);
}
}
if (totalFoodsEatenAllTime > 0) {
persistentCompound.setInteger("Total", totalFoodsEatenAllTime);
Expand All @@ -316,8 +325,10 @@ public void writeToNBTData(NBTTagCompound data) {

if (!persistentCompound.hasNoTags()) rootPersistentCompound.setTag(TAG_KEY, persistentCompound);

if (!player.getEntityData().hasKey(EntityPlayer.PERSISTED_NBT_TAG))
player.getEntityData().setTag(EntityPlayer.PERSISTED_NBT_TAG, rootPersistentCompound);
if (!player.getEntityData()
.hasKey(EntityPlayer.PERSISTED_NBT_TAG))
player.getEntityData()
.setTag(EntityPlayer.PERSISTED_NBT_TAG, rootPersistentCompound);
}

@Override
Expand All @@ -331,15 +342,23 @@ public void init(Entity entity, World world) {}
@Override
// null compound parameter means load persistent data only
public void readFromNBTData(NBTTagCompound data) {
NBTTagCompound rootPersistentCompound = player.getEntityData().getCompoundTag(EntityPlayer.PERSISTED_NBT_TAG);
NBTTagCompound rootPersistentCompound = player.getEntityData()
.getCompoundTag(EntityPlayer.PERSISTED_NBT_TAG);
NBTTagCompound persistentCompound = rootPersistentCompound.getCompoundTag(TAG_KEY);

if ((data != null && data.hasKey(TAG_KEY)) || rootPersistentCompound.hasKey(TAG_KEY)) {
NBTTagCompound nonPersistentCompound = data != null ? data.getCompoundTag(TAG_KEY) : new NBTTagCompound();

NBTTagCompound nbtHistory = ModConfig.FOOD_HISTORY_PERSISTS_THROUGH_DEATH
? persistentCompound.getCompoundTag("History")
: nonPersistentCompound.getCompoundTag("History");
? persistentCompound.getCompoundTag("History")
: nonPersistentCompound.getCompoundTag("History");

NBTTagCompound nbtFullHistory = ModConfig.FOOD_MILESTONES_PERSISTS_THROUGH_DEATH
? persistentCompound.getCompoundTag("FullHistory")
: nonPersistentCompound.getCompoundTag("FullHistory");

fullHistory.readFromNBTData(nbtHistory);
fullHistory.readFromNBTData(nbtFullHistory);

recentHistory.readFromNBTData(nbtHistory);

Expand All @@ -348,6 +367,5 @@ public void readFromNBTData(NBTTagCompound data) {
ticksActive = persistentCompound.getLong("Ticks");
}

fullHistory.readFromNBTData(persistentCompound.getCompoundTag("FullHistory"));
}
}
6 changes: 4 additions & 2 deletions src/main/java/squeek/spiceoflife/foodtracker/FoodLists.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ public final class FoodLists {
public static void setUp() {
if (ModConfig.DEV_LOGGING_ENABLED) ModSpiceOfLife.Log.info("Starting populating food list.");
Stream<Item> stream = StreamSupport.stream(Item.itemRegistry.spliterator(), false);
allFoods = stream.map(ItemStack::new).filter(FoodHelper::isFood)
.sorted(Comparator.comparing(ItemStack::getDisplayName)).collect(Collectors.toList());
allFoods = stream.map(ItemStack::new)
.filter(FoodHelper::isFood)
.sorted(Comparator.comparing(ItemStack::getDisplayName))
.collect(Collectors.toList());

allFoods.forEach((i -> {
FoodValues fv = FoodHelper.getFoodValues(i);
Expand Down
50 changes: 34 additions & 16 deletions src/main/java/squeek/spiceoflife/foodtracker/FoodModifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void getFoodValues(FoodEvent.GetPlayerFoodValues event) {
ItemStack actualFood = event.food;
if (FoodHelper.isFoodContainer(event.food)) {
actualFood = ((ItemFoodContainer) event.food.getItem())
.getBestFoodForPlayerToEat(event.food, event.player);
.getBestFoodForPlayerToEat(event.food, event.player);
}

if (actualFood != null) {
Expand Down Expand Up @@ -91,7 +91,8 @@ public static float getFoodModifier(FoodHistory foodHistory, ItemStack food) {
// if this food has mutliple food groups, calculate the modifier for each individually
// and then take the average
// for foods with <= 1 food group, this just means dividing the modifier by 1
FoodGroup[] foodGroups = FoodGroupRegistry.getFoodGroupsForFood(food).toArray(new FoodGroup[0]);
FoodGroup[] foodGroups = FoodGroupRegistry.getFoodGroupsForFood(food)
.toArray(new FoodGroup[0]);
int numIterations = Math.max(1, foodGroups.length);
float modifierSum = 0f;

Expand All @@ -112,19 +113,36 @@ public static float getFoodGroupModifier(FoodHistory foodHistory, ItemStack food

if (foodValues != null) {
BigDecimal result = effectiveFoodModifier.expression.with("count", new BigDecimal(count))
.and("cur_history_length", new BigDecimal(historySize))
.and("food_hunger_value", new BigDecimal(foodValues.hunger))
.and("food_saturation_mod", new BigDecimal(foodValues.saturationModifier))
.and("cur_hunger", new BigDecimal(foodHistory.player.getFoodStats().getFoodLevel()))
.and("cur_saturation", new BigDecimal(foodHistory.player.getFoodStats().getSaturationLevel()))
.and("total_food_eaten", new BigDecimal(foodHistory.totalFoodsEatenAllTime))
.and("max_history_length", new BigDecimal(ModConfig.FOOD_HISTORY_LENGTH))
.and("hunger_count", new BigDecimal(totalFoodValues.hunger))
.and("saturation_count", new BigDecimal(totalFoodValues.saturationModifier))
.and("food_group_count", new BigDecimal(FoodGroupRegistry.getFoodGroupsForFood(food).size()))
.and("distinct_food_groups_eaten", new BigDecimal(foodHistory.getDistinctFoodGroups().size()))
.and("total_food_groups", new BigDecimal(FoodGroupRegistry.numFoodGroups()))
.and("exact_count", new BigDecimal(foodHistory.getFoodCountIgnoringFoodGroups(food))).eval();
.and("cur_history_length", new BigDecimal(historySize))
.and("food_hunger_value", new BigDecimal(foodValues.hunger))
.and("food_saturation_mod", new BigDecimal(foodValues.saturationModifier))
.and(
"cur_hunger",
new BigDecimal(
foodHistory.player.getFoodStats()
.getFoodLevel()))
.and(
"cur_saturation",
new BigDecimal(
foodHistory.player.getFoodStats()
.getSaturationLevel()))
.and("total_food_eaten", new BigDecimal(foodHistory.totalFoodsEatenAllTime))
.and("max_history_length", new BigDecimal(ModConfig.FOOD_HISTORY_LENGTH))
.and("hunger_count", new BigDecimal(totalFoodValues.hunger))
.and("saturation_count", new BigDecimal(totalFoodValues.saturationModifier))
.and(
"food_group_count",
new BigDecimal(
FoodGroupRegistry.getFoodGroupsForFood(food)
.size()))
.and(
"distinct_food_groups_eaten",
new BigDecimal(
foodHistory.getDistinctFoodGroups()
.size()))
.and("total_food_groups", new BigDecimal(FoodGroupRegistry.numFoodGroups()))
.and("exact_count", new BigDecimal(foodHistory.getFoodCountIgnoringFoodGroups(food)))
.eval();

return result.floatValue();
}
Expand All @@ -137,7 +155,7 @@ public void getFoodEatingSpeed(PlayerUseItemEvent.Start event) {
ItemStack actualFood = event.item;
if (FoodHelper.isFoodContainer(event.item)) {
actualFood = ((ItemFoodContainer) event.item.getItem())
.getBestFoodForPlayerToEat(event.item, event.entityPlayer);
.getBestFoodForPlayerToEat(event.item, event.entityPlayer);
}

if (actualFood != null) {
Expand Down
31 changes: 20 additions & 11 deletions src/main/java/squeek/spiceoflife/foodtracker/FoodTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
public class FoodTracker {

public static int getFoodHistoryLengthInRelevantUnits(EntityPlayer player) {
return FoodHistory.get(player).getHistoryLength();
return FoodHistory.get(player)
.getHistoryLength();
}

public static ItemStack getFoodLastEatenBy(EntityPlayer player) {
return FoodHistory.get(player).getLastEatenFood().itemStack;
return FoodHistory.get(player)
.getLastEatenFood().itemStack;
}

/**
Expand All @@ -46,9 +48,10 @@ public void onFoodEaten(FoodEvent.FoodEaten event) {

public static void addFoodEatenByPlayer(FoodEaten foodEaten, EntityPlayer player) {
// client needs to be told by the server otherwise the client can get out of sync easily
if (!player.worldObj.isRemote && player instanceof EntityPlayerMP)
PacketDispatcher.get().sendTo(new PacketFoodHistory(foodEaten), (EntityPlayerMP) player);
FoodHistory.get(player).addFood(foodEaten);
if (!player.worldObj.isRemote && player instanceof EntityPlayerMP) PacketDispatcher.get()
.sendTo(new PacketFoodHistory(foodEaten), (EntityPlayerMP) player);
FoodHistory.get(player)
.addFood(foodEaten);
}

/**
Expand Down Expand Up @@ -85,10 +88,12 @@ public void onPlayerLogin(PlayerLoggedInEvent event) {
}

public static void syncFoodHistory(FoodHistory foodHistory) {
PacketDispatcher.get().sendTo(
PacketDispatcher.get()
.sendTo(
new PacketFoodEatenAllTime(foodHistory.totalFoodsEatenAllTime),
(EntityPlayerMP) foodHistory.player);
PacketDispatcher.get().sendTo(new PacketFoodHistory(foodHistory, true), (EntityPlayerMP) foodHistory.player);
PacketDispatcher.get()
.sendTo(new PacketFoodHistory(foodHistory, true), (EntityPlayerMP) foodHistory.player);
MaxHealthHandler.updateFoodHPModifier(foodHistory.player);
}

Expand All @@ -106,8 +111,9 @@ public void onPlayerChangedDimension(PlayerChangedDimensionEvent event) {
*/
@SubscribeEvent
public void onLivingDeathEvent(LivingDeathEvent event) {
if (FMLCommonHandler.instance().getEffectiveSide().isClient() || !(event.entity instanceof EntityPlayer))
return;
if (FMLCommonHandler.instance()
.getEffectiveSide()
.isClient() || !(event.entity instanceof EntityPlayer)) return;

EntityPlayer player = (EntityPlayer) event.entity;

Expand All @@ -120,7 +126,9 @@ public void onLivingDeathEvent(LivingDeathEvent event) {
*/
@SubscribeEvent
public void onPlayerRespawn(PlayerRespawnEvent event) {
if (FMLCommonHandler.instance().getEffectiveSide().isClient()) return;
if (FMLCommonHandler.instance()
.getEffectiveSide()
.isClient()) return;

// load any persistent food history data
FoodHistory foodHistory = FoodHistory.get(event.player);
Expand All @@ -135,6 +143,7 @@ public void onPlayerRespawn(PlayerRespawnEvent event) {
*/
@SubscribeEvent
public void onClientConnectedToServer(ClientConnectedToServerEvent event) {
if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT) ModConfig.assumeClientOnly();
if (FMLCommonHandler.instance()
.getEffectiveSide() == Side.CLIENT) ModConfig.assumeClientOnly();
}
}
Loading