Skip to content

Commit

Permalink
Calendar-dimension-saftey-crash-prevention-bug-fix'n update
Browse files Browse the repository at this point in the history
Fixes #594, Fixes #587, Fixes #583, Fixes #577.
Added epsilons to alloy math
  • Loading branch information
alcatrazEscapee committed Nov 29, 2019
1 parent bc1880d commit 1a78fc7
Show file tree
Hide file tree
Showing 27 changed files with 499 additions and 486 deletions.
39 changes: 4 additions & 35 deletions src/main/java/net/dries007/tfc/CommonEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

package net.dries007.tfc;

import java.util.List;

import net.minecraft.block.Block;
import net.minecraft.block.BlockLeaves;
import net.minecraft.block.state.IBlockState;
Expand Down Expand Up @@ -376,9 +374,6 @@ public static void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event)
final EntityPlayerMP player = (EntityPlayerMP) event.player;
player.inventoryContainer.addListener(new CapabilityContainerListener(player));

// World Data (Calendar) Sync Handler
CalendarTFC.INSTANCE.updatePlayer(player);

// Food Stats
FoodStats originalStats = player.getFoodStats();
if (!(originalStats instanceof FoodStatsTFC))
Expand Down Expand Up @@ -407,27 +402,6 @@ public static void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event)
{
TerraFirmaCraft.getNetwork().sendTo(new PacketPlayerDataUpdate(skills.serializeNBT()), player);
}

// Check total players and reset player / calendar time ticking
int players = event.player.world.playerEntities.size();
CalendarTFC.INSTANCE.setArePlayersLoggedOn(event.player.world, players > 0);
}
}

@SubscribeEvent
public static void onPlayerLoggedOut(PlayerEvent.PlayerLoggedOutEvent event)
{
if (event.player instanceof EntityPlayerMP)
{
// Check total players and reset player / calendar time ticking
List<EntityPlayer> players = event.player.world.playerEntities;
int playerCount = players.size();
// The player logging out doesn't count
if (players.contains(event.player))
{
playerCount--;
}
CalendarTFC.INSTANCE.setArePlayersLoggedOn(event.player.world, playerCount > 0);
}
}

Expand Down Expand Up @@ -539,12 +513,7 @@ public static void onLivingSpawnEvent(LivingSpawnEvent.CheckSpawn event)
public static void onGameRuleChange(GameRuleChangeEvent event)
{
GameRules rules = event.getRules();
if ("doDaylightCycle".equals(event.getRuleName()))
{
// This is only called on server, so it needs to sync to client
CalendarTFC.INSTANCE.setDoDaylightCycle(event.getServer().getEntityWorld(), rules);
}
else if ("naturalRegeneration".equals(event.getRuleName()) && ConfigTFC.GENERAL.forceNoVanillaNaturalRegeneration)
if ("naturalRegeneration".equals(event.getRuleName()) && ConfigTFC.GENERAL.forceNoVanillaNaturalRegeneration)
{
// Natural regeneration should be disabled, allows TFC to have custom regeneration
event.getRules().setOrCreateGameRule("naturalRegeneration", "false");
Expand All @@ -561,7 +530,7 @@ public static void onWorldLoad(WorldEvent.Load event)
{
// Calendar Sync / Initialization
CalendarWorldData data = CalendarWorldData.get(world);
CalendarTFC.INSTANCE.reset(data.getCalendar());
CalendarTFC.INSTANCE.resetTo(data.getCalendar());
TerraFirmaCraft.getNetwork().sendToAll(new PacketCalendarUpdate(CalendarTFC.INSTANCE));
}

Expand Down Expand Up @@ -624,8 +593,8 @@ public static void onPlayerTick(TickEvent.PlayerTickEvent event)
if (hugeHeavyCount >= 1)
{
// Add extra exhaustion from carrying a heavy item
// Equivalent to jumping once every two seconds (TFC reduces this, as most of exhaustion is passive)
event.player.addExhaustion(0.5f);
// This is equivalent to an additional 25% of passive exhaustion
event.player.addExhaustion(FoodStatsTFC.PASSIVE_EXHAUSTION * 20 * 0.25f / 0.4f);
}
if (hugeHeavyCount >= 2)
{
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/dries007/tfc/TerraFirmaCraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import net.dries007.tfc.objects.entity.EntitiesTFC;
import net.dries007.tfc.objects.items.ItemsTFC;
import net.dries007.tfc.proxy.IProxy;
import net.dries007.tfc.util.calendar.CalendarTFC;
import net.dries007.tfc.util.fuel.FuelManager;
import net.dries007.tfc.world.classic.WorldTypeTFC;
import net.dries007.tfc.world.classic.chunkdata.CapabilityChunkData;
Expand Down Expand Up @@ -222,6 +223,9 @@ public void onServerStarting(FMLServerStartingEvent event)
event.registerServerCommand(new CommandTimeTFC());
event.registerServerCommand(new CommandFindVeins());
event.registerServerCommand(new CommandDebugInfo());

// Initialize calendar for the current server
CalendarTFC.INSTANCE.init(event.getServer());
}

@Mod.EventHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import net.dries007.tfc.Constants;
import net.dries007.tfc.TerraFirmaCraft;
import net.dries007.tfc.network.PacketFoodStatsUpdate;
import net.dries007.tfc.util.calendar.CalendarTFC;
import net.dries007.tfc.util.calendar.ICalendar;

@ParametersAreNonnullByDefault
Expand Down Expand Up @@ -314,13 +313,13 @@ public float getThirst()
@Override
public boolean attemptDrink(float value, boolean simulate)
{
int ticksPassed = (int) (CalendarTFC.TOTAL_TIME.getTicks() - lastDrinkTick);
int ticksPassed = (int) (sourcePlayer.world.getTotalWorldTime() - lastDrinkTick);
if (ticksPassed >= 20 && thirst < MAX_PLAYER_THIRST)
{
// One drink every so often
lastDrinkTick = CalendarTFC.TOTAL_TIME.getTicks();
if (!simulate)
{
// One drink every so often
lastDrinkTick = sourcePlayer.world.getTotalWorldTime();
addThirst(value);
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ default void addNutrientInfo(@Nonnull ItemStack stack, @Nonnull List<String> tex
{
// Calculate the date to display in calendar time
long rottenCalendarTime = rottenDate - CalendarTFC.PLAYER_TIME.getTicks() + CalendarTFC.CALENDAR_TIME.getTicks();
text.add(TextFormatting.DARK_GREEN + I18n.format("tfc.tooltip.food_expiry_date", ICalendarFormatted.getTimeAndDate(rottenCalendarTime, CalendarTFC.INSTANCE.getDaysInMonth())));
text.add(TextFormatting.DARK_GREEN + I18n.format("tfc.tooltip.food_expiry_date", ICalendarFormatted.getTimeAndDate(rottenCalendarTime, CalendarTFC.CALENDAR_TIME.getDaysInMonth())));

if (ConfigTFC.GENERAL.debug)
{
Expand Down
29 changes: 18 additions & 11 deletions src/main/java/net/dries007/tfc/api/recipes/AlloyRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@

import net.dries007.tfc.api.registries.TFCRegistries;
import net.dries007.tfc.api.types.Metal;
import net.dries007.tfc.util.Alloy;

/**
* todo: in 1.13+ move this to a json recipe type
*/
public class AlloyRecipe extends IForgeRegistryEntry.Impl<AlloyRecipe>
{
private final ImmutableMap<Metal, AlloyRange> metalMap;
private final ImmutableMap<Metal, DoubleRange> metalMap;
private final Metal result;

private AlloyRecipe(@Nonnull Metal result, ImmutableMap<Metal, AlloyRange> alloyMap)
private AlloyRecipe(@Nonnull Metal result, ImmutableMap<Metal, DoubleRange> alloyMap)
{
this.metalMap = alloyMap;
this.result = result;
Expand All @@ -45,15 +46,15 @@ public String toString()
return getRegistryName().getPath();
}

public ImmutableMap<Metal, AlloyRange> getMetals()
public ImmutableMap<Metal, DoubleRange> getMetals()
{
return metalMap;
}

public static class Builder
{
private final Metal result;
private final ImmutableMap.Builder<Metal, AlloyRange> builder;
private final ImmutableMap.Builder<Metal, DoubleRange> builder;

public Builder(@Nonnull Metal result)
{
Expand All @@ -71,10 +72,10 @@ public Builder(@Nonnull ResourceLocation loc)

public Builder add(@Nonnull ResourceLocation loc, double min, double max)
{
return add(loc, new AlloyRange(min, max));
return add(loc, new DoubleRange(min, max));
}

public Builder add(@Nonnull ResourceLocation loc, @Nonnull AlloyRange condition)
public Builder add(@Nonnull ResourceLocation loc, @Nonnull DoubleRange condition)
{
Metal metal = TFCRegistries.METALS.getValue(loc);
if (metal == null)
Expand All @@ -84,10 +85,10 @@ public Builder add(@Nonnull ResourceLocation loc, @Nonnull AlloyRange condition)

public Builder add(@Nonnull Metal metal, double min, double max)
{
return add(metal, new AlloyRange(min, max));
return add(metal, new DoubleRange(min, max));
}

public Builder add(@Nonnull Metal metal, @Nonnull AlloyRange condition)
public Builder add(@Nonnull Metal metal, @Nonnull DoubleRange condition)
{
builder.put(metal, condition);
return this;
Expand All @@ -99,11 +100,17 @@ public AlloyRecipe build()
}
}

public static class AlloyRange
/**
* A range that an alloy must fall within
* Values are to be interpreted as percentages, and are compared with an accuracy of epsilon
*
* @see Alloy#EPSILON
*/
public static final class DoubleRange
{
private double min, max;

AlloyRange(double min, double max)
DoubleRange(double min, double max)
{
this.min = min;
this.max = max;
Expand All @@ -121,7 +128,7 @@ public double getMax()

public boolean test(double value)
{
return value >= min && value <= max;
return value >= min - Alloy.EPSILON && value <= max + Alloy.EPSILON;
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/net/dries007/tfc/client/ClientEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public static void onRenderGameOverlayText(RenderGameOverlayEvent.Text event)

// Always add calendar info
list.add(I18n.format("tfc.tooltip.date", CalendarTFC.CALENDAR_TIME.getTimeAndDate()));
list.add(I18n.format("tfc.tooltip.debug_times", CalendarTFC.TOTAL_TIME.getTicks(), CalendarTFC.PLAYER_TIME.getTicks(), CalendarTFC.CALENDAR_TIME.getTicks()));
list.add(I18n.format("tfc.tooltip.debug_times", CalendarTFC.PLAYER_TIME.getTicks(), CalendarTFC.CALENDAR_TIME.getTicks()));

if (chunkDataValid)
{
Expand Down
Loading

0 comments on commit 1a78fc7

Please sign in to comment.