Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Nutrients stuff
  • Loading branch information
alcatrazEscapee committed Jun 10, 2019
1 parent 6605a13 commit bbd2787
Show file tree
Hide file tree
Showing 58 changed files with 1,005 additions and 335 deletions.
11 changes: 10 additions & 1 deletion generateResources.py
Expand Up @@ -292,13 +292,14 @@ def zipfolder(zip_name, target_dir):
'onion': (False, 7),
'potato': (False, 7),
'soybean': (False, 7),
'squash': (False, 7),
# 'squash': (False, 7),
'sugarcane': (False, 8),
'red_bell_pepper': (False, 7),
'tomato': (False, 8),
'yellow_bell_pepper': (False, 7),
'jute': (True, 6)
}
SPREADING_CROPS = ['pumpkin', 'squash', 'melon']

FOODS = [
'banana',
Expand Down Expand Up @@ -905,6 +906,11 @@ def item(filename_parts, *layers, parent='item/generated'):
'stage': dict(('%d' % i, {'textures': {'crop': 'tfc:blocks/crop/%s_%d' % (crop, i)}}) for i in range(stages))
})

for crop in SPREADING_CROPS:
blockstate(('crop', crop), 'crop', {'crop': 'tfc:blocks/crop/%s_0' % crop}, {
'stage': dict(('%d' % i, {'textures': {'crop': 'tfc:blocks/crop/%s_%d' % (crop, i)}}) for i in range(8))
})

# _____ _
# |_ _| |
# | | | |_ ___ _ __ ___ ___
Expand Down Expand Up @@ -1035,3 +1041,6 @@ def item(filename_parts, *layers, parent='item/generated'):

for crop in SIMPLE_CROPS.keys():
item(('crop', 'seeds', crop), 'tfc:items/crop/seeds/%s' % crop)

for crop in SPREADING_CROPS:
item(('crop', 'seeds', crop), 'tfc:items/crop/seeds/%s' % crop)
10 changes: 10 additions & 0 deletions src/main/java/net/dries007/tfc/CommonEventHandler.java
Expand Up @@ -25,11 +25,13 @@

import net.dries007.tfc.api.capability.ItemStickCapability;
import net.dries007.tfc.api.capability.nuturient.CapabilityNutrients;
import net.dries007.tfc.api.capability.nuturient.IPlayerNutrients;
import net.dries007.tfc.api.capability.size.CapabilityItemSize;
import net.dries007.tfc.api.capability.size.Size;
import net.dries007.tfc.api.capability.size.Weight;
import net.dries007.tfc.api.util.IPlaceableItem;
import net.dries007.tfc.network.PacketCalendarUpdate;
import net.dries007.tfc.network.PacketNutrientsUpdate;
import net.dries007.tfc.objects.container.CapabilityContainerListener;
import net.dries007.tfc.util.Helpers;

Expand Down Expand Up @@ -168,6 +170,14 @@ public static void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event)

// World Data (Calendar) Sync Handler
TerraFirmaCraft.getNetwork().sendTo(new PacketCalendarUpdate(), player);

// Player nutrients
IPlayerNutrients cap = player.getCapability(CapabilityNutrients.CAPABILITY_PLAYER_NUTRIENTS, null);
if (cap != null)
{
cap.updateNutrientsFastForward();
TerraFirmaCraft.getNetwork().sendTo(new PacketNutrientsUpdate(cap), player);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/dries007/tfc/TerraFirmaCraft.java
Expand Up @@ -127,6 +127,7 @@ public void preInit(FMLPreInitializationEvent event)
network.registerMessage(new PacketCapabilityContainerUpdate.Handler(), PacketCapabilityContainerUpdate.class, ++id, Side.CLIENT);
network.registerMessage(new PacketCalendarUpdate.Handler(), PacketCalendarUpdate.class, ++id, Side.CLIENT);
network.registerMessage(new PacketBarrelUpdate.Handler(), PacketBarrelUpdate.class, ++id, Side.CLIENT);
network.registerMessage(new PacketNutrientsUpdate.Handler(), PacketNutrientsUpdate.class, ++id, Side.CLIENT);

EntitiesTFC.preInit();
CalendarTFC.preInit();
Expand Down Expand Up @@ -201,6 +202,7 @@ public void onServerStarting(FMLServerStartingEvent event)
event.registerServerCommand(new CommandHeat());
event.registerServerCommand(new CommandTimeTFC());
event.registerServerCommand(new CommandFindVeins());
event.registerServerCommand(new CommandNutrients());
}

@Mod.EventHandler
Expand Down
Expand Up @@ -26,6 +26,9 @@

public final class CapabilityNutrients
{
public static final float MIN_PLAYER_NUTRIENTS = 0f;
public static final float MAX_PLAYER_NUTRIENTS = 100f;

@CapabilityInject(INutrients.class)
public static final Capability<INutrients> CAPABILITY_NUTRIENTS = Helpers.getNull();
@CapabilityInject(IPlayerNutrients.class)
Expand Down Expand Up @@ -72,7 +75,10 @@ public static void onFinishUsingItem(LivingEntityUseItemEvent.Finish event)
for (Nutrient nutrient : Nutrient.values())
{
playerCap.addNutrient(nutrient, itemCap.getNutrients(stack, nutrient));


}
((PlayerNutrientsHandler) playerCap).debug();
}
}
}
Expand Down
Expand Up @@ -11,5 +11,13 @@ public interface IPlayerNutrients
{
float getNutrient(Nutrient nutrient);

float[] getNutrients();

void setNutrients(float[] nutrients);

void setNutrient(Nutrient nutrient, float amount);

void addNutrient(Nutrient nutrient, float amount);

void updateNutrientsFastForward();
}
Expand Up @@ -13,11 +13,17 @@
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;

import net.dries007.tfc.TerraFirmaCraft;
import net.dries007.tfc.util.agriculture.Nutrient;
import net.dries007.tfc.world.classic.CalendarTFC;

import static net.dries007.tfc.api.capability.nuturient.CapabilityNutrients.MAX_PLAYER_NUTRIENTS;
import static net.dries007.tfc.api.capability.nuturient.CapabilityNutrients.MIN_PLAYER_NUTRIENTS;

public class PlayerNutrientsHandler implements IPlayerNutrients, ICapabilitySerializable<NBTTagCompound>
{
private final float[] nutrients;
private long lastUpdateTick;

public PlayerNutrientsHandler()
{
Expand All @@ -26,21 +32,58 @@ public PlayerNutrientsHandler()

public PlayerNutrientsHandler(@Nullable NBTTagCompound nbt)
{
this.nutrients = new float[Nutrient.TOTAL];
nutrients = new float[Nutrient.TOTAL];
for (int i = 0; i < nutrients.length; i++)
{
nutrients[i] = 0.8f * MAX_PLAYER_NUTRIENTS;
}

deserializeNBT(nbt);
}

@Override
public float getNutrient(Nutrient nutrient)
{
return 0;
updateNutrients();
return nutrients[nutrient.ordinal()];
}

@Override
public void addNutrient(Nutrient nutrient, float amount)
public float[] getNutrients()
{
updateNutrients();
return nutrients;
}

@Override
public void setNutrients(float[] nutrients)
{
System.arraycopy(nutrients, 0, this.nutrients, 0, this.nutrients.length);
}

@Override
public void setNutrient(Nutrient nutrient, float amount)
{
if (amount < MIN_PLAYER_NUTRIENTS)
{
nutrients[nutrient.ordinal()] = MIN_PLAYER_NUTRIENTS;
}
else if (amount > MAX_PLAYER_NUTRIENTS)
{
nutrients[nutrient.ordinal()] = MAX_PLAYER_NUTRIENTS;
}
else
{
nutrients[nutrient.ordinal()] = amount;
}
}

@Override
public void addNutrient(Nutrient nutrient, float amount)
{
updateNutrients();
float newAmount = nutrients[nutrient.ordinal()] + amount;
setNutrient(nutrient, newAmount);
}

@Override
Expand All @@ -57,15 +100,23 @@ public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFaci
return capability == CapabilityNutrients.CAPABILITY_PLAYER_NUTRIENTS ? (T) this : null;
}

public void updateNutrientsFastForward()
{
lastUpdateTick = CalendarTFC.getCalendarTime();
}

@Override
@Nonnull
public NBTTagCompound serializeNBT()
{
updateNutrients();

NBTTagCompound nbt = new NBTTagCompound();
for (Nutrient nutrient : Nutrient.values())
{
nbt.setFloat(nutrient.name().toLowerCase(), this.nutrients[nutrient.ordinal()]);
}
nbt.setLong("lastUpdateTick", lastUpdateTick);
return nbt;
}

Expand All @@ -76,8 +127,26 @@ public void deserializeNBT(@Nullable NBTTagCompound nbt)
{
for (Nutrient nutrient : Nutrient.values())
{
this.nutrients[nutrient.ordinal()] = nbt.getFloat(nutrient.name().toLowerCase());
nutrients[nutrient.ordinal()] = nbt.getFloat(nutrient.name().toLowerCase());
}
lastUpdateTick = nbt.getLong("lastUpdateTick");
}
}

public void debug()
{
TerraFirmaCraft.getLog().info("Player Nutrients!!!");
for (Nutrient n : Nutrient.values())
TerraFirmaCraft.getLog().info("Nutrient: " + n.name() + " = " + nutrients[n.ordinal()]);
}

private void updateNutrients()
{
int ticksPassed = (int) (CalendarTFC.getCalendarTime() - lastUpdateTick);
for (Nutrient nutrient : Nutrient.values())
{
setNutrient(nutrient, nutrients[nutrient.ordinal()] - (nutrient.getDecayModifier() * ticksPassed));
}
lastUpdateTick = CalendarTFC.getCalendarTime();
}
}
9 changes: 1 addition & 8 deletions src/main/java/net/dries007/tfc/api/types/ICrop.java
Expand Up @@ -14,7 +14,7 @@
public interface ICrop
{
/**
* @return the minimum time the crop will take to grow one stage (in months)
* @return the minimum time the crop will take to grow one stage (in hours)
*/
float getGrowthTime();

Expand All @@ -41,13 +41,6 @@ public interface ICrop
*/
boolean isValidForGrowth(float temperature, float rainfall);

/**
* Can the crop be picked via right click?
*
* @return true if the crop can be right clicked to receive a food item and -2 to growth
*/
boolean isPickable();

/**
* Get the food item dropped by the crop upon breaking / picking
*
Expand Down
Expand Up @@ -66,6 +66,7 @@

import static net.dries007.tfc.api.util.TFCConstants.MOD_ID;
import static net.dries007.tfc.objects.blocks.BlockPlacedHide.SIZE;
import static net.dries007.tfc.objects.blocks.crops.BlockCropTFC.WILD;

@SideOnly(Side.CLIENT)
@Mod.EventBusSubscriber(value = Side.CLIENT, modid = MOD_ID)
Expand Down Expand Up @@ -192,7 +193,7 @@ public ModelResourceLocation getModelLocation(@Nonnull ItemStack stack)
}

for (Block block : BlocksTFC.getAllCropBlocks())
ModelLoader.setCustomStateMapper(block, new StateMap.Builder().build());
ModelLoader.setCustomStateMapper(block, new StateMap.Builder().ignore(WILD).build());

BlocksTFC.getAllBlockRockVariants().stream().filter(x -> x.type == Rock.Type.FARMLAND).forEach(e ->
ModelLoader.setCustomStateMapper(e, new StateMap.Builder().ignore(BlockFarmlandTFC.MOISTURE).build())
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/net/dries007/tfc/client/gui/GuiNutrition.java
Expand Up @@ -7,16 +7,21 @@

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiInventory;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import net.dries007.tfc.TerraFirmaCraft;
import net.dries007.tfc.api.capability.nuturient.CapabilityNutrients;
import net.dries007.tfc.api.capability.nuturient.IPlayerNutrients;
import net.dries007.tfc.client.TFCGuiHandler;
import net.dries007.tfc.client.button.GuiButtonPlayerInventoryTab;
import net.dries007.tfc.network.PacketSwitchPlayerInventoryTab;
import net.dries007.tfc.util.Helpers;
import net.dries007.tfc.util.agriculture.Nutrient;

import static net.dries007.tfc.api.util.TFCConstants.MOD_ID;

Expand All @@ -25,9 +30,19 @@ public class GuiNutrition extends GuiContainerTFC
{
private static final ResourceLocation BACKGROUND = new ResourceLocation(MOD_ID, "textures/gui/player_nutrition.png");

private float[] cachedNutrients;

public GuiNutrition(Container container, InventoryPlayer playerInv)
{
super(container, playerInv, BACKGROUND);

cachedNutrients = new float[Nutrient.TOTAL];
IPlayerNutrients cap = playerInv.player.getCapability(CapabilityNutrients.CAPABILITY_PLAYER_NUTRIENTS, null);
if (cap != null)
{
for (Nutrient n : Nutrient.values())
cachedNutrients[n.ordinal()] = cap.getNutrient(n);
}
}

@Override
Expand All @@ -47,7 +62,11 @@ protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY)
{
super.drawGuiContainerForegroundLayer(mouseX, mouseY);

// todo: draw nutrition bars
for (Nutrient n : Nutrient.values())
{
String stuff = I18n.format(Helpers.getEnumName(n)) + ": " + cachedNutrients[n.ordinal()];
fontRenderer.drawString(stuff, xSize / 2 - fontRenderer.getStringWidth(stuff) / 2, 10 + 12 * n.ordinal(), 0x404040);
}
}

@Override
Expand Down

0 comments on commit bbd2787

Please sign in to comment.