From db074ea0865eb0500698235061b42651c69a047e Mon Sep 17 00:00:00 2001 From: Thom van den Akker Date: Thu, 17 Oct 2024 19:24:10 +0200 Subject: [PATCH 1/2] Turn study items into datapack and request all possible study items at once if nothing is present in inventory --- .../configuration/ServerConfiguration.java | 17 +-- .../api/entity/ai/workers/util/StudyItem.java | 66 --------- .../workerbuildings/BuildingLibrary.java | 82 ++--------- .../core/datalistener/StudyItemListener.java | 132 ++++++++++++++++++ .../ai/workers/education/EntityAIStudy.java | 80 ++++++----- .../core/event/FMLEventHandler.java | 1 + .../data/minecolonies/study_items/book.json | 5 + .../data/minecolonies/study_items/paper.json | 5 + 8 files changed, 203 insertions(+), 185 deletions(-) delete mode 100755 src/main/java/com/minecolonies/api/entity/ai/workers/util/StudyItem.java create mode 100644 src/main/java/com/minecolonies/core/datalistener/StudyItemListener.java create mode 100644 src/main/resources/data/minecolonies/study_items/book.json create mode 100644 src/main/resources/data/minecolonies/study_items/paper.json diff --git a/src/main/java/com/minecolonies/api/configuration/ServerConfiguration.java b/src/main/java/com/minecolonies/api/configuration/ServerConfiguration.java index ec3bb5fabb7..fdbb2426341 100755 --- a/src/main/java/com/minecolonies/api/configuration/ServerConfiguration.java +++ b/src/main/java/com/minecolonies/api/configuration/ServerConfiguration.java @@ -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 ######## ------------------- * @@ -97,7 +97,6 @@ public class ServerConfiguration extends AbstractConfiguration * ------------------- ######## Compatibility Settings ######## ------------------- * * -------------------------------------------------------------------------------- */ - public final ForgeConfigSpec.ConfigValue> configListStudyItems; public final ForgeConfigSpec.ConfigValue> configListRecruitmentItems; public final ForgeConfigSpec.ConfigValue> luckyOres; public final ForgeConfigSpec.ConfigValue> diseases; @@ -119,7 +118,6 @@ public class ServerConfiguration extends AbstractConfiguration public final ForgeConfigSpec.BooleanValue creativeResolve; - /** * Builds server configuration. * @@ -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", diff --git a/src/main/java/com/minecolonies/api/entity/ai/workers/util/StudyItem.java b/src/main/java/com/minecolonies/api/entity/ai/workers/util/StudyItem.java deleted file mode 100755 index 624da4f72a5..00000000000 --- a/src/main/java/com/minecolonies/api/entity/ai/workers/util/StudyItem.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.minecolonies.api.entity.ai.workers.util; - -import net.minecraft.world.item.Item; -import org.jetbrains.annotations.NotNull; - -public class StudyItem -{ - /** - * The actual item to use for studies - */ - private final Item item; - - /** - * The percent chance increase for skillups [100-1000] - */ - private final int skillIncrease; - - /** - * The percent chance to break upon skillup-try [0-100] - */ - private final int breakChance; - - /** - * Inventory slot the item is found in, set for specific use - */ - private int slot = -1; - - /** - * Initializing Study item, making sure the rates are valid. - * - * @param breakChance the break chance. - * @param item the item. - * @param skillIncrease the skill increase factor. - */ - public StudyItem(@NotNull final Item item, final int skillIncrease, final int breakChance) - { - this.item = item; - this.skillIncrease = skillIncrease > 0 ? skillIncrease : 100; - this.breakChance = breakChance < 0 ? 0 : breakChance; - } - - public int getSkillIncreasePct() - { - return skillIncrease; - } - - public Item getItem() - { - return item; - } - - public int getBreakPct() - { - return breakChance; - } - - public int getSlot() - { - return slot; - } - - public void setSlot(final int slot) - { - this.slot = slot; - } -} diff --git a/src/main/java/com/minecolonies/core/colony/buildings/workerbuildings/BuildingLibrary.java b/src/main/java/com/minecolonies/core/colony/buildings/workerbuildings/BuildingLibrary.java index 4bff7854b18..4b41e8bdbc5 100755 --- a/src/main/java/com/minecolonies/core/colony/buildings/workerbuildings/BuildingLibrary.java +++ b/src/main/java/com/minecolonies/core/colony/buildings/workerbuildings/BuildingLibrary.java @@ -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; @@ -36,25 +31,20 @@ public class BuildingLibrary extends AbstractBuilding */ private static final String LIBRARY_HUT_NAME = "library"; - /** - * List of registered barrels. - */ - private final List 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 bookCases = new ArrayList<>(); /** - * Study Item list, loaded from config, Typle First is the study chance increase,second is the breakchance + * Random obj for random calc. */ - private final List studyItems; + private final Random random = new Random(); /** * Instantiates the building. @@ -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 parseFromConfig() - { - final List 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 @@ -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) { @@ -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) { @@ -180,9 +127,4 @@ public BlockPos getRandomBookShelf() bookCases.remove(returnPos); return getPosition(); } - - public List getStudyItems() - { - return studyItems; - } } diff --git a/src/main/java/com/minecolonies/core/datalistener/StudyItemListener.java b/src/main/java/com/minecolonies/core/datalistener/StudyItemListener.java new file mode 100644 index 00000000000..4c77366563c --- /dev/null +++ b/src/main/java/com/minecolonies/core/datalistener/StudyItemListener.java @@ -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 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 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 jsonElementMap, + final @NotNull ResourceManager resourceManager, + final @NotNull ProfilerFiller profiler) + { + final Map newItems = new HashMap<>(); + for (final Map.Entry 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; + } +} diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIStudy.java b/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIStudy.java index 3b389599029..439998d5068 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIStudy.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIStudy.java @@ -1,17 +1,20 @@ package com.minecolonies.core.entity.ai.workers.education; import com.minecolonies.api.colony.ICitizenData; +import com.minecolonies.api.colony.requestsystem.requestable.StackList; import com.minecolonies.api.entity.ai.statemachine.AITarget; import com.minecolonies.api.entity.ai.statemachine.states.IAIState; -import com.minecolonies.api.entity.ai.workers.util.StudyItem; import com.minecolonies.api.util.InventoryUtils; import com.minecolonies.api.util.ItemStackUtils; import com.minecolonies.api.util.Tuple; import com.minecolonies.core.colony.buildings.workerbuildings.BuildingLibrary; import com.minecolonies.core.colony.jobs.JobStudent; +import com.minecolonies.core.datalistener.StudyItemListener; +import com.minecolonies.core.datalistener.StudyItemListener.StudyItem; import com.minecolonies.core.entity.ai.workers.AbstractEntityAISkill; import com.minecolonies.core.entity.pathfinding.navigation.PathfindingAIHelper; import net.minecraft.core.BlockPos; +import net.minecraft.resources.ResourceLocation; import net.minecraft.sounds.SoundEvents; import net.minecraft.world.InteractionHand; import net.minecraft.world.item.ItemStack; @@ -19,7 +22,9 @@ import org.jetbrains.annotations.NotNull; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import static com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState.*; import static com.minecolonies.api.util.constant.Constants.TICKS_SECOND; @@ -41,22 +46,19 @@ public class EntityAIStudy extends AbstractEntityAISkill getExpectedBuildingClass() + { + return BuildingLibrary.class; + } + @Override protected void updateRenderMetaData() { @@ -89,12 +97,6 @@ protected void updateRenderMetaData() worker.setRenderMetadata(renderMeta); } - @Override - public Class getExpectedBuildingClass() - { - return BuildingLibrary.class; - } - /** * The AI task for the student to study. For this he should walk between the different bookcase hit them once and then stand around for a while. * @@ -115,37 +117,40 @@ private IAIState study() return getState(); } + final Map studyItems = StudyItemListener.getAllStudyItems(); + // Search for Items to use to study - final List currentItems = new ArrayList<>(); - for (final StudyItem curItem : building.getStudyItems()) + final List availableItemKeys = new ArrayList<>(); + final Map availableItems = new HashMap<>(); + for (final StudyItem curItem : studyItems.values()) { - final int slot = InventoryUtils.findFirstSlotInProviderNotEmptyWith(worker, - itemStack -> !ItemStackUtils.isEmpty(itemStack) && itemStack.getItem() == curItem.getItem()); - + final int slot = InventoryUtils.findFirstSlotInProviderNotEmptyWith(worker, (item) -> item.is(curItem.item())); if (slot != -1) { - curItem.setSlot(slot); - currentItems.add(curItem); + availableItemKeys.add(curItem); + availableItems.put(curItem, slot); } } // Create a new Request for items - if (currentItems.isEmpty()) + if (availableItems.isEmpty()) { - for (final StudyItem studyItem : building.getStudyItems()) + final List itemsToRequest = new ArrayList<>(); + for (final StudyItem studyItem : studyItems.values()) { - final int bSlot = InventoryUtils.findFirstSlotInProviderWith(building, studyItem.getItem()); + final int bSlot = InventoryUtils.findFirstSlotInProviderWith(building, studyItem.item()); if (bSlot > -1) { - needsCurrently = new Tuple<>(itemStack -> studyItem.getItem() == itemStack.getItem(), 10); + needsCurrently = new Tuple<>(itemStack -> studyItem.item() == itemStack.getItem(), 10); return GATHERING_REQUIRED_MATERIALS; } - else - { - checkIfRequestForItemExistOrCreateAsync(new ItemStack(studyItem.getItem(), studyItem.getBreakPct() / 10 > 0 ? studyItem.getBreakPct() / 10 : 1)); - } + + final ItemStack itemStack = new ItemStack(studyItem.item(), studyItem.item().getDefaultInstance().getMaxStackSize()); + itemsToRequest.add(itemStack); } + checkIfRequestForItemExistOrCreate(new StackList(itemsToRequest, "Study Items", 64)); + // Default levelup data.getCitizenSkillHandler().tryLevelUpIntelligence(data.getRandom(), ONE_IN_X_CHANCE, data); worker.setItemInHand(InteractionHand.MAIN_HAND, ItemStackUtils.EMPTY); @@ -153,18 +158,19 @@ private IAIState study() // Use random item else { - final StudyItem chosenItem = currentItems.get(world.random.nextInt(currentItems.size())); + final StudyItem chosenItem = availableItemKeys.get(world.random.nextInt(availableItems.size())); + final int chosenSlot = availableItems.get(chosenItem); - worker.setItemInHand(InteractionHand.MAIN_HAND, new ItemStack(chosenItem.getItem(), 1)); - if (data.getCitizenSkillHandler().tryLevelUpIntelligence(data.getRandom(), ONE_IN_X_CHANCE * (100D / chosenItem.getSkillIncreasePct()), data)) + worker.setItemInHand(InteractionHand.MAIN_HAND, new ItemStack(chosenItem.item(), 1)); + if (data.getCitizenSkillHandler().tryLevelUpIntelligence(data.getRandom(), ONE_IN_X_CHANCE * (10D / chosenItem.skillIncreaseChance()), data)) { building.getModule(STATS_MODULE).increment(INT_LEVELED); } // Break item rand - if (world.random.nextInt(100) <= chosenItem.getBreakPct()) + if (world.random.nextInt(100) <= chosenItem.breakChance()) { - data.getInventory().extractItem(chosenItem.getSlot(), 1, false); - building.getModule(STATS_MODULE).increment(ITEM_USED + ";" + chosenItem.getItem().getDescriptionId()); + data.getInventory().extractItem(chosenSlot, 1, false); + building.getModule(STATS_MODULE).increment(ITEM_USED + ";" + chosenItem.item().getDescriptionId()); } } diff --git a/src/main/java/com/minecolonies/core/event/FMLEventHandler.java b/src/main/java/com/minecolonies/core/event/FMLEventHandler.java index ecc742de3e1..d8b690cc43c 100755 --- a/src/main/java/com/minecolonies/core/event/FMLEventHandler.java +++ b/src/main/java/com/minecolonies/core/event/FMLEventHandler.java @@ -51,6 +51,7 @@ public static void onAddReloadListenerEvent(@NotNull final AddReloadListenerEven event.addListener(new CitizenNameListener()); event.addListener(new QuestJsonListener()); event.addListener(new ItemNbtListener()); + event.addListener(new StudyItemListener()); } @SubscribeEvent diff --git a/src/main/resources/data/minecolonies/study_items/book.json b/src/main/resources/data/minecolonies/study_items/book.json new file mode 100644 index 00000000000..7491121fb42 --- /dev/null +++ b/src/main/resources/data/minecolonies/study_items/book.json @@ -0,0 +1,5 @@ +{ + "item": "minecraft:book", + "skill_increase_chance": 60, + "break_chance": 10 +} \ No newline at end of file diff --git a/src/main/resources/data/minecolonies/study_items/paper.json b/src/main/resources/data/minecolonies/study_items/paper.json new file mode 100644 index 00000000000..fe4e858d86b --- /dev/null +++ b/src/main/resources/data/minecolonies/study_items/paper.json @@ -0,0 +1,5 @@ +{ + "item": "minecraft:paper", + "skill_increase_chance": 40, + "break_chance": 100 +} \ No newline at end of file From 372f8eeca6e6b7aa87b60a819d9b5e3fbfc54d90 Mon Sep 17 00:00:00 2001 From: Thom van den Akker Date: Sun, 17 Nov 2024 16:10:32 +0100 Subject: [PATCH 2/2] Changes --- .../buildings/workerbuildings/BuildingLibrary.java | 9 ++------- .../workerbuildings/BuildingUniversity.java | 8 ++------ .../entity/ai/workers/education/EntityAIStudy.java | 12 ++++-------- 3 files changed, 8 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/minecolonies/core/colony/buildings/workerbuildings/BuildingLibrary.java b/src/main/java/com/minecolonies/core/colony/buildings/workerbuildings/BuildingLibrary.java index 4b41e8bdbc5..a1d608d3cdb 100755 --- a/src/main/java/com/minecolonies/core/colony/buildings/workerbuildings/BuildingLibrary.java +++ b/src/main/java/com/minecolonies/core/colony/buildings/workerbuildings/BuildingLibrary.java @@ -1,6 +1,7 @@ package com.minecolonies.core.colony.buildings.workerbuildings; import com.minecolonies.api.colony.IColony; +import com.minecolonies.api.util.MathUtils; import com.minecolonies.core.colony.buildings.AbstractBuilding; import com.minecolonies.core.datalistener.StudyItemListener; import net.minecraft.core.BlockPos; @@ -16,7 +17,6 @@ import java.util.ArrayList; import java.util.List; -import java.util.Random; import static com.minecolonies.api.util.constant.NbtTagConstants.TAG_BOOKCASES; import static com.minecolonies.api.util.constant.NbtTagConstants.TAG_POS; @@ -41,11 +41,6 @@ public class BuildingLibrary extends AbstractBuilding */ private final List bookCases = new ArrayList<>(); - /** - * Random obj for random calc. - */ - private final Random random = new Random(); - /** * Instantiates the building. * @@ -119,7 +114,7 @@ public BlockPos getRandomBookShelf() { return getPosition(); } - final BlockPos returnPos = bookCases.get(random.nextInt(bookCases.size())); + final BlockPos returnPos = bookCases.get(MathUtils.RANDOM.nextInt(bookCases.size())); if (colony.getWorld().getBlockState(returnPos).is(Tags.Blocks.BOOKSHELVES)) { return returnPos; diff --git a/src/main/java/com/minecolonies/core/colony/buildings/workerbuildings/BuildingUniversity.java b/src/main/java/com/minecolonies/core/colony/buildings/workerbuildings/BuildingUniversity.java index 65f45c250e3..50ce10cbcf0 100755 --- a/src/main/java/com/minecolonies/core/colony/buildings/workerbuildings/BuildingUniversity.java +++ b/src/main/java/com/minecolonies/core/colony/buildings/workerbuildings/BuildingUniversity.java @@ -5,6 +5,7 @@ import com.minecolonies.api.colony.jobs.ModJobs; import com.minecolonies.api.research.IGlobalResearchTree; import com.minecolonies.api.research.ILocalResearch; +import com.minecolonies.api.util.MathUtils; import com.minecolonies.api.util.MessageUtils; import com.minecolonies.core.colony.buildings.AbstractBuilding; import com.minecolonies.core.colony.buildings.modules.WorkerBuildingModule; @@ -50,11 +51,6 @@ public class BuildingUniversity extends AbstractBuilding */ private final List bookCases = new ArrayList<>(); - /** - * Random obj for random calc. - */ - private final Random random = new Random(); - /** * Instantiates the building. * @@ -121,7 +117,7 @@ public BlockPos getRandomBookShelf() { return getPosition(); } - final BlockPos returnPos = bookCases.get(random.nextInt(bookCases.size())); + final BlockPos returnPos = bookCases.get(MathUtils.RANDOM.nextInt(bookCases.size())); if (colony.getWorld().getBlockState(returnPos).is(Tags.Blocks.BOOKSHELVES)) { return returnPos; diff --git a/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIStudy.java b/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIStudy.java index 439998d5068..dee2f982caa 100755 --- a/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIStudy.java +++ b/src/main/java/com/minecolonies/core/entity/ai/workers/education/EntityAIStudy.java @@ -14,17 +14,13 @@ import com.minecolonies.core.entity.ai.workers.AbstractEntityAISkill; import com.minecolonies.core.entity.pathfinding.navigation.PathfindingAIHelper; import net.minecraft.core.BlockPos; -import net.minecraft.resources.ResourceLocation; import net.minecraft.sounds.SoundEvents; import net.minecraft.world.InteractionHand; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import org.jetbrains.annotations.NotNull; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import static com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState.*; import static com.minecolonies.api.util.constant.Constants.TICKS_SECOND; @@ -117,12 +113,12 @@ private IAIState study() return getState(); } - final Map studyItems = StudyItemListener.getAllStudyItems(); + final Collection studyItems = StudyItemListener.getAllStudyItems().values(); // Search for Items to use to study final List availableItemKeys = new ArrayList<>(); final Map availableItems = new HashMap<>(); - for (final StudyItem curItem : studyItems.values()) + for (final StudyItem curItem : studyItems) { final int slot = InventoryUtils.findFirstSlotInProviderNotEmptyWith(worker, (item) -> item.is(curItem.item())); if (slot != -1) @@ -136,7 +132,7 @@ private IAIState study() if (availableItems.isEmpty()) { final List itemsToRequest = new ArrayList<>(); - for (final StudyItem studyItem : studyItems.values()) + for (final StudyItem studyItem : studyItems) { final int bSlot = InventoryUtils.findFirstSlotInProviderWith(building, studyItem.item()); if (bSlot > -1)