Skip to content

Commit

Permalink
Feature/1.19 stats (ldtteam#8736)
Browse files Browse the repository at this point in the history
Add a way to track statistics in a colony
Add a few statistics to some jobs
Add display to the townhall UI
  • Loading branch information
Raycoms committed Nov 6, 2022
1 parent 87a66ab commit 9e3a732
Show file tree
Hide file tree
Showing 29 changed files with 431 additions and 27 deletions.
13 changes: 13 additions & 0 deletions src/api/java/com/minecolonies/api/colony/IColony.java
Original file line number Diff line number Diff line change
Expand Up @@ -463,4 +463,17 @@ default List<BlockPos> getWayPoints(@NotNull BlockPos position, @NotNull BlockPo
* @return the matching file.
*/
CitizenNameFile getCitizenNameFile();

/**
* Get the statistics manager of the colony.
*
* @return the statistics manager.
*/
IStatisticsManager getStatisticsManager();

/**
* Get the current day of the colony.
* @return the current day progress of the colony.
*/
int getDay();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.minecolonies.api.colony.managers.interfaces;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import org.jetbrains.annotations.NotNull;

import java.util.Set;

/**
* Interface for the statistics manager
*/
public interface IStatisticsManager
{
/**
* Increment a given statistic.
* Creates a new if it doesn't exist yet.
* Assigns a timestamp to the entry.
* @param id the id of the stat.
*/
void increment(@NotNull final String id);

/**
* Increment a given statistic by some quantity.
* Creates a new if it doesn't exist yet.
* Assigns a timestamp to the entry.
* @param id the id of the stat.
* @param qty the quantity.
*/
void incrementBy(@NotNull String id, final int qty);

/**
* Get the total for a given stat,
* @param id the id of the stat.
* @return the total since colony creation.
*/
int getStatTotal(@NotNull String id);

/**
* Get the number of occurrences in a given period.
* @param id the id of the stat.
* @param dayStart the start day.
* @param dayEnd the end day.
* @return the count.
*/
int getStatsInPeriod(@NotNull String id, final int dayStart, final int dayEnd);

/**
* Serialize to bytebuf.
* @param buf the buffer to write to.
*/
void serialize(@NotNull final FriendlyByteBuf buf);

/**
* Deserialize from bytebuf.
* @param buf the buffer to read from.
*/
void deserialize(@NotNull final FriendlyByteBuf buf);

/**
* Reads the eventManager nbt and creates events from it
*
* @param compound the compound to read from.
*/
void readFromNBT(@NotNull final CompoundTag compound);

/**
* Write the eventmanager and all events to NBT
*
* @param compound the compound to write to.
*/
void writeToNBT(@NotNull final CompoundTag compound);

/**
* Getter for the whole stat list.
* @return the map of stats.
*/
@NotNull
Set<String> getStatTypes();
}
10 changes: 5 additions & 5 deletions src/api/java/com/minecolonies/api/util/InventoryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
Expand Down Expand Up @@ -2966,7 +2965,7 @@ public static boolean doStorageSetsMatch(Map<ItemStorage, ItemStorage> first, Ma
* @param foodPredicate food choosing predicate
* @return true if any food was transferred
*/
public static boolean transferFoodUpToSaturation(
public static int transferFoodUpToSaturation(
final ICapabilityProvider source,
final IItemHandler target,
final int requiredSaturation,
Expand All @@ -2975,7 +2974,7 @@ public static boolean transferFoodUpToSaturation(
Set<IItemHandler> handlers = getItemHandlersFromProvider(source);

int foundSaturation = 0;

int transferedItems = 0;
for (final IItemHandler handler : handlers)
{
for (int i = 0; i < handler.getSlots(); i++)
Expand Down Expand Up @@ -3007,6 +3006,7 @@ public static boolean transferFoodUpToSaturation(
foundSaturation = requiredSaturation;
}

transferedItems += extractedFood.getCount();
if (!ItemStackUtils.isEmpty(extractedFood))
{
if (!addItemStackToItemHandler(target, extractedFood))
Expand All @@ -3024,13 +3024,13 @@ public static boolean transferFoodUpToSaturation(

if (foundSaturation >= requiredSaturation)
{
return true;
return transferedItems;
}
}
}
}

return foundSaturation > 0;
return transferedItems;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ public final class NbtTagConstants
*/
public static final String TAG_COL_NAME_STYLE = "namestyle";

/**
* The tag to store the day progress.
*/
public static final String COLONY_DAY = "colonyday";

/**
* Tag to store if raidable to a colony.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.minecolonies.api.util.constant;

/**
* Constants regarding stats.
*/
public final class StatisticsConstants
{
public static final String TREE_CUT = "trees_cut";
public static final String DEATH = "death";
public static final String BIRTH = "birth";
public static final String ORES_MINED = "ores_mined";
public static final String BLOCKS_MINED = "blocks_mined";
public static final String BLOCKS_PLACED = "blocks_placed";
public static final String MOBS_KILLED = "mobs_killed";
public static final String ITEMS_DELIVERED = "items_delivered";
public static final String ITEMS_CRAFTED = "items_crafted";
public static final String FOOD_SERVED = "food_served";
}
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,8 @@ public final class TranslationConstants
public static final String PARTIAL_JOURNEY_MAP_INFO = "com.minecolonies.coremod.journeymap.";
@NonNls
public static final String PARTIAL_EXPEDITION_STATUS = "com.minecolonies.gui.workerhuts.expedition.";
@NonNls
public static final String PARTIAL_STATS_MODIFIER_NAME = "com.minecolonies.coremod.gui.townhall.stats.";

//</editor-fold>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public final class WindowConstants
/**
* Id of the button displaying the Happiness in the GUI
*/
public static final String BUTTON_HAPPINESS = "happiness";
public static final String BUTTON_STATS = "happiness";

/**
* Id of the recall button in the GUI.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public AbstractWindowTownHall(final BuildingTownHall.View townHall, final String
registerButton(BUTTON_PERMISSIONS, () -> new WindowPermissionsPage(townHall).open());
registerButton(BUTTON_CITIZENS, () -> new WindowCitizenPage(townHall).open());
registerButton(BUTTON_WORKORDER, () -> new WindowWorkOrderPage(townHall).open());
registerButton(BUTTON_HAPPINESS, () -> new WindowHappinessPage(townHall).open());
registerButton(BUTTON_STATS, () -> new WindowStatsPage(townHall).open());

findPaneOfTypeByID(getWindowId() + "0", Image.class).hide();
findPaneOfTypeByID(getWindowId(), ButtonImage.class).hide();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,31 @@
import com.ldtteam.blockui.views.View;
import com.minecolonies.api.colony.ICitizenDataView;
import com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingTownHall;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.resources.ResourceLocation;

import java.util.HashMap;
import java.util.Map;

import static com.minecolonies.api.util.constant.TranslationConstants.PARTIAL_HAPPINESS_MODIFIER_NAME;
import static com.minecolonies.api.util.constant.TranslationConstants.PARTIAL_STATS_MODIFIER_NAME;
import static com.minecolonies.api.util.constant.WindowConstants.*;
import static com.minecolonies.coremod.client.gui.modules.WindowBuilderResModule.BLACK;

/**
* BOWindow for the town hall.
*/
public class WindowHappinessPage extends AbstractWindowTownHall
public class WindowStatsPage extends AbstractWindowTownHall
{
/**
* Constructor for the town hall window.
*
* @param townHall {@link BuildingTownHall.View}.
*/
public WindowHappinessPage(final BuildingTownHall.View townHall)
public WindowStatsPage(final BuildingTownHall.View townHall)
{
super(townHall, "layouthappiness.xml");
super(townHall, "layoutstats.xml");
}

/**
Expand All @@ -38,10 +40,11 @@ public void onOpened()
{
super.onOpened();
updateHappiness();
updateStats();
}

/**
* Update the display for the happiness
* Update the display for the happiness.
*/
private void updateHappiness()
{
Expand All @@ -56,7 +59,15 @@ private void updateHappiness()
}

final View pane = findPaneOfTypeByID("happinesspage", View.class);
int yPos = 62;
final Text titleLabel = new Text();
titleLabel.setSize(136, 11);
titleLabel.setPosition(25, 42);
titleLabel.setColors(BLACK);
titleLabel.setText(new TranslatableComponent("com.minecolonies.coremod.gui.townhall.currenthappiness"));
pane.addChild(titleLabel);


int yPos = 60;
for (final Map.Entry<String, Double> entry : happinessMap.entrySet())
{
final double value = entry.getValue() / building.getColony().getCitizenCount();
Expand Down Expand Up @@ -94,9 +105,30 @@ else if (value > 0.75)
}
}

/**
* Update the display for the stats.
*/
private void updateStats()
{
final View pane = findPaneOfTypeByID("statspage", View.class);
int yPos = 65;

for (final String entry : building.getColony().getStatisticsManager().getStatTypes())
{
final Text label = new Text();
label.setSize(136, 11);
label.setPosition(25, yPos);
label.setColors(BLACK);
label.setText(new TranslatableComponent(PARTIAL_STATS_MODIFIER_NAME + entry, building.getColony().getStatisticsManager().getStatTotal(entry)));
pane.addChild(label);

yPos += 12;
}
}

@Override
protected String getWindowId()
{
return BUTTON_HAPPINESS;
return BUTTON_STATS;
}
}
31 changes: 30 additions & 1 deletion src/main/java/com/minecolonies/coremod/colony/Colony.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ public class Colony implements IColony
*/
private final IProgressManager progressManager = new ProgressManager(this);

/**
* Event manager of the colony.
*/
private final IStatisticsManager statisticManager = new StatisticsManager(this);

/**
* The Positions which players can freely interact.
*/
Expand Down Expand Up @@ -321,6 +326,11 @@ public class Colony implements IColony
*/
private String nameStyle = "default";

/**
* Current day of the colony.
*/
private int day = 0;

/**
* Constructor for a newly created Colony.
*
Expand Down Expand Up @@ -577,6 +587,7 @@ private boolean checkDayTime()
else if (!isDay && WorldUtil.isDayTime(world))
{
isDay = true;
day++;
citizenManager.onWakeUp();
}
return false;
Expand Down Expand Up @@ -730,6 +741,8 @@ public void read(@NotNull final CompoundTag compound)
}

eventManager.readFromNBT(compound);
statisticManager.readFromNBT(compound);

eventDescManager.deserializeNBT(compound.getCompound(NbtTagConstants.TAG_EVENT_DESC_MANAGER));

if (compound.getAllKeys().contains(TAG_RESEARCH))
Expand Down Expand Up @@ -822,6 +835,7 @@ public void read(@NotNull final CompoundTag compound)
{
this.nameStyle = compound.getString(TAG_COL_NAME_STYLE);
}
this.day = compound.getInt(COLONY_DAY);
this.colonyTag = compound;
}

Expand Down Expand Up @@ -879,6 +893,8 @@ public CompoundTag write(@NotNull final CompoundTag compound)

progressManager.write(compound);
eventManager.writeToNBT(compound);
statisticManager.writeToNBT(compound);

compound.put(NbtTagConstants.TAG_EVENT_DESC_MANAGER, eventDescManager.serializeNBT());
raidManager.write(compound);

Expand Down Expand Up @@ -926,6 +942,7 @@ public CompoundTag write(@NotNull final CompoundTag compound)
compound.putLong(TAG_LAST_ONLINE, lastOnlineTime);
compound.putString(TAG_COL_TEXT, textureStyle);
compound.putString(TAG_COL_NAME_STYLE, nameStyle);
compound.putInt(COLONY_DAY, day);

this.colonyTag = compound;

Expand Down Expand Up @@ -1550,6 +1567,12 @@ public IEventManager getEventManager()
return eventManager;
}

@Override
public IStatisticsManager getStatisticsManager()
{
return statisticManager;
}

@Override
public IReproductionManager getReproductionManager()
{
Expand Down Expand Up @@ -1897,10 +1920,16 @@ public CitizenNameFile getCitizenNameFile()
/**
* Check if we need to update the view's chunk ticket info
*
* @return
* @return true if dirty.
*/
public boolean isTicketedChunksDirty()
{
return ticketedChunksDirty;
}

@Override
public int getDay()
{
return day;
}
}
Loading

0 comments on commit 9e3a732

Please sign in to comment.