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

Overhaul study items #10338

Merged
merged 3 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ public class ServerConfiguration extends AbstractConfiguration
* ------------------- ######## Claim settings ######## ------------------- *
* --------------------------------------------------------------------------- */

public final ForgeConfigSpec.IntValue maxColonySize;
public final ForgeConfigSpec.IntValue minColonyDistance;
public final ForgeConfigSpec.IntValue initialColonySize;
public final ForgeConfigSpec.IntValue maxDistanceFromWorldSpawn;
public final ForgeConfigSpec.IntValue minDistanceFromWorldSpawn;
public final ForgeConfigSpec.IntValue maxColonySize;
public final ForgeConfigSpec.IntValue minColonyDistance;
public final ForgeConfigSpec.IntValue initialColonySize;
public final ForgeConfigSpec.IntValue maxDistanceFromWorldSpawn;
public final ForgeConfigSpec.IntValue minDistanceFromWorldSpawn;

/* ------------------------------------------------------------------------- *
* ------------------- ######## Combat Settings ######## ------------------- *
Expand Down Expand Up @@ -97,7 +97,6 @@ public class ServerConfiguration extends AbstractConfiguration
* ------------------- ######## Compatibility Settings ######## ------------------- *
* -------------------------------------------------------------------------------- */

public final ForgeConfigSpec.ConfigValue<List<? extends String>> configListStudyItems;
public final ForgeConfigSpec.ConfigValue<List<? extends String>> configListRecruitmentItems;
public final ForgeConfigSpec.ConfigValue<List<? extends String>> luckyOres;
public final ForgeConfigSpec.ConfigValue<List<? extends String>> diseases;
Expand All @@ -119,7 +118,6 @@ public class ServerConfiguration extends AbstractConfiguration

public final ForgeConfigSpec.BooleanValue creativeResolve;


/**
* Builds server configuration.
*
Expand Down Expand Up @@ -200,11 +198,6 @@ protected ServerConfiguration(final ForgeConfigSpec.Builder builder)

swapToCategory(builder, "compatibility");

configListStudyItems = defineList(builder, "configliststudyitems",
Arrays.asList
("minecraft:paper;400;100", "minecraft:book;600;10"),
s -> s instanceof String);

configListRecruitmentItems = defineList(builder, "configlistrecruitmentitems",
Arrays.asList
("minecraft:hay_block;3",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package com.minecolonies.core.colony.buildings.workerbuildings;

import com.minecolonies.api.colony.IColony;
import com.minecolonies.api.entity.ai.workers.util.StudyItem;
import com.minecolonies.api.util.Log;
import com.minecolonies.core.MineColonies;
import com.minecolonies.core.colony.buildings.AbstractBuilding;
import com.minecolonies.core.datalistener.StudyItemListener;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Tuple;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraftforge.common.Tags;
import net.minecraftforge.registries.ForgeRegistries;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
Expand All @@ -36,25 +31,20 @@ public class BuildingLibrary extends AbstractBuilding
*/
private static final String LIBRARY_HUT_NAME = "library";

/**
* List of registered barrels.
*/
private final List<BlockPos> bookCases = new ArrayList<>();

/**
* Max building level of the hut.
*/
private static final int MAX_BUILDING_LEVEL = 5;

/**
* Random obj for random calc.
* List of registered barrels.
*/
private final Random random = new Random();
private final List<BlockPos> bookCases = new ArrayList<>();

/**
* Study Item list, loaded from config, Typle<int,int> First is the study chance increase,second is the breakchance
* Random obj for random calc.
*/
private final List<StudyItem> studyItems;
private final Random random = new Random();
Thodor12 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Instantiates the building.
Expand All @@ -65,50 +55,7 @@ public class BuildingLibrary extends AbstractBuilding
public BuildingLibrary(final IColony c, final BlockPos l)
{
super(c, l);

studyItems = parseFromConfig();
}

/**
* Parses Study Items from the Config and adds them on the keepX list
*
* @return the list of study items
*/
private List<StudyItem> parseFromConfig()
{
final List<StudyItem> studyItemList = new ArrayList<>();

for (final String entry : MineColonies.getConfig().getServer().configListStudyItems.get())
{

try
{
final String[] entries = entry.split(";");
if (entries.length < 3)
{
Log.getLogger().info("Minecolonies: Parsing config for study items for Library failed for entry:" + entry);
continue;
}
final Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(entries[0]));
final int skillChance = Integer.parseInt(entries[1]);
final int breakChance = Integer.parseInt(entries[2]);

if (item == null || skillChance < 100 || skillChance > 1000 || breakChance > 100 || breakChance < 0)
{
Log.getLogger().info("Minecolonies: Parsing config for study items for Library failed for entry:" + entry);
continue;
}

studyItemList.add(new StudyItem(item, skillChance, breakChance));
// Keep a certain part of the items in the Chest
keepX.put(itemStack -> itemStack.getItem() == item, new Tuple<>(breakChance < 5 ? 5 : breakChance, true));
}
catch (NumberFormatException | ClassCastException e)
{
Log.getLogger().info("Minecolonies: Parsing config for study items for Library failed for entry:" + entry + " Exception:" + e.getMessage());
}
}
return studyItemList;
keepX.put(StudyItemListener::isStudyItem, new Tuple<>(64, true));
}

@NotNull
Expand All @@ -118,12 +65,6 @@ public String getSchematicName()
return LIBRARY_HUT_NAME;
}

@Override
public int getMaxBuildingLevel()
{
return MAX_BUILDING_LEVEL;
}

@Override
public void deserializeNBT(final CompoundTag compound)
{
Expand Down Expand Up @@ -151,6 +92,12 @@ public CompoundTag serializeNBT()
return compound;
}

@Override
public int getMaxBuildingLevel()
{
return MAX_BUILDING_LEVEL;
}

@Override
public void registerBlockPosition(@NotNull final Block block, @NotNull final BlockPos pos, @NotNull final Level world)
{
Expand Down Expand Up @@ -180,9 +127,4 @@ public BlockPos getRandomBookShelf()
bookCases.remove(returnPos);
return getPosition();
}

public List<StudyItem> getStudyItems()
{
return studyItems;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.minecolonies.core.datalistener;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.minecolonies.api.util.Log;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener;
import net.minecraft.util.GsonHelper;
import net.minecraft.util.Mth;
import net.minecraft.util.profiling.ProfilerFiller;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;

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

/**
* Loads and listens to study items for the {@link com.minecolonies.core.colony.buildings.workerbuildings.BuildingLibrary}.
*/
public class StudyItemListener extends SimpleJsonResourceReloadListener
{
/**
* Gson instance
*/
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();

/**
* Json constants
*/
private static final String KEY_ITEM = "item";
private static final String KEY_SKILL_INCREASE_CHANCE = "skill_increase_chance";
private static final String KEY_BREAK_CHANCE = "break_chance";

/**
* The current list of study items.
*/
private static Map<ResourceLocation, StudyItem> ACTIVE_LIST = new HashMap<>();

/**
* Container class for any study item.
*
* @param item the item to use.
* @param skillIncreaseChance chance for skill to increase after using the item.
* @param breakChance chance for the item to be used up after using it.
*/
public record StudyItem(Item item, int skillIncreaseChance, int breakChance)
{
}

/**
* Default constructor.
*/
public StudyItemListener()
{
super(GSON, "study_items");
}

/**
* Get the current map of study items.
*
* @return the map of study items.
*/
public static Map<ResourceLocation, StudyItem> getAllStudyItems()
{
return ACTIVE_LIST;
}

/**
* Check if the given item is a valid study item.
*
* @param stack the item stack to check.
* @return true if so.
*/
public static boolean isStudyItem(final ItemStack stack)
{
return ACTIVE_LIST.containsKey(BuiltInRegistries.ITEM.getKey(stack.getItem()));
}

@Override
protected void apply(
final @NotNull Map<ResourceLocation, JsonElement> jsonElementMap,
final @NotNull ResourceManager resourceManager,
final @NotNull ProfilerFiller profiler)
{
final Map<ResourceLocation, StudyItem> newItems = new HashMap<>();
for (final Map.Entry<ResourceLocation, JsonElement> entry : jsonElementMap.entrySet())
{
if (!entry.getValue().isJsonObject())
{
return;
}

final JsonObject object = entry.getValue().getAsJsonObject();

final Item item = BuiltInRegistries.ITEM.get(new ResourceLocation(GsonHelper.getAsString(object, KEY_ITEM)));
final int skillIncreaseChance = percentage(object, KEY_SKILL_INCREASE_CHANCE);
final int breakChance = percentage(object, KEY_BREAK_CHANCE);

newItems.put(entry.getKey(), new StudyItem(item, skillIncreaseChance, breakChance));
}
ACTIVE_LIST = Collections.unmodifiableMap(newItems);
}

/**
* Parse a json field as a percentage.
*
* @param object the input json object.
* @param field the field name to parse.
* @return the percentage.
*/
private int percentage(final JsonObject object, final String field)
{
final int raw = GsonHelper.getAsInt(object, field, 0);
final int clamped = Mth.clamp(raw, 0, 100);
if (raw != clamped)
{
Log.getLogger()
.warn(
"Parsing study item for Library contains a problem. Expected value for {} exceeded the range of [0,100], actual values was {}. Value was automatically clamped to {}.",
field,
raw,
clamped);
}
return clamped;
}
}
Loading